src/Pure/axclass.ML
author wenzelm
Mon May 07 00:49:59 2007 +0200 (2007-05-07)
changeset 22846 fb79144af9a3
parent 22745 17bc6af2011e
child 23421 c9007fc4a646
permissions -rw-r--r--
simplified DataFun interfaces;
wenzelm@404
     1
(*  Title:      Pure/axclass.ML
wenzelm@404
     2
    ID:         $Id$
wenzelm@404
     3
    Author:     Markus Wenzel, TU Muenchen
wenzelm@404
     4
wenzelm@19511
     5
Type classes as parameter records and predicates, with explicit
wenzelm@19511
     6
definitions and proofs.
wenzelm@404
     7
*)
wenzelm@404
     8
wenzelm@404
     9
signature AX_CLASS =
wenzelm@3938
    10
sig
haftmann@21463
    11
  val get_definition: theory -> class -> {def: thm, intro: thm, axioms: thm list,
haftmann@21925
    12
    params: (string * typ) list}
wenzelm@19243
    13
  val class_intros: theory -> thm list
haftmann@20107
    14
  val class_of_param: theory -> string -> class option
haftmann@21463
    15
  val params_of_class: theory -> class -> string * (string * typ) list
haftmann@21953
    16
  val param_tyvarname: string
wenzelm@19511
    17
  val print_axclasses: theory -> unit
wenzelm@19405
    18
  val cert_classrel: theory -> class * class -> class * class
wenzelm@19405
    19
  val read_classrel: theory -> xstring * xstring -> class * class
wenzelm@19405
    20
  val add_classrel: thm -> theory -> theory
wenzelm@19405
    21
  val add_arity: thm -> theory -> theory
wenzelm@19405
    22
  val prove_classrel: class * class -> tactic -> theory -> theory
wenzelm@19243
    23
  val prove_arity: string * sort list * sort -> tactic -> theory -> theory
haftmann@22745
    24
  val define_class: bstring * class list -> string list ->
wenzelm@19418
    25
    ((bstring * attribute list) * term list) list -> theory -> class * theory
wenzelm@19511
    26
  val axiomatize_class: bstring * xstring list -> theory -> theory
wenzelm@19511
    27
  val axiomatize_class_i: bstring * class list -> theory -> theory
wenzelm@19511
    28
  val axiomatize_classrel: (xstring * xstring) list -> theory -> theory
wenzelm@19511
    29
  val axiomatize_classrel_i: (class * class) list -> theory -> theory
wenzelm@19511
    30
  val axiomatize_arity: xstring * string list * string -> theory -> theory
wenzelm@19511
    31
  val axiomatize_arity_i: arity -> theory -> theory
wenzelm@19574
    32
  type cache
wenzelm@19574
    33
  val cache: cache
wenzelm@19574
    34
  val of_sort: theory -> typ * sort -> cache -> thm list * cache  (*exception Sorts.CLASS_ERROR*)
wenzelm@3938
    35
end;
wenzelm@404
    36
wenzelm@15801
    37
structure AxClass: AX_CLASS =
wenzelm@404
    38
struct
wenzelm@404
    39
wenzelm@19405
    40
(** theory data **)
wenzelm@423
    41
wenzelm@19405
    42
(* class parameters (canonical order) *)
wenzelm@423
    43
wenzelm@19405
    44
type param = string * class;
wenzelm@423
    45
wenzelm@19405
    46
fun add_param pp ((x, c): param) params =
wenzelm@19405
    47
  (case AList.lookup (op =) params x of
wenzelm@19405
    48
    NONE => (x, c) :: params
wenzelm@19405
    49
  | SOME c' => error ("Duplicate class parameter " ^ quote x ^
wenzelm@19405
    50
      " for " ^ Pretty.string_of_sort pp [c] ^
wenzelm@19405
    51
      (if c = c' then "" else " and " ^ Pretty.string_of_sort pp [c'])));
wenzelm@423
    52
wenzelm@19405
    53
fun merge_params _ ([], qs) = qs
wenzelm@19405
    54
  | merge_params pp (ps, qs) =
wenzelm@19405
    55
      fold_rev (fn q => if member (op =) ps q then I else add_param pp q) qs ps;
wenzelm@423
    56
wenzelm@423
    57
wenzelm@19511
    58
(* axclasses *)
wenzelm@6379
    59
wenzelm@19243
    60
