src/HOL/Tools/res_reconstruct.ML
author paulson
Fri Aug 24 14:16:44 2007 +0200 (2007-08-24)
changeset 24425 ca97c6f3d9cd
parent 24387 cf2470f64b1d
child 24493 d4380e9b287b
permissions -rwxr-xr-x
Returning both a "one-line" proof and a structured proof
     1 (*  ID:         $Id$
     2     Author:     L C Paulson and Claire Quigley
     3     Copyright   2004  University of Cambridge
     4 *)
     5 
     6 (***************************************************************************)
     7 (*  Code to deal with the transfer of proofs from a prover process         *)
     8 (***************************************************************************)
     9 signature RES_RECONSTRUCT =
    10 sig
    11   datatype atp = E | SPASS | Vampire
    12   val modulus:     int ref
    13   val recon_sorts: bool ref
    14   val checkEProofFound:
    15 	TextIO.instream * TextIO.outstream * Posix.Process.pid *
    16 	string * Proof.context * thm * int * string Vector.vector -> bool
    17   val checkVampProofFound:
    18 	TextIO.instream * TextIO.outstream * Posix.Process.pid *
    19 	string * Proof.context * thm * int * string Vector.vector -> bool
    20   val checkSpassProofFound:
    21 	TextIO.instream * TextIO.outstream * Posix.Process.pid *
    22 	string * Proof.context * thm * int * string Vector.vector -> bool
    23   val signal_parent: TextIO.outstream * Posix.Process.pid * string * string -> unit
    24   val txt_path: string -> Path.T
    25   val fix_sorts: sort Vartab.table -> term -> term
    26   val invert_const: string -> string
    27   val invert_type_const: string -> string
    28   val num_typargs: Context.theory -> string -> int
    29   val make_tvar: string -> typ
    30   val strip_prefix: string -> string -> string option
    31 end;
    32 
    33 structure ResReconstruct : RES_RECONSTRUCT =
    34 struct
    35 
    36 val trace_path = Path.basic "atp_trace";
    37 
    38 fun trace s = if !Output.debugging then File.append (File.tmp_path trace_path) s
    39               else ();
    40 
    41 datatype atp = E | SPASS | Vampire;
    42 
    43 val recon_sorts = ref true;
    44 
    45 val modulus = ref 1;    (*keep every nth proof line*)
    46 
    47 (**** PARSING OF TSTP FORMAT ****)
    48 
    49 (*Syntax trees, either termlist or formulae*)
    50 datatype stree = Int of int | Br of string * stree list;
    51 
    52 fun atom x = Br(x,[]);
    53 
    54 fun scons (x,y) = Br("cons", [x,y]);
    55 val listof = foldl scons (atom "nil");
    56 
    57 (*Strings enclosed in single quotes, e.g. filenames*)
    58 val quoted = $$"'" |-- Scan.repeat (~$$"'") --| $$"'" >> implode;
    59 
    60 (*Intended for $true and $false*)
    61 fun tf s = "c_" ^ str (Char.toUpper (String.sub(s,0))) ^ String.extract(s,1,NONE);
    62 val truefalse = $$"$" |-- Symbol.scan_id >> (atom o tf);
    63 
    64 (*Integer constants, typically proof line numbers*)
    65 fun is_digit s = Char.isDigit (String.sub(s,0));
    66 val integer = Scan.many1 is_digit >> (valOf o Int.fromString o implode);
    67 
    68 (*Generalized FO terms, which include filenames, numbers, etc.*)
    69 fun termlist x = (term -- Scan.repeat ($$"," |-- term) >> op::) x
    70 and term x = (quoted >> atom || integer>>Int || truefalse ||
    71               Symbol.scan_id -- Scan.optional ($$"(" |-- termlist --| $$")") [] >> Br ||
    72               $$"(" |-- term --| $$")" ||
    73               $$"[" |-- termlist --| $$"]" >> listof) x;
    74 
    75 fun negate t = Br("c_Not", [t]);
    76 fun equate (t1,t2) = Br("c_equal", [t1,t2]);
    77 
    78 (*Apply equal or not-equal to a term*)
    79 fun syn_equal (t, NONE) = t
    80   | syn_equal (t1, SOME (NONE, t2)) = equate (t1,t2)
    81   | syn_equal (t1, SOME (SOME _, t2)) = negate (equate (t1,t2));
    82 
    83 (*Literals can involve negation, = and !=.*)
    84 val literal = $$"~" |-- term >> negate ||
    85               (term -- Scan.option (Scan.option ($$"!") --| $$"=" -- term) >> syn_equal) ;
    86 
    87 val literals = literal -- Scan.repeat ($$"|" |-- literal) >> op:: ;
    88 
    89 (*Clause: a list of literals separated by the disjunction sign*)
    90 val clause = $$"(" |-- literals --| $$")";
    91 
    92 val annotations = $$"," |-- term -- Scan.option ($$"," |-- termlist);
    93 
    94 (*<cnf_annotated> ::=Ęcnf(<name>,<formula_role>,<cnf_formula><annotations>).
    95   The <name> could be an identifier, but we assume integers.*)
    96 val tstp_line = (Scan.this_string "cnf" -- $$"(") |--
    97                 integer --| $$"," -- Symbol.scan_id --| $$"," --
    98                 clause -- Scan.option annotations --| $$ ")";
    99 
   100 
   101 (**** INTERPRETATION OF TSTP SYNTAX TREES ****)
   102 
   103 exception STREE of stree;
   104 
   105 (*If string s has the prefix s1, return the result of deleting it.*)
   106 fun strip_prefix s1 s =
   107   if String.isPrefix s1 s 
   108   then SOME (ResClause.undo_ascii_of (String.extract (s, size s1, NONE)))
   109   else NONE;
   110 
   111 (*Invert the table of translations between Isabelle and ATPs*)
   112 val type_const_trans_table_inv =
   113       Symtab.make (map swap (Symtab.dest ResClause.type_const_trans_table));
   114 
   115 fun invert_type_const c =
   116     case Symtab.lookup type_const_trans_table_inv c of
   117         SOME c' => c'
   118       | NONE => c;
   119 
   120 fun make_tvar b = TVar(("'" ^ b, 0), HOLogic.typeS);
   121 fun make_var (b,T) = Var((b,0),T);
   122 
   123 (*Type variables are given the basic sort, HOL.type. Some will later be constrained
   124   by information from type literals, or by type inference.*)
   125 fun type_of_stree t =
   126   case t of
   127       Int _ => raise STREE t
   128     | Br (a,ts) =>
   129         let val Ts = map type_of_stree ts
   130         in
   131           case strip_prefix ResClause.tconst_prefix a of
   132               SOME b => Type(invert_type_const b, Ts)
   133             | NONE =>
   134                 if not (null ts) then raise STREE t  (*only tconsts have type arguments*)
   135                 else
   136                 case strip_prefix ResClause.tfree_prefix a of
   137                     SOME b => TFree("'" ^ b, HOLogic.typeS)
   138                   | NONE =>
   139                 case strip_prefix ResClause.tvar_prefix a of
   140                     SOME b => make_tvar b
   141                   | NONE => make_tvar a   (*Variable from the ATP, say X1*)
   142         end;
   143 
   144 (*Invert the table of translations between Isabelle and ATPs*)
   145 val const_trans_table_inv =
   146       Symtab.update ("fequal", "op =")
   147         (Symtab.make (map swap (Symtab.dest ResClause.const_trans_table)));
   148 
   149 fun invert_const c =
   150     case Symtab.lookup const_trans_table_inv c of
   151         SOME c' => c'
   152       | NONE => c;
   153 
   154 (*The number of type arguments of a constant, zero if it's monomorphic*)
   155 fun num_typargs thy s = length (Sign.const_typargs thy (s, Sign.the_const_type thy s));
   156 
   157 (*Generates a constant, given its type arguments*)
   158 fun const_of thy (a,Ts) = Const(a, Sign.const_instance thy (a,Ts));
   159 
   160 (*First-order translation. No types are known for variables. HOLogic.typeT should allow
   161   them to be inferred.*)
   162 fun term_of_stree args thy t =
   163   case t of
   164       Int _ => raise STREE t
   165     | Br ("hBOOL",[t]) => term_of_stree [] thy t  (*ignore hBOOL*)
   166     | Br ("hAPP",[t,u]) => term_of_stree (u::args) thy t
   167     | Br (a,ts) =>
   168         case strip_prefix ResClause.const_prefix a of
   169             SOME "equal" =>
   170               list_comb(Const ("op =", HOLogic.typeT), List.map (term_of_stree [] thy) ts)
   171           | SOME b =>
   172               let val c = invert_const b
   173                   val nterms = length ts - num_typargs thy c
   174                   val us = List.map (term_of_stree [] thy) (List.take(ts,nterms) @ args)
   175                   (*Extra args from hAPP come AFTER any arguments given directly to the
   176                     constant.*)
   177                   val Ts = List.map type_of_stree (List.drop(ts,nterms))
   178               in  list_comb(const_of thy (c, Ts), us)  end
   179           | NONE => (*a variable, not a constant*)
   180               let val T = HOLogic.typeT
   181                   val opr = (*a Free variable is typically a Skolem function*)
   182                     case strip_prefix ResClause.fixed_var_prefix a of
   183                         SOME b => Free(b,T)
   184                       | NONE =>
   185                     case strip_prefix ResClause.schematic_var_prefix a of
   186                         SOME b => make_var (b,T)
   187                       | NONE => make_var (a,T)    (*Variable from the ATP, say X1*)
   188               in  list_comb (opr, List.map (term_of_stree [] thy) (ts@args))  end;
   189 
   190 (*Type class literal applied to a type. Returns triple of polarity, class, type.*)
   191 fun constraint_of_stree pol (Br("c_Not",[t])) = constraint_of_stree (not pol) t
   192   | constraint_of_stree pol t = case t of
   193         Int _ => raise STREE t
   194       | Br (a,ts) =>
   195             (case (strip_prefix ResClause.class_prefix a, map type_of_stree ts) of
   196                  (SOME b, [T]) => (pol, b, T)
   197                | _ => raise STREE t);
   198 
   199 (** Accumulate type constraints in a clause: negative type literals **)
   200 
   201 fun addix (key,z)  = Vartab.map_default (key,[]) (cons z);
   202 
   203 fun add_constraint ((false, cl, TFree(a,_)), vt) = addix ((a,~1),cl) vt
   204   | add_constraint ((false, cl, TVar(ix,_)), vt) = addix (ix,cl) vt
   205   | add_constraint (_, vt) = vt;
   206 
   207 (*False literals (which E includes in its proofs) are deleted*)
   208 val nofalses = filter (not o equal HOLogic.false_const);
   209 
   210 (*Final treatment of the list of "real" literals from a clause.*)
   211 fun finish [] = HOLogic.true_const  (*No "real" literals means only type information*)
   212   | finish lits =
   213       case nofalses lits of
   214           [] => HOLogic.false_const  (*The empty clause, since we started with real literals*)
   215         | xs => foldr1 HOLogic.mk_disj (rev xs);
   216 
   217 (*Accumulate sort constraints in vt, with "real" literals in lits.*)
   218 fun lits_of_strees ctxt (vt, lits) [] = (vt, finish lits)
   219   | lits_of_strees ctxt (vt, lits) (t::ts) =
   220       lits_of_strees ctxt (add_constraint (constraint_of_stree true t, vt), lits) ts
   221       handle STREE _ =>
   222       lits_of_strees ctxt (vt, term_of_stree [] (ProofContext.theory_of ctxt) t :: lits) ts;
   223 
   224 (*Update TVars/TFrees with detected sort constraints.*)
   225 fun fix_sorts vt =
   226   let fun tysubst (Type (a, Ts)) = Type (a, map tysubst Ts)
   227         | tysubst (TVar (xi, s)) = TVar (xi, getOpt (Vartab.lookup vt xi, s))
   228         | tysubst (TFree (x, s)) = TFree (x, getOpt (Vartab.lookup vt (x,~1), s))
   229       fun tmsubst (Const (a, T)) = Const (a, tysubst T)
   230         | tmsubst (Free (a, T)) = Free (a, tysubst T)
   231         | tmsubst (Var (xi, T)) = Var (xi, tysubst T)
   232         | tmsubst (t as Bound _) = t
   233         | tmsubst (Abs (a, T, t)) = Abs (a, tysubst T, tmsubst t)
   234         | tmsubst (t $ u) = tmsubst t $ tmsubst u;
   235   in fn t => if Vartab.is_empty vt then t else tmsubst t end;
   236 
   237 (*Interpret a list of syntax trees as a clause, given by "real" literals and sort constraints.
   238   vt0 holds the initial sort constraints, from the conjecture clauses.*)
   239 fun clause_of_strees ctxt vt0 ts =
   240   let val (vt, dt) = lits_of_strees ctxt (vt0,[]) ts in
   241     singleton (ProofContext.infer_types ctxt) (TypeInfer.constrain (fix_sorts vt dt) HOLogic.boolT)
   242   end;
   243 
   244 (*Quantification over a list of Vars. FIXME: for term.ML??*)
   245 fun list_all_var ([], t: term) = t
   246   | list_all_var ((v as Var(ix,T)) :: vars, t) =
   247       (all T) $ Abs(string_of_indexname ix, T, abstract_over (v, list_all_var (vars,t)));
   248 
   249 fun gen_all_vars t = list_all_var (term_vars t, t);
   250 
   251 fun ints_of_stree_aux (Int n, ns) = n::ns
   252   | ints_of_stree_aux (Br(_,ts), ns) = foldl ints_of_stree_aux ns ts;
   253 
   254 fun ints_of_stree t = ints_of_stree_aux (t, []);
   255 
   256 fun decode_tstp ctxt vt0 (name, role, ts, annots) =
   257   let val deps = case annots of NONE => [] | SOME (source,_) => ints_of_stree source
   258   in  (name, role, clause_of_strees ctxt vt0 ts, deps)  end;
   259 
   260 fun dest_tstp ((((name, role), ts), annots), chs) =
   261   case chs of
   262           "."::_ => (name, role, ts, annots)
   263         | _ => error ("TSTP line not terminated by \".\": " ^ implode chs);
   264 
   265 
   266 (** Global sort constraints on TFrees (from tfree_tcs) are positive unit clauses. **)
   267 
   268 fun add_tfree_constraint ((true, cl, TFree(a,_)), vt) = addix ((a,~1),cl) vt
   269   | add_tfree_constraint (_, vt) = vt;
   270 
   271 fun tfree_constraints_of_clauses vt [] = vt
   272   | tfree_constraints_of_clauses vt ([lit]::tss) =
   273       (tfree_constraints_of_clauses (add_tfree_constraint (constraint_of_stree true lit, vt)) tss
   274        handle STREE _ => (*not a positive type constraint: ignore*)
   275        tfree_constraints_of_clauses vt tss)
   276   | tfree_constraints_of_clauses vt (_::tss) = tfree_constraints_of_clauses vt tss;
   277 
   278 
   279 (**** Translation of TSTP files to Isar Proofs ****)
   280 
   281 fun decode_tstp_list ctxt tuples =
   282   let val vt0 = tfree_constraints_of_clauses Vartab.empty (map #3 tuples)
   283   in  map (decode_tstp ctxt vt0) tuples  end;
   284 
   285 (*FIXME: simmilar function in res_atp. Move to HOLogic?*)
   286 fun dest_disj_aux (Const ("op |", _) $ t $ t') disjs = dest_disj_aux t (dest_disj_aux t' disjs)
   287   | dest_disj_aux t disjs = t::disjs;
   288 
   289 fun dest_disj t = dest_disj_aux t [];
   290 
   291 (** Finding a matching assumption. The literals may be permuted, and variable names
   292     may disagree. We have to try all combinations of literals (quadratic!) and 
   293     match up the variable names consistently. **)
   294 
   295 fun strip_alls_aux n (Const("all",_)$Abs(a,T,t))  =  
   296       strip_alls_aux (n+1) (subst_bound (Var ((a,n), T), t))
   297   | strip_alls_aux _ t  =  t;
   298 
   299 val strip_alls = strip_alls_aux 0;
   300 
   301 exception MATCH_LITERAL;
   302 
   303 (*Ignore types: they are not to be trusted...*)
   304 fun match_literal (t1$u1) (t2$u2) env =
   305       match_literal t1 t2 (match_literal u1 u2 env)
   306   | match_literal (Abs (_,_,t1)) (Abs (_,_,t2)) env = 
   307       match_literal t1 t2 env
   308   | match_literal (Bound i1) (Bound i2) env = 
   309       if i1=i2 then env else raise MATCH_LITERAL
   310   | match_literal (Const(a1,_)) (Const(a2,_)) env = 
   311       if a1=a2 then env else raise MATCH_LITERAL
   312   | match_literal (Free(a1,_)) (Free(a2,_)) env = 
   313       if a1=a2 then env else raise MATCH_LITERAL
   314   | match_literal (Var(ix1,_)) (Var(ix2,_)) env = insert (op =) (ix1,ix2) env
   315   | match_literal _ _ env = raise MATCH_LITERAL;
   316 
   317 (*Checking that all variable associations are unique. The list env contains no
   318   repetitions, but does it contain say (x,y) and (y,y)? *)
   319 fun good env = 
   320   let val (xs,ys) = ListPair.unzip env
   321   in  not (has_duplicates (op=) xs orelse has_duplicates (op=) ys)  end;
   322 
   323 (*Match one list of literals against another, ignoring types and the order of
   324   literals. Sorting is unreliable because we don't have types or variable names.*)
   325 fun matches_aux _ [] [] = true
   326   | matches_aux env (lit::lits) ts =
   327       let fun match1 us [] = false
   328             | match1 us (t::ts) =
   329                 let val env' = match_literal lit t env
   330                 in  (good env' andalso matches_aux env' lits (us@ts)) orelse 
   331                     match1 (t::us) ts  
   332                 end
   333                 handle MATCH_LITERAL => match1 (t::us) ts
   334       in  match1 [] ts  end; 
   335 
   336 (*Is this length test useful?*)
   337 fun matches (lits1,lits2) = 
   338   length lits1 = length lits2  andalso  
   339   matches_aux [] (map Envir.eta_contract lits1) (map Envir.eta_contract lits2);
   340 
   341 fun permuted_clause t =
   342   let val lits = dest_disj t
   343       fun perm [] = NONE
   344         | perm (ctm::ctms) =
   345             if matches (lits, dest_disj (HOLogic.dest_Trueprop (strip_alls ctm)))
   346             then SOME ctm else perm ctms
   347   in perm end;
   348 
   349 fun have_or_show "show " lname = "show \""
   350   | have_or_show have lname = have ^ lname ^ ": \""
   351 
   352 (*ctms is a list of conjecture clauses as yielded by Isabelle. Those returned by the
   353   ATP may have their literals reordered.*)
   354 fun isar_lines ctxt ctms =
   355   let val string_of = ProofContext.string_of_term ctxt
   356       fun doline have (lname, t, []) =  (*No deps: it's a conjecture clause, with no proof.*)
   357            (case permuted_clause t ctms of
   358                 SOME u => "assume " ^ lname ^ ": \"" ^ string_of u ^ "\"\n"
   359               | NONE => "assume? " ^ lname ^ ": \"" ^ string_of t ^ "\"\n")  (*no match!!*)
   360         | doline have (lname, t, deps) =
   361             have_or_show have lname ^ string_of (gen_all_vars (HOLogic.mk_Trueprop t)) ^
   362             "\"\n  by (metis " ^ space_implode " " deps ^ ")\n"
   363       fun dolines [(lname, t, deps)] = [doline "show " (lname, t, deps)]
   364         | dolines ((lname, t, deps)::lines) = doline "have " (lname, t, deps) :: dolines lines
   365   in setmp show_sorts (!recon_sorts) dolines end;
   366 
   367 fun notequal t (_,t',_) = not (t aconv t');
   368 
   369 (*No "real" literals means only type information*)
   370 fun eq_types t = t aconv HOLogic.true_const;
   371 
   372 fun replace_dep (old:int, new) dep = if dep=old then new else [dep];
   373 
   374 fun replace_deps (old:int, new) (lno, t, deps) =
   375       (lno, t, foldl (op union_int) [] (map (replace_dep (old, new)) deps));
   376 
   377 (*Discard axioms; consolidate adjacent lines that prove the same clause, since they differ
   378   only in type information.*)
   379 fun add_prfline ((lno, "axiom", t, []), lines) =  (*axioms are not proof lines*)
   380       if eq_types t (*must be clsrel/clsarity: type information, so delete refs to it*)
   381       then map (replace_deps (lno, [])) lines
   382       else
   383        (case take_prefix (notequal t) lines of
   384            (_,[]) => lines                  (*no repetition of proof line*)
   385          | (pre, (lno',t',deps')::post) =>  (*repetition: replace later line by earlier one*)
   386              pre @ map (replace_deps (lno', [lno])) post)
   387   | add_prfline ((lno, role, t, []), lines) =  (*no deps: conjecture clause*)
   388       (lno, t, []) :: lines
   389   | add_prfline ((lno, role, t, deps), lines) =
   390       if eq_types t then (lno, t, deps) :: lines
   391       (*Type information will be deleted later; skip repetition test.*)
   392       else (*FIXME: Doesn't this code risk conflating proofs involving different types??*)
   393       case take_prefix (notequal t) lines of
   394          (_,[]) => (lno, t, deps) :: lines  (*no repetition of proof line*)
   395        | (pre, (lno',t',deps')::post) =>
   396            (lno, t', deps) ::               (*repetition: replace later line by earlier one*)
   397            (pre @ map (replace_deps (lno', [lno])) post);
   398 
   399 (*Recursively delete empty lines (type information) from the proof.*)
   400 fun add_nonnull_prfline ((lno, t, []), lines) = (*no dependencies, so a conjecture clause*)
   401      if eq_types t (*must be type information, tfree_tcs, clsrel, clsarity: delete refs to it*)
   402      then delete_dep lno lines
   403      else (lno, t, []) :: lines
   404   | add_nonnull_prfline ((lno, t, deps), lines) = (lno, t, deps) :: lines
   405 and delete_dep lno lines = foldr add_nonnull_prfline [] (map (replace_deps (lno, [])) lines);
   406 
   407 fun bad_free (Free (a,_)) = String.isPrefix "llabs_" a orelse String.isPrefix "sko_" a
   408   | bad_free _ = false;
   409 
   410 (*TVars are forbidden in goals. Also, we don't want lines with <2 dependencies.
   411   To further compress proofs, setting modulus:=n deletes every nth line, and nlines
   412   counts the number of proof lines processed so far.
   413   Deleted lines are replaced by their own dependencies. Note that the "add_nonnull_prfline"
   414   phase may delete some dependencies, hence this phase comes later.*)
   415 fun add_wanted_prfline ((lno, t, []), (nlines, lines)) =
   416       (nlines, (lno, t, []) :: lines)   (*conjecture clauses must be kept*)
   417   | add_wanted_prfline (line, (nlines, [])) = (nlines, [line])   (*final line must be kept*)
   418   | add_wanted_prfline ((lno, t, deps), (nlines, lines)) =
   419       if eq_types t orelse not (null (term_tvars t)) orelse
   420          length deps < 2 orelse nlines mod !modulus <> 0 orelse
   421          exists bad_free (term_frees t)
   422       then (nlines+1, map (replace_deps (lno, deps)) lines) (*Delete line*)
   423       else (nlines+1, (lno, t, deps) :: lines);
   424 
   425 (*Replace numeric proof lines by strings, either from thm_names or sequential line numbers*)
   426 fun stringify_deps thm_names deps_map [] = []
   427   | stringify_deps thm_names deps_map ((lno, t, deps) :: lines) =
   428       if lno <= Vector.length thm_names  (*axiom*)
   429       then (Vector.sub(thm_names,lno-1), t, []) :: stringify_deps thm_names deps_map lines
   430       else let val lname = Int.toString (length deps_map)
   431                fun fix lno = if lno <= Vector.length thm_names
   432                              then SOME(Vector.sub(thm_names,lno-1))
   433                              else AList.lookup op= deps_map lno;
   434            in  (lname, t, List.mapPartial fix (distinct (op=) deps)) ::
   435                stringify_deps thm_names ((lno,lname)::deps_map) lines
   436            end;
   437 
   438 val proofstart = "\nproof (neg_clausify)\n";
   439 
   440 fun isar_header [] = proofstart
   441   | isar_header ts = proofstart ^ "fix " ^ space_implode " " ts ^ "\n";
   442 
   443 fun decode_tstp_file cnfs ctxt th sgno thm_names =
   444   let val tuples = map (dest_tstp o tstp_line o explode) cnfs
   445       val nonnull_lines =
   446               foldr add_nonnull_prfline []
   447                     (foldr add_prfline [] (decode_tstp_list ctxt tuples))
   448       val (_,lines) = foldr add_wanted_prfline (0,[]) nonnull_lines
   449       val (ccls,fixes) = ResAxioms.neg_conjecture_clauses th sgno
   450       val ccls = map forall_intr_vars ccls
   451   in
   452     app (fn th => Output.debug (fn () => string_of_thm th)) ccls;
   453     isar_header (map #1 fixes) ^
   454     String.concat (isar_lines ctxt (map prop_of ccls) (stringify_deps thm_names [] lines))
   455   end;
   456 
   457 (*Could use split_lines, but it can return blank lines...*)
   458 val lines = String.tokens (equal #"\n");
   459 
   460 val nospaces = String.translate (fn c => if Char.isSpace c then "" else str c);
   461 
   462 val txt_path = Path.ext "txt" o Path.explode o nospaces;
   463 
   464 fun signal_success probfile toParent ppid msg =
   465   let val _ = trace ("\nReporting Success for " ^ probfile ^ "\n" ^ msg)
   466   in
   467     (*We write the proof to a file because sending very long lines may fail...*)
   468     File.write (txt_path probfile) msg;
   469     TextIO.output (toParent, "Success.\n");
   470     TextIO.output (toParent, probfile ^ "\n");
   471     TextIO.flushOut toParent;
   472     Posix.Process.kill(Posix.Process.K_PROC ppid, Posix.Signal.usr2);
   473     (*Give the parent time to respond before possibly sending another signal*)
   474     OS.Process.sleep (Time.fromMilliseconds 600)
   475   end;
   476 
   477 
   478 (**** retrieve the axioms that were used in the proof ****)
   479 
   480 (*Get names of axioms used. Axioms are indexed from 1, while the vector is indexed from 0*)
   481 fun get_axiom_names (thm_names: string vector) step_nums =
   482     let fun is_axiom n = n <= Vector.length thm_names
   483         fun index i = Vector.sub(thm_names, i-1)
   484         val axnums = List.filter is_axiom step_nums
   485         val axnames = sort_distinct string_ord (map index axnums)
   486     in
   487 	if length axnums = length step_nums then "UNSOUND!!" :: axnames
   488 	else axnames
   489     end
   490 
   491  (*String contains multiple lines. We want those of the form
   492      "253[0:Inp] et cetera..."
   493   A list consisting of the first number in each line is returned. *)
   494 fun get_spass_linenums proofextract =
   495   let val toks = String.tokens (not o Char.isAlphaNum)
   496       fun inputno (ntok::"0"::"Inp"::_) = Int.fromString ntok
   497         | inputno _ = NONE
   498       val lines = String.tokens (fn c => c = #"\n") proofextract
   499   in  List.mapPartial (inputno o toks) lines  end
   500 
   501 fun get_axiom_names_spass proofextract thm_names =
   502    get_axiom_names thm_names (get_spass_linenums proofextract);
   503 
   504 fun not_comma c = c <>  #",";
   505 
   506 (*A valid TSTP axiom line has the form  cnf(NNN,axiom,...) where NNN is a positive integer.*)
   507 fun parse_tstp_line s =
   508   let val ss = Substring.full (unprefix "cnf(" (nospaces s))
   509       val (intf,rest) = Substring.splitl not_comma ss
   510       val (rolef,rest) = Substring.splitl not_comma (Substring.triml 1 rest)
   511       (*We only allow negated_conjecture because the line number will be removed in
   512         get_axiom_names above, while suppressing the UNSOUND warning*)
   513       val ints = if Substring.string rolef mem_string ["axiom","negated_conjecture"]
   514                  then Substring.string intf
   515                  else "error"
   516   in  Int.fromString ints  end
   517   handle Fail _ => NONE;
   518 
   519 fun get_axiom_names_tstp proofextract thm_names =
   520    get_axiom_names thm_names (List.mapPartial parse_tstp_line (split_lines proofextract));
   521 
   522  (*String contains multiple lines. We want those of the form
   523      "*********** [448, input] ***********".
   524   A list consisting of the first number in each line is returned. *)
   525 fun get_vamp_linenums proofextract =
   526   let val toks = String.tokens (not o Char.isAlphaNum)
   527       fun inputno [ntok,"input"] = Int.fromString ntok
   528         | inputno _ = NONE
   529       val lines = String.tokens (fn c => c = #"\n") proofextract
   530   in  List.mapPartial (inputno o toks) lines  end
   531 
   532 fun get_axiom_names_vamp proofextract thm_names =
   533    get_axiom_names thm_names (get_vamp_linenums proofextract);
   534 
   535 fun get_axiom_names E       = get_axiom_names_tstp
   536   | get_axiom_names SPASS   = get_axiom_names_spass
   537   | get_axiom_names Vampire = get_axiom_names_vamp;
   538 
   539 fun rules_to_metis [] = "metis"
   540   | rules_to_metis xs = "(metis " ^ space_implode " " xs ^ ")"
   541 
   542 fun metis_line atp proofextract thm_names =
   543   "apply " ^ rules_to_metis (get_axiom_names atp proofextract thm_names);
   544 
   545 (*The signal handler in watcher.ML must be able to read the output of this.*)
   546 fun lemma_list atp proofextract thm_names probfile toParent ppid =
   547   signal_success probfile toParent ppid (metis_line atp proofextract thm_names);
   548 
   549 fun tstp_extract atp proofextract thm_names probfile toParent ppid ctxt th sgno =
   550   let val cnfs = filter (String.isPrefix "cnf(") (map nospaces (lines proofextract))
   551       val line1 = metis_line atp proofextract thm_names
   552   in
   553     signal_success probfile toParent ppid
   554       (line1 ^ "\n" ^ decode_tstp_file cnfs ctxt th sgno thm_names)
   555   end;
   556 
   557 (**** Extracting proofs from an ATP's output ****)
   558 
   559 (*Return everything in s that comes before the string t*)
   560 fun cut_before t s =
   561   let val (s1,s2) = Substring.position t (Substring.full s)
   562   in  if Substring.size s2 = 0 then error "cut_before: string not found"
   563       else Substring.string s2
   564   end;
   565 
   566 val start_E = "# Proof object starts here."
   567 val end_E   = "# Proof object ends here."
   568 val start_V6 = "%================== Proof: ======================"
   569 val end_V6   = "%==============  End of proof. =================="
   570 val start_V8 = "=========== Refutation =========="
   571 val end_V8 = "======= End of refutation ======="
   572 val end_SPASS = "Formulae used in the proof"
   573 
   574 (*********************************************************************************)
   575 (*  Inspect the output of an ATP process to see if it has found a proof,     *)
   576 (*  and if so, transfer output to the input pipe of the main Isabelle process    *)
   577 (*********************************************************************************)
   578 
   579 (*Returns "true" if it successfully returns a lemma list, otherwise "false", but this
   580   return value is currently never used!*)
   581 fun startTransfer endS (fromChild, toParent, ppid, probfile, ctxt, th, sgno, thm_names) =
   582  let fun transferInput currentString =
   583       (case TextIO.inputLine fromChild of
   584         NONE =>  (*end of file?*)
   585 	  (trace ("\n extraction_failed.  End bracket: " ^ endS ^
   586 	          "\naccumulated text: " ^ currentString);
   587 	   false)
   588       | SOME thisLine =>
   589 	if String.isPrefix endS thisLine
   590 	then let val proofextract = currentString ^ cut_before endS thisLine
   591 	         val atp = if endS = end_V8 then Vampire
   592 			   else if endS = end_SPASS then SPASS
   593 			   else E
   594 	     in
   595 	       trace ("\nExtracted proof:\n" ^ proofextract);
   596 	       if String.isPrefix "cnf(" proofextract
   597 	       then tstp_extract atp proofextract thm_names probfile toParent ppid ctxt th sgno
   598 	       else lemma_list atp proofextract thm_names probfile toParent ppid;
   599 	       true
   600 	     end
   601 	     handle e => (*FIXME: exn handler is too general!*)
   602 	      (trace ("\nstartTransfer: In exception handler: " ^ Toplevel.exn_message e);
   603 	       TextIO.output (toParent, "Translation failed\n" ^ probfile);
   604 	       TextIO.flushOut toParent;
   605 	       Posix.Process.kill(Posix.Process.K_PROC ppid, Posix.Signal.usr2);
   606 	       true)
   607 	else transferInput (currentString^thisLine))
   608  in
   609      transferInput ""
   610  end
   611 
   612 
   613 (*The signal handler in watcher.ML must be able to read the output of this.*)
   614 fun signal_parent (toParent, ppid, msg, probfile) =
   615  (TextIO.output (toParent, msg);
   616   TextIO.output (toParent, probfile ^ "\n");
   617   TextIO.flushOut toParent;
   618   trace ("\nSignalled parent: " ^ msg ^ probfile);
   619   Posix.Process.kill(Posix.Process.K_PROC ppid, Posix.Signal.usr2);
   620   (*Give the parent time to respond before possibly sending another signal*)
   621   OS.Process.sleep (Time.fromMilliseconds 600));
   622 
   623 (*FIXME: once TSTP output is produced by all ATPs, these three functions can be combined.*)
   624 
   625 (*Called from watcher. Returns true if the Vampire process has returned a verdict.*)
   626 fun checkVampProofFound (arg as (fromChild, toParent, ppid, probfile, ctxt, th, sgno, thm_names)) =
   627   (case TextIO.inputLine fromChild of
   628     NONE => (trace "\nNo proof output seen"; false)
   629   | SOME thisLine =>
   630      if String.isPrefix start_V8 thisLine
   631      then startTransfer end_V8 arg
   632      else if (String.isPrefix "Satisfiability detected" thisLine) orelse
   633              (String.isPrefix "Refutation not found" thisLine)
   634      then (signal_parent (toParent, ppid, "Failure\n", probfile);
   635 	   true)
   636      else checkVampProofFound arg);
   637 
   638 (*Called from watcher. Returns true if the E process has returned a verdict.*)
   639 fun checkEProofFound (arg as (fromChild, toParent, ppid, probfile, ctxt, th, sgno, thm_names)) =
   640   (case TextIO.inputLine fromChild of
   641     NONE => (trace "\nNo proof output seen"; false)
   642   | SOME thisLine =>
   643      if String.isPrefix start_E thisLine
   644      then startTransfer end_E arg
   645      else if String.isPrefix "# Problem is satisfiable" thisLine
   646      then (signal_parent (toParent, ppid, "Invalid\n", probfile);
   647 	   true)
   648      else if String.isPrefix "# Cannot determine problem status within resource limit" thisLine
   649      then (signal_parent (toParent, ppid, "Failure\n", probfile);
   650 	   true)
   651      else checkEProofFound arg);
   652 
   653 (*Called from watcher. Returns true if the SPASS process has returned a verdict.*)
   654 fun checkSpassProofFound (arg as (fromChild, toParent, ppid, probfile, ctxt, th, sgno, thm_names)) =
   655   (case TextIO.inputLine fromChild of
   656     NONE => (trace "\nNo proof output seen"; false)
   657   | SOME thisLine =>
   658      if String.isPrefix "Here is a proof" thisLine
   659      then startTransfer end_SPASS arg
   660      else if thisLine = "SPASS beiseite: Completion found.\n"
   661      then (signal_parent (toParent, ppid, "Invalid\n", probfile);
   662 	   true)
   663      else if thisLine = "SPASS beiseite: Ran out of time.\n" orelse
   664              thisLine = "SPASS beiseite: Maximal number of loops exceeded.\n"
   665      then (signal_parent (toParent, ppid, "Failure\n", probfile);
   666 	   true)
   667     else checkSpassProofFound arg);
   668 
   669 end;