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