src/Pure/axclass.ML
author wenzelm
Sun Apr 25 21:18:04 2010 +0200 (2010-04-25 ago)
changeset 36328 4d9deabf6474
parent 36327 c0415cb24a10
child 36329 85004134055c
permissions -rw-r--r--
replaced Sorts.rep_algebra by slightly more abstract selectors classes_of/arities_of;
wenzelm@404
     1
(*  Title:      Pure/axclass.ML
wenzelm@404
     2
    Author:     Markus Wenzel, TU Muenchen
wenzelm@404
     3
wenzelm@24964
     4
Type classes defined as predicates, associated with a record of
wenzelm@24964
     5
parameters.
wenzelm@404
     6
*)
wenzelm@404
     7
wenzelm@404
     8
signature AX_CLASS =
wenzelm@3938
     9
sig
wenzelm@30344
    10
  val define_class: binding * class list -> string list ->
wenzelm@30211
    11
    (Thm.binding * term list) list -> theory -> class * theory
haftmann@24589
    12
  val add_classrel: thm -> theory -> theory
haftmann@24589
    13
  val add_arity: thm -> theory -> theory
haftmann@24589
    14
  val prove_classrel: class * class -> tactic -> theory -> theory
haftmann@24589
    15
  val prove_arity: string * sort list * sort -> tactic -> theory -> theory
wenzelm@36327
    16
  type info = {def: thm, intro: thm, axioms: thm list, params: (string * typ) list}
wenzelm@36327
    17
  val get_info: theory -> class -> info
wenzelm@19243
    18
  val class_intros: theory -> thm list
haftmann@20107
    19
  val class_of_param: theory -> string -> class option
wenzelm@19405
    20
  val cert_classrel: theory -> class * class -> class * class
wenzelm@19405
    21
  val read_classrel: theory -> xstring * xstring -> class * class
wenzelm@30344
    22
  val axiomatize_class: binding * class list -> theory -> theory
wenzelm@30344
    23
  val axiomatize_class_cmd: binding * xstring list -> theory -> theory
wenzelm@24929
    24
  val axiomatize_classrel: (class * class) list -> theory -> theory
wenzelm@24929
    25
  val axiomatize_classrel_cmd: (xstring * xstring) list -> theory -> theory
wenzelm@24929
    26
  val axiomatize_arity: arity -> theory -> theory
wenzelm@24929
    27
  val axiomatize_arity_cmd: xstring * string list * string -> theory -> theory
haftmann@25486
    28
  val instance_name: string * class -> string
haftmann@25597
    29
  val declare_overloaded: string * typ -> theory -> term * theory
haftmann@30519
    30
  val define_overloaded: binding -> string * term -> theory -> thm * theory
haftmann@25597
    31
  val unoverload: theory -> thm -> thm
haftmann@25597
    32
  val overload: theory -> thm -> thm
haftmann@25597
    33
  val unoverload_conv: theory -> conv
haftmann@25597
    34
  val overload_conv: theory -> conv
haftmann@25597
    35
  val unoverload_const: theory -> string * typ -> string
haftmann@31249
    36
  val lookup_inst_param: Consts.T -> ((string * string) * 'a) list -> string * typ -> 'a option
haftmann@25597
    37
  val param_of_inst: theory -> string * string -> string
haftmann@25597
    38
  val inst_of_param: theory -> string -> (string * string) option
wenzelm@33172
    39
  val thynames_of_arity: theory -> class * string -> string list
wenzelm@3938
    40
end;
wenzelm@404
    41
wenzelm@15801
    42
structure AxClass: AX_CLASS =
wenzelm@404
    43
struct
wenzelm@404
    44
wenzelm@19405
    45
(** theory data **)
wenzelm@423
    46
wenzelm@19405
    47
(* class parameters (canonical order) *)
wenzelm@423
    48
wenzelm@19405
    49
type param = string * class;
wenzelm@423
    50
wenzelm@19405
    51
fun add_param pp ((x, c): param) params =
wenzelm@19405
    52
  (case AList.lookup (op =) params x of
wenzelm@19405
    53
    NONE => (x, c) :: params
wenzelm@19405
    54
  | SOME c' => error ("Duplicate class parameter " ^ quote x ^
wenzelm@19405
    55
      " for " ^ Pretty.string_of_sort pp [c] ^
wenzelm@19405
    56
      (if c = c' then "" else " and " ^ Pretty.string_of_sort pp [c'])));
wenzelm@423
    57
wenzelm@19405
    58
fun merge_params _ ([], qs) = qs
wenzelm@19405
    59
  | merge_params pp (ps, qs) =
wenzelm@19405
    60
      fold_rev (fn q => if member (op =) ps q then I else add_param pp q) qs ps;
wenzelm@423
    61
wenzelm@423
    62
wenzelm@36327
    63
(* axclass info *)
wenzelm@6379
    64
wenzelm@36327
    65
type info =
wenzelm@19460
    66
 {def: thm,
wenzelm@19392
    67
  intro: thm,
haftmann@21463
    68
  axioms: thm list,
haftmann@21925
    69
  params: (string * typ) list};
wenzelm@19392
    70
wenzelm@36327
    71
type axclasses = info Symtab.table * param list;
wenzelm@19392
    72
wenzelm@36327
    73
fun make_axclass ((def, intro, axioms), params): info =
haftmann@27397
    74
  {def = def, intro = intro, axioms = axioms, params = params};
wenzelm@19405
    75
wenzelm@19405
    76
fun merge_axclasses pp ((tab1, params1), (tab2, params2)) : axclasses =
wenzelm@19405
    77
  (Symtab.merge (K true) (tab1, tab2), merge_params pp (params1, params2));
