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