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