src/HOL/Tools/Sledgehammer/sledgehammer_proof_reconstruct.ML
author blanchet
Mon Jun 28 17:31:38 2010 +0200 (2010-06-28)
changeset 37616 c8d2d84d6011
parent 37578 9367cb36b1c4
child 37624 3ee568334813
permissions -rw-r--r--
always perform relevance filtering on original formulas
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@37578
    11
  type name_pool = Metis_Clauses.name_pool
blanchet@36281
    12
blanchet@37479
    13
  val metis_line: bool -> int -> int -> string list -> string
blanchet@36223
    14
  val metis_proof_text:
blanchet@37479
    15
    bool * minimize_command * string * string vector * thm * int
blanchet@36281
    16
    -> string * string list
blanchet@36223
    17
  val isar_proof_text:
blanchet@37479
    18
    name_pool option * bool * int * Proof.context * int list list
blanchet@37479
    19
    -> bool * minimize_command * string * string vector * thm * int
blanchet@36287
    20
    -> string * string list
blanchet@36223
    21
  val proof_text:
blanchet@37479
    22
    bool -> name_pool option * bool * int * Proof.context * int list list
blanchet@37479
    23
    -> bool * minimize_command * string * string vector * thm * int
blanchet@36287
    24
    -> string * string list
paulson@24425
    25
end;
paulson@21978
    26
blanchet@35826
    27
structure Sledgehammer_Proof_Reconstruct : SLEDGEHAMMER_PROOF_RECONSTRUCT =
paulson@21978
    28
struct
paulson@21978
    29
blanchet@37574
    30
open Clausifier
blanchet@37578
    31
open Metis_Clauses
blanchet@36478
    32
open Sledgehammer_Util
blanchet@37616
    33
open Sledgehammer_Fact_Filter
paulson@21978
    34
blanchet@36281
    35
type minimize_command = string list -> string
blanchet@36281
    36
blanchet@36291
    37
fun is_ident_char c = Char.isAlphaNum c orelse c = #"_"
blanchet@36392
    38
fun is_head_digit s = Char.isDigit (String.sub (s, 0))
blanchet@36291
    39
krauss@36607
    40
val index_in_shape : int -> int list list -> int =
krauss@36607
    41
  find_index o exists o curry (op =)
blanchet@36402
    42
fun is_axiom_clause_number thm_names num = num <= Vector.length thm_names
blanchet@36570
    43
fun is_conjecture_clause_number conjecture_shape num =
blanchet@36570
    44
  index_in_shape num conjecture_shape >= 0
blanchet@36291
    45
blanchet@36393
    46
fun ugly_name NONE s = s
blanchet@36393
    47
  | ugly_name (SOME the_pool) s =
blanchet@36393
    48
    case Symtab.lookup (snd the_pool) s of
blanchet@36393
    49
      SOME s' => s'
blanchet@36393
    50
    | NONE => s
blanchet@36393
    51
blanchet@36491
    52
fun smart_lambda v t =
blanchet@36551
    53
  Abs (case v of
blanchet@36551
    54
         Const (s, _) =>
blanchet@36551
    55
         List.last (space_explode skolem_infix (Long_Name.base_name s))
blanchet@36551
    56
       | Var ((s, _), _) => s
blanchet@36551
    57
       | Free (s, _) => s
blanchet@36551
    58
       | _ => "", fastype_of v, abstract_over (v, t))
blanchet@36491
    59
fun forall_of v t = HOLogic.all_const (fastype_of v) $ smart_lambda v t
blanchet@36491
    60
blanchet@36491
    61
