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