src/HOL/Tools/ATP/atp_proof.ML
author blanchet
Fri, 17 Dec 2010 00:27:40 +0100
changeset 41470 f9783376d9b1
parent 41451 1393514094d7
child 41505 13972ced98d9
permissions -rw-r--r--
more precise/correct SMT error handling
     1 (*  Title:      HOL/Tools/ATP/atp_proof.ML
     2     Author:     Lawrence C. Paulson, Cambridge University Computer Laboratory
     3     Author:     Claire Quigley, Cambridge University Computer Laboratory
     4     Author:     Jasmin Blanchette, TU Muenchen
     5 
     6 Abstract representation of ATP proofs and TSTP/Vampire/SPASS syntax.
     7 *)
     8 
     9 signature ATP_PROOF =
    10 sig
    11   type 'a fo_term = 'a ATP_Problem.fo_term
    12   type 'a uniform_formula = 'a ATP_Problem.uniform_formula
    13 
    14   datatype failure =
    15     Unprovable | IncompleteUnprovable | CantConnect | TimedOut |
    16     OutOfResources | SpassTooOld | VampireTooOld | NoPerl | NoLibwwwPerl |
    17     NoRealZ3 | MalformedInput | MalformedOutput | Interrupted | Crashed |
    18     InternalError | UnknownError
    19 
    20   type step_name = string * string option
    21 
    22   datatype 'a step =
    23     Definition of step_name * 'a * 'a |
    24     Inference of step_name * 'a * step_name list
    25 
    26   type 'a proof = 'a uniform_formula step list
    27 
    28   val strip_spaces : (char -> bool) -> string -> string
    29   val string_for_failure : string -> failure -> string
    30   val extract_important_message : string -> string
    31   val extract_known_failure :
    32     (failure * string) list -> string -> failure option
    33   val extract_tstplike_proof_and_outcome :
    34     bool -> int -> (string * string) list -> (failure * string) list -> string
    35     -> string * failure option
    36   val is_same_step : step_name * step_name -> bool
    37   val atp_proof_from_tstplike_string : bool -> string -> string proof
    38   val map_term_names_in_atp_proof :
    39     (string -> string) -> string proof -> string proof
    40   val nasty_atp_proof : string Symtab.table -> string proof -> string proof
    41 end;
    42 
    43 structure ATP_Proof : ATP_PROOF =
    44 struct
    45 
    46 open ATP_Problem
    47 
    48 datatype failure =
    49   Unprovable | IncompleteUnprovable | CantConnect | TimedOut | OutOfResources |
    50   SpassTooOld | VampireTooOld | NoPerl | NoLibwwwPerl | NoRealZ3 |
    51   MalformedInput | MalformedOutput | Interrupted | Crashed | InternalError |
    52   UnknownError
    53 
    54 fun strip_spaces_in_list _ [] = []
    55   | strip_spaces_in_list _ [c1] = if Char.isSpace c1 then [] else [str c1]
    56   | strip_spaces_in_list is_evil [c1, c2] =
    57     strip_spaces_in_list is_evil [c1] @ strip_spaces_in_list is_evil [c2]
    58   | strip_spaces_in_list is_evil (c1 :: c2 :: c3 :: cs) =
    59     if Char.isSpace c1 then
    60       strip_spaces_in_list is_evil (c2 :: c3 :: cs)
    61     else if Char.isSpace c2 then
    62       if Char.isSpace c3 then
    63         strip_spaces_in_list is_evil (c1 :: c3 :: cs)
    64       else
    65         str c1 :: (if forall is_evil [c1, c3] then [" "] else []) @
    66         strip_spaces_in_list is_evil (c3 :: cs)
    67     else
    68       str c1 :: strip_spaces_in_list is_evil (c2 :: c3 :: cs)
    69 fun strip_spaces is_evil =
    70   implode o strip_spaces_in_list is_evil o String.explode
    71 
    72 fun is_ident_char c = Char.isAlphaNum c orelse c = #"_"
    73 val strip_spaces_except_between_ident_chars = strip_spaces is_ident_char
    74 
    75 fun missing_message_tail prover =
    76   " appears to be missing. You will need to install it if you want to run " ^
    77   prover ^ "s remotely."
    78 
    79 fun string_for_failure prover Unprovable =
    80     "The " ^ prover ^ " problem is unprovable."
    81   | string_for_failure prover IncompleteUnprovable =
    82     "The " ^ prover ^ " cannot prove the problem."
    83   | string_for_failure _ CantConnect = "Cannot connect to remote server."
    84   | string_for_failure _ TimedOut = "Timed out."
    85   | string_for_failure prover OutOfResources =
    86     "The " ^ prover ^ " ran out of resources."
    87   | string_for_failure _ SpassTooOld =
    88     "Isabelle requires a more recent version of SPASS with support for the \
    89     \TPTP syntax. To install it, download and extract the package \
    90     \\"http://isabelle.in.tum.de/dist/contrib/spass-3.7.tar.gz\" and add the \
    91     \\"spass-3.7\" directory's absolute path to " ^
    92     quote (Path.implode (Path.expand (Path.appends
    93                (Path.variable "ISABELLE_HOME_USER" ::
    94                 map Path.basic ["etc", "components"])))) ^
    95     " on a line of its own."
    96   | string_for_failure _ VampireTooOld =
    97     "Isabelle requires a more recent version of Vampire. To install it, follow \
    98     \the instructions from the Sledgehammer manual (\"isabelle doc\
    99     \ sledgehammer\")."
   100   | string_for_failure prover NoPerl = "Perl" ^ missing_message_tail prover
   101   | string_for_failure prover NoLibwwwPerl =
   102     "The Perl module \"libwww-perl\"" ^ missing_message_tail prover
   103   | string_for_failure prover NoRealZ3 =
   104     "The environment variable \"Z3_REAL_SOLVER\" must be set to Z3's full path."
   105   | string_for_failure prover MalformedInput =
   106     "The " ^ prover ^ " problem is malformed. Please report this to the \
   107     \Isabelle developers."
   108   | string_for_failure prover MalformedOutput =
   109     "The " ^ prover ^ " output is malformed."
   110   | string_for_failure prover Interrupted =
   111     "The " ^ prover ^ " was interrupted."
   112   | string_for_failure prover Crashed = "The " ^ prover ^ " crashed."
   113   | string_for_failure prover InternalError =
   114     "An internal " ^ prover ^ " error occurred."
   115   | string_for_failure prover UnknownError =
   116     (* "An" is correct for "ATP" and "SMT". *)
   117     "An " ^ prover ^ " error occurred."
   118 
   119 fun extract_delimited (begin_delim, end_delim) output =
   120   output |> first_field begin_delim |> the |> snd
   121          |> first_field end_delim |> the |> fst
   122          |> first_field "\n" |> the |> snd
   123   handle Option.Option => ""
   124 
   125 val tstp_important_message_delims =
   126   ("% SZS start RequiredInformation", "% SZS end RequiredInformation")
   127 
   128 fun extract_important_message output =
   129   case extract_delimited tstp_important_message_delims output of
   130     "" => ""
   131   | s => s |> space_explode "\n" |> filter_out (curry (op =) "")
   132            |> map (perhaps (try (unprefix "%")))
   133            |> map (perhaps (try (unprefix " ")))
   134            |> space_implode "\n " |> quote
   135 
   136 (* Splits by the first possible of a list of delimiters. *)
   137 fun extract_tstplike_proof delims output =
   138   case pairself (find_first (fn s => String.isSubstring s output))
   139                 (ListPair.unzip delims) of
   140     (SOME begin_delim, SOME end_delim) =>
   141     extract_delimited (begin_delim, end_delim) output
   142   | _ => ""
   143 
   144 fun extract_known_failure known_failures output =
   145   known_failures
   146   |> find_first (fn (_, pattern) => String.isSubstring pattern output)
   147   |> Option.map fst
   148 
   149 fun extract_tstplike_proof_and_outcome complete res_code proof_delims
   150                                        known_failures output =
   151   case extract_known_failure known_failures output of
   152     NONE => (case extract_tstplike_proof proof_delims output of
   153              "" => ("", SOME (if res_code = 0 andalso output = "" then
   154                                 Interrupted
   155                               else
   156                                 UnknownError))
   157            | tstplike_proof => if res_code = 0 then (tstplike_proof, NONE)
   158                                else ("", SOME UnknownError))
   159   | SOME failure =>
   160     ("", SOME (if failure = IncompleteUnprovable andalso complete then
   161                  Unprovable
   162                else
   163                  failure))
   164 
   165 fun mk_anot (AConn (ANot, [phi])) = phi
   166   | mk_anot phi = AConn (ANot, [phi])
   167 fun mk_aconn c (phi1, phi2) = AConn (c, [phi1, phi2])
   168 
   169 type step_name = string * string option
   170 
   171 fun is_same_step p = p |> pairself fst |> op =
   172 
   173 fun step_name_ord p =
   174   let val q = pairself fst p in
   175     (* The "unprefix" part is to cope with remote Vampire's output. The proper
   176        solution would be to perform a topological sort, e.g. using the nice
   177        "Graph" functor. *)
   178     case pairself (Int.fromString o perhaps (try (unprefix "f"))) q of
   179       (NONE, NONE) => string_ord q
   180     | (NONE, SOME _) => LESS
   181     | (SOME _, NONE) => GREATER
   182     | (SOME i, SOME j) => int_ord (i, j)
   183   end
   184 
   185 datatype 'a step =
   186   Definition of step_name * 'a * 'a |
   187   Inference of step_name * 'a * step_name list
   188 
   189 type 'a proof = 'a uniform_formula step list
   190 
   191 fun step_name (Definition (name, _, _)) = name
   192   | step_name (Inference (name, _, _)) = name
   193 
   194 (**** PARSING OF TSTP FORMAT ****)
   195 
   196 (*Strings enclosed in single quotes, e.g. filenames*)
   197 val scan_general_id =
   198   $$ "'" |-- Scan.repeat (~$$ "'") --| $$ "'" >> implode
   199   || Scan.repeat ($$ "$") -- Scan.many1 Symbol.is_letdig
   200      >> (fn (ss1, ss2) => implode ss1 ^ implode ss2)
   201 
   202 (* Generalized first-order terms, which include file names, numbers, etc. *)
   203 fun parse_annotation strict x =
   204   ((scan_general_id ::: Scan.repeat ($$ " " |-- scan_general_id)
   205       >> (strict ? filter (is_some o Int.fromString)))
   206    -- Scan.optional (parse_annotation strict) [] >> op @
   207    || $$ "(" |-- parse_annotations strict --| $$ ")"
   208    || $$ "[" |-- parse_annotations strict --| $$ "]") x
   209 and parse_annotations strict x =
   210   (Scan.optional (parse_annotation strict
   211                   ::: Scan.repeat ($$ "," |-- parse_annotation strict)) []
   212    >> flat) x
   213 
   214 (* Vampire proof lines sometimes contain needless information such as "(0:3)",
   215    which can be hard to disambiguate from function application in an LL(1)
   216    parser. As a workaround, we extend the TPTP term syntax with such detritus
   217    and ignore it. *)
   218 fun parse_vampire_detritus x =
   219   (scan_general_id |-- $$ ":" --| scan_general_id >> K []) x
   220 
   221 fun parse_term x =
   222   (scan_general_id
   223      -- Scan.optional ($$ "(" |-- (parse_vampire_detritus || parse_terms)
   224                        --| $$ ")") []
   225      --| Scan.optional ($$ "(" |-- parse_vampire_detritus --| $$ ")") []
   226    >> ATerm) x
   227 and parse_terms x = (parse_term ::: Scan.repeat ($$ "," |-- parse_term)) x
   228 
   229 fun parse_atom x =
   230   (parse_term -- Scan.option (Scan.option ($$ "!") --| $$ "=" -- parse_term)
   231    >> (fn (u1, NONE) => AAtom u1
   232         | (u1, SOME (NONE, u2)) => AAtom (ATerm ("c_equal", [u1, u2]))
   233         | (u1, SOME (SOME _, u2)) =>
   234           mk_anot (AAtom (ATerm ("c_equal", [u1, u2]))))) x
   235 
   236 fun fo_term_head (ATerm (s, _)) = s
   237 
   238 (* TPTP formulas are fully parenthesized, so we don't need to worry about
   239    operator precedence. *)
   240 fun parse_formula x =
   241   (($$ "(" |-- parse_formula --| $$ ")"
   242     || ($$ "!" >> K AForall || $$ "?" >> K AExists)
   243        --| $$ "[" -- parse_terms --| $$ "]" --| $$ ":" -- parse_formula
   244        >> (fn ((q, ts), phi) => AQuant (q, map fo_term_head ts, phi))
   245     || $$ "~" |-- parse_formula >> mk_anot
   246     || parse_atom)
   247    -- Scan.option ((Scan.this_string "=>" >> K AImplies
   248                     || Scan.this_string "<=>" >> K AIff
   249                     || Scan.this_string "<~>" >> K ANotIff
   250                     || Scan.this_string "<=" >> K AIf
   251                     || $$ "|" >> K AOr || $$ "&" >> K AAnd)
   252                    -- parse_formula)
   253    >> (fn (phi1, NONE) => phi1
   254         | (phi1, SOME (c, phi2)) => mk_aconn c (phi1, phi2))) x
   255 
   256 val parse_tstp_extra_arguments =
   257   Scan.optional ($$ "," |-- parse_annotation false
   258                  --| Scan.option ($$ "," |-- parse_annotations false)) []
   259 
   260 val vampire_unknown_fact = "unknown"
   261 
   262 (* Syntax: (fof|cnf)\(<num>, <formula_role>, <formula> <extra_arguments>\).
   263    The <num> could be an identifier, but we assume integers. *)
   264 val parse_tstp_line =
   265   ((Scan.this_string "fof" || Scan.this_string "cnf") -- $$ "(")
   266     |-- scan_general_id --| $$ "," -- Symbol.scan_id --| $$ ","
   267     -- parse_formula -- parse_tstp_extra_arguments --| $$ ")" --| $$ "."
   268    >> (fn (((num, role), phi), deps) =>
   269           let
   270             val (name, deps) =
   271               case deps of
   272                 ["file", _, s] =>
   273                 ((num, if s = vampire_unknown_fact then NONE else SOME s), [])
   274               | _ => ((num, NONE), deps)
   275           in
   276             case role of
   277               "definition" =>
   278               (case phi of
   279                  AConn (AIff, [phi1 as AAtom _, phi2]) =>
   280                  Definition (name, phi1, phi2)
   281                | AAtom (ATerm ("c_equal", _)) =>
   282                  (* Vampire's equality proxy axiom *)
   283                  Inference (name, phi, map (rpair NONE) deps)
   284                | _ => raise Fail "malformed definition")
   285             | _ => Inference (name, phi, map (rpair NONE) deps)
   286           end)
   287 
   288 (**** PARSING OF VAMPIRE OUTPUT ****)
   289 
   290 val parse_vampire_braced_stuff =
   291   $$ "{" -- Scan.repeat (scan_general_id --| Scan.option ($$ ",")) -- $$ "}"
   292 val parse_vampire_parenthesized_detritus =
   293   $$ "(" |-- parse_vampire_detritus --| $$ ")"
   294 
   295 (* Syntax: <num>. <formula> <annotation> *)
   296 val parse_vampire_line =
   297   scan_general_id --| $$ "." -- parse_formula
   298     --| Scan.option parse_vampire_braced_stuff
   299     --| Scan.option parse_vampire_parenthesized_detritus
   300     -- parse_annotation true
   301   >> (fn ((num, phi), deps) =>
   302          Inference ((num, NONE), phi, map (rpair NONE) deps))
   303 
   304 (**** PARSING OF SPASS OUTPUT ****)
   305 
   306 (* SPASS returns clause references of the form "x.y". We ignore "y", whose role
   307    is not clear anyway. *)
   308 val parse_dot_name = scan_general_id --| $$ "." --| scan_general_id
   309 
   310 val parse_spass_annotations =
   311   Scan.optional ($$ ":" |-- Scan.repeat (parse_dot_name
   312                                          --| Scan.option ($$ ","))) []
   313 
   314 (* It is not clear why some literals are followed by sequences of stars and/or
   315    pluses. We ignore them. *)
   316 fun parse_decorated_atom x =
   317   (parse_atom --| Scan.repeat ($$ "*" || $$ "+" || $$ " ")) x
   318 
   319 fun mk_horn ([], []) = AAtom (ATerm ("c_False", []))
   320   | mk_horn ([], pos_lits) = foldr1 (mk_aconn AOr) pos_lits
   321   | mk_horn (neg_lits, []) = mk_anot (foldr1 (mk_aconn AAnd) neg_lits)
   322   | mk_horn (neg_lits, pos_lits) =
   323     mk_aconn AImplies (foldr1 (mk_aconn AAnd) neg_lits,
   324                        foldr1 (mk_aconn AOr) pos_lits)
   325 
   326 fun parse_horn_clause x =
   327   (Scan.repeat parse_decorated_atom --| $$ "|" --| $$ "|"
   328      -- Scan.repeat parse_decorated_atom --| $$ "-" --| $$ ">"
   329      -- Scan.repeat parse_decorated_atom
   330    >> (mk_horn o apfst (op @))) x
   331 
   332 (* Syntax: <num>[0:<inference><annotations>]
   333    <atoms> || <atoms> -> <atoms>. *)
   334 fun parse_spass_line x =
   335   (scan_general_id --| $$ "[" --| $$ "0" --| $$ ":" --| Symbol.scan_id
   336      -- parse_spass_annotations --| $$ "]" -- parse_horn_clause --| $$ "."
   337    >> (fn ((num, deps), u) =>
   338           Inference ((num, NONE), u, map (rpair NONE) deps))) x
   339 
   340 fun parse_line x = (parse_tstp_line || parse_vampire_line || parse_spass_line) x
   341 val parse_proof =
   342   fst o Scan.finite Symbol.stopper
   343             (Scan.error (!! (fn _ => raise Fail "unrecognized ATP output")
   344                             (Scan.repeat1 parse_line)))
   345   o raw_explode o strip_spaces_except_between_ident_chars
   346 
   347 fun clean_up_dependency seen dep = find_first (curry is_same_step dep) seen
   348 fun clean_up_dependencies _ [] = []
   349   | clean_up_dependencies seen ((step as Definition (name, _, _)) :: steps) =
   350     step :: clean_up_dependencies (name :: seen) steps
   351   | clean_up_dependencies seen (Inference (name, u, deps) :: steps) =
   352     Inference (name, u, map_filter (clean_up_dependency seen) deps) ::
   353     clean_up_dependencies (name :: seen) steps
   354 
   355 fun atp_proof_from_tstplike_string clean =
   356   suffix "$" (* the $ sign acts as a sentinel (FIXME: needed?) *)
   357   #> parse_proof
   358   #> clean ? (sort (step_name_ord o pairself step_name)
   359               #> clean_up_dependencies [])
   360 
   361 fun map_term_names_in_term f (ATerm (s, ts)) =
   362   ATerm (f s, map (map_term_names_in_term f) ts)
   363 fun map_term_names_in_formula f (AQuant (q, xs, phi)) =
   364     AQuant (q, xs, map_term_names_in_formula f phi)
   365   | map_term_names_in_formula f (AConn (c, phis)) =
   366     AConn (c, map (map_term_names_in_formula f) phis)
   367   | map_term_names_in_formula f (AAtom t) = AAtom (map_term_names_in_term f t)
   368 fun map_term_names_in_step f (Definition (name, phi1, phi2)) =
   369     Definition (name, map_term_names_in_formula f phi1,
   370                 map_term_names_in_formula f phi2)
   371   | map_term_names_in_step f (Inference (name, phi, deps)) =
   372     Inference (name, map_term_names_in_formula f phi, deps)
   373 fun map_term_names_in_atp_proof f = map (map_term_names_in_step f)
   374 
   375 fun nasty_name pool s = s |> Symtab.lookup pool |> the_default s
   376 fun nasty_atp_proof pool =
   377   if Symtab.is_empty pool then I
   378   else map_term_names_in_atp_proof (nasty_name pool)
   379 
   380 end;