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