src/HOL/Tools/ATP/atp_proof_reconstruct.ML
author blanchet
Tue Nov 19 19:36:24 2013 +0100 (2013-11-19)
changeset 54505 d023838eb252
parent 54501 77c9460e01b0
child 54506 8b5caa190054
permissions -rw-r--r--
more refactoring to accommodate SMT proofs
blanchet@46320
     1
(*  Title:      HOL/Tools/ATP/atp_proof_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
blanchet@49914
     6
Basic proof reconstruction from ATP proofs.
wenzelm@33310
     7
*)
wenzelm@33310
     8
blanchet@46320
     9
signature ATP_PROOF_RECONSTRUCT =
paulson@24425
    10
sig
blanchet@53586
    11
  type ('a, 'b) atp_term = ('a, 'b) ATP_Problem.atp_term
blanchet@53586
    12
  type ('a, 'b, 'c, 'd) atp_formula = ('a, 'b, 'c, 'd) ATP_Problem.atp_formula
blanchet@54500
    13
  type stature = ATP_Problem_Generate.stature
blanchet@54499
    14
  type ('a, 'b) atp_step = ('a, 'b) ATP_Proof.atp_step
blanchet@54499
    15
  type 'a atp_proof = 'a ATP_Proof.atp_proof
blanchet@45519
    16
blanchet@45519
    17
  val metisN : string
blanchet@45519
    18
  val full_typesN : string
blanchet@45519
    19
  val partial_typesN : string
blanchet@45519
    20
  val no_typesN : string
blanchet@45519
    21
  val really_full_type_enc : string
blanchet@45519
    22
  val full_type_enc : string
blanchet@45519
    23
  val partial_type_enc : string
blanchet@45519
    24
  val no_type_enc : string
blanchet@45519
    25
  val full_type_encs : string list
blanchet@45519
    26
  val partial_type_encs : string list
blanchet@54500
    27
  val default_metis_lam_trans : string
blanchet@45554
    28
  val metis_call : string -> string -> string
blanchet@49983
    29
  val forall_of : term -> term -> term
blanchet@49983
    30
  val exists_of : term -> term -> term
blanchet@45519
    31
  val unalias_type_enc : string -> string list
blanchet@52031
    32
  val term_of_atp :
blanchet@49914
    33
    Proof.context -> bool -> int Symtab.table -> typ option ->
blanchet@53586
    34
    (string, string) atp_term -> term
blanchet@52031
    35
  val prop_of_atp :
blanchet@49914
    36
    Proof.context -> bool -> int Symtab.table ->
blanchet@53586
    37
    (string, string, (string, string) atp_term, string) atp_formula -> term
blanchet@54499
    38
blanchet@54500
    39
  val used_facts_in_atp_proof :
blanchet@54500
    40
    Proof.context -> (string * stature) list vector -> string atp_proof ->
blanchet@54500
    41
    (string * stature) list
blanchet@54500
    42
  val used_facts_in_unsound_atp_proof :
blanchet@54500
    43
    Proof.context -> (string * stature) list vector -> 'a atp_proof ->
blanchet@54500
    44
    string list option
blanchet@54500
    45
  val lam_trans_of_atp_proof : string atp_proof -> string -> string
blanchet@54500
    46
  val is_typed_helper_used_in_atp_proof : string atp_proof -> bool
blanchet@54505
    47
  val termify_atp_proof : Proof.context -> string Symtab.table -> (string * term) list ->
blanchet@54499
    48
    int Symtab.table -> string atp_proof -> (term, string) atp_step list
