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