src/HOL/Tools/Metis/metis_translate.ML
author blanchet
Tue May 31 16:38:36 2011 +0200 (2011-05-31)
changeset 43092 93ec303e1917
parent 43091 b0b30df60056
child 43093 40e50afbc203
permissions -rw-r--r--
more work on new metis that exploits the powerful new type encodings
blanchet@39958
     1
(*  Title:      HOL/Tools/Metis/metis_translate.ML
blanchet@38027
     2
    Author:     Jia Meng, Cambridge University Computer Laboratory and NICTA
blanchet@39497
     3
    Author:     Kong W. Susanto, Cambridge University Computer Laboratory
blanchet@39497
     4
    Author:     Lawrence C. Paulson, Cambridge University Computer Laboratory
blanchet@36393
     5
    Author:     Jasmin Blanchette, TU Muenchen
paulson@15347
     6
blanchet@39494
     7
Translation of HOL to FOL for Metis.
paulson@15347
     8
*)
paulson@15347
     9
blanchet@39494
    10
signature METIS_TRANSLATE =
wenzelm@24310
    11
sig
blanchet@43085
    12
  type type_literal = ATP_Translate.type_literal
blanchet@37577
    13
blanchet@43087
    14
  datatype mode = FO | HO | FT | New
blanchet@43085
    15
blanchet@40157
    16
  type metis_problem =
blanchet@39497
    17
    {axioms: (Metis_Thm.thm * thm) list,
blanchet@39497
    18
     tfrees: type_literal list,
blanchet@39886
    19
     old_skolems: (string * term) list}
blanchet@39497
    20
blanchet@42098
    21
  val metis_generated_var_prefix : string
blanchet@39896
    22
  val new_skolem_const_prefix : string
blanchet@37618
    23
  val num_type_args: theory -> string -> int
blanchet@40259
    24
  val new_skolem_var_name_from_const : string -> string
blanchet@39886
    25
  val reveal_old_skolem_terms : (string * term) list -> term -> term
blanchet@39497
    26
  val string_of_mode : mode -> string
blanchet@40157
    27
  val prepare_metis_problem :
blanchet@43092
    28
    Proof.context -> mode -> thm list -> thm list -> mode * metis_problem
wenzelm@24310
    29
end
paulson@15347
    30
blanchet@39494
    31
structure Metis_Translate : METIS_TRANSLATE =
paulson@15347
    32
struct
paulson@15347
    33
blanchet@43092
    34
open ATP_Problem
blanchet@43085
    35
open ATP_Translate
paulson@15347
    36
blanchet@43085
    37
val metis_generated_var_prefix = "_"
quigley@17150
    38
blanchet@37618
    39
(* The number of type arguments of a constant, zero if it's monomorphic. For
blanchet@37618
    40
   (instances of) Skolem pseudoconstants, this information is encoded in the
blanchet@37618
    41
   constant name. *)
blanchet@37618
    42
fun num_type_args thy s =
blanchet@39896
    43
  if String.isPrefix skolem_const_prefix s then
blanchet@39499
    44
    s |> space_explode Long_Name.separator |> List.last |> Int.fromString |> the
blanchet@37618
    45
  else
blanchet@37618
    46
    (s, Sign.the_const_type thy s) |> Sign.const_typargs thy |> length
quigley@17150
    47
blanchet@40259
    48
fun new_skolem_var_name_from_const s =
blanchet@40145
    49
  let val ss = s |> space_explode Long_Name.separator in
blanchet@40259
    50
    nth ss (length ss - 2)
blanchet@40145
    51
  end
blanchet@39886
    52
blanchet@40145
    53
fun predicate_of thy ((@{const Not} $ P), pos) = predicate_of thy (P, not pos)
blanchet@40145
    54
  | predicate_of thy (t, pos) =
blanchet@40145
    55
    (combterm_from_term thy [] (Envir.eta_contract t), pos)
blanchet@37577
    56
blanchet@40145
    57
fun literals_of_term1 args thy (@{const Trueprop} $ P) =
blanchet@40145
    58
    literals_of_term1 args thy P
blanchet@40145
    59
  | literals_of_term1 args thy (@{const HOL.disj} $ P $ Q) =
