src/HOL/Tools/Sledgehammer/sledgehammer_reconstruct.ML
author blanchet
Mon Sep 13 21:24:10 2010 +0200 (2010-09-13)
changeset 39353 7f11d833d65b
parent 39288 f1ae2493d93f
parent 39327 61547eda78b4
child 39368 f661064b2b80
permissions -rw-r--r--
merged
blanchet@38988
     1
(*  Title:      HOL/Tools/Sledgehammer/sledgehammer_reconstruct.ML
blanchet@38027
     2
    Author:     Lawrence C. Paulson, Cambridge University Computer Laboratory
blanchet@38027
     3
    Author:     Claire Quigley, Cambridge University Computer Laboratory
blanchet@36392
     4
    Author:     Jasmin Blanchette, TU Muenchen
paulson@21978
     5
wenzelm@33310
     6
Transfer of proofs from external provers.
wenzelm@33310
     7
*)
wenzelm@33310
     8
blanchet@38988
     9
signature SLEDGEHAMMER_RECONSTRUCT =
paulson@24425
    10
sig
blanchet@38988
    11
  type locality = Sledgehammer_Filter.locality
blanchet@36281
    12
  type minimize_command = string list -> string
blanchet@38818
    13
  type metis_params =
blanchet@39327
    14
    string * bool * minimize_command * string * (string * locality) list vector
blanchet@39327
    15
    * thm * int
blanchet@38818
    16
  type isar_params =
blanchet@38040
    17
    string Symtab.table * bool * int * Proof.context * int list list
blanchet@38818
    18
  type text_result = string * (string * locality) list
blanchet@38818
    19
blanchet@38818
    20
  val metis_proof_text : metis_params -> text_result
blanchet@38818
    21
  val isar_proof_text : isar_params -> metis_params -> text_result
blanchet@38818
    22
  val proof_text : bool -> isar_params -> metis_params -> text_result
paulson@24425
    23
end;
paulson@21978
    24
blanchet@38988
    25
structure Sledgehammer_Reconstruct : SLEDGEHAMMER_RECONSTRUCT =
paulson@21978
    26
struct
paulson@21978
    27
blanchet@38028
    28
open ATP_Problem
blanchet@37578
    29
open Metis_Clauses
blanchet@36478
    30
open Sledgehammer_Util
blanchet@38988
    31
open Sledgehammer_Filter
blanchet@38282
    32
open Sledgehammer_Translate
paulson@21978
    33
blanchet@36281
    34
type minimize_command = string list -> string
blanchet@38818
    35
type metis_params =
blanchet@39327
    36
  string * bool * minimize_command * string * (string * locality) list vector
blanchet@39327
    37
  * thm * int
blanchet@38818
    38
type isar_params =
blanchet@38818
    39
  string Symtab.table * bool * int * Proof.context * int list list
blanchet@38818
    40
type text_result = string * (string * locality) list
blanchet@36281
    41
blanchet@38014
    42
(* Simple simplifications to ensure that sort annotations don't leave a trail of
blanchet@38014
    43
   spurious "True"s. *)
blanchet@38014
    44
fun s_not @{const False} = @{const True}
blanchet@38014
    45
  | s_not @{const True} = @{const False}
blanchet@38014
    46
  | s_not (@{const Not} $ t) = t
blanchet@38014
    47
  | s_not t = @{const Not} $ t
blanchet@38014
    48
fun s_conj (@{const True}, t2) = t2
blanchet@38014
    49
  | s_conj (t1, @{const True}) = t1
blanchet@38014
    50
  | s_conj p = HOLogic.mk_conj p
blanchet@38014
    51
fun s_disj (@{const False}, t2) = t2
blanchet@38014
    52
  | s_disj (t1, @{const False}) = t1
blanchet@38014
    53
  | s_disj p = HOLogic.mk_disj p
blanchet@38014
    54
fun s_imp (@{const True}, t2) = t2
blanchet@38014
    55
  | s_imp (t1, @{const False}) = s_not t1
blanchet@38014
    56
  | s_imp p = HOLogic.mk_imp p
blanchet@38014
    57
fun s_iff (@{const True}, t2) = t2
blanchet@38014
    58
  | s_iff (t1, @{const True}) = t1
blanchet@38014
    59
  | s_iff (t1, t2) = HOLogic.eq_const HOLogic.boolT $ t1 $ t2
blanchet@38014
    60
blanchet@38014
    61
fun mk_anot (AConn (ANot, [phi])) = phi
blanchet@38014
    62
  | mk_anot phi = AConn (ANot, [phi])
blanchet@37991
    63
fun mk_aconn c (phi1, phi2) = AConn (c, [phi1, phi2])
blanchet@37991
    64
blanchet@38066
    65
fun index_in_shape x = find_index (exists (curry (op =) x))
blanchet@38282
    66
fun is_axiom_number axiom_names num =
blanchet@38282
    67
  num > 0 andalso num <= Vector.length axiom_names andalso
blanchet@38818
    68
  not (null (Vector.sub (axiom_names, num - 1)))
blanchet@38085
    69
fun is_conjecture_number conjecture_shape num =
blanchet@36570
    70
  index_in_shape num conjecture_shape >= 0
blanchet@36291
    71
blanchet@37991
    72
fun negate_term (Const (@{const_name All}, T) $ Abs (s, T', t')) =
blanchet@37991
    73
    Const (@{const_name Ex}, T) $ Abs (s, T', negate_term t')
blanchet@37991
    74
  | negate_term (Const (@{const_name Ex}, T) $ Abs (s, T', t')) =
blanchet@37991
    75
    Const (@{const_name All}, T) $ Abs (s, T', negate_term t')
haftmann@38786
    76
  | negate_term (@{const HOL.implies} $ t1 $ t2) =
haftmann@38795
    77
    @{const HOL.conj} $ t1 $ negate_term t2
haftmann@38795
    78
  | negate_term (@{const HOL.conj} $ t1 $ t2) =
haftmann@38795
    79
    @{const HOL.disj} $ negate_term t1 $ negate_term t2
haftmann@38795
    80
  | negate_term (@{const HOL.disj} $ t1 $ t2) =
haftmann@38795
    81
    @{const HOL.conj} $ negate_term t1 $ negate_term t2
blanchet@37991
    82
  | negate_term (@{const Not} $ t) = t
blanchet@37991
    83
  | negate_term t = @{const Not} $ t
blanchet@37991
    84
blanchet@36491
    85
