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