wenzelm@19405
    78
wenzelm@19392
    79
wenzelm@19392
    80
(* instances *)
wenzelm@19392
    81
wenzelm@20628
    82
val classrel_prefix = "classrel_";
wenzelm@20628
    83
val arity_prefix = "arity_";
wenzelm@19511
    84
wenzelm@19574
    85
type instances =
wenzelm@36325
    86
  (thm * proof) Symreltab.table *  (*classrel theorems*)
wenzelm@36325
    87
  ((class * sort list) * ((thm * string) * proof)) list Symtab.table;  (*arity theorems with theory name*)
wenzelm@6379
    88
wenzelm@36325
    89
(*transitive closure of classrels and arity completion is done in Theory.at_begin hook*)
wenzelm@19574
    90
fun merge_instances ((classrel1, arities1): instances, (classrel2, arities2)) =
wenzelm@36325
    91
 (Symreltab.join (K fst) (classrel1, classrel2),
wenzelm@19574
    92
  Symtab.join (K (merge (eq_fst op =))) (arities1, arities2));
wenzelm@19392
    93
wenzelm@19392
    94
haftmann@25597
    95
(* instance parameters *)
haftmann@25597
    96
haftmann@25597
    97
type inst_params =
haftmann@25597
    98
  (string * thm) Symtab.table Symtab.table
haftmann@25597
    99
    (*constant name ~> type constructor ~> (constant name, equation)*)
haftmann@25597
   100
  * (string * string) Symtab.table; (*constant name ~> (constant name, type constructor)*)
haftmann@25597
   101
haftmann@25597
   102
fun merge_inst_params ((const_param1, param_const1), (const_param2, param_const2)) =
haftmann@25597
   103
  (Symtab.join  (K (Symtab.merge (K true))) (const_param1, const_param2),
haftmann@25597
   104
    Symtab.merge (K true) (param_const1, param_const2));
haftmann@25597
   105
haftmann@25597
   106
wenzelm@19511
   107
(* setup data *)
wenzelm@19392
   108
wenzelm@36327
   109
structure Data = Theory_Data_PP
wenzelm@22846
   110
(
wenzelm@36325
   111
  type T = axclasses * ((instances * inst_params) * (class * class) list);
wenzelm@36325
   112
  val empty = ((Symtab.empty, []), (((Symreltab.empty, Symtab.empty), (Symtab.empty, Symtab.empty)), []));
wenzelm@19574
   113
  val extend = I;
wenzelm@36325
   114
  fun merge pp ((axclasses1, ((instances1, inst_params1), diff_merge_classrels1)),
wenzelm@36325
   115
    (axclasses2, ((instances2, inst_params2), diff_merge_classrels2))) =
wenzelm@36325
   116
    let
wenzelm@36327
   117
      val (classrels1, classrels2) = pairself (Symreltab.keys o fst) (instances1, instances2);
wenzelm@36327
   118
      val diff_merge_classrels =
wenzelm@36327
   119
        subtract (op =) classrels1 classrels2 @
wenzelm@36327
   120
        subtract (op =) classrels2 classrels1 @
wenzelm@36327
   121
        diff_merge_classrels1 @ diff_merge_classrels2;
wenzelm@36325
   122
    in
wenzelm@36325
   123
      (merge_axclasses pp (axclasses1, axclasses2),
wenzelm@36325
   124
        ((merge_instances (instances1, instances2), merge_inst_params (inst_params1, inst_params2)),
wenzelm@36325
   125
          diff_merge_classrels))
wenzelm@36325
   126
    end;
wenzelm@22846
   127
);
wenzelm@6379
   128
wenzelm@6379
   129
wenzelm@19574
   130
(* maintain axclasses *)
wenzelm@19392
   131
wenzelm@36327
   132
val get_axclasses = #1 o Data.get;
wenzelm@36327
   133
val map_axclasses = Data.map o apfst;
wenzelm@6379
   134
wenzelm@24929
   135
