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