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