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