src/Pure/axclass.ML
author wenzelm
Sat Apr 29 23:16:45 2006 +0200 (2006-04-29)
changeset 19503 10921826b160
parent 19482 9f11af8f7ef9
child 19511 b4bd790f9373
permissions -rw-r--r--
instances data: mutable cache;
added of_sort: interface for derived instances;
wenzelm@404
     1
(*  Title:      Pure/axclass.ML
wenzelm@404
     2
    ID:         $Id$
wenzelm@404
     3
    Author:     Markus Wenzel, TU Muenchen
wenzelm@404
     4
wenzelm@19460
     5
Axiomatic type classes: managing predicates and parameter collections.
wenzelm@404
     6
*)
wenzelm@404
     7
wenzelm@404
     8
signature AX_CLASS =
wenzelm@3938
     9
sig
wenzelm@6379
    10
  val print_axclasses: theory -> unit
wenzelm@19460
    11
  val get_info: 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@19405
    15
  val cert_classrel: theory -> class * class -> class * class
wenzelm@19405
    16
  val read_classrel: theory -> xstring * xstring -> class * class
wenzelm@19405
    17
  val add_classrel: thm -> theory -> theory
wenzelm@19405
    18
  val add_arity: thm -> theory -> theory
wenzelm@19405
    19
  val prove_classrel: class * class -> tactic -> theory -> theory
wenzelm@19243
    20
  val prove_arity: string * sort list * sort -> tactic -> theory -> theory
wenzelm@19503
    21
  val of_sort: theory -> typ * sort -> thm list
wenzelm@19418
    22
  val add_axclass: bstring * xstring list -> string list ->
wenzelm@19418
    23
    ((bstring * Attrib.src list) * string list) list -> theory -> class * theory
wenzelm@19418
    24
  val add_axclass_i: bstring * class list -> string list ->
wenzelm@19418
    25
    ((bstring * attribute list) * term list) list -> theory -> class * theory
wenzelm@3938
    26
end;
wenzelm@404
    27
wenzelm@15801
    28
structure AxClass: AX_CLASS =
wenzelm@404
    29
struct
wenzelm@404
    30
wenzelm@19405
    31
(** theory data **)
wenzelm@423
    32
wenzelm@19405
    33
(* class parameters (canonical order) *)
wenzelm@423
    34
wenzelm@19405
    35
type param = string * class;
wenzelm@423
    36
wenzelm@19405
    37
fun add_param pp ((x, c): param) params =
wenzelm@19405
    38
  (case AList.lookup (op =) params x of
wenzelm@19405
    39
    NONE => (x, c) :: params
wenzelm@19405
    40
  | SOME c' => error ("Duplicate class parameter " ^ quote x ^
wenzelm@19405
    41
      " for " ^ Pretty.string_of_sort pp [c] ^
wenzelm@19405
    42
      (if c = c' then "" else " and " ^ Pretty.string_of_sort pp [c'])));
wenzelm@423
    43
wenzelm@19405
    44
fun merge_params _ ([], qs) = qs
wenzelm@19405
    45
  | merge_params pp (ps, qs) =
wenzelm@19405
    46
      fold_rev (fn q => if member (op =) ps q then I else add_param pp q) qs ps;
wenzelm@423
    47
wenzelm@423
    48
wenzelm@19392
    49
(* axclass *)
wenzelm@6379
    50
wenzelm@19243
    51
val introN = "intro";
wenzelm@19243
    52
val axiomsN = "axioms";
wenzelm@19243
    53
wenzelm@19392
    54
datatype axclass = AxClass of
wenzelm@19460
    55
 {def: thm,
wenzelm@19392
    56
  intro: thm,
wenzelm@19392
    57
  axioms: thm list};
wenzelm@19392
    58
wenzelm@19460
    59
type axclasses = axclass Symtab.table * param list;
wenzelm@19392
    60
wenzelm@19460
    61
fun make_axclass (def, intro, axioms) =
wenzelm@19460
    62
  AxClass {def = def, intro = intro, axioms = axioms};
wenzelm@19405
    63
wenzelm@19405
    64