blanchet@54505
    49
  val factify_atp_proof : (string * 'a) list vector -> term list -> term ->
blanchet@54505
    50
    (term, string) atp_step list -> (term, string) atp_step list
paulson@24425
    51
end;
paulson@21978
    52
blanchet@46320
    53
structure ATP_Proof_Reconstruct : ATP_PROOF_RECONSTRUCT =
paulson@21978
    54
struct
paulson@21978
    55
blanchet@43085
    56
open ATP_Util
blanchet@38028
    57
open ATP_Problem
blanchet@39452
    58
open ATP_Proof
blanchet@46320
    59
open ATP_Problem_Generate
paulson@21978
    60
blanchet@45519
    61
val metisN = "metis"
blanchet@45519
    62
blanchet@45519
    63
val full_typesN = "full_types"
blanchet@45519
    64
val partial_typesN = "partial_types"
blanchet@45519
    65
val no_typesN = "no_types"
blanchet@45519
    66
blanchet@45519
    67
val really_full_type_enc = "mono_tags"
blanchet@45519
    68
val full_type_enc = "poly_guards_query"
blanchet@45519
    69
val partial_type_enc = "poly_args"
blanchet@45519
    70
val no_type_enc = "erased"
blanchet@45519
    71
blanchet@45519
    72
val full_type_encs = [full_type_enc, really_full_type_enc]
blanchet@45519
    73
val partial_type_encs = partial_type_enc :: full_type_encs
blanchet@45519
    74
blanchet@45519
    75
val type_enc_aliases =
blanchet@45519
    76
  [(full_typesN, full_type_encs),
blanchet@45519
    77
   (partial_typesN, partial_type_encs),
blanchet@45519
    78
   (no_typesN, [no_type_enc])]
blanchet@45519
    79
blanchet@45519
    80
fun unalias_type_enc s =
blanchet@45519
    81
  AList.lookup (op =) type_enc_aliases s |> the_default [s]
blanchet@45519
    82
blanchet@54500
    83
val default_metis_lam_trans = combsN
blanchet@45519
    84
blanchet@45519
    85
fun metis_call type_enc lam_trans =
blanchet@45519
    86
  let
blanchet@45519
    87
    val type_enc =
blanchet@45519
    88
      case AList.find (fn (enc, encs) => enc = hd encs) type_enc_aliases
blanchet@45519
    89
                      type_enc of
blanchet@45519
    90
        [alias] => alias
blanchet@45519
    91
      | _ => type_enc
blanchet@45522
    92
    val opts = [] |> type_enc <> partial_typesN ? cons type_enc
blanchet@54500
    93
                  |> lam_trans <> default_metis_lam_trans ? cons lam_trans
blanchet@45520
    94
  in metisN ^ (if null opts then "" else " (" ^ commas opts ^ ")") end
blanchet@45519
    95
blanchet@50670
    96
fun term_name' (Var ((s, _), _)) = perhaps (try Name.dest_skolem) s
blanchet@52034
    97
  | term_name' _ = ""
blanchet@50670
    98
blanchet@50670
    99
fun lambda' v = Term.lambda_name (term_name' v, v)
blanchet@50670
   100
blanchet@50670
   101
fun forall_of v t = HOLogic.all_const (fastype_of v) $ lambda' v t
blanchet@50670
   102
fun exists_of v t = HOLogic.exists_const (fastype_of v) $ lambda' v t
blanchet@39425
   103
blanchet@43135
   104
fun make_tfree ctxt w =
blanchet@43135
   105
  let val ww = "'" ^ w in
blanchet@43135
   106
    TFree (ww, the_default HOLogic.typeS (Variable.def_sort ctxt (ww, ~1)))
blanchet@43135
   107
  end
blanchet@43135
   108
blanchet@53586
   109
exception ATP_TERM of (string, string) atp_term list
blanchet@53586
   110
exception ATP_FORMULA of
blanchet@53586
   111
    (string, string, (string, string) atp_term, string) atp_formula list
blanchet@37991
   112
exception SAME of unit
paulson@21978
   113
blanchet@36909
   114
(* Type variables are given the basic sort "HOL.type". Some will later be
blanchet@37991
   115
   constrained by information from type literals, or by type inference. *)
blanchet@52031
   116
fun typ_of_atp ctxt (u as ATerm ((a, _), us)) =
blanchet@52031
   117
  let val Ts = map (typ_of_atp ctxt) us in
blanchet@45511
   118
    case unprefix_and_unascii type_const_prefix a of
blanchet@37991
   119
      SOME b => Type (invert_const b, Ts)
blanchet@37991
   120
    | NONE =>
blanchet@37991
   121
      if not (null us) then
blanchet@53586
   122
        raise ATP_TERM [u]  (* only "tconst"s have type arguments *)
blanchet@45511
   123
      else case unprefix_and_unascii tfree_prefix a of
blanchet@43135
   124
        SOME b => make_tfree ctxt b
blanchet@36486
   125
      | NONE =>
blanchet@43302
   126
        (* Could be an Isabelle variable or a variable from the ATP, say "X1"
blanchet@43302
   127
           or "_5018". Sometimes variables from the ATP are indistinguishable
blanchet@43302
   128
           from Isabelle variables, which forces us to use a type parameter in
blanchet@43302
   129
           all cases. *)
blanchet@45511
   130
        (a |> perhaps (unprefix_and_unascii tvar_prefix), HOLogic.typeS)
blanchet@43302
   131
        |> Type_Infer.param 0
blanchet@37991
   132
  end
paulson@21978
   133
blanchet@38014
   134
(* Type class literal applied to a type. Returns triple of polarity, class,
blanchet@38014
   135
   type. *)
blanchet@52031
   136
fun type_constraint_of_term ctxt (u as ATerm ((a, _), us)) =
blanchet@52031
   137
  case (unprefix_and_unascii class_prefix a, map (typ_of_atp ctxt) us) of
blanchet@42606
   138
    (SOME b, [T]) => (b, T)
blanchet@53586
   139
  | _ => raise ATP_TERM [u]
blanchet@38014
   140
blanchet@43907
   141
(* Accumulate type constraints in a formula: negative type literals. *)
blanchet@38014
   142