val introN = "intro";
wenzelm@19511
    61
val superN = "super";
wenzelm@19243
    62
val axiomsN = "axioms";
wenzelm@19243
    63
haftmann@21463
    64
val param_tyvarname = "'a";
haftmann@21463
    65
wenzelm@19392
    66
datatype axclass = AxClass of
wenzelm@19460
    67
 {def: thm,
wenzelm@19392
    68
  intro: thm,
haftmann@21463
    69
  axioms: thm list,
haftmann@21925
    70
  params: (string * typ) list};
wenzelm@19392
    71
wenzelm@19460
    72
type axclasses = axclass Symtab.table * param list;
wenzelm@19392
    73
haftmann@21925
    74
fun make_axclass ((def, intro, axioms), params) =
haftmann@21925
    75
  AxClass {def = def, intro = intro, axioms = axioms, params = params};
wenzelm@19405
    76
wenzelm@19405
    77
fun merge_axclasses pp ((tab1, params1), (tab2, params2)) : axclasses =
wenzelm@19405
    78
  (Symtab.merge (K true) (tab1, tab2), merge_params pp (params1, params2));
wenzelm@19405
    79
wenzelm@19392
    80
wenzelm@19392
    81
(* instances *)
wenzelm@19392
    82
wenzelm@20628
    83
val classrel_prefix = "classrel_";
wenzelm@20628
    84
val arity_prefix = "arity_";
wenzelm@19511
    85
wenzelm@19574
    86
type instances =
wenzelm@19574
    87
  ((class * class) * thm) list *
wenzelm@19574
    88
  ((class * sort list) * thm) list Symtab.table;
wenzelm@6379
    89
wenzelm@19574
    90
fun merge_instances ((classrel1, arities1): instances, (classrel2, arities2)) =
wenzelm@19574
    91
 (merge (eq_fst op =) (classrel1, classrel2),
wenzelm@19574
    92
  Symtab.join (K (merge (eq_fst op =))) (arities1, arities2));
wenzelm@19392
    93
wenzelm@19392
    94
wenzelm@19511
    95
(* setup data *)
wenzelm@19392
    96
wenzelm@19392
    97
structure AxClassData = TheoryDataFun
wenzelm@22846
    98
(
wenzelm@19574
    99
  type T = axclasses * instances;
wenzelm@22846
   100
  val empty = ((Symtab.empty, []), ([], Symtab.empty));
wenzelm@19574
   101
  val copy = I;
wenzelm@19574
   102
  val extend = I;
wenzelm@19574
   103
  fun merge pp ((axclasses1, instances1), (axclasses2, instances2)) =
wenzelm@19574
   104
    (merge_axclasses pp (axclasses1, axclasses2), (merge_instances (instances1, instances2)));
wenzelm@22846
   105
);
wenzelm@6379
   106
wenzelm@6379
   107
wenzelm@19574
   108
(* maintain axclasses *)
wenzelm@19392
   109
wenzelm@19574
   110
val get_axclasses = #1 o AxClassData.get;
wenzelm@19574
   111
fun map_axclasses f = AxClassData.map (apfst f);
wenzelm@19574
   112
wenzelm@19574
   113
val lookup_def = Symtab.lookup o #1 o get_axclasses;
wenzelm@6379
   114
wenzelm@19511
   115
fun get_definition thy c =
wenzelm@19511
   116
  (case lookup_def thy c of
wenzelm@19392
   117
    SOME (AxClass info) => info
wenzelm@21919
   118
  | NONE => error ("No such axclass: " ^ quote c));
wenzelm@6379
   119
wenzelm@19123
   120
fun class_intros thy =
wenzelm@19392
   121
  let
wenzelm@19392
   122
    fun add_intro c =
wenzelm@19511
   123
      (case lookup_def thy c of SOME (AxClass {intro, ...}) => cons intro | _ => I);
wenzelm@21931
   124
    val classes = Sign.all_classes thy;
wenzelm@19392
   125
  in map (Thm.class_triv thy) classes @ fold add_intro classes [] end;
wenzelm@15876
   126
wenzelm@15876
   127
wenzelm@19460
   128
fun get_params thy pred =
wenzelm@19574
   129
  let val params = #2 (get_axclasses thy);
wenzelm@19460
   130
  in fold (fn (x, c) => if pred c then cons x else I) params [] end;