fun merge_axclasses pp ((tab1, params1), (tab2, params2)) : axclasses =
wenzelm@19405
    65
  (Symtab.merge (K true) (tab1, tab2), merge_params pp (params1, params2));
wenzelm@19405
    66
wenzelm@19392
    67
wenzelm@19392
    68
(* instances *)
wenzelm@19392
    69
wenzelm@19392
    70
datatype instances = Instances of
wenzelm@19405
    71
 {classes: unit Graph.T,                 (*raw relation -- no closure!*)
wenzelm@19405
    72
  classrel: ((class * class) * thm) list,
wenzelm@19503
    73
  arities: ((class * sort list) * thm) list Symtab.table,
wenzelm@19503
    74
  types: (class * thm) list Typtab.table};
wenzelm@19392
    75
wenzelm@19503
    76
fun make_instances (classes, classrel, arities, types) =
wenzelm@19503
    77
  Instances {classes = classes, classrel = classrel, arities = arities, types = types};
wenzelm@6379
    78
wenzelm@19503
    79
fun map_instances f (Instances {classes, classrel, arities, types}) =
wenzelm@19503
    80
  make_instances (f (classes, classrel, arities, types));
wenzelm@19392
    81
wenzelm@19392
    82
fun merge_instances
wenzelm@19503
    83
   (Instances {classes = classes1, classrel = classrel1, arities = arities1, types = types1},
wenzelm@19503
    84
    Instances {classes = classes2, classrel = classrel2, arities = arities2, types = types2}) =
wenzelm@19392
    85
  make_instances
wenzelm@19405
    86
   (Graph.merge (K true) (classes1, classes2),
wenzelm@19405
    87
    merge (eq_fst op =) (classrel1, classrel2),
wenzelm@19503
    88
    Symtab.join (K (merge (eq_fst op =))) (arities1, arities2),
wenzelm@19503
    89
    Typtab.join (K (merge (eq_fst op =))) (types1, types2));
wenzelm@19392
    90
wenzelm@19392
    91
wenzelm@19405
    92
(* data *)
wenzelm@19392
    93
wenzelm@19392
    94
structure AxClassData = TheoryDataFun
wenzelm@16458
    95
(struct
wenzelm@19392
    96
  val name = "Pure/axclass";
wenzelm@19503
    97
  type T = axclasses * instances ref;
wenzelm@19503
    98
  val empty : T =
wenzelm@19503
    99
    ((Symtab.empty, []), ref (make_instances (Graph.empty, [], Symtab.empty, Typtab.empty)));
wenzelm@19503
   100
  fun copy (axclasses, ref instances) : T = (axclasses, ref instances);
wenzelm@19503
   101
  val extend = copy;
wenzelm@19503
   102
  fun merge pp ((axclasses1, ref instances1), (axclasses2, ref instances2)) =
wenzelm@19503
   103
    (merge_axclasses pp (axclasses1, axclasses2), ref (merge_instances (instances1, instances2)));
wenzelm@19460
   104
  fun print _ _ = ();
wenzelm@16458
   105
end);
wenzelm@6379
   106
wenzelm@19392
   107
val _ = Context.add_setup AxClassData.init;
wenzelm@6379
   108
wenzelm@6379
   109
wenzelm@19503
   110
(* classes *)
wenzelm@19392
   111
wenzelm@19405
   112
val lookup_info = Symtab.lookup o #1 o #1 o AxClassData.get;
wenzelm@6379
   113
wenzelm@17281
   114
fun get_info thy c =
wenzelm@17281
   115
  (case lookup_info thy c of
wenzelm@19392
   116
    SOME (AxClass info) => info
wenzelm@19392
   117
  | NONE => error ("Unknown axclass " ^ quote c));
wenzelm@6379
   118
wenzelm@19123
   119
fun class_intros thy =
wenzelm@19392
   120
  let
wenzelm@19392
   121
    fun add_intro c =
wenzelm@19392
   122
      (case lookup_info thy c of SOME (AxClass {intro, ...}) => cons intro | _ => I);
wenzelm@19392
   123
    val classes = Sign.classes thy;