blanchet@40145
    60
    literals_of_term1 (literals_of_term1 args thy P) thy Q
blanchet@40145
    61
  | literals_of_term1 (lits, ts) thy P =
blanchet@40145
    62
    let val ((pred, ts'), pol) = predicate_of thy (P, true) in
blanchet@43085
    63
      ((pol, pred) :: lits, union (op =) ts ts')
blanchet@37577
    64
    end
blanchet@37577
    65
val literals_of_term = literals_of_term1 ([], [])
blanchet@37577
    66
blanchet@39896
    67
fun old_skolem_const_name i j num_T_args =
blanchet@39896
    68
  old_skolem_const_prefix ^ Long_Name.separator ^
wenzelm@41491
    69
  (space_implode Long_Name.separator (map string_of_int [i, j, num_T_args]))
blanchet@37577
    70
blanchet@39886
    71
fun conceal_old_skolem_terms i old_skolems t =
blanchet@39953
    72
  if exists_Const (curry (op =) @{const_name Meson.skolem} o fst) t then
blanchet@37577
    73
    let
blanchet@39886
    74
      fun aux old_skolems
blanchet@39953
    75
             (t as (Const (@{const_name Meson.skolem}, Type (_, [_, T])) $ _)) =
blanchet@37577
    76
          let
blanchet@39886
    77
            val (old_skolems, s) =
blanchet@37577
    78
              if i = ~1 then
blanchet@39886
    79
                (old_skolems, @{const_name undefined})
blanchet@39886
    80
              else case AList.find (op aconv) old_skolems t of
blanchet@39886
    81
                s :: _ => (old_skolems, s)
blanchet@37577
    82
              | [] =>
blanchet@37577
    83
                let
blanchet@39896
    84
                  val s = old_skolem_const_name i (length old_skolems)
blanchet@39896
    85
                                                (length (Term.add_tvarsT T []))
blanchet@39886
    86
                in ((s, t) :: old_skolems, s) end
blanchet@39886
    87
          in (old_skolems, Const (s, T)) end
blanchet@39886
    88
        | aux old_skolems (t1 $ t2) =
blanchet@37577
    89
          let
blanchet@39886
    90
            val (old_skolems, t1) = aux old_skolems t1
blanchet@39886
    91
            val (old_skolems, t2) = aux old_skolems t2
blanchet@39886
    92
          in (old_skolems, t1 $ t2) end
blanchet@39886
    93
        | aux old_skolems (Abs (s, T, t')) =
blanchet@39886
    94
          let val (old_skolems, t') = aux old_skolems t' in
blanchet@39886
    95
            (old_skolems, Abs (s, T, t'))
blanchet@37577
    96
          end
blanchet@39886
    97
        | aux old_skolems t = (old_skolems, t)
blanchet@39886
    98
    in aux old_skolems t end
blanchet@37577
    99
  else
blanchet@39886
   100
    (old_skolems, t)
blanchet@37577
   101
blanchet@39886
   102
fun reveal_old_skolem_terms old_skolems =
blanchet@37632
   103
  map_aterms (fn t as Const (s, _) =>
blanchet@39896
   104
                 if String.isPrefix old_skolem_const_prefix s then
blanchet@39886
   105
                   AList.lookup (op =) old_skolems s |> the
blanchet@37632
   106
                   |> map_types Type_Infer.paramify_vars
blanchet@37632
   107
                 else
blanchet@37632
   108
                   t
blanchet@37632
   109
               | t => t)
blanchet@37632
   110
blanchet@37577
   111
blanchet@39497
   112
(* ------------------------------------------------------------------------- *)
blanchet@39497
   113
(* HOL to FOL  (Isabelle to Metis)                                           *)
blanchet@39497
   114
(* ------------------------------------------------------------------------- *)
blanchet@39497
   115
blanchet@43087
   116
(* first-order, higher-order, fully-typed, new *)
blanchet@43087
   117
datatype mode = FO | HO | FT | New
blanchet@39497
   118
blanchet@39497
   119
fun string_of_mode FO = "FO"
blanchet@39497
   120
  | string_of_mode HO = "HO"
blanchet@39497
   121
  | string_of_mode FT = "FT"
blanchet@43087
   122
  | string_of_mode New = "New"
blanchet@39497
   123
blanchet@42745
   124
fun fn_isa_to_met_sublevel "equal" = "c_fequal"
blanchet@41139
   125
  | fn_isa_to_met_sublevel "c_False" = "c_fFalse"
blanchet@41139
   126
  | fn_isa_to_met_sublevel "c_True" = "c_fTrue"
blanchet@41139
   127
  | fn_isa_to_met_sublevel "c_Not" = "c_fNot"
blanchet@41139
   128
  | fn_isa_to_met_sublevel "c_conj" = "c_fconj"
blanchet@41139
   129
  | fn_isa_to_met_sublevel "c_disj" = "c_fdisj"
blanchet@41139
   130
  | fn_isa_to_met_sublevel "c_implies" = "c_fimplies"
blanchet@39497
   131
  | fn_isa_to_met_sublevel x = x
blanchet@42758
   132
blanchet@39497
   133
fun fn_isa_to_met_toplevel "equal" = "="
blanchet@39497
   134
  | fn_isa_to_met_toplevel x = x
blanchet@39497
   135
blanchet@39497
   136
fun metis_lit b c args = (b, (c, args));
blanchet@39497
   137
blanchet@42562
   138
fun metis_term_from_typ (Type (s, Ts)) =
blanchet@42562
   139
    Metis_Term.Fn (make_fixed_type_const s, map metis_term_from_typ Ts)
blanchet@42562
   140
  | metis_term_from_typ (TFree (s, _)) =
blanchet@42562
   141
    Metis_Term.Fn (make_fixed_type_var s, [])
blanchet@42562
   142
  | metis_term_from_typ (TVar (x, _)) =
blanchet@42562
   143
    Metis_Term.Var (make_schematic_type_var x)
blanchet@39497
   144
blanchet@39497
   145
(*These two functions insert type literals before the real literals. That is the
blanchet@39497
   146
  opposite order from TPTP linkup, but maybe OK.*)
blanchet@39497
   147
blanchet@39497
   148
fun hol_term_to_fol_FO tm =
blanchet@39497
   149
  case strip_combterm_comb tm of
blanchet@42562
   150
      (CombConst ((c, _), _, Ts), tms) =>
blanchet@42562
   151
        let val tyargs = map metis_term_from_typ Ts
blanchet@42562
   152
            val args = map hol_term_to_fol_FO tms
blanchet@39497
   153
        in Metis_Term.Fn (c, tyargs @ args) end
blanchet@39497
   154
    | (CombVar ((v, _), _), []) => Metis_Term.Var v
blanchet@39497
   155
    | _ => raise Fail "non-first-order combterm"
blanchet@39497
   156
blanchet@42562
   157
fun hol_term_to_fol_HO (CombConst ((a, _), _, Ts)) =
blanchet@42562
   158
      Metis_Term.Fn (fn_isa_to_met_sublevel a, map metis_term_from_typ Ts)
blanchet@39497
   159
  | hol_term_to_fol_HO (CombVar ((s, _), _)) = Metis_Term.Var s
blanchet@39497
   160
  | hol_term_to_fol_HO (CombApp (tm1, tm2)) =
blanchet@39497
   161
       Metis_Term.Fn (".", map hol_term_to_fol_HO [tm1, tm2]);
blanchet@39497
   162
blanchet@39497
   163
(*The fully-typed translation, to avoid type errors*)
blanchet@42562
   164
fun tag_with_type tm T =
blanchet@42562
   165
  Metis_Term.Fn (type_tag_name, [tm, metis_term_from_typ T])
blanchet@39497
   166
blanchet@41138
   167
fun hol_term_to_fol_FT (CombVar ((s, _), ty)) =
blanchet@41138
   168
    tag_with_type (Metis_Term.Var s) ty
blanchet@41138
   169
  | hol_term_to_fol_FT (CombConst ((a, _), ty, _)) =
blanchet@41138
   170
    tag_with_type (Metis_Term.Fn (fn_isa_to_met_sublevel a, [])) ty
blanchet@41138
   171
  | hol_term_to_fol_FT (tm as CombApp (tm1,tm2)) =
blanchet@41138
   172
    tag_with_type (Metis_Term.Fn (".", map hol_term_to_fol_FT [tm1, tm2]))
blanchet@41138
   173
                  (combtyp_of tm)
blanchet@39497
   174
blanchet@43085
   175
fun hol_literal_to_fol FO (pos, tm) =
blanchet@42562
   176
      let
blanchet@42562
   177
        val (CombConst((p, _), _, Ts), tms) = strip_combterm_comb tm
blanchet@42562
   178
        val tylits = if p = "equal" then [] else map metis_term_from_typ Ts
blanchet@42562
   179
        val lits = map hol_term_to_fol_FO tms
blanchet@39497
   180
      in metis_lit pos (fn_isa_to_met_toplevel p) (tylits @ lits) end
blanchet@43085
   181
  | hol_literal_to_fol HO (pos, tm) =
blanchet@39497
   182
     (case strip_combterm_comb tm of
blanchet@39497
   183
          (CombConst(("equal", _), _, _), tms) =>
blanchet@39497
   184
            metis_lit pos "=" (map hol_term_to_fol_HO tms)
blanchet@39497
   185
        | _ => metis_lit pos "{}" [hol_term_to_fol_HO tm])   (*hBOOL*)
blanchet@43085
   186
  | hol_literal_to_fol FT (pos, tm) =
blanchet@39497
   187
     (case strip_combterm_comb tm of
blanchet@39497
   188
          (CombConst(("equal", _), _, _), tms) =>
blanchet@39497
   189
            metis_lit pos "=" (map hol_term_to_fol_FT tms)
blanchet@39497
   190
        | _ => metis_lit pos "{}" [hol_term_to_fol_FT tm])   (*hBOOL*);
blanchet@39497
   191
blanchet@40145
   192
fun literals_of_hol_term thy mode t =
blanchet@40145
   193
  let val (lits, types_sorts) = literals_of_term thy t in
blanchet@40145
   194
    (map (hol_literal_to_fol mode) lits, types_sorts)
blanchet@40145
   195
  end
blanchet@39497
   196
blanchet@39497
   197
(*Sign should be "true" for conjecture type constraints, "false" for type lits in clauses.*)
blanchet@39497
   198
fun metis_of_type_literals pos (TyLitVar ((s, _), (s', _))) =
blanchet@39497
   199
    metis_lit pos s [Metis_Term.Var s']
blanchet@39497
   200
  | metis_of_type_literals pos (TyLitFree ((s, _), (s', _))) =
blanchet@39497
   201
    metis_lit pos s [Metis_Term.Fn (s',[])]
blanchet@39497
   202
blanchet@42352
   203
fun has_default_sort _ (TVar _) = false
blanchet@42352
   204
  | has_default_sort ctxt (TFree (x, s)) =
blanchet@42352
   205
    (s = the_default [] (Variable.def_sort ctxt (x, ~1)));
blanchet@39497
   206
blanchet@39497
   207
fun metis_of_tfree tf =
blanchet@39497
   208
  Metis_Thm.axiom (Metis_LiteralSet.singleton (metis_of_type_literals true tf));
blanchet@39497
   209
blanchet@43090
   210
fun hol_thm_to_fol is_conjecture ctxt mode j old_skolems th =
blanchet@39497
   211
  let
wenzelm@42361
   212
    val thy = Proof_Context.theory_of ctxt
blanchet@39886
   213
    val (old_skolems, (mlits, types_sorts)) =
blanchet@39888
   214
     th |> prop_of |> Logic.strip_imp_concl
blanchet@39888
   215
        |> conceal_old_skolem_terms j old_skolems
blanchet@40145
   216
        ||> (HOLogic.dest_Trueprop #> literals_of_hol_term thy mode)
blanchet@39497
   217
  in
blanchet@39497
   218
    if is_conjecture then
blanchet@39497
   219
      (Metis_Thm.axiom (Metis_LiteralSet.fromList mlits),
blanchet@43085
   220
       raw_type_literals_for_types types_sorts, old_skolems)
blanchet@39497
   221
    else
blanchet@39497
   222
      let
blanchet@42352
   223
        val tylits = types_sorts |> filter_out (has_default_sort ctxt)
blanchet@43085
   224
                                 |> raw_type_literals_for_types
blanchet@43090
   225
        val mtylits = map (metis_of_type_literals false) tylits
blanchet@39497
   226
      in
blanchet@39497
   227
        (Metis_Thm.axiom (Metis_LiteralSet.fromList(mtylits @ mlits)), [],
blanchet@39886
   228
         old_skolems)
blanchet@39497
   229
      end
blanchet@39497
   230
  end;
blanchet@39497
   231
blanchet@39497
   232
(* ------------------------------------------------------------------------- *)
blanchet@39497
   233
(* Logic maps manage the interface between HOL and first-order logic.        *)
blanchet@39497
   234
(* ------------------------------------------------------------------------- *)
blanchet@39497
   235
blanchet@40157
   236
type metis_problem =
blanchet@39497
   237
  {axioms: (Metis_Thm.thm * thm) list,
blanchet@39497
   238
   tfrees: type_literal list,
blanchet@39886
   239
   old_skolems: (string * term) list}
blanchet@39497
   240
blanchet@39497
   241
fun is_quasi_fol_clause thy =
blanchet@39886
   242
  Meson.is_fol_term thy o snd o conceal_old_skolem_terms ~1 [] o prop_of
blanchet@39497
   243
blanchet@39497
   244
(*Extract TFree constraints from context to include as conjecture clauses*)
blanchet@39497
   245
fun init_tfrees ctxt =
blanchet@39497
   246
  let fun add ((a,i),s) Ts = if i = ~1 then TFree(a,s) :: Ts else Ts in
blanchet@39497
   247
    Vartab.fold add (#2 (Variable.constraints_of ctxt)) []
blanchet@43085
   248
    |> raw_type_literals_for_types
blanchet@39497
   249
  end;
blanchet@39497
   250
blanchet@39497
   251
fun const_in_metis c (pred, tm_list) =
blanchet@39497
   252
  let
blanchet@39497
   253
    fun in_mterm (Metis_Term.Var _) = false
blanchet@41156
   254
      | in_mterm (Metis_Term.Fn (nm, tm_list)) =
blanchet@41156
   255
        c = nm orelse exists in_mterm tm_list
blanchet@41156
   256
  in c = pred orelse exists in_mterm tm_list end
blanchet@39497
   257
blanchet@39497
   258
(* ARITY CLAUSE *)
blanchet@39497
   259
fun m_arity_cls (TConsLit ((c, _), (t, _), args)) =
blanchet@39497
   260
    metis_lit true c [Metis_Term.Fn(t, map (Metis_Term.Var o fst) args)]
blanchet@39497
   261
  | m_arity_cls (TVarLit ((c, _), (s, _))) =
blanchet@39497
   262
    metis_lit false c [Metis_Term.Var s]
blanchet@39497
   263
(*TrueI is returned as the Isabelle counterpart because there isn't any.*)
blanchet@43086
   264
fun arity_cls ({prem_lits, concl_lits, ...} : arity_clause) =
blanchet@39497
   265
  (TrueI,
blanchet@42895
   266
   Metis_Thm.axiom (Metis_LiteralSet.fromList
blanchet@42895
   267
                        (map m_arity_cls (concl_lits :: prem_lits))));
blanchet@39497
   268
blanchet@39497
   269
(* CLASSREL CLAUSE *)
blanchet@39497
   270
fun m_class_rel_cls (subclass, _) (superclass, _) =
blanchet@43091
   271
  [metis_lit false subclass [Metis_Term.Var "T"],
blanchet@43091
   272
   metis_lit true superclass [Metis_Term.Var "T"]]
blanchet@43086
   273
fun class_rel_cls ({subclass, superclass, ...} : class_rel_clause) =
blanchet@43091
   274
  (TrueI, m_class_rel_cls subclass superclass
blanchet@43091
   275
          |> Metis_LiteralSet.fromList |> Metis_Thm.axiom)
blanchet@39497
   276
blanchet@39497
   277
fun type_ext thy tms =
blanchet@43091
   278
  let
blanchet@43091
   279
    val subs = tfree_classes_of_terms tms
blanchet@43091
   280
    val supers = tvar_classes_of_terms tms
blanchet@43091
   281
    val tycons = type_consts_of_terms thy tms
blanchet@43091
   282
    val (supers', arity_clauses) = make_arity_clauses thy tycons supers
blanchet@43091
   283
    val class_rel_clauses = make_class_rel_clauses thy subs supers'
blanchet@43091
   284
  in map class_rel_cls class_rel_clauses @ map arity_cls arity_clauses end
blanchet@39497
   285
blanchet@43092
   286
fun metis_name_from_atp s =
blanchet@43092
   287
  if is_tptp_equal s then "="
blanchet@43092
   288
  else if s = predicator_name then "{}"
blanchet@43092
   289
  else if s = app_op_name then "."
blanchet@43092
   290
  else s
blanchet@43092
   291
fun metis_term_from_atp (ATerm (s, tms)) =
blanchet@43092
   292
  if is_tptp_variable s then Metis_Term.Var s
blanchet@43092
   293
  else Metis_Term.Fn (metis_name_from_atp s, map metis_term_from_atp tms)
blanchet@43092
   294
fun metis_atom_from_atp (AAtom (ATerm (s, tms))) =
blanchet@43092
   295
    (metis_name_from_atp s, map metis_term_from_atp tms)
blanchet@43092
   296
  | metis_atom_from_atp _ = raise Fail "not CNF -- expected atom"
blanchet@43092
   297
fun metis_literal_from_atp (AConn (ANot, [phi])) =
blanchet@43092
   298
    (false, metis_atom_from_atp phi)
blanchet@43092
   299
  | metis_literal_from_atp phi = (true, metis_atom_from_atp phi)
blanchet@43092
   300
fun metis_literals_from_atp (AConn (AOr, [phi1, phi2])) =
blanchet@43092
   301
    uncurry (union (op =)) (pairself metis_literals_from_atp (phi1, phi2))
blanchet@43092
   302
  | metis_literals_from_atp phi = [metis_literal_from_atp phi]
blanchet@43092
   303
fun metis_axiom_from_atp clauses (Formula (ident, _, phi, _, _)) =
blanchet@43092
   304
    let val j = ident |> unprefix conjecture_prefix |> Int.fromString |> the in
blanchet@43092
   305
      (phi |> metis_literals_from_atp |> Metis_LiteralSet.fromList
blanchet@43092
   306
           |> Metis_Thm.axiom,
blanchet@43092
   307
       Meson.make_meta_clause (nth clauses j))
blanchet@43092
   308
    end
blanchet@43092
   309
  | metis_axiom_from_atp _ _ = raise Fail "not CNF -- expected formula"
blanchet@43092
   310
blanchet@39497
   311
(* Function to generate metis clauses, including comb and type clauses *)
blanchet@43092
   312
fun prepare_metis_problem ctxt New conj_clauses fact_clauses =
blanchet@43091
   313
    let
blanchet@43092
   314
      val type_sys = Preds (Polymorphic, (* Nonmonotonic_Types FIXME ### *) No_Types, Light)
blanchet@43092
   315
      val explicit_apply = NONE
blanchet@43092
   316
      val clauses = conj_clauses @ fact_clauses
blanchet@43092
   317
      val (atp_problem, pool, _, _, _, _, sym_tab) =
blanchet@43092
   318
        prepare_atp_problem ctxt CNF Hypothesis Axiom type_sys
blanchet@43092
   319
                            explicit_apply false (map prop_of clauses)
blanchet@43092
   320
                            @{prop False} []
blanchet@43092
   321
      (* val _ = tracing (PolyML.makestring atp_problem) FIXME ### *)
blanchet@43092
   322
      val axioms =
blanchet@43092
   323
        atp_problem
blanchet@43092
   324
        |> maps (map_filter (try (metis_axiom_from_atp clauses)) o snd)
blanchet@43092
   325
    in (New, {axioms = axioms, tfrees = [], old_skolems = [] (* FIXME ### *)}) end
blanchet@43092
   326
  | prepare_metis_problem ctxt mode conj_clauses fact_clauses =
blanchet@43087
   327
    let
blanchet@43087
   328
      val thy = Proof_Context.theory_of ctxt
blanchet@43087
   329
      (* The modes FO and FT are sticky. HO can be downgraded to FO. *)
blanchet@43087
   330
      val mode =
blanchet@43087
   331
        if mode = HO andalso
blanchet@43091
   332
           forall (forall (is_quasi_fol_clause thy))
blanchet@43092
   333
                  [conj_clauses, fact_clauses] then
blanchet@43087
   334
          FO
blanchet@43087
   335
        else
blanchet@43087
   336
          mode
blanchet@41139
   337
      fun add_thm is_conjecture (isa_ith, metis_ith)
blanchet@40157
   338
                  {axioms, tfrees, old_skolems} : metis_problem =
blanchet@39497
   339
        let
blanchet@39886
   340
          val (mth, tfree_lits, old_skolems) =
blanchet@43090
   341
            hol_thm_to_fol is_conjecture ctxt mode (length axioms) old_skolems
blanchet@43090
   342
                           metis_ith
blanchet@39497
   343
        in
blanchet@43091
   344
          {axioms = (mth, isa_ith) :: axioms,
blanchet@43091
   345
           tfrees = union (op =) tfree_lits tfrees, old_skolems = old_skolems}
blanchet@39497
   346
        end;
blanchet@43091
   347
      fun add_type_thm (ith, mth) {axioms, tfrees, old_skolems} =
blanchet@43091
   348
        {axioms = (mth, ith) :: axioms, tfrees = tfrees,
blanchet@43091
   349
         old_skolems = old_skolems}
blanchet@43091
   350
      fun add_tfrees {axioms, tfrees, old_skolems} =
blanchet@43091
   351
        {axioms =
blanchet@43091
   352
           map (rpair TrueI o metis_of_tfree) (distinct (op =) tfrees) @ axioms,
blanchet@43091
   353
         tfrees = tfrees, old_skolems = old_skolems}
blanchet@43091
   354
      val problem =
blanchet@43091
   355
        {axioms = [], tfrees = init_tfrees ctxt, old_skolems = []}
blanchet@43091
   356
        |> fold (add_thm true o `Meson.make_meta_clause) conj_clauses
blanchet@43091
   357
        |> add_tfrees
blanchet@43092
   358
        |> fold (add_thm false o `Meson.make_meta_clause) fact_clauses
blanchet@43091
   359
      val clause_lists = map (Metis_Thm.clause o #1) (#axioms problem)
blanchet@39497
   360
      fun is_used c =
blanchet@39497
   361
        exists (Metis_LiteralSet.exists (const_in_metis c o #2)) clause_lists
blanchet@43091
   362
      val problem =
blanchet@39497
   363
        if mode = FO then
blanchet@43091
   364
          problem
blanchet@39497
   365
        else
blanchet@39497
   366
          let
blanchet@41156
   367
            val fdefs = @{thms fFalse_def fTrue_def fNot_def fconj_def fdisj_def
blanchet@41156
   368
                               fimplies_def fequal_def}
blanchet@41139
   369
            val prepare_helper =
blanchet@41139
   370
              zero_var_indexes
blanchet@41139
   371
              #> `(Meson.make_meta_clause
blanchet@41139
   372
                   #> rewrite_rule (map safe_mk_meta_eq fdefs))
blanchet@39497
   373
            val helper_ths =
blanchet@43085
   374
              helper_table
blanchet@42561
   375
              |> filter (is_used o prefix const_prefix o fst)
blanchet@42894
   376
              |> maps (fn (_, (needs_full_types, thms)) =>
blanchet@41140
   377
                          if needs_full_types andalso mode <> FT then []
blanchet@41140
   378
                          else map prepare_helper thms)
blanchet@43091
   379
          in problem |> fold (add_thm false) helper_ths end
blanchet@43092
   380
      val type_ths = type_ext thy (map prop_of (conj_clauses @ fact_clauses))
blanchet@43091
   381
    in (mode, fold add_type_thm type_ths problem) end
blanchet@39497
   382
paulson@15347
   383
end;