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