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