src/HOL/Tools/Metis/metis_translate.ML
author blanchet
Tue May 31 16:38:36 2011 +0200 (2011-05-31)
changeset 43087 b870759ce0f3
parent 43086 4dce7f2bb59f
child 43090 f6331d785128
permissions -rw-r--r--
added new metis mode, with no implementation yet
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@39886
    28
    mode -> Proof.context -> bool -> thm list -> thm list list
blanchet@40157
    29
    -> mode * metis_problem
wenzelm@24310
    30
end
paulson@15347
    31
blanchet@39494
    32
structure Metis_Translate : METIS_TRANSLATE =
paulson@15347
    33
struct
paulson@15347
    34
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@40145
   210
fun hol_thm_to_fol is_conjecture ctxt type_lits 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@39497
   225
        val mtylits =
blanchet@39497
   226
          if type_lits then map (metis_of_type_literals false) tylits else []
blanchet@39497
   227
      in
blanchet@39497
   228
        (Metis_Thm.axiom (Metis_LiteralSet.fromList(mtylits @ mlits)), [],
blanchet@39886
   229
         old_skolems)
blanchet@39497
   230
      end
blanchet@39497
   231
  end;
blanchet@39497
   232
blanchet@39497
   233
(* ------------------------------------------------------------------------- *)
blanchet@39497
   234
(* Logic maps manage the interface between HOL and first-order logic.        *)
blanchet@39497
   235
(* ------------------------------------------------------------------------- *)
blanchet@39497
   236
blanchet@40157
   237
type metis_problem =
blanchet@39497
   238
  {axioms: (Metis_Thm.thm * thm) list,
blanchet@39497
   239
   tfrees: type_literal list,
blanchet@39886
   240
   old_skolems: (string * term) list}
blanchet@39497
   241
blanchet@39497
   242
fun is_quasi_fol_clause thy =
blanchet@39886
   243
  Meson.is_fol_term thy o snd o conceal_old_skolem_terms ~1 [] o prop_of
blanchet@39497
   244
blanchet@39497
   245
(*Extract TFree constraints from context to include as conjecture clauses*)
blanchet@39497
   246
fun init_tfrees ctxt =
blanchet@39497
   247
  let fun add ((a,i),s) Ts = if i = ~1 then TFree(a,s) :: Ts else Ts in
blanchet@39497
   248
    Vartab.fold add (#2 (Variable.constraints_of ctxt)) []
blanchet@43085
   249
    |> raw_type_literals_for_types
blanchet@39497
   250
  end;
blanchet@39497
   251
blanchet@39497
   252
(*Insert non-logical axioms corresponding to all accumulated TFrees*)
blanchet@40157
   253
fun add_tfrees {axioms, tfrees, old_skolems} : metis_problem =
blanchet@39497
   254
     {axioms = map (rpair TrueI o metis_of_tfree) (distinct (op =) tfrees) @
blanchet@39497
   255
               axioms,
blanchet@39886
   256
      tfrees = tfrees, old_skolems = old_skolems}
blanchet@39497
   257
blanchet@39497
   258
(*transform isabelle type / arity clause to metis clause *)
blanchet@39497
   259
fun add_type_thm [] lmap = lmap
blanchet@39886
   260
  | add_type_thm ((ith, mth) :: cls) {axioms, tfrees, old_skolems} =
blanchet@39497
   261
      add_type_thm cls {axioms = (mth, ith) :: axioms, tfrees = tfrees,
blanchet@39886
   262
                        old_skolems = old_skolems}
blanchet@39497
   263
blanchet@39497
   264
fun const_in_metis c (pred, tm_list) =
blanchet@39497
   265
  let
blanchet@39497
   266
    fun in_mterm (Metis_Term.Var _) = false
blanchet@41156
   267
      | in_mterm (Metis_Term.Fn (nm, tm_list)) =
blanchet@41156
   268
        c = nm orelse exists in_mterm tm_list
blanchet@41156
   269
  in c = pred orelse exists in_mterm tm_list end
blanchet@39497
   270
blanchet@39497
   271
(* ARITY CLAUSE *)
blanchet@39497
   272
fun m_arity_cls (TConsLit ((c, _), (t, _), args)) =
blanchet@39497
   273
    metis_lit true c [Metis_Term.Fn(t, map (Metis_Term.Var o fst) args)]
blanchet@39497
   274
  | m_arity_cls (TVarLit ((c, _), (s, _))) =
blanchet@39497
   275
    metis_lit false c [Metis_Term.Var s]
blanchet@39497
   276
(*TrueI is returned as the Isabelle counterpart because there isn't any.*)
blanchet@43086
   277
fun arity_cls ({prem_lits, concl_lits, ...} : arity_clause) =
blanchet@39497
   278
  (TrueI,
blanchet@42895
   279
   Metis_Thm.axiom (Metis_LiteralSet.fromList
blanchet@42895
   280
                        (map m_arity_cls (concl_lits :: prem_lits))));
blanchet@39497
   281
blanchet@39497
   282
(* CLASSREL CLAUSE *)
blanchet@39497
   283
fun m_class_rel_cls (subclass, _) (superclass, _) =
blanchet@39497
   284
  [metis_lit false subclass [Metis_Term.Var "T"], metis_lit true superclass [Metis_Term.Var "T"]];
blanchet@43086
   285
fun class_rel_cls ({subclass, superclass, ...} : class_rel_clause) =
blanchet@39497
   286
  (TrueI, Metis_Thm.axiom (Metis_LiteralSet.fromList (m_class_rel_cls subclass superclass)));
blanchet@39497
   287
blanchet@39497
   288
fun type_ext thy tms =
blanchet@39497
   289
  let val subs = tfree_classes_of_terms tms
blanchet@39497
   290
      val supers = tvar_classes_of_terms tms
blanchet@43003
   291
      val tycons = type_consts_of_terms thy tms
blanchet@39497
   292
      val (supers', arity_clauses) = make_arity_clauses thy tycons supers
blanchet@39497
   293
      val class_rel_clauses = make_class_rel_clauses thy subs supers'
blanchet@39497
   294
  in  map class_rel_cls class_rel_clauses @ map arity_cls arity_clauses
blanchet@39497
   295
  end;
blanchet@39497
   296
blanchet@39497
   297
(* Function to generate metis clauses, including comb and type clauses *)
blanchet@43087
   298
fun prepare_metis_problem New ctxt type_lits cls thss =
blanchet@43087
   299
    error "Not implemented yet"
blanchet@43087
   300
  | prepare_metis_problem mode ctxt type_lits cls thss =
blanchet@43087
   301
    let
blanchet@43087
   302
      val thy = Proof_Context.theory_of ctxt
blanchet@43087
   303
      (* The modes FO and FT are sticky. HO can be downgraded to FO. *)
blanchet@43087
   304
      val mode =
blanchet@43087
   305
        if mode = HO andalso
blanchet@43087
   306
           forall (forall (is_quasi_fol_clause thy)) (cls :: thss) then
blanchet@43087
   307
          FO
blanchet@43087
   308
        else
blanchet@43087
   309
          mode
blanchet@39497
   310
      (*transform isabelle clause to metis clause *)
blanchet@41139
   311
      fun add_thm is_conjecture (isa_ith, metis_ith)
blanchet@40157
   312
                  {axioms, tfrees, old_skolems} : metis_problem =
blanchet@39497
   313
        let
blanchet@39886
   314
          val (mth, tfree_lits, old_skolems) =
blanchet@40145
   315
            hol_thm_to_fol is_conjecture ctxt type_lits mode (length axioms)
blanchet@39886
   316
                           old_skolems metis_ith
blanchet@39497
   317
        in
blanchet@41139
   318
           {axioms = (mth, isa_ith) :: axioms,
blanchet@39886
   319
            tfrees = union (op =) tfree_lits tfrees, old_skolems = old_skolems}
blanchet@39497
   320
        end;
blanchet@39886
   321
      val lmap = {axioms = [], tfrees = init_tfrees ctxt, old_skolems = []}
blanchet@41139
   322
                 |> fold (add_thm true o `Meson.make_meta_clause) cls
blanchet@39497
   323
                 |> add_tfrees
blanchet@41139
   324
                 |> fold (fold (add_thm false o `Meson.make_meta_clause)) thss
blanchet@39497
   325
      val clause_lists = map (Metis_Thm.clause o #1) (#axioms lmap)
blanchet@39497
   326
      fun is_used c =
blanchet@39497
   327
        exists (Metis_LiteralSet.exists (const_in_metis c o #2)) clause_lists
blanchet@39497
   328
      val lmap =
blanchet@39497
   329
        if mode = FO then
blanchet@39497
   330
          lmap
blanchet@39497
   331
        else
blanchet@39497
   332
          let
blanchet@41156
   333
            val fdefs = @{thms fFalse_def fTrue_def fNot_def fconj_def fdisj_def
blanchet@41156
   334
                               fimplies_def fequal_def}
blanchet@41139
   335
            val prepare_helper =
blanchet@41139
   336
              zero_var_indexes
blanchet@41139
   337
              #> `(Meson.make_meta_clause
blanchet@41139
   338
                   #> rewrite_rule (map safe_mk_meta_eq fdefs))
blanchet@39497
   339
            val helper_ths =
blanchet@43085
   340
              helper_table
blanchet@42561
   341
              |> filter (is_used o prefix const_prefix o fst)
blanchet@42894
   342
              |> maps (fn (_, (needs_full_types, thms)) =>
blanchet@41140
   343
                          if needs_full_types andalso mode <> FT then []
blanchet@41140
   344
                          else map prepare_helper thms)
blanchet@40145
   345
          in lmap |> fold (add_thm false) helper_ths end
blanchet@43087
   346
    in
blanchet@43087
   347
      (mode,
blanchet@43087
   348
       add_type_thm (type_ext thy (maps (map prop_of) (cls :: thss))) lmap)
blanchet@43087
   349
    end
blanchet@39497
   350
paulson@15347
   351
end;