datatype ('a, 'b, 'c, 'd, 'e) raw_step =
blanchet@36491
    62
  Definition of 'a * 'b * 'c |
blanchet@36491
    63
  Inference of 'a * 'd * 'e list
blanchet@36491
    64
paulson@21978
    65
(**** PARSING OF TSTP FORMAT ****)
paulson@21978
    66
blanchet@36548
    67
fun strip_spaces_in_list [] = ""
blanchet@36548
    68
  | strip_spaces_in_list [c1] = if Char.isSpace c1 then "" else str c1
blanchet@36548
    69
  | strip_spaces_in_list [c1, c2] =
blanchet@36548
    70
    strip_spaces_in_list [c1] ^ strip_spaces_in_list [c2]
blanchet@36548
    71
  | strip_spaces_in_list (c1 :: c2 :: c3 :: cs) =
blanchet@36548
    72
    if Char.isSpace c1 then
blanchet@36548
    73
      strip_spaces_in_list (c2 :: c3 :: cs)
blanchet@36548
    74
    else if Char.isSpace c2 then
blanchet@36548
    75
      if Char.isSpace c3 then
blanchet@36548
    76
        strip_spaces_in_list (c1 :: c3 :: cs)
blanchet@36548
    77
      else
blanchet@36548
    78
        str c1 ^ (if forall is_ident_char [c1, c3] then " " else "") ^
blanchet@36548
    79
        strip_spaces_in_list (c3 :: cs)
blanchet@36548
    80
    else
blanchet@36548
    81
      str c1 ^ strip_spaces_in_list (c2 :: c3 :: cs)
blanchet@36548
    82
val strip_spaces = strip_spaces_in_list o String.explode
blanchet@36548
    83
blanchet@36291
    84
(* Syntax trees, either term list or formulae *)
blanchet@36486
    85
datatype node = IntLeaf of int | StrNode of string * node list
paulson@21978
    86
blanchet@36548
    87
fun str_leaf s = StrNode (s, [])
paulson@21978
    88
blanchet@36486
    89
fun scons (x, y) = StrNode ("cons", [x, y])
blanchet@36548
    90
val slist_of = List.foldl scons (str_leaf "nil")
paulson@21978
    91
paulson@21978
    92
(*Strings enclosed in single quotes, e.g. filenames*)
blanchet@36392
    93
val parse_quoted = $$ "'" |-- Scan.repeat (~$$ "'") --| $$ "'" >> implode;
paulson@21978
    94
paulson@21978
    95
(*Integer constants, typically proof line numbers*)
blanchet@36392
    96
val parse_integer = Scan.many1 is_head_digit >> (the o Int.fromString o implode)
paulson@21978
    97
blanchet@36548
    98
val parse_dollar_name =
blanchet@36548
    99
  Scan.repeat ($$ "$") -- Symbol.scan_id >> (fn (ss, s) => implode ss ^ s)
blanchet@36548
   100
blanchet@36369
   101
(* needed for SPASS's output format *)
blanchet@36548
   102
fun repair_name _ "$true" = "c_True"
blanchet@36548
   103
  | repair_name _ "$false" = "c_False"
blanchet@36559
   104
  | repair_name _ "$$e" = "c_equal" (* seen in Vampire 11 proofs *)
blanchet@36548
   105
  | repair_name _ "equal" = "c_equal" (* probably not needed *)
blanchet@36393
   106
  | repair_name pool s = ugly_name pool s
blanchet@36392
   107
(* Generalized first-order terms, which include file names, numbers, etc. *)
blanchet@36393
   108
(* The "x" argument is not strictly necessary, but without it Poly/ML loops
blanchet@36393
   109
   forever at compile time. *)
blanchet@36393
   110
fun parse_term pool x =
blanchet@36548
   111
     (parse_quoted >> str_leaf
blanchet@36486
   112
   || parse_integer >> IntLeaf
blanchet@36548
   113
   || (parse_dollar_name >> repair_name pool)
blanchet@36486
   114
      -- Scan.optional ($$ "(" |-- parse_terms pool --| $$ ")") [] >> StrNode
blanchet@36393
   115
   || $$ "(" |-- parse_term pool --| $$ ")"
blanchet@36393
   116
   || $$ "[" |-- Scan.optional (parse_terms pool) [] --| $$ "]" >> slist_of) x
blanchet@36393
   117
and parse_terms pool x =
blanchet@36393
   118
  (parse_term pool ::: Scan.repeat ($$ "," |-- parse_term pool)) x
paulson@21978
   119
blanchet@36486
   120
fun negate_node u = StrNode ("c_Not", [u])
blanchet@36486
   121
fun equate_nodes u1 u2 = StrNode ("c_equal", [u1, u2])
paulson@21978
   122
blanchet@36392
   123
(* Apply equal or not-equal to a term. *)
blanchet@36486
   124
fun repair_predicate_term (u, NONE) = u
blanchet@36486
   125
  | repair_predicate_term (u1, SOME (NONE, u2)) = equate_nodes u1 u2
blanchet@36486
   126
  | repair_predicate_term (u1, SOME (SOME _, u2)) =
blanchet@36486
   127
    negate_node (equate_nodes u1 u2)
blanchet@36393
   128
fun parse_predicate_term pool =
blanchet@36393
   129
  parse_term pool -- Scan.option (Scan.option ($$ "!") --| $$ "="
blanchet@36393
   130
                                  -- parse_term pool)
blanchet@36393
   131
  >> repair_predicate_term
blanchet@36393
   132
fun parse_literal pool x =
blanchet@36486
   133
  ($$ "~" |-- parse_literal pool >> negate_node || parse_predicate_term pool) x
blanchet@36393
   134
fun parse_literals pool =
blanchet@36393
   135
  parse_literal pool ::: Scan.repeat ($$ "|" |-- parse_literal pool)
blanchet@36548
   136
fun parse_parenthesized_literals pool =
blanchet@36548
   137
  $$ "(" |-- parse_literals pool --| $$ ")" || parse_literals pool
blanchet@36393
   138
fun parse_clause pool =
blanchet@36548
   139
  parse_parenthesized_literals pool
blanchet@36548
   140
    ::: Scan.repeat ($$ "|" |-- parse_parenthesized_literals pool)
blanchet@36548
   141
  >> List.concat
blanchet@36291
   142
blanchet@36486
   143
fun ints_of_node (IntLeaf n) = cons n
blanchet@36486
   144
  | ints_of_node (StrNode (_, us)) = fold ints_of_node us
blanchet@36392
   145
val parse_tstp_annotations =
blanchet@36393
   146
  Scan.optional ($$ "," |-- parse_term NONE
blanchet@36393
   147
                   --| Scan.option ($$ "," |-- parse_terms NONE)
blanchet@36486
   148
                 >> (fn source => ints_of_node source [])) []
blanchet@36486
   149
blanchet@36486
   150
fun parse_definition pool =
blanchet@36486
   151
  $$ "(" |-- parse_literal NONE --| Scan.this_string "<=>"
blanchet@36486
   152
  -- parse_clause pool --| $$ ")"
blanchet@36291
   153
blanchet@36486
   154
(* Syntax: cnf(<num>, <formula_role>, <cnf_formula> <annotations>).
blanchet@36486
   155
   The <num> could be an identifier, but we assume integers. *)
blanchet@36486
   156
fun finish_tstp_definition_line (num, (u, us)) = Definition (num, u, us)
blanchet@36486
   157
fun finish_tstp_inference_line ((num, us), deps) = Inference (num, us, deps)
blanchet@36393
   158
fun parse_tstp_line pool =
blanchet@36486
   159
     ((Scan.this_string "fof" -- $$ "(") |-- parse_integer --| $$ ","
blanchet@36486
   160
       --| Scan.this_string "definition" --| $$ "," -- parse_definition pool
blanchet@36486
   161
       --| parse_tstp_annotations --| $$ ")" --| $$ "."
blanchet@36486
   162
      >> finish_tstp_definition_line)
blanchet@36486
   163
  || ((Scan.this_string "cnf" -- $$ "(") |-- parse_integer --| $$ ","
blanchet@36486
   164
       --| Symbol.scan_id --| $$ "," -- parse_clause pool
blanchet@36486
   165
       -- parse_tstp_annotations --| $$ ")" --| $$ "."
blanchet@36486
   166
      >> finish_tstp_inference_line)
blanchet@36291
   167
blanchet@36291
   168
(**** PARSING OF SPASS OUTPUT ****)
blanchet@36291
   169
blanchet@36392
   170
(* SPASS returns clause references of the form "x.y". We ignore "y", whose role
blanchet@36392
   171
   is not clear anyway. *)
blanchet@36392
   172
val parse_dot_name = parse_integer --| $$ "." --| parse_integer
paulson@21978
   173
blanchet@36392
   174
val parse_spass_annotations =
blanchet@36392
   175
  Scan.optional ($$ ":" |-- Scan.repeat (parse_dot_name
blanchet@36392
   176
                                         --| Scan.option ($$ ","))) []
blanchet@36291
   177
blanchet@36574
   178
(* It is not clear why some literals are followed by sequences of stars and/or
blanchet@36574
   179
   pluses. We ignore them. *)
blanchet@36574
   180
fun parse_decorated_predicate_term pool =
blanchet@36562
   181
  parse_predicate_term pool --| Scan.repeat ($$ "*" || $$ "+" || $$ " ")
blanchet@36291
   182
blanchet@36393
   183
fun parse_horn_clause pool =
blanchet@36574
   184
  Scan.repeat (parse_decorated_predicate_term pool) --| $$ "|" --| $$ "|"
blanchet@36574
   185
    -- Scan.repeat (parse_decorated_predicate_term pool) --| $$ "-" --| $$ ">"
blanchet@36574
   186
    -- Scan.repeat (parse_decorated_predicate_term pool)
blanchet@36558
   187
  >> (fn (([], []), []) => [str_leaf "c_False"]
blanchet@36558
   188
       | ((clauses1, clauses2), clauses3) =>
blanchet@36558
   189
         map negate_node (clauses1 @ clauses2) @ clauses3)
paulson@21978
   190
blanchet@36558
   191
(* Syntax: <num>[0:<inference><annotations>]
blanchet@36558
   192
   <cnf_formulas> || <cnf_formulas> -> <cnf_formulas>. *)
blanchet@36486
   193
fun finish_spass_line ((num, deps), us) = Inference (num, us, deps)
blanchet@36402
   194
fun parse_spass_line pool =
blanchet@36392
   195
  parse_integer --| $$ "[" --| $$ "0" --| $$ ":" --| Symbol.scan_id
blanchet@36558
   196
  -- parse_spass_annotations --| $$ "]" -- parse_horn_clause pool --| $$ "."
blanchet@36486
   197
  >> finish_spass_line
blanchet@36291
   198
blanchet@36548
   199
fun parse_line pool = parse_tstp_line pool || parse_spass_line pool
blanchet@36548
   200
fun parse_lines pool = Scan.repeat1 (parse_line pool)
blanchet@36548
   201
fun parse_proof pool =
blanchet@36548
   202
  fst o Scan.finite Symbol.stopper
blanchet@36548
   203
            (Scan.error (!! (fn _ => raise Fail "unrecognized ATP output")
blanchet@36548
   204
                            (parse_lines pool)))
blanchet@36548
   205
  o explode o strip_spaces
paulson@21978
   206
paulson@21978
   207
(**** INTERPRETATION OF TSTP SYNTAX TREES ****)
paulson@21978
   208
blanchet@36909
   209
exception NODE of node list
paulson@21978
   210
blanchet@36909
   211
(* Type variables are given the basic sort "HOL.type". Some will later be
blanchet@36909
   212
  constrained by information from type literals, or by type inference. *)
blanchet@36967
   213
fun type_from_node _ (u as IntLeaf _) = raise NODE [u]
blanchet@36967
   214
  | type_from_node tfrees (u as StrNode (a, us)) =
blanchet@36967
   215
    let val Ts = map (type_from_node tfrees) us in
blanchet@36486
   216
      case strip_prefix tconst_prefix a of
blanchet@37572
   217
        SOME b => Type (invert_const b, Ts)
blanchet@36486
   218
      | NONE =>
blanchet@36486
   219
        if not (null us) then
blanchet@36909
   220
          raise NODE [u]  (* only "tconst"s have type arguments *)
blanchet@36486
   221
        else case strip_prefix tfree_prefix a of
blanchet@36967
   222
          SOME b =>
blanchet@36967
   223
          let val s = "'" ^ b in
blanchet@36967
   224
            TFree (s, AList.lookup (op =) tfrees s |> the_default HOLogic.typeS)
blanchet@36967
   225
          end
blanchet@36486
   226
        | NONE =>
blanchet@36486
   227
          case strip_prefix tvar_prefix a of
blanchet@36967
   228
            SOME b => TVar (("'" ^ b, 0), HOLogic.typeS)
blanchet@36967
   229
          | NONE =>
blanchet@36967
   230
            (* Variable from the ATP, say "X1" *)
wenzelm@37145
   231
            Type_Infer.param 0 (a, HOLogic.typeS)
blanchet@36486
   232
    end
paulson@21978
   233
blanchet@36486
   234
fun fix_atp_variable_name s =
blanchet@36486
   235
  let
blanchet@36486
   236
    fun subscript_name s n = s ^ nat_subscript n
blanchet@36486
   237
    val s = String.map Char.toLower s
blanchet@36486
   238
  in
blanchet@36486
   239
    case space_explode "_" s of
blanchet@36486
   240
      [_] => (case take_suffix Char.isDigit (String.explode s) of
blanchet@36486
   241
                (cs1 as _ :: _, cs2 as _ :: _) =>
blanchet@36486
   242
                subscript_name (String.implode cs1)
blanchet@36486
   243
                               (the (Int.fromString (String.implode cs2)))
blanchet@36486
   244
              | (_, _) => s)
blanchet@36486
   245
    | [s1, s2] => (case Int.fromString s2 of
blanchet@36486
   246
                     SOME n => subscript_name s1 n
blanchet@36486
   247
                   | NONE => s)
blanchet@36486
   248
    | _ => s
blanchet@36486
   249
  end
blanchet@36486
   250
blanchet@36909
   251
(* First-order translation. No types are known for variables. "HOLogic.typeT"
blanchet@36909
   252
   should allow them to be inferred.*)
blanchet@36967
   253
fun term_from_node thy full_types tfrees =
blanchet@36909
   254
  let
blanchet@36909
   255
    fun aux opt_T args u =
blanchet@36909
   256
      case u of
blanchet@36909
   257
        IntLeaf _ => raise NODE [u]
blanchet@36909
   258
      | StrNode ("hBOOL", [u1]) => aux (SOME @{typ bool}) [] u1
blanchet@36909
   259
      | StrNode ("hAPP", [u1, u2]) => aux opt_T (u2 :: args) u1
blanchet@36909
   260
      | StrNode ("c_Not", [u1]) => @{const Not} $ aux (SOME @{typ bool}) [] u1
blanchet@36909
   261
      | StrNode (a, us) =>
blanchet@36909
   262
        if a = type_wrapper_name then
blanchet@36909
   263
          case us of
blanchet@36967
   264
            [term_u, typ_u] =>
blanchet@36967
   265
            aux (SOME (type_from_node tfrees typ_u)) args term_u
blanchet@36909
   266
          | _ => raise NODE us
blanchet@36909
   267
        else case strip_prefix const_prefix a of
blanchet@36909
   268
          SOME "equal" =>
blanchet@36909
   269
          list_comb (Const (@{const_name "op ="}, HOLogic.typeT),
blanchet@36909
   270
                     map (aux NONE []) us)
blanchet@36909
   271
        | SOME b =>
blanchet@36909
   272
          let
blanchet@36909
   273
            val c = invert_const b
blanchet@36909
   274
            val num_type_args = num_type_args thy c
blanchet@36909
   275
            val actual_num_type_args = if full_types then 0 else num_type_args
blanchet@36909
   276
            val num_term_args = length us - actual_num_type_args
blanchet@36909
   277
            val ts = map (aux NONE []) (take num_term_args us @ args)
blanchet@36909
   278
            val t =
blanchet@36909
   279
              Const (c, if full_types then
blanchet@36909
   280
                          case opt_T of
blanchet@36909
   281
                            SOME T => map fastype_of ts ---> T
blanchet@36909
   282
                          | NONE =>
blanchet@36909
   283
                            if num_type_args = 0 then
blanchet@36909
   284
                              Sign.const_instance thy (c, [])
blanchet@36909
   285
                            else
blanchet@36909
   286
                              raise Fail ("no type information for " ^ quote c)
blanchet@36909
   287
                        else
blanchet@36909
   288
                          (* Extra args from "hAPP" come after any arguments
blanchet@36909
   289
                             given directly to the constant. *)
blanchet@37410
   290
                          if String.isPrefix skolem_theory_name c then
blanchet@37399
   291
                            map fastype_of ts ---> HOLogic.typeT
blanchet@37399
   292
                          else
blanchet@37399
   293
                            Sign.const_instance thy (c,
blanchet@37399
   294
                                map (type_from_node tfrees)
blanchet@37399
   295
                                    (drop num_term_args us)))
blanchet@36909
   296
          in list_comb (t, ts) end
blanchet@36909
   297
        | NONE => (* a free or schematic variable *)
blanchet@36909
   298
          let
blanchet@36909
   299
            val ts = map (aux NONE []) (us @ args)
blanchet@36909
   300
            val T = map fastype_of ts ---> HOLogic.typeT
blanchet@36909
   301
            val t =
blanchet@36909
   302
              case strip_prefix fixed_var_prefix a of
blanchet@36909
   303
                SOME b => Free (b, T)
blanchet@36909
   304
              | NONE =>
blanchet@36909
   305
                case strip_prefix schematic_var_prefix a of
blanchet@36967
   306
                  SOME b => Var ((b, 0), T)
blanchet@36909
   307
                | NONE =>
blanchet@36909
   308
                  (* Variable from the ATP, say "X1" *)
blanchet@36967
   309
                  Var ((fix_atp_variable_name a, 0), T)
blanchet@36909
   310
          in list_comb (t, ts) end
blanchet@36909
   311
  in aux end
paulson@21978
   312
blanchet@36392
   313
(* Type class literal applied to a type. Returns triple of polarity, class,
blanchet@36392
   314
   type. *)
blanchet@36967
   315
fun type_constraint_from_node pos tfrees (StrNode ("c_Not", [u])) =
blanchet@36967
   316
    type_constraint_from_node (not pos) tfrees u
blanchet@36967
   317
  | type_constraint_from_node pos tfrees u = case u of
blanchet@36909
   318
        IntLeaf _ => raise NODE [u]
blanchet@36486
   319
      | StrNode (a, us) =>
blanchet@36967
   320
            (case (strip_prefix class_prefix a,
blanchet@36967
   321
                   map (type_from_node tfrees) us) of
blanchet@36486
   322
                 (SOME b, [T]) => (pos, b, T)
blanchet@36909
   323
               | _ => raise NODE [u])
paulson@21978
   324
paulson@21978
   325
(** Accumulate type constraints in a clause: negative type literals **)
paulson@21978
   326
blanchet@36485
   327
fun add_var (key, z)  = Vartab.map_default (key, []) (cons z)
paulson@21978
   328
blanchet@36909
   329
fun add_type_constraint (false, cl, TFree (a ,_)) = add_var ((a, ~1), cl)
blanchet@36909
   330
  | add_type_constraint (false, cl, TVar (ix, _)) = add_var (ix, cl)
blanchet@36909
   331
  | add_type_constraint _ = I
paulson@21978
   332
blanchet@36491
   333
fun is_positive_literal (@{const Not} $ _) = false
blanchet@37498
   334
  | is_positive_literal _ = true
blanchet@36402
   335
blanchet@36485
   336
fun negate_term thy (Const (@{const_name All}, T) $ Abs (s, T', t')) =
blanchet@36402
   337
    Const (@{const_name Ex}, T) $ Abs (s, T', negate_term thy t')
blanchet@36402
   338
  | negate_term thy (Const (@{const_name Ex}, T) $ Abs (s, T', t')) =
blanchet@36402
   339
    Const (@{const_name All}, T) $ Abs (s, T', negate_term thy t')
blanchet@36402
   340
  | negate_term thy (@{const "op -->"} $ t1 $ t2) =
blanchet@36402
   341
    @{const "op &"} $ t1 $ negate_term thy t2
blanchet@36402
   342
  | negate_term thy (@{const "op &"} $ t1 $ t2) =
blanchet@36402
   343
    @{const "op |"} $ negate_term thy t1 $ negate_term thy t2
blanchet@36402
   344
  | negate_term thy (@{const "op |"} $ t1 $ t2) =
blanchet@36402
   345
    @{const "op &"} $ negate_term thy t1 $ negate_term thy t2
blanchet@36486
   346
  | negate_term _ (@{const Not} $ t) = t
blanchet@36486
   347
  | negate_term _ t = @{const Not} $ t
blanchet@36402
   348
blanchet@36402
   349
fun clause_for_literals _ [] = HOLogic.false_const
blanchet@36402
   350
  | clause_for_literals _ [lit] = lit
blanchet@36402
   351
  | clause_for_literals thy lits =
blanchet@36402
   352
    case List.partition is_positive_literal lits of
blanchet@36402
   353
      (pos_lits as _ :: _, neg_lits as _ :: _) =>
blanchet@36402
   354
      @{const "op -->"}
blanchet@36402
   355
          $ foldr1 HOLogic.mk_conj (map (negate_term thy) neg_lits)
blanchet@36402
   356
          $ foldr1 HOLogic.mk_disj pos_lits
blanchet@36402
   357
    | _ => foldr1 HOLogic.mk_disj lits
blanchet@36402
   358
blanchet@36402
   359
(* Final treatment of the list of "real" literals from a clause.
blanchet@36402
   360
   No "real" literals means only type information. *)
blanchet@36402
   361
fun finish_clause _ [] = HOLogic.true_const
blanchet@36402
   362
  | finish_clause thy lits =
blanchet@36402
   363
    lits |> filter_out (curry (op =) HOLogic.false_const) |> rev
blanchet@36402
   364
         |> clause_for_literals thy
paulson@22491
   365
paulson@21978
   366
(*Accumulate sort constraints in vt, with "real" literals in lits.*)
blanchet@36967
   367
fun lits_of_nodes thy full_types tfrees =
blanchet@36967
   368
  let
blanchet@36967
   369
    fun aux (vt, lits) [] = (vt, finish_clause thy lits)
blanchet@36967
   370
      | aux (vt, lits) (u :: us) =
blanchet@36967
   371
        aux (add_type_constraint
blanchet@36967
   372
                 (type_constraint_from_node true tfrees u) vt, lits) us
blanchet@36967
   373
        handle NODE _ =>
blanchet@36967
   374
               aux (vt, term_from_node thy full_types tfrees (SOME @{typ bool})
blanchet@36967
   375
                                       [] u :: lits) us
blanchet@36967
   376
  in aux end
paulson@21978
   377
blanchet@36967
   378
(* Update TVars with detected sort constraints. It's not totally clear when
blanchet@36967
   379
   this code is necessary. *)
blanchet@36967
   380
fun repair_tvar_sorts vt =
blanchet@36556
   381
  let
blanchet@36556
   382
    fun do_type (Type (a, Ts)) = Type (a, map do_type Ts)
blanchet@36556
   383
      | do_type (TVar (xi, s)) = TVar (xi, the_default s (Vartab.lookup vt xi))
blanchet@36967
   384
      | do_type (TFree z) = TFree z
blanchet@36556
   385
    fun do_term (Const (a, T)) = Const (a, do_type T)
blanchet@36556
   386
      | do_term (Free (a, T)) = Free (a, do_type T)
blanchet@36556
   387
      | do_term (Var (xi, T)) = Var (xi, do_type T)
blanchet@36556
   388
      | do_term (t as Bound _) = t
blanchet@36556
   389
      | do_term (Abs (a, T, t)) = Abs (a, do_type T, do_term t)
blanchet@36556
   390
      | do_term (t1 $ t2) = do_term t1 $ do_term t2
blanchet@36556
   391
  in not (Vartab.is_empty vt) ? do_term end
blanchet@36551
   392
blanchet@36551
   393
fun unskolemize_term t =
blanchet@36909
   394
  Term.add_consts t []
blanchet@36909
   395
  |> filter (is_skolem_const_name o fst) |> map Const
blanchet@36909
   396
  |> rpair t |-> fold forall_of
paulson@21978
   397
blanchet@36555
   398
val combinator_table =
blanchet@36555
   399
  [(@{const_name COMBI}, @{thm COMBI_def_raw}),
blanchet@36555
   400
   (@{const_name COMBK}, @{thm COMBK_def_raw}),
blanchet@36555
   401
   (@{const_name COMBB}, @{thm COMBB_def_raw}),
blanchet@36555
   402
   (@{const_name COMBC}, @{thm COMBC_def_raw}),
blanchet@36555
   403
   (@{const_name COMBS}, @{thm COMBS_def_raw})]
blanchet@36555
   404
blanchet@36555
   405
fun uncombine_term (t1 $ t2) = betapply (pairself uncombine_term (t1, t2))
blanchet@36555
   406
  | uncombine_term (Abs (s, T, t')) = Abs (s, T, uncombine_term t')
blanchet@36555
   407
  | uncombine_term (t as Const (x as (s, _))) =
blanchet@36555
   408
    (case AList.lookup (op =) combinator_table s of
blanchet@36555
   409
       SOME thm => thm |> prop_of |> specialize_type @{theory} x |> Logic.dest_equals |> snd
blanchet@36555
   410
     | NONE => t)
blanchet@36555
   411
  | uncombine_term t = t
blanchet@36555
   412
blanchet@36486
   413
(* Interpret a list of syntax trees as a clause, given by "real" literals and
blanchet@36486
   414
   sort constraints. "vt" holds the initial sort constraints, from the
blanchet@36486
   415
   conjecture clauses. *)
blanchet@36967
   416
fun clause_of_nodes ctxt full_types tfrees us =
blanchet@36909
   417
  let
blanchet@36909
   418
    val thy = ProofContext.theory_of ctxt
blanchet@36967
   419
    val (vt, t) = lits_of_nodes thy full_types tfrees (Vartab.empty, []) us
blanchet@36967
   420
  in repair_tvar_sorts vt t end
blanchet@36556
   421
fun check_formula ctxt =
wenzelm@37145
   422
  Type_Infer.constrain @{typ bool}
blanchet@36486
   423
  #> Syntax.check_term (ProofContext.set_mode ProofContext.mode_schematic ctxt)
paulson@21978
   424
paulson@21978
   425
paulson@21978
   426
(**** Translation of TSTP files to Isar Proofs ****)
paulson@21978
   427
blanchet@36486
   428
fun unvarify_term (Var ((s, 0), T)) = Free (s, T)
blanchet@36486
   429
  | unvarify_term t = raise TERM ("unvarify_term: non-Var", [t])
paulson@21978
   430
blanchet@36967
   431
fun decode_line full_types tfrees (Definition (num, u, us)) ctxt =
blanchet@36486
   432
    let
blanchet@36967
   433
      val t1 = clause_of_nodes ctxt full_types tfrees [u]
blanchet@36551
   434
      val vars = snd (strip_comb t1)
blanchet@36486
   435
      val frees = map unvarify_term vars
blanchet@36486
   436
      val unvarify_args = subst_atomic (vars ~~ frees)
blanchet@36967
   437
      val t2 = clause_of_nodes ctxt full_types tfrees us
blanchet@36551
   438
      val (t1, t2) =
blanchet@36551
   439
        HOLogic.eq_const HOLogic.typeT $ t1 $ t2
blanchet@36556
   440
        |> unvarify_args |> uncombine_term |> check_formula ctxt
blanchet@36555
   441
        |> HOLogic.dest_eq
blanchet@36486
   442
    in
blanchet@36551
   443
      (Definition (num, t1, t2),
blanchet@36551
   444
       fold Variable.declare_term (maps OldTerm.term_frees [t1, t2]) ctxt)
blanchet@36486
   445
    end
blanchet@36967
   446
  | decode_line full_types tfrees (Inference (num, us, deps)) ctxt =
blanchet@36551
   447
    let
blanchet@36967
   448
      val t = us |> clause_of_nodes ctxt full_types tfrees
blanchet@36556
   449
                 |> unskolemize_term |> uncombine_term |> check_formula ctxt
blanchet@36551
   450
    in
blanchet@36551
   451
      (Inference (num, t, deps),
blanchet@36551
   452
       fold Variable.declare_term (OldTerm.term_frees t) ctxt)
blanchet@36486
   453
    end
blanchet@36967
   454
fun decode_lines ctxt full_types tfrees lines =
blanchet@36967
   455
  fst (fold_map (decode_line full_types tfrees) lines ctxt)
paulson@21978
   456
blanchet@37323
   457
fun aint_actual_inference _ (Definition _) = true
blanchet@37323
   458
  | aint_actual_inference t (Inference (_, t', _)) = not (t aconv t')
blanchet@36486
   459
blanchet@36486
   460
(* No "real" literals means only type information (tfree_tcs, clsrel, or
blanchet@36486
   461
   clsarity). *)
blanchet@36486
   462
val is_only_type_information = curry (op aconv) HOLogic.true_const
blanchet@36486
   463
blanchet@36486
   464
fun replace_one_dep (old, new) dep = if dep = old then new else [dep]
blanchet@36486
   465
fun replace_deps_in_line _ (line as Definition _) = line
blanchet@36486
   466
  | replace_deps_in_line p (Inference (num, t, deps)) =
blanchet@36486
   467
    Inference (num, t, fold (union (op =) o replace_one_dep p) deps [])
paulson@21978
   468
paulson@22491
   469
(*Discard axioms; consolidate adjacent lines that prove the same clause, since they differ
paulson@22491
   470
  only in type information.*)
blanchet@36551
   471
fun add_line _ _ (line as Definition _) lines = line :: lines
blanchet@36551
   472
  | add_line conjecture_shape thm_names (Inference (num, t, [])) lines =
blanchet@36570
   473
    (* No dependencies: axiom, conjecture clause, or internal axioms or
blanchet@36570
   474
       definitions (Vampire). *)
blanchet@36486
   475
    if is_axiom_clause_number thm_names num then
blanchet@36486
   476
      (* Axioms are not proof lines. *)
blanchet@36486
   477
      if is_only_type_information t then
blanchet@36486
   478
        map (replace_deps_in_line (num, [])) lines
blanchet@36486
   479
      (* Is there a repetition? If so, replace later line by earlier one. *)
blanchet@37323
   480
      else case take_prefix (aint_actual_inference t) lines of
blanchet@36486
   481
        (_, []) => lines (*no repetition of proof line*)
blanchet@36486
   482
      | (pre, Inference (num', _, _) :: post) =>
blanchet@36486
   483
        pre @ map (replace_deps_in_line (num', [num])) post
blanchet@36570
   484
    else if is_conjecture_clause_number conjecture_shape num then
blanchet@36486
   485
      Inference (num, t, []) :: lines
blanchet@36551
   486
    else
blanchet@36570
   487
      map (replace_deps_in_line (num, [])) lines
blanchet@36551
   488
  | add_line _ _ (Inference (num, t, deps)) lines =
blanchet@36486
   489
    (* Type information will be deleted later; skip repetition test. *)
blanchet@36486
   490
    if is_only_type_information t then
blanchet@36486
   491
      Inference (num, t, deps) :: lines
blanchet@36486
   492
    (* Is there a repetition? If so, replace later line by earlier one. *)
blanchet@37323
   493
    else case take_prefix (aint_actual_inference t) lines of
blanchet@36486
   494
      (* FIXME: Doesn't this code risk conflating proofs involving different
blanchet@36486
   495
         types?? *)
blanchet@36486
   496
       (_, []) => Inference (num, t, deps) :: lines
blanchet@36486
   497
     | (pre, Inference (num', t', _) :: post) =>
blanchet@36486
   498
       Inference (num, t', deps) ::
blanchet@36486
   499
       pre @ map (replace_deps_in_line (num', [num])) post
paulson@22044
   500
blanchet@36486
   501
(* Recursively delete empty lines (type information) from the proof. *)
blanchet@36486
   502
fun add_nontrivial_line (Inference (num, t, [])) lines =
blanchet@36486
   503
    if is_only_type_information t then delete_dep num lines
blanchet@36486
   504
    else Inference (num, t, []) :: lines
blanchet@36486
   505
  | add_nontrivial_line line lines = line :: lines
blanchet@36395
   506
and delete_dep num lines =
blanchet@36486
   507
  fold_rev add_nontrivial_line (map (replace_deps_in_line (num, [])) lines) []
blanchet@36486
   508
blanchet@37323
   509
(* ATPs sometimes reuse free variable names in the strangest ways. Removing
blanchet@37323
   510
   offending lines often does the trick. *)
blanchet@36560
   511
fun is_bad_free frees (Free x) = not (member (op =) frees x)
blanchet@36560
   512
  | is_bad_free _ _ = false
paulson@22470
   513
blanchet@36570
   514
(* Vampire is keen on producing these. *)
blanchet@36570
   515
fun is_trivial_formula (@{const Not} $ (Const (@{const_name "op ="}, _)
blanchet@36570
   516
                                        $ t1 $ t2)) = (t1 aconv t2)
blanchet@37498
   517
  | is_trivial_formula _ = false
blanchet@36570
   518
blanchet@37498
   519
fun add_desired_line _ _ _ _ (line as Definition (num, _, _)) (j, lines) =
blanchet@37323
   520
    (j, line :: map (replace_deps_in_line (num, [])) lines)
blanchet@37498
   521
  | add_desired_line isar_shrink_factor conjecture_shape thm_names frees
blanchet@36570
   522
                     (Inference (num, t, deps)) (j, lines) =
blanchet@36402
   523
    (j + 1,
blanchet@36570
   524
     if is_axiom_clause_number thm_names num orelse
blanchet@36570
   525
        is_conjecture_clause_number conjecture_shape num orelse
blanchet@36570
   526
        (not (is_only_type_information t) andalso
blanchet@36570
   527
         null (Term.add_tvars t []) andalso
blanchet@36570
   528
         not (exists_subterm (is_bad_free frees) t) andalso
blanchet@36570
   529
         not (is_trivial_formula t) andalso
blanchet@36570
   530
         (null lines orelse (* last line must be kept *)
blanchet@36924
   531
          (length deps >= 2 andalso j mod isar_shrink_factor = 0))) then
blanchet@36570
   532
       Inference (num, t, deps) :: lines  (* keep line *)
blanchet@36402
   533
     else
blanchet@36570
   534
       map (replace_deps_in_line (num, deps)) lines)  (* drop line *)
paulson@21978
   535
blanchet@36402
   536
(** EXTRACTING LEMMAS **)
paulson@21979
   537
blanchet@36223
   538
(* A list consisting of the first number in each line is returned.
blanchet@36395
   539
   TSTP: Interesting lines have the form "cnf(108, axiom, ...)", where the
blanchet@36223
   540
   number (108) is extracted.
blanchet@36395
   541
   SPASS: Lines have the form "108[0:Inp] ...", where the first number (108) is
blanchet@36223
   542
   extracted. *)
blanchet@36402
   543
fun extract_clause_numbers_in_atp_proof atp_proof =
blanchet@35865
   544
  let
blanchet@36395
   545
    val tokens_of = String.tokens (not o is_ident_char)
blanchet@36402
   546
    fun extract_num ("cnf" :: num :: "axiom" :: _) = Int.fromString num
blanchet@36395
   547
      | extract_num (num :: "0" :: "Inp" :: _) = Int.fromString num
blanchet@36395
   548
      | extract_num _ = NONE
blanchet@36402
   549
  in atp_proof |> split_lines |> map_filter (extract_num o tokens_of) end
blanchet@37399
   550
blanchet@37399
   551
val indent_size = 2
blanchet@37399
   552
val no_label = ("", ~1)
blanchet@37399
   553
blanchet@37399
   554
val raw_prefix = "X"
blanchet@37399
   555
val assum_prefix = "A"
blanchet@37399
   556
val fact_prefix = "F"
blanchet@37399
   557
blanchet@37399
   558
fun string_for_label (s, num) = s ^ string_of_int num
blanchet@37399
   559
blanchet@37399
   560
fun metis_using [] = ""
blanchet@37399
   561
  | metis_using ls =
blanchet@37399
   562
    "using " ^ space_implode " " (map string_for_label ls) ^ " "
blanchet@37399
   563
fun metis_apply _ 1 = "by "
blanchet@37399
   564
  | metis_apply 1 _ = "apply "
blanchet@37399
   565
  | metis_apply i _ = "prefer " ^ string_of_int i ^ " apply "
blanchet@37479
   566
fun metis_name full_types = if full_types then "metisFT" else "metis"
blanchet@37479
   567
fun metis_call full_types [] = metis_name full_types
blanchet@37479
   568
  | metis_call full_types ss =
blanchet@37479
   569
    "(" ^ metis_name full_types ^ " " ^ space_implode " " ss ^ ")"
blanchet@37479
   570
fun metis_command full_types i n (ls, ss) =
blanchet@37479
   571
  metis_using ls ^ metis_apply i n ^ metis_call full_types ss
blanchet@37479
   572
fun metis_line full_types i n ss =
blanchet@36063
   573
  "Try this command: " ^
blanchet@37479
   574
  Markup.markup Markup.sendback (metis_command full_types i n ([], ss)) ^ ".\n"
blanchet@36281
   575
fun minimize_line _ [] = ""
blanchet@36281
   576
  | minimize_line minimize_command facts =
blanchet@36281
   577
    case minimize_command facts of
blanchet@36281
   578
      "" => ""
blanchet@36281
   579
    | command =>
blanchet@36065
   580
      "To minimize the number of lemmas, try this command: " ^
blanchet@36281
   581
      Markup.markup Markup.sendback command ^ ".\n"
immler@31840
   582
blanchet@37171
   583
val unprefix_chained = perhaps (try (unprefix chained_prefix))
blanchet@37171
   584
blanchet@37479
   585
fun metis_proof_text (full_types, minimize_command, atp_proof, thm_names, goal,
blanchet@37479
   586
                      i) =
blanchet@36063
   587
  let
blanchet@37171
   588
    val raw_lemmas =
blanchet@36402
   589
      atp_proof |> extract_clause_numbers_in_atp_proof
blanchet@36402
   590
                |> filter (is_axiom_clause_number thm_names)
blanchet@36402
   591
                |> map (fn i => Vector.sub (thm_names, i - 1))
blanchet@37171
   592
    val (chained_lemmas, other_lemmas) =
blanchet@37171
   593
      raw_lemmas |> List.partition (String.isPrefix chained_prefix)
blanchet@37171
   594
                 |>> map (unprefix chained_prefix)
blanchet@37171
   595
                 |> pairself (sort_distinct string_ord)
blanchet@37171
   596
    val lemmas = other_lemmas @ chained_lemmas
blanchet@36063
   597
    val n = Logic.count_prems (prop_of goal)
blanchet@37171
   598
  in
blanchet@37479
   599
    (metis_line full_types i n other_lemmas ^
blanchet@37479
   600
     minimize_line minimize_command lemmas, lemmas)
blanchet@37171
   601
  end
immler@31037
   602
blanchet@36486
   603
(** Isar proof construction and manipulation **)
blanchet@36486
   604
blanchet@36486
   605
fun merge_fact_sets (ls1, ss1) (ls2, ss2) =
blanchet@36486
   606
  (union (op =) ls1 ls2, union (op =) ss1 ss2)
blanchet@36402
   607
blanchet@36402
   608
type label = string * int
blanchet@36402
   609
type facts = label list * string list
blanchet@36402
   610
blanchet@36402
   611
datatype qualifier = Show | Then | Moreover | Ultimately
blanchet@36291
   612
blanchet@36402
   613
datatype step =
blanchet@36478
   614
  Fix of (string * typ) list |
blanchet@36486
   615
  Let of term * term |
blanchet@36402
   616
  Assume of label * term |
blanchet@36402
   617
  Have of qualifier list * label * term * byline
blanchet@36402
   618
and byline =
blanchet@36564
   619
  ByMetis of facts |
blanchet@36402
   620
  CaseSplit of step list list * facts
blanchet@36402
   621
blanchet@36574
   622
fun smart_case_split [] facts = ByMetis facts
blanchet@36574
   623
  | smart_case_split proofs facts = CaseSplit (proofs, facts)
blanchet@36574
   624
blanchet@36475
   625
fun add_fact_from_dep thm_names num =
blanchet@36475
   626
  if is_axiom_clause_number thm_names num then
blanchet@36480
   627
    apsnd (insert (op =) (Vector.sub (thm_names, num - 1)))
blanchet@36475
   628
  else
blanchet@36480
   629
    apfst (insert (op =) (raw_prefix, num))
blanchet@36402
   630
blanchet@36491
   631
fun forall_vars t = fold_rev forall_of (map Var (Term.add_vars t [])) t
blanchet@36491
   632
blanchet@37498
   633
fun step_for_line _ _ (Definition (_, t1, t2)) = Let (t1, t2)
blanchet@36486
   634
  | step_for_line _ _ (Inference (num, t, [])) = Assume ((raw_prefix, num), t)
blanchet@36486
   635
  | step_for_line thm_names j (Inference (num, t, deps)) =
blanchet@36486
   636
    Have (if j = 1 then [Show] else [], (raw_prefix, num),
blanchet@36491
   637
          forall_vars t,
blanchet@36564
   638
          ByMetis (fold (add_fact_from_dep thm_names) deps ([], [])))
blanchet@36291
   639
blanchet@36967
   640
fun proof_from_atp_proof pool ctxt full_types tfrees isar_shrink_factor
blanchet@36967
   641
                         atp_proof conjecture_shape thm_names params frees =
blanchet@36402
   642
  let
blanchet@36486
   643
    val lines =
blanchet@36574
   644
      atp_proof ^ "$" (* the $ sign acts as a sentinel *)
blanchet@36548
   645
      |> parse_proof pool
blanchet@36967
   646
      |> decode_lines ctxt full_types tfrees
blanchet@36551
   647
      |> rpair [] |-> fold_rev (add_line conjecture_shape thm_names)
blanchet@36486
   648
      |> rpair [] |-> fold_rev add_nontrivial_line
blanchet@37498
   649
      |> rpair (0, []) |-> fold_rev (add_desired_line isar_shrink_factor
blanchet@36570
   650
                                               conjecture_shape thm_names frees)
blanchet@36486
   651
      |> snd
blanchet@36402
   652
  in
blanchet@36909
   653
    (if null params then [] else [Fix params]) @
blanchet@36486
   654
    map2 (step_for_line thm_names) (length lines downto 1) lines
blanchet@36402
   655
  end
blanchet@36402
   656
blanchet@36402
   657
(* When redirecting proofs, we keep information about the labels seen so far in
blanchet@36402
   658
   the "backpatches" data structure. The first component indicates which facts
blanchet@36402
   659
   should be associated with forthcoming proof steps. The second component is a
blanchet@37322
   660
   pair ("assum_ls", "drop_ls"), where "assum_ls" are the labels that should
blanchet@37322
   661
   become assumptions and "drop_ls" are the labels that should be dropped in a
blanchet@37322
   662
   case split. *)
blanchet@36402
   663
type backpatches = (label * facts) list * (label list * label list)
blanchet@36402
   664
blanchet@36556
   665
fun used_labels_of_step (Have (_, _, _, by)) =
blanchet@36402
   666
    (case by of
blanchet@36564
   667
       ByMetis (ls, _) => ls
blanchet@36556
   668
     | CaseSplit (proofs, (ls, _)) =>
blanchet@36556
   669
       fold (union (op =) o used_labels_of) proofs ls)
blanchet@36556
   670
  | used_labels_of_step _ = []
blanchet@36556
   671
and used_labels_of proof = fold (union (op =) o used_labels_of_step) proof []
blanchet@36402
   672
blanchet@36402
   673
fun new_labels_of_step (Fix _) = []
blanchet@36486
   674
  | new_labels_of_step (Let _) = []
blanchet@36402
   675
  | new_labels_of_step (Assume (l, _)) = [l]
blanchet@36402
   676
  | new_labels_of_step (Have (_, l, _, _)) = [l]
blanchet@36402
   677
val new_labels_of = maps new_labels_of_step
blanchet@36402
   678
blanchet@36402
   679
val join_proofs =
blanchet@36402
   680
  let
blanchet@36402
   681
    fun aux _ [] = NONE
blanchet@36402
   682
      | aux proof_tail (proofs as (proof1 :: _)) =
blanchet@36402
   683
        if exists null proofs then
blanchet@36402
   684
          NONE
blanchet@36402
   685
        else if forall (curry (op =) (hd proof1) o hd) (tl proofs) then
blanchet@36402
   686
          aux (hd proof1 :: proof_tail) (map tl proofs)
blanchet@36402
   687
        else case hd proof1 of
blanchet@37498
   688
          Have ([], l, t, _) => (* FIXME: should we really ignore the "by"? *)
blanchet@36402
   689
          if forall (fn Have ([], l', t', _) :: _ => (l, t) = (l', t')
blanchet@36402
   690
                      | _ => false) (tl proofs) andalso
blanchet@36402
   691
             not (exists (member (op =) (maps new_labels_of proofs))
blanchet@36556
   692
                         (used_labels_of proof_tail)) then
blanchet@36402
   693
            SOME (l, t, map rev proofs, proof_tail)
blanchet@36402
   694
          else
blanchet@36402
   695
            NONE
blanchet@36402
   696
        | _ => NONE
blanchet@36402
   697
  in aux [] o map rev end
blanchet@36402
   698
blanchet@36402
   699
fun case_split_qualifiers proofs =
blanchet@36402
   700
  case length proofs of
blanchet@36402
   701
    0 => []
blanchet@36402
   702
  | 1 => [Then]
blanchet@36402
   703
  | _ => [Ultimately]
blanchet@36402
   704
blanchet@36491
   705
fun redirect_proof thy conjecture_shape hyp_ts concl_t proof =
wenzelm@33310
   706
  let
blanchet@37324
   707
    (* The first pass outputs those steps that are independent of the negated
blanchet@37324
   708
       conjecture. The second pass flips the proof by contradiction to obtain a
blanchet@37324
   709
       direct proof, introducing case splits when an inference depends on
blanchet@37324
   710
       several facts that depend on the negated conjecture. *)
blanchet@37324
   711
    fun find_hyp num = nth hyp_ts (index_in_shape num conjecture_shape)
blanchet@36402
   712
    val concl_ls = map (pair raw_prefix) (List.last conjecture_shape)
blanchet@37324
   713
    val canonicalize_labels =
blanchet@37324
   714
      map (fn l => if member (op =) concl_ls l then hd concl_ls else l)
blanchet@37324
   715
      #> distinct (op =)
blanchet@36402
   716
    fun first_pass ([], contra) = ([], contra)
blanchet@36491
   717
      | first_pass ((step as Fix _) :: proof, contra) =
blanchet@36491
   718
        first_pass (proof, contra) |>> cons step
blanchet@36491
   719
      | first_pass ((step as Let _) :: proof, contra) =
blanchet@36491
   720
        first_pass (proof, contra) |>> cons step
blanchet@37498
   721
      | first_pass ((step as Assume (l as (_, num), _)) :: proof, contra) =
blanchet@36402
   722
        if member (op =) concl_ls l then
blanchet@37324
   723
          first_pass (proof, contra ||> l = hd concl_ls ? cons step)
blanchet@36402
   724
        else
blanchet@36551
   725
          first_pass (proof, contra) |>> cons (Assume (l, find_hyp num))
blanchet@37324
   726
      | first_pass (Have (qs, l, t, ByMetis (ls, ss)) :: proof, contra) =
blanchet@37324
   727
        let
blanchet@37324
   728
          val ls = canonicalize_labels ls
blanchet@37324
   729
          val step = Have (qs, l, t, ByMetis (ls, ss))
blanchet@37324
   730
        in
blanchet@37324
   731
          if exists (member (op =) (fst contra)) ls then
blanchet@37324
   732
            first_pass (proof, contra |>> cons l ||> cons step)
blanchet@37324
   733
          else
blanchet@37324
   734
            first_pass (proof, contra) |>> cons step
blanchet@37324
   735
        end
blanchet@36402
   736
      | first_pass _ = raise Fail "malformed proof"
blanchet@36402
   737
    val (proof_top, (contra_ls, contra_proof)) =
blanchet@36402
   738
      first_pass (proof, (concl_ls, []))
blanchet@36402
   739
    val backpatch_label = the_default ([], []) oo AList.lookup (op =) o fst
blanchet@36402
   740
    fun backpatch_labels patches ls =
blanchet@36402
   741
      fold merge_fact_sets (map (backpatch_label patches) ls) ([], [])
blanchet@36402
   742
    fun second_pass end_qs ([], assums, patches) =
blanchet@37324
   743
        ([Have (end_qs, no_label, concl_t,
blanchet@36564
   744
                ByMetis (backpatch_labels patches (map snd assums)))], patches)
blanchet@36402
   745
      | second_pass end_qs (Assume (l, t) :: proof, assums, patches) =
blanchet@36402
   746
        second_pass end_qs (proof, (t, l) :: assums, patches)
blanchet@36564
   747
      | second_pass end_qs (Have (qs, l, t, ByMetis (ls, ss)) :: proof, assums,
blanchet@36402
   748
                            patches) =
blanchet@36402
   749
        if member (op =) (snd (snd patches)) l andalso
blanchet@37322
   750
           not (member (op =) (fst (snd patches)) l) andalso
blanchet@36402
   751
           not (AList.defined (op =) (fst patches) l) then
blanchet@36402
   752
          second_pass end_qs (proof, assums, patches ||> apsnd (append ls))
blanchet@36402
   753
        else
blanchet@36402
   754
          (case List.partition (member (op =) contra_ls) ls of
blanchet@36402
   755
             ([contra_l], co_ls) =>
blanchet@37322
   756
             if member (op =) qs Show then
blanchet@37322
   757
               second_pass end_qs (proof, assums,
blanchet@37322
   758
                                   patches |>> cons (contra_l, (co_ls, ss)))
blanchet@37322
   759
             else
blanchet@36402
   760
               second_pass end_qs
blanchet@36402
   761
                           (proof, assums,
blanchet@36402
   762
                            patches |>> cons (contra_l, (l :: co_ls, ss)))
blanchet@36402
   763
               |>> cons (if member (op =) (fst (snd patches)) l then
blanchet@36491
   764
                           Assume (l, negate_term thy t)
blanchet@36402
   765
                         else
blanchet@36491
   766
                           Have (qs, l, negate_term thy t,
blanchet@36564
   767
                                 ByMetis (backpatch_label patches l)))
blanchet@36402
   768
           | (contra_ls as _ :: _, co_ls) =>
blanchet@36402
   769
             let
blanchet@36402
   770
               val proofs =
blanchet@36402
   771
                 map_filter
blanchet@36402
   772
                     (fn l =>
blanchet@36402
   773
                         if member (op =) concl_ls l then
blanchet@36402
   774
                           NONE
blanchet@36402
   775
                         else
blanchet@36402
   776
                           let
blanchet@36402
   777
                             val drop_ls = filter (curry (op <>) l) contra_ls
blanchet@36402
   778
                           in
blanchet@36402
   779
                             second_pass []
blanchet@36402
   780
                                 (proof, assums,
blanchet@36402
   781
                                  patches ||> apfst (insert (op =) l)
blanchet@36402
   782
                                          ||> apsnd (union (op =) drop_ls))
blanchet@36402
   783
                             |> fst |> SOME
blanchet@36402
   784
                           end) contra_ls
blanchet@37324
   785
               val (assumes, facts) =
blanchet@37324
   786
                 if member (op =) (fst (snd patches)) l then
blanchet@37324
   787
                   ([Assume (l, negate_term thy t)], (l :: co_ls, ss))
blanchet@37324
   788
                 else
blanchet@37324
   789
                   ([], (co_ls, ss))
blanchet@36402
   790
             in
blanchet@36402
   791
               (case join_proofs proofs of
blanchet@36402
   792
                  SOME (l, t, proofs, proof_tail) =>
blanchet@36402
   793
                  Have (case_split_qualifiers proofs @
blanchet@36402
   794
                        (if null proof_tail then end_qs else []), l, t,
blanchet@36574
   795
                        smart_case_split proofs facts) :: proof_tail
blanchet@36402
   796
                | NONE =>
blanchet@36402
   797
                  [Have (case_split_qualifiers proofs @ end_qs, no_label,
blanchet@36574
   798
                         concl_t, smart_case_split proofs facts)],
blanchet@36402
   799
                patches)
blanchet@37324
   800
               |>> append assumes
blanchet@36402
   801
             end
blanchet@36402
   802
           | _ => raise Fail "malformed proof")
blanchet@36402
   803
       | second_pass _ _ = raise Fail "malformed proof"
blanchet@36486
   804
    val proof_bottom =
blanchet@36486
   805
      second_pass [Show] (contra_proof, [], ([], ([], []))) |> fst
blanchet@36402
   806
  in proof_top @ proof_bottom end
blanchet@36402
   807
blanchet@36402
   808
val kill_duplicate_assumptions_in_proof =
blanchet@36402
   809
  let
blanchet@36402
   810
    fun relabel_facts subst =
blanchet@36402
   811
      apfst (map (fn l => AList.lookup (op =) subst l |> the_default l))
blanchet@36491
   812
    fun do_step (step as Assume (l, t)) (proof, subst, assums) =
blanchet@36402
   813
        (case AList.lookup (op aconv) assums t of
blanchet@36967
   814
           SOME l' => (proof, (l, l') :: subst, assums)
blanchet@36491
   815
         | NONE => (step :: proof, subst, (t, l) :: assums))
blanchet@36402
   816
      | do_step (Have (qs, l, t, by)) (proof, subst, assums) =
blanchet@36402
   817
        (Have (qs, l, t,
blanchet@36402
   818
               case by of
blanchet@36564
   819
                 ByMetis facts => ByMetis (relabel_facts subst facts)
blanchet@36402
   820
               | CaseSplit (proofs, facts) =>
blanchet@36402
   821
                 CaseSplit (map do_proof proofs, relabel_facts subst facts)) ::
blanchet@36402
   822
         proof, subst, assums)
blanchet@36491
   823
      | do_step step (proof, subst, assums) = (step :: proof, subst, assums)
blanchet@36402
   824
    and do_proof proof = fold do_step proof ([], [], []) |> #1 |> rev
blanchet@36402
   825
  in do_proof end
blanchet@36402
   826
blanchet@36402
   827
val then_chain_proof =
blanchet@36402
   828
  let
blanchet@36402
   829
    fun aux _ [] = []
blanchet@36491
   830
      | aux _ ((step as Assume (l, _)) :: proof) = step :: aux l proof
blanchet@36402
   831
      | aux l' (Have (qs, l, t, by) :: proof) =
blanchet@36402
   832
        (case by of
blanchet@36564
   833
           ByMetis (ls, ss) =>
blanchet@36402
   834
           Have (if member (op =) ls l' then
blanchet@36402
   835
                   (Then :: qs, l, t,
blanchet@36564
   836
                    ByMetis (filter_out (curry (op =) l') ls, ss))
blanchet@36402
   837
                 else
blanchet@36564
   838
                   (qs, l, t, ByMetis (ls, ss)))
blanchet@36402
   839
         | CaseSplit (proofs, facts) =>
blanchet@36402
   840
           Have (qs, l, t, CaseSplit (map (aux no_label) proofs, facts))) ::
blanchet@36402
   841
        aux l proof
blanchet@36491
   842
      | aux _ (step :: proof) = step :: aux no_label proof
blanchet@36402
   843
  in aux no_label end
blanchet@36402
   844
blanchet@36402
   845
fun kill_useless_labels_in_proof proof =
blanchet@36402
   846
  let
blanchet@36556
   847
    val used_ls = used_labels_of proof
blanchet@36402
   848
    fun do_label l = if member (op =) used_ls l then l else no_label
blanchet@36556
   849
    fun do_step (Assume (l, t)) = Assume (do_label l, t)
blanchet@36556
   850
      | do_step (Have (qs, l, t, by)) =
blanchet@36402
   851
        Have (qs, do_label l, t,
blanchet@36402
   852
              case by of
blanchet@36402
   853
                CaseSplit (proofs, facts) =>
blanchet@36556
   854
                CaseSplit (map (map do_step) proofs, facts)
blanchet@36402
   855
              | _ => by)
blanchet@36556
   856
      | do_step step = step
blanchet@36556
   857
  in map do_step proof end
blanchet@36402
   858
blanchet@36402
   859
fun prefix_for_depth n = replicate_string (n + 1)
blanchet@36402
   860
blanchet@36402
   861
val relabel_proof =
blanchet@36402
   862
  let
blanchet@36402
   863
    fun aux _ _ _ [] = []
blanchet@36402
   864
      | aux subst depth (next_assum, next_fact) (Assume (l, t) :: proof) =
blanchet@36402
   865
        if l = no_label then
blanchet@36402
   866
          Assume (l, t) :: aux subst depth (next_assum, next_fact) proof
blanchet@36402
   867
        else
blanchet@36402
   868
          let val l' = (prefix_for_depth depth assum_prefix, next_assum) in
blanchet@36402
   869
            Assume (l', t) ::
blanchet@36402
   870
            aux ((l, l') :: subst) depth (next_assum + 1, next_fact) proof
blanchet@36402
   871
          end
blanchet@36402
   872
      | aux subst depth (next_assum, next_fact) (Have (qs, l, t, by) :: proof) =
blanchet@36402
   873
        let
blanchet@36402
   874
          val (l', subst, next_fact) =
blanchet@36402
   875
            if l = no_label then
blanchet@36402
   876
              (l, subst, next_fact)
blanchet@36402
   877
            else
blanchet@36402
   878
              let
blanchet@36402
   879
                val l' = (prefix_for_depth depth fact_prefix, next_fact)
blanchet@36402
   880
              in (l', (l, l') :: subst, next_fact + 1) end
blanchet@36570
   881
          val relabel_facts =
blanchet@36570
   882
            apfst (map (fn l =>
blanchet@36570
   883
                           case AList.lookup (op =) subst l of
blanchet@36570
   884
                             SOME l' => l'
blanchet@36570
   885
                           | NONE => raise Fail ("unknown label " ^
blanchet@36570
   886
                                                 quote (string_for_label l))))
blanchet@36402
   887
          val by =
blanchet@36402
   888
            case by of
blanchet@36564
   889
              ByMetis facts => ByMetis (relabel_facts facts)
blanchet@36402
   890
            | CaseSplit (proofs, facts) =>
blanchet@36402
   891
              CaseSplit (map (aux subst (depth + 1) (1, 1)) proofs,
blanchet@36402
   892
                         relabel_facts facts)
blanchet@36402
   893
        in
blanchet@36402
   894
          Have (qs, l', t, by) ::
blanchet@36402
   895
          aux subst depth (next_assum, next_fact) proof
blanchet@36402
   896
        end
blanchet@36491
   897
      | aux subst depth nextp (step :: proof) =
blanchet@36491
   898
        step :: aux subst depth nextp proof
blanchet@36402
   899
  in aux [] 0 (1, 1) end
blanchet@36402
   900
blanchet@37479
   901
fun string_for_proof ctxt full_types i n =
blanchet@36402
   902
  let
blanchet@37319
   903
    fun fix_print_mode f x =
blanchet@37319
   904
      setmp_CRITICAL show_no_free_types true
blanchet@37319
   905
          (setmp_CRITICAL show_types true
blanchet@37319
   906
               (Print_Mode.setmp (filter (curry (op =) Symbol.xsymbolsN)
blanchet@37319
   907
                                         (print_mode_value ())) f)) x
blanchet@36402
   908
    fun do_indent ind = replicate_string (ind * indent_size) " "
blanchet@36478
   909
    fun do_free (s, T) =
blanchet@36478
   910
      maybe_quote s ^ " :: " ^
blanchet@36478
   911
      maybe_quote (fix_print_mode (Syntax.string_of_typ ctxt) T)
blanchet@36570
   912
    fun do_label l = if l = no_label then "" else string_for_label l ^ ": "
blanchet@36402
   913
    fun do_have qs =
blanchet@36402
   914
      (if member (op =) qs Moreover then "moreover " else "") ^
blanchet@36402
   915
      (if member (op =) qs Ultimately then "ultimately " else "") ^
blanchet@36402
   916
      (if member (op =) qs Then then
blanchet@36402
   917
         if member (op =) qs Show then "thus" else "hence"
blanchet@36402
   918
       else
blanchet@36402
   919
         if member (op =) qs Show then "show" else "have")
blanchet@36478
   920
    val do_term = maybe_quote o fix_print_mode (Syntax.string_of_term ctxt)
blanchet@36570
   921
    fun do_facts (ls, ss) =
blanchet@36570
   922
      let
blanchet@36570
   923
        val ls = ls |> sort_distinct (prod_ord string_ord int_ord)
blanchet@37171
   924
        val ss = ss |> map unprefix_chained |> sort_distinct string_ord
blanchet@37479
   925
      in metis_command full_types 1 1 (ls, ss) end
blanchet@36478
   926
    and do_step ind (Fix xs) =
blanchet@36478
   927
        do_indent ind ^ "fix " ^ space_implode " and " (map do_free xs) ^ "\n"
blanchet@36486
   928
      | do_step ind (Let (t1, t2)) =
blanchet@36486
   929
        do_indent ind ^ "let " ^ do_term t1 ^ " = " ^ do_term t2 ^ "\n"
blanchet@36402
   930
      | do_step ind (Assume (l, t)) =
blanchet@36402
   931
        do_indent ind ^ "assume " ^ do_label l ^ do_term t ^ "\n"
blanchet@36564
   932
      | do_step ind (Have (qs, l, t, ByMetis facts)) =
blanchet@36402
   933
        do_indent ind ^ do_have qs ^ " " ^
blanchet@36479
   934
        do_label l ^ do_term t ^ " " ^ do_facts facts ^ "\n"
blanchet@36402
   935
      | do_step ind (Have (qs, l, t, CaseSplit (proofs, facts))) =
blanchet@36402
   936
        space_implode (do_indent ind ^ "moreover\n")
blanchet@36402
   937
                      (map (do_block ind) proofs) ^
blanchet@36479
   938
        do_indent ind ^ do_have qs ^ " " ^ do_label l ^ do_term t ^ " " ^
blanchet@36478
   939
        do_facts facts ^ "\n"
blanchet@36402
   940
    and do_steps prefix suffix ind steps =
blanchet@36402
   941
      let val s = implode (map (do_step ind) steps) in
blanchet@36402
   942
        replicate_string (ind * indent_size - size prefix) " " ^ prefix ^
blanchet@36402
   943
        String.extract (s, ind * indent_size,
blanchet@36402
   944
                        SOME (size s - ind * indent_size - 1)) ^
blanchet@36402
   945
        suffix ^ "\n"
blanchet@36402
   946
      end
blanchet@36402
   947
    and do_block ind proof = do_steps "{ " " }" (ind + 1) proof
blanchet@36564
   948
    (* One-step proofs are pointless; better use the Metis one-liner
blanchet@36564
   949
       directly. *)
blanchet@36564
   950
    and do_proof [Have (_, _, _, ByMetis _)] = ""
blanchet@36564
   951
      | do_proof proof =
blanchet@36480
   952
        (if i <> 1 then "prefer " ^ string_of_int i ^ "\n" else "") ^
blanchet@36480
   953
        do_indent 0 ^ "proof -\n" ^
blanchet@36480
   954
        do_steps "" "" 1 proof ^
blanchet@36480
   955
        do_indent 0 ^ (if n <> 1 then "next" else "qed") ^ "\n"
blanchet@36488
   956
  in do_proof end
blanchet@36402
   957
blanchet@37498
   958
fun strip_subgoal goal i =
blanchet@37498
   959
  let
blanchet@37498
   960
    val (t, frees) = Logic.goal_params (prop_of goal) i
blanchet@37498
   961
    val hyp_ts = t |> Logic.strip_assums_hyp |> map (curry subst_bounds frees)
blanchet@37498
   962
    val concl_t = t |> Logic.strip_assums_concl |> curry subst_bounds frees
blanchet@37498
   963
  in (rev (map dest_Free frees), hyp_ts, concl_t) end
blanchet@37498
   964
blanchet@37479
   965
fun isar_proof_text (pool, debug, isar_shrink_factor, ctxt, conjecture_shape)
blanchet@37479
   966
                    (other_params as (full_types, _, atp_proof, thm_names, goal,
blanchet@37479
   967
                                      i)) =
blanchet@36402
   968
  let
blanchet@36402
   969
    val thy = ProofContext.theory_of ctxt
blanchet@36909
   970
    val (params, hyp_ts, concl_t) = strip_subgoal goal i
blanchet@36909
   971
    val frees = fold Term.add_frees (concl_t :: hyp_ts) []
blanchet@36967
   972
    val tfrees = fold Term.add_tfrees (concl_t :: hyp_ts) []
blanchet@36402
   973
    val n = Logic.count_prems (prop_of goal)
blanchet@37479
   974
    val (one_line_proof, lemma_names) = metis_proof_text other_params
blanchet@36283
   975
    fun isar_proof_for () =
blanchet@36967
   976
      case proof_from_atp_proof pool ctxt full_types tfrees isar_shrink_factor
blanchet@36924
   977
                                atp_proof conjecture_shape thm_names params
blanchet@36924
   978
                                frees
blanchet@36491
   979
           |> redirect_proof thy conjecture_shape hyp_ts concl_t
blanchet@36402
   980
           |> kill_duplicate_assumptions_in_proof
blanchet@36402
   981
           |> then_chain_proof
blanchet@36402
   982
           |> kill_useless_labels_in_proof
blanchet@36402
   983
           |> relabel_proof
blanchet@37479
   984
           |> string_for_proof ctxt full_types i n of
blanchet@36283
   985
        "" => ""
blanchet@36402
   986
      | proof => "\nStructured proof:\n" ^ Markup.markup Markup.sendback proof
blanchet@35868
   987
    val isar_proof =
blanchet@36402
   988
      if debug then
blanchet@36283
   989
        isar_proof_for ()
blanchet@36283
   990
      else
blanchet@36283
   991
        try isar_proof_for ()
blanchet@36287
   992
        |> the_default "Warning: The Isar proof construction failed.\n"
blanchet@36283
   993
  in (one_line_proof ^ isar_proof, lemma_names) end
paulson@21978
   994
blanchet@36557
   995
fun proof_text isar_proof isar_params other_params =
blanchet@36557
   996
  (if isar_proof then isar_proof_text isar_params else metis_proof_text)
blanchet@36557
   997
      other_params
blanchet@36223
   998
immler@31038
   999
end;