src/Pure/axclass.ML
author wenzelm
Mon Jul 27 17:44:55 2015 +0200 (2015-07-27)
changeset 60801 7664e0916eec
parent 59859 f9d1442c70f3
child 61246 077b88f9ec16
permissions -rw-r--r--
tuned signature;
     1 (*  Title:      Pure/axclass.ML
     2     Author:     Markus Wenzel, TU Muenchen
     3 
     4 Type classes defined as predicates, associated with a record of
     5 parameters.  Proven class relations and type arities.
     6 *)
     7 
     8 signature AXCLASS =
     9 sig
    10   type info = {def: thm, intro: thm, axioms: thm list, params: (string * typ) list}
    11   val get_info: theory -> class -> info
    12   val class_of_param: theory -> string -> class option
    13   val instance_name: string * class -> string
    14   val thynames_of_arity: theory -> string * class -> string list
    15   val param_of_inst: theory -> string * string -> string
    16   val inst_of_param: theory -> string -> (string * string) option
    17   val unoverload: theory -> thm -> thm
    18   val overload: theory -> thm -> thm
    19   val unoverload_conv: theory -> conv
    20   val overload_conv: theory -> conv
    21   val lookup_inst_param: Consts.T -> ((string * string) * 'a) list -> string * typ -> 'a option
    22   val unoverload_const: theory -> string * typ -> string
    23   val cert_classrel: theory -> class * class -> class * class
    24   val read_classrel: theory -> xstring * xstring -> class * class
    25   val declare_overloaded: string * typ -> theory -> term * theory
    26   val define_overloaded: binding -> string * term -> theory -> thm * theory
    27   val add_classrel: thm -> theory -> theory
    28   val add_arity: thm -> theory -> theory
    29   val prove_classrel: class * class -> (Proof.context -> tactic) -> theory -> theory
    30   val prove_arity: string * sort list * sort -> (Proof.context -> tactic) -> theory -> theory
    31   val define_class: binding * class list -> string list ->
    32     (Thm.binding * term list) list -> theory -> class * theory
    33   val classrel_axiomatization: (class * class) list -> theory -> theory
    34   val arity_axiomatization: arity -> theory -> theory
    35   val class_axiomatization: binding * class list -> theory -> theory
    36 end;
    37 
    38 structure Axclass: AXCLASS =
    39 struct
    40 
    41 (** theory data **)
    42 
    43 (* axclass info *)
    44 
    45 type info =
    46  {def: thm,
    47   intro: thm,
    48   axioms: thm list,
    49   params: (string * typ) list};
    50 
    51 fun make_axclass (def, intro, axioms, params): info =
    52   {def = def, intro = intro, axioms = axioms, params = params};
    53 
    54 
    55 (* class parameters (canonical order) *)
    56 
    57 type param = string * class;
    58 
    59 fun add_param ctxt ((x, c): param) params =
    60   (case AList.lookup (op =) params x of
    61     NONE => (x, c) :: params
    62   | SOME c' =>
    63       error ("Duplicate class parameter " ^ quote x ^ " for " ^ Syntax.string_of_sort ctxt [c] ^
    64         (if c = c' then "" else " and " ^ Syntax.string_of_sort ctxt [c'])));
    65 
    66 
    67 (* setup data *)
    68 
    69 datatype data = Data of
    70  {axclasses: info Symtab.table,
    71   params: param list,
    72   proven_classrels: thm Symreltab.table,
    73   proven_arities: ((class * sort list) * (thm * string)) list Symtab.table,
    74     (*arity theorems with theory name*)
    75   inst_params:
    76     (string * thm) Symtab.table Symtab.table *
    77       (*constant name ~> type constructor ~> (constant name, equation)*)
    78     (string * string) Symtab.table (*constant name ~> (constant name, type constructor)*)};
    79 
    80 fun make_data
    81     (axclasses, params, proven_classrels, proven_arities, inst_params) =
    82   Data {axclasses = axclasses, params = params, proven_classrels = proven_classrels,
    83     proven_arities = proven_arities, inst_params = inst_params};
    84 
    85 structure Data = Theory_Data_PP
    86 (
    87   type T = data;
    88   val empty =
    89     make_data (Symtab.empty, [], Symreltab.empty, Symtab.empty, (Symtab.empty, Symtab.empty));
    90   val extend = I;
    91   fun merge pp
    92       (Data {axclasses = axclasses1, params = params1, proven_classrels = proven_classrels1,
    93         proven_arities = proven_arities1, inst_params = inst_params1},
    94        Data {axclasses = axclasses2, params = params2, proven_classrels = proven_classrels2,
    95         proven_arities = proven_arities2, inst_params = inst_params2}) =
    96     let
    97       val ctxt = Syntax.init_pretty pp;
    98 
    99       val axclasses' = Symtab.merge (K true) (axclasses1, axclasses2);
   100       val params' =
   101         if null params1 then params2
   102         else
   103           fold_rev (fn p => if member (op =) params1 p then I else add_param ctxt p)
   104             params2 params1;
   105 
   106       (*see Theory.at_begin hook for transitive closure of classrels and arity completion*)
   107       val proven_classrels' = Symreltab.merge (K true) (proven_classrels1, proven_classrels2);
   108       val proven_arities' = Symtab.merge_list (eq_fst op =) (proven_arities1, proven_arities2);
   109 
   110       val inst_params' =
   111         (Symtab.join (K (Symtab.merge (K true))) (#1 inst_params1, #1 inst_params2),
   112           Symtab.merge (K true) (#2 inst_params1, #2 inst_params2));
   113     in
   114       make_data (axclasses', params', proven_classrels', proven_arities', inst_params')
   115     end;
   116 );
   117 
   118 fun map_data f =
   119   Data.map (fn Data {axclasses, params, proven_classrels, proven_arities, inst_params} =>
   120     make_data (f (axclasses, params, proven_classrels, proven_arities, inst_params)));
   121 
   122 fun map_axclasses f =
   123   map_data (fn (axclasses, params, proven_classrels, proven_arities, inst_params) =>
   124     (f axclasses, params, proven_classrels, proven_arities, inst_params));
   125 
   126 fun map_params f =
   127   map_data (fn (axclasses, params, proven_classrels, proven_arities, inst_params) =>
   128     (axclasses, f params, proven_classrels, proven_arities, inst_params));
   129 
   130 fun map_proven_classrels f =
   131   map_data (fn (axclasses, params, proven_classrels, proven_arities, inst_params) =>
   132     (axclasses, params, f proven_classrels, proven_arities, inst_params));
   133 
   134 fun map_proven_arities f =
   135   map_data (fn (axclasses, params, proven_classrels, proven_arities, inst_params) =>
   136     (axclasses, params, proven_classrels, f proven_arities, inst_params));
   137 
   138 fun map_inst_params f =
   139   map_data (fn (axclasses, params, proven_classrels, proven_arities, inst_params) =>
   140     (axclasses, params, proven_classrels, proven_arities, f inst_params));
   141 
   142 val rep_data = Data.get #> (fn Data args => args);
   143 
   144 val axclasses_of = #axclasses o rep_data;
   145 val params_of = #params o rep_data;
   146 val proven_classrels_of = #proven_classrels o rep_data;
   147 val proven_arities_of = #proven_arities o rep_data;
   148 val inst_params_of = #inst_params o rep_data;
   149 
   150 
   151 (* axclasses with parameters *)
   152 
   153 fun get_info thy c =
   154   (case Symtab.lookup (axclasses_of thy) c of
   155     SOME info => info
   156   | NONE => error ("No such axclass: " ^ quote c));
   157 
   158 fun all_params_of thy S =
   159   let val params = params_of thy;
   160   in fold (fn (x, c) => if Sign.subsort thy (S, [c]) then cons x else I) params [] end;
   161 
   162 fun class_of_param thy = AList.lookup (op =) (params_of thy);
   163 
   164 
   165 (* maintain instances *)
   166 
   167 val classrel_prefix = "classrel_";
   168 val arity_prefix = "arity_";
   169 
   170 fun instance_name (a, c) = Long_Name.base_name c ^ "_" ^ Long_Name.base_name a;
   171 
   172 
   173 val update_classrel = map_proven_classrels o Symreltab.update;
   174 
   175 val is_classrel = Symreltab.defined o proven_classrels_of;
   176 
   177 fun the_classrel thy (c1, c2) =
   178   (case Symreltab.lookup (proven_classrels_of thy) (c1, c2) of
   179     SOME thm => Thm.transfer thy thm
   180   | NONE => error ("Unproven class relation " ^
   181       Syntax.string_of_classrel (Proof_Context.init_global thy) [c1, c2]));
   182 
   183 fun complete_classrels thy =
   184   let
   185     fun complete (c, (_, (all_preds, all_succs))) (finished1, thy1) =
   186       let
   187         fun compl c1 c2 (finished2, thy2) =
   188           if is_classrel thy2 (c1, c2) then (finished2, thy2)
   189           else
   190             (false,
   191               thy2
   192               |> update_classrel ((c1, c2),
   193                 (the_classrel thy2 (c1, c) RS the_classrel thy2 (c, c2))
   194                 |> Thm.instantiate' [SOME (Thm.global_ctyp_of thy2 (TVar ((Name.aT, 0), [])))] []
   195                 |> Thm.close_derivation));
   196 
   197         val proven = is_classrel thy1;
   198         val preds = Graph.Keys.fold (fn c1 => proven (c1, c) ? cons c1) all_preds [];
   199         val succs = Graph.Keys.fold (fn c2 => proven (c, c2) ? cons c2) all_succs [];
   200       in
   201         fold_product compl preds succs (finished1, thy1)
   202       end;
   203   in
   204     (case Graph.fold complete (Sorts.classes_of (Sign.classes_of thy)) (true, thy) of
   205       (true, _) => NONE
   206     | (_, thy') => SOME thy')
   207   end;
   208 
   209 
   210 fun the_arity thy (a, Ss, c) =
   211   (case AList.lookup (op =) (Symtab.lookup_list (proven_arities_of thy) a) (c, Ss) of
   212     SOME (thm, _) => Thm.transfer thy thm
   213   | NONE => error ("Unproven type arity " ^
   214       Syntax.string_of_arity (Proof_Context.init_global thy) (a, Ss, [c])));
   215 
   216 fun thynames_of_arity thy (a, c) =
   217   Symtab.lookup_list (proven_arities_of thy) a
   218   |> map_filter (fn ((c', _), (_, name)) => if c = c' then SOME name else NONE)
   219   |> rev;
   220 
   221 fun insert_arity_completions thy t ((c, Ss), ((th, thy_name))) (finished, arities) =
   222   let
   223     val algebra = Sign.classes_of thy;
   224     val ars = Symtab.lookup_list arities t;
   225     val super_class_completions =
   226       Sign.super_classes thy c
   227       |> filter_out (fn c1 => exists (fn ((c2, Ss2), _) =>
   228             c1 = c2 andalso Sorts.sorts_le algebra (Ss2, Ss)) ars);
   229 
   230     val names = Name.invent Name.context Name.aT (length Ss);
   231     val std_vars = map (fn a => SOME (Thm.global_ctyp_of thy (TVar ((a, 0), [])))) names;
   232 
   233     val completions = super_class_completions |> map (fn c1 =>
   234       let
   235         val th1 =
   236           (th RS the_classrel thy (c, c1))
   237           |> Thm.instantiate' std_vars []
   238           |> Thm.close_derivation;
   239       in ((th1, thy_name), c1) end);
   240 
   241     val finished' = finished andalso null completions;
   242     val arities' = fold (fn (th, c1) => Symtab.cons_list (t, ((c1, Ss), th))) completions arities;
   243   in (finished', arities') end;
   244 
   245 fun put_arity_completion ((t, Ss, c), th) thy =
   246   let val ar = ((c, Ss), (th, Context.theory_name thy)) in
   247     thy
   248     |> map_proven_arities
   249       (Symtab.insert_list (eq_fst op =) (t, ar) #>
   250        curry (insert_arity_completions thy t ar) true #> #2)
   251   end;
   252 
   253 fun complete_arities thy =
   254   let
   255     val arities = proven_arities_of thy;
   256     val (finished, arities') =
   257       Symtab.fold (fn (t, ars) => fold (insert_arity_completions thy t) ars)
   258         arities (true, arities);
   259   in
   260     if finished then NONE
   261     else SOME (map_proven_arities (K arities') thy)
   262   end;
   263 
   264 val _ = Theory.setup
   265   (Theory.at_begin complete_classrels #> Theory.at_begin complete_arities);
   266 
   267 val _ = Proofterm.install_axclass_proofs
   268   {classrel_proof = Thm.proof_of oo the_classrel,
   269    arity_proof = Thm.proof_of oo the_arity};
   270 
   271 
   272 (* maintain instance parameters *)
   273 
   274 fun get_inst_param thy (c, tyco) =
   275   (case Symtab.lookup (the_default Symtab.empty (Symtab.lookup (#1 (inst_params_of thy)) c)) tyco of
   276     SOME c' => c'
   277   | NONE => error ("No instance parameter for constant " ^ quote c ^ " on type " ^ quote tyco));
   278 
   279 fun add_inst_param (c, tyco) inst =
   280   (map_inst_params o apfst o Symtab.map_default (c, Symtab.empty)) (Symtab.update_new (tyco, inst))
   281   #> (map_inst_params o apsnd) (Symtab.update_new (#1 inst, (c, tyco)));
   282 
   283 val inst_of_param = Symtab.lookup o #2 o inst_params_of;
   284 val param_of_inst = #1 oo get_inst_param;
   285 
   286 fun inst_thms thy =
   287   Symtab.fold (Symtab.fold (cons o #2 o #2) o #2) (#1 (inst_params_of thy)) [];
   288 
   289 fun get_inst_tyco consts = try (#1 o dest_Type o the_single o Consts.typargs consts);
   290 
   291 fun unoverload thy =
   292   rewrite_rule (Proof_Context.init_global thy) (inst_thms thy);
   293 
   294 fun overload thy =
   295   rewrite_rule (Proof_Context.init_global thy) (map Thm.symmetric (inst_thms thy));
   296 
   297 fun unoverload_conv thy =
   298   Raw_Simplifier.rewrite (Proof_Context.init_global thy) true (inst_thms thy);
   299 
   300 fun overload_conv thy =
   301   Raw_Simplifier.rewrite (Proof_Context.init_global thy) true (map Thm.symmetric (inst_thms thy));
   302 
   303 fun lookup_inst_param consts params (c, T) =
   304   (case get_inst_tyco consts (c, T) of
   305     SOME tyco => AList.lookup (op =) params (c, tyco)
   306   | NONE => NONE);
   307 
   308 fun unoverload_const thy (c_ty as (c, _)) =
   309   if is_some (class_of_param thy c) then
   310     (case get_inst_tyco (Sign.consts_of thy) c_ty of
   311       SOME tyco => try (param_of_inst thy) (c, tyco) |> the_default c
   312     | NONE => c)
   313   else c;
   314 
   315 
   316 
   317 (** instances **)
   318 
   319 (* class relations *)
   320 
   321 fun cert_classrel thy raw_rel =
   322   let
   323     val string_of_sort = Syntax.string_of_sort_global thy;
   324     val (c1, c2) = apply2 (Sign.certify_class thy) raw_rel;
   325     val _ = Sign.primitive_classrel (c1, c2) thy;
   326     val _ =
   327       (case subtract (op =) (all_params_of thy [c1]) (all_params_of thy [c2]) of
   328         [] => ()
   329       | xs => raise TYPE ("Class " ^ string_of_sort [c1] ^ " lacks parameter(s) " ^
   330           commas_quote xs ^ " of " ^ string_of_sort [c2], [], []));
   331   in (c1, c2) end;
   332 
   333 fun read_classrel thy raw_rel =
   334   cert_classrel thy (apply2 (Proof_Context.read_class (Proof_Context.init_global thy)) raw_rel)
   335     handle TYPE (msg, _, _) => error msg;
   336 
   337 
   338 (* declaration and definition of instances of overloaded constants *)
   339 
   340 fun inst_tyco_of thy (c, T) =
   341   (case get_inst_tyco (Sign.consts_of thy) (c, T) of
   342     SOME tyco => tyco
   343   | NONE => error ("Illegal type for instantiation of class parameter: " ^
   344       quote (c ^ " :: " ^ Syntax.string_of_typ_global thy T)));
   345 
   346 fun declare_overloaded (c, T) thy =
   347   let
   348     val class =
   349       (case class_of_param thy c of
   350         SOME class => class
   351       | NONE => error ("Not a class parameter: " ^ quote c));
   352     val tyco = inst_tyco_of thy (c, T);
   353     val name_inst = instance_name (tyco, class) ^ "_inst";
   354     val c' = instance_name (tyco, c);
   355     val T' = Type.strip_sorts T;
   356   in
   357     thy
   358     |> Sign.qualified_path true (Binding.name name_inst)
   359     |> Sign.declare_const_global ((Binding.name c', T'), NoSyn)
   360     |-> (fn const' as Const (c'', _) =>
   361       Thm.add_def_global false true
   362         (Binding.name (Thm.def_name c'), Logic.mk_equals (Const (c, T'), const'))
   363       #>> apsnd Thm.varifyT_global
   364       #-> (fn (_, thm) => add_inst_param (c, tyco) (c'', thm)
   365         #> Global_Theory.add_thm ((Binding.concealed (Binding.name c'), thm), [])
   366         #> #2
   367         #> pair (Const (c, T))))
   368     ||> Sign.restore_naming thy
   369   end;
   370 
   371 fun define_overloaded b (c, t) thy =
   372   let
   373     val T = Term.fastype_of t;
   374     val tyco = inst_tyco_of thy (c, T);
   375     val (c', eq) = get_inst_param thy (c, tyco);
   376     val prop = Logic.mk_equals (Const (c', T), t);
   377     val b' = Thm.def_binding_optional (Binding.name (instance_name (tyco, c))) b;
   378   in
   379     thy
   380     |> Thm.add_def_global false false (b', prop)
   381     |>> (fn (_, thm) => Drule.transitive_thm OF [eq, thm])
   382   end;
   383 
   384 
   385 (* primitive rules *)
   386 
   387 fun add_classrel raw_th thy =
   388   let
   389     val th = Thm.strip_shyps (Thm.transfer thy raw_th);
   390     val prop = Thm.plain_prop_of th;
   391     fun err () = raise THM ("add_classrel: malformed class relation", 0, [th]);
   392     val rel = Logic.dest_classrel prop handle TERM _ => err ();
   393     val (c1, c2) = cert_classrel thy rel handle TYPE _ => err ();
   394     val binding =
   395       Binding.concealed (Binding.name (prefix classrel_prefix (Logic.name_classrel (c1, c2))));
   396     val (th', thy') = Global_Theory.store_thm (binding, th) thy;
   397     val th'' = th'
   398       |> Thm.unconstrainT
   399       |> Thm.instantiate' [SOME (Thm.global_ctyp_of thy' (TVar ((Name.aT, 0), [])))] [];
   400   in
   401     thy'
   402     |> Sign.primitive_classrel (c1, c2)
   403     |> map_proven_classrels (Symreltab.update ((c1, c2), th''))
   404     |> perhaps complete_classrels
   405     |> perhaps complete_arities
   406   end;
   407 
   408 fun add_arity raw_th thy =
   409   let
   410     val th = Thm.strip_shyps (Thm.transfer thy raw_th);
   411     val prop = Thm.plain_prop_of th;
   412     fun err () = raise THM ("add_arity: malformed type arity", 0, [th]);
   413     val arity as (t, Ss, c) = Logic.dest_arity prop handle TERM _ => err ();
   414 
   415     val binding =
   416       Binding.concealed (Binding.name (prefix arity_prefix (Logic.name_arity arity)));
   417     val (th', thy') = Global_Theory.store_thm (binding, th) thy;
   418 
   419     val args = Name.invent_names Name.context Name.aT Ss;
   420     val T = Type (t, map TFree args);
   421     val std_vars = map (fn (a, S) => SOME (Thm.global_ctyp_of thy' (TVar ((a, 0), [])))) args;
   422 
   423     val missing_params = Sign.complete_sort thy' [c]
   424       |> maps (these o Option.map #params o try (get_info thy'))
   425       |> filter_out (fn (const, _) => can (get_inst_param thy') (const, t))
   426       |> (map o apsnd o map_atyps) (K T);
   427     val th'' = th'
   428       |> Thm.unconstrainT
   429       |> Thm.instantiate' std_vars [];
   430   in
   431     thy'
   432     |> fold (#2 oo declare_overloaded) missing_params
   433     |> Sign.primitive_arity (t, Ss, [c])
   434     |> put_arity_completion ((t, Ss, c), th'')
   435   end;
   436 
   437 
   438 (* tactical proofs *)
   439 
   440 fun prove_classrel raw_rel tac thy =
   441   let
   442     val ctxt = Proof_Context.init_global thy;
   443     val (c1, c2) = cert_classrel thy raw_rel;
   444     val th =
   445       Goal.prove ctxt [] [] (Logic.mk_classrel (c1, c2)) (fn {context, ...} => tac context)
   446         handle ERROR msg =>
   447           cat_error msg ("The error(s) above occurred while trying to prove class relation " ^
   448             quote (Syntax.string_of_classrel ctxt [c1, c2]));
   449   in
   450     thy |> add_classrel th
   451   end;
   452 
   453 fun prove_arity raw_arity tac thy =
   454   let
   455     val ctxt = Proof_Context.init_global thy;
   456     val arity = Proof_Context.cert_arity ctxt raw_arity;
   457     val names = map (prefix arity_prefix) (Logic.name_arities arity);
   458     val props = Logic.mk_arities arity;
   459     val ths =
   460       Goal.prove_common ctxt NONE [] [] props
   461       (fn {context, ...} => Goal.precise_conjunction_tac (length props) 1 THEN tac context)
   462         handle ERROR msg =>
   463           cat_error msg ("The error(s) above occurred while trying to prove type arity " ^
   464             quote (Syntax.string_of_arity ctxt arity));
   465   in
   466     thy |> fold add_arity ths
   467   end;
   468 
   469 
   470 
   471 (** class definitions **)
   472 
   473 fun split_defined n eq =
   474   let
   475     val intro =
   476       (eq RS Drule.equal_elim_rule2)
   477       |> Conjunction.curry_balanced n
   478       |> n = 0 ? Thm.eq_assumption 1;
   479     val dests =
   480       if n = 0 then []
   481       else
   482         (eq RS Drule.equal_elim_rule1)
   483         |> Balanced_Tree.dest (fn th =>
   484           (th RS Conjunction.conjunctionD1, th RS Conjunction.conjunctionD2)) n;
   485   in (intro, dests) end;
   486 
   487 fun define_class (bclass, raw_super) raw_params raw_specs thy =
   488   let
   489     val ctxt = Syntax.init_pretty_global thy;
   490 
   491 
   492     (* class *)
   493 
   494     val bconst = Binding.map_name Logic.const_of_class bclass;
   495     val class = Sign.full_name thy bclass;
   496     val super = Sign.minimize_sort thy (Sign.certify_sort thy raw_super);
   497 
   498     fun check_constraint (a, S) =
   499       if Sign.subsort thy (super, S) then ()
   500       else error ("Sort constraint of type variable " ^
   501         Syntax.string_of_typ (Config.put show_sorts true ctxt) (TFree (a, S)) ^
   502         " needs to be weaker than " ^ Syntax.string_of_sort ctxt super);
   503 
   504 
   505     (* params *)
   506 
   507     val params = raw_params |> map (fn p =>
   508       let
   509         val T = Sign.the_const_type thy p;
   510         val _ =
   511           (case Term.add_tvarsT T [] of
   512             [((a, _), S)] => check_constraint (a, S)
   513           | _ => error ("Exactly one type variable expected in class parameter " ^ quote p));
   514         val T' = Term.map_type_tvar (fn _ => TFree (Name.aT, [class])) T;
   515       in (p, T') end);
   516 
   517 
   518     (* axioms *)
   519 
   520     fun prep_axiom t =
   521       (case Term.add_tfrees t [] of
   522         [(a, S)] => check_constraint (a, S)
   523       | [] => ()
   524       | _ => error ("Multiple type variables in class axiom:\n" ^ Syntax.string_of_term ctxt t);
   525       t
   526       |> Term.map_types (Term.map_atyps (fn TFree _ => Term.aT [] | U => U))
   527       |> Logic.close_form);
   528 
   529     val axiomss = map (map (prep_axiom o Sign.cert_prop thy) o snd) raw_specs;
   530     val name_atts = map fst raw_specs;
   531 
   532 
   533     (* definition *)
   534 
   535     val conjs = Logic.mk_of_sort (Term.aT [], super) @ flat axiomss;
   536     val class_eq =
   537       Logic.mk_equals (Logic.mk_of_class (Term.aT [], class), Logic.mk_conjunction_balanced conjs);
   538 
   539     val ([def], def_thy) =
   540       thy
   541       |> Sign.primitive_class (bclass, super)
   542       |> Global_Theory.add_defs false [((Thm.def_binding bconst, class_eq), [])];
   543     val (raw_intro, (raw_classrel, raw_axioms)) =
   544       split_defined (length conjs) def ||> chop (length super);
   545 
   546 
   547     (* facts *)
   548 
   549     val class_triv = Thm.class_triv def_thy class;
   550     val ([(_, [intro]), (_, classrel), (_, axioms)], facts_thy) =
   551       def_thy
   552       |> Sign.qualified_path true bconst
   553       |> Global_Theory.note_thmss ""
   554         [((Binding.name "intro", []), [([Drule.export_without_context raw_intro], [])]),
   555          ((Binding.name "super", []), [(map Drule.export_without_context raw_classrel, [])]),
   556          ((Binding.name "axioms", []),
   557            [(map (fn th => Drule.export_without_context (class_triv RS th)) raw_axioms, [])])]
   558       ||> Sign.restore_naming def_thy;
   559 
   560 
   561     (* result *)
   562 
   563     val axclass = make_axclass (def, intro, axioms, params);
   564     val result_thy =
   565       facts_thy
   566       |> map_proven_classrels
   567           (fold2 (fn c => fn th => Symreltab.update ((class, c), th)) super classrel)
   568       |> perhaps complete_classrels
   569       |> Sign.qualified_path false bconst
   570       |> Global_Theory.note_thmss "" (name_atts ~~ map Thm.simple_fact (unflat axiomss axioms))
   571       |> #2
   572       |> Sign.restore_naming facts_thy
   573       |> map_axclasses (Symtab.update (class, axclass))
   574       |> map_params (fold (fn (x, _) => add_param ctxt (x, class)) params);
   575 
   576   in (class, result_thy) end;
   577 
   578 
   579 
   580 (** axiomatizations **)
   581 
   582 local
   583 
   584 (*old-style axioms*)
   585 fun add_axioms prep mk name add raw_args thy =
   586   let
   587     val args = prep thy raw_args;
   588     val specs = mk args;
   589     val names = name args;
   590   in
   591     thy
   592     |> fold_map Thm.add_axiom_global (map Binding.name names ~~ specs)
   593     |-> fold (add o Drule.export_without_context o snd)
   594   end;
   595 
   596 fun class_const c =
   597   (Logic.const_of_class c, Term.itselfT (Term.aT []) --> propT);
   598 
   599 in
   600 
   601 val classrel_axiomatization =
   602   add_axioms (map o cert_classrel) (map Logic.mk_classrel)
   603     (map (prefix classrel_prefix o Logic.name_classrel)) add_classrel;
   604 
   605 val arity_axiomatization =
   606   add_axioms (Proof_Context.cert_arity o Proof_Context.init_global) Logic.mk_arities
   607     (map (prefix arity_prefix) o Logic.name_arities) add_arity;
   608 
   609 fun class_axiomatization (bclass, raw_super) thy =
   610   let
   611     val class = Sign.full_name thy bclass;
   612     val super = map (Sign.certify_class thy) raw_super |> Sign.minimize_sort thy;
   613   in
   614     thy
   615     |> Sign.primitive_class (bclass, super)
   616     |> classrel_axiomatization (map (fn c => (class, c)) super)
   617     |> Theory.add_deps_global "" (class_const class) (map class_const super)
   618   end;
   619 
   620 end;
   621 
   622 end;