wenzelm@19392
   124
  in map (Thm.class_triv thy) classes @ fold add_intro classes [] end;
wenzelm@15876
   125
wenzelm@15876
   126
wenzelm@19503
   127
(* parameters *)
wenzelm@19460
   128
wenzelm@19460
   129
fun get_params thy pred =
wenzelm@19460
   130
  let val params = #2 (#1 (AxClassData.get thy))
wenzelm@19460
   131
  in fold (fn (x, c) => if pred c then cons x else I) params [] end;
wenzelm@19460
   132
wenzelm@19460
   133
fun params_of thy c = get_params thy (fn c' => c' = c);
wenzelm@19460
   134
fun all_params_of thy S = get_params thy (fn c => Sign.subsort thy (S, [c]));
wenzelm@19460
   135
wenzelm@19460
   136
wenzelm@19503
   137
(* instances *)
wenzelm@19503
   138
wenzelm@19503
   139
val get_instances = AxClassData.get #> (fn (_, ref (Instances insts)) => insts);
wenzelm@19503
   140
wenzelm@19503
   141
fun store_instance f thy (x, th) =
wenzelm@19503
   142
  let
wenzelm@19503
   143
    val th' = Drule.standard' th;
wenzelm@19503
   144
    val _ = change (#2 (AxClassData.get thy)) (map_instances (f (x, th')));
wenzelm@19503
   145
  in th' end;
wenzelm@19503
   146
wenzelm@19503
   147
val store_classrel = store_instance (fn ((c1, c2), th) => fn (classes, classrel, arities, types) =>
wenzelm@19503
   148
  (classes
wenzelm@19503
   149
    |> Graph.default_node (c1, ())
wenzelm@19503
   150
    |> Graph.default_node (c2, ())
wenzelm@19503
   151
    |> Graph.add_edge (c1, c2),
wenzelm@19503
   152
    insert (eq_fst op =) ((c1, c2), th) classrel, arities, types));
wenzelm@19503
   153
wenzelm@19503
   154
val store_arity = store_instance (fn ((t, Ss, c), th) => fn (classes, classrel, arities, types) =>
wenzelm@19503
   155
  (classes, classrel, arities |> Symtab.insert_list (eq_fst op =) (t, ((c, Ss), th)), types));
wenzelm@19503
   156
wenzelm@19503
   157
val store_type = store_instance (fn ((T, c), th) => fn (classes, classrel, arities, types) =>
wenzelm@19503
   158
  (classes, classrel, arities, types |> Typtab.insert_list (eq_fst op =) (T, (c, th))));
wenzelm@19503
   159
wenzelm@19503
   160
wenzelm@19460
   161
(* print_axclasses *)
wenzelm@19460
   162
wenzelm@19460
   163
fun print_axclasses thy =
wenzelm@19460
   164
  let
wenzelm@19460
   165
    val axclasses = #1 (#1 (AxClassData.get thy));
wenzelm@19460
   166
    val ctxt = ProofContext.init thy;
wenzelm@19460
   167
wenzelm@19460
   168
    fun pretty_axclass (class, AxClass {def, intro, axioms}) =
wenzelm@19460
   169
      Pretty.block (Pretty.fbreaks
wenzelm@19460
   170
       [Pretty.block
wenzelm@19460
   171
          [Pretty.str "axclass ", ProofContext.pretty_sort ctxt [class], Pretty.str ":"],
wenzelm@19460
   172
        Pretty.strs ("parameters:" :: params_of thy class),
wenzelm@19460
   173
        ProofContext.pretty_fact ctxt ("def", [def]),
wenzelm@19460
   174
        ProofContext.pretty_fact ctxt (introN, [intro]),
wenzelm@19460
   175
        ProofContext.pretty_fact ctxt (axiomsN, axioms)]);
wenzelm@19460
   176
  in Pretty.writeln (Pretty.chunks (map pretty_axclass (Symtab.dest axclasses))) end;
wenzelm@19460
   177
wenzelm@19460
   178
wenzelm@404
   179
wenzelm@19503
   180
(** instance proofs **)
wenzelm@19418
   181
wenzelm@19418
   182
(* class relations *)
wenzelm@19418
   183
wenzelm@19405
   184
fun cert_classrel thy raw_rel =
wenzelm@15876
   185
  let
wenzelm@19405
   186
    val (c1, c2) = pairself (Sign.certify_class thy) raw_rel;
wenzelm@19405
   187
    val _ = Type.add_classrel (Sign.pp thy) [(c1, c2)] (Sign.tsig_of thy);
wenzelm@19405
   188
    val _ =
wenzelm@19460
   189
      (case subtract (op =) (all_params_of thy [c1]) (all_params_of thy [c2]) of
wenzelm@19405
   190
        [] => ()
wenzelm@19405
   191
      | xs => raise TYPE ("Class " ^ Sign.string_of_sort thy [c1] ^ " lacks parameter(s) " ^
wenzelm@19405
   192
          commas_quote xs ^ " of " ^ Sign.string_of_sort thy [c2], [], []));
wenzelm@19405
   193
  in (c1, c2) end;
wenzelm@19405
   194
wenzelm@19405
   195
fun read_classrel thy raw_rel =
wenzelm@19405
   196
  cert_classrel thy (pairself (Sign.read_class thy) raw_rel)
wenzelm@19405
   197
    handle TYPE (msg, _, _) => error msg;
wenzelm@19405
   198
wenzelm@19405
   199
wenzelm@19405
   200
(* primitive rules *)
wenzelm@19405
   201
wenzelm@19405
   202
fun add_classrel th thy =
wenzelm@19405
   203
  let
wenzelm@19405
   204
    fun err () = raise THM ("add_classrel: malformed class relation", 0, [th]);
wenzelm@19405
   205
    val prop = Drule.plain_prop_of (Thm.transfer thy th);
wenzelm@19405
   206
    val rel = Logic.dest_classrel prop handle TERM _ => err ();
wenzelm@19405
   207
    val (c1, c2) = cert_classrel thy rel handle TYPE _ => err ();
wenzelm@19503
   208
    val thy' = thy |> Theory.add_classrel_i [(c1, c2)];
wenzelm@19503
   209
    val _ = store_classrel thy' ((c1, c2), Drule.unconstrainTs th);
wenzelm@19503
   210
  in thy' end;
wenzelm@15876
   211
wenzelm@19405
   212
fun add_arity th thy =
wenzelm@15876
   213
  let
wenzelm@19405
   214
    val prop = Drule.plain_prop_of (Thm.transfer thy th);
wenzelm@19405
   215
    val (t, Ss, c) = Logic.dest_arity prop handle TERM _ =>
wenzelm@19405
   216
      raise THM ("add_arity: malformed type arity", 0, [th]);
wenzelm@19503
   217
    val thy' = thy |> Theory.add_arities_i [(t, Ss, [c])];
wenzelm@19503
   218
    val _ = store_arity thy' ((t, Ss, c), Drule.unconstrainTs th);
wenzelm@19503
   219
  in thy' end;
wenzelm@404
   220
wenzelm@404
   221
wenzelm@19243
   222
(* tactical proofs *)
wenzelm@15876
   223
wenzelm@19405
   224
fun prove_classrel raw_rel tac thy =
wenzelm@15876
   225
  let
wenzelm@19405
   226
    val (c1, c2) = cert_classrel thy raw_rel;
wenzelm@19405
   227
    val th = Goal.prove thy [] [] (Logic.mk_classrel (c1, c2)) (fn _ => tac) handle ERROR msg =>
wenzelm@19243
   228
      cat_error msg ("The error(s) above occurred while trying to prove class relation " ^
wenzelm@19243
   229
        quote (Sign.string_of_classrel thy [c1, c2]));
wenzelm@19405
   230
  in add_classrel th thy end;
wenzelm@404
   231
wenzelm@19243
   232
fun prove_arity raw_arity tac thy =
wenzelm@15876
   233
  let
wenzelm@19243
   234
    val arity = Sign.cert_arity thy raw_arity;
wenzelm@19405
   235
    val props = Logic.mk_arities arity;
wenzelm@17956
   236
    val ths = Goal.prove_multi thy [] [] props
wenzelm@18678
   237
      (fn _ => Tactic.precise_conjunction_tac (length props) 1 THEN tac) handle ERROR msg =>
wenzelm@19243
   238
        cat_error msg ("The error(s) above occurred while trying to prove type arity " ^
wenzelm@19243
   239
          quote (Sign.string_of_arity thy arity));
wenzelm@19405
   240
  in fold add_arity ths thy end;
wenzelm@19392
   241
wenzelm@19418
   242
wenzelm@19503
   243
(* derived instances -- cached *)
wenzelm@19503
   244
wenzelm@19503
   245
fun derive_classrel thy (c1, c2) =
wenzelm@19503
   246
  let
wenzelm@19503
   247
    val {classes, classrel, ...} = get_instances thy;
wenzelm@19503
   248
    val lookup = AList.lookup (op =) classrel;
wenzelm@19503
   249
    fun derive [c, c'] = the (lookup (c, c'))
