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