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