src/HOL/Tools/Sledgehammer/sledgehammer_proof_reconstruct.ML
author blanchet
Fri Apr 16 15:49:13 2010 +0200 (2010-04-16)
changeset 36170 0cdb76723c88
parent 36140 08b2a7ecb6c3
child 36223 217ca1273786
permissions -rw-r--r--
added original constant names to Sledgehammer internal terms + output short names if "debug" is set (for increased readability)
     1 (*  Title:      HOL/Tools/Sledgehammer/sledgehammer_proof_reconstruct.ML
     2     Author:     Lawrence C Paulson and Claire Quigley, Cambridge University Computer Laboratory
     3 
     4 Transfer of proofs from external provers.
     5 *)
     6 
     7 signature SLEDGEHAMMER_PROOF_RECONSTRUCT =
     8 sig
     9   val chained_hint: string
    10   val invert_const: string -> string
    11   val invert_type_const: string -> string
    12   val num_typargs: theory -> string -> int
    13   val make_tvar: string -> typ
    14   val strip_prefix: string -> string -> string option
    15   val is_proof_well_formed: string -> bool
    16   val metis_line: int -> int -> string list -> string
    17   val metis_lemma_list: bool -> string ->
    18     string * string vector * (int * int) * Proof.context * thm * int -> string * string list
    19   val structured_isar_proof: int -> bool -> string ->
    20     string * string vector * (int * int) * Proof.context * thm * int -> string * string list
    21 end;
    22 
    23 structure Sledgehammer_Proof_Reconstruct : SLEDGEHAMMER_PROOF_RECONSTRUCT =
    24 struct
    25 
    26 open Sledgehammer_FOL_Clause
    27 open Sledgehammer_Fact_Preprocessor
    28 
    29 val trace_proof_path = Path.basic "atp_trace";
    30 
    31 fun trace_proof_msg f =
    32   if !trace then File.append (File.tmp_path trace_proof_path) (f ()) else ();
    33 
    34 fun string_of_thm ctxt = PrintMode.setmp [] (Display.string_of_thm ctxt);
    35 
    36 (**** PARSING OF TSTP FORMAT ****)
    37 
    38 (*Syntax trees, either termlist or formulae*)
    39 datatype stree = Int of int | Br of string * stree list;
    40 
    41 fun atom x = Br(x,[]);
    42 
    43 fun scons (x,y) = Br("cons", [x,y]);
    44 val listof = List.foldl scons (atom "nil");
    45 
    46 (*Strings enclosed in single quotes, e.g. filenames*)
    47 val quoted = $$"'" |-- Scan.repeat (~$$"'") --| $$"'" >> implode;
    48 
    49 (*Intended for $true and $false*)
    50 fun tf s = "c_" ^ str (Char.toUpper (String.sub(s,0))) ^ String.extract(s,1,NONE);
    51 val truefalse = $$"$" |-- Symbol.scan_id >> (atom o tf);
    52 
    53 (*Integer constants, typically proof line numbers*)
    54 fun is_digit s = Char.isDigit (String.sub(s,0));
    55 val integer = Scan.many1 is_digit >> (the o Int.fromString o implode);
    56 
    57 (*Generalized FO terms, which include filenames, numbers, etc.*)
    58 fun termlist x = (term ::: Scan.repeat ($$"," |-- term)) x
    59 and term x = (quoted >> atom || integer>>Int || truefalse ||
    60               Symbol.scan_id -- Scan.optional ($$"(" |-- termlist --| $$")") [] >> Br ||
    61               $$"(" |-- term --| $$")" ||
    62               $$"[" |-- Scan.optional termlist [] --| $$"]" >> listof) x;
    63 
    64 fun negate t = Br("c_Not", [t]);
    65 fun equate (t1,t2) = Br("c_equal", [t1,t2]);
    66 
    67 (*Apply equal or not-equal to a term*)
    68 fun syn_equal (t, NONE) = t
    69   | syn_equal (t1, SOME (NONE, t2)) = equate (t1,t2)
    70   | syn_equal (t1, SOME (SOME _, t2)) = negate (equate (t1,t2));
    71 
    72 (*Literals can involve negation, = and !=.*)
    73 fun literal x = ($$"~" |-- literal >> negate ||
    74                  (term -- Scan.option (Scan.option ($$"!") --| $$"=" -- term) >> syn_equal)) x;
    75 
    76 val literals = literal ::: Scan.repeat ($$"|" |-- literal);
    77 
    78 (*Clause: a list of literals separated by the disjunction sign*)
    79 val clause = $$"(" |-- literals --| $$")" || Scan.single literal;
    80 
    81 val annotations = $$"," |-- term -- Scan.option ($$"," |-- termlist);
    82 
    83 (*<cnf_annotated> ::= cnf(<name>,<formula_role>,<cnf_formula><annotations>).
    84   The <name> could be an identifier, but we assume integers.*)
    85 val tstp_line = (Scan.this_string "cnf" -- $$"(") |--
    86                 integer --| $$"," -- Symbol.scan_id --| $$"," --
    87                 clause -- Scan.option annotations --| $$ ")";
    88 
    89 
    90 (**** INTERPRETATION OF TSTP SYNTAX TREES ****)
    91 
    92 exception STREE of stree;
    93 
    94 (*If string s has the prefix s1, return the result of deleting it.*)
    95 fun strip_prefix s1 s =
    96   if String.isPrefix s1 s
    97   then SOME (undo_ascii_of (String.extract (s, size s1, NONE)))
    98   else NONE;
    99 
   100 (*Invert the table of translations between Isabelle and ATPs*)
   101 val type_const_trans_table_inv =
   102       Symtab.make (map swap (Symtab.dest type_const_trans_table));
   103 
   104 fun invert_type_const c =
   105     case Symtab.lookup type_const_trans_table_inv c of
   106         SOME c' => c'
   107       | NONE => c;
   108 
   109 fun make_tvar b = TVar(("'" ^ b, 0), HOLogic.typeS);
   110 fun make_var (b,T) = Var((b,0),T);
   111 
   112 (*Type variables are given the basic sort, HOL.type. Some will later be constrained
   113   by information from type literals, or by type inference.*)
   114 fun type_of_stree t =
   115   case t of
   116       Int _ => raise STREE t
   117     | Br (a,ts) =>
   118         let val Ts = map type_of_stree ts
   119         in
   120           case strip_prefix tconst_prefix a of
   121               SOME b => Type(invert_type_const b, Ts)
   122             | NONE =>
   123                 if not (null ts) then raise STREE t  (*only tconsts have type arguments*)
   124                 else
   125                 case strip_prefix tfree_prefix a of
   126                     SOME b => TFree("'" ^ b, HOLogic.typeS)
   127                   | NONE =>
   128                 case strip_prefix tvar_prefix a of
   129                     SOME b => make_tvar b
   130                   | NONE => make_tvar a   (*Variable from the ATP, say X1*)
   131         end;
   132 
   133 (*Invert the table of translations between Isabelle and ATPs*)
   134 val const_trans_table_inv =
   135       Symtab.update ("fequal", "op =")
   136         (Symtab.make (map swap (Symtab.dest const_trans_table)));
   137 
   138 fun invert_const c =
   139     case Symtab.lookup const_trans_table_inv c of
   140         SOME c' => c'
   141       | NONE => c;
   142 
   143 (*The number of type arguments of a constant, zero if it's monomorphic*)
   144 fun num_typargs thy s = length (Sign.const_typargs thy (s, Sign.the_const_type thy s));
   145 
   146 (*Generates a constant, given its type arguments*)
   147 fun const_of thy (a,Ts) = Const(a, Sign.const_instance thy (a,Ts));
   148 
   149 (*First-order translation. No types are known for variables. HOLogic.typeT should allow
   150   them to be inferred.*)
   151 fun term_of_stree args thy t =
   152   case t of
   153       Int _ => raise STREE t
   154     | Br ("hBOOL",[t]) => term_of_stree [] thy t  (*ignore hBOOL*)
   155     | Br ("hAPP",[t,u]) => term_of_stree (u::args) thy t
   156     | Br (a,ts) =>
   157         case strip_prefix const_prefix a of
   158             SOME "equal" =>
   159               list_comb(Const (@{const_name "op ="}, HOLogic.typeT), List.map (term_of_stree [] thy) ts)
   160           | SOME b =>
   161               let val c = invert_const b
   162                   val nterms = length ts - num_typargs thy c
   163                   val us = List.map (term_of_stree [] thy) (List.take(ts,nterms) @ args)
   164                   (*Extra args from hAPP come AFTER any arguments given directly to the
   165                     constant.*)
   166                   val Ts = List.map type_of_stree (List.drop(ts,nterms))
   167               in  list_comb(const_of thy (c, Ts), us)  end
   168           | NONE => (*a variable, not a constant*)
   169               let val T = HOLogic.typeT
   170                   val opr = (*a Free variable is typically a Skolem function*)
   171                     case strip_prefix fixed_var_prefix a of
   172                         SOME b => Free(b,T)
   173                       | NONE =>
   174                     case strip_prefix schematic_var_prefix a of
   175                         SOME b => make_var (b,T)
   176                       | NONE => make_var (a,T)    (*Variable from the ATP, say X1*)
   177               in  list_comb (opr, List.map (term_of_stree [] thy) (ts@args))  end;
   178 
   179 (*Type class literal applied to a type. Returns triple of polarity, class, type.*)
   180 fun constraint_of_stree pol (Br("c_Not",[t])) = constraint_of_stree (not pol) t
   181   | constraint_of_stree pol t = case t of
   182         Int _ => raise STREE t
   183       | Br (a,ts) =>
   184             (case (strip_prefix class_prefix a, map type_of_stree ts) of
   185                  (SOME b, [T]) => (pol, b, T)
   186                | _ => raise STREE t);
   187 
   188 (** Accumulate type constraints in a clause: negative type literals **)
   189 
   190 fun addix (key,z)  = Vartab.map_default (key,[]) (cons z);
   191 
   192 fun add_constraint ((false, cl, TFree(a,_)), vt) = addix ((a,~1),cl) vt
   193   | add_constraint ((false, cl, TVar(ix,_)), vt) = addix (ix,cl) vt
   194   | add_constraint (_, vt) = vt;
   195 
   196 (*False literals (which E includes in its proofs) are deleted*)
   197 val nofalses = filter (not o equal HOLogic.false_const);
   198 
   199 (*Final treatment of the list of "real" literals from a clause.*)
   200 fun finish [] = HOLogic.true_const  (*No "real" literals means only type information*)
   201   | finish lits =
   202       case nofalses lits of
   203           [] => HOLogic.false_const  (*The empty clause, since we started with real literals*)
   204         | xs => foldr1 HOLogic.mk_disj (rev xs);
   205 
   206 (*Accumulate sort constraints in vt, with "real" literals in lits.*)
   207 fun lits_of_strees _ (vt, lits) [] = (vt, finish lits)
   208   | lits_of_strees ctxt (vt, lits) (t::ts) =
   209       lits_of_strees ctxt (add_constraint (constraint_of_stree true t, vt), lits) ts
   210       handle STREE _ =>
   211       lits_of_strees ctxt (vt, term_of_stree [] (ProofContext.theory_of ctxt) t :: lits) ts;
   212 
   213 (*Update TVars/TFrees with detected sort constraints.*)
   214 fun fix_sorts vt =
   215   let fun tysubst (Type (a, Ts)) = Type (a, map tysubst Ts)
   216         | tysubst (TVar (xi, s)) = TVar (xi, the_default s (Vartab.lookup vt xi))
   217         | tysubst (TFree (x, s)) = TFree (x, the_default s (Vartab.lookup vt (x, ~1)))
   218       fun tmsubst (Const (a, T)) = Const (a, tysubst T)
   219         | tmsubst (Free (a, T)) = Free (a, tysubst T)
   220         | tmsubst (Var (xi, T)) = Var (xi, tysubst T)
   221         | tmsubst (t as Bound _) = t
   222         | tmsubst (Abs (a, T, t)) = Abs (a, tysubst T, tmsubst t)
   223         | tmsubst (t $ u) = tmsubst t $ tmsubst u;
   224   in fn t => if Vartab.is_empty vt then t else tmsubst t end;
   225 
   226 (*Interpret a list of syntax trees as a clause, given by "real" literals and sort constraints.
   227   vt0 holds the initial sort constraints, from the conjecture clauses.*)
   228 fun clause_of_strees ctxt vt0 ts =
   229   let val (vt, dt) = lits_of_strees ctxt (vt0,[]) ts in
   230     singleton (Syntax.check_terms ctxt) (TypeInfer.constrain HOLogic.boolT (fix_sorts vt dt))
   231   end;
   232 
   233 fun gen_all_vars t = fold_rev Logic.all (OldTerm.term_vars t) t;
   234 
   235 fun ints_of_stree_aux (Int n, ns) = n::ns
   236   | ints_of_stree_aux (Br(_,ts), ns) = List.foldl ints_of_stree_aux ns ts;
   237 
   238 fun ints_of_stree t = ints_of_stree_aux (t, []);
   239 
   240 fun decode_tstp vt0 (name, role, ts, annots) ctxt =
   241   let val deps = case annots of NONE => [] | SOME (source,_) => ints_of_stree source
   242       val cl = clause_of_strees ctxt vt0 ts
   243   in  ((name, role, cl, deps), fold Variable.declare_term (OldTerm.term_frees cl) ctxt)  end;
   244 
   245 fun dest_tstp ((((name, role), ts), annots), chs) =
   246   case chs of
   247           "."::_ => (name, role, ts, annots)
   248         | _ => error ("TSTP line not terminated by \".\": " ^ implode chs);
   249 
   250 
   251 (** Global sort constraints on TFrees (from tfree_tcs) are positive unit clauses. **)
   252 
   253 fun add_tfree_constraint ((true, cl, TFree(a,_)), vt) = addix ((a,~1),cl) vt
   254   | add_tfree_constraint (_, vt) = vt;
   255 
   256 fun tfree_constraints_of_clauses vt [] = vt
   257   | tfree_constraints_of_clauses vt ([lit]::tss) =
   258       (tfree_constraints_of_clauses (add_tfree_constraint (constraint_of_stree true lit, vt)) tss
   259        handle STREE _ => (*not a positive type constraint: ignore*)
   260        tfree_constraints_of_clauses vt tss)
   261   | tfree_constraints_of_clauses vt (_::tss) = tfree_constraints_of_clauses vt tss;
   262 
   263 
   264 (**** Translation of TSTP files to Isar Proofs ****)
   265 
   266 fun decode_tstp_list ctxt tuples =
   267   let val vt0 = tfree_constraints_of_clauses Vartab.empty (map #3 tuples)
   268   in  #1 (fold_map (decode_tstp vt0) tuples ctxt) end;
   269 
   270 (** Finding a matching assumption. The literals may be permuted, and variable names
   271     may disagree. We have to try all combinations of literals (quadratic!) and
   272     match up the variable names consistently. **)
   273 
   274 fun strip_alls_aux n (Const(@{const_name all}, _)$Abs(a,T,t))  =
   275       strip_alls_aux (n+1) (subst_bound (Var ((a,n), T), t))
   276   | strip_alls_aux _ t  =  t;
   277 
   278 val strip_alls = strip_alls_aux 0;
   279 
   280 exception MATCH_LITERAL;
   281 
   282 (*Ignore types: they are not to be trusted...*)
   283 fun match_literal (t1$u1) (t2$u2) env =
   284       match_literal t1 t2 (match_literal u1 u2 env)
   285   | match_literal (Abs (_,_,t1)) (Abs (_,_,t2)) env =
   286       match_literal t1 t2 env
   287   | match_literal (Bound i1) (Bound i2) env =
   288       if i1=i2 then env else raise MATCH_LITERAL
   289   | match_literal (Const(a1,_)) (Const(a2,_)) env =
   290       if a1=a2 then env else raise MATCH_LITERAL
   291   | match_literal (Free(a1,_)) (Free(a2,_)) env =
   292       if a1=a2 then env else raise MATCH_LITERAL
   293   | match_literal (Var(ix1,_)) (Var(ix2,_)) env = insert (op =) (ix1,ix2) env
   294   | match_literal _ _ _ = raise MATCH_LITERAL;
   295 
   296 (*Checking that all variable associations are unique. The list env contains no
   297   repetitions, but does it contain say (x,y) and (y,y)? *)
   298 fun good env =
   299   let val (xs,ys) = ListPair.unzip env
   300   in  not (has_duplicates (op=) xs orelse has_duplicates (op=) ys)  end;
   301 
   302 (*Match one list of literals against another, ignoring types and the order of
   303   literals. Sorting is unreliable because we don't have types or variable names.*)
   304 fun matches_aux _ [] [] = true
   305   | matches_aux env (lit::lits) ts =
   306       let fun match1 us [] = false
   307             | match1 us (t::ts) =
   308                 let val env' = match_literal lit t env
   309                 in  (good env' andalso matches_aux env' lits (us@ts)) orelse
   310                     match1 (t::us) ts
   311                 end
   312                 handle MATCH_LITERAL => match1 (t::us) ts
   313       in  match1 [] ts  end;
   314 
   315 (*Is this length test useful?*)
   316 fun matches (lits1,lits2) =
   317   length lits1 = length lits2  andalso
   318   matches_aux [] (map Envir.eta_contract lits1) (map Envir.eta_contract lits2);
   319 
   320 fun permuted_clause t =
   321   let val lits = HOLogic.disjuncts t
   322       fun perm [] = NONE
   323         | perm (ctm::ctms) =
   324             if matches (lits, HOLogic.disjuncts (HOLogic.dest_Trueprop (strip_alls ctm)))
   325             then SOME ctm else perm ctms
   326   in perm end;
   327 
   328 (*ctms is a list of conjecture clauses as yielded by Isabelle. Those returned by the
   329   ATP may have their literals reordered.*)
   330 fun isar_proof_body ctxt sorts ctms =
   331   let
   332     val _ = trace_proof_msg (K "\n\nisar_proof_body: start\n")
   333     val string_of_term = 
   334       PrintMode.setmp (filter (curry (op =) Symbol.xsymbolsN)
   335                               (print_mode_value ()))
   336                       (Syntax.string_of_term ctxt)
   337     fun have_or_show "show" _ = "  show \""
   338       | have_or_show have lname = "  " ^ have ^ " " ^ lname ^ ": \""
   339     fun do_line _ (lname, t, []) =
   340        (* No deps: it's a conjecture clause, with no proof. *)
   341        (case permuted_clause t ctms of
   342           SOME u => "  assume " ^ lname ^ ": \"" ^ string_of_term u ^ "\"\n"
   343         | NONE => raise TERM ("Sledgehammer_Proof_Reconstruct.isar_proof_body",
   344                               [t]))
   345       | do_line have (lname, t, deps) =
   346         have_or_show have lname ^
   347         string_of_term (gen_all_vars (HOLogic.mk_Trueprop t)) ^
   348         "\"\n    by (metis " ^ space_implode " " deps ^ ")\n"
   349     fun do_lines [(lname, t, deps)] = [do_line "show" (lname, t, deps)]
   350       | do_lines ((lname, t, deps) :: lines) =
   351         do_line "have" (lname, t, deps) :: do_lines lines
   352   in setmp_CRITICAL show_sorts sorts do_lines end;
   353 
   354 fun unequal t (_, t', _) = not (t aconv t');
   355 
   356 (*No "real" literals means only type information*)
   357 fun eq_types t = t aconv HOLogic.true_const;
   358 
   359 fun replace_dep (old:int, new) dep = if dep=old then new else [dep];
   360 
   361 fun replace_deps (old:int, new) (lno, t, deps) =
   362       (lno, t, List.foldl (uncurry (union (op =))) [] (map (replace_dep (old, new)) deps));
   363 
   364 (*Discard axioms; consolidate adjacent lines that prove the same clause, since they differ
   365   only in type information.*)
   366 fun add_prfline ((lno, "axiom", t, []), lines) =  (*axioms are not proof lines*)
   367       if eq_types t (*must be clsrel/clsarity: type information, so delete refs to it*)
   368       then map (replace_deps (lno, [])) lines
   369       else
   370        (case take_prefix (unequal t) lines of
   371            (_,[]) => lines                  (*no repetition of proof line*)
   372          | (pre, (lno', _, _) :: post) =>   (*repetition: replace later line by earlier one*)
   373              pre @ map (replace_deps (lno', [lno])) post)
   374   | add_prfline ((lno, _, t, []), lines) =  (*no deps: conjecture clause*)
   375       (lno, t, []) :: lines
   376   | add_prfline ((lno, _, t, deps), lines) =
   377       if eq_types t then (lno, t, deps) :: lines
   378       (*Type information will be deleted later; skip repetition test.*)
   379       else (*FIXME: Doesn't this code risk conflating proofs involving different types??*)
   380       case take_prefix (unequal t) lines of
   381          (_,[]) => (lno, t, deps) :: lines  (*no repetition of proof line*)
   382        | (pre, (lno', t', _) :: post) =>
   383            (lno, t', deps) ::               (*repetition: replace later line by earlier one*)
   384            (pre @ map (replace_deps (lno', [lno])) post);
   385 
   386 (*Recursively delete empty lines (type information) from the proof.*)
   387 fun add_nonnull_prfline ((lno, t, []), lines) = (*no dependencies, so a conjecture clause*)
   388      if eq_types t (*must be type information, tfree_tcs, clsrel, clsarity: delete refs to it*)
   389      then delete_dep lno lines
   390      else (lno, t, []) :: lines
   391   | add_nonnull_prfline ((lno, t, deps), lines) = (lno, t, deps) :: lines
   392 and delete_dep lno lines = List.foldr add_nonnull_prfline [] (map (replace_deps (lno, [])) lines);
   393 
   394 fun bad_free (Free (a,_)) = String.isPrefix skolem_prefix a
   395   | bad_free _ = false;
   396 
   397 (*TVars are forbidden in goals. Also, we don't want lines with <2 dependencies.
   398   To further compress proofs, setting modulus:=n deletes every nth line, and nlines
   399   counts the number of proof lines processed so far.
   400   Deleted lines are replaced by their own dependencies. Note that the "add_nonnull_prfline"
   401   phase may delete some dependencies, hence this phase comes later.*)
   402 fun add_wanted_prfline ctxt _ ((lno, t, []), (nlines, lines)) =
   403       (nlines, (lno, t, []) :: lines)   (*conjecture clauses must be kept*)
   404   | add_wanted_prfline ctxt modulus ((lno, t, deps), (nlines, lines)) =
   405       if eq_types t orelse not (null (Term.add_tvars t [])) orelse
   406          exists_subterm bad_free t orelse
   407          (not (null lines) andalso   (*final line can't be deleted for these reasons*)
   408           (length deps < 2 orelse nlines mod modulus <> 0))
   409       then (nlines+1, map (replace_deps (lno, deps)) lines) (*Delete line*)
   410       else (nlines+1, (lno, t, deps) :: lines);
   411 
   412 (*Replace numeric proof lines by strings, either from thm_names or sequential line numbers*)
   413 fun stringify_deps thm_names deps_map [] = []
   414   | stringify_deps thm_names deps_map ((lno, t, deps) :: lines) =
   415       if lno <= Vector.length thm_names  (*axiom*)
   416       then (Vector.sub(thm_names,lno-1), t, []) :: stringify_deps thm_names deps_map lines
   417       else let val lname = Int.toString (length deps_map)
   418                fun fix lno = if lno <= Vector.length thm_names
   419                              then SOME(Vector.sub(thm_names,lno-1))
   420                              else AList.lookup op= deps_map lno;
   421            in  (lname, t, map_filter fix (distinct (op=) deps)) ::
   422                stringify_deps thm_names ((lno,lname)::deps_map) lines
   423            end;
   424 
   425 fun isar_proof_start i =
   426   (if i = 1 then "" else "prefer " ^ string_of_int i ^ "\n") ^
   427   "proof (neg_clausify)\n";
   428 fun isar_fixes [] = ""
   429   | isar_fixes ts = "  fix " ^ space_implode " " ts ^ "\n";
   430 fun isar_proof_end 1 = "qed"
   431   | isar_proof_end _ = "next"
   432 
   433 fun isar_proof_from_tstp_file cnfs modulus sorts ctxt goal i thm_names =
   434   let
   435     val _ = trace_proof_msg (K "\nisar_proof_from_tstp_file: start\n")
   436     val tuples = map (dest_tstp o tstp_line o explode) cnfs
   437     val _ = trace_proof_msg (fn () =>
   438       Int.toString (length tuples) ^ " tuples extracted\n")
   439     val ctxt = ProofContext.set_mode ProofContext.mode_schematic ctxt
   440     val raw_lines = List.foldr add_prfline [] (decode_tstp_list ctxt tuples)
   441     val _ = trace_proof_msg (fn () =>
   442       Int.toString (length raw_lines) ^ " raw_lines extracted\n")
   443     val nonnull_lines = List.foldr add_nonnull_prfline [] raw_lines
   444     val _ = trace_proof_msg (fn () =>
   445       Int.toString (length nonnull_lines) ^ " nonnull_lines extracted\n")
   446     val (_, lines) = List.foldr (add_wanted_prfline ctxt modulus) (0,[]) nonnull_lines
   447     val _ = trace_proof_msg (fn () =>
   448       Int.toString (length lines) ^ " lines extracted\n")
   449     val (ccls, fixes) = neg_conjecture_clauses ctxt goal i
   450     val _ = trace_proof_msg (fn () =>
   451       Int.toString (length ccls) ^ " conjecture clauses\n")
   452     val ccls = map forall_intr_vars ccls
   453     val _ = app (fn th => trace_proof_msg
   454                               (fn () => "\nccl: " ^ string_of_thm ctxt th)) ccls
   455     val body = isar_proof_body ctxt sorts (map prop_of ccls)
   456                                (stringify_deps thm_names [] lines)
   457     val n = Logic.count_prems (prop_of goal)
   458     val _ = trace_proof_msg (K "\nisar_proof_from_tstp_file: finishing\n")
   459   in
   460     isar_proof_start i ^ isar_fixes (map #1 fixes) ^ implode body ^
   461     isar_proof_end n ^ "\n"
   462   end
   463   handle STREE _ => error "Could not extract proof (ATP output malformed?)";
   464 
   465 
   466 (*=== EXTRACTING PROOF-TEXT === *)
   467 
   468 val begin_proof_strs = ["# SZS output start CNFRefutation.",
   469   "=========== Refutation ==========",
   470   "Here is a proof"];
   471 
   472 val end_proof_strs = ["# SZS output end CNFRefutation",
   473   "======= End of refutation =======",
   474   "Formulae used in the proof"];
   475 
   476 fun get_proof_extract proof =
   477   let
   478     (*splits to_split by the first possible of a list of splitters*)
   479     val (begin_string, end_string) =
   480       (find_first (fn s => String.isSubstring s proof) begin_proof_strs,
   481       find_first (fn s => String.isSubstring s proof) end_proof_strs)
   482   in
   483     if is_none begin_string orelse is_none end_string
   484     then error "Could not extract proof (no substring indicating a proof)"
   485     else proof |> first_field (the begin_string) |> the |> snd
   486                |> first_field (the end_string) |> the |> fst
   487   end;
   488 
   489 (* ==== CHECK IF PROOF WAS SUCCESSFUL === *)
   490 
   491 fun is_proof_well_formed proof =
   492   exists (fn s => String.isSubstring s proof) begin_proof_strs andalso
   493   exists (fn s => String.isSubstring s proof) end_proof_strs
   494 
   495 (* === EXTRACTING LEMMAS === *)
   496 (* lines have the form "cnf(108, axiom, ...",
   497 the number (108) has to be extracted)*)
   498 fun get_step_nums false extract =
   499   let
   500     val toks = String.tokens (not o Char.isAlphaNum)
   501     fun inputno ("cnf" :: ntok :: "axiom" :: _) = Int.fromString ntok
   502       | inputno ("cnf" :: ntok :: "negated" :: "conjecture" :: _) =
   503         Int.fromString ntok
   504       | inputno _ = NONE
   505     val lines = split_lines extract
   506   in map_filter (inputno o toks) lines end
   507 (*String contains multiple lines. We want those of the form
   508   "253[0:Inp] et cetera..."
   509   A list consisting of the first number in each line is returned. *)
   510 |  get_step_nums true proofextract =
   511   let val toks = String.tokens (not o Char.isAlphaNum)
   512   fun inputno (ntok::"0"::"Inp"::_) = Int.fromString ntok
   513     | inputno _ = NONE
   514   val lines = split_lines proofextract
   515   in  map_filter (inputno o toks) lines  end
   516   
   517 (*extracting lemmas from tstp-output between the lines from above*)
   518 fun extract_lemmas get_step_nums (proof, thm_names, conj_count, _, _, _) =
   519   let
   520     (* get the names of axioms from their numbers*)
   521     fun get_axiom_names thm_names step_nums =
   522       let
   523         val last_axiom = Vector.length thm_names
   524         fun is_axiom n = n <= last_axiom
   525         fun is_conj n = n >= fst conj_count andalso
   526                         n < fst conj_count + snd conj_count
   527         fun getname i = Vector.sub(thm_names, i-1)
   528       in
   529         (sort_distinct string_ord (filter (fn x => x <> "??.unknown")
   530           (map getname (filter is_axiom step_nums))),
   531         exists is_conj step_nums)
   532       end
   533   in get_axiom_names thm_names (get_step_nums (get_proof_extract proof)) end;
   534 
   535 (*Used to label theorems chained into the sledgehammer call*)
   536 val chained_hint = "CHAINED";
   537 val kill_chained = filter_out (curry (op =) chained_hint)
   538 
   539 fun apply_command _ 1 = "by "
   540   | apply_command 1 _ = "apply "
   541   | apply_command i _ = "prefer " ^ string_of_int i ^ " apply "
   542 fun metis_command i n [] =
   543     apply_command i n ^ "metis"
   544   | metis_command i n xs =
   545     apply_command i n ^ "(metis " ^ space_implode " " xs ^ ")"
   546 fun metis_line i n xs =
   547   "Try this command: " ^
   548   Markup.markup Markup.sendback (metis_command i n xs) ^ ".\n" 
   549 fun minimize_line _ [] = ""
   550   | minimize_line name xs =
   551       "To minimize the number of lemmas, try this command: " ^
   552       Markup.markup Markup.sendback
   553                     ("sledgehammer minimize [atp = " ^ name ^ "] (" ^
   554                      space_implode " " xs ^ ")") ^ ".\n"
   555 
   556 fun metis_lemma_list dfg name (result as (_, _, _, _, goal, i)) =
   557   let
   558     val (lemmas, used_conj) = extract_lemmas (get_step_nums dfg) result
   559     val n = Logic.count_prems (prop_of goal)
   560     val xs = kill_chained lemmas
   561   in
   562     (metis_line i n xs ^ minimize_line name xs ^
   563      (if used_conj then
   564         ""
   565       else
   566         "\nWarning: The goal is provable because the context is inconsistent."),
   567      kill_chained lemmas)
   568   end;
   569 
   570 fun structured_isar_proof modulus sorts name
   571         (result as (proof, thm_names, conj_count, ctxt, goal, i)) =
   572   let
   573     (* We could use "split_lines", but it can return blank lines. *)
   574     val lines = String.tokens (equal #"\n");
   575     val kill_spaces =
   576       String.translate (fn c => if Char.isSpace c then "" else str c)
   577     val extract = get_proof_extract proof
   578     val cnfs = filter (String.isPrefix "cnf(") (map kill_spaces (lines extract))
   579     val (one_line_proof, lemma_names) = metis_lemma_list false name result
   580     val tokens = String.tokens (fn c => c = #" ") one_line_proof
   581     val isar_proof =
   582       if member (op =) tokens chained_hint then ""
   583       else isar_proof_from_tstp_file cnfs modulus sorts ctxt goal i thm_names
   584   in
   585     (one_line_proof ^
   586      (if isar_proof = "" then ""
   587       else "\nStructured proof:\n" ^ Markup.markup Markup.sendback isar_proof),
   588      lemma_names)
   589   end
   590 
   591 end;