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