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