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