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