fun add_var (key, z)  = Vartab.map_default (key, []) (cons z)
blanchet@42606
   143
fun add_type_constraint false (cl, TFree (a ,_)) = add_var ((a, ~1), cl)
blanchet@42606
   144
  | add_type_constraint false (cl, TVar (ix, _)) = add_var (ix, cl)
blanchet@42606
   145
  | add_type_constraint _ _ = I
blanchet@38014
   146
blanchet@50676
   147
fun repair_var_name s =
blanchet@36486
   148
  let
blanchet@36486
   149
    fun subscript_name s n = s ^ nat_subscript n
blanchet@50704
   150
    val s = s |> String.map Char.toLower
blanchet@36486
   151
  in
blanchet@36486
   152
    case space_explode "_" s of
blanchet@36486
   153
      [_] => (case take_suffix Char.isDigit (String.explode s) of
blanchet@36486
   154
                (cs1 as _ :: _, cs2 as _ :: _) =>
blanchet@36486
   155
                subscript_name (String.implode cs1)
blanchet@36486
   156
                               (the (Int.fromString (String.implode cs2)))
blanchet@36486
   157
              | (_, _) => s)
blanchet@36486
   158
    | [s1, s2] => (case Int.fromString s2 of
blanchet@36486
   159
                     SOME n => subscript_name s1 n
blanchet@36486
   160
                   | NONE => s)
blanchet@36486
   161
    | _ => s
blanchet@36486
   162
  end
blanchet@43182
   163
blanchet@43907
   164
(* The number of type arguments of a constant, zero if it's monomorphic. For
blanchet@43907
   165
   (instances of) Skolem pseudoconstants, this information is encoded in the
blanchet@43907
   166
   constant name. *)
blanchet@52125
   167
fun robust_const_num_type_args thy s =
blanchet@43907
   168
  if String.isPrefix skolem_const_prefix s then
wenzelm@46711
   169
    s |> Long_Name.explode |> List.last |> Int.fromString |> the
blanchet@45554
   170
  else if String.isPrefix lam_lifted_prefix s then
blanchet@45554
   171
    if String.isPrefix lam_lifted_poly_prefix s then 2 else 0
blanchet@43907
   172
  else
blanchet@43907
   173
    (s, Sign.the_const_type thy s) |> Sign.const_typargs thy |> length
blanchet@43907
   174
blanchet@43182
   175
fun slack_fastype_of t = fastype_of t handle TERM _ => HOLogic.typeT
blanchet@43182
   176
blanchet@51192
   177
(* Cope with "tt(X) = X" atoms, where "X" is existentially quantified. *)
blanchet@51192
   178
