src/Pure/axclass.ML
author wenzelm
Sun Apr 30 22:50:05 2006 +0200 (2006-04-30)
changeset 19511 b4bd790f9373
parent 19503 10921826b160
child 19522 a4c790594737
permissions -rw-r--r--
renamed add_axclass(_i) to define_axclass(_i);
renamed get_info to get_definition;
added axiomatize_class/classrel/arity (supercede Sign.add_classes/classrel/arities);
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@19511
    32
  val of_sort: theory -> typ * sort -> thm list
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@19503
   152
fun store_instance f thy (x, th) =
wenzelm@19503
   153
  let
wenzelm@19503
   154
    val th' = Drule.standard' th;
wenzelm@19503
   155
    val _ = change (#2 (AxClassData.get thy)) (map_instances (f (x, th')));
wenzelm@19503
   156
  in th' end;
wenzelm@19503
   157
wenzelm@19503
   158
val store_classrel = store_instance (fn ((c1, c2), th) => fn (classes, classrel, arities, types) =>
wenzelm@19503
   159
  (classes
wenzelm@19503
   160
    |> Graph.default_node (c1, ())
wenzelm@19503
   161
    |> Graph.default_node (c2, ())
wenzelm@19503
   162
    |> Graph.add_edge (c1, c2),
wenzelm@19503
   163
    insert (eq_fst op =) ((c1, c2), th) classrel, arities, types));
wenzelm@19503
   164
wenzelm@19503
   165
val store_arity = store_instance (fn ((t, Ss, c), th) => fn (classes, classrel, arities, types) =>
wenzelm@19503
   166
  (classes, classrel, arities |> Symtab.insert_list (eq_fst op =) (t, ((c, Ss), th)), types));
wenzelm@19503
   167
wenzelm@19503
   168
val store_type = store_instance (fn ((T, c), th) => fn (classes, classrel, arities, types) =>
wenzelm@19503
   169
  (classes, classrel, arities, types |> Typtab.insert_list (eq_fst op =) (T, (c, th))));
wenzelm@19503
   170
wenzelm@19503
   171
wenzelm@19511
   172
(* print data *)
wenzelm@19460
   173
wenzelm@19460
   174
fun print_axclasses thy =
wenzelm@19460
   175
  let
wenzelm@19460
   176
    val axclasses = #1 (#1 (AxClassData.get thy));
wenzelm@19460
   177
    val ctxt = ProofContext.init thy;
wenzelm@19460
   178
wenzelm@19460
   179
    fun pretty_axclass (class, AxClass {def, intro, axioms}) =
wenzelm@19460
   180
      Pretty.block (Pretty.fbreaks
wenzelm@19460
   181
       [Pretty.block
wenzelm@19511
   182
          [Pretty.str "class ", ProofContext.pretty_sort ctxt [class], Pretty.str ":"],
wenzelm@19460
   183
        Pretty.strs ("parameters:" :: params_of thy class),
wenzelm@19460
   184
        ProofContext.pretty_fact ctxt ("def", [def]),
wenzelm@19460
   185
        ProofContext.pretty_fact ctxt (introN, [intro]),
wenzelm@19460
   186
        ProofContext.pretty_fact ctxt (axiomsN, axioms)]);
wenzelm@19460
   187
  in Pretty.writeln (Pretty.chunks (map pretty_axclass (Symtab.dest axclasses))) end;
wenzelm@19460
   188
wenzelm@19460
   189
wenzelm@404
   190
wenzelm@19511
   191
(** instances **)
wenzelm@19418
   192
wenzelm@19418
   193
(* class relations *)
wenzelm@19418
   194
wenzelm@19405
   195
fun cert_classrel thy raw_rel =
wenzelm@15876
   196
  let
wenzelm@19405
   197
    val (c1, c2) = pairself (Sign.certify_class thy) raw_rel;
wenzelm@19511
   198
    val _ = Type.add_classrel (Sign.pp thy) (c1, c2) (Sign.tsig_of thy);
wenzelm@19405
   199
    val _ =
wenzelm@19460
   200
      (case subtract (op =) (all_params_of thy [c1]) (all_params_of thy [c2]) of
wenzelm@19405
   201
        [] => ()
wenzelm@19405
   202
      | xs => raise TYPE ("Class " ^ Sign.string_of_sort thy [c1] ^ " lacks parameter(s) " ^
wenzelm@19405
   203
          commas_quote xs ^ " of " ^ Sign.string_of_sort thy [c2], [], []));
wenzelm@19405
   204
  in (c1, c2) end;
wenzelm@19405
   205
wenzelm@19405
   206
fun read_classrel thy raw_rel =
wenzelm@19405
   207
  cert_classrel thy (pairself (Sign.read_class thy) raw_rel)
wenzelm@19405
   208
    handle TYPE (msg, _, _) => error msg;
wenzelm@19405
   209
wenzelm@19405
   210
wenzelm@19405
   211
(* primitive rules *)
wenzelm@19405
   212
wenzelm@19405
   213
fun add_classrel th thy =
wenzelm@19405
   214
  let
wenzelm@19405
   215
    fun err () = raise THM ("add_classrel: malformed class relation", 0, [th]);
wenzelm@19405
   216
    val prop = Drule.plain_prop_of (Thm.transfer thy th);
wenzelm@19405
   217
    val rel = Logic.dest_classrel prop handle TERM _ => err ();
wenzelm@19405
   218
    val (c1, c2) = cert_classrel thy rel handle TYPE _ => err ();
wenzelm@19511
   219
    val thy' = thy |> Sign.primitive_classrel (c1, c2);
wenzelm@19503
   220
    val _ = store_classrel thy' ((c1, c2), Drule.unconstrainTs th);
wenzelm@19503
   221
  in thy' end;
wenzelm@15876
   222
wenzelm@19405
   223
fun add_arity th thy =
wenzelm@15876
   224
  let
wenzelm@19405
   225
    val prop = Drule.plain_prop_of (Thm.transfer thy th);
wenzelm@19405
   226
    val (t, Ss, c) = Logic.dest_arity prop handle TERM _ =>
wenzelm@19405
   227
      raise THM ("add_arity: malformed type arity", 0, [th]);
wenzelm@19511
   228
    val thy' = thy |> Sign.primitive_arity (t, Ss, [c]);
wenzelm@19503
   229
    val _ = store_arity thy' ((t, Ss, c), Drule.unconstrainTs th);
wenzelm@19503
   230
  in thy' end;
wenzelm@404
   231
wenzelm@404
   232
wenzelm@19243
   233
(* tactical proofs *)
wenzelm@15876
   234
wenzelm@19405
   235
fun prove_classrel raw_rel tac thy =
wenzelm@15876
   236
  let
wenzelm@19405
   237
    val (c1, c2) = cert_classrel thy raw_rel;
wenzelm@19405
   238
    val th = Goal.prove thy [] [] (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@19511
   243
    |> PureThy.add_thms [((classrelN ^ "_" ^ serial_string (), 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@19405
   250
    val props = Logic.mk_arities arity;
wenzelm@17956
   251
    val ths = Goal.prove_multi thy [] [] props
wenzelm@18678
   252
      (fn _ => Tactic.precise_conjunction_tac (length props) 1 THEN tac) handle ERROR msg =>
wenzelm@19243
   253
        cat_error msg ("The error(s) above occurred while trying to prove type arity " ^
wenzelm@19243
   254
          quote (Sign.string_of_arity thy arity));
wenzelm@19511
   255
  in
wenzelm@19511
   256
    thy
wenzelm@19511
   257
    |> PureThy.add_thms (ths |> map (fn th => ((arityN ^ "_" ^ serial_string (), th), [])))
wenzelm@19511
   258
    |-> fold add_arity
wenzelm@19511
   259
  end;
wenzelm@19511
   260
wenzelm@19511
   261
wenzelm@19511
   262
wenzelm@19511
   263
(** class definitions **)
wenzelm@19511
   264
wenzelm@19511
   265
local
wenzelm@19511
   266
wenzelm@19511
   267
fun def_class prep_class prep_att prep_propp
wenzelm@19511
   268
    (bclass, raw_super) params raw_specs thy =
wenzelm@19511
   269
  let
wenzelm@19511
   270
    val ctxt = ProofContext.init thy;
wenzelm@19511
   271
    val pp = ProofContext.pp ctxt;
wenzelm@19511
   272
wenzelm@19511
   273
wenzelm@19511
   274
    (* prepare specification *)
wenzelm@19511
   275
wenzelm@19511
   276
    val bconst = Logic.const_of_class bclass;
wenzelm@19511
   277
    val class = Sign.full_name thy bclass;
wenzelm@19511
   278
    val super = map (prep_class thy) raw_super |> Sign.certify_sort thy;
wenzelm@19511
   279
wenzelm@19511
   280
    fun prep_axiom t =
wenzelm@19511
   281
      (case Term.add_tfrees t [] of
wenzelm@19511
   282
        [(a, S)] =>
wenzelm@19511
   283
          if Sign.subsort thy (super, S) then t
wenzelm@19511
   284
          else error ("Sort constraint of type variable " ^
wenzelm@19511
   285
            setmp show_sorts true (Pretty.string_of_typ pp) (TFree (a, S)) ^
wenzelm@19511
   286
            " needs to be weaker than " ^ Pretty.string_of_sort pp super)
wenzelm@19511
   287
      | [] => t
wenzelm@19511
   288
      | _ => error ("Multiple type variables in class axiom:\n" ^ Pretty.string_of_term pp t))
wenzelm@19511
   289
      |> map_term_types (Term.map_atyps (fn TFree _ => Term.aT [] | U => U))
wenzelm@19511
   290
      |> Logic.close_form;
wenzelm@19511
   291
wenzelm@19511
   292
    val axiomss = prep_propp (ctxt, map (map (rpair ([], [])) o snd) raw_specs)
wenzelm@19511
   293
      |> snd |> map (map (prep_axiom o fst));
wenzelm@19511
   294
    val name_atts = Attrib.map_specs (prep_att thy) raw_specs |> map fst;
wenzelm@19511
   295
wenzelm@19511
   296
wenzelm@19511
   297
    (* definition *)
wenzelm@19511
   298
wenzelm@19511
   299
    val conjs = map (curry Logic.mk_inclass (Term.aT [])) super @ flat axiomss;
wenzelm@19511
   300
    val class_eq =
wenzelm@19511
   301
      Logic.mk_equals (Logic.mk_inclass (Term.aT [], class), Logic.mk_conjunction_list conjs);
wenzelm@19511
   302
wenzelm@19511
   303
    val ([def], def_thy) =
wenzelm@19511
   304
      thy
wenzelm@19511
   305
      |> Sign.primitive_class (bclass, super)
wenzelm@19511
   306
      |> PureThy.add_defs_i false [((Thm.def_name bconst, class_eq), [])];
wenzelm@19511
   307
    val (raw_intro, (raw_classrel, raw_axioms)) =
wenzelm@19511
   308
      (Conjunction.split_defined (length conjs) def) ||> chop (length super);
wenzelm@19392
   309
wenzelm@19418
   310
wenzelm@19511
   311
    (* facts *)
wenzelm@19511
   312
wenzelm@19511
   313
    val class_triv = Thm.class_triv def_thy class;
wenzelm@19511
   314
    val ([(_, [intro]), (_, classrel), (_, axioms)], facts_thy) =
wenzelm@19511
   315
      def_thy
wenzelm@19511
   316
      |> PureThy.note_thmss_qualified "" bconst
wenzelm@19511
   317
        [((introN, []), [([Drule.standard raw_intro], [])]),
wenzelm@19511
   318
         ((superN, []), [(map Drule.standard raw_classrel, [])]),
wenzelm@19511
   319
         ((axiomsN, []), [(map (fn th => Drule.standard (class_triv RS th)) raw_axioms, [])])];
wenzelm@19511
   320
    val _ = map (store_classrel facts_thy) (map (pair class) super ~~ classrel);
wenzelm@19511
   321
wenzelm@19511
   322
wenzelm@19511
   323
    (* result *)
wenzelm@19511
   324
wenzelm@19511
   325
    val result_thy =
wenzelm@19511
   326
      facts_thy
wenzelm@19511
   327
      |> Sign.add_path bconst
wenzelm@19511
   328
      |> PureThy.note_thmss_i "" (name_atts ~~ map Thm.simple_fact (unflat axiomss axioms)) |> snd
wenzelm@19511
   329
      |> Sign.restore_naming facts_thy
wenzelm@19511
   330
      |> AxClassData.map (apfst (fn (axclasses, parameters) =>
wenzelm@19511
   331
        (Symtab.update (class, make_axclass (def, intro, axioms)) axclasses,
wenzelm@19511
   332
          fold (fn x => add_param pp (x, class)) params parameters)));
wenzelm@19511
   333
wenzelm@19511
   334
  in (class, result_thy) end;
wenzelm@19511
   335
wenzelm@19511
   336
in
wenzelm@19511
   337
wenzelm@19511
   338
val define_class = def_class Sign.read_class Attrib.attribute ProofContext.read_propp;
wenzelm@19511
   339
val define_class_i = def_class Sign.certify_class (K I) ProofContext.cert_propp;
wenzelm@19511
   340
wenzelm@19511
   341
end;
wenzelm@19511
   342
wenzelm@19511
   343
wenzelm@19511
   344
(** axiomatizations **)
wenzelm@19511
   345
wenzelm@19511
   346
local
wenzelm@19511
   347
wenzelm@19511
   348
fun axiomatize kind add prep arg thy =
wenzelm@19511
   349
  let val specs = arg |> prep thy |> map (fn prop => ((kind ^ "_" ^ serial_string (), prop), []))
wenzelm@19511
   350
  in thy |> PureThy.add_axioms_i specs |-> fold add end;
wenzelm@19511
   351
wenzelm@19511
   352
fun ax_classrel prep =
wenzelm@19511
   353
  axiomatize classrelN add_classrel (fn thy => map (prep thy #> Logic.mk_classrel));
wenzelm@19511
   354
wenzelm@19511
   355
fun ax_arity prep =
wenzelm@19511
   356
  axiomatize arityN add_arity (fn thy => prep thy #> Logic.mk_arities);
wenzelm@19511
   357
wenzelm@19511
   358
fun ax_class prep_class prep_classrel (bclass, raw_super) thy =
wenzelm@19511
   359
  let
wenzelm@19511
   360
    val class = Sign.full_name thy bclass;
wenzelm@19511
   361
    val super = map (prep_class thy) raw_super |> Sign.certify_sort thy;
wenzelm@19511
   362
  in
wenzelm@19511
   363
    thy
wenzelm@19511
   364
    |> Sign.primitive_class (bclass, super)
wenzelm@19511
   365
    |> ax_classrel prep_classrel (map (fn c => (class, c)) super)
wenzelm@19511
   366
  end;
wenzelm@19511
   367
wenzelm@19511
   368
in
wenzelm@19511
   369
wenzelm@19511
   370
val axiomatize_class = ax_class Sign.read_class read_classrel;
wenzelm@19511
   371
val axiomatize_class_i = ax_class Sign.certify_class cert_classrel;
wenzelm@19511
   372
val axiomatize_classrel = ax_classrel read_classrel;
wenzelm@19511
   373
val axiomatize_classrel_i = ax_classrel cert_classrel;
wenzelm@19511
   374
val axiomatize_arity = ax_arity Sign.read_arity;
wenzelm@19511
   375
val axiomatize_arity_i = ax_arity Sign.cert_arity;
wenzelm@19511
   376
wenzelm@19511
   377
end;
wenzelm@19511
   378
wenzelm@19511
   379
wenzelm@19511
   380
wenzelm@19511
   381
(** explicit derivations -- cached **)
wenzelm@19511
   382
wenzelm@19511
   383
local
wenzelm@19503
   384
wenzelm@19503
   385
fun derive_classrel thy (c1, c2) =
wenzelm@19503
   386
  let
wenzelm@19503
   387
    val {classes, classrel, ...} = get_instances thy;
wenzelm@19503
   388
    val lookup = AList.lookup (op =) classrel;
wenzelm@19503
   389
    fun derive [c, c'] = the (lookup (c, c'))
wenzelm@19503
   390
      | derive (c :: c' :: cs) = derive [c, c'] RS derive (c' :: cs);
wenzelm@19503
   391
  in
wenzelm@19503
   392
    (case lookup (c1, c2) of
wenzelm@19503
   393
      SOME rule => rule
wenzelm@19503
   394
    | NONE =>
wenzelm@19503
   395
        (case Graph.find_paths classes (c1, c2) of
wenzelm@19503
   396
          [] => error ("Cannot derive class relation " ^ Sign.string_of_classrel thy [c1, c2])
wenzelm@19503
   397
        | path :: _ => store_classrel thy ((c1, c2), derive path)))
wenzelm@19503
   398
  end;
wenzelm@19503
   399
wenzelm@19503
   400
fun weaken_subclass thy (c1, th) c2 =
wenzelm@19503
   401
  if c1 = c2 then th
wenzelm@19503
   402
  else th RS derive_classrel thy (c1, c2);
wenzelm@19503
   403
wenzelm@19503
   404
fun weaken_subsort thy S1 S2 = S2 |> map (fn c2 =>
wenzelm@19503
   405
  (case S1 |> find_first (fn (c1, _) => Sign.subsort thy ([c1], [c2])) of
wenzelm@19503
   406
    SOME c1 => weaken_subclass thy c1 c2
wenzelm@19503
   407
  | NONE => error ("Cannot derive subsort relation " ^
wenzelm@19503
   408
      Sign.string_of_sort thy (map #1 S1) ^ " < " ^ Sign.string_of_sort thy S2)));
wenzelm@19503
   409
wenzelm@19503
   410
fun apply_arity thy t dom c =
wenzelm@19503
   411
  let
wenzelm@19503
   412
    val {arities, ...} = get_instances thy;
wenzelm@19503
   413
    val subsort = Sign.subsort thy;
wenzelm@19503
   414
    val Ss = map (map #1) dom;
wenzelm@19503
   415
  in
wenzelm@19503
   416
    (case Symtab.lookup_list arities t |> find_first (fn ((c', Ss'), _) =>
wenzelm@19503
   417
        subsort ([c'], [c]) andalso ListPair.all subsort (Ss, Ss')) of
wenzelm@19503
   418
      SOME ((c', Ss'), rule) =>
wenzelm@19503
   419
        weaken_subclass thy (c', rule OF flat (map2 (weaken_subsort thy) dom Ss')) c
wenzelm@19503
   420
    | NONE => error ("Cannot derive type arity " ^ Sign.string_of_arity thy (t, Ss, [c])))
wenzelm@19503
   421
  end;
wenzelm@19503
   422
wenzelm@19503
   423
fun derive_type thy hyps =
wenzelm@19503
   424
  let
wenzelm@19503
   425
    fun derive (Type (a, Ts)) S =
wenzelm@19503
   426
          let val Ss = Sign.arity_sorts thy a S
wenzelm@19503
   427
          in map (apply_arity thy a (map2 (fn T => fn S => S ~~ derive T S) Ts Ss)) S end
wenzelm@19503
   428
      | derive (TFree (a, [])) S =
wenzelm@19503
   429
          weaken_subsort thy (the_default [] (AList.lookup (op =) hyps a)) S
wenzelm@19503
   430
      | derive T _ = error ("Illegal occurrence of type variable " ^
wenzelm@19503
   431
          setmp show_sorts true (Sign.string_of_typ thy) T);
wenzelm@19503
   432
  in derive end;
wenzelm@19503
   433
wenzelm@19511
   434
in
wenzelm@19511
   435
wenzelm@19503
   436
fun of_sort thy (typ, sort) =
wenzelm@19503
   437
  let
wenzelm@19503
   438
    fun lookup () = AList.lookup (op =) (Typtab.lookup_list (#types (get_instances thy)) typ);
wenzelm@19503
   439
    val sort' = filter (is_none o lookup ()) sort;
wenzelm@19503
   440
    val _ = conditional (not (null sort')) (fn () =>
wenzelm@19503
   441
      let
wenzelm@19503
   442
        val vars = Term.fold_atyps (insert (op =)) typ [];
wenzelm@19503
   443
        val renaming =
wenzelm@19503
   444
          map2 (fn T => fn a => (T, (a, case T of TFree (_, S) => S | TVar (_, S) => S)))
wenzelm@19503
   445
            vars (Term.invent_names [] "'a" (length vars));
wenzelm@19503
   446
        val typ' = typ |> Term.map_atyps
wenzelm@19503
   447
          (fn T => TFree (#1 (the (AList.lookup (op =) renaming T)), []));
wenzelm@19503
   448
wenzelm@19503
   449
        val hyps = renaming |> map (fn (_, (a, S)) => (a, S ~~ (S |> map (fn c =>
wenzelm@19503
   450
          Thm.assume (Thm.cterm_of thy (Logic.mk_inclass (TFree (a, []), c)))))));
wenzelm@19503
   451
        val inst = renaming |> map (fn (T, (a, S)) =>
wenzelm@19503
   452
          pairself (Thm.ctyp_of thy) (TVar ((a, 0), S), T));
wenzelm@19503
   453
wenzelm@19503
   454
        val ths =
wenzelm@19503
   455
          derive_type thy hyps typ' sort'
wenzelm@19503
   456
          |> map (Thm.instantiate (inst, []));
wenzelm@19503
   457
        val _ = map (store_type thy) (map (pair typ) sort' ~~ ths);
wenzelm@19503
   458
      in () end);
wenzelm@19503
   459
  in map (the o lookup ()) sort end;
wenzelm@19503
   460
wenzelm@19511
   461
end;
wenzelm@19418
   462
wenzelm@15876
   463
end;