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