datatype ('a, 'b, 'c, 'd, 'e) raw_step =
blanchet@36491
    86
  Definition of 'a * 'b * 'c |
blanchet@36491
    87
  Inference of 'a * 'd * 'e list
blanchet@36491
    88
blanchet@38035
    89
fun raw_step_number (Definition (num, _, _)) = num
blanchet@38035
    90
  | raw_step_number (Inference (num, _, _)) = num
paulson@21978
    91
blanchet@38035
    92
(**** PARSING OF TSTP FORMAT ****)
paulson@21978
    93
paulson@21978
    94
(*Strings enclosed in single quotes, e.g. filenames*)
blanchet@37991
    95
val scan_quoted = $$ "'" |-- Scan.repeat (~$$ "'") --| $$ "'" >> implode;
paulson@21978
    96
blanchet@37991
    97
val scan_dollar_name =
blanchet@36548
    98
  Scan.repeat ($$ "$") -- Symbol.scan_id >> (fn (ss, s) => implode ss ^ s)
blanchet@36548
    99
blanchet@36548
   100
fun repair_name _ "$true" = "c_True"
blanchet@36548
   101
  | repair_name _ "$false" = "c_False"
blanchet@38007
   102
  | repair_name _ "$$e" = "c_equal" (* seen in Vampire proofs *)
blanchet@38035
   103
  | repair_name _ "equal" = "c_equal" (* needed by SPASS? *)
blanchet@38035
   104
  | repair_name pool s =
blanchet@38035
   105
    case Symtab.lookup pool s of
blanchet@38035
   106
      SOME s' => s'
blanchet@38035
   107
    | NONE =>
blanchet@38035
   108
      if String.isPrefix "sQ" s andalso String.isSuffix "_eqProxy" s then
blanchet@38035
   109
        "c_equal" (* seen in Vampire proofs *)
blanchet@38035
   110
      else
blanchet@38035
   111
        s
blanchet@36392
   112
(* Generalized first-order terms, which include file names, numbers, etc. *)
blanchet@38035
   113
val parse_potential_integer =
blanchet@38035
   114
  (scan_dollar_name || scan_quoted) >> K NONE
blanchet@38035
   115
  || scan_integer >> SOME
blanchet@38035
   116
fun parse_annotation x =
blanchet@38035
   117
  ((parse_potential_integer ::: Scan.repeat ($$ " " |-- parse_potential_integer)
blanchet@38036
   118
    >> map_filter I) -- Scan.optional parse_annotation []
blanchet@38035
   119
     >> uncurry (union (op =))
blanchet@38035
   120
   || $$ "(" |-- parse_annotations --| $$ ")"
blanchet@38035
   121
   || $$ "[" |-- parse_annotations --| $$ "]") x
blanchet@38035
   122
and parse_annotations x =
blanchet@38036
   123
  (Scan.optional (parse_annotation
blanchet@38036
   124
                  ::: Scan.repeat ($$ "," |-- parse_annotation)) []
blanchet@38035
   125
   >> (fn numss => fold (union (op =)) numss [])) x
blanchet@38035
   126
blanchet@38035
   127
(* Vampire proof lines sometimes contain needless information such as "(0:3)",
blanchet@38035
   128
   which can be hard to disambiguate from function application in an LL(1)
blanchet@38035
   129
   parser. As a workaround, we extend the TPTP term syntax with such detritus
blanchet@38035
   130
   and ignore it. *)
blanchet@38066
   131
fun parse_vampire_detritus x =
blanchet@38066
   132
  (scan_integer |-- $$ ":" --| scan_integer >> K []) x
blanchet@38035
   133
blanchet@36393
   134
fun parse_term pool x =
blanchet@37991
   135
  ((scan_dollar_name >> repair_name pool)
blanchet@38035
   136
    -- Scan.optional ($$ "(" |-- (parse_vampire_detritus || parse_terms pool)
blanchet@38035
   137
                      --| $$ ")") []
blanchet@38035
   138
    --| Scan.optional ($$ "(" |-- parse_vampire_detritus --| $$ ")") []
blanchet@38035
   139
   >> ATerm) x
blanchet@36393
   140
and parse_terms pool x =
blanchet@36393
   141
  (parse_term pool ::: Scan.repeat ($$ "," |-- parse_term pool)) x
paulson@21978
   142
blanchet@38034
   143
fun parse_atom pool =
blanchet@36393
   144
  parse_term pool -- Scan.option (Scan.option ($$ "!") --| $$ "="
blanchet@36393
   145
                                  -- parse_term pool)
blanchet@38035
   146
  >> (fn (u1, NONE) => AAtom u1
blanchet@38034
   147
       | (u1, SOME (NONE, u2)) => AAtom (ATerm ("c_equal", [u1, u2]))
blanchet@37991
   148
       | (u1, SOME (SOME _, u2)) =>
blanchet@38034
   149
         mk_anot (AAtom (ATerm ("c_equal", [u1, u2]))))
blanchet@37991
   150
blanchet@37991
   151
fun fo_term_head (ATerm (s, _)) = s
blanchet@36291
   152
blanchet@37991
   153
(* TPTP formulas are fully parenthesized, so we don't need to worry about
blanchet@37991
   154
   operator precedence. *)
blanchet@37991
   155
fun parse_formula pool x =
blanchet@37991
   156
  (($$ "(" |-- parse_formula pool --| $$ ")"
blanchet@37991
   157
    || ($$ "!" >> K AForall || $$ "?" >> K AExists)
blanchet@37991
   158
       --| $$ "[" -- parse_terms pool --| $$ "]" --| $$ ":"
blanchet@37991
   159
       -- parse_formula pool
blanchet@37991
   160
       >> (fn ((q, ts), phi) => AQuant (q, map fo_term_head ts, phi))
blanchet@37991
   161
    || $$ "~" |-- parse_formula pool >> mk_anot
blanchet@38034
   162
    || parse_atom pool)
blanchet@37991
   163
   -- Scan.option ((Scan.this_string "=>" >> K AImplies
blanchet@37991
   164
                    || Scan.this_string "<=>" >> K AIff
blanchet@37991
   165
                    || Scan.this_string "<~>" >> K ANotIff
blanchet@37991
   166
                    || Scan.this_string "<=" >> K AIf
blanchet@37991
   167
                    || $$ "|" >> K AOr || $$ "&" >> K AAnd)
blanchet@37991
   168
                   -- parse_formula pool)
blanchet@37991
   169
   >> (fn (phi1, NONE) => phi1
blanchet@37991
   170
        | (phi1, SOME (c, phi2)) => mk_aconn c (phi1, phi2))) x
blanchet@37991
   171
blanchet@38035
   172
val parse_tstp_extra_arguments =
blanchet@38035
   173
  Scan.optional ($$ "," |-- parse_annotation
blanchet@38035
   174
                 --| Scan.option ($$ "," |-- parse_annotations)) []
blanchet@36486
   175
blanchet@38035
   176
(* Syntax: (fof|cnf)\(<num>, <formula_role>, <formula> <extra_arguments>\).
blanchet@36486
   177
   The <num> could be an identifier, but we assume integers. *)
blanchet@37991
   178
 fun parse_tstp_line pool =
blanchet@37991
   179
   ((Scan.this_string "fof" || Scan.this_string "cnf") -- $$ "(")
blanchet@37991
   180
     |-- scan_integer --| $$ "," -- Symbol.scan_id --| $$ ","
blanchet@38035
   181
     -- parse_formula pool -- parse_tstp_extra_arguments --| $$ ")" --| $$ "."
blanchet@37991
   182
    >> (fn (((num, role), phi), deps) =>
blanchet@37991
   183
           case role of
blanchet@37991
   184
             "definition" =>
blanchet@37991
   185
             (case phi of
blanchet@38034
   186
                AConn (AIff, [phi1 as AAtom _, phi2]) =>
blanchet@38007
   187
                Definition (num, phi1, phi2)
blanchet@38036
   188
              | AAtom (ATerm ("c_equal", _)) =>
blanchet@38007
   189
                Inference (num, phi, deps) (* Vampire's equality proxy axiom *)
blanchet@37991
   190
              | _ => raise Fail "malformed definition")
blanchet@37991
   191
           | _ => Inference (num, phi, deps))
blanchet@36291
   192
blanchet@38035
   193
(**** PARSING OF VAMPIRE OUTPUT ****)
blanchet@38035
   194
blanchet@38035
   195
(* Syntax: <num>. <formula> <annotation> *)
blanchet@38035
   196
fun parse_vampire_line pool =
blanchet@38035
   197
  scan_integer --| $$ "." -- parse_formula pool -- parse_annotation
blanchet@38035
   198
  >> (fn ((num, phi), deps) => Inference (num, phi, deps))
blanchet@38035
   199
blanchet@36291
   200
(**** PARSING OF SPASS OUTPUT ****)
blanchet@36291
   201
blanchet@36392
   202
(* SPASS returns clause references of the form "x.y". We ignore "y", whose role
blanchet@36392
   203
   is not clear anyway. *)
blanchet@37962
   204
val parse_dot_name = scan_integer --| $$ "." --| scan_integer
paulson@21978
   205
blanchet@36392
   206
val parse_spass_annotations =
blanchet@36392
   207
  Scan.optional ($$ ":" |-- Scan.repeat (parse_dot_name
blanchet@36392
   208
                                         --| Scan.option ($$ ","))) []
blanchet@36291
   209
blanchet@36574
   210
(* It is not clear why some literals are followed by sequences of stars and/or
blanchet@36574
   211
   pluses. We ignore them. *)
blanchet@38034
   212
fun parse_decorated_atom pool =
blanchet@38034
   213
  parse_atom pool --| Scan.repeat ($$ "*" || $$ "+" || $$ " ")
blanchet@36291
   214
blanchet@38034
   215
fun mk_horn ([], []) = AAtom (ATerm ("c_False", []))
blanchet@37991
   216
  | mk_horn ([], pos_lits) = foldr1 (mk_aconn AOr) pos_lits
blanchet@37991
   217
  | mk_horn (neg_lits, []) = mk_anot (foldr1 (mk_aconn AAnd) neg_lits)
blanchet@37991
   218
  | mk_horn (neg_lits, pos_lits) =
blanchet@37991
   219
    mk_aconn AImplies (foldr1 (mk_aconn AAnd) neg_lits,
blanchet@37991
   220
                       foldr1 (mk_aconn AOr) pos_lits)
blanchet@37991
   221
blanchet@36393
   222
fun parse_horn_clause pool =
blanchet@38034
   223
  Scan.repeat (parse_decorated_atom pool) --| $$ "|" --| $$ "|"
blanchet@38034
   224
    -- Scan.repeat (parse_decorated_atom pool) --| $$ "-" --| $$ ">"
blanchet@38034
   225
    -- Scan.repeat (parse_decorated_atom pool)
blanchet@37991
   226
  >> (mk_horn o apfst (op @))
paulson@21978
   227
blanchet@36558
   228
(* Syntax: <num>[0:<inference><annotations>]
blanchet@38034
   229
   <atoms> || <atoms> -> <atoms>. *)
blanchet@36402
   230
fun parse_spass_line pool =
blanchet@37962
   231
  scan_integer --| $$ "[" --| $$ "0" --| $$ ":" --| Symbol.scan_id
blanchet@38035
   232
    -- parse_spass_annotations --| $$ "]" -- parse_horn_clause pool --| $$ "."
blanchet@37991
   233
  >> (fn ((num, deps), u) => Inference (num, u, deps))
blanchet@36291
   234
blanchet@38035
   235
fun parse_line pool =
blanchet@38035
   236
  parse_tstp_line pool || parse_vampire_line pool || parse_spass_line pool
blanchet@36548
   237
fun parse_lines pool = Scan.repeat1 (parse_line pool)
blanchet@36548
   238
fun parse_proof pool =
blanchet@36548
   239
  fst o Scan.finite Symbol.stopper
blanchet@36548
   240
            (Scan.error (!! (fn _ => raise Fail "unrecognized ATP output")
blanchet@36548
   241
                            (parse_lines pool)))
blanchet@38738
   242
  o explode o strip_spaces_except_between_ident_chars
paulson@21978
   243
paulson@21978
   244
(**** INTERPRETATION OF TSTP SYNTAX TREES ****)
paulson@21978
   245
blanchet@37991
   246
exception FO_TERM of string fo_term list
blanchet@37994
   247
exception FORMULA of (string, string fo_term) formula list
blanchet@37991
   248
exception SAME of unit
paulson@21978
   249
blanchet@36909
   250
(* Type variables are given the basic sort "HOL.type". Some will later be
blanchet@37991
   251
   constrained by information from type literals, or by type inference. *)
blanchet@37991
   252
fun type_from_fo_term tfrees (u as ATerm (a, us)) =
blanchet@37991
   253
  let val Ts = map (type_from_fo_term tfrees) us in
blanchet@38748
   254
    case strip_prefix_and_unascii type_const_prefix a of
blanchet@37991
   255
      SOME b => Type (invert_const b, Ts)
blanchet@37991
   256
    | NONE =>
blanchet@37991
   257
      if not (null us) then
blanchet@37991
   258
        raise FO_TERM [u]  (* only "tconst"s have type arguments *)
blanchet@38748
   259
      else case strip_prefix_and_unascii tfree_prefix a of
blanchet@37991
   260
        SOME b =>
blanchet@37991
   261
        let val s = "'" ^ b in
blanchet@37991
   262
          TFree (s, AList.lookup (op =) tfrees s |> the_default HOLogic.typeS)
blanchet@37991
   263
        end
blanchet@36486
   264
      | NONE =>
blanchet@38748
   265
        case strip_prefix_and_unascii tvar_prefix a of
blanchet@37991
   266
          SOME b => TVar (("'" ^ b, 0), HOLogic.typeS)
blanchet@36486
   267
        | NONE =>
blanchet@37991
   268
          (* Variable from the ATP, say "X1" *)
blanchet@37991
   269
          Type_Infer.param 0 (a, HOLogic.typeS)
blanchet@37991
   270
  end
paulson@21978
   271
blanchet@38014
   272
(* Type class literal applied to a type. Returns triple of polarity, class,
blanchet@38014
   273
   type. *)
blanchet@38014
   274
fun type_constraint_from_term pos tfrees (u as ATerm (a, us)) =
blanchet@38748
   275
  case (strip_prefix_and_unascii class_prefix a,
blanchet@38014
   276
        map (type_from_fo_term tfrees) us) of
blanchet@38014
   277
    (SOME b, [T]) => (pos, b, T)
blanchet@38014
   278
  | _ => raise FO_TERM [u]
blanchet@38014
   279
blanchet@38085
   280
(** Accumulate type constraints in a formula: negative type literals **)
blanchet@38014
   281
fun add_var (key, z)  = Vartab.map_default (key, []) (cons z)
blanchet@38014
   282
fun add_type_constraint (false, cl, TFree (a ,_)) = add_var ((a, ~1), cl)
blanchet@38014
   283
  | add_type_constraint (false, cl, TVar (ix, _)) = add_var (ix, cl)
blanchet@38014
   284
  | add_type_constraint _ = I
blanchet@38014
   285
blanchet@38490
   286
fun repair_atp_variable_name f s =
blanchet@36486
   287
  let
blanchet@36486
   288
    fun subscript_name s n = s ^ nat_subscript n
blanchet@38488
   289
    val s = String.map f s
blanchet@36486
   290
  in
blanchet@36486
   291
    case space_explode "_" s of
blanchet@36486
   292
      [_] => (case take_suffix Char.isDigit (String.explode s) of
blanchet@36486
   293
                (cs1 as _ :: _, cs2 as _ :: _) =>
blanchet@36486
   294
                subscript_name (String.implode cs1)
blanchet@36486
   295
                               (the (Int.fromString (String.implode cs2)))
blanchet@36486
   296
              | (_, _) => s)
blanchet@36486
   297
    | [s1, s2] => (case Int.fromString s2 of
blanchet@36486
   298
                     SOME n => subscript_name s1 n
blanchet@36486
   299
                   | NONE => s)
blanchet@36486
   300
    | _ => s
blanchet@36486
   301
  end
blanchet@36486
   302
blanchet@36909
   303
(* First-order translation. No types are known for variables. "HOLogic.typeT"
blanchet@38014
   304
   should allow them to be inferred. *)
blanchet@38014
   305
fun raw_term_from_pred thy full_types tfrees =
blanchet@36909
   306
  let
blanchet@37643
   307
    fun aux opt_T extra_us u =
blanchet@36909
   308
      case u of
blanchet@37991
   309
        ATerm ("hBOOL", [u1]) => aux (SOME @{typ bool}) [] u1
blanchet@37991
   310
      | ATerm ("hAPP", [u1, u2]) => aux opt_T (u2 :: extra_us) u1
blanchet@37991
   311
      | ATerm (a, us) =>
blanchet@36909
   312
        if a = type_wrapper_name then
blanchet@36909
   313
          case us of
blanchet@37643
   314
            [typ_u, term_u] =>
blanchet@37991
   315
            aux (SOME (type_from_fo_term tfrees typ_u)) extra_us term_u
blanchet@37991
   316
          | _ => raise FO_TERM us
blanchet@38748
   317
        else case strip_prefix_and_unascii const_prefix a of
blanchet@36909
   318
          SOME "equal" =>
blanchet@39106
   319
          let val ts = map (aux NONE []) us in
blanchet@39106
   320
            if length ts = 2 andalso hd ts aconv List.last ts then
blanchet@39106
   321
              (* Vampire is keen on producing these. *)
blanchet@39106
   322
              @{const True}
blanchet@39106
   323
            else
blanchet@39106
   324
              list_comb (Const (@{const_name HOL.eq}, HOLogic.typeT), ts)
blanchet@39106
   325
          end
blanchet@36909
   326
        | SOME b =>
blanchet@36909
   327
          let
blanchet@36909
   328
            val c = invert_const b
blanchet@36909
   329
            val num_type_args = num_type_args thy c
blanchet@37643
   330
            val (type_us, term_us) =
blanchet@37643
   331
              chop (if full_types then 0 else num_type_args) us
blanchet@37643
   332
            (* Extra args from "hAPP" come after any arguments given directly to
blanchet@37643
   333
               the constant. *)
blanchet@37643
   334
            val term_ts = map (aux NONE []) term_us
blanchet@37643
   335
            val extra_ts = map (aux NONE []) extra_us
blanchet@36909
   336
            val t =
blanchet@36909
   337
              Const (c, if full_types then
blanchet@36909
   338
                          case opt_T of
blanchet@37643
   339
                            SOME T => map fastype_of term_ts ---> T
blanchet@36909
   340
                          | NONE =>
blanchet@36909
   341
                            if num_type_args = 0 then
blanchet@36909
   342
                              Sign.const_instance thy (c, [])
blanchet@36909
   343
                            else
blanchet@36909
   344
                              raise Fail ("no type information for " ^ quote c)
blanchet@36909
   345
                        else
blanchet@37998
   346
                          Sign.const_instance thy (c,
blanchet@37998
   347
                              map (type_from_fo_term tfrees) type_us))
blanchet@37643
   348
          in list_comb (t, term_ts @ extra_ts) end
blanchet@36909
   349
        | NONE => (* a free or schematic variable *)
blanchet@36909
   350
          let
blanchet@37643
   351
            val ts = map (aux NONE []) (us @ extra_us)
blanchet@36909
   352
            val T = map fastype_of ts ---> HOLogic.typeT
blanchet@36909
   353
            val t =
blanchet@38748
   354
              case strip_prefix_and_unascii fixed_var_prefix a of
blanchet@36909
   355
                SOME b => Free (b, T)
blanchet@36909
   356
              | NONE =>
blanchet@38748
   357
                case strip_prefix_and_unascii schematic_var_prefix a of
blanchet@36967
   358
                  SOME b => Var ((b, 0), T)
blanchet@36909
   359
                | NONE =>
blanchet@38017
   360
                  if is_tptp_variable a then
blanchet@38490
   361
                    Var ((repair_atp_variable_name Char.toLower a, 0), T)
blanchet@38017
   362
                  else
blanchet@38488
   363
                    (* Skolem constants? *)
blanchet@38490
   364
                    Var ((repair_atp_variable_name Char.toUpper a, 0), T)
blanchet@36909
   365
          in list_comb (t, ts) end
blanchet@38014
   366
  in aux (SOME HOLogic.boolT) [] end
paulson@21978
   367
blanchet@38014
   368
fun term_from_pred thy full_types tfrees pos (u as ATerm (s, _)) =
blanchet@38014
   369
  if String.isPrefix class_prefix s then
blanchet@38014
   370
    add_type_constraint (type_constraint_from_term pos tfrees u)
blanchet@38014
   371
    #> pair @{const True}
blanchet@38014
   372
  else
blanchet@38014
   373
    pair (raw_term_from_pred thy full_types tfrees u)
blanchet@36402
   374
blanchet@36555
   375
val combinator_table =
blanchet@36555
   376
  [(@{const_name COMBI}, @{thm COMBI_def_raw}),
blanchet@36555
   377
   (@{const_name COMBK}, @{thm COMBK_def_raw}),
blanchet@36555
   378
   (@{const_name COMBB}, @{thm COMBB_def_raw}),
blanchet@36555
   379
   (@{const_name COMBC}, @{thm COMBC_def_raw}),
blanchet@36555
   380
   (@{const_name COMBS}, @{thm COMBS_def_raw})]
blanchet@36555
   381
blanchet@36555
   382
fun uncombine_term (t1 $ t2) = betapply (pairself uncombine_term (t1, t2))
blanchet@36555
   383
  | uncombine_term (Abs (s, T, t')) = Abs (s, T, uncombine_term t')
blanchet@36555
   384
  | uncombine_term (t as Const (x as (s, _))) =
blanchet@36555
   385
    (case AList.lookup (op =) combinator_table s of
blanchet@36555
   386
       SOME thm => thm |> prop_of |> specialize_type @{theory} x |> Logic.dest_equals |> snd
blanchet@36555
   387
     | NONE => t)
blanchet@36555
   388
  | uncombine_term t = t
blanchet@36555
   389
blanchet@37991
   390
(* Update schematic type variables with detected sort constraints. It's not
blanchet@37991
   391
   totally clear when this code is necessary. *)
blanchet@38014
   392
fun repair_tvar_sorts (t, tvar_tab) =
blanchet@36909
   393
  let
blanchet@37991
   394
    fun do_type (Type (a, Ts)) = Type (a, map do_type Ts)
blanchet@37991
   395
      | do_type (TVar (xi, s)) =
blanchet@37991
   396
        TVar (xi, the_default s (Vartab.lookup tvar_tab xi))
blanchet@37991
   397
      | do_type (TFree z) = TFree z
blanchet@37991
   398
    fun do_term (Const (a, T)) = Const (a, do_type T)
blanchet@37991
   399
      | do_term (Free (a, T)) = Free (a, do_type T)
blanchet@37991
   400
      | do_term (Var (xi, T)) = Var (xi, do_type T)
blanchet@37991
   401
      | do_term (t as Bound _) = t
blanchet@37991
   402
      | do_term (Abs (a, T, t)) = Abs (a, do_type T, do_term t)
blanchet@37991
   403
      | do_term (t1 $ t2) = do_term t1 $ do_term t2
blanchet@37991
   404
  in t |> not (Vartab.is_empty tvar_tab) ? do_term end
blanchet@37991
   405
blanchet@37991
   406
fun quantify_over_free quant_s free_s body_t =
blanchet@37991
   407
  case Term.add_frees body_t [] |> filter (curry (op =) free_s o fst) of
blanchet@37991
   408
    [] => body_t
blanchet@37991
   409
  | frees as (_, free_T) :: _ =>
blanchet@37991
   410
    Abs (free_s, free_T, fold (curry abstract_over) (map Free frees) body_t)
blanchet@37991
   411
blanchet@38085
   412
(* Interpret an ATP formula as a HOL term, extracting sort constraints as they
blanchet@38085
   413
   appear in the formula. *)
blanchet@38014
   414
fun prop_from_formula thy full_types tfrees phi =
blanchet@38014
   415
  let
blanchet@38014
   416
    fun do_formula pos phi =
blanchet@37991
   417
      case phi of
blanchet@38014
   418
        AQuant (_, [], phi) => do_formula pos phi
blanchet@37991
   419
      | AQuant (q, x :: xs, phi') =>
blanchet@38014
   420
        do_formula pos (AQuant (q, xs, phi'))
blanchet@38014
   421
        #>> quantify_over_free (case q of
blanchet@38014
   422
                                  AForall => @{const_name All}
blanchet@38490
   423
                                | AExists => @{const_name Ex})
blanchet@38490
   424
                               (repair_atp_variable_name Char.toLower x)
blanchet@38014
   425
      | AConn (ANot, [phi']) => do_formula (not pos) phi' #>> s_not
blanchet@37991
   426
      | AConn (c, [phi1, phi2]) =>
blanchet@38014
   427
        do_formula (pos |> c = AImplies ? not) phi1
blanchet@38014
   428
        ##>> do_formula pos phi2
blanchet@38014
   429
        #>> (case c of
blanchet@38014
   430
               AAnd => s_conj
blanchet@38014
   431
             | AOr => s_disj
blanchet@38014
   432
             | AImplies => s_imp
blanchet@38038
   433
             | AIf => s_imp o swap
blanchet@38038
   434
             | AIff => s_iff
blanchet@38038
   435
             | ANotIff => s_not o s_iff)
blanchet@38034
   436
      | AAtom tm => term_from_pred thy full_types tfrees pos tm
blanchet@37991
   437
      | _ => raise FORMULA [phi]
blanchet@38014
   438
  in repair_tvar_sorts (do_formula true phi Vartab.empty) end
blanchet@37991
   439
blanchet@36556
   440
fun check_formula ctxt =
wenzelm@39288
   441
  Type.constraint HOLogic.boolT
blanchet@36486
   442
  #> Syntax.check_term (ProofContext.set_mode ProofContext.mode_schematic ctxt)
paulson@21978
   443
paulson@21978
   444
paulson@21978
   445
(**** Translation of TSTP files to Isar Proofs ****)
paulson@21978
   446
blanchet@36486
   447
fun unvarify_term (Var ((s, 0), T)) = Free (s, T)
blanchet@36486
   448
  | unvarify_term t = raise TERM ("unvarify_term: non-Var", [t])
paulson@21978
   449
blanchet@37991
   450
fun decode_line full_types tfrees (Definition (num, phi1, phi2)) ctxt =
blanchet@36486
   451
    let
blanchet@37991
   452
      val thy = ProofContext.theory_of ctxt
blanchet@37991
   453
      val t1 = prop_from_formula thy full_types tfrees phi1
blanchet@36551
   454
      val vars = snd (strip_comb t1)
blanchet@36486
   455
      val frees = map unvarify_term vars
blanchet@36486
   456
      val unvarify_args = subst_atomic (vars ~~ frees)
blanchet@37991
   457
      val t2 = prop_from_formula thy full_types tfrees phi2
blanchet@36551
   458
      val (t1, t2) =
blanchet@36551
   459
        HOLogic.eq_const HOLogic.typeT $ t1 $ t2
blanchet@36556
   460
        |> unvarify_args |> uncombine_term |> check_formula ctxt
blanchet@36555
   461
        |> HOLogic.dest_eq
blanchet@36486
   462
    in
blanchet@36551
   463
      (Definition (num, t1, t2),
blanchet@36551
   464
       fold Variable.declare_term (maps OldTerm.term_frees [t1, t2]) ctxt)
blanchet@36486
   465
    end
blanchet@37991
   466
  | decode_line full_types tfrees (Inference (num, u, deps)) ctxt =
blanchet@36551
   467
    let
blanchet@37991
   468
      val thy = ProofContext.theory_of ctxt
blanchet@37991
   469
      val t = u |> prop_from_formula thy full_types tfrees
blanchet@37998
   470
                |> uncombine_term |> check_formula ctxt
blanchet@36551
   471
    in
blanchet@36551
   472
      (Inference (num, t, deps),
blanchet@36551
   473
       fold Variable.declare_term (OldTerm.term_frees t) ctxt)
blanchet@36486
   474
    end
blanchet@36967
   475
fun decode_lines ctxt full_types tfrees lines =
blanchet@36967
   476
  fst (fold_map (decode_line full_types tfrees) lines ctxt)
paulson@21978
   477
blanchet@38035
   478
fun is_same_inference _ (Definition _) = false
blanchet@38035
   479
  | is_same_inference t (Inference (_, t', _)) = t aconv t'
blanchet@36486
   480
blanchet@36486
   481
(* No "real" literals means only type information (tfree_tcs, clsrel, or
blanchet@36486
   482
   clsarity). *)
blanchet@36486
   483
val is_only_type_information = curry (op aconv) HOLogic.true_const
blanchet@36486
   484
blanchet@36486
   485
fun replace_one_dep (old, new) dep = if dep = old then new else [dep]
blanchet@36486
   486
fun replace_deps_in_line _ (line as Definition _) = line
blanchet@36486
   487
  | replace_deps_in_line p (Inference (num, t, deps)) =
blanchet@36486
   488
    Inference (num, t, fold (union (op =) o replace_one_dep p) deps [])
paulson@21978
   489
blanchet@38085
   490
(* Discard axioms; consolidate adjacent lines that prove the same formula, since
blanchet@38085
   491
   they differ only in type information.*)
blanchet@36551
   492
fun add_line _ _ (line as Definition _) lines = line :: lines
blanchet@38282
   493
  | add_line conjecture_shape axiom_names (Inference (num, t, [])) lines =
blanchet@38085
   494
    (* No dependencies: axiom, conjecture, or (for Vampire) internal axioms or
blanchet@38085
   495
       definitions. *)
blanchet@38282
   496
    if is_axiom_number axiom_names num then
blanchet@36486
   497
      (* Axioms are not proof lines. *)
blanchet@36486
   498
      if is_only_type_information t then
blanchet@36486
   499
        map (replace_deps_in_line (num, [])) lines
blanchet@36486
   500
      (* Is there a repetition? If so, replace later line by earlier one. *)
blanchet@38035
   501
      else case take_prefix (not o is_same_inference t) lines of
blanchet@36486
   502
        (_, []) => lines (*no repetition of proof line*)
blanchet@36486
   503
      | (pre, Inference (num', _, _) :: post) =>
blanchet@36486
   504
        pre @ map (replace_deps_in_line (num', [num])) post
blanchet@38085
   505
    else if is_conjecture_number conjecture_shape num then
blanchet@38105
   506
      Inference (num, negate_term t, []) :: lines
blanchet@36551
   507
    else
blanchet@36570
   508
      map (replace_deps_in_line (num, [])) lines
blanchet@36551
   509
  | add_line _ _ (Inference (num, t, deps)) lines =
blanchet@36486
   510
    (* Type information will be deleted later; skip repetition test. *)
blanchet@36486
   511
    if is_only_type_information t then
blanchet@36486
   512
      Inference (num, t, deps) :: lines
blanchet@36486
   513
    (* Is there a repetition? If so, replace later line by earlier one. *)
blanchet@38035
   514
    else case take_prefix (not o is_same_inference t) lines of
blanchet@36486
   515
      (* FIXME: Doesn't this code risk conflating proofs involving different
blanchet@38035
   516
         types? *)
blanchet@36486
   517
       (_, []) => Inference (num, t, deps) :: lines
blanchet@36486
   518
     | (pre, Inference (num', t', _) :: post) =>
blanchet@36486
   519
       Inference (num, t', deps) ::
blanchet@36486
   520
       pre @ map (replace_deps_in_line (num', [num])) post
paulson@22044
   521
blanchet@36486
   522
(* Recursively delete empty lines (type information) from the proof. *)
blanchet@36486
   523
fun add_nontrivial_line (Inference (num, t, [])) lines =
blanchet@36486
   524
    if is_only_type_information t then delete_dep num lines
blanchet@36486
   525
    else Inference (num, t, []) :: lines
blanchet@36486
   526
  | add_nontrivial_line line lines = line :: lines
blanchet@36395
   527
and delete_dep num lines =
blanchet@36486
   528
  fold_rev add_nontrivial_line (map (replace_deps_in_line (num, [])) lines) []
blanchet@36486
   529
blanchet@37323
   530
(* ATPs sometimes reuse free variable names in the strangest ways. Removing
blanchet@37323
   531
   offending lines often does the trick. *)
blanchet@36560
   532
fun is_bad_free frees (Free x) = not (member (op =) frees x)
blanchet@36560
   533
  | is_bad_free _ _ = false
paulson@22470
   534
blanchet@37498
   535
fun add_desired_line _ _ _ _ (line as Definition (num, _, _)) (j, lines) =
blanchet@37323
   536
    (j, line :: map (replace_deps_in_line (num, [])) lines)
blanchet@38282
   537
  | add_desired_line isar_shrink_factor conjecture_shape axiom_names frees
blanchet@36570
   538
                     (Inference (num, t, deps)) (j, lines) =
blanchet@36402
   539
    (j + 1,
blanchet@38282
   540
     if is_axiom_number axiom_names num orelse
blanchet@38085
   541
        is_conjecture_number conjecture_shape num orelse
blanchet@36570
   542
        (not (is_only_type_information t) andalso
blanchet@36570
   543
         null (Term.add_tvars t []) andalso
blanchet@36570
   544
         not (exists_subterm (is_bad_free frees) t) andalso
blanchet@36570
   545
         (null lines orelse (* last line must be kept *)
blanchet@36924
   546
          (length deps >= 2 andalso j mod isar_shrink_factor = 0))) then
blanchet@36570
   547
       Inference (num, t, deps) :: lines  (* keep line *)
blanchet@36402
   548
     else
blanchet@36570
   549
       map (replace_deps_in_line (num, deps)) lines)  (* drop line *)
paulson@21978
   550
blanchet@36402
   551
(** EXTRACTING LEMMAS **)
paulson@21979
   552
blanchet@38599
   553
(* Like "split_line", but ignores "\n" that follow a comma (as in SNARK's
blanchet@38599
   554
   output). *)
blanchet@38599
   555
val split_proof_lines =
blanchet@38599
   556
  let
blanchet@38599
   557
    fun aux [] [] = []
blanchet@38599
   558
      | aux line [] = [implode (rev line)]
blanchet@38599
   559
      | aux line ("," :: "\n" :: rest) = aux ("," :: line) rest
blanchet@38599
   560
      | aux line ("\n" :: rest) = aux line [] @ aux [] rest
blanchet@38599
   561
      | aux line (s :: rest) = aux (s :: line) rest
blanchet@38599
   562
  in aux [] o explode end
blanchet@38599
   563
blanchet@37991
   564
(* A list consisting of the first number in each line is returned. For TSTP,
blanchet@37991
   565
   interesting lines have the form "fof(108, axiom, ...)", where the number
blanchet@37991
   566
   (108) is extracted. For SPASS, lines have the form "108[0:Inp] ...", where
blanchet@38033
   567
   the first number (108) is extracted. For Vampire, we look for
blanchet@38033
   568
   "108. ... [input]". *)
blanchet@38282
   569
fun used_facts_in_atp_proof axiom_names atp_proof =
blanchet@35865
   570
  let
blanchet@38818
   571
    fun axiom_names_at_index num =
blanchet@38039
   572
      let val j = Int.fromString num |> the_default ~1 in
blanchet@38818
   573
        if is_axiom_number axiom_names j then Vector.sub (axiom_names, j - 1)
blanchet@38818
   574
        else []
blanchet@38039
   575
      end
blanchet@38039
   576
    val tokens_of =
blanchet@38039
   577
      String.tokens (fn c => not (Char.isAlphaNum c) andalso c <> #"_")
blanchet@38599
   578
    fun do_line (tag :: num :: "axiom" :: (rest as _ :: _)) =
blanchet@38599
   579
        if tag = "cnf" orelse tag = "fof" then
blanchet@38748
   580
          (case strip_prefix_and_unascii axiom_prefix (List.last rest) of
blanchet@38599
   581
             SOME name =>
blanchet@38698
   582
             if member (op =) rest "file" then
blanchet@38818
   583
               ([(name, name |> find_first_in_list_vector axiom_names |> the)]
blanchet@38818
   584
                handle Option.Option =>
blanchet@38818
   585
                       error ("No such fact: " ^ quote name ^ "."))
blanchet@38698
   586
             else
blanchet@38818
   587
               axiom_names_at_index num
blanchet@38818
   588
           | NONE => axiom_names_at_index num)
blanchet@38599
   589
        else
blanchet@38818
   590
          []
blanchet@38818
   591
      | do_line (num :: "0" :: "Inp" :: _) = axiom_names_at_index num
blanchet@38039
   592
      | do_line (num :: rest) =
blanchet@38818
   593
        (case List.last rest of "input" => axiom_names_at_index num | _ => [])
blanchet@38818
   594
      | do_line _ = []
blanchet@38818
   595
  in atp_proof |> split_proof_lines |> maps (do_line o tokens_of) end
blanchet@37399
   596
blanchet@37399
   597
val indent_size = 2
blanchet@37399
   598
val no_label = ("", ~1)
blanchet@37399
   599
blanchet@37399
   600
val raw_prefix = "X"
blanchet@37399
   601
val assum_prefix = "A"
blanchet@37399
   602
val fact_prefix = "F"
blanchet@37399
   603
blanchet@37399
   604
fun string_for_label (s, num) = s ^ string_of_int num
blanchet@37399
   605
blanchet@37399
   606
fun metis_using [] = ""
blanchet@37399
   607
  | metis_using ls =
blanchet@37399
   608
    "using " ^ space_implode " " (map string_for_label ls) ^ " "
blanchet@37399
   609
fun metis_apply _ 1 = "by "
blanchet@37399
   610
  | metis_apply 1 _ = "apply "
blanchet@37399
   611
  | metis_apply i _ = "prefer " ^ string_of_int i ^ " apply "
blanchet@37479
   612
fun metis_name full_types = if full_types then "metisFT" else "metis"
blanchet@37479
   613
fun metis_call full_types [] = metis_name full_types
blanchet@37479
   614
  | metis_call full_types ss =
blanchet@37479
   615
    "(" ^ metis_name full_types ^ " " ^ space_implode " " ss ^ ")"
blanchet@37479
   616
fun metis_command full_types i n (ls, ss) =
blanchet@37479
   617
  metis_using ls ^ metis_apply i n ^ metis_call full_types ss
blanchet@39327
   618
fun metis_line banner full_types i n ss =
blanchet@39327
   619
  banner ^ ": " ^
blanchet@38597
   620
  Markup.markup Markup.sendback (metis_command full_types i n ([], ss)) ^ "."
blanchet@36281
   621
fun minimize_line _ [] = ""
blanchet@38696
   622
  | minimize_line minimize_command ss =
blanchet@38696
   623
    case minimize_command ss of
blanchet@36281
   624
      "" => ""
blanchet@36281
   625
    | command =>
blanchet@38597
   626
      "\nTo minimize the number of lemmas, try this: " ^
blanchet@38597
   627
      Markup.markup Markup.sendback command ^ "."
immler@31840
   628
blanchet@38282
   629
fun used_facts axiom_names =
blanchet@38282
   630
  used_facts_in_atp_proof axiom_names
blanchet@38752
   631
  #> List.partition (curry (op =) Chained o snd)
blanchet@38752
   632
  #> pairself (sort_distinct (string_ord o pairself fst))
blanchet@38015
   633
blanchet@39327
   634
fun metis_proof_text (banner, full_types, minimize_command, atp_proof,
blanchet@39327
   635
                      axiom_names, goal, i) =
blanchet@36063
   636
  let
blanchet@38282
   637
    val (chained_lemmas, other_lemmas) = used_facts axiom_names atp_proof
blanchet@36063
   638
    val n = Logic.count_prems (prop_of goal)
blanchet@37171
   639
  in
blanchet@39327
   640
    (metis_line banner full_types i n (map fst other_lemmas) ^
blanchet@38752
   641
     minimize_line minimize_command (map fst (other_lemmas @ chained_lemmas)),
blanchet@38752
   642
     other_lemmas @ chained_lemmas)
blanchet@37171
   643
  end
immler@31037
   644
blanchet@36486
   645
(** Isar proof construction and manipulation **)
blanchet@36486
   646
blanchet@36486
   647
fun merge_fact_sets (ls1, ss1) (ls2, ss2) =
blanchet@36486
   648
  (union (op =) ls1 ls2, union (op =) ss1 ss2)
blanchet@36402
   649
blanchet@36402
   650
type label = string * int
blanchet@36402
   651
type facts = label list * string list
blanchet@36402
   652
blanchet@36402
   653
datatype qualifier = Show | Then | Moreover | Ultimately
blanchet@36291
   654
blanchet@36402
   655
datatype step =
blanchet@36478
   656
  Fix of (string * typ) list |
blanchet@36486
   657
  Let of term * term |
blanchet@36402
   658
  Assume of label * term |
blanchet@36402
   659
  Have of qualifier list * label * term * byline
blanchet@36402
   660
and byline =
blanchet@36564
   661
  ByMetis of facts |
blanchet@36402
   662
  CaseSplit of step list list * facts
blanchet@36402
   663
blanchet@36574
   664
fun smart_case_split [] facts = ByMetis facts
blanchet@36574
   665
  | smart_case_split proofs facts = CaseSplit (proofs, facts)
blanchet@36574
   666
blanchet@38282
   667
fun add_fact_from_dep axiom_names num =
blanchet@38282
   668
  if is_axiom_number axiom_names num then
blanchet@38818
   669
    apsnd (union (op =) (map fst (Vector.sub (axiom_names, num - 1))))
blanchet@36475
   670
  else
blanchet@36480
   671
    apfst (insert (op =) (raw_prefix, num))
blanchet@36402
   672
blanchet@37998
   673
fun forall_of v t = HOLogic.all_const (fastype_of v) $ lambda v t
blanchet@36491
   674
fun forall_vars t = fold_rev forall_of (map Var (Term.add_vars t [])) t
blanchet@36491
   675
blanchet@37498
   676
fun step_for_line _ _ (Definition (_, t1, t2)) = Let (t1, t2)
blanchet@36486
   677
  | step_for_line _ _ (Inference (num, t, [])) = Assume ((raw_prefix, num), t)
blanchet@38282
   678
  | step_for_line axiom_names j (Inference (num, t, deps)) =
blanchet@36486
   679
    Have (if j = 1 then [Show] else [], (raw_prefix, num),
blanchet@36491
   680
          forall_vars t,
blanchet@38282
   681
          ByMetis (fold (add_fact_from_dep axiom_names) deps ([], [])))
blanchet@36291
   682
blanchet@36967
   683
fun proof_from_atp_proof pool ctxt full_types tfrees isar_shrink_factor
blanchet@38282
   684
                         atp_proof conjecture_shape axiom_names params frees =
blanchet@36402
   685
  let
blanchet@36486
   686
    val lines =
blanchet@38035
   687
      atp_proof ^ "$" (* the $ sign acts as a sentinel (FIXME: needed?) *)
blanchet@36548
   688
      |> parse_proof pool
blanchet@38035
   689
      |> sort (int_ord o pairself raw_step_number)
blanchet@36967
   690
      |> decode_lines ctxt full_types tfrees
blanchet@38282
   691
      |> rpair [] |-> fold_rev (add_line conjecture_shape axiom_names)
blanchet@36486
   692
      |> rpair [] |-> fold_rev add_nontrivial_line
blanchet@37498
   693
      |> rpair (0, []) |-> fold_rev (add_desired_line isar_shrink_factor
blanchet@38282
   694
                                             conjecture_shape axiom_names frees)
blanchet@36486
   695
      |> snd
blanchet@36402
   696
  in
blanchet@36909
   697
    (if null params then [] else [Fix params]) @
blanchet@38282
   698
    map2 (step_for_line axiom_names) (length lines downto 1) lines
blanchet@36402
   699
  end
blanchet@36402
   700
blanchet@36402
   701
(* When redirecting proofs, we keep information about the labels seen so far in
blanchet@36402
   702
   the "backpatches" data structure. The first component indicates which facts
blanchet@36402
   703
   should be associated with forthcoming proof steps. The second component is a
blanchet@37322
   704
   pair ("assum_ls", "drop_ls"), where "assum_ls" are the labels that should
blanchet@37322
   705
   become assumptions and "drop_ls" are the labels that should be dropped in a
blanchet@37322
   706
   case split. *)
blanchet@36402
   707
type backpatches = (label * facts) list * (label list * label list)
blanchet@36402
   708
blanchet@36556
   709
fun used_labels_of_step (Have (_, _, _, by)) =
blanchet@36402
   710
    (case by of
blanchet@36564
   711
       ByMetis (ls, _) => ls
blanchet@36556
   712
     | CaseSplit (proofs, (ls, _)) =>
blanchet@36556
   713
       fold (union (op =) o used_labels_of) proofs ls)
blanchet@36556
   714
  | used_labels_of_step _ = []
blanchet@36556
   715
and used_labels_of proof = fold (union (op =) o used_labels_of_step) proof []
blanchet@36402
   716
blanchet@36402
   717
fun new_labels_of_step (Fix _) = []
blanchet@36486
   718
  | new_labels_of_step (Let _) = []
blanchet@36402
   719
  | new_labels_of_step (Assume (l, _)) = [l]
blanchet@36402
   720
  | new_labels_of_step (Have (_, l, _, _)) = [l]
blanchet@36402
   721
val new_labels_of = maps new_labels_of_step
blanchet@36402
   722
blanchet@36402
   723
val join_proofs =
blanchet@36402
   724
  let
blanchet@36402
   725
    fun aux _ [] = NONE
blanchet@36402
   726
      | aux proof_tail (proofs as (proof1 :: _)) =
blanchet@36402
   727
        if exists null proofs then
blanchet@36402
   728
          NONE
blanchet@36402
   729
        else if forall (curry (op =) (hd proof1) o hd) (tl proofs) then
blanchet@36402
   730
          aux (hd proof1 :: proof_tail) (map tl proofs)
blanchet@36402
   731
        else case hd proof1 of
blanchet@37498
   732
          Have ([], l, t, _) => (* FIXME: should we really ignore the "by"? *)
blanchet@36402
   733
          if forall (fn Have ([], l', t', _) :: _ => (l, t) = (l', t')
blanchet@36402
   734
                      | _ => false) (tl proofs) andalso
blanchet@36402
   735
             not (exists (member (op =) (maps new_labels_of proofs))
blanchet@36556
   736
                         (used_labels_of proof_tail)) then
blanchet@36402
   737
            SOME (l, t, map rev proofs, proof_tail)
blanchet@36402
   738
          else
blanchet@36402
   739
            NONE
blanchet@36402
   740
        | _ => NONE
blanchet@36402
   741
  in aux [] o map rev end
blanchet@36402
   742
blanchet@36402
   743
fun case_split_qualifiers proofs =
blanchet@36402
   744
  case length proofs of
blanchet@36402
   745
    0 => []
blanchet@36402
   746
  | 1 => [Then]
blanchet@36402
   747
  | _ => [Ultimately]
blanchet@36402
   748
blanchet@37991
   749
fun redirect_proof conjecture_shape hyp_ts concl_t proof =
wenzelm@33310
   750
  let
blanchet@37324
   751
    (* The first pass outputs those steps that are independent of the negated
blanchet@37324
   752
       conjecture. The second pass flips the proof by contradiction to obtain a
blanchet@37324
   753
       direct proof, introducing case splits when an inference depends on
blanchet@37324
   754
       several facts that depend on the negated conjecture. *)
blanchet@38038
   755
    fun find_hyp num =
blanchet@38038
   756
      nth hyp_ts (index_in_shape num conjecture_shape)
blanchet@38038
   757
      handle Subscript =>
blanchet@38038
   758
             raise Fail ("Cannot find hypothesis " ^ Int.toString num)
blanchet@38040
   759
     val concl_ls = map (pair raw_prefix) (List.last conjecture_shape)
blanchet@38040
   760
     val canonicalize_labels =
blanchet@38040
   761
       map (fn l => if member (op =) concl_ls l then hd concl_ls else l)
blanchet@38040
   762
       #> distinct (op =)
blanchet@38040
   763
     fun first_pass ([], contra) = ([], contra)
blanchet@38040
   764
       | first_pass ((step as Fix _) :: proof, contra) =
blanchet@38040
   765
         first_pass (proof, contra) |>> cons step
blanchet@38040
   766
       | first_pass ((step as Let _) :: proof, contra) =
blanchet@38040
   767
         first_pass (proof, contra) |>> cons step
blanchet@38040
   768
       | first_pass ((step as Assume (l as (_, num), _)) :: proof, contra) =
blanchet@38040
   769
         if member (op =) concl_ls l then
blanchet@38040
   770
           first_pass (proof, contra ||> l = hd concl_ls ? cons step)
blanchet@38040
   771
         else
blanchet@38040
   772
           first_pass (proof, contra) |>> cons (Assume (l, find_hyp num))
blanchet@38040
   773
       | first_pass (Have (qs, l, t, ByMetis (ls, ss)) :: proof, contra) =
blanchet@38040
   774
         let
blanchet@38040
   775
           val ls = canonicalize_labels ls
blanchet@38040
   776
           val step = Have (qs, l, t, ByMetis (ls, ss))
blanchet@38040
   777
         in
blanchet@38040
   778
           if exists (member (op =) (fst contra)) ls then
blanchet@38040
   779
             first_pass (proof, contra |>> cons l ||> cons step)
blanchet@38040
   780
           else
blanchet@38040
   781
             first_pass (proof, contra) |>> cons step
blanchet@38040
   782
         end
blanchet@38040
   783
       | first_pass _ = raise Fail "malformed proof"
blanchet@36402
   784
    val (proof_top, (contra_ls, contra_proof)) =
blanchet@38040
   785
      first_pass (proof, (concl_ls, []))
blanchet@36402
   786
    val backpatch_label = the_default ([], []) oo AList.lookup (op =) o fst
blanchet@36402
   787
    fun backpatch_labels patches ls =
blanchet@36402
   788
      fold merge_fact_sets (map (backpatch_label patches) ls) ([], [])
blanchet@36402
   789
    fun second_pass end_qs ([], assums, patches) =
blanchet@37324
   790
        ([Have (end_qs, no_label, concl_t,
blanchet@36564
   791
                ByMetis (backpatch_labels patches (map snd assums)))], patches)
blanchet@36402
   792
      | second_pass end_qs (Assume (l, t) :: proof, assums, patches) =
blanchet@36402
   793
        second_pass end_qs (proof, (t, l) :: assums, patches)
blanchet@36564
   794
      | second_pass end_qs (Have (qs, l, t, ByMetis (ls, ss)) :: proof, assums,
blanchet@36402
   795
                            patches) =
blanchet@36402
   796
        if member (op =) (snd (snd patches)) l andalso
blanchet@37322
   797
           not (member (op =) (fst (snd patches)) l) andalso
blanchet@36402
   798
           not (AList.defined (op =) (fst patches) l) then
blanchet@36402
   799
          second_pass end_qs (proof, assums, patches ||> apsnd (append ls))
blanchet@36402
   800
        else
blanchet@36402
   801
          (case List.partition (member (op =) contra_ls) ls of
blanchet@36402
   802
             ([contra_l], co_ls) =>
blanchet@37322
   803
             if member (op =) qs Show then
blanchet@37322
   804
               second_pass end_qs (proof, assums,
blanchet@37322
   805
                                   patches |>> cons (contra_l, (co_ls, ss)))
blanchet@37322
   806
             else
blanchet@36402
   807
               second_pass end_qs
blanchet@36402
   808
                           (proof, assums,
blanchet@36402
   809
                            patches |>> cons (contra_l, (l :: co_ls, ss)))
blanchet@36402
   810
               |>> cons (if member (op =) (fst (snd patches)) l then
blanchet@37991
   811
                           Assume (l, negate_term t)
blanchet@36402
   812
                         else
blanchet@37991
   813
                           Have (qs, l, negate_term t,
blanchet@36564
   814
                                 ByMetis (backpatch_label patches l)))
blanchet@36402
   815
           | (contra_ls as _ :: _, co_ls) =>
blanchet@36402
   816
             let
blanchet@36402
   817
               val proofs =
blanchet@36402
   818
                 map_filter
blanchet@36402
   819
                     (fn l =>
blanchet@38040
   820
                         if member (op =) concl_ls l then
blanchet@36402
   821
                           NONE
blanchet@36402
   822
                         else
blanchet@36402
   823
                           let
blanchet@36402
   824
                             val drop_ls = filter (curry (op <>) l) contra_ls
blanchet@36402
   825
                           in
blanchet@36402
   826
                             second_pass []
blanchet@36402
   827
                                 (proof, assums,
blanchet@36402
   828
                                  patches ||> apfst (insert (op =) l)
blanchet@36402
   829
                                          ||> apsnd (union (op =) drop_ls))
blanchet@36402
   830
                             |> fst |> SOME
blanchet@36402
   831
                           end) contra_ls
blanchet@37324
   832
               val (assumes, facts) =
blanchet@37324
   833
                 if member (op =) (fst (snd patches)) l then
blanchet@37991
   834
                   ([Assume (l, negate_term t)], (l :: co_ls, ss))
blanchet@37324
   835
                 else
blanchet@37324
   836
                   ([], (co_ls, ss))
blanchet@36402
   837
             in
blanchet@36402
   838
               (case join_proofs proofs of
blanchet@36402
   839
                  SOME (l, t, proofs, proof_tail) =>
blanchet@36402
   840
                  Have (case_split_qualifiers proofs @
blanchet@36402
   841
                        (if null proof_tail then end_qs else []), l, t,
blanchet@36574
   842
                        smart_case_split proofs facts) :: proof_tail
blanchet@36402
   843
                | NONE =>
blanchet@36402
   844
                  [Have (case_split_qualifiers proofs @ end_qs, no_label,
blanchet@36574
   845
                         concl_t, smart_case_split proofs facts)],
blanchet@36402
   846
                patches)
blanchet@37324
   847
               |>> append assumes
blanchet@36402
   848
             end
blanchet@36402
   849
           | _ => raise Fail "malformed proof")
blanchet@36402
   850
       | second_pass _ _ = raise Fail "malformed proof"
blanchet@36486
   851
    val proof_bottom =
blanchet@36486
   852
      second_pass [Show] (contra_proof, [], ([], ([], []))) |> fst
blanchet@36402
   853
  in proof_top @ proof_bottom end
blanchet@36402
   854
blanchet@38490
   855
(* FIXME: Still needed? Probably not. *)
blanchet@36402
   856
val kill_duplicate_assumptions_in_proof =
blanchet@36402
   857
  let
blanchet@36402
   858
    fun relabel_facts subst =
blanchet@36402
   859
      apfst (map (fn l => AList.lookup (op =) subst l |> the_default l))
blanchet@36491
   860
    fun do_step (step as Assume (l, t)) (proof, subst, assums) =
blanchet@36402
   861
        (case AList.lookup (op aconv) assums t of
blanchet@36967
   862
           SOME l' => (proof, (l, l') :: subst, assums)
blanchet@36491
   863
         | NONE => (step :: proof, subst, (t, l) :: assums))
blanchet@36402
   864
      | do_step (Have (qs, l, t, by)) (proof, subst, assums) =
blanchet@36402
   865
        (Have (qs, l, t,
blanchet@36402
   866
               case by of
blanchet@36564
   867
                 ByMetis facts => ByMetis (relabel_facts subst facts)
blanchet@36402
   868
               | CaseSplit (proofs, facts) =>
blanchet@36402
   869
                 CaseSplit (map do_proof proofs, relabel_facts subst facts)) ::
blanchet@36402
   870
         proof, subst, assums)
blanchet@36491
   871
      | do_step step (proof, subst, assums) = (step :: proof, subst, assums)
blanchet@36402
   872
    and do_proof proof = fold do_step proof ([], [], []) |> #1 |> rev
blanchet@36402
   873
  in do_proof end
blanchet@36402
   874
blanchet@36402
   875
val then_chain_proof =
blanchet@36402
   876
  let
blanchet@36402
   877
    fun aux _ [] = []
blanchet@36491
   878
      | aux _ ((step as Assume (l, _)) :: proof) = step :: aux l proof
blanchet@36402
   879
      | aux l' (Have (qs, l, t, by) :: proof) =
blanchet@36402
   880
        (case by of
blanchet@36564
   881
           ByMetis (ls, ss) =>
blanchet@36402
   882
           Have (if member (op =) ls l' then
blanchet@36402
   883
                   (Then :: qs, l, t,
blanchet@36564
   884
                    ByMetis (filter_out (curry (op =) l') ls, ss))
blanchet@36402
   885
                 else
blanchet@36564
   886
                   (qs, l, t, ByMetis (ls, ss)))
blanchet@36402
   887
         | CaseSplit (proofs, facts) =>
blanchet@36402
   888
           Have (qs, l, t, CaseSplit (map (aux no_label) proofs, facts))) ::
blanchet@36402
   889
        aux l proof
blanchet@36491
   890
      | aux _ (step :: proof) = step :: aux no_label proof
blanchet@36402
   891
  in aux no_label end
blanchet@36402
   892
blanchet@36402
   893
fun kill_useless_labels_in_proof proof =
blanchet@36402
   894
  let
blanchet@36556
   895
    val used_ls = used_labels_of proof
blanchet@36402
   896
    fun do_label l = if member (op =) used_ls l then l else no_label
blanchet@36556
   897
    fun do_step (Assume (l, t)) = Assume (do_label l, t)
blanchet@36556
   898
      | do_step (Have (qs, l, t, by)) =
blanchet@36402
   899
        Have (qs, do_label l, t,
blanchet@36402
   900
              case by of
blanchet@36402
   901
                CaseSplit (proofs, facts) =>
blanchet@36556
   902
                CaseSplit (map (map do_step) proofs, facts)
blanchet@36402
   903
              | _ => by)
blanchet@36556
   904
      | do_step step = step
blanchet@36556
   905
  in map do_step proof end
blanchet@36402
   906
blanchet@36402
   907
fun prefix_for_depth n = replicate_string (n + 1)
blanchet@36402
   908
blanchet@36402
   909
val relabel_proof =
blanchet@36402
   910
  let
blanchet@36402
   911
    fun aux _ _ _ [] = []
blanchet@36402
   912
      | aux subst depth (next_assum, next_fact) (Assume (l, t) :: proof) =
blanchet@36402
   913
        if l = no_label then
blanchet@36402
   914
          Assume (l, t) :: aux subst depth (next_assum, next_fact) proof
blanchet@36402
   915
        else
blanchet@36402
   916
          let val l' = (prefix_for_depth depth assum_prefix, next_assum) in
blanchet@36402
   917
            Assume (l', t) ::
blanchet@36402
   918
            aux ((l, l') :: subst) depth (next_assum + 1, next_fact) proof
blanchet@36402
   919
          end
blanchet@36402
   920
      | aux subst depth (next_assum, next_fact) (Have (qs, l, t, by) :: proof) =
blanchet@36402
   921
        let
blanchet@36402
   922
          val (l', subst, next_fact) =
blanchet@36402
   923
            if l = no_label then
blanchet@36402
   924
              (l, subst, next_fact)
blanchet@36402
   925
            else
blanchet@36402
   926
              let
blanchet@36402
   927
                val l' = (prefix_for_depth depth fact_prefix, next_fact)
blanchet@36402
   928
              in (l', (l, l') :: subst, next_fact + 1) end
blanchet@36570
   929
          val relabel_facts =
blanchet@36570
   930
            apfst (map (fn l =>
blanchet@36570
   931
                           case AList.lookup (op =) subst l of
blanchet@36570
   932
                             SOME l' => l'
blanchet@36570
   933
                           | NONE => raise Fail ("unknown label " ^
blanchet@36570
   934
                                                 quote (string_for_label l))))
blanchet@36402
   935
          val by =
blanchet@36402
   936
            case by of
blanchet@36564
   937
              ByMetis facts => ByMetis (relabel_facts facts)
blanchet@36402
   938
            | CaseSplit (proofs, facts) =>
blanchet@36402
   939
              CaseSplit (map (aux subst (depth + 1) (1, 1)) proofs,
blanchet@36402
   940
                         relabel_facts facts)
blanchet@36402
   941
        in
blanchet@36402
   942
          Have (qs, l', t, by) ::
blanchet@36402
   943
          aux subst depth (next_assum, next_fact) proof
blanchet@36402
   944
        end
blanchet@36491
   945
      | aux subst depth nextp (step :: proof) =
blanchet@36491
   946
        step :: aux subst depth nextp proof
blanchet@36402
   947
  in aux [] 0 (1, 1) end
blanchet@36402
   948
wenzelm@39115
   949
fun string_for_proof ctxt0 full_types i n =
blanchet@36402
   950
  let
wenzelm@39134
   951
    val ctxt = ctxt0
wenzelm@39134
   952
      |> Config.put show_free_types false
wenzelm@39134
   953
      |> Config.put show_types true
blanchet@37319
   954
    fun fix_print_mode f x =
wenzelm@39134
   955
      Print_Mode.setmp (filter (curry (op =) Symbol.xsymbolsN)
wenzelm@39134
   956
                               (print_mode_value ())) f x
blanchet@36402
   957
    fun do_indent ind = replicate_string (ind * indent_size) " "
blanchet@36478
   958
    fun do_free (s, T) =
blanchet@36478
   959
      maybe_quote s ^ " :: " ^
blanchet@36478
   960
      maybe_quote (fix_print_mode (Syntax.string_of_typ ctxt) T)
blanchet@36570
   961
    fun do_label l = if l = no_label then "" else string_for_label l ^ ": "
blanchet@36402
   962
    fun do_have qs =
blanchet@36402
   963
      (if member (op =) qs Moreover then "moreover " else "") ^
blanchet@36402
   964
      (if member (op =) qs Ultimately then "ultimately " else "") ^
blanchet@36402
   965
      (if member (op =) qs Then then
blanchet@36402
   966
         if member (op =) qs Show then "thus" else "hence"
blanchet@36402
   967
       else
blanchet@36402
   968
         if member (op =) qs Show then "show" else "have")
blanchet@36478
   969
    val do_term = maybe_quote o fix_print_mode (Syntax.string_of_term ctxt)
blanchet@36570
   970
    fun do_facts (ls, ss) =
blanchet@38698
   971
      metis_command full_types 1 1
blanchet@38698
   972
                    (ls |> sort_distinct (prod_ord string_ord int_ord),
blanchet@38698
   973
                     ss |> sort_distinct string_ord)
blanchet@36478
   974
    and do_step ind (Fix xs) =
blanchet@36478
   975
        do_indent ind ^ "fix " ^ space_implode " and " (map do_free xs) ^ "\n"
blanchet@36486
   976
      | do_step ind (Let (t1, t2)) =
blanchet@36486
   977
        do_indent ind ^ "let " ^ do_term t1 ^ " = " ^ do_term t2 ^ "\n"
blanchet@36402
   978
      | do_step ind (Assume (l, t)) =
blanchet@36402
   979
        do_indent ind ^ "assume " ^ do_label l ^ do_term t ^ "\n"
blanchet@36564
   980
      | do_step ind (Have (qs, l, t, ByMetis facts)) =
blanchet@36402
   981
        do_indent ind ^ do_have qs ^ " " ^
blanchet@36479
   982
        do_label l ^ do_term t ^ " " ^ do_facts facts ^ "\n"
blanchet@36402
   983
      | do_step ind (Have (qs, l, t, CaseSplit (proofs, facts))) =
blanchet@36402
   984
        space_implode (do_indent ind ^ "moreover\n")
blanchet@36402
   985
                      (map (do_block ind) proofs) ^
blanchet@36479
   986
        do_indent ind ^ do_have qs ^ " " ^ do_label l ^ do_term t ^ " " ^
blanchet@36478
   987
        do_facts facts ^ "\n"
blanchet@36402
   988
    and do_steps prefix suffix ind steps =
blanchet@36402
   989
      let val s = implode (map (do_step ind) steps) in
blanchet@36402
   990
        replicate_string (ind * indent_size - size prefix) " " ^ prefix ^
blanchet@36402
   991
        String.extract (s, ind * indent_size,
blanchet@36402
   992
                        SOME (size s - ind * indent_size - 1)) ^
blanchet@36402
   993
        suffix ^ "\n"
blanchet@36402
   994
      end
blanchet@36402
   995
    and do_block ind proof = do_steps "{ " " }" (ind + 1) proof
blanchet@36564
   996
    (* One-step proofs are pointless; better use the Metis one-liner
blanchet@36564
   997
       directly. *)
blanchet@36564
   998
    and do_proof [Have (_, _, _, ByMetis _)] = ""
blanchet@36564
   999
      | do_proof proof =
blanchet@36480
  1000
        (if i <> 1 then "prefer " ^ string_of_int i ^ "\n" else "") ^
blanchet@36480
  1001
        do_indent 0 ^ "proof -\n" ^
blanchet@36480
  1002
        do_steps "" "" 1 proof ^
blanchet@38599
  1003
        do_indent 0 ^ (if n <> 1 then "next" else "qed")
blanchet@36488
  1004
  in do_proof end
blanchet@36402
  1005
blanchet@37479
  1006
fun isar_proof_text (pool, debug, isar_shrink_factor, ctxt, conjecture_shape)
blanchet@39327
  1007
                    (other_params as (_, full_types, _, atp_proof, axiom_names,
blanchet@38282
  1008
                                      goal, i)) =
blanchet@36402
  1009
  let
blanchet@36909
  1010
    val (params, hyp_ts, concl_t) = strip_subgoal goal i
blanchet@36909
  1011
    val frees = fold Term.add_frees (concl_t :: hyp_ts) []
blanchet@36967
  1012
    val tfrees = fold Term.add_tfrees (concl_t :: hyp_ts) []
blanchet@36402
  1013
    val n = Logic.count_prems (prop_of goal)
blanchet@37479
  1014
    val (one_line_proof, lemma_names) = metis_proof_text other_params
blanchet@36283
  1015
    fun isar_proof_for () =
blanchet@36967
  1016
      case proof_from_atp_proof pool ctxt full_types tfrees isar_shrink_factor
blanchet@38282
  1017
                                atp_proof conjecture_shape axiom_names params
blanchet@36924
  1018
                                frees
blanchet@37991
  1019
           |> redirect_proof conjecture_shape hyp_ts concl_t
blanchet@36402
  1020
           |> kill_duplicate_assumptions_in_proof
blanchet@36402
  1021
           |> then_chain_proof
blanchet@36402
  1022
           |> kill_useless_labels_in_proof
blanchet@36402
  1023
           |> relabel_proof
blanchet@37479
  1024
           |> string_for_proof ctxt full_types i n of
blanchet@38599
  1025
        "" => "\nNo structured proof available."
blanchet@38599
  1026
      | proof => "\n\nStructured proof:\n" ^ Markup.markup Markup.sendback proof
blanchet@35868
  1027
    val isar_proof =
blanchet@36402
  1028
      if debug then
blanchet@36283
  1029
        isar_proof_for ()
blanchet@36283
  1030
      else
blanchet@36283
  1031
        try isar_proof_for ()
blanchet@38599
  1032
        |> the_default "\nWarning: The Isar proof construction failed."
blanchet@36283
  1033
  in (one_line_proof ^ isar_proof, lemma_names) end
paulson@21978
  1034
blanchet@36557
  1035
fun proof_text isar_proof isar_params other_params =
blanchet@36557
  1036
  (if isar_proof then isar_proof_text isar_params else metis_proof_text)
blanchet@36557
  1037
      other_params
blanchet@36223
  1038
immler@31038
  1039
end;