fun loose_aconv (Free (s, _), Free (s', _)) = s = s'
blanchet@51192
   179
  | loose_aconv (t, t') = t aconv t'
blanchet@51192
   180
blanchet@50703
   181
val vampire_skolem_prefix = "sK"
blanchet@50703
   182
blanchet@36909
   183
(* First-order translation. No types are known for variables. "HOLogic.typeT"
blanchet@38014
   184
   should allow them to be inferred. *)
blanchet@52031
   185
fun term_of_atp ctxt textual sym_tab =
blanchet@36909
   186
  let
blanchet@43135
   187
    val thy = Proof_Context.theory_of ctxt
blanchet@43212
   188
    (* For Metis, we use 1 rather than 0 because variable references in clauses
blanchet@43212
   189
       may otherwise conflict with variable constraints in the goal. At least,
blanchet@43212
   190
       type inference often fails otherwise. See also "axiom_inference" in
blanchet@43212
   191
       "Metis_Reconstruct". *)
blanchet@43094
   192
    val var_index = if textual then 0 else 1
blanchet@43131
   193
    fun do_term extra_ts opt_T u =
blanchet@36909
   194
      case u of
blanchet@48132
   195
        ATerm ((s, _), us) =>
smolkas@51878
   196
        if s = ""
blanchet@53586
   197
          then error "Isar proof reconstruction failed because the ATP proof \
blanchet@53586
   198
                     \contains unparsable material."
smolkas@51878
   199
        else if String.isPrefix native_type_prefix s then
blanchet@42962
   200
          @{const True} (* ignore TPTP type information *)
blanchet@44773
   201
        else if s = tptp_equal then
blanchet@43093
   202
          let val ts = map (do_term [] NONE) us in
blanchet@43094
   203
            if textual andalso length ts = 2 andalso
blanchet@51192
   204
               loose_aconv (hd ts, List.last ts) then
blanchet@39106
   205
              @{const True}
blanchet@39106
   206
            else
blanchet@39106
   207
              list_comb (Const (@{const_name HOL.eq}, HOLogic.typeT), ts)
blanchet@39106
   208
          end
blanchet@45511
   209
        else case unprefix_and_unascii const_prefix s of
blanchet@44773
   210
          SOME s' =>
blanchet@42761
   211
          let
blanchet@44773
   212
            val ((s', s''), mangled_us) =
blanchet@44773
   213
              s' |> unmangled_const |>> `invert_const
blanchet@42761
   214
          in
blanchet@42755
   215
            if s' = type_tag_name then
blanchet@42589
   216
              case mangled_us @ us of
blanchet@42589
   217
                [typ_u, term_u] =>
blanchet@52031
   218
                do_term extra_ts (SOME (typ_of_atp ctxt typ_u)) term_u
blanchet@53586
   219
              | _ => raise ATP_TERM us
blanchet@42966
   220
            else if s' = predicator_name then
blanchet@43093
   221
              do_term [] (SOME @{typ bool}) (hd us)
blanchet@42966
   222
            else if s' = app_op_name then
blanchet@43131
   223
              let val extra_t = do_term [] NONE (List.last us) in
blanchet@43131
   224
                do_term (extra_t :: extra_ts)
blanchet@43131
   225
                        (case opt_T of
blanchet@43182
   226
                           SOME T => SOME (slack_fastype_of extra_t --> T)
blanchet@43131
   227
                         | NONE => NONE)
blanchet@43131
   228
                        (nth us (length us - 2))
blanchet@43131
   229
              end
blanchet@44396
   230
            else if s' = type_guard_name then
blanchet@42551
   231
              @{const True} (* ignore type predicates *)
blanchet@42549
   232
            else
blanchet@42549
   233
              let
blanchet@44773
   234
                val new_skolem = String.isPrefix new_skolem_const_prefix s''
blanchet@42755
   235
                val num_ty_args =
blanchet@42755
   236
                  length us - the_default 0 (Symtab.lookup sym_tab s)
blanchet@42549
   237
                val (type_us, term_us) =
blanchet@42549
   238
                  chop num_ty_args us |>> append mangled_us
blanchet@43093
   239
                val term_ts = map (do_term [] NONE) term_us
blanchet@42549
   240
                val T =
blanchet@43183
   241
                  (if not (null type_us) andalso
blanchet@52125
   242
                      robust_const_num_type_args thy s' = length type_us then
blanchet@52031
   243
                     let val Ts = type_us |> map (typ_of_atp ctxt) in
blanchet@43191
   244
                       if new_skolem then
blanchet@43191
   245
                         SOME (Type_Infer.paramify_vars (tl Ts ---> hd Ts))
blanchet@43200
   246
                       else if textual then
blanchet@43200
   247
                         try (Sign.const_instance thy) (s', Ts)
blanchet@43191
   248
                       else
blanchet@43200
   249
                         NONE
blanchet@43191
   250
                     end
blanchet@43183
   251
                   else
blanchet@43183
   252
                     NONE)
blanchet@43183
   253
                  |> (fn SOME T => T
blanchet@43183
   254
                       | NONE => map slack_fastype_of term_ts --->
blanchet@43183
   255
                                 (case opt_T of
blanchet@43183
   256
                                    SOME T => T
blanchet@43183
   257
                                  | NONE => HOLogic.typeT))
blanchet@43191
   258
                val t =
blanchet@43191
   259
                  if new_skolem then
blanchet@52031
   260
                    Var ((new_skolem_var_name_of_const s'', var_index), T)
blanchet@43191
   261
                  else
blanchet@43191
   262
                    Const (unproxify_const s', T)
blanchet@43191
   263
              in list_comb (t, term_ts @ extra_ts) end
blanchet@42549
   264
          end
blanchet@36909
   265
        | NONE => (* a free or schematic variable *)
blanchet@36909
   266
          let
blanchet@50693
   267
            (* This assumes that distinct names are mapped to distinct names by
blanchet@50693
   268
               "Variable.variant_frees". This does not hold in general but
blanchet@50693
   269
               should hold for ATP-generated Skolem function names, since these
blanchet@50693
   270
               end with a digit and "variant_frees" appends letters. *)
blanchet@50676
   271
            fun fresh_up s =
blanchet@50676
   272
              [(s, ())] |> Variable.variant_frees ctxt [] |> hd |> fst
blanchet@50703
   273
            val term_ts =
blanchet@50703
   274
              map (do_term [] NONE) us
blanchet@50703
   275
              (* Vampire (2.6) passes arguments to Skolem functions in reverse
blanchet@50703
   276
                 order *)
blanchet@50703
   277
              |> String.isPrefix vampire_skolem_prefix s ? rev
blanchet@45042
   278
            val ts = term_ts @ extra_ts
blanchet@45042
   279
            val T =
blanchet@45042
   280
              case opt_T of
blanchet@45042
   281
                SOME T => map slack_fastype_of term_ts ---> T
blanchet@45042
   282
              | NONE => map slack_fastype_of ts ---> HOLogic.typeT
blanchet@36909
   283
            val t =
blanchet@45511
   284
              case unprefix_and_unascii fixed_var_prefix s of
blanchet@45511
   285
                SOME s => Free (s, T)
blanchet@36909
   286
              | NONE =>
blanchet@45511
   287
                case unprefix_and_unascii schematic_var_prefix s of
blanchet@44773
   288
                  SOME s => Var ((s, var_index), T)
blanchet@36909
   289
                | NONE =>
blanchet@50676
   290
                  if textual andalso not (is_tptp_variable s) then
blanchet@50676
   291
                    Free (s |> textual ? (repair_var_name #> fresh_up), T)
blanchet@50676
   292
                  else
blanchet@50676
   293
                    Var ((s |> textual ? repair_var_name, var_index), T)
blanchet@36909
   294
          in list_comb (t, ts) end
blanchet@43093
   295
  in do_term [] end
paulson@21978
   296
blanchet@52031
   297
fun term_of_atom ctxt textual sym_tab pos (u as ATerm ((s, _), _)) =
blanchet@38014
   298
  if String.isPrefix class_prefix s then
blanchet@52031
   299
    add_type_constraint pos (type_constraint_of_term ctxt u)
blanchet@38014
   300
    #> pair @{const True}
blanchet@38014
   301
  else
blanchet@52031
   302
    pair (term_of_atp ctxt textual sym_tab (SOME @{typ bool}) u)
blanchet@36402
   303
blanchet@37991
   304
(* Update schematic type variables with detected sort constraints. It's not
blanchet@42563
   305
   totally clear whether this code is necessary. *)
blanchet@38014
   306
fun repair_tvar_sorts (t, tvar_tab) =
blanchet@36909
   307
  let
blanchet@37991
   308
    fun do_type (Type (a, Ts)) = Type (a, map do_type Ts)
blanchet@37991
   309
      | do_type (TVar (xi, s)) =
blanchet@37991
   310
        TVar (xi, the_default s (Vartab.lookup tvar_tab xi))
blanchet@37991
   311
      | do_type (TFree z) = TFree z
blanchet@37991
   312
    fun do_term (Const (a, T)) = Const (a, do_type T)
blanchet@37991
   313
      | do_term (Free (a, T)) = Free (a, do_type T)
blanchet@37991
   314
      | do_term (Var (xi, T)) = Var (xi, do_type T)
blanchet@37991
   315
      | do_term (t as Bound _) = t
blanchet@37991
   316
      | do_term (Abs (a, T, t)) = Abs (a, do_type T, do_term t)
blanchet@37991
   317
      | do_term (t1 $ t2) = do_term t1 $ do_term t2
blanchet@37991
   318
  in t |> not (Vartab.is_empty tvar_tab) ? do_term end
blanchet@37991
   319
blanchet@39425
   320
fun quantify_over_var quant_of var_s t =
blanchet@39425
   321
  let
blanchet@39425
   322
    val vars = [] |> Term.add_vars t |> filter (fn ((s, _), _) => s = var_s)
blanchet@52758
   323
    val normTs = vars |> AList.group (op =) |> map (apsnd hd)
blanchet@52758
   324
    fun norm_var_types (Var (x, T)) =
blanchet@52758
   325
        Var (x, case AList.lookup (op =) normTs x of
blanchet@52758
   326
                  NONE => T
blanchet@52758
   327
                | SOME T' => T')
blanchet@52758
   328
      | norm_var_types t = t
blanchet@52758
   329
  in t |> map_aterms norm_var_types |> fold_rev quant_of (map Var normTs) end
blanchet@37991
   330
blanchet@38085
   331
(* Interpret an ATP formula as a HOL term, extracting sort constraints as they
blanchet@38085
   332
   appear in the formula. *)
blanchet@52031
   333
fun prop_of_atp ctxt textual sym_tab phi =
blanchet@38014
   334
  let
blanchet@38014
   335
    fun do_formula pos phi =
blanchet@37991
   336
      case phi of
blanchet@38014
   337
        AQuant (_, [], phi) => do_formula pos phi
blanchet@42526
   338
      | AQuant (q, (s, _) :: xs, phi') =>
blanchet@38014
   339
        do_formula pos (AQuant (q, xs, phi'))
blanchet@42526
   340
        (* FIXME: TFF *)
blanchet@49914
   341
        #>> quantify_over_var
blanchet@49914
   342
              (case q of AForall => forall_of | AExists => exists_of)
blanchet@50676
   343
              (s |> textual ? repair_var_name)
blanchet@38014
   344
      | AConn (ANot, [phi']) => do_formula (not pos) phi' #>> s_not
blanchet@37991
   345
      | AConn (c, [phi1, phi2]) =>
blanchet@38014
   346
        do_formula (pos |> c = AImplies ? not) phi1
blanchet@38014
   347
        ##>> do_formula pos phi2
blanchet@38014
   348
        #>> (case c of
blanchet@38014
   349
               AAnd => s_conj
blanchet@38014
   350
             | AOr => s_disj
blanchet@38014
   351
             | AImplies => s_imp
blanchet@38038
   352
             | AIff => s_iff
blanchet@43163
   353
             | ANot => raise Fail "impossible connective")
blanchet@52031
   354
      | AAtom tm => term_of_atom ctxt textual sym_tab pos tm
blanchet@53586
   355
      | _ => raise ATP_FORMULA [phi]
blanchet@38014
   356
  in repair_tvar_sorts (do_formula true phi Vartab.empty) end
blanchet@37991
   357
blanchet@54500
   358
fun find_first_in_list_vector vec key =
blanchet@54500
   359
  Vector.foldl (fn (ps, NONE) => AList.lookup (op =) ps key
blanchet@54500
   360
                 | (_, value) => value) NONE vec
blanchet@54500
   361
blanchet@54500
   362
val unprefix_fact_number = space_implode "_" o tl o space_explode "_"
blanchet@54500
   363
blanchet@54500
   364
fun resolve_one_named_fact fact_names s =
blanchet@54500
   365
  case try (unprefix fact_prefix) s of
blanchet@54500
   366
    SOME s' =>
blanchet@54500
   367
    let val s' = s' |> unprefix_fact_number |> unascii_of in
blanchet@54500
   368
      s' |> find_first_in_list_vector fact_names |> Option.map (pair s')
blanchet@54500
   369
    end
blanchet@54500
   370
  | NONE => NONE
blanchet@54500
   371
fun resolve_fact fact_names = map_filter (resolve_one_named_fact fact_names)
blanchet@54500
   372
fun is_fact fact_names = not o null o resolve_fact fact_names
blanchet@54500
   373
blanchet@54500
   374
fun resolve_one_named_conjecture s =
blanchet@54500
   375
  case try (unprefix conjecture_prefix) s of
blanchet@54500
   376
    SOME s' => Int.fromString s'
blanchet@54500
   377
  | NONE => NONE
blanchet@54500
   378
blanchet@54500
   379
val resolve_conjecture = map_filter resolve_one_named_conjecture
blanchet@54500
   380
val is_conjecture = not o null o resolve_conjecture
blanchet@54500
   381
blanchet@54500
   382
fun is_axiom_used_in_proof pred =
blanchet@54500
   383
  exists (fn ((_, ss), _, _, _, []) => exists pred ss | _ => false)
blanchet@54500
   384
blanchet@54500
   385
fun add_non_rec_defs fact_names accum =
blanchet@54500
   386
  Vector.foldl (fn (facts, facts') =>
blanchet@54500
   387
      union (op =) (filter (fn (_, (_, status)) => status = Non_Rec_Def) facts)
blanchet@54500
   388
            facts')
blanchet@54500
   389
    accum fact_names
blanchet@54500
   390
blanchet@54500
   391
val isa_ext = Thm.get_name_hint @{thm ext}
blanchet@54500
   392
val isa_short_ext = Long_Name.base_name isa_ext
blanchet@54500
   393
blanchet@54500
   394
fun ext_name ctxt =
blanchet@54500
   395
  if Thm.eq_thm_prop (@{thm ext},
blanchet@54500
   396
       singleton (Attrib.eval_thms ctxt) (Facts.named isa_short_ext, [])) then
blanchet@54500
   397
    isa_short_ext
blanchet@54500
   398
  else
blanchet@54500
   399
    isa_ext
blanchet@54500
   400
blanchet@54500
   401
val leo2_extcnf_equal_neg_rule = "extcnf_equal_neg"
blanchet@54500
   402
val leo2_unfold_def_rule = "unfold_def"
blanchet@54500
   403
blanchet@54500
   404
fun add_fact ctxt fact_names ((_, ss), _, _, rule, deps) =
blanchet@54500
   405
  (if rule = leo2_extcnf_equal_neg_rule then
blanchet@54500
   406
     insert (op =) (ext_name ctxt, (Global, General))
blanchet@54500
   407
   else if rule = leo2_unfold_def_rule then
blanchet@54500
   408
     (* LEO 1.3.3 does not record definitions properly, leading to missing
blanchet@54500
   409
        dependencies in the TSTP proof. Remove the next line once this is
blanchet@54500
   410
        fixed. *)
blanchet@54500
   411
     add_non_rec_defs fact_names
blanchet@54500
   412
   else if rule = agsyhol_coreN orelse rule = satallax_coreN then
blanchet@54500
   413
     (fn [] =>
blanchet@54500
   414
         (* agsyHOL and Satallax don't include definitions in their
blanchet@54500
   415
            unsatisfiable cores, so we assume the worst and include them all
blanchet@54500
   416
            here. *)
blanchet@54500
   417
         [(ext_name ctxt, (Global, General))] |> add_non_rec_defs fact_names
blanchet@54500
   418
       | facts => facts)
blanchet@54500
   419
   else
blanchet@54500
   420
     I)
blanchet@54500
   421
  #> (if null deps then union (op =) (resolve_fact fact_names ss) else I)
blanchet@54500
   422
blanchet@54500
   423
fun used_facts_in_atp_proof ctxt fact_names atp_proof =
blanchet@54500
   424
  if null atp_proof then Vector.foldl (uncurry (union (op =))) [] fact_names
blanchet@54500
   425
  else fold (add_fact ctxt fact_names) atp_proof []
blanchet@54500
   426
blanchet@54500
   427
fun used_facts_in_unsound_atp_proof _ _ [] = NONE
blanchet@54500
   428
  | used_facts_in_unsound_atp_proof ctxt fact_names atp_proof =
blanchet@54500
   429
    let val used_facts = used_facts_in_atp_proof ctxt fact_names atp_proof in
blanchet@54500
   430
      if forall (fn (_, (sc, _)) => sc = Global) used_facts andalso
blanchet@54500
   431
         not (is_axiom_used_in_proof (is_conjecture o single) atp_proof) then
blanchet@54500
   432
        SOME (map fst used_facts)
blanchet@54500
   433
      else
blanchet@54500
   434
        NONE
blanchet@54500
   435
    end
blanchet@54500
   436
blanchet@54500
   437
val ascii_of_lam_fact_prefix = ascii_of lam_fact_prefix
blanchet@54500
   438
blanchet@54500
   439
(* overapproximation (good enough) *)
blanchet@54500
   440
fun is_lam_lifted s =
blanchet@54500
   441
  String.isPrefix fact_prefix s andalso
blanchet@54500
   442
  String.isSubstring ascii_of_lam_fact_prefix s
blanchet@54500
   443
blanchet@54500
   444
val is_combinator_def = String.isPrefix (helper_prefix ^ combinator_prefix)
blanchet@54500
   445
blanchet@54500
   446
fun lam_trans_of_atp_proof atp_proof default =
blanchet@54500
   447
  case (is_axiom_used_in_proof is_combinator_def atp_proof,
blanchet@54500
   448
        is_axiom_used_in_proof is_lam_lifted atp_proof) of
blanchet@54500
   449
    (false, false) => default
blanchet@54500
   450
  | (false, true) => liftingN
blanchet@54500
   451
(*  | (true, true) => combs_and_liftingN -- not supported by "metis" *)
blanchet@54500
   452
  | (true, _) => combsN
blanchet@54500
   453
blanchet@54500
   454
val is_typed_helper_name =
blanchet@54500
   455
  String.isPrefix helper_prefix andf String.isSuffix typed_helper_suffix
blanchet@54500
   456
blanchet@54500
   457
fun is_typed_helper_used_in_atp_proof atp_proof =
blanchet@54500
   458
  is_axiom_used_in_proof is_typed_helper_name atp_proof
blanchet@54500
   459
blanchet@54499
   460
fun repair_name "$true" = "c_True"
blanchet@54499
   461
  | repair_name "$false" = "c_False"
blanchet@54499
   462
  | repair_name "$$e" = tptp_equal (* seen in Vampire proofs *)
blanchet@54499
   463
  | repair_name s =
blanchet@54499
   464
    if is_tptp_equal s orelse
blanchet@54499
   465
       (* seen in Vampire proofs *)
blanchet@54499
   466
       (String.isPrefix "sQ" s andalso String.isSuffix "_eqProxy" s) then
blanchet@54499
   467
      tptp_equal
blanchet@54499
   468
    else
blanchet@54499
   469
      s
blanchet@54499
   470
blanchet@54499
   471
fun infer_formula_types ctxt =
blanchet@54499
   472
  Type.constraint HOLogic.boolT
blanchet@54499
   473
  #> Syntax.check_term
blanchet@54499
   474
         (Proof_Context.set_mode Proof_Context.mode_schematic ctxt)
blanchet@54499
   475
blanchet@54499
   476
val combinator_table =
blanchet@54499
   477
  [(@{const_name Meson.COMBI}, @{thm Meson.COMBI_def [abs_def]}),
blanchet@54499
   478
   (@{const_name Meson.COMBK}, @{thm Meson.COMBK_def [abs_def]}),
blanchet@54499
   479
   (@{const_name Meson.COMBB}, @{thm Meson.COMBB_def [abs_def]}),
blanchet@54499
   480
   (@{const_name Meson.COMBC}, @{thm Meson.COMBC_def [abs_def]}),
blanchet@54499
   481
   (@{const_name Meson.COMBS}, @{thm Meson.COMBS_def [abs_def]})]
blanchet@54499
   482
blanchet@54499
   483
fun uncombine_term thy =
blanchet@54499
   484
  let
blanchet@54499
   485
    fun aux (t1 $ t2) = betapply (pairself aux (t1, t2))
blanchet@54499
   486
      | aux (Abs (s, T, t')) = Abs (s, T, aux t')
blanchet@54499
   487
      | aux (t as Const (x as (s, _))) =
blanchet@54499
   488
        (case AList.lookup (op =) combinator_table s of
blanchet@54499
   489
           SOME thm => thm |> prop_of |> specialize_type thy x
blanchet@54499
   490
                           |> Logic.dest_equals |> snd
blanchet@54499
   491
         | NONE => t)
blanchet@54499
   492
      | aux t = t
blanchet@54499
   493
  in aux end
blanchet@54499
   494
blanchet@54499
   495
fun unlift_term lifted =
blanchet@54499
   496
  map_aterms (fn t as Const (s, _) =>
blanchet@54499
   497
                 if String.isPrefix lam_lifted_prefix s then
blanchet@54499
   498
                   case AList.lookup (op =) lifted s of
blanchet@54499
   499
                     SOME t =>
blanchet@54499
   500
                     (* FIXME: do something about the types *)
blanchet@54499
   501
                     unlift_term lifted t
blanchet@54499
   502
                   | NONE => t
blanchet@54499
   503
                 else
blanchet@54499
   504
                   t
blanchet@54499
   505
               | t => t)
blanchet@54499
   506
blanchet@54499
   507
fun decode_line ctxt lifted sym_tab (name, role, u, rule, deps) =
blanchet@54499
   508
  let
blanchet@54499
   509
    val thy = Proof_Context.theory_of ctxt
blanchet@54499
   510
    val t =
blanchet@54499
   511
      u |> prop_of_atp ctxt true sym_tab
blanchet@54499
   512
        |> uncombine_term thy
blanchet@54499
   513
        |> unlift_term lifted
blanchet@54499
   514
        |> infer_formula_types ctxt
blanchet@54499
   515
  in (name, role, t, rule, deps) end
blanchet@54499
   516
blanchet@54499
   517
val waldmeister_conjecture_num = "1.0.0.0"
blanchet@54499
   518
blanchet@54499
   519
fun repair_waldmeister_endgame arg =
blanchet@54499
   520
  let
blanchet@54499
   521
    fun do_tail (name, _, t, rule, deps) =
blanchet@54499
   522
      (name, Negated_Conjecture, s_not t, rule, deps)
blanchet@54499
   523
    fun do_body [] = []
blanchet@54499
   524
      | do_body ((line as ((num, _), _, _, _, _)) :: lines) =
blanchet@54499
   525
        if num = waldmeister_conjecture_num then map do_tail (line :: lines)
blanchet@54499
   526
        else line :: do_body lines
blanchet@54499
   527
  in do_body arg end
blanchet@54499
   528
blanchet@54499
   529
fun termify_atp_proof ctxt pool lifted sym_tab =
blanchet@54499
   530
  clean_up_atp_proof_dependencies
blanchet@54499
   531
  #> nasty_atp_proof pool
blanchet@54499
   532
  #> map_term_names_in_atp_proof repair_name
blanchet@54499
   533
  #> map (decode_line ctxt lifted sym_tab)
blanchet@54499
   534
  #> repair_waldmeister_endgame
blanchet@54499
   535
blanchet@54505
   536
fun factify_atp_proof fact_names hyp_ts concl_t atp_proof =
blanchet@54505
   537
  let
blanchet@54505
   538
    fun factify_step ((num, ss), role, t, rule, deps) =
blanchet@54505
   539
      let
blanchet@54505
   540
        val (ss', role', t') =
blanchet@54505
   541
          (case resolve_conjecture ss of
blanchet@54505
   542
            [j] =>
blanchet@54505
   543
            if j = length hyp_ts then ([], Conjecture, concl_t) else ([], Hypothesis, nth hyp_ts j)
blanchet@54505
   544
           | _ =>
blanchet@54505
   545
             (case resolve_fact fact_names ss of
blanchet@54505
   546
               [] => (ss, Plain, t)
blanchet@54505
   547
             | facts => (map fst facts, Axiom, t)))
blanchet@54505
   548
      in
blanchet@54505
   549
        ((num, ss'), role', t', rule, deps)
blanchet@54505
   550
      end
blanchet@54505
   551
blanchet@54505
   552
    val atp_proof = map factify_step atp_proof
blanchet@54505
   553
    val names = map #1 atp_proof
blanchet@54505
   554
blanchet@54505
   555
    fun repair_dep (num, ss) = (num, the_default ss (AList.lookup (op =) names num))
blanchet@54505
   556
    fun repair_deps (name, role, t, rule, deps) = (name, role, t, rule, map repair_dep deps)
blanchet@54505
   557
blanchet@54505
   558
  in map repair_deps atp_proof end
blanchet@54505
   559
immler@31038
   560
end;