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