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