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