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