wenzelm@19460
   131
wenzelm@19460
   132
fun params_of thy c = get_params thy (fn c' => c' = c);
wenzelm@19460
   133
fun all_params_of thy S = get_params thy (fn c => Sign.subsort thy (S, [c]));
wenzelm@19460
   134
haftmann@20107
   135
fun class_of_param thy =
haftmann@19955
   136
  AList.lookup (op =) (#2 (get_axclasses thy));
haftmann@19955
   137
haftmann@21463
   138
fun params_of_class thy class =
haftmann@21463
   139
  (param_tyvarname, (#params o get_definition thy) class);
wenzelm@19460
   140
wenzelm@21919
   141
wenzelm@19511
   142
(* maintain instances *)
wenzelm@19503
   143
wenzelm@19574
   144
val get_instances = #2 o AxClassData.get;
wenzelm@19574
   145
fun map_instances f = AxClassData.map (apsnd f);
wenzelm@19528
   146
wenzelm@19528
   147
wenzelm@19574
   148
fun the_classrel thy (c1, c2) =
wenzelm@19574
   149
  (case AList.lookup (op =) (#1 (get_instances thy)) (c1, c2) of
wenzelm@19574
   150
    SOME th => Thm.transfer thy th
wenzelm@19574
   151
  | NONE => error ("Unproven class relation " ^ Sign.string_of_classrel thy [c1, c2]));
wenzelm@19574
   152
wenzelm@19574
   153
fun put_classrel arg = map_instances (fn (classrel, arities) =>
wenzelm@19574
   154
  (insert (eq_fst op =) arg classrel, arities));
wenzelm@19503
   155
wenzelm@19503
   156
wenzelm@19574
   157
fun the_arity thy a (c, Ss) =
wenzelm@19574
   158
  (case AList.lookup (op =) (Symtab.lookup_list (#2 (get_instances thy)) a) (c, Ss)  of
wenzelm@19574
   159
    SOME th => Thm.transfer thy th
wenzelm@19574
   160
  | NONE => error ("Unproven type arity " ^ Sign.string_of_arity thy (a, Ss, [c])));
wenzelm@19503
   161
wenzelm@19574
   162
fun put_arity ((t, Ss, c), th) = map_instances (fn (classrel, arities) =>
wenzelm@19574
   163
  (classrel, arities |> Symtab.insert_list (eq_fst op =) (t, ((c, Ss), th))));
wenzelm@19503
   164
wenzelm@19503
   165
wenzelm@19511
   166
(* print data *)
wenzelm@19460
   167
wenzelm@19460
   168
fun print_axclasses thy =
wenzelm@19460
   169
  let
wenzelm@19574
   170
    val axclasses = #1 (get_axclasses thy);
wenzelm@19460
   171
    val ctxt = ProofContext.init thy;
wenzelm@19460
   172
haftmann@21925
   173
    fun pretty_axclass (class, AxClass {def, intro, axioms, params}) =
wenzelm@19460
   174
      Pretty.block (Pretty.fbreaks
wenzelm@19460
   175
       [Pretty.block
wenzelm@19511
   176
          [Pretty.str "class ", ProofContext.pretty_sort ctxt [class], Pretty.str ":"],
wenzelm@19460
   177
        Pretty.strs ("parameters:" :: params_of thy class),
wenzelm@19460
   178
        ProofContext.pretty_fact ctxt ("def", [def]),
wenzelm@19460
   179
        ProofContext.pretty_fact ctxt (introN, [intro]),
wenzelm@19460
   180
        ProofContext.pretty_fact ctxt (axiomsN, axioms)]);
wenzelm@19460
   181
  in Pretty.writeln (Pretty.chunks (map pretty_axclass (Symtab.dest axclasses))) end;
wenzelm@19460
   182
wenzelm@19460
   183
wenzelm@404
   184
wenzelm@19511
   185
(** instances **)
wenzelm@19418
   186
wenzelm@19418
   187
(* class relations *)
wenzelm@19418
   188
wenzelm@19405
   189
fun cert_classrel thy raw_rel =
wenzelm@15876
   190
  let
wenzelm@19405
   191
    val (c1, c2) = pairself (Sign.certify_class thy) raw_rel;
wenzelm@19511
   192
    val _ = Type.add_classrel (Sign.pp thy) (c1, c2) (Sign.tsig_of thy);
wenzelm@19405
   193
    val _ =
wenzelm@19460
   194
      (case subtract (op =) (all_params_of thy [c1]) (all_params_of thy [c2]) of
wenzelm@19405
   195
        [] => ()
wenzelm@19405
   196
      | xs => raise TYPE ("Class " ^ Sign.string_of_sort thy [c1] ^ " lacks parameter(s) " ^
wenzelm@19405
   197
          commas_quote xs ^ " of " ^ Sign.string_of_sort thy [c2], [], []));
wenzelm@19405
   198
  in (c1, c2) end;
wenzelm@19405
   199
wenzelm@19405
   200
fun read_classrel thy raw_rel =
wenzelm@19405
   201
  cert_classrel thy (pairself (Sign.read_class thy) raw_rel)
wenzelm@19405
   202
    handle TYPE (msg, _, _) => error msg;
wenzelm@19405
   203
wenzelm@19405
   204
wenzelm@19405
   205
(* primitive rules *)
wenzelm@19405
   206
wenzelm@19405
   207
fun add_classrel th thy =
wenzelm@19405
   208
  let
wenzelm@19405
   209
    fun err () = raise THM ("add_classrel: malformed class relation", 0, [th]);
wenzelm@22691
   210
    val prop = Thm.plain_prop_of (Thm.transfer thy th);
wenzelm@19405
   211
    val rel = Logic.dest_classrel prop handle TERM _ => err ();
wenzelm@19405
   212
    val (c1, c2) = cert_classrel thy rel handle TYPE _ => err ();
wenzelm@19574
   213
  in
wenzelm@19574
   214
    thy
wenzelm@19574
   215
    |> Sign.primitive_classrel (c1, c2)
wenzelm@19574
   216
    |> put_classrel ((c1, c2), Drule.unconstrainTs th)
wenzelm@19574
   217
  end;
wenzelm@15876
   218
wenzelm@19405
   219
fun add_arity th thy =
wenzelm@15876
   220
  let
wenzelm@19528
   221
    fun err () = raise THM ("add_arity: malformed type arity", 0, [th]);
wenzelm@22691
   222
    val prop = Thm.plain_prop_of (Thm.transfer thy th);
wenzelm@19528
   223
    val (t, Ss, c) = Logic.dest_arity prop handle TERM _ => err ();
wenzelm@19528
   224
    val _ = if map (Sign.certify_sort thy) Ss = Ss then () else err ();
wenzelm@19574
   225
  in
wenzelm@19574
   226
    thy
wenzelm@19574
   227
    |> Sign.primitive_arity (t, Ss, [c])
wenzelm@19574
   228
    |> put_arity ((t, Ss, c), Drule.unconstrainTs th)
wenzelm@19574
   229
  end;
wenzelm@404
   230
wenzelm@404
   231
wenzelm@19243
   232
(* tactical proofs *)
wenzelm@15876
   233
wenzelm@19405
   234
fun prove_classrel raw_rel tac thy =
wenzelm@15876
   235
  let
wenzelm@19405
   236
    val (c1, c2) = cert_classrel thy raw_rel;
wenzelm@20049
   237
    val th = Goal.prove (ProofContext.init thy) [] []
wenzelm@20049
   238
        (Logic.mk_classrel (c1, c2)) (fn _ => tac) handle ERROR msg =>
wenzelm@19243
   239
      cat_error msg ("The error(s) above occurred while trying to prove class relation " ^
wenzelm@19243
   240
        quote (Sign.string_of_classrel thy [c1, c2]));
wenzelm@19511
   241
  in
wenzelm@19511
   242
    thy
wenzelm@20628
   243
    |> PureThy.add_thms [((prefix classrel_prefix (Logic.name_classrel (c1, c2)), th), [])]
wenzelm@19511
   244
    |-> (fn [th'] => add_classrel th')
wenzelm@19511
   245
  end;
wenzelm@404
   246
wenzelm@19243
   247
fun prove_arity raw_arity tac thy =
wenzelm@15876
   248
  let
wenzelm@19243
   249
    val arity = Sign.cert_arity thy raw_arity;
wenzelm@20628
   250
    val names = map (prefix arity_prefix) (Logic.name_arities arity);
wenzelm@19405
   251
    val props = Logic.mk_arities arity;
wenzelm@20049
   252
    val ths = Goal.prove_multi (ProofContext.init thy) [] [] props
wenzelm@21687
   253
      (fn _ => Goal.precise_conjunction_tac (length props) 1 THEN tac) handle ERROR msg =>
wenzelm@19243
   254
        cat_error msg ("The error(s) above occurred while trying to prove type arity " ^
wenzelm@19243
   255
          quote (Sign.string_of_arity thy arity));
wenzelm@19511
   256
  in
wenzelm@19511
   257
    thy
wenzelm@20628
   258
    |> PureThy.add_thms (map (rpair []) (names ~~ ths))
wenzelm@19511
   259
    |-> fold add_arity
wenzelm@19511
   260
  end;
wenzelm@19511
   261
wenzelm@19511
   262
wenzelm@19511
   263
wenzelm@19511
   264
(** class definitions **)
wenzelm@19511
   265
haftmann@22745
   266
fun define_class (bclass, raw_super) params raw_specs thy =
wenzelm@19511
   267
  let
wenzelm@19511
   268
    val ctxt = ProofContext.init thy;
wenzelm@19511
   269
    val pp = ProofContext.pp ctxt;
wenzelm@19511
   270
wenzelm@19511
   271
wenzelm@19511
   272
    (* prepare specification *)
wenzelm@19511
   273
wenzelm@19511
   274
    val bconst = Logic.const_of_class bclass;
wenzelm@19511
   275
    val class = Sign.full_name thy bclass;
haftmann@22745
   276
    val super = Sign.certify_sort thy raw_super;
wenzelm@19511
   277
wenzelm@19511
   278
    fun prep_axiom t =
wenzelm@19511
   279
      (case Term.add_tfrees t [] of
wenzelm@19511
   280
        [(a, S)] =>
wenzelm@19511
   281
          if Sign.subsort thy (super, S) then t
wenzelm@19511
   282
          else error ("Sort constraint of type variable " ^
wenzelm@19511
   283
            setmp show_sorts true (Pretty.string_of_typ pp) (TFree (a, S)) ^
wenzelm@19511
   284
            " needs to be weaker than " ^ Pretty.string_of_sort pp super)
wenzelm@19511
   285
      | [] => t
wenzelm@19511
   286
      | _ => error ("Multiple type variables in class axiom:\n" ^ Pretty.string_of_term pp t))
wenzelm@20548
   287
      |> map_types (Term.map_atyps (fn TFree _ => Term.aT [] | U => U))
wenzelm@19511
   288
      |> Logic.close_form;
wenzelm@19511
   289
haftmann@22745
   290
    (*FIXME is ProofContext.cert_propp really neccessary?*)
haftmann@22745
   291
    val axiomss = ProofContext.cert_propp (ctxt, map (map (rpair []) o snd) raw_specs)
wenzelm@19511
   292
      |> snd |> map (map (prep_axiom o fst));
haftmann@22745
   293
    val name_atts = map fst raw_specs;
wenzelm@19511
   294
wenzelm@19511
   295
wenzelm@19511
   296
    (* definition *)
wenzelm@19511
   297
wenzelm@19511
   298
    val conjs = map (curry Logic.mk_inclass (Term.aT [])) super @ flat axiomss;
wenzelm@19511
   299
    val class_eq =
wenzelm@19511
   300
      Logic.mk_equals (Logic.mk_inclass (Term.aT [], class), Logic.mk_conjunction_list conjs);
wenzelm@19511
   301
wenzelm@19511
   302
    val ([def], def_thy) =
wenzelm@19511
   303
      thy
wenzelm@19511
   304
      |> Sign.primitive_class (bclass, super)
wenzelm@19511
   305
      |> PureThy.add_defs_i false [((Thm.def_name bconst, class_eq), [])];
wenzelm@19511
   306
    val (raw_intro, (raw_classrel, raw_axioms)) =
wenzelm@19511
   307
      (Conjunction.split_defined (length conjs) def) ||> chop (length super);
wenzelm@19392
   308
wenzelm@19418
   309
wenzelm@19511
   310
    (* facts *)
wenzelm@19511
   311
wenzelm@19511
   312
    val class_triv = Thm.class_triv def_thy class;
wenzelm@19511
   313
    val ([(_, [intro]), (_, classrel), (_, axioms)], facts_thy) =
wenzelm@19511
   314
      def_thy
wenzelm@19511
   315
      |> PureThy.note_thmss_qualified "" bconst
wenzelm@19511
   316
        [((introN, []), [([Drule.standard raw_intro], [])]),
wenzelm@19511
   317
         ((superN, []), [(map Drule.standard raw_classrel, [])]),
wenzelm@19511
   318
         ((axiomsN, []), [(map (fn th => Drule.standard (class_triv RS th)) raw_axioms, [])])];
wenzelm@19511
   319
haftmann@21463
   320
    (* params *)
haftmann@21463
   321
haftmann@21463
   322
    val params_typs = map (fn param =>
haftmann@21463
   323
      let
haftmann@21463
   324
        val ty = Sign.the_const_type thy param;
haftmann@21893
   325
        val _ = case Term.typ_tvars ty
haftmann@21893
   326
         of [_] => ()
haftmann@21953
   327
          | _ => error ("Exactly one type variable required in parameter " ^ quote param);
haftmann@21893
   328
        val ty' = Term.map_type_tvar (fn _ => TFree (param_tyvarname, [class])) ty;
haftmann@21463
   329
      in (param, ty') end) params;
wenzelm@19511
   330
wenzelm@19511
   331
    (* result *)
wenzelm@19511
   332
haftmann@21925
   333
    val axclass = make_axclass ((def, intro, axioms), params_typs);
wenzelm@19511
   334
    val result_thy =
wenzelm@19511
   335
      facts_thy
wenzelm@19574
   336
      |> fold put_classrel (map (pair class) super ~~ classrel)
wenzelm@19511
   337
      |> Sign.add_path bconst
wenzelm@19511
   338
      |> PureThy.note_thmss_i "" (name_atts ~~ map Thm.simple_fact (unflat axiomss axioms)) |> snd
wenzelm@19511
   339
      |> Sign.restore_naming facts_thy
wenzelm@19574
   340
      |> map_axclasses (fn (axclasses, parameters) =>
wenzelm@21919
   341
        (Symtab.update (class, axclass) axclasses,
wenzelm@19574
   342
          fold (fn x => add_param pp (x, class)) params parameters));
wenzelm@19511
   343
wenzelm@19511
   344
  in (class, result_thy) end;
wenzelm@19511
   345
wenzelm@19511
   346
wenzelm@19531
   347
wenzelm@19511
   348
(** axiomatizations **)
wenzelm@19511
   349
wenzelm@19511
   350
local
wenzelm@19511
   351
wenzelm@20628
   352
fun axiomatize prep mk name add raw_args thy =
wenzelm@20628
   353
  let
wenzelm@20628
   354
    val args = prep thy raw_args;
wenzelm@20628
   355
    val specs = mk args;
wenzelm@20628
   356
    val names = name args;
wenzelm@20628
   357
  in thy |> PureThy.add_axioms_i (map (rpair []) (names ~~ specs)) |-> fold add end;
wenzelm@19511
   358
wenzelm@19511
   359
fun ax_classrel prep =
wenzelm@20628
   360
  axiomatize (map o prep) (map Logic.mk_classrel)
wenzelm@20628
   361
    (map (prefix classrel_prefix o Logic.name_classrel)) add_classrel;
wenzelm@19511
   362
wenzelm@19511
   363
fun ax_arity prep =
wenzelm@20628
   364
  axiomatize prep Logic.mk_arities
wenzelm@20628
   365
    (map (prefix arity_prefix) o Logic.name_arities) add_arity;
wenzelm@19511
   366
wenzelm@19706
   367
fun class_const c =
wenzelm@19706
   368
  (Logic.const_of_class c, Term.itselfT (Term.aT []) --> propT);
wenzelm@19706
   369
wenzelm@19511
   370
fun ax_class prep_class prep_classrel (bclass, raw_super) thy =
wenzelm@19511
   371
  let
wenzelm@19511
   372
    val class = Sign.full_name thy bclass;
wenzelm@19511
   373
    val super = map (prep_class thy) raw_super |> Sign.certify_sort thy;
wenzelm@19511
   374
  in
wenzelm@19511
   375
    thy
wenzelm@19511
   376
    |> Sign.primitive_class (bclass, super)
wenzelm@19511
   377
    |> ax_classrel prep_classrel (map (fn c => (class, c)) super)
wenzelm@19706
   378
    |> Theory.add_deps "" (class_const class) (map class_const super)
wenzelm@19511
   379
  end;
wenzelm@19511
   380
wenzelm@19511
   381
in
wenzelm@19511
   382
wenzelm@19511
   383
val axiomatize_class = ax_class Sign.read_class read_classrel;
wenzelm@19511
   384
val axiomatize_class_i = ax_class Sign.certify_class cert_classrel;
wenzelm@19511
   385
val axiomatize_classrel = ax_classrel read_classrel;
wenzelm@19511
   386
val axiomatize_classrel_i = ax_classrel cert_classrel;
wenzelm@19511
   387
val axiomatize_arity = ax_arity Sign.read_arity;
wenzelm@19511
   388
val axiomatize_arity_i = ax_arity Sign.cert_arity;
wenzelm@19511
   389
wenzelm@19511
   390
end;
wenzelm@19511
   391
wenzelm@19511
   392
wenzelm@19511
   393
wenzelm@19511
   394
(** explicit derivations -- cached **)
wenzelm@19511
   395
wenzelm@19574
   396
datatype cache = Types of (class * thm) list Typtab.table;
wenzelm@19574
   397
val cache = Types Typtab.empty;
wenzelm@19574
   398
wenzelm@19511
   399
local
wenzelm@19503
   400
wenzelm@19574
   401
fun lookup_type (Types cache) = AList.lookup (op =) o Typtab.lookup_list cache;
wenzelm@19574
   402
fun insert_type T der (Types cache) = Types (Typtab.insert_list (eq_fst op =) (T, der) cache);
wenzelm@19503
   403
wenzelm@19522
   404
fun derive_type _ (_, []) = []
wenzelm@19522
   405
  | derive_type thy (typ, sort) =
wenzelm@19522
   406
      let
wenzelm@19528
   407
        val vars = Term.fold_atyps
wenzelm@19522
   408
            (fn T as TFree (_, S) => insert (eq_fst op =) (T, S)
wenzelm@19522
   409
              | T as TVar (_, S) => insert (eq_fst op =) (T, S)
wenzelm@19528
   410
              | _ => I) typ [];
wenzelm@19574
   411
        val hyps = vars
wenzelm@19574
   412
          |> map (fn (T, S) => (T, Drule.sort_triv thy (T, S) ~~ S));
wenzelm@19642
   413
        val ths = (typ, sort) |> Sorts.of_sort_derivation (Sign.pp thy) (Sign.classes_of thy)
wenzelm@22570
   414
           {class_relation =
wenzelm@19574
   415
              fn (th, c1) => fn c2 => th RS the_classrel thy (c1, c2),
wenzelm@22570
   416
            type_constructor =
wenzelm@19574
   417
              fn a => fn dom => fn c =>
wenzelm@19574
   418
                let val Ss = map (map snd) dom and ths = maps (map fst) dom
wenzelm@19574
   419
                in ths MRS the_arity thy a (c, Ss) end,
wenzelm@22570
   420
            type_variable =
wenzelm@19574
   421
              the_default [] o AList.lookup (op =) hyps};
wenzelm@19574
   422
      in ths end;
wenzelm@19503
   423
wenzelm@19511
   424
in
wenzelm@19511
   425
wenzelm@19574
   426
fun of_sort thy (typ, sort) cache =
wenzelm@19574
   427
  let
wenzelm@19574
   428
    val sort' = filter (is_none o lookup_type cache typ) sort;
wenzelm@19574
   429
    val ths' = derive_type thy (typ, sort')
wenzelm@19574
   430
      handle ERROR msg => cat_error msg ("The error(s) above occurred for sort derivation: " ^
wenzelm@19574
   431
        Sign.string_of_typ thy typ ^ " :: " ^ Sign.string_of_sort thy sort');
wenzelm@19574
   432
    val cache' = cache |> fold (insert_type typ) (sort' ~~ ths');
wenzelm@19574
   433
    val ths =
wenzelm@19574
   434
      sort |> map (fn c =>
wenzelm@19574
   435
        Goal.finish (the (lookup_type cache' typ c) RS
wenzelm@19574
   436
          Goal.init (Thm.cterm_of thy (Logic.mk_inclass (typ, c))))
wenzelm@20260
   437
        |> Thm.adjust_maxidx_thm ~1);
wenzelm@19574
   438
  in (ths, cache') end;
wenzelm@19503
   439
wenzelm@19511
   440
end;
wenzelm@19418
   441
wenzelm@15876
   442
end;