fun get_info thy c =
wenzelm@36327
   136
  (case Symtab.lookup (#1 (get_axclasses thy)) c of
wenzelm@36327
   137
    SOME info => info
wenzelm@21919
   138
  | NONE => error ("No such axclass: " ^ quote c));
wenzelm@6379
   139
wenzelm@19123
   140
fun class_intros thy =
wenzelm@19392
   141
  let
wenzelm@36327
   142
    fun add_intro c = (case try (get_info thy) c of SOME {intro, ...} => cons intro | _ => I);
wenzelm@21931
   143
    val classes = Sign.all_classes thy;
wenzelm@19392
   144
  in map (Thm.class_triv thy) classes @ fold add_intro classes [] end;
wenzelm@15876
   145
wenzelm@36327
   146
fun all_params_of thy S =
wenzelm@19574
   147
  let val params = #2 (get_axclasses thy);
wenzelm@36327
   148
  in fold (fn (x, c) => if Sign.subsort thy (S, [c]) then cons x else I) params [] end;
wenzelm@19460
   149
wenzelm@24964
   150
fun class_of_param thy = AList.lookup (op =) (#2 (get_axclasses thy));
wenzelm@19460
   151
wenzelm@21919
   152
wenzelm@19511
   153
(* maintain instances *)
wenzelm@19503
   154
wenzelm@30364
   155
fun instance_name (a, c) = Long_Name.base_name c ^ "_" ^ Long_Name.base_name a;
haftmann@25486
   156
wenzelm@36327
   157
val get_instances = #1 o #1 o #2 o Data.get;
wenzelm@36327
   158
val map_instances = Data.map o apsnd o apfst o apfst;
wenzelm@36325
   159
wenzelm@36327
   160
val get_diff_merge_classrels = #2 o #2 o Data.get;
wenzelm@36327
   161
val clear_diff_merge_classrels = Data.map (apsnd (apsnd (K [])));
wenzelm@19528
   162
wenzelm@19528
   163
wenzelm@19574
   164
fun the_classrel thy (c1, c2) =
wenzelm@36325
   165
  (case Symreltab.lookup (#1 (get_instances thy)) (c1, c2) of
wenzelm@36325
   166
    SOME classrel => classrel
wenzelm@24920
   167
  | NONE => error ("Unproven class relation " ^
wenzelm@24920
   168
      Syntax.string_of_classrel (ProofContext.init thy) [c1, c2]));
wenzelm@19574
   169
wenzelm@36325
   170
fun the_classrel_thm thy = Thm.transfer thy o fst o the_classrel thy;
wenzelm@36325
   171
fun the_classrel_prf thy = snd o the_classrel thy;
wenzelm@36325
   172
wenzelm@36325
   173
fun put_trancl_classrel ((c1, c2), th) thy =
wenzelm@36325
   174
  let
wenzelm@36327
   175
    val cert = Thm.cterm_of thy;
wenzelm@36327
   176
    val certT = Thm.ctyp_of thy;
wenzelm@36327
   177
wenzelm@36327
   178
    val classrels = fst (get_instances thy);
wenzelm@36328
   179
    val classes = Sorts.classes_of (Sign.classes_of thy);
wenzelm@36325
   180
wenzelm@36325
   181
    fun reflcl_classrel (c1', c2') =
wenzelm@36327
   182
      if c1' = c2'
wenzelm@36327
   183
      then Thm.trivial (cert (Logic.mk_of_class (TVar ((Name.aT, 0), []), c1')))
wenzelm@36327
   184
      else the_classrel_thm thy (c1', c2');
wenzelm@36325
   185
    fun gen_classrel (c1_pred, c2_succ) =
wenzelm@36325
   186
      let
wenzelm@36325
   187
        val th' = ((reflcl_classrel (c1_pred, c1) RS th) RS reflcl_classrel (c2, c2_succ))
wenzelm@36327
   188
          |> Drule.instantiate' [SOME (certT (TVar ((Name.aT, 0), [])))] []
wenzelm@36327
   189
          |> Thm.close_derivation;
wenzelm@36327
   190
        val prf' = th' |> Thm.proof_of;
wenzelm@36327
   191
      in ((c1_pred, c2_succ), (th', prf')) end;
wenzelm@36325
   192
wenzelm@36327
   193
    val new_classrels =
wenzelm@36327
   194
      Library.map_product pair (c1 :: Graph.imm_preds classes c1) (c2 :: Graph.imm_succs classes c2)
wenzelm@36325
   195
      |> filter_out (Symreltab.defined classrels)
wenzelm@36327
   196
      |> map gen_classrel;
wenzelm@36327
   197
    val needed = not (null new_classrels);
wenzelm@36325
   198
  in
wenzelm@36325
   199
    (needed,
wenzelm@36325
   200
     if needed then
wenzelm@36325
   201
       thy |> map_instances (fn (classrels, arities) =>
wenzelm@36325
   202
         (classrels |> fold Symreltab.update new_classrels, arities))
wenzelm@36325
   203
     else thy)
wenzelm@36325
   204
  end;
wenzelm@36325
   205
wenzelm@36325
   206
fun complete_classrels thy =
wenzelm@36325
   207
  let
wenzelm@36327
   208
    val diff_merge_classrels = get_diff_merge_classrels thy;
wenzelm@36327
   209
    val classrels = fst (get_instances thy);
wenzelm@36325
   210
    val (needed, thy') = (false, thy) |>
wenzelm@36325
   211
      fold (fn c12 => fn (needed, thy) =>
wenzelm@36325
   212
          put_trancl_classrel (c12, Symreltab.lookup classrels c12 |> the |> fst) thy
wenzelm@36325
   213
          |>> (fn b => needed orelse b))
wenzelm@36327
   214
        diff_merge_classrels;
wenzelm@36325
   215
  in
wenzelm@36325
   216
    if null diff_merge_classrels then NONE
wenzelm@36325
   217
    else thy' |> clear_diff_merge_classrels |> SOME
wenzelm@36325
   218
  end;
wenzelm@19503
   219
wenzelm@19503
   220
wenzelm@19574
   221
fun the_arity thy a (c, Ss) =
wenzelm@24929
   222
  (case AList.lookup (op =) (Symtab.lookup_list (#2 (get_instances thy)) a) (c, Ss) of
wenzelm@36325
   223
    SOME arity => arity
wenzelm@24920
   224
  | NONE => error ("Unproven type arity " ^
wenzelm@24920
   225
      Syntax.string_of_arity (ProofContext.init thy) (a, Ss, [c])));
wenzelm@19503
   226
wenzelm@36325
   227
fun the_arity_thm thy a c_Ss = the_arity thy a c_Ss |> fst |> fst |> Thm.transfer thy;
wenzelm@36325
   228
fun the_arity_prf thy a c_Ss = the_arity thy a c_Ss |> snd;
wenzelm@36325
   229
wenzelm@33172
   230
fun thynames_of_arity thy (c, a) =
wenzelm@33172
   231
  Symtab.lookup_list (#2 (get_instances thy)) a
wenzelm@36325
   232
  |> map_filter (fn ((c', _), ((_, name),_)) => if c = c' then SOME name else NONE)
haftmann@27497
   233
  |> rev;
haftmann@27397
   234
wenzelm@36325
   235
fun insert_arity_completions thy (t, ((c, Ss), ((th, thy_name), _))) arities =
haftmann@27547
   236
  let
haftmann@27547
   237
    val algebra = Sign.classes_of thy;
wenzelm@33172
   238
    val super_class_completions =
wenzelm@33172
   239
      Sign.super_classes thy c
haftmann@27547
   240
      |> filter_out (fn c1 => exists (fn ((c2, Ss2), _) => c1 = c2
wenzelm@33172
   241
          andalso Sorts.sorts_le algebra (Ss2, Ss)) (Symtab.lookup_list arities t));
wenzelm@36325
   242
    val names_and_Ss = Name.names Name.context Name.aT (map (K []) Ss);
wenzelm@36325
   243
    val completions = super_class_completions |> map (fn c1 =>
wenzelm@36325
   244
      let
wenzelm@36325
   245
        val th1 = (th RS the_classrel_thm thy (c, c1))
wenzelm@36325
   246
          |> Drule.instantiate' (map (SOME o ctyp_of thy o TVar o apfst (rpair 0)) names_and_Ss) []
wenzelm@36327
   247
          |> Thm.close_derivation;
wenzelm@36327
   248
        val prf1 = Thm.proof_of th1;
wenzelm@36327
   249
      in (((th1, thy_name), prf1), c1) end);
wenzelm@36325
   250
    val arities' = fold (fn (th_thy_prf1, c1) => Symtab.cons_list (t, ((c1, Ss), th_thy_prf1)))
haftmann@27547
   251
      completions arities;
wenzelm@33172
   252
  in (null completions, arities') end;
wenzelm@19503
   253
haftmann@27547
   254
fun put_arity ((t, Ss, c), th) thy =
haftmann@27547
   255
  let
wenzelm@36325
   256
    val arity' = (t, ((c, Ss), ((th, Context.theory_name thy), Thm.proof_of th)));
haftmann@27547
   257
  in
haftmann@27547
   258
    thy
haftmann@27547
   259
    |> map_instances (fn (classrel, arities) => (classrel,
haftmann@27547
   260
      arities
haftmann@27547
   261
      |> Symtab.insert_list (eq_fst op =) arity'
haftmann@27547
   262
      |> insert_arity_completions thy arity'
haftmann@27547
   263
      |> snd))
haftmann@27547
   264
  end;
wenzelm@19503
   265
wenzelm@31944
   266
fun complete_arities thy =
haftmann@27547
   267
  let
haftmann@27547
   268
    val arities = snd (get_instances thy);
wenzelm@33172
   269
    val (finished, arities') = arities
wenzelm@33172
   270
      |> fold_map (insert_arity_completions thy) (Symtab.dest_list arities);
wenzelm@33172
   271
  in
wenzelm@33172
   272
    if forall I finished then NONE
haftmann@27547
   273
    else SOME (thy |> map_instances (fn (classrel, _) => (classrel, arities')))
haftmann@27547
   274
  end;
haftmann@27547
   275
wenzelm@36325
   276
val _ = Context.>> (Context.map_theory
wenzelm@36325
   277
  (Theory.at_begin complete_classrels #> Theory.at_begin complete_arities))
haftmann@27397
   278
haftmann@27397
   279
haftmann@25597
   280
(* maintain instance parameters *)
haftmann@25597
   281
wenzelm@36327
   282
val get_inst_params = #2 o #1 o #2 o Data.get;
wenzelm@36327
   283
val map_inst_params = Data.map o apsnd o apfst o apsnd;
haftmann@25597
   284
haftmann@25597
   285
fun get_inst_param thy (c, tyco) =
wenzelm@36327
   286
  (case Symtab.lookup (the_default Symtab.empty (Symtab.lookup (#1 (get_inst_params thy)) c)) tyco of
wenzelm@36327
   287
    SOME c' => c'
wenzelm@36327
   288
  | NONE => error ("No instance parameter for constant " ^ quote c ^ " on type " ^ quote tyco));
haftmann@25597
   289
wenzelm@36327
   290
fun add_inst_param (c, tyco) inst =
wenzelm@36327
   291
  (map_inst_params o apfst o Symtab.map_default (c, Symtab.empty)) (Symtab.update_new (tyco, inst))
haftmann@25597
   292
  #> (map_inst_params o apsnd) (Symtab.update_new (fst inst, (c, tyco)));
haftmann@25597
   293
haftmann@25597
   294
val inst_of_param = Symtab.lookup o snd o get_inst_params;
haftmann@25597
   295
val param_of_inst = fst oo get_inst_param;
haftmann@25597
   296
wenzelm@36327
   297
fun inst_thms thy =
wenzelm@36327
   298
  (Symtab.fold (Symtab.fold (cons o snd o snd) o snd) o fst) (get_inst_params thy) [];
haftmann@25597
   299
haftmann@31249
   300
fun get_inst_tyco consts = try (fst o dest_Type o the_single o Consts.typargs consts);
haftmann@25597
   301
haftmann@25597
   302
fun unoverload thy = MetaSimplifier.simplify true (inst_thms thy);
haftmann@25597
   303
fun overload thy = MetaSimplifier.simplify true (map Thm.symmetric (inst_thms thy));
haftmann@25597
   304
haftmann@25597
   305
fun unoverload_conv thy = MetaSimplifier.rewrite true (inst_thms thy);
haftmann@25597
   306
fun overload_conv thy = MetaSimplifier.rewrite true (map Thm.symmetric (inst_thms thy));
haftmann@25597
   307
wenzelm@36327
   308
fun lookup_inst_param consts params (c, T) =
wenzelm@36327
   309
  (case get_inst_tyco consts (c, T) of
wenzelm@36327
   310
    SOME tyco => AList.lookup (op =) params (c, tyco)
wenzelm@36327
   311
  | NONE => NONE);
haftmann@31249
   312
haftmann@25597
   313
fun unoverload_const thy (c_ty as (c, _)) =
wenzelm@36327
   314
  if is_some (class_of_param thy c) then
wenzelm@36327
   315
    (case get_inst_tyco (Sign.consts_of thy) c_ty of
wenzelm@36327
   316
      SOME tyco => try (param_of_inst thy) (c, tyco) |> the_default c
wenzelm@36327
   317
    | NONE => c)
haftmann@33969
   318
  else c;
haftmann@25597
   319
wenzelm@404
   320
wenzelm@36327
   321
wenzelm@19511
   322
(** instances **)
wenzelm@19418
   323
wenzelm@19418
   324
(* class relations *)
wenzelm@19418
   325
wenzelm@19405
   326
fun cert_classrel thy raw_rel =
wenzelm@15876
   327
  let
wenzelm@26939
   328
    val string_of_sort = Syntax.string_of_sort_global thy;
wenzelm@19405
   329
    val (c1, c2) = pairself (Sign.certify_class thy) raw_rel;
wenzelm@24271
   330
    val _ = Sign.primitive_classrel (c1, c2) (Theory.copy thy);
wenzelm@19405
   331
    val _ =
wenzelm@19460
   332
      (case subtract (op =) (all_params_of thy [c1]) (all_params_of thy [c2]) of
wenzelm@19405
   333
        [] => ()
wenzelm@26939
   334
      | xs => raise TYPE ("Class " ^ string_of_sort [c1] ^ " lacks parameter(s) " ^
wenzelm@26939
   335
          commas_quote xs ^ " of " ^ string_of_sort [c2], [], []));
wenzelm@19405
   336
  in (c1, c2) end;
wenzelm@19405
   337
wenzelm@19405
   338
fun read_classrel thy raw_rel =
wenzelm@35669
   339
  cert_classrel thy (pairself (ProofContext.read_class (ProofContext.init thy)) raw_rel)
wenzelm@19405
   340
    handle TYPE (msg, _, _) => error msg;
wenzelm@19405
   341
wenzelm@36327
   342
val shyps_topped = forall null o #shyps o Thm.rep_thm;
wenzelm@36327
   343
wenzelm@19405
   344
haftmann@25597
   345
(* declaration and definition of instances of overloaded constants *)
haftmann@25597
   346
wenzelm@35201
   347
fun inst_tyco_of thy (c, T) =
wenzelm@35201
   348
  (case get_inst_tyco (Sign.consts_of thy) (c, T) of
wenzelm@35201
   349
    SOME tyco => tyco
wenzelm@35201
   350
  | NONE => error ("Illegal type for instantiation of class parameter: " ^
wenzelm@35201
   351
      quote (c ^ " :: " ^ Syntax.string_of_typ_global thy T)));
haftmann@31249
   352
haftmann@25597
   353
fun declare_overloaded (c, T) thy =
haftmann@25597
   354
  let
wenzelm@35201
   355
    val class =
wenzelm@35201
   356
      (case class_of_param thy c of
wenzelm@35201
   357
        SOME class => class
wenzelm@35201
   358
      | NONE => error ("Not a class parameter: " ^ quote c));
haftmann@31249
   359
    val tyco = inst_tyco_of thy (c, T);
haftmann@25597
   360
    val name_inst = instance_name (tyco, class) ^ "_inst";
wenzelm@30364
   361
    val c' = Long_Name.base_name c ^ "_" ^ Long_Name.base_name tyco;
haftmann@25597
   362
    val T' = Type.strip_sorts T;
haftmann@25597
   363
  in
haftmann@25597
   364
    thy
wenzelm@35201
   365
    |> Sign.qualified_path true (Binding.name name_inst)
wenzelm@33173
   366
    |> Sign.declare_const ((Binding.name c', T'), NoSyn)
wenzelm@30344
   367
    |-> (fn const' as Const (c'', _) =>
wenzelm@30344
   368
      Thm.add_def false true
wenzelm@30344
   369
        (Binding.name (Thm.def_name c'), Logic.mk_equals (Const (c, T'), const'))
wenzelm@36106
   370
      #>> apsnd Thm.varifyT_global
wenzelm@36106
   371
      #-> (fn (_, thm) => add_inst_param (c, tyco) (c'', thm)
wenzelm@36106
   372
        #> PureThy.add_thm ((Binding.conceal (Binding.name c'), thm), [])
wenzelm@36106
   373
        #> snd
wenzelm@36106
   374
        #> pair (Const (c, T))))
wenzelm@35201
   375
    ||> Sign.restore_naming thy
haftmann@25597
   376
  end;
haftmann@25597
   377
haftmann@30519
   378
fun define_overloaded b (c, t) thy =
haftmann@25597
   379
  let
haftmann@25597
   380
    val T = Term.fastype_of t;
haftmann@31249
   381
    val tyco = inst_tyco_of thy (c, T);
haftmann@25597
   382
    val (c', eq) = get_inst_param thy (c, tyco);
haftmann@25597
   383
    val prop = Logic.mk_equals (Const (c', T), t);
haftmann@30519
   384
    val b' = Thm.def_binding_optional
haftmann@30519
   385
      (Binding.name (Long_Name.base_name c ^ "_" ^ Long_Name.base_name tyco)) b;
haftmann@25597
   386
  in
haftmann@25597
   387
    thy
haftmann@30519
   388
    |> Thm.add_def false false (b', prop)
wenzelm@36106
   389
    |>> (fn (_, thm) =>  Drule.transitive_thm OF [eq, thm])
haftmann@25597
   390
  end;
haftmann@25597
   391
haftmann@25597
   392
haftmann@30951
   393
(* primitive rules *)
haftmann@30951
   394
wenzelm@31948
   395
fun add_classrel raw_th thy =
haftmann@30951
   396
  let
wenzelm@31948
   397
    val th = Thm.strip_shyps (Thm.transfer thy raw_th);
wenzelm@31948
   398
    val prop = Thm.plain_prop_of th;
haftmann@30951
   399
    fun err () = raise THM ("add_classrel: malformed class relation", 0, [th]);
haftmann@30951
   400
    val rel = Logic.dest_classrel prop handle TERM _ => err ();
haftmann@30951
   401
    val (c1, c2) = cert_classrel thy rel handle TYPE _ => err ();
wenzelm@36325
   402
    val th' = th
wenzelm@36325
   403
      |> Drule.instantiate' [SOME (ctyp_of thy (TVar ((Name.aT, 0), [c1])))] []
wenzelm@36325
   404
      |> Drule.unconstrainTs;
wenzelm@36327
   405
    val _ = shyps_topped th' orelse raise Fail "add_classrel: nontop shyps after unconstrain";
haftmann@30951
   406
  in
haftmann@30951
   407
    thy
haftmann@30951
   408
    |> Sign.primitive_classrel (c1, c2)
wenzelm@36325
   409
    |> (snd oo put_trancl_classrel) ((c1, c2), th')
haftmann@30951
   410
    |> perhaps complete_arities
haftmann@30951
   411
  end;
haftmann@30951
   412
wenzelm@31948
   413
fun add_arity raw_th thy =
haftmann@30951
   414
  let
wenzelm@31948
   415
    val th = Thm.strip_shyps (Thm.transfer thy raw_th);
wenzelm@31948
   416
    val prop = Thm.plain_prop_of th;
haftmann@30951
   417
    fun err () = raise THM ("add_arity: malformed type arity", 0, [th]);
haftmann@30951
   418
    val (t, Ss, c) = Logic.dest_arity prop handle TERM _ => err ();
wenzelm@36325
   419
    val names = Name.names Name.context Name.aT Ss;
wenzelm@36325
   420
    val T = Type (t, map TFree names);
haftmann@30951
   421
    val missing_params = Sign.complete_sort thy [c]
haftmann@30951
   422
      |> maps (these o Option.map #params o try (get_info thy))
haftmann@30951
   423
      |> filter_out (fn (const, _) => can (get_inst_param thy) (const, t))
haftmann@30951
   424
      |> (map o apsnd o map_atyps) (K T);
haftmann@30951
   425
    val _ = map (Sign.certify_sort thy) Ss = Ss orelse err ();
wenzelm@36325
   426
    val th' = th
wenzelm@36325
   427
      |> Drule.instantiate' (map (SOME o ctyp_of thy o TVar o apfst (rpair 0)) names) []
wenzelm@36325
   428
      |> Drule.unconstrainTs;
wenzelm@36327
   429
    val _ = shyps_topped th' orelse raise Fail "add_arity: nontop shyps after unconstrain";
haftmann@30951
   430
  in
haftmann@30951
   431
    thy
haftmann@30951
   432
    |> fold (snd oo declare_overloaded) missing_params
haftmann@30951
   433
    |> Sign.primitive_arity (t, Ss, [c])
wenzelm@36325
   434
    |> put_arity ((t, Ss, c), th')
haftmann@30951
   435
  end;
haftmann@30951
   436
haftmann@30951
   437
haftmann@30951
   438
(* tactical proofs *)
haftmann@30951
   439
haftmann@30951
   440
fun prove_classrel raw_rel tac thy =
haftmann@30951
   441
  let
haftmann@30951
   442
    val ctxt = ProofContext.init thy;
haftmann@30951
   443
    val (c1, c2) = cert_classrel thy raw_rel;
haftmann@30951
   444
    val th = Goal.prove ctxt [] [] (Logic.mk_classrel (c1, c2)) (K tac) handle ERROR msg =>
haftmann@30951
   445
      cat_error msg ("The error(s) above occurred while trying to prove class relation " ^
haftmann@30951
   446
        quote (Syntax.string_of_classrel ctxt [c1, c2]));
haftmann@30951
   447
  in
haftmann@30951
   448
    thy
haftmann@30951
   449
    |> PureThy.add_thms [((Binding.name
haftmann@30951
   450
        (prefix classrel_prefix (Logic.name_classrel (c1, c2))), th), [])]
haftmann@30951
   451
    |-> (fn [th'] => add_classrel th')
haftmann@30951
   452
  end;
haftmann@30951
   453
haftmann@30951
   454
fun prove_arity raw_arity tac thy =
haftmann@30951
   455
  let
haftmann@30951
   456
    val ctxt = ProofContext.init thy;
wenzelm@35669
   457
    val arity = ProofContext.cert_arity ctxt raw_arity;
haftmann@30951
   458
    val names = map (prefix arity_prefix) (Logic.name_arities arity);
haftmann@30951
   459
    val props = Logic.mk_arities arity;
haftmann@30951
   460
    val ths = Goal.prove_multi ctxt [] [] props
haftmann@30951
   461
      (fn _ => Goal.precise_conjunction_tac (length props) 1 THEN tac) handle ERROR msg =>
haftmann@30951
   462
        cat_error msg ("The error(s) above occurred while trying to prove type arity " ^
haftmann@30951
   463
          quote (Syntax.string_of_arity ctxt arity));
haftmann@30951
   464
  in
haftmann@30951
   465
    thy
haftmann@30951
   466
    |> PureThy.add_thms (map (rpair []) (map Binding.name names ~~ ths))
haftmann@30951
   467
    |-> fold add_arity
haftmann@30951
   468
  end;
haftmann@30951
   469
haftmann@30951
   470
wenzelm@19511
   471
wenzelm@19511
   472
(** class definitions **)
wenzelm@19511
   473
wenzelm@23421
   474
fun split_defined n eq =
wenzelm@23421
   475
  let
wenzelm@23421
   476
    val intro =
wenzelm@23421
   477
      (eq RS Drule.equal_elim_rule2)
wenzelm@23421
   478
      |> Conjunction.curry_balanced n
wenzelm@23421
   479
      |> n = 0 ? Thm.eq_assumption 1;
wenzelm@23421
   480
    val dests =
wenzelm@23421
   481
      if n = 0 then []
wenzelm@23421
   482
      else
wenzelm@23421
   483
        (eq RS Drule.equal_elim_rule1)
wenzelm@32765
   484
        |> Balanced_Tree.dest (fn th =>
wenzelm@23421
   485
          (th RS Conjunction.conjunctionD1, th RS Conjunction.conjunctionD2)) n;
wenzelm@23421
   486
  in (intro, dests) end;
wenzelm@23421
   487
wenzelm@24964
   488
fun define_class (bclass, raw_super) raw_params raw_specs thy =
wenzelm@19511
   489
  let
wenzelm@19511
   490
    val ctxt = ProofContext.init thy;
wenzelm@24964
   491
    val pp = Syntax.pp ctxt;
wenzelm@19511
   492
wenzelm@19511
   493
wenzelm@24964
   494
    (* class *)
wenzelm@19511
   495
wenzelm@30344
   496
    val bconst = Binding.map_name Logic.const_of_class bclass;
wenzelm@30344
   497
    val class = Sign.full_name thy bclass;
wenzelm@24731
   498
    val super = Sign.minimize_sort thy (Sign.certify_sort thy raw_super);
wenzelm@19511
   499
wenzelm@24964
   500
    fun check_constraint (a, S) =
wenzelm@24964
   501
      if Sign.subsort thy (super, S) then ()
wenzelm@24964
   502
      else error ("Sort constraint of type variable " ^
wenzelm@32966
   503
        setmp_CRITICAL show_sorts true (Pretty.string_of_typ pp) (TFree (a, S)) ^
wenzelm@24964
   504
        " needs to be weaker than " ^ Pretty.string_of_sort pp super);
wenzelm@24964
   505
wenzelm@24964
   506
wenzelm@24964
   507
    (* params *)
wenzelm@24964
   508
wenzelm@24964
   509
    val params = raw_params |> map (fn p =>
wenzelm@24964
   510
      let
wenzelm@24964
   511
        val T = Sign.the_const_type thy p;
wenzelm@24964
   512
        val _ =
wenzelm@24964
   513
          (case Term.add_tvarsT T [] of
wenzelm@24964
   514
            [((a, _), S)] => check_constraint (a, S)
wenzelm@24964
   515
          | _ => error ("Exactly one type variable expected in class parameter " ^ quote p));
wenzelm@24964
   516
        val T' = Term.map_type_tvar (fn _ => TFree (Name.aT, [class])) T;
wenzelm@24964
   517
      in (p, T') end);
wenzelm@24964
   518
wenzelm@24964
   519
wenzelm@24964
   520
    (* axioms *)
wenzelm@24964
   521
wenzelm@19511
   522
    fun prep_axiom t =
wenzelm@19511
   523
      (case Term.add_tfrees t [] of
wenzelm@24964
   524
        [(a, S)] => check_constraint (a, S)
wenzelm@24964
   525
      | [] => ()
wenzelm@24964
   526
      | _ => error ("Multiple type variables in class axiom:\n" ^ Pretty.string_of_term pp t);
wenzelm@24964
   527
      t
wenzelm@24964
   528
      |> Term.map_types (Term.map_atyps (fn TFree _ => Term.aT [] | U => U))
wenzelm@24964
   529
      |> Logic.close_form);
wenzelm@19511
   530
wenzelm@24681
   531
    val axiomss = map (map (prep_axiom o Sign.cert_prop thy) o snd) raw_specs;
haftmann@22745
   532
    val name_atts = map fst raw_specs;
wenzelm@19511
   533
wenzelm@19511
   534
wenzelm@19511
   535
    (* definition *)
wenzelm@19511
   536
wenzelm@35854
   537
    val conjs = Logic.mk_of_sort (Term.aT [], super) @ flat axiomss;
wenzelm@19511
   538
    val class_eq =
wenzelm@31943
   539
      Logic.mk_equals (Logic.mk_of_class (Term.aT [], class), Logic.mk_conjunction_balanced conjs);
wenzelm@19511
   540
wenzelm@19511
   541
    val ([def], def_thy) =
wenzelm@19511
   542
      thy
wenzelm@19511
   543
      |> Sign.primitive_class (bclass, super)
wenzelm@35238
   544
      |> PureThy.add_defs false [((Thm.def_binding bconst, class_eq), [])];
wenzelm@19511
   545
    val (raw_intro, (raw_classrel, raw_axioms)) =
wenzelm@23421
   546
      split_defined (length conjs) def ||> chop (length super);
wenzelm@19392
   547
wenzelm@19418
   548
wenzelm@19511
   549
    (* facts *)
wenzelm@19511
   550
wenzelm@19511
   551
    val class_triv = Thm.class_triv def_thy class;
wenzelm@19511
   552
    val ([(_, [intro]), (_, classrel), (_, axioms)], facts_thy) =
wenzelm@19511
   553
      def_thy
wenzelm@35201
   554
      |> Sign.qualified_path true bconst
haftmann@27691
   555
      |> PureThy.note_thmss ""
wenzelm@36326
   556
        [((Binding.name "intro", []), [([Drule.export_without_context raw_intro], [])]),
wenzelm@36326
   557
         ((Binding.name "super", []), [(map Drule.export_without_context raw_classrel, [])]),
wenzelm@36326
   558
         ((Binding.name "axioms", []),
wenzelm@35021
   559
           [(map (fn th => Drule.export_without_context (class_triv RS th)) raw_axioms, [])])]
haftmann@27691
   560
      ||> Sign.restore_naming def_thy;
wenzelm@19511
   561
wenzelm@24847
   562
wenzelm@19511
   563
    (* result *)
wenzelm@19511
   564
wenzelm@24964
   565
    val axclass = make_axclass ((def, intro, axioms), params);
wenzelm@19511
   566
    val result_thy =
wenzelm@19511
   567
      facts_thy
wenzelm@36325
   568
      |> fold (snd oo put_trancl_classrel) (map (pair class) super ~~ classrel)
wenzelm@35201
   569
      |> Sign.qualified_path false bconst
haftmann@27691
   570
      |> PureThy.note_thmss "" (name_atts ~~ map Thm.simple_fact (unflat axiomss axioms)) |> snd
wenzelm@19511
   571
      |> Sign.restore_naming facts_thy
wenzelm@19574
   572
      |> map_axclasses (fn (axclasses, parameters) =>
wenzelm@21919
   573
        (Symtab.update (class, axclass) axclasses,
wenzelm@24964
   574
          fold (fn (x, _) => add_param pp (x, class)) params parameters));
wenzelm@19511
   575
wenzelm@19511
   576
  in (class, result_thy) end;
wenzelm@19511
   577
wenzelm@19511
   578
wenzelm@19531
   579
wenzelm@19511
   580
(** axiomatizations **)
wenzelm@19511
   581
wenzelm@19511
   582
local
wenzelm@19511
   583
wenzelm@35896
   584
(* old-style axioms *)
wenzelm@35896
   585
wenzelm@35896
   586
fun add_axiom (b, prop) =
wenzelm@35896
   587
  Thm.add_axiom (b, prop) #->
wenzelm@36106
   588
  (fn (_, thm) => PureThy.add_thm ((b, Drule.export_without_context thm), []));
wenzelm@35896
   589
wenzelm@20628
   590
fun axiomatize prep mk name add raw_args thy =
wenzelm@20628
   591
  let
wenzelm@20628
   592
    val args = prep thy raw_args;
wenzelm@20628
   593
    val specs = mk args;
wenzelm@20628
   594
    val names = name args;
haftmann@29579
   595
  in
haftmann@29579
   596
    thy
wenzelm@35896
   597
    |> fold_map add_axiom (map Binding.name names ~~ specs)
haftmann@29579
   598
    |-> fold add
haftmann@29579
   599
  end;
wenzelm@19511
   600
wenzelm@19511
   601
fun ax_classrel prep =
wenzelm@20628
   602
  axiomatize (map o prep) (map Logic.mk_classrel)
wenzelm@20628
   603
    (map (prefix classrel_prefix o Logic.name_classrel)) add_classrel;
wenzelm@19511
   604
wenzelm@19511
   605
fun ax_arity prep =
wenzelm@35669
   606
  axiomatize (prep o ProofContext.init) Logic.mk_arities
wenzelm@20628
   607
    (map (prefix arity_prefix) o Logic.name_arities) add_arity;
wenzelm@19511
   608
wenzelm@19706
   609
fun class_const c =
wenzelm@19706
   610
  (Logic.const_of_class c, Term.itselfT (Term.aT []) --> propT);
wenzelm@19706
   611
wenzelm@19511
   612
fun ax_class prep_class prep_classrel (bclass, raw_super) thy =
wenzelm@19511
   613
  let
wenzelm@30344
   614
    val class = Sign.full_name thy bclass;
wenzelm@24731
   615
    val super = map (prep_class thy) raw_super |> Sign.minimize_sort thy;
wenzelm@19511
   616
  in
wenzelm@19511
   617
    thy
wenzelm@19511
   618
    |> Sign.primitive_class (bclass, super)
wenzelm@19511
   619
    |> ax_classrel prep_classrel (map (fn c => (class, c)) super)
wenzelm@19706
   620
    |> Theory.add_deps "" (class_const class) (map class_const super)
wenzelm@19511
   621
  end;
wenzelm@19511
   622
wenzelm@19511
   623
in
wenzelm@19511
   624
wenzelm@24929
   625
val axiomatize_class = ax_class Sign.certify_class cert_classrel;
wenzelm@35669
   626
val axiomatize_class_cmd = ax_class (ProofContext.read_class o ProofContext.init) read_classrel;
wenzelm@24929
   627
val axiomatize_classrel = ax_classrel cert_classrel;
wenzelm@24929
   628
val axiomatize_classrel_cmd = ax_classrel read_classrel;
wenzelm@35669
   629
val axiomatize_arity = ax_arity ProofContext.cert_arity;
wenzelm@35669
   630
val axiomatize_arity_cmd = ax_arity ProofContext.read_arity;
wenzelm@19511
   631
wenzelm@19511
   632
end;
wenzelm@19511
   633
wenzelm@19511
   634
end;