wenzelm@19503
   250
      | derive (c :: c' :: cs) = derive [c, c'] RS derive (c' :: cs);
wenzelm@19503
   251
  in
wenzelm@19503
   252
    (case lookup (c1, c2) of
wenzelm@19503
   253
      SOME rule => rule
wenzelm@19503
   254
    | NONE =>
wenzelm@19503
   255
        (case Graph.find_paths classes (c1, c2) of
wenzelm@19503
   256
          [] => error ("Cannot derive class relation " ^ Sign.string_of_classrel thy [c1, c2])
wenzelm@19503
   257
        | path :: _ => store_classrel thy ((c1, c2), derive path)))
wenzelm@19503
   258
  end;
wenzelm@19503
   259
wenzelm@19503
   260
fun weaken_subclass thy (c1, th) c2 =
wenzelm@19503
   261
  if c1 = c2 then th
wenzelm@19503
   262
  else th RS derive_classrel thy (c1, c2);
wenzelm@19503
   263
wenzelm@19503
   264
fun weaken_subsort thy S1 S2 = S2 |> map (fn c2 =>
wenzelm@19503
   265
  (case S1 |> find_first (fn (c1, _) => Sign.subsort thy ([c1], [c2])) of
wenzelm@19503
   266
    SOME c1 => weaken_subclass thy c1 c2
wenzelm@19503
   267
  | NONE => error ("Cannot derive subsort relation " ^
wenzelm@19503
   268
      Sign.string_of_sort thy (map #1 S1) ^ " < " ^ Sign.string_of_sort thy S2)));
wenzelm@19503
   269
wenzelm@19503
   270
fun apply_arity thy t dom c =
wenzelm@19503
   271
  let
wenzelm@19503
   272
    val {arities, ...} = get_instances thy;
wenzelm@19503
   273
    val subsort = Sign.subsort thy;
wenzelm@19503
   274
    val Ss = map (map #1) dom;
wenzelm@19503
   275
  in
wenzelm@19503
   276
    (case Symtab.lookup_list arities t |> find_first (fn ((c', Ss'), _) =>
wenzelm@19503
   277
        subsort ([c'], [c]) andalso ListPair.all subsort (Ss, Ss')) of
wenzelm@19503
   278
      SOME ((c', Ss'), rule) =>
wenzelm@19503
   279
        weaken_subclass thy (c', rule OF flat (map2 (weaken_subsort thy) dom Ss')) c
wenzelm@19503
   280
    | NONE => error ("Cannot derive type arity " ^ Sign.string_of_arity thy (t, Ss, [c])))
wenzelm@19503
   281
  end;
wenzelm@19503
   282
wenzelm@19503
   283
fun derive_type thy hyps =
wenzelm@19503
   284
  let
wenzelm@19503
   285
    fun derive (Type (a, Ts)) S =
wenzelm@19503
   286
          let val Ss = Sign.arity_sorts thy a S
wenzelm@19503
   287
          in map (apply_arity thy a (map2 (fn T => fn S => S ~~ derive T S) Ts Ss)) S end
wenzelm@19503
   288
      | derive (TFree (a, [])) S =
wenzelm@19503
   289
          weaken_subsort thy (the_default [] (AList.lookup (op =) hyps a)) S
wenzelm@19503
   290
      | derive T _ = error ("Illegal occurrence of type variable " ^
wenzelm@19503
   291
          setmp show_sorts true (Sign.string_of_typ thy) T);
wenzelm@19503
   292
  in derive end;
wenzelm@19503
   293
wenzelm@19503
   294
fun of_sort thy (typ, sort) =
wenzelm@19503
   295
  let
wenzelm@19503
   296
    fun lookup () = AList.lookup (op =) (Typtab.lookup_list (#types (get_instances thy)) typ);
wenzelm@19503
   297
    val sort' = filter (is_none o lookup ()) sort;
wenzelm@19503
   298
    val _ = conditional (not (null sort')) (fn () =>
wenzelm@19503
   299
      let
wenzelm@19503
   300
        val vars = Term.fold_atyps (insert (op =)) typ [];
wenzelm@19503
   301
        val renaming =
wenzelm@19503
   302
          map2 (fn T => fn a => (T, (a, case T of TFree (_, S) => S | TVar (_, S) => S)))
wenzelm@19503
   303
            vars (Term.invent_names [] "'a" (length vars));
wenzelm@19503
   304
        val typ' = typ |> Term.map_atyps
wenzelm@19503
   305
          (fn T => TFree (#1 (the (AList.lookup (op =) renaming T)), []));
wenzelm@19503
   306
wenzelm@19503
   307
        val hyps = renaming |> map (fn (_, (a, S)) => (a, S ~~ (S |> map (fn c =>
wenzelm@19503
   308
          Thm.assume (Thm.cterm_of thy (Logic.mk_inclass (TFree (a, []), c)))))));
wenzelm@19503
   309
        val inst = renaming |> map (fn (T, (a, S)) =>
wenzelm@19503
   310
          pairself (Thm.ctyp_of thy) (TVar ((a, 0), S), T));
wenzelm@19503
   311
wenzelm@19503
   312
        val ths =
wenzelm@19503
   313
          derive_type thy hyps typ' sort'
wenzelm@19503
   314
          |> map (Thm.instantiate (inst, []));
wenzelm@19503
   315
        val _ = map (store_type thy) (map (pair typ) sort' ~~ ths);
wenzelm@19503
   316
      in () end);
wenzelm@19503
   317
  in map (the o lookup ()) sort end;
wenzelm@19503
   318
wenzelm@19503
   319
wenzelm@19418
   320
wenzelm@19418
   321
(** axclass definitions **)
wenzelm@19418
   322
wenzelm@19418
   323
(* add_axclass(_i) *)
wenzelm@19418
   324
wenzelm@19418
   325
fun gen_axclass prep_class prep_att prep_propp
wenzelm@19418
   326
    (bclass, raw_super) params raw_specs thy =
wenzelm@19418
   327
  let
wenzelm@19418
   328
    val ctxt = ProofContext.init thy;
wenzelm@19418
   329
    val pp = ProofContext.pp ctxt;
wenzelm@19418
   330
wenzelm@19418
   331
wenzelm@19418
   332
    (* prepare specification *)
wenzelm@19418
   333
wenzelm@19418
   334
    val bconst = Logic.const_of_class bclass;
wenzelm@19418
   335
    val class = Sign.full_name thy bclass;
wenzelm@19418
   336
    val super = map (prep_class thy) raw_super |> Sign.certify_sort thy;
wenzelm@19418
   337
wenzelm@19418
   338
    fun prep_axiom t =
wenzelm@19418
   339
      (case Term.add_tfrees t [] of
wenzelm@19418
   340
        [(a, S)] =>
wenzelm@19418
   341
          if Sign.subsort thy (super, S) then t
wenzelm@19418
   342
          else error ("Sort constraint of type variable " ^
wenzelm@19418
   343
            setmp show_sorts true (Pretty.string_of_typ pp) (TFree (a, S)) ^
wenzelm@19418
   344
            " needs to be weaker than " ^ Pretty.string_of_sort pp super)
wenzelm@19418
   345
      | [] => t
wenzelm@19418
   346
      | _ => error ("Multiple type variables in class axiom:\n" ^ Pretty.string_of_term pp t))
wenzelm@19418
   347
      |> map_term_types (Term.map_atyps (fn TFree _ => Term.aT [] | U => U))
wenzelm@19418
   348
      |> Logic.close_form;
wenzelm@19418
   349
wenzelm@19418
   350
    val axiomss = prep_propp (ctxt, map (map (rpair ([], [])) o snd) raw_specs)
wenzelm@19418
   351
      |> snd |> map (map (prep_axiom o fst));
wenzelm@19418
   352
    val name_atts = Attrib.map_specs (prep_att thy) raw_specs |> map fst;
wenzelm@19418
   353
wenzelm@19418
   354
wenzelm@19418
   355
    (* definition *)
wenzelm@19418
   356
wenzelm@19482
   357
    val conjs = map (curry Logic.mk_inclass (Term.aT [])) super @ flat axiomss;
wenzelm@19418
   358
    val class_eq =
wenzelm@19418
   359
      Logic.mk_equals (Logic.mk_inclass (Term.aT [], class), Logic.mk_conjunction_list conjs);
wenzelm@19418
   360
wenzelm@19418
   361
    val ([def], def_thy) =
wenzelm@19418
   362
      thy
wenzelm@19418
   363
      |> Theory.add_classes_i [(bclass, super)]
wenzelm@19418
   364
      |> PureThy.add_defs_i false [((Thm.def_name bconst, class_eq), [])];
wenzelm@19418
   365
    val (raw_intro, (raw_classrel, raw_axioms)) =
wenzelm@19418
   366
      (Conjunction.split_defined (length conjs) def) ||> chop (length super);
wenzelm@19418
   367
wenzelm@19418
   368
wenzelm@19418
   369
    (* facts *)
wenzelm@19418
   370
wenzelm@19418
   371
    val class_triv = Thm.class_triv def_thy class;
wenzelm@19418
   372
    val ([(_, [intro]), (_, axioms)], facts_thy) =
wenzelm@19418
   373
      def_thy
wenzelm@19418
   374
      |> PureThy.note_thmss_qualified "" bconst
wenzelm@19418
   375
        [((introN, []), [([Drule.standard raw_intro], [])]),
wenzelm@19503
   376
         ((axiomsN, []), [(map (fn th => Drule.standard (class_triv RS th)) raw_axioms, [])])];
wenzelm@19503
   377
    val _ = map (store_classrel facts_thy)
wenzelm@19503
   378
      (map (pair class) super ~~ map Drule.standard raw_classrel);
wenzelm@19418
   379
wenzelm@19418
   380
wenzelm@19418
   381
    (* result *)
wenzelm@19418
   382
wenzelm@19418
   383
    val result_thy =
wenzelm@19418
   384
      facts_thy
wenzelm@19418
   385
      |> Sign.add_path bconst
wenzelm@19418
   386
      |> PureThy.note_thmss_i "" (name_atts ~~ map Thm.simple_fact (unflat axiomss axioms)) |> snd
wenzelm@19418
   387
      |> Sign.restore_naming facts_thy
wenzelm@19503
   388
      |> AxClassData.map (apfst (fn (axclasses, parameters) =>
wenzelm@19503
   389
        (Symtab.update (class, make_axclass (def, intro, axioms)) axclasses,
wenzelm@19503
   390
          fold (fn x => add_param pp (x, class)) params parameters)));
wenzelm@19418
   391
wenzelm@19418
   392
  in (class, result_thy) end;
wenzelm@19418
   393
wenzelm@19418
   394
val add_axclass = gen_axclass Sign.read_class Attrib.attribute ProofContext.read_propp;
wenzelm@19418
   395
val add_axclass_i = gen_axclass Sign.certify_class (K I) ProofContext.cert_propp;
wenzelm@19418
   396
wenzelm@15876
   397
end;