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