src/HOL/Tools/Sledgehammer/sledgehammer_proof_reconstruct.ML
author blanchet
Wed Apr 28 13:00:30 2010 +0200 (2010-04-28)
changeset 36488 32c92af68ec9
parent 36486 c2d7e2dff59e
child 36491 6769f8eacaac
permissions -rw-r--r--
remove Sledgehammer's "sorts" option to annotate variables with sorts in proof;
what we need is smarter type annotations for variables _and_ constants
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@36488
    24
    name_pool option * bool * int * Proof.context * int list list
blanchet@36287
    25
    -> minimize_command * string * string vector * thm * int
blanchet@36287
    26
    -> string * string list
blanchet@36223
    27
  val proof_text:
blanchet@36488
    28
    bool -> name_pool option * bool * int * Proof.context * int list list
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@36486
    36
datatype ('a, 'b, 'c, 'd, 'e) raw_step =
blanchet@36486
    37
  Definition of 'a * 'b * 'c |
blanchet@36486
    38
  Inference of 'a * 'd * 'e list
blanchet@36486
    39
blanchet@36478
    40
open Sledgehammer_Util
blanchet@35865
    41
open Sledgehammer_FOL_Clause
blanchet@35865
    42
open Sledgehammer_Fact_Preprocessor
paulson@21978
    43
blanchet@36281
    44
type minimize_command = string list -> string
blanchet@36281
    45
blanchet@36291
    46
fun is_ident_char c = Char.isAlphaNum c orelse c = #"_"
blanchet@36392
    47
fun is_head_digit s = Char.isDigit (String.sub (s, 0))
blanchet@36291
    48
blanchet@36402
    49
fun is_axiom_clause_number thm_names num = num <= Vector.length thm_names
blanchet@36291
    50
blanchet@36393
    51
fun ugly_name NONE s = s
blanchet@36393
    52
  | ugly_name (SOME the_pool) s =
blanchet@36393
    53
    case Symtab.lookup (snd the_pool) s of
blanchet@36393
    54
      SOME s' => s'
blanchet@36393
    55
    | NONE => s
blanchet@36393
    56
paulson@21978
    57
(**** PARSING OF TSTP FORMAT ****)
paulson@21978
    58
blanchet@36291
    59
(* Syntax trees, either term list or formulae *)
blanchet@36486
    60
datatype node = IntLeaf of int | StrNode of string * node list
paulson@21978
    61
blanchet@36486
    62
fun atom x = StrNode (x, [])
paulson@21978
    63
blanchet@36486
    64
fun scons (x, y) = StrNode ("cons", [x, y])
blanchet@36392
    65
val slist_of = List.foldl scons (atom "nil")
paulson@21978
    66
paulson@21978
    67
(*Strings enclosed in single quotes, e.g. filenames*)
blanchet@36392
    68
val parse_quoted = $$ "'" |-- Scan.repeat (~$$ "'") --| $$ "'" >> implode;
paulson@21978
    69
paulson@21978
    70
(*Integer constants, typically proof line numbers*)
blanchet@36392
    71
val parse_integer = Scan.many1 is_head_digit >> (the o Int.fromString o implode)
paulson@21978
    72
blanchet@36369
    73
(* needed for SPASS's output format *)
blanchet@36393
    74
fun repair_bool_literal "true" = "c_True"
blanchet@36393
    75
  | repair_bool_literal "false" = "c_False"
blanchet@36393
    76
fun repair_name pool "equal" = "c_equal"
blanchet@36393
    77
  | repair_name pool s = ugly_name pool s
blanchet@36392
    78
(* Generalized first-order terms, which include file names, numbers, etc. *)
blanchet@36393
    79
(* The "x" argument is not strictly necessary, but without it Poly/ML loops
blanchet@36393
    80
   forever at compile time. *)
blanchet@36393
    81
fun parse_term pool x =
blanchet@36486
    82
     (parse_quoted >> atom
blanchet@36486
    83
   || parse_integer >> IntLeaf
blanchet@36393
    84
   || $$ "$" |-- Symbol.scan_id >> (atom o repair_bool_literal)
blanchet@36393
    85
   || (Symbol.scan_id >> repair_name pool)
blanchet@36486
    86
      -- Scan.optional ($$ "(" |-- parse_terms pool --| $$ ")") [] >> StrNode
blanchet@36393
    87
   || $$ "(" |-- parse_term pool --| $$ ")"
blanchet@36393
    88
   || $$ "[" |-- Scan.optional (parse_terms pool) [] --| $$ "]" >> slist_of) x
blanchet@36393
    89
and parse_terms pool x =
blanchet@36393
    90
  (parse_term pool ::: Scan.repeat ($$ "," |-- parse_term pool)) x
paulson@21978
    91
blanchet@36486
    92
fun negate_node u = StrNode ("c_Not", [u])
blanchet@36486
    93
fun equate_nodes u1 u2 = StrNode ("c_equal", [u1, u2])
paulson@21978
    94
blanchet@36392
    95
(* Apply equal or not-equal to a term. *)
blanchet@36486
    96
fun repair_predicate_term (u, NONE) = u
blanchet@36486
    97
  | repair_predicate_term (u1, SOME (NONE, u2)) = equate_nodes u1 u2
blanchet@36486
    98
  | repair_predicate_term (u1, SOME (SOME _, u2)) =
blanchet@36486
    99
    negate_node (equate_nodes u1 u2)
blanchet@36393
   100
fun parse_predicate_term pool =
blanchet@36393
   101
  parse_term pool -- Scan.option (Scan.option ($$ "!") --| $$ "="
blanchet@36393
   102
                                  -- parse_term pool)
blanchet@36393
   103
  >> repair_predicate_term
blanchet@36486
   104
(* Literals can involve "~", "=", and "!=". *)
blanchet@36393
   105
fun parse_literal pool x =
blanchet@36486
   106
  ($$ "~" |-- parse_literal pool >> negate_node || parse_predicate_term pool) x
paulson@21978
   107
blanchet@36393
   108
fun parse_literals pool =
blanchet@36393
   109
  parse_literal pool ::: Scan.repeat ($$ "|" |-- parse_literal pool)
paulson@21978
   110
blanchet@36486
   111
(* Clause: a list of literals separated by disjunction operators ("|"). *)
blanchet@36393
   112
fun parse_clause pool =
blanchet@36393
   113
  $$ "(" |-- parse_literals pool --| $$ ")" || Scan.single (parse_literal pool)
blanchet@36291
   114
blanchet@36486
   115
fun ints_of_node (IntLeaf n) = cons n
blanchet@36486
   116
  | ints_of_node (StrNode (_, us)) = fold ints_of_node us
blanchet@36392
   117
val parse_tstp_annotations =
blanchet@36393
   118
  Scan.optional ($$ "," |-- parse_term NONE
blanchet@36393
   119
                   --| Scan.option ($$ "," |-- parse_terms NONE)
blanchet@36486
   120
                 >> (fn source => ints_of_node source [])) []
blanchet@36486
   121
blanchet@36486
   122
fun parse_definition pool =
blanchet@36486
   123
  $$ "(" |-- parse_literal NONE --| Scan.this_string "<=>"
blanchet@36486
   124
  -- parse_clause pool --| $$ ")"
blanchet@36291
   125
blanchet@36486
   126
(* Syntax: cnf(<num>, <formula_role>, <cnf_formula> <annotations>).
blanchet@36486
   127
   The <num> could be an identifier, but we assume integers. *)
blanchet@36486
   128
fun finish_tstp_definition_line (num, (u, us)) = Definition (num, u, us)
blanchet@36486
   129
fun finish_tstp_inference_line ((num, us), deps) = Inference (num, us, deps)
blanchet@36393
   130
fun parse_tstp_line pool =
blanchet@36486
   131
     ((Scan.this_string "fof" -- $$ "(") |-- parse_integer --| $$ ","
blanchet@36486
   132
       --| Scan.this_string "definition" --| $$ "," -- parse_definition pool
blanchet@36486
   133
       --| parse_tstp_annotations --| $$ ")" --| $$ "."
blanchet@36486
   134
      >> finish_tstp_definition_line)
blanchet@36486
   135
  || ((Scan.this_string "cnf" -- $$ "(") |-- parse_integer --| $$ ","
blanchet@36486
   136
       --| Symbol.scan_id --| $$ "," -- parse_clause pool
blanchet@36486
   137
       -- parse_tstp_annotations --| $$ ")" --| $$ "."
blanchet@36486
   138
      >> finish_tstp_inference_line)
blanchet@36291
   139
blanchet@36291
   140
(**** PARSING OF SPASS OUTPUT ****)
blanchet@36291
   141
blanchet@36392
   142
(* SPASS returns clause references of the form "x.y". We ignore "y", whose role
blanchet@36392
   143
   is not clear anyway. *)
blanchet@36392
   144
val parse_dot_name = parse_integer --| $$ "." --| parse_integer
paulson@21978
   145
blanchet@36392
   146
val parse_spass_annotations =
blanchet@36392
   147
  Scan.optional ($$ ":" |-- Scan.repeat (parse_dot_name
blanchet@36392
   148
                                         --| Scan.option ($$ ","))) []
blanchet@36291
   149
blanchet@36392
   150
(* It is not clear why some literals are followed by sequences of stars. We
blanchet@36392
   151
   ignore them. *)
blanchet@36393
   152
fun parse_starred_predicate_term pool =
blanchet@36393
   153
  parse_predicate_term pool --| Scan.repeat ($$ "*" || $$ " ")
blanchet@36291
   154
blanchet@36393
   155
fun parse_horn_clause pool =
blanchet@36393
   156
  Scan.repeat (parse_starred_predicate_term pool) --| $$ "-" --| $$ ">"
blanchet@36393
   157
  -- Scan.repeat (parse_starred_predicate_term pool)
blanchet@36392
   158
  >> (fn ([], []) => [atom "c_False"]
blanchet@36486
   159
       | (clauses1, clauses2) => map negate_node clauses1 @ clauses2)
paulson@21978
   160
blanchet@36486
   161
(* Syntax: <num>[0:<inference><annotations>] ||
blanchet@36393
   162
           <cnf_formulas> -> <cnf_formulas>. *)
blanchet@36486
   163
fun finish_spass_line ((num, deps), us) = Inference (num, us, deps)
blanchet@36402
   164
fun parse_spass_line pool =
blanchet@36392
   165
  parse_integer --| $$ "[" --| $$ "0" --| $$ ":" --| Symbol.scan_id
blanchet@36392
   166
  -- parse_spass_annotations --| $$ "]" --| $$ "|" --| $$ "|"
blanchet@36393
   167
  -- parse_horn_clause pool --| $$ "."
blanchet@36486
   168
  >> finish_spass_line
blanchet@36291
   169
blanchet@36402
   170
fun parse_line pool = fst o (parse_tstp_line pool || parse_spass_line pool)
paulson@21978
   171
paulson@21978
   172
(**** INTERPRETATION OF TSTP SYNTAX TREES ****)
paulson@21978
   173
blanchet@36486
   174
exception NODE of node
paulson@21978
   175
paulson@21978
   176
(*If string s has the prefix s1, return the result of deleting it.*)
wenzelm@23139
   177
fun strip_prefix s1 s =
immler@31038
   178
  if String.isPrefix s1 s
blanchet@35865
   179
  then SOME (undo_ascii_of (String.extract (s, size s1, NONE)))
paulson@21978
   180
  else NONE;
paulson@21978
   181
paulson@21978
   182
(*Invert the table of translations between Isabelle and ATPs*)
paulson@21978
   183
val type_const_trans_table_inv =
blanchet@35865
   184
      Symtab.make (map swap (Symtab.dest type_const_trans_table));
paulson@21978
   185
paulson@21978
   186
fun invert_type_const c =
paulson@21978
   187
    case Symtab.lookup type_const_trans_table_inv c of
paulson@21978
   188
        SOME c' => c'
paulson@21978
   189
      | NONE => c;
paulson@21978
   190
blanchet@36285
   191
fun make_tvar s = TVar (("'" ^ s, 0), HOLogic.typeS);
blanchet@36285
   192
fun make_tparam s = TypeInfer.param 0 (s, HOLogic.typeS)
paulson@21978
   193
fun make_var (b,T) = Var((b,0),T);
paulson@21978
   194
paulson@21978
   195
(*Type variables are given the basic sort, HOL.type. Some will later be constrained
paulson@21978
   196
  by information from type literals, or by type inference.*)
blanchet@36486
   197
fun type_of_node (u as IntLeaf _) = raise NODE u
blanchet@36486
   198
  | type_of_node (u as StrNode (a, us)) =
blanchet@36486
   199
    let val Ts = map type_of_node us in
blanchet@36486
   200
      case strip_prefix tconst_prefix a of
blanchet@36486
   201
        SOME b => Type (invert_type_const b, Ts)
blanchet@36486
   202
      | NONE =>
blanchet@36486
   203
        if not (null us) then
blanchet@36486
   204
          raise NODE u  (*only tconsts have type arguments*)
blanchet@36486
   205
        else case strip_prefix tfree_prefix a of
blanchet@36486
   206
          SOME b => TFree ("'" ^ b, HOLogic.typeS)
blanchet@36486
   207
        | NONE =>
blanchet@36486
   208
          case strip_prefix tvar_prefix a of
blanchet@36486
   209
            SOME b => make_tvar b
blanchet@36486
   210
          | NONE => make_tparam a  (* Variable from the ATP, say "X1" *)
blanchet@36486
   211
    end
paulson@21978
   212
paulson@21978
   213
(*Invert the table of translations between Isabelle and ATPs*)
paulson@21978
   214
val const_trans_table_inv =
blanchet@36402
   215
  Symtab.update ("fequal", @{const_name "op ="})
blanchet@36402
   216
                (Symtab.make (map swap (Symtab.dest const_trans_table)))
paulson@21978
   217
blanchet@36402
   218
fun invert_const c = c |> Symtab.lookup const_trans_table_inv |> the_default 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
blanchet@36486
   226
fun fix_atp_variable_name s =
blanchet@36486
   227
  let
blanchet@36486
   228
    fun subscript_name s n = s ^ nat_subscript n
blanchet@36486
   229
    val s = String.map Char.toLower s
blanchet@36486
   230
  in
blanchet@36486
   231
    case space_explode "_" s of
blanchet@36486
   232
      [_] => (case take_suffix Char.isDigit (String.explode s) of
blanchet@36486
   233
                (cs1 as _ :: _, cs2 as _ :: _) =>
blanchet@36486
   234
                subscript_name (String.implode cs1)
blanchet@36486
   235
                               (the (Int.fromString (String.implode cs2)))
blanchet@36486
   236
              | (_, _) => s)
blanchet@36486
   237
    | [s1, s2] => (case Int.fromString s2 of
blanchet@36486
   238
                     SOME n => subscript_name s1 n
blanchet@36486
   239
                   | NONE => s)
blanchet@36486
   240
    | _ => s
blanchet@36486
   241
  end
blanchet@36486
   242
paulson@21978
   243
(*First-order translation. No types are known for variables. HOLogic.typeT should allow
paulson@21978
   244
  them to be inferred.*)
blanchet@36486
   245
fun term_of_node args thy u =
blanchet@36486
   246
  case u of
blanchet@36486
   247
    IntLeaf _ => raise NODE u
blanchet@36486
   248
  | StrNode ("hBOOL", [u]) => term_of_node [] thy u  (* ignore hBOOL *)
blanchet@36486
   249
  | StrNode ("hAPP", [u1, u2]) => term_of_node (u2 :: args) thy u1
blanchet@36486
   250
  | StrNode (a, us) =>
blanchet@36486
   251
    case strip_prefix const_prefix a of
blanchet@36486
   252
      SOME "equal" =>
blanchet@36486
   253
      list_comb (Const (@{const_name "op ="}, HOLogic.typeT),
blanchet@36486
   254
                 map (term_of_node [] thy) us)
blanchet@36486
   255
    | SOME b =>
blanchet@36486
   256
      let
blanchet@36486
   257
        val c = invert_const b
blanchet@36486
   258
        val nterms = length us - num_typargs thy c
blanchet@36486
   259
        val ts = map (term_of_node [] thy) (take nterms us @ args)
blanchet@36486
   260
        (*Extra args from hAPP come AFTER any arguments given directly to the
blanchet@36486
   261
          constant.*)
blanchet@36486
   262
        val Ts = map type_of_node (drop nterms us)
blanchet@36486
   263
      in list_comb(const_of thy (c, Ts), ts) end
blanchet@36486
   264
    | NONE => (*a variable, not a constant*)
blanchet@36486
   265
      let
blanchet@36486
   266
        val opr =
blanchet@36486
   267
          (* a Free variable is typically a Skolem function *)
blanchet@36486
   268
          case strip_prefix fixed_var_prefix a of
blanchet@36486
   269
            SOME b => Free (b, HOLogic.typeT)
blanchet@36486
   270
          | NONE =>
blanchet@36486
   271
            case strip_prefix schematic_var_prefix a of
blanchet@36486
   272
              SOME b => make_var (b, HOLogic.typeT)
blanchet@36486
   273
            | NONE =>
blanchet@36486
   274
              (* Variable from the ATP, say "X1" *)
blanchet@36486
   275
              make_var (fix_atp_variable_name a, HOLogic.typeT)
blanchet@36486
   276
      in list_comb (opr, map (term_of_node [] thy) (us @ args)) end
paulson@21978
   277
blanchet@36392
   278
(* Type class literal applied to a type. Returns triple of polarity, class,
blanchet@36392
   279
   type. *)
blanchet@36486
   280
fun constraint_of_node pos (StrNode ("c_Not", [u])) =
blanchet@36486
   281
    constraint_of_node (not pos) u
blanchet@36486
   282
  | constraint_of_node pos u = case u of
blanchet@36486
   283
        IntLeaf _ => raise NODE u
blanchet@36486
   284
      | StrNode (a, us) =>
blanchet@36486
   285
            (case (strip_prefix class_prefix a, map type_of_node us) of
blanchet@36486
   286
                 (SOME b, [T]) => (pos, b, T)
blanchet@36486
   287
               | _ => raise NODE u)
paulson@21978
   288
paulson@21978
   289
(** Accumulate type constraints in a clause: negative type literals **)
paulson@21978
   290
blanchet@36485
   291
fun add_var (key, z)  = Vartab.map_default (key, []) (cons z)
paulson@21978
   292
blanchet@36485
   293
fun add_constraint ((false, cl, TFree(a,_)), vt) = add_var ((a,~1),cl) vt
blanchet@36485
   294
  | add_constraint ((false, cl, TVar(ix,_)), vt) = add_var (ix,cl) vt
paulson@21978
   295
  | add_constraint (_, vt) = vt;
paulson@21978
   296
blanchet@36402
   297
fun is_positive_literal (@{const Trueprop} $ t) = is_positive_literal t
blanchet@36402
   298
  | is_positive_literal (@{const Not} $ _) = false
blanchet@36402
   299
  | is_positive_literal t = true
blanchet@36402
   300
blanchet@36485
   301
fun negate_term thy (Const (@{const_name All}, T) $ Abs (s, T', t')) =
blanchet@36402
   302
    Const (@{const_name Ex}, T) $ Abs (s, T', negate_term thy t')
blanchet@36402
   303
  | negate_term thy (Const (@{const_name Ex}, T) $ Abs (s, T', t')) =
blanchet@36402
   304
    Const (@{const_name All}, T) $ Abs (s, T', negate_term thy t')
blanchet@36402
   305
  | negate_term thy (@{const "op -->"} $ t1 $ t2) =
blanchet@36402
   306
    @{const "op &"} $ t1 $ negate_term thy t2
blanchet@36402
   307
  | negate_term thy (@{const "op &"} $ t1 $ t2) =
blanchet@36402
   308
    @{const "op |"} $ negate_term thy t1 $ negate_term thy t2
blanchet@36402
   309
  | negate_term thy (@{const "op |"} $ t1 $ t2) =
blanchet@36402
   310
    @{const "op &"} $ negate_term thy t1 $ negate_term thy t2
blanchet@36486
   311
  | negate_term _ (@{const Not} $ t) = t
blanchet@36486
   312
  | negate_term _ t = @{const Not} $ t
blanchet@36485
   313
fun negate_formula thy (@{const Trueprop} $ t) =
blanchet@36485
   314
    @{const Trueprop} $ negate_term thy t
blanchet@36485
   315
  | negate_formula thy t =
blanchet@36485
   316
    if fastype_of t = @{typ prop} then Logic.mk_implies (t, @{prop False})
blanchet@36485
   317
    else @{const Not} $ t
blanchet@36402
   318
blanchet@36402
   319
fun clause_for_literals _ [] = HOLogic.false_const
blanchet@36402
   320
  | clause_for_literals _ [lit] = lit
blanchet@36402
   321
  | clause_for_literals thy lits =
blanchet@36402
   322
    case List.partition is_positive_literal lits of
blanchet@36402
   323
      (pos_lits as _ :: _, neg_lits as _ :: _) =>
blanchet@36402
   324
      @{const "op -->"}
blanchet@36402
   325
          $ foldr1 HOLogic.mk_conj (map (negate_term thy) neg_lits)
blanchet@36402
   326
          $ foldr1 HOLogic.mk_disj pos_lits
blanchet@36402
   327
    | _ => foldr1 HOLogic.mk_disj lits
blanchet@36402
   328
blanchet@36402
   329
(* Final treatment of the list of "real" literals from a clause.
blanchet@36402
   330
   No "real" literals means only type information. *)
blanchet@36402
   331
fun finish_clause _ [] = HOLogic.true_const
blanchet@36402
   332
  | finish_clause thy lits =
blanchet@36402
   333
    lits |> filter_out (curry (op =) HOLogic.false_const) |> rev
blanchet@36402
   334
         |> clause_for_literals thy
paulson@22491
   335
paulson@21978
   336
(*Accumulate sort constraints in vt, with "real" literals in lits.*)
blanchet@36486
   337
fun lits_of_nodes thy (vt, lits) [] = (vt, finish_clause thy lits)
blanchet@36486
   338
  | lits_of_nodes thy (vt, lits) (u :: us) =
blanchet@36486
   339
    lits_of_nodes thy (add_constraint (constraint_of_node true u, vt), lits) us
blanchet@36486
   340
    handle NODE _ => lits_of_nodes thy (vt, term_of_node [] thy u :: lits) us
paulson@21978
   341
paulson@21978
   342
(*Update TVars/TFrees with detected sort constraints.*)
blanchet@36393
   343
fun repair_sorts vt =
paulson@21978
   344
  let fun tysubst (Type (a, Ts)) = Type (a, map tysubst Ts)
wenzelm@33035
   345
        | tysubst (TVar (xi, s)) = TVar (xi, the_default s (Vartab.lookup vt xi))
wenzelm@33035
   346
        | tysubst (TFree (x, s)) = TFree (x, the_default s (Vartab.lookup vt (x, ~1)))
paulson@21978
   347
      fun tmsubst (Const (a, T)) = Const (a, tysubst T)
paulson@21978
   348
        | tmsubst (Free (a, T)) = Free (a, tysubst T)
paulson@21978
   349
        | tmsubst (Var (xi, T)) = Var (xi, tysubst T)
paulson@21978
   350
        | tmsubst (t as Bound _) = t
paulson@21978
   351
        | tmsubst (Abs (a, T, t)) = Abs (a, tysubst T, tmsubst t)
blanchet@36486
   352
        | tmsubst (t1 $ t2) = tmsubst t1 $ tmsubst t2
blanchet@36285
   353
  in not (Vartab.is_empty vt) ? tmsubst end;
paulson@21978
   354
blanchet@36486
   355
(* Interpret a list of syntax trees as a clause, given by "real" literals and
blanchet@36486
   356
   sort constraints. "vt" holds the initial sort constraints, from the
blanchet@36486
   357
   conjecture clauses. *)
blanchet@36486
   358
fun clause_of_nodes ctxt vt us =
blanchet@36486
   359
  let val (vt, dt) = lits_of_nodes (ProofContext.theory_of ctxt) (vt, []) us in
blanchet@36486
   360
    dt |> repair_sorts vt
blanchet@36291
   361
  end
blanchet@36486
   362
fun check_clause ctxt =
blanchet@36486
   363
  TypeInfer.constrain HOLogic.boolT
blanchet@36486
   364
  #> Syntax.check_term (ProofContext.set_mode ProofContext.mode_schematic ctxt)
blanchet@36486
   365
fun checked_clause_of_nodes ctxt = check_clause ctxt oo clause_of_nodes ctxt
paulson@21978
   366
blanchet@36486
   367
(** Global sort constraints on TFrees (from tfree_tcs) are positive unit
blanchet@36486
   368
    clauses. **)
paulson@21978
   369
blanchet@36486
   370
fun add_tfree_constraint (true, cl, TFree (a, _)) = add_var ((a, ~1), cl)
blanchet@36486
   371
  | add_tfree_constraint _ = I
paulson@21978
   372
fun tfree_constraints_of_clauses vt [] = vt
blanchet@36486
   373
  | tfree_constraints_of_clauses vt ([lit] :: uss) =
blanchet@36486
   374
    (tfree_constraints_of_clauses (add_tfree_constraint
blanchet@36486
   375
                                          (constraint_of_node true lit) vt) uss
blanchet@36486
   376
     handle NODE _ => (* Not a positive type constraint? Ignore the literal. *)
blanchet@36486
   377
     tfree_constraints_of_clauses vt uss)
blanchet@36486
   378
  | tfree_constraints_of_clauses vt (_ :: uss) =
blanchet@36486
   379
    tfree_constraints_of_clauses vt uss
paulson@21978
   380
paulson@21978
   381
paulson@21978
   382
(**** Translation of TSTP files to Isar Proofs ****)
paulson@21978
   383
blanchet@36486
   384
fun unvarify_term (Var ((s, 0), T)) = Free (s, T)
blanchet@36486
   385
  | unvarify_term t = raise TERM ("unvarify_term: non-Var", [t])
paulson@21978
   386
blanchet@36486
   387
fun clauses_in_lines (Definition (_, u, us)) = u :: us
blanchet@36486
   388
  | clauses_in_lines (Inference (_, us, _)) = us
paulson@21978
   389
blanchet@36486
   390
fun decode_line vt (Definition (num, u, us)) ctxt =
blanchet@36486
   391
    let
blanchet@36486
   392
      val cl1 = clause_of_nodes ctxt vt [u]
blanchet@36486
   393
      val vars = snd (strip_comb cl1)
blanchet@36486
   394
      val frees = map unvarify_term vars
blanchet@36486
   395
      val unvarify_args = subst_atomic (vars ~~ frees)
blanchet@36486
   396
      val cl2 = clause_of_nodes ctxt vt us
blanchet@36486
   397
      val (cl1, cl2) =
blanchet@36486
   398
        HOLogic.eq_const HOLogic.typeT $ cl1 $ cl2
blanchet@36486
   399
        |> unvarify_args |> check_clause ctxt |> HOLogic.dest_eq
blanchet@36486
   400
    in
blanchet@36486
   401
      (Definition (num, cl1, cl2),
blanchet@36486
   402
       fold Variable.declare_term (maps OldTerm.term_frees [cl1, cl2]) ctxt)
blanchet@36486
   403
    end
blanchet@36486
   404
  | decode_line vt (Inference (num, us, deps)) ctxt =
blanchet@36486
   405
    let val cl = us |> clause_of_nodes ctxt vt |> check_clause ctxt in
blanchet@36486
   406
      (Inference (num, cl, deps),
blanchet@36486
   407
       fold Variable.declare_term (OldTerm.term_frees cl) ctxt)
blanchet@36486
   408
    end
blanchet@36486
   409
fun decode_lines ctxt lines =
blanchet@36486
   410
  let
blanchet@36486
   411
    val vt = tfree_constraints_of_clauses Vartab.empty
blanchet@36486
   412
                                          (map clauses_in_lines lines)
blanchet@36486
   413
  in #1 (fold_map (decode_line vt) lines ctxt) end
paulson@21978
   414
blanchet@36486
   415
fun aint_inference _ (Definition _) = true
blanchet@36486
   416
  | aint_inference t (Inference (_, t', _)) = not (t aconv t')
blanchet@36486
   417
blanchet@36486
   418
(* No "real" literals means only type information (tfree_tcs, clsrel, or
blanchet@36486
   419
   clsarity). *)
blanchet@36486
   420
val is_only_type_information = curry (op aconv) HOLogic.true_const
blanchet@36486
   421
blanchet@36486
   422
fun replace_one_dep (old, new) dep = if dep = old then new else [dep]
blanchet@36486
   423
fun replace_deps_in_line _ (line as Definition _) = line
blanchet@36486
   424
  | replace_deps_in_line p (Inference (num, t, deps)) =
blanchet@36486
   425
    Inference (num, t, fold (union (op =) o replace_one_dep p) deps [])
paulson@21978
   426
paulson@22491
   427
(*Discard axioms; consolidate adjacent lines that prove the same clause, since they differ
paulson@22491
   428
  only in type information.*)
blanchet@36486
   429
fun add_line _ (line as Definition _) lines = line :: lines
blanchet@36486
   430
  | add_line thm_names (Inference (num, t, [])) lines =
blanchet@36486
   431
    (* No dependencies: axiom or conjecture clause *)
blanchet@36486
   432
    if is_axiom_clause_number thm_names num then
blanchet@36486
   433
      (* Axioms are not proof lines. *)
blanchet@36486
   434
      if is_only_type_information t then
blanchet@36486
   435
        map (replace_deps_in_line (num, [])) lines
blanchet@36486
   436
      (* Is there a repetition? If so, replace later line by earlier one. *)
blanchet@36486
   437
      else case take_prefix (aint_inference t) lines of
blanchet@36486
   438
        (_, []) => lines (*no repetition of proof line*)
blanchet@36486
   439
      | (pre, Inference (num', _, _) :: post) =>
blanchet@36486
   440
        pre @ map (replace_deps_in_line (num', [num])) post
blanchet@36486
   441
    else
blanchet@36486
   442
      Inference (num, t, []) :: lines
blanchet@36486
   443
  | add_line _ (Inference (num, t, deps)) lines =
blanchet@36486
   444
    (* Type information will be deleted later; skip repetition test. *)
blanchet@36486
   445
    if is_only_type_information t then
blanchet@36486
   446
      Inference (num, t, deps) :: lines
blanchet@36486
   447
    (* Is there a repetition? If so, replace later line by earlier one. *)
blanchet@36486
   448
    else case take_prefix (aint_inference t) lines of
blanchet@36486
   449
      (* FIXME: Doesn't this code risk conflating proofs involving different
blanchet@36486
   450
         types?? *)
blanchet@36486
   451
       (_, []) => Inference (num, t, deps) :: lines
blanchet@36486
   452
     | (pre, Inference (num', t', _) :: post) =>
blanchet@36486
   453
       Inference (num, t', deps) ::
blanchet@36486
   454
       pre @ map (replace_deps_in_line (num', [num])) post
paulson@22044
   455
blanchet@36486
   456
(* Recursively delete empty lines (type information) from the proof. *)
blanchet@36486
   457
fun add_nontrivial_line (Inference (num, t, [])) lines =
blanchet@36486
   458
    if is_only_type_information t then delete_dep num lines
blanchet@36486
   459
    else Inference (num, t, []) :: lines
blanchet@36486
   460
  | add_nontrivial_line line lines = line :: lines
blanchet@36395
   461
and delete_dep num lines =
blanchet@36486
   462
  fold_rev add_nontrivial_line (map (replace_deps_in_line (num, [])) lines) []
blanchet@36486
   463
blanchet@36486
   464
fun is_bad_free (Free (a, _)) = String.isPrefix skolem_prefix a
blanchet@36486
   465
  | is_bad_free _ = false
paulson@22470
   466
blanchet@36486
   467
fun add_desired_line _ _ (line as Definition _) (j, lines) = (j, line :: lines)
blanchet@36486
   468
  | add_desired_line ctxt _ (Inference (num, t, [])) (j, lines) =
blanchet@36486
   469
    (j, Inference (num, t, []) :: lines)  (* conjecture clauses must be kept *)
blanchet@36486
   470
  | add_desired_line ctxt shrink_factor (Inference (num, t, deps)) (j, lines) =
blanchet@36402
   471
    (j + 1,
blanchet@36486
   472
     if is_only_type_information t orelse
blanchet@36486
   473
        not (null (Term.add_tvars t [])) orelse
blanchet@36486
   474
        exists_subterm is_bad_free t orelse
blanchet@36474
   475
        (length deps < 2 orelse j mod shrink_factor <> 0) then
blanchet@36486
   476
       map (replace_deps_in_line (num, deps)) lines  (* delete line *)
blanchet@36402
   477
     else
blanchet@36486
   478
       Inference (num, t, deps) :: lines)
paulson@21978
   479
blanchet@36402
   480
(** EXTRACTING LEMMAS **)
paulson@21979
   481
blanchet@36223
   482
(* A list consisting of the first number in each line is returned.
blanchet@36395
   483
   TSTP: Interesting lines have the form "cnf(108, axiom, ...)", where the
blanchet@36223
   484
   number (108) is extracted.
blanchet@36395
   485
   SPASS: Lines have the form "108[0:Inp] ...", where the first number (108) is
blanchet@36223
   486
   extracted. *)
blanchet@36402
   487
fun extract_clause_numbers_in_atp_proof atp_proof =
blanchet@35865
   488
  let
blanchet@36395
   489
    val tokens_of = String.tokens (not o is_ident_char)
blanchet@36402
   490
    fun extract_num ("cnf" :: num :: "axiom" :: _) = Int.fromString num
blanchet@36402
   491
      | extract_num ("cnf" :: num :: "negated_conjecture" :: _) =
blanchet@36402
   492
        Int.fromString num
blanchet@36395
   493
      | extract_num (num :: "0" :: "Inp" :: _) = Int.fromString num
blanchet@36395
   494
      | extract_num _ = NONE
blanchet@36402
   495
  in atp_proof |> split_lines |> map_filter (extract_num o tokens_of) end
wenzelm@33310
   496
  
blanchet@36395
   497
(* Used to label theorems chained into the Sledgehammer call (or rather
blanchet@36395
   498
   goal?) *)
blanchet@36395
   499
val chained_hint = "sledgehammer_chained"
blanchet@35865
   500
blanchet@36063
   501
fun apply_command _ 1 = "by "
blanchet@36063
   502
  | apply_command 1 _ = "apply "
blanchet@36063
   503
  | apply_command i _ = "prefer " ^ string_of_int i ^ " apply "
blanchet@36063
   504
fun metis_command i n [] =
blanchet@36063
   505
    apply_command i n ^ "metis"
blanchet@36063
   506
  | metis_command i n xs =
blanchet@36063
   507
    apply_command i n ^ "(metis " ^ space_implode " " xs ^ ")"
blanchet@36063
   508
fun metis_line i n xs =
blanchet@36063
   509
  "Try this command: " ^
blanchet@36063
   510
  Markup.markup Markup.sendback (metis_command i n xs) ^ ".\n" 
blanchet@36281
   511
fun minimize_line _ [] = ""
blanchet@36281
   512
  | minimize_line minimize_command facts =
blanchet@36281
   513
    case minimize_command facts of
blanchet@36281
   514
      "" => ""
blanchet@36281
   515
    | command =>
blanchet@36065
   516
      "To minimize the number of lemmas, try this command: " ^
blanchet@36281
   517
      Markup.markup Markup.sendback command ^ ".\n"
immler@31840
   518
blanchet@36402
   519
fun metis_proof_text (minimize_command, atp_proof, thm_names, goal, i) =
blanchet@36063
   520
  let
blanchet@36231
   521
    val lemmas =
blanchet@36402
   522
      atp_proof |> extract_clause_numbers_in_atp_proof
blanchet@36402
   523
                |> filter (is_axiom_clause_number thm_names)
blanchet@36402
   524
                |> map (fn i => Vector.sub (thm_names, i - 1))
blanchet@36402
   525
                |> filter_out (fn s => s = "??.unknown" orelse s = chained_hint)
blanchet@36402
   526
                |> sort_distinct string_ord
blanchet@36063
   527
    val n = Logic.count_prems (prop_of goal)
blanchet@36395
   528
  in (metis_line i n lemmas ^ minimize_line minimize_command lemmas, lemmas) end
immler@31037
   529
blanchet@36486
   530
val is_valid_line =
blanchet@36486
   531
  String.isPrefix "fof(" orf String.isPrefix "cnf(" orf String.isSubstring "||"
blanchet@36402
   532
blanchet@36486
   533
(** Isar proof construction and manipulation **)
blanchet@36486
   534
blanchet@36486
   535
fun merge_fact_sets (ls1, ss1) (ls2, ss2) =
blanchet@36486
   536
  (union (op =) ls1 ls2, union (op =) ss1 ss2)
blanchet@36402
   537
blanchet@36402
   538
type label = string * int
blanchet@36402
   539
type facts = label list * string list
blanchet@36402
   540
blanchet@36402
   541
datatype qualifier = Show | Then | Moreover | Ultimately
blanchet@36291
   542
blanchet@36402
   543
datatype step =
blanchet@36478
   544
  Fix of (string * typ) list |
blanchet@36486
   545
  Let of term * term |
blanchet@36402
   546
  Assume of label * term |
blanchet@36402
   547
  Have of qualifier list * label * term * byline
blanchet@36402
   548
and byline =
blanchet@36402
   549
  Facts of facts |
blanchet@36402
   550
  CaseSplit of step list list * facts
blanchet@36402
   551
blanchet@36402
   552
val raw_prefix = "X"
blanchet@36402
   553
val assum_prefix = "A"
blanchet@36402
   554
val fact_prefix = "F"
blanchet@36402
   555
blanchet@36475
   556
fun add_fact_from_dep thm_names num =
blanchet@36475
   557
  if is_axiom_clause_number thm_names num then
blanchet@36480
   558
    apsnd (insert (op =) (Vector.sub (thm_names, num - 1)))
blanchet@36475
   559
  else
blanchet@36480
   560
    apfst (insert (op =) (raw_prefix, num))
blanchet@36402
   561
blanchet@36486
   562
fun quantify_over_all_vars t = fold_rev Logic.all (map Var ((*Term.add_vars t###*) [])) t
blanchet@36486
   563
fun step_for_line _ _ (Definition (num, t1, t2)) = Let (t1, t2)
blanchet@36486
   564
  | step_for_line _ _ (Inference (num, t, [])) = Assume ((raw_prefix, num), t)
blanchet@36486
   565
  | step_for_line thm_names j (Inference (num, t, deps)) =
blanchet@36486
   566
    Have (if j = 1 then [Show] else [], (raw_prefix, num),
blanchet@36486
   567
          quantify_over_all_vars (HOLogic.mk_Trueprop t),
blanchet@36475
   568
          Facts (fold (add_fact_from_dep thm_names) deps ([], [])))
blanchet@36291
   569
blanchet@36291
   570
fun strip_spaces_in_list [] = ""
blanchet@36402
   571
  | strip_spaces_in_list [c1] = if Char.isSpace c1 then "" else str c1
blanchet@36402
   572
  | strip_spaces_in_list [c1, c2] =
blanchet@36402
   573
    strip_spaces_in_list [c1] ^ strip_spaces_in_list [c2]
blanchet@36291
   574
  | strip_spaces_in_list (c1 :: c2 :: c3 :: cs) =
blanchet@36291
   575
    if Char.isSpace c1 then
blanchet@36291
   576
      strip_spaces_in_list (c2 :: c3 :: cs)
blanchet@36291
   577
    else if Char.isSpace c2 then
blanchet@36291
   578
      if Char.isSpace c3 then
blanchet@36291
   579
        strip_spaces_in_list (c1 :: c3 :: cs)
blanchet@36291
   580
      else
blanchet@36402
   581
        str c1 ^ (if forall is_ident_char [c1, c3] then " " else "") ^
blanchet@36291
   582
        strip_spaces_in_list (c3 :: cs)
blanchet@36291
   583
    else
blanchet@36291
   584
      str c1 ^ strip_spaces_in_list (c2 :: c3 :: cs)
blanchet@36291
   585
val strip_spaces = strip_spaces_in_list o String.explode
blanchet@36291
   586
blanchet@36474
   587
fun proof_from_atp_proof pool ctxt shrink_factor atp_proof thm_names frees =
blanchet@36402
   588
  let
blanchet@36486
   589
    val lines =
blanchet@36486
   590
      atp_proof
blanchet@36486
   591
      |> split_lines |> map strip_spaces |> filter is_valid_line
blanchet@36486
   592
      |> map (parse_line pool o explode)
blanchet@36486
   593
      |> decode_lines ctxt
blanchet@36486
   594
      |> rpair [] |-> fold_rev (add_line thm_names)
blanchet@36486
   595
      |> rpair [] |-> fold_rev add_nontrivial_line
blanchet@36486
   596
      |> rpair (0, []) |-> fold_rev (add_desired_line ctxt shrink_factor)
blanchet@36486
   597
      |> snd
blanchet@36402
   598
  in
blanchet@36402
   599
    (if null frees then [] else [Fix frees]) @
blanchet@36486
   600
    map2 (step_for_line thm_names) (length lines downto 1) lines
blanchet@36402
   601
  end
blanchet@36402
   602
blanchet@36402
   603
val indent_size = 2
blanchet@36402
   604
val no_label = ("", ~1)
blanchet@36402
   605
blanchet@36402
   606
fun no_show qs = not (member (op =) qs Show)
blanchet@36402
   607
blanchet@36402
   608
(* When redirecting proofs, we keep information about the labels seen so far in
blanchet@36402
   609
   the "backpatches" data structure. The first component indicates which facts
blanchet@36402
   610
   should be associated with forthcoming proof steps. The second component is a
blanchet@36402
   611
   pair ("keep_ls", "drop_ls"), where "keep_ls" are the labels to keep and
blanchet@36402
   612
   "drop_ls" are those that should be dropped in a case split. *)
blanchet@36402
   613
type backpatches = (label * facts) list * (label list * label list)
blanchet@36402
   614
blanchet@36402
   615
fun using_of_step (Have (_, _, _, by)) =
blanchet@36402
   616
    (case by of
blanchet@36402
   617
       Facts (ls, _) => ls
blanchet@36402
   618
     | CaseSplit (proofs, (ls, _)) => fold (union (op =) o using_of) proofs ls)
blanchet@36402
   619
  | using_of_step _ = []
blanchet@36402
   620
and using_of proof = fold (union (op =) o using_of_step) proof []
blanchet@36402
   621
blanchet@36402
   622
fun new_labels_of_step (Fix _) = []
blanchet@36486
   623
  | new_labels_of_step (Let _) = []
blanchet@36402
   624
  | new_labels_of_step (Assume (l, _)) = [l]
blanchet@36402
   625
  | new_labels_of_step (Have (_, l, _, _)) = [l]
blanchet@36402
   626
val new_labels_of = maps new_labels_of_step
blanchet@36402
   627
blanchet@36402
   628
val join_proofs =
blanchet@36402
   629
  let
blanchet@36402
   630
    fun aux _ [] = NONE
blanchet@36402
   631
      | aux proof_tail (proofs as (proof1 :: _)) =
blanchet@36402
   632
        if exists null proofs then
blanchet@36402
   633
          NONE
blanchet@36402
   634
        else if forall (curry (op =) (hd proof1) o hd) (tl proofs) then
blanchet@36402
   635
          aux (hd proof1 :: proof_tail) (map tl proofs)
blanchet@36402
   636
        else case hd proof1 of
blanchet@36402
   637
          Have ([], l, t, by) =>
blanchet@36402
   638
          if forall (fn Have ([], l', t', _) :: _ => (l, t) = (l', t')
blanchet@36402
   639
                      | _ => false) (tl proofs) andalso
blanchet@36402
   640
             not (exists (member (op =) (maps new_labels_of proofs))
blanchet@36402
   641
                         (using_of proof_tail)) then
blanchet@36402
   642
            SOME (l, t, map rev proofs, proof_tail)
blanchet@36402
   643
          else
blanchet@36402
   644
            NONE
blanchet@36402
   645
        | _ => NONE
blanchet@36402
   646
  in aux [] o map rev end
blanchet@36402
   647
blanchet@36402
   648
fun case_split_qualifiers proofs =
blanchet@36402
   649
  case length proofs of
blanchet@36402
   650
    0 => []
blanchet@36402
   651
  | 1 => [Then]
blanchet@36402
   652
  | _ => [Ultimately]
blanchet@36402
   653
blanchet@36402
   654
val index_in_shape = find_index o exists o curry (op =)
blanchet@36402
   655
blanchet@36402
   656
fun direct_proof thy conjecture_shape hyp_ts concl_t proof =
wenzelm@33310
   657
  let
blanchet@36402
   658
    val concl_ls = map (pair raw_prefix) (List.last conjecture_shape)
blanchet@36402
   659
    fun find_hyp (_, j) = nth hyp_ts (index_in_shape j conjecture_shape)
blanchet@36402
   660
    fun first_pass ([], contra) = ([], contra)
blanchet@36402
   661
      | first_pass ((ps as Fix _) :: proof, contra) =
blanchet@36402
   662
        first_pass (proof, contra) |>> cons ps
blanchet@36486
   663
      | first_pass ((ps as Let _) :: proof, contra) =
blanchet@36486
   664
        first_pass (proof, contra) |>> cons ps
blanchet@36402
   665
      | first_pass ((ps as Assume (l, t)) :: proof, contra) =
blanchet@36402
   666
        if member (op =) concl_ls l then
blanchet@36402
   667
          first_pass (proof, contra ||> cons ps)
blanchet@36402
   668
        else
blanchet@36402
   669
          first_pass (proof, contra) |>> cons (Assume (l, find_hyp l))
blanchet@36402
   670
      | first_pass ((ps as Have (qs, l, t, Facts (ls, ss))) :: proof, contra) =
blanchet@36402
   671
        if exists (member (op =) (fst contra)) ls then
blanchet@36402
   672
          first_pass (proof, contra |>> cons l ||> cons ps)
blanchet@36402
   673
        else
blanchet@36402
   674
          first_pass (proof, contra) |>> cons ps
blanchet@36402
   675
      | first_pass _ = raise Fail "malformed proof"
blanchet@36402
   676
    val (proof_top, (contra_ls, contra_proof)) =
blanchet@36402
   677
      first_pass (proof, (concl_ls, []))
blanchet@36402
   678
    val backpatch_label = the_default ([], []) oo AList.lookup (op =) o fst
blanchet@36402
   679
    fun backpatch_labels patches ls =
blanchet@36402
   680
      fold merge_fact_sets (map (backpatch_label patches) ls) ([], [])
blanchet@36402
   681
    fun second_pass end_qs ([], assums, patches) =
blanchet@36402
   682
        ([Have (end_qs, no_label,
blanchet@36402
   683
                if length assums < length concl_ls then
blanchet@36485
   684
                  clause_for_literals thy
blanchet@36485
   685
                                      (map (negate_formula thy o fst) assums)
blanchet@36402
   686
                else
blanchet@36402
   687
                  concl_t,
blanchet@36402
   688
                Facts (backpatch_labels patches (map snd assums)))], patches)
blanchet@36402
   689
      | second_pass end_qs (Assume (l, t) :: proof, assums, patches) =
blanchet@36402
   690
        second_pass end_qs (proof, (t, l) :: assums, patches)
blanchet@36402
   691
      | second_pass end_qs (Have (qs, l, t, Facts (ls, ss)) :: proof, assums,
blanchet@36402
   692
                            patches) =
blanchet@36402
   693
        if member (op =) (snd (snd patches)) l andalso
blanchet@36402
   694
           not (AList.defined (op =) (fst patches) l) then
blanchet@36402
   695
          second_pass end_qs (proof, assums, patches ||> apsnd (append ls))
blanchet@36402
   696
        else
blanchet@36402
   697
          (case List.partition (member (op =) contra_ls) ls of
blanchet@36402
   698
             ([contra_l], co_ls) =>
blanchet@36402
   699
             if no_show qs then
blanchet@36402
   700
               second_pass end_qs
blanchet@36402
   701
                           (proof, assums,
blanchet@36402
   702
                            patches |>> cons (contra_l, (l :: co_ls, ss)))
blanchet@36402
   703
               |>> cons (if member (op =) (fst (snd patches)) l then
blanchet@36485
   704
                           Assume (l, negate_formula thy t)
blanchet@36402
   705
                         else
blanchet@36485
   706
                           Have (qs, l, negate_formula thy t,
blanchet@36402
   707
                                 Facts (backpatch_label patches l)))
blanchet@36402
   708
             else
blanchet@36402
   709
               second_pass end_qs (proof, assums,
blanchet@36402
   710
                                   patches |>> cons (contra_l, (co_ls, ss)))
blanchet@36402
   711
           | (contra_ls as _ :: _, co_ls) =>
blanchet@36402
   712
             let
blanchet@36402
   713
               val proofs =
blanchet@36402
   714
                 map_filter
blanchet@36402
   715
                     (fn l =>
blanchet@36402
   716
                         if member (op =) concl_ls l then
blanchet@36402
   717
                           NONE
blanchet@36402
   718
                         else
blanchet@36402
   719
                           let
blanchet@36402
   720
                             val drop_ls = filter (curry (op <>) l) contra_ls
blanchet@36402
   721
                           in
blanchet@36402
   722
                             second_pass []
blanchet@36402
   723
                                 (proof, assums,
blanchet@36402
   724
                                  patches ||> apfst (insert (op =) l)
blanchet@36402
   725
                                          ||> apsnd (union (op =) drop_ls))
blanchet@36402
   726
                             |> fst |> SOME
blanchet@36402
   727
                           end) contra_ls
blanchet@36402
   728
               val facts = (co_ls, [])
blanchet@36402
   729
             in
blanchet@36402
   730
               (case join_proofs proofs of
blanchet@36402
   731
                  SOME (l, t, proofs, proof_tail) =>
blanchet@36402
   732
                  Have (case_split_qualifiers proofs @
blanchet@36402
   733
                        (if null proof_tail then end_qs else []), l, t,
blanchet@36402
   734
                        CaseSplit (proofs, facts)) :: proof_tail
blanchet@36402
   735
                | NONE =>
blanchet@36402
   736
                  [Have (case_split_qualifiers proofs @ end_qs, no_label,
blanchet@36402
   737
                         concl_t, CaseSplit (proofs, facts))],
blanchet@36402
   738
                patches)
blanchet@36402
   739
             end
blanchet@36402
   740
           | _ => raise Fail "malformed proof")
blanchet@36402
   741
       | second_pass _ _ = raise Fail "malformed proof"
blanchet@36486
   742
    val proof_bottom =
blanchet@36486
   743
      second_pass [Show] (contra_proof, [], ([], ([], []))) |> fst
blanchet@36402
   744
  in proof_top @ proof_bottom end
blanchet@36402
   745
blanchet@36402
   746
val kill_duplicate_assumptions_in_proof =
blanchet@36402
   747
  let
blanchet@36402
   748
    fun relabel_facts subst =
blanchet@36402
   749
      apfst (map (fn l => AList.lookup (op =) subst l |> the_default l))
blanchet@36486
   750
    fun do_step (ps as Assume (l, t)) (proof, subst, assums) =
blanchet@36402
   751
        (case AList.lookup (op aconv) assums t of
blanchet@36402
   752
           SOME l' => (proof, (l', l) :: subst, assums)
blanchet@36402
   753
         | NONE => (ps :: proof, subst, (t, l) :: assums))
blanchet@36402
   754
      | do_step (Have (qs, l, t, by)) (proof, subst, assums) =
blanchet@36402
   755
        (Have (qs, l, t,
blanchet@36402
   756
               case by of
blanchet@36402
   757
                 Facts facts => Facts (relabel_facts subst facts)
blanchet@36402
   758
               | CaseSplit (proofs, facts) =>
blanchet@36402
   759
                 CaseSplit (map do_proof proofs, relabel_facts subst facts)) ::
blanchet@36402
   760
         proof, subst, assums)
blanchet@36486
   761
      | do_step ps (proof, subst, assums) = (ps :: proof, subst, assums)
blanchet@36402
   762
    and do_proof proof = fold do_step proof ([], [], []) |> #1 |> rev
blanchet@36402
   763
  in do_proof end
blanchet@36402
   764
blanchet@36402
   765
val then_chain_proof =
blanchet@36402
   766
  let
blanchet@36402
   767
    fun aux _ [] = []
blanchet@36402
   768
      | aux _ ((ps as Assume (l, _)) :: proof) = ps :: aux l proof
blanchet@36402
   769
      | aux l' (Have (qs, l, t, by) :: proof) =
blanchet@36402
   770
        (case by of
blanchet@36402
   771
           Facts (ls, ss) =>
blanchet@36402
   772
           Have (if member (op =) ls l' then
blanchet@36402
   773
                   (Then :: qs, l, t,
blanchet@36402
   774
                    Facts (filter_out (curry (op =) l') ls, ss))
blanchet@36402
   775
                 else
blanchet@36402
   776
                   (qs, l, t, Facts (ls, ss)))
blanchet@36402
   777
         | CaseSplit (proofs, facts) =>
blanchet@36402
   778
           Have (qs, l, t, CaseSplit (map (aux no_label) proofs, facts))) ::
blanchet@36402
   779
        aux l proof
blanchet@36486
   780
      | aux _ (ps :: proof) = ps :: aux no_label proof
blanchet@36402
   781
  in aux no_label end
blanchet@36402
   782
blanchet@36402
   783
fun kill_useless_labels_in_proof proof =
blanchet@36402
   784
  let
blanchet@36402
   785
    val used_ls = using_of proof
blanchet@36402
   786
    fun do_label l = if member (op =) used_ls l then l else no_label
blanchet@36486
   787
    fun kill (Assume (l, t)) = Assume (do_label l, t)
blanchet@36402
   788
      | kill (Have (qs, l, t, by)) =
blanchet@36402
   789
        Have (qs, do_label l, t,
blanchet@36402
   790
              case by of
blanchet@36402
   791
                CaseSplit (proofs, facts) =>
blanchet@36402
   792
                CaseSplit (map (map kill) proofs, facts)
blanchet@36402
   793
              | _ => by)
blanchet@36486
   794
      | kill ps = ps
blanchet@36402
   795
  in map kill proof end
blanchet@36402
   796
blanchet@36402
   797
fun prefix_for_depth n = replicate_string (n + 1)
blanchet@36402
   798
blanchet@36402
   799
val relabel_proof =
blanchet@36402
   800
  let
blanchet@36402
   801
    fun aux _ _ _ [] = []
blanchet@36402
   802
      | aux subst depth (next_assum, next_fact) (Assume (l, t) :: proof) =
blanchet@36402
   803
        if l = no_label then
blanchet@36402
   804
          Assume (l, t) :: aux subst depth (next_assum, next_fact) proof
blanchet@36402
   805
        else
blanchet@36402
   806
          let val l' = (prefix_for_depth depth assum_prefix, next_assum) in
blanchet@36402
   807
            Assume (l', t) ::
blanchet@36402
   808
            aux ((l, l') :: subst) depth (next_assum + 1, next_fact) proof
blanchet@36402
   809
          end
blanchet@36402
   810
      | aux subst depth (next_assum, next_fact) (Have (qs, l, t, by) :: proof) =
blanchet@36402
   811
        let
blanchet@36402
   812
          val (l', subst, next_fact) =
blanchet@36402
   813
            if l = no_label then
blanchet@36402
   814
              (l, subst, next_fact)
blanchet@36402
   815
            else
blanchet@36402
   816
              let
blanchet@36402
   817
                val l' = (prefix_for_depth depth fact_prefix, next_fact)
blanchet@36402
   818
              in (l', (l, l') :: subst, next_fact + 1) end
blanchet@36486
   819
          val relabel_facts = apfst (map_filter (AList.lookup (op =) subst))
blanchet@36402
   820
          val by =
blanchet@36402
   821
            case by of
blanchet@36402
   822
              Facts facts => Facts (relabel_facts facts)
blanchet@36402
   823
            | CaseSplit (proofs, facts) =>
blanchet@36402
   824
              CaseSplit (map (aux subst (depth + 1) (1, 1)) proofs,
blanchet@36402
   825
                         relabel_facts facts)
blanchet@36402
   826
        in
blanchet@36402
   827
          Have (qs, l', t, by) ::
blanchet@36402
   828
          aux subst depth (next_assum, next_fact) proof
blanchet@36402
   829
        end
blanchet@36486
   830
      | aux subst depth nextp (ps :: proof) = ps :: aux subst depth nextp proof
blanchet@36402
   831
  in aux [] 0 (1, 1) end
blanchet@36402
   832
blanchet@36488
   833
fun string_for_proof ctxt i n =
blanchet@36402
   834
  let
blanchet@36478
   835
    fun fix_print_mode f =
blanchet@36478
   836
      PrintMode.setmp (filter (curry (op =) Symbol.xsymbolsN)
blanchet@36478
   837
                      (print_mode_value ())) f
blanchet@36402
   838
    fun do_indent ind = replicate_string (ind * indent_size) " "
blanchet@36478
   839
    fun do_free (s, T) =
blanchet@36478
   840
      maybe_quote s ^ " :: " ^
blanchet@36478
   841
      maybe_quote (fix_print_mode (Syntax.string_of_typ ctxt) T)
blanchet@36402
   842
    fun do_raw_label (s, j) = s ^ string_of_int j
blanchet@36402
   843
    fun do_label l = if l = no_label then "" else do_raw_label l ^ ": "
blanchet@36402
   844
    fun do_have qs =
blanchet@36402
   845
      (if member (op =) qs Moreover then "moreover " else "") ^
blanchet@36402
   846
      (if member (op =) qs Ultimately then "ultimately " else "") ^
blanchet@36402
   847
      (if member (op =) qs Then then
blanchet@36402
   848
         if member (op =) qs Show then "thus" else "hence"
blanchet@36402
   849
       else
blanchet@36402
   850
         if member (op =) qs Show then "show" else "have")
blanchet@36478
   851
    val do_term = maybe_quote o fix_print_mode (Syntax.string_of_term ctxt)
blanchet@36402
   852
    fun do_using [] = ""
blanchet@36402
   853
      | do_using ls = "using " ^ (space_implode " " (map do_raw_label ls)) ^ " "
blanchet@36486
   854
    fun do_by_facts [] = "by metis"
blanchet@36486
   855
      | do_by_facts ss = "by (metis " ^ space_implode " " ss ^ ")"
blanchet@36486
   856
    fun do_facts (ls, ss) = do_using ls ^ do_by_facts ss
blanchet@36478
   857
    and do_step ind (Fix xs) =
blanchet@36478
   858
        do_indent ind ^ "fix " ^ space_implode " and " (map do_free xs) ^ "\n"
blanchet@36486
   859
      | do_step ind (Let (t1, t2)) =
blanchet@36486
   860
        do_indent ind ^ "let " ^ do_term t1 ^ " = " ^ do_term t2 ^ "\n"
blanchet@36402
   861
      | do_step ind (Assume (l, t)) =
blanchet@36402
   862
        do_indent ind ^ "assume " ^ do_label l ^ do_term t ^ "\n"
blanchet@36402
   863
      | do_step ind (Have (qs, l, t, Facts facts)) =
blanchet@36402
   864
        do_indent ind ^ do_have qs ^ " " ^
blanchet@36479
   865
        do_label l ^ do_term t ^ " " ^ do_facts facts ^ "\n"
blanchet@36402
   866
      | do_step ind (Have (qs, l, t, CaseSplit (proofs, facts))) =
blanchet@36402
   867
        space_implode (do_indent ind ^ "moreover\n")
blanchet@36402
   868
                      (map (do_block ind) proofs) ^
blanchet@36479
   869
        do_indent ind ^ do_have qs ^ " " ^ do_label l ^ do_term t ^ " " ^
blanchet@36478
   870
        do_facts facts ^ "\n"
blanchet@36402
   871
    and do_steps prefix suffix ind steps =
blanchet@36402
   872
      let val s = implode (map (do_step ind) steps) in
blanchet@36402
   873
        replicate_string (ind * indent_size - size prefix) " " ^ prefix ^
blanchet@36402
   874
        String.extract (s, ind * indent_size,
blanchet@36402
   875
                        SOME (size s - ind * indent_size - 1)) ^
blanchet@36402
   876
        suffix ^ "\n"
blanchet@36402
   877
      end
blanchet@36402
   878
    and do_block ind proof = do_steps "{ " " }" (ind + 1) proof
blanchet@36480
   879
    (* One-step proofs are pointless; better use the Metis one-liner. *)
blanchet@36480
   880
    and do_proof [_] = ""
blanchet@36480
   881
      | do_proof proof =
blanchet@36480
   882
        (if i <> 1 then "prefer " ^ string_of_int i ^ "\n" else "") ^
blanchet@36480
   883
        do_indent 0 ^ "proof -\n" ^
blanchet@36480
   884
        do_steps "" "" 1 proof ^
blanchet@36480
   885
        do_indent 0 ^ (if n <> 1 then "next" else "qed") ^ "\n"
blanchet@36488
   886
  in do_proof end
blanchet@36402
   887
blanchet@36488
   888
fun isar_proof_text (pool, debug, shrink_factor, ctxt, conjecture_shape)
blanchet@36402
   889
                    (minimize_command, atp_proof, thm_names, goal, i) =
blanchet@36402
   890
  let
blanchet@36402
   891
    val thy = ProofContext.theory_of ctxt
blanchet@36402
   892
    val (frees, hyp_ts, concl_t) = strip_subgoal goal i
blanchet@36402
   893
    val n = Logic.count_prems (prop_of goal)
blanchet@36223
   894
    val (one_line_proof, lemma_names) =
blanchet@36402
   895
      metis_proof_text (minimize_command, atp_proof, thm_names, goal, i)
blanchet@36283
   896
    fun isar_proof_for () =
blanchet@36474
   897
      case proof_from_atp_proof pool ctxt shrink_factor atp_proof thm_names
blanchet@36474
   898
                                frees
blanchet@36402
   899
           |> direct_proof thy conjecture_shape hyp_ts concl_t
blanchet@36402
   900
           |> kill_duplicate_assumptions_in_proof
blanchet@36402
   901
           |> then_chain_proof
blanchet@36402
   902
           |> kill_useless_labels_in_proof
blanchet@36402
   903
           |> relabel_proof
blanchet@36488
   904
           |> string_for_proof ctxt i n of
blanchet@36283
   905
        "" => ""
blanchet@36402
   906
      | proof => "\nStructured proof:\n" ^ Markup.markup Markup.sendback proof
blanchet@35868
   907
    val isar_proof =
blanchet@36402
   908
      if debug then
blanchet@36283
   909
        isar_proof_for ()
blanchet@36283
   910
      else
blanchet@36283
   911
        try isar_proof_for ()
blanchet@36287
   912
        |> the_default "Warning: The Isar proof construction failed.\n"
blanchet@36283
   913
  in (one_line_proof ^ isar_proof, lemma_names) end
paulson@21978
   914
blanchet@36422
   915
fun proof_text isar_proof isar_params =
blanchet@36422
   916
  if isar_proof then isar_proof_text isar_params else metis_proof_text
blanchet@36223
   917
immler@31038
   918
end;