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