src/Pure/axclass.ML
author berghofe
Mon Jan 21 14:18:49 2008 +0100 (2008-01-21)
changeset 25939 ddea202704b4
parent 25863 5b4a8b1d0f88
child 26246 e212c22f35c2
permissions -rw-r--r--
Removed Logic.auto_rename.
     1 (*  Title:      Pure/axclass.ML
     2     ID:         $Id$
     3     Author:     Markus Wenzel, TU Muenchen
     4 
     5 Type classes defined as predicates, associated with a record of
     6 parameters.
     7 *)
     8 
     9 signature AX_CLASS =
    10 sig
    11   val define_class: bstring * class list -> string list ->
    12     ((bstring * attribute list) * term list) list -> theory -> class * theory
    13   val add_classrel: thm -> theory -> theory
    14   val add_arity: thm -> theory -> theory
    15   val prove_classrel: class * class -> tactic -> theory -> theory
    16   val prove_arity: string * sort list * sort -> tactic -> theory -> theory
    17   val get_info: theory -> class ->
    18     {def: thm, intro: thm, axioms: thm list, params: (string * typ) list}
    19   val class_intros: theory -> thm list
    20   val class_of_param: theory -> string -> class option
    21   val cert_classrel: theory -> class * class -> class * class
    22   val read_classrel: theory -> xstring * xstring -> class * class
    23   val axiomatize_class: bstring * class list -> theory -> theory
    24   val axiomatize_class_cmd: bstring * xstring list -> theory -> theory
    25   val axiomatize_classrel: (class * class) list -> theory -> theory
    26   val axiomatize_classrel_cmd: (xstring * xstring) list -> theory -> theory
    27   val axiomatize_arity: arity -> theory -> theory
    28   val axiomatize_arity_cmd: xstring * string list * string -> theory -> theory
    29   val instance_name: string * class -> string
    30   val declare_overloaded: string * typ -> theory -> term * theory
    31   val define_overloaded: string -> string * term -> theory -> thm * theory
    32   val inst_tyco_of: theory -> string * typ -> string option
    33   val unoverload: theory -> thm -> thm
    34   val overload: theory -> thm -> thm
    35   val unoverload_conv: theory -> conv
    36   val overload_conv: theory -> conv
    37   val unoverload_const: theory -> string * typ -> string
    38   val param_of_inst: theory -> string * string -> string
    39   val inst_of_param: theory -> string -> (string * string) option
    40   type cache
    41   val of_sort: theory -> typ * sort -> cache -> thm list * cache  (*exception Sorts.CLASS_ERROR*)
    42   val cache: cache
    43   val axiomsN: string
    44 end;
    45 
    46 structure AxClass: AX_CLASS =
    47 struct
    48 
    49 (** theory data **)
    50 
    51 (* class parameters (canonical order) *)
    52 
    53 type param = string * class;
    54 
    55 fun add_param pp ((x, c): param) params =
    56   (case AList.lookup (op =) params x of
    57     NONE => (x, c) :: params
    58   | SOME c' => error ("Duplicate class parameter " ^ quote x ^
    59       " for " ^ Pretty.string_of_sort pp [c] ^
    60       (if c = c' then "" else " and " ^ Pretty.string_of_sort pp [c'])));
    61 
    62 fun merge_params _ ([], qs) = qs
    63   | merge_params pp (ps, qs) =
    64       fold_rev (fn q => if member (op =) ps q then I else add_param pp q) qs ps;
    65 
    66 
    67 (* axclasses *)
    68 
    69 val introN = "intro";
    70 val superN = "super";
    71 val axiomsN = "axioms";
    72 
    73 datatype axclass = AxClass of
    74  {def: thm,
    75   intro: thm,
    76   axioms: thm list,
    77   params: (string * typ) list};
    78 
    79 type axclasses = axclass Symtab.table * param list;
    80 
    81 fun make_axclass ((def, intro, axioms), params) =
    82   AxClass {def = def, intro = intro, axioms = axioms, params = params};
    83 
    84 fun merge_axclasses pp ((tab1, params1), (tab2, params2)) : axclasses =
    85   (Symtab.merge (K true) (tab1, tab2), merge_params pp (params1, params2));
    86 
    87 
    88 (* instances *)
    89 
    90 val classrel_prefix = "classrel_";
    91 val arity_prefix = "arity_";
    92 
    93 type instances =
    94   ((class * class) * thm) list *
    95   ((class * sort list) * thm) list Symtab.table;
    96 
    97 fun merge_instances ((classrel1, arities1): instances, (classrel2, arities2)) =
    98  (merge (eq_fst op =) (classrel1, classrel2),
    99   Symtab.join (K (merge (eq_fst op =))) (arities1, arities2));
   100 
   101 
   102 (* instance parameters *)
   103 
   104 type inst_params =
   105   (string * thm) Symtab.table Symtab.table
   106     (*constant name ~> type constructor ~> (constant name, equation)*)
   107   * (string * string) Symtab.table; (*constant name ~> (constant name, type constructor)*)
   108 
   109 fun merge_inst_params ((const_param1, param_const1), (const_param2, param_const2)) =
   110   (Symtab.join  (K (Symtab.merge (K true))) (const_param1, const_param2),
   111     Symtab.merge (K true) (param_const1, param_const2));
   112 
   113 
   114 (* setup data *)
   115 
   116 structure AxClassData = TheoryDataFun
   117 (
   118   type T = axclasses * (instances * inst_params);
   119   val empty = ((Symtab.empty, []), (([], Symtab.empty), (Symtab.empty, Symtab.empty)));
   120   val copy = I;
   121   val extend = I;
   122   fun merge pp ((axclasses1, (instances1, inst_params1)), (axclasses2, (instances2, inst_params2))) =
   123     (merge_axclasses pp (axclasses1, axclasses2),
   124       (merge_instances (instances1, instances2), merge_inst_params (inst_params1, inst_params2)));
   125 );
   126 
   127 
   128 (* maintain axclasses *)
   129 
   130 val get_axclasses = #1 o AxClassData.get;
   131 val map_axclasses = AxClassData.map o apfst;
   132 
   133 val lookup_def = Symtab.lookup o #1 o get_axclasses;
   134 
   135 fun get_info thy c =
   136   (case lookup_def thy c of
   137     SOME (AxClass info) => info
   138   | NONE => error ("No such axclass: " ^ quote c));
   139 
   140 fun class_intros thy =
   141   let
   142     fun add_intro c =
   143       (case lookup_def thy c of SOME (AxClass {intro, ...}) => cons intro | _ => I);
   144     val classes = Sign.all_classes thy;
   145   in map (Thm.class_triv thy) classes @ fold add_intro classes [] end;
   146 
   147 
   148 fun get_params thy pred =
   149   let val params = #2 (get_axclasses thy);
   150   in fold (fn (x, c) => if pred c then cons x else I) params [] end;
   151 
   152 fun params_of thy c = get_params thy (fn c' => c' = c);
   153 fun all_params_of thy S = get_params thy (fn c => Sign.subsort thy (S, [c]));
   154 
   155 fun class_of_param thy = AList.lookup (op =) (#2 (get_axclasses thy));
   156 
   157 
   158 (* maintain instances *)
   159 
   160 fun instance_name (a, c) = NameSpace.base c ^ "_" ^ NameSpace.base a;
   161 
   162 val get_instances = #1 o #2 o AxClassData.get;
   163 val map_instances = AxClassData.map o apsnd o apfst;
   164 
   165 
   166 fun the_classrel thy (c1, c2) =
   167   (case AList.lookup (op =) (#1 (get_instances thy)) (c1, c2) of
   168     SOME th => Thm.transfer thy th
   169   | NONE => error ("Unproven class relation " ^
   170       Syntax.string_of_classrel (ProofContext.init thy) [c1, c2]));
   171 
   172 fun put_classrel arg = map_instances (fn (classrel, arities) =>
   173   (insert (eq_fst op =) arg classrel, arities));
   174 
   175 
   176 fun the_arity thy a (c, Ss) =
   177   (case AList.lookup (op =) (Symtab.lookup_list (#2 (get_instances thy)) a) (c, Ss) of
   178     SOME th => Thm.transfer thy th
   179   | NONE => error ("Unproven type arity " ^
   180       Syntax.string_of_arity (ProofContext.init thy) (a, Ss, [c])));
   181 
   182 fun put_arity ((t, Ss, c), th) = map_instances (fn (classrel, arities) =>
   183   (classrel, arities |> Symtab.insert_list (eq_fst op =) (t, ((c, Ss), th))));
   184 
   185 
   186 (* maintain instance parameters *)
   187 
   188 val get_inst_params = #2 o #2 o AxClassData.get;
   189 val map_inst_params = AxClassData.map o apsnd o apsnd;
   190 
   191 fun get_inst_param thy (c, tyco) =
   192   (the o Symtab.lookup ((the o Symtab.lookup (fst (get_inst_params thy))) c)) tyco;
   193 
   194 fun add_inst_param (c, tyco) inst = (map_inst_params o apfst
   195       o Symtab.map_default (c, Symtab.empty)) (Symtab.update_new (tyco, inst))
   196   #> (map_inst_params o apsnd) (Symtab.update_new (fst inst, (c, tyco)));
   197 
   198 val inst_of_param = Symtab.lookup o snd o get_inst_params;
   199 val param_of_inst = fst oo get_inst_param;
   200 
   201 fun inst_thms thy = (Symtab.fold (Symtab.fold (cons o snd o snd) o snd) o fst)
   202   (get_inst_params thy) [];
   203 
   204 fun inst_tyco_of thy = try (fst o dest_Type o the_single o Sign.const_typargs thy);
   205 
   206 fun unoverload thy = MetaSimplifier.simplify true (inst_thms thy);
   207 fun overload thy = MetaSimplifier.simplify true (map Thm.symmetric (inst_thms thy));
   208 
   209 fun unoverload_conv thy = MetaSimplifier.rewrite true (inst_thms thy);
   210 fun overload_conv thy = MetaSimplifier.rewrite true (map Thm.symmetric (inst_thms thy));
   211 
   212 fun unoverload_const thy (c_ty as (c, _)) =
   213   case class_of_param thy c
   214    of SOME class => (case inst_tyco_of thy c_ty
   215        of SOME tyco => try (param_of_inst thy) (c, tyco) |> the_default c
   216         | NONE => c)
   217     | NONE => c;
   218 
   219 
   220 (** instances **)
   221 
   222 (* class relations *)
   223 
   224 fun cert_classrel thy raw_rel =
   225   let
   226     val (c1, c2) = pairself (Sign.certify_class thy) raw_rel;
   227     val _ = Sign.primitive_classrel (c1, c2) (Theory.copy thy);
   228     val _ =
   229       (case subtract (op =) (all_params_of thy [c1]) (all_params_of thy [c2]) of
   230         [] => ()
   231       | xs => raise TYPE ("Class " ^ Sign.string_of_sort thy [c1] ^ " lacks parameter(s) " ^
   232           commas_quote xs ^ " of " ^ Sign.string_of_sort thy [c2], [], []));
   233   in (c1, c2) end;
   234 
   235 fun read_classrel thy raw_rel =
   236   cert_classrel thy (pairself (Sign.read_class thy) raw_rel)
   237     handle TYPE (msg, _, _) => error msg;
   238 
   239 
   240 (* primitive rules *)
   241 
   242 fun add_classrel th thy =
   243   let
   244     fun err () = raise THM ("add_classrel: malformed class relation", 0, [th]);
   245     val prop = Thm.plain_prop_of (Thm.transfer thy th);
   246     val rel = Logic.dest_classrel prop handle TERM _ => err ();
   247     val (c1, c2) = cert_classrel thy rel handle TYPE _ => err ();
   248   in
   249     thy
   250     |> Sign.primitive_classrel (c1, c2)
   251     |> put_classrel ((c1, c2), Drule.unconstrainTs th)
   252   end;
   253 
   254 fun add_arity th thy =
   255   let
   256     fun err () = raise THM ("add_arity: malformed type arity", 0, [th]);
   257     val prop = Thm.plain_prop_of (Thm.transfer thy th);
   258     val (t, Ss, c) = Logic.dest_arity prop handle TERM _ => err ();
   259     val _ = map (Sign.certify_sort thy) Ss = Ss orelse err ();
   260     (*FIXME turn this into a mere check as soon as "attach" has disappeared*)
   261     val missing_params = Sign.complete_sort thy [c]
   262       |> maps (these o Option.map (fn AxClass { params, ... } => params) o lookup_def thy)
   263       |> filter_out (fn (p, _) => can (get_inst_param thy) (p, t));
   264     fun declare_missing (p, T0) thy =
   265       let
   266         val name_inst = instance_name (t, c) ^ "_inst";
   267         val p' = NameSpace.base p ^ "_" ^ NameSpace.base t;
   268         val vs = Name.names Name.context Name.aT (replicate (Sign.arity_number thy t) []);
   269         val T = map_atyps (fn _ => Type (t, map TFree vs)) T0;
   270       in
   271         thy
   272         |> Sign.sticky_prefix name_inst
   273         |> Sign.no_base_names
   274         |> Sign.declare_const [] (p', T, NoSyn)
   275         |-> (fn const' as Const (p'', _) => Thm.add_def false true
   276               (Thm.def_name p', Logic.mk_equals (const', Const (p, T)))
   277         #>> Thm.varifyT
   278         #-> (fn thm => add_inst_param (p, t) (p'', Thm.symmetric thm)
   279         #> PureThy.note Thm.internalK (p', thm)
   280         #> snd
   281         #> Sign.restore_naming thy))
   282       end;
   283 
   284   in
   285     thy
   286     |> Sign.primitive_arity (t, Ss, [c])
   287     |> put_arity ((t, Ss, c), Drule.unconstrainTs th)
   288     |> fold declare_missing missing_params
   289   end;
   290 
   291 
   292 (* tactical proofs *)
   293 
   294 fun prove_classrel raw_rel tac thy =
   295   let
   296     val ctxt = ProofContext.init thy;
   297     val (c1, c2) = cert_classrel thy raw_rel;
   298     val th = Goal.prove ctxt [] []
   299         (Logic.mk_classrel (c1, c2)) (fn _ => tac) handle ERROR msg =>
   300       cat_error msg ("The error(s) above occurred while trying to prove class relation " ^
   301         quote (Syntax.string_of_classrel ctxt [c1, c2]));
   302   in
   303     thy
   304     |> PureThy.add_thms [((prefix classrel_prefix (Logic.name_classrel (c1, c2)), th), [])]
   305     |-> (fn [th'] => add_classrel th')
   306   end;
   307 
   308 fun prove_arity raw_arity tac thy =
   309   let
   310     val ctxt = ProofContext.init thy;
   311     val arity = Sign.cert_arity thy raw_arity;
   312     val names = map (prefix arity_prefix) (Logic.name_arities arity);
   313     val props = Logic.mk_arities arity;
   314     val ths = Goal.prove_multi ctxt [] [] props
   315       (fn _ => Goal.precise_conjunction_tac (length props) 1 THEN tac) handle ERROR msg =>
   316         cat_error msg ("The error(s) above occurred while trying to prove type arity " ^
   317           quote (Syntax.string_of_arity ctxt arity));
   318   in
   319     thy
   320     |> PureThy.add_thms (map (rpair []) (names ~~ ths))
   321     |-> fold add_arity
   322   end;
   323 
   324 
   325 (* instance parameters and overloaded definitions *)
   326 
   327 (* declaration and definition of instances of overloaded constants *)
   328 
   329 fun declare_overloaded (c, T) thy =
   330   let
   331     val class = case class_of_param thy c
   332      of SOME class => class
   333       | NONE => error ("Not a class parameter: " ^ quote c);
   334     val tyco = case inst_tyco_of thy (c, T)
   335      of SOME tyco => tyco
   336       | NONE => error ("Illegal type for instantiation of class parameter: "
   337         ^ quote (c ^ " :: " ^ Sign.string_of_typ thy T));
   338     val name_inst = instance_name (tyco, class) ^ "_inst";
   339     val c' = NameSpace.base c ^ "_" ^ NameSpace.base tyco;
   340     val T' = Type.strip_sorts T;
   341   in
   342     thy
   343     |> Sign.sticky_prefix name_inst
   344     |> Sign.no_base_names
   345     |> Sign.declare_const [] (c', T', NoSyn)
   346     |-> (fn const' as Const (c'', _) => Thm.add_def false true
   347           (Thm.def_name c', Logic.mk_equals (Const (c, T'), const'))
   348     #>> Thm.varifyT
   349     #-> (fn thm => add_inst_param (c, tyco) (c'', thm)
   350     #> PureThy.note Thm.internalK (c', thm)
   351     #> snd
   352     #> Sign.restore_naming thy
   353     #> pair (Const (c, T))))
   354   end;
   355 
   356 fun define_overloaded name (c, t) thy =
   357   let
   358     val T = Term.fastype_of t;
   359     val SOME tyco = inst_tyco_of thy (c, T);
   360     val (c', eq) = get_inst_param thy (c, tyco);
   361     val prop = Logic.mk_equals (Const (c', T), t);
   362     val name' = Thm.def_name_optional
   363       (NameSpace.base c ^ "_" ^ NameSpace.base tyco) name;
   364   in
   365     thy
   366     |> Thm.add_def false false (name', prop)
   367     |>> (fn thm =>  Drule.transitive_thm OF [eq, thm])
   368   end;
   369 
   370 
   371 
   372 (** class definitions **)
   373 
   374 fun split_defined n eq =
   375   let
   376     val intro =
   377       (eq RS Drule.equal_elim_rule2)
   378       |> Conjunction.curry_balanced n
   379       |> n = 0 ? Thm.eq_assumption 1;
   380     val dests =
   381       if n = 0 then []
   382       else
   383         (eq RS Drule.equal_elim_rule1)
   384         |> BalancedTree.dest (fn th =>
   385           (th RS Conjunction.conjunctionD1, th RS Conjunction.conjunctionD2)) n;
   386   in (intro, dests) end;
   387 
   388 fun define_class (bclass, raw_super) raw_params raw_specs thy =
   389   let
   390     val ctxt = ProofContext.init thy;
   391     val pp = Syntax.pp ctxt;
   392 
   393 
   394     (* class *)
   395 
   396     val bconst = Logic.const_of_class bclass;
   397     val class = Sign.full_name thy bclass;
   398     val super = Sign.minimize_sort thy (Sign.certify_sort thy raw_super);
   399 
   400     fun check_constraint (a, S) =
   401       if Sign.subsort thy (super, S) then ()
   402       else error ("Sort constraint of type variable " ^
   403         setmp show_sorts true (Pretty.string_of_typ pp) (TFree (a, S)) ^
   404         " needs to be weaker than " ^ Pretty.string_of_sort pp super);
   405 
   406 
   407     (* params *)
   408 
   409     val params = raw_params |> map (fn p =>
   410       let
   411         val T = Sign.the_const_type thy p;
   412         val _ =
   413           (case Term.add_tvarsT T [] of
   414             [((a, _), S)] => check_constraint (a, S)
   415           | _ => error ("Exactly one type variable expected in class parameter " ^ quote p));
   416         val T' = Term.map_type_tvar (fn _ => TFree (Name.aT, [class])) T;
   417       in (p, T') end);
   418 
   419 
   420     (* axioms *)
   421 
   422     fun prep_axiom t =
   423       (case Term.add_tfrees t [] of
   424         [(a, S)] => check_constraint (a, S)
   425       | [] => ()
   426       | _ => error ("Multiple type variables in class axiom:\n" ^ Pretty.string_of_term pp t);
   427       t
   428       |> Term.map_types (Term.map_atyps (fn TFree _ => Term.aT [] | U => U))
   429       |> Logic.close_form);
   430 
   431     val axiomss = map (map (prep_axiom o Sign.cert_prop thy) o snd) raw_specs;
   432     val name_atts = map fst raw_specs;
   433 
   434 
   435     (* definition *)
   436 
   437     val conjs = map (curry Logic.mk_inclass (Term.aT [])) super @ flat axiomss;
   438     val class_eq =
   439       Logic.mk_equals (Logic.mk_inclass (Term.aT [], class), Logic.mk_conjunction_balanced conjs);
   440 
   441     val ([def], def_thy) =
   442       thy
   443       |> Sign.primitive_class (bclass, super)
   444       |> PureThy.add_defs_i false [((Thm.def_name bconst, class_eq), [])];
   445     val (raw_intro, (raw_classrel, raw_axioms)) =
   446       split_defined (length conjs) def ||> chop (length super);
   447 
   448 
   449     (* facts *)
   450 
   451     val class_triv = Thm.class_triv def_thy class;
   452     val ([(_, [intro]), (_, classrel), (_, axioms)], facts_thy) =
   453       def_thy
   454       |> PureThy.note_thmss_qualified "" bconst
   455         [((introN, []), [([Drule.standard raw_intro], [])]),
   456          ((superN, []), [(map Drule.standard raw_classrel, [])]),
   457          ((axiomsN, []), [(map (fn th => Drule.standard (class_triv RS th)) raw_axioms, [])])];
   458 
   459 
   460     (* result *)
   461 
   462     val axclass = make_axclass ((def, intro, axioms), params);
   463     val result_thy =
   464       facts_thy
   465       |> fold put_classrel (map (pair class) super ~~ classrel)
   466       |> Sign.add_path bconst
   467       |> PureThy.note_thmss_i "" (name_atts ~~ map Thm.simple_fact (unflat axiomss axioms)) |> snd
   468       |> Sign.restore_naming facts_thy
   469       |> map_axclasses (fn (axclasses, parameters) =>
   470         (Symtab.update (class, axclass) axclasses,
   471           fold (fn (x, _) => add_param pp (x, class)) params parameters));
   472 
   473   in (class, result_thy) end;
   474 
   475 
   476 
   477 (** axiomatizations **)
   478 
   479 local
   480 
   481 fun axiomatize prep mk name add raw_args thy =
   482   let
   483     val args = prep thy raw_args;
   484     val specs = mk args;
   485     val names = name args;
   486   in thy |> PureThy.add_axioms_i (map (rpair []) (names ~~ specs)) |-> fold add end;
   487 
   488 fun ax_classrel prep =
   489   axiomatize (map o prep) (map Logic.mk_classrel)
   490     (map (prefix classrel_prefix o Logic.name_classrel)) add_classrel;
   491 
   492 fun ax_arity prep =
   493   axiomatize prep Logic.mk_arities
   494     (map (prefix arity_prefix) o Logic.name_arities) add_arity;
   495 
   496 fun class_const c =
   497   (Logic.const_of_class c, Term.itselfT (Term.aT []) --> propT);
   498 
   499 fun ax_class prep_class prep_classrel (bclass, raw_super) thy =
   500   let
   501     val class = Sign.full_name thy bclass;
   502     val super = map (prep_class thy) raw_super |> Sign.minimize_sort thy;
   503   in
   504     thy
   505     |> Sign.primitive_class (bclass, super)
   506     |> ax_classrel prep_classrel (map (fn c => (class, c)) super)
   507     |> Theory.add_deps "" (class_const class) (map class_const super)
   508   end;
   509 
   510 in
   511 
   512 val axiomatize_class = ax_class Sign.certify_class cert_classrel;
   513 val axiomatize_class_cmd = ax_class Sign.read_class read_classrel;
   514 val axiomatize_classrel = ax_classrel cert_classrel;
   515 val axiomatize_classrel_cmd = ax_classrel read_classrel;
   516 val axiomatize_arity = ax_arity Sign.cert_arity;
   517 val axiomatize_arity_cmd = ax_arity Sign.read_arity;
   518 
   519 end;
   520 
   521 
   522 
   523 (** explicit derivations -- cached **)
   524 
   525 datatype cache = Types of (class * thm) list Typtab.table;
   526 
   527 local
   528 
   529 fun lookup_type (Types cache) = AList.lookup (op =) o Typtab.lookup_list cache;
   530 fun insert_type T der (Types cache) = Types (Typtab.insert_list (eq_fst op =) (T, der) cache);
   531 
   532 fun derive_type _ (_, []) = []
   533   | derive_type thy (typ, sort) =
   534       let
   535         val vars = Term.fold_atyps
   536             (fn T as TFree (_, S) => insert (eq_fst op =) (T, S)
   537               | T as TVar (_, S) => insert (eq_fst op =) (T, S)
   538               | _ => I) typ [];
   539         val hyps = vars
   540           |> map (fn (T, S) => (T, Drule.sort_triv thy (T, S) ~~ S));
   541         val ths = (typ, sort) |> Sorts.of_sort_derivation (Sign.pp thy) (Sign.classes_of thy)
   542            {class_relation =
   543               fn (th, c1) => fn c2 => th RS the_classrel thy (c1, c2),
   544             type_constructor =
   545               fn a => fn dom => fn c =>
   546                 let val Ss = map (map snd) dom and ths = maps (map fst) dom
   547                 in ths MRS the_arity thy a (c, Ss) end,
   548             type_variable =
   549               the_default [] o AList.lookup (op =) hyps};
   550       in ths end;
   551 
   552 in
   553 
   554 fun of_sort thy (typ, sort) cache =
   555   let
   556     val sort' = filter (is_none o lookup_type cache typ) sort;
   557     val ths' = derive_type thy (typ, sort')
   558       handle ERROR msg => cat_error msg ("The error(s) above occurred for sort derivation: " ^
   559         Sign.string_of_typ thy typ ^ " :: " ^ Sign.string_of_sort thy sort');
   560     val cache' = cache |> fold (insert_type typ) (sort' ~~ ths');
   561     val ths =
   562       sort |> map (fn c =>
   563         Goal.finish (the (lookup_type cache' typ c) RS
   564           Goal.init (Thm.cterm_of thy (Logic.mk_inclass (typ, c))))
   565         |> Thm.adjust_maxidx_thm ~1);
   566   in (ths, cache') end;
   567 
   568 end;
   569 
   570 val cache = Types Typtab.empty;
   571 
   572 end;