src/Pure/axclass.ML
author wenzelm
Tue Apr 27 16:09:15 2010 +0200 (2010-04-27)
changeset 36421 066e35d1c0d7
parent 36420 66fd989c8e15
child 36427 85bc9b7c4d18
permissions -rw-r--r--
tuned classrel completion -- bypass composition with reflexive edges;
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@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
haftmann@20107
    18
  val class_of_param: theory -> string -> class option
wenzelm@19405
    19
  val cert_classrel: theory -> class * class -> class * class
wenzelm@19405
    20
  val read_classrel: theory -> xstring * xstring -> class * class
wenzelm@30344
    21
  val axiomatize_class: binding * class list -> theory -> theory
wenzelm@30344
    22
  val axiomatize_class_cmd: binding * xstring list -> theory -> theory
wenzelm@24929
    23
  val axiomatize_classrel: (class * class) list -> theory -> theory
wenzelm@24929
    24
  val axiomatize_classrel_cmd: (xstring * xstring) list -> theory -> theory
wenzelm@24929
    25
  val axiomatize_arity: arity -> theory -> theory
wenzelm@24929
    26
  val axiomatize_arity_cmd: xstring * string list * string -> theory -> theory
haftmann@25486
    27
  val instance_name: string * class -> string
haftmann@25597
    28
  val declare_overloaded: string * typ -> theory -> term * theory
haftmann@30519
    29
  val define_overloaded: binding -> string * term -> theory -> thm * theory
haftmann@25597
    30
  val unoverload: theory -> thm -> thm
haftmann@25597
    31
  val overload: theory -> thm -> thm
haftmann@25597
    32
  val unoverload_conv: theory -> conv
haftmann@25597
    33
  val overload_conv: theory -> conv
haftmann@25597
    34
  val unoverload_const: theory -> string * typ -> string
haftmann@31249
    35
  val lookup_inst_param: Consts.T -> ((string * string) * 'a) list -> string * typ -> 'a option
haftmann@25597
    36
  val param_of_inst: theory -> string * string -> string
haftmann@25597
    37
  val inst_of_param: theory -> string -> (string * string) option
wenzelm@33172
    38
  val thynames_of_arity: theory -> class * string -> string list
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@36421
   193
fun (SOME a RSO SOME b) = SOME (a RS b)
wenzelm@36421
   194
  | (x RSO NONE) = x
wenzelm@36421
   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@36418
   199
    SOME thm => Thm.transfer thy thm
wenzelm@24920
   200
  | NONE => error ("Unproven class relation " ^
wenzelm@24920
   201
      Syntax.string_of_classrel (ProofContext.init thy) [c1, c2]));
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@36421
   209
      if c1' = c2' then NONE else SOME (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@19574
   244
fun the_arity thy a (c, Ss) =
wenzelm@36329
   245
  (case AList.lookup (op =) (Symtab.lookup_list (proven_arities_of thy) a) (c, Ss) of
wenzelm@36418
   246
    SOME (thm, _) => Thm.transfer thy thm
wenzelm@24920
   247
  | NONE => error ("Unproven type arity " ^
wenzelm@24920
   248
      Syntax.string_of_arity (ProofContext.init thy) (a, Ss, [c])));
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@36419
   270
          (th RS 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@36329
   298
  (Theory.at_begin complete_classrels #> Theory.at_begin complete_arities));
haftmann@27397
   299
wenzelm@36418
   300
val the_classrel_prf = Thm.proof_of oo the_classrel;
wenzelm@36418
   301
val the_arity_prf = Thm.proof_of ooo the_arity;
wenzelm@36418
   302
haftmann@27397
   303
haftmann@25597
   304
(* maintain instance parameters *)
haftmann@25597
   305
haftmann@25597
   306
fun get_inst_param thy (c, tyco) =
wenzelm@36329
   307
  (case Symtab.lookup (the_default Symtab.empty (Symtab.lookup (#1 (inst_params_of thy)) c)) tyco of
wenzelm@36327
   308
    SOME c' => c'
wenzelm@36327
   309
  | NONE => error ("No instance parameter for constant " ^ quote c ^ " on type " ^ quote tyco));
haftmann@25597
   310
wenzelm@36327
   311
fun add_inst_param (c, tyco) inst =
wenzelm@36327
   312
  (map_inst_params o apfst o Symtab.map_default (c, Symtab.empty)) (Symtab.update_new (tyco, inst))
wenzelm@36417
   313
  #> (map_inst_params o apsnd) (Symtab.update_new (#1 inst, (c, tyco)));
haftmann@25597
   314
wenzelm@36329
   315
val inst_of_param = Symtab.lookup o #2 o inst_params_of;
wenzelm@36417
   316
val param_of_inst = #1 oo get_inst_param;
haftmann@25597
   317
wenzelm@36327
   318
fun inst_thms thy =
wenzelm@36329
   319
  Symtab.fold (Symtab.fold (cons o #2 o #2) o #2) (#1 (inst_params_of thy)) [];
haftmann@25597
   320
wenzelm@36417
   321
fun get_inst_tyco consts = try (#1 o dest_Type o the_single o Consts.typargs consts);
haftmann@25597
   322
haftmann@25597
   323
fun unoverload thy = MetaSimplifier.simplify true (inst_thms thy);
haftmann@25597
   324
fun overload thy = MetaSimplifier.simplify true (map Thm.symmetric (inst_thms thy));
haftmann@25597
   325
haftmann@25597
   326
fun unoverload_conv thy = MetaSimplifier.rewrite true (inst_thms thy);
haftmann@25597
   327
fun overload_conv thy = MetaSimplifier.rewrite true (map Thm.symmetric (inst_thms thy));
haftmann@25597
   328
wenzelm@36327
   329
fun lookup_inst_param consts params (c, T) =
wenzelm@36327
   330
  (case get_inst_tyco consts (c, T) of
wenzelm@36327
   331
    SOME tyco => AList.lookup (op =) params (c, tyco)
wenzelm@36327
   332
  | NONE => NONE);
haftmann@31249
   333
haftmann@25597
   334
fun unoverload_const thy (c_ty as (c, _)) =
wenzelm@36327
   335
  if is_some (class_of_param thy c) then
wenzelm@36327
   336
    (case get_inst_tyco (Sign.consts_of thy) c_ty of
wenzelm@36327
   337
      SOME tyco => try (param_of_inst thy) (c, tyco) |> the_default c
wenzelm@36327
   338
    | NONE => c)
haftmann@33969
   339
  else c;
haftmann@25597
   340
wenzelm@404
   341
wenzelm@36327
   342
wenzelm@19511
   343
(** instances **)
wenzelm@19418
   344
wenzelm@19418
   345
(* class relations *)
wenzelm@19418
   346
wenzelm@19405
   347
fun cert_classrel thy raw_rel =
wenzelm@15876
   348
  let
wenzelm@26939
   349
    val string_of_sort = Syntax.string_of_sort_global thy;
wenzelm@19405
   350
    val (c1, c2) = pairself (Sign.certify_class thy) raw_rel;
wenzelm@24271
   351
    val _ = Sign.primitive_classrel (c1, c2) (Theory.copy thy);
wenzelm@19405
   352
    val _ =
wenzelm@19460
   353
      (case subtract (op =) (all_params_of thy [c1]) (all_params_of thy [c2]) of
wenzelm@19405
   354
        [] => ()
wenzelm@26939
   355
      | xs => raise TYPE ("Class " ^ string_of_sort [c1] ^ " lacks parameter(s) " ^
wenzelm@26939
   356
          commas_quote xs ^ " of " ^ string_of_sort [c2], [], []));
wenzelm@19405
   357
  in (c1, c2) end;
wenzelm@19405
   358
wenzelm@19405
   359
fun read_classrel thy raw_rel =
wenzelm@35669
   360
  cert_classrel thy (pairself (ProofContext.read_class (ProofContext.init thy)) raw_rel)
wenzelm@19405
   361
    handle TYPE (msg, _, _) => error msg;
wenzelm@19405
   362
wenzelm@19405
   363
haftmann@25597
   364
(* declaration and definition of instances of overloaded constants *)
haftmann@25597
   365
wenzelm@35201
   366
fun inst_tyco_of thy (c, T) =
wenzelm@35201
   367
  (case get_inst_tyco (Sign.consts_of thy) (c, T) of
wenzelm@35201
   368
    SOME tyco => tyco
wenzelm@35201
   369
  | NONE => error ("Illegal type for instantiation of class parameter: " ^
wenzelm@35201
   370
      quote (c ^ " :: " ^ Syntax.string_of_typ_global thy T)));
haftmann@31249
   371
haftmann@25597
   372
fun declare_overloaded (c, T) thy =
haftmann@25597
   373
  let
wenzelm@35201
   374
    val class =
wenzelm@35201
   375
      (case class_of_param thy c of
wenzelm@35201
   376
        SOME class => class
wenzelm@35201
   377
      | NONE => error ("Not a class parameter: " ^ quote c));
haftmann@31249
   378
    val tyco = inst_tyco_of thy (c, T);
haftmann@25597
   379
    val name_inst = instance_name (tyco, class) ^ "_inst";
wenzelm@36417
   380
    val c' = instance_name (tyco, c);
haftmann@25597
   381
    val T' = Type.strip_sorts T;
haftmann@25597
   382
  in
haftmann@25597
   383
    thy
wenzelm@35201
   384
    |> Sign.qualified_path true (Binding.name name_inst)
wenzelm@33173
   385
    |> Sign.declare_const ((Binding.name c', T'), NoSyn)
wenzelm@30344
   386
    |-> (fn const' as Const (c'', _) =>
wenzelm@30344
   387
      Thm.add_def false true
wenzelm@30344
   388
        (Binding.name (Thm.def_name c'), Logic.mk_equals (Const (c, T'), const'))
wenzelm@36106
   389
      #>> apsnd Thm.varifyT_global
wenzelm@36106
   390
      #-> (fn (_, thm) => add_inst_param (c, tyco) (c'', thm)
wenzelm@36106
   391
        #> PureThy.add_thm ((Binding.conceal (Binding.name c'), thm), [])
wenzelm@36417
   392
        #> #2
wenzelm@36106
   393
        #> pair (Const (c, T))))
wenzelm@35201
   394
    ||> Sign.restore_naming thy
haftmann@25597
   395
  end;
haftmann@25597
   396
haftmann@30519
   397
fun define_overloaded b (c, t) thy =
haftmann@25597
   398
  let
haftmann@25597
   399
    val T = Term.fastype_of t;
haftmann@31249
   400
    val tyco = inst_tyco_of thy (c, T);
haftmann@25597
   401
    val (c', eq) = get_inst_param thy (c, tyco);
haftmann@25597
   402
    val prop = Logic.mk_equals (Const (c', T), t);
wenzelm@36417
   403
    val b' = Thm.def_binding_optional (Binding.name (instance_name (tyco, c))) b;
haftmann@25597
   404
  in
haftmann@25597
   405
    thy
haftmann@30519
   406
    |> Thm.add_def false false (b', prop)
wenzelm@36106
   407
    |>> (fn (_, thm) =>  Drule.transitive_thm OF [eq, thm])
haftmann@25597
   408
  end;
haftmann@25597
   409
haftmann@25597
   410
haftmann@30951
   411
(* primitive rules *)
haftmann@30951
   412
wenzelm@36329
   413
val shyps_topped = forall null o #shyps o Thm.rep_thm;
wenzelm@36329
   414
wenzelm@31948
   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@36325
   422
    val th' = th
wenzelm@36325
   423
      |> Drule.instantiate' [SOME (ctyp_of thy (TVar ((Name.aT, 0), [c1])))] []
wenzelm@36330
   424
      |> Thm.unconstrain_allTs;
wenzelm@36327
   425
    val _ = shyps_topped th' orelse raise Fail "add_classrel: nontop shyps after unconstrain";
haftmann@30951
   426
  in
haftmann@30951
   427
    thy
haftmann@30951
   428
    |> Sign.primitive_classrel (c1, c2)
wenzelm@36417
   429
    |> (#2 oo put_trancl_classrel) ((c1, c2), th')
haftmann@30951
   430
    |> perhaps complete_arities
haftmann@30951
   431
  end;
haftmann@30951
   432
wenzelm@31948
   433
fun add_arity raw_th thy =
haftmann@30951
   434
  let
wenzelm@31948
   435
    val th = Thm.strip_shyps (Thm.transfer thy raw_th);
wenzelm@31948
   436
    val prop = Thm.plain_prop_of th;
haftmann@30951
   437
    fun err () = raise THM ("add_arity: malformed type arity", 0, [th]);
haftmann@30951
   438
    val (t, Ss, c) = Logic.dest_arity prop handle TERM _ => err ();
wenzelm@36417
   439
wenzelm@36417
   440
    val args = Name.names Name.context Name.aT Ss;
wenzelm@36417
   441
    val T = Type (t, map TFree args);
wenzelm@36417
   442
    val std_vars = map (fn (a, S) => SOME (ctyp_of thy (TVar ((a, 0), S)))) args;
wenzelm@36417
   443
haftmann@30951
   444
    val missing_params = Sign.complete_sort thy [c]
haftmann@30951
   445
      |> maps (these o Option.map #params o try (get_info thy))
haftmann@30951
   446
      |> filter_out (fn (const, _) => can (get_inst_param thy) (const, t))
haftmann@30951
   447
      |> (map o apsnd o map_atyps) (K T);
haftmann@30951
   448
    val _ = map (Sign.certify_sort thy) Ss = Ss orelse err ();
wenzelm@36325
   449
    val th' = th
wenzelm@36417
   450
      |> Drule.instantiate' std_vars []
wenzelm@36330
   451
      |> Thm.unconstrain_allTs;
wenzelm@36327
   452
    val _ = shyps_topped th' orelse raise Fail "add_arity: nontop shyps after unconstrain";
haftmann@30951
   453
  in
haftmann@30951
   454
    thy
wenzelm@36417
   455
    |> fold (#2 oo declare_overloaded) missing_params
haftmann@30951
   456
    |> Sign.primitive_arity (t, Ss, [c])
wenzelm@36325
   457
    |> put_arity ((t, Ss, c), th')
haftmann@30951
   458
  end;
haftmann@30951
   459
haftmann@30951
   460
haftmann@30951
   461
(* tactical proofs *)
haftmann@30951
   462
haftmann@30951
   463
fun prove_classrel raw_rel tac thy =
haftmann@30951
   464
  let
haftmann@30951
   465
    val ctxt = ProofContext.init thy;
haftmann@30951
   466
    val (c1, c2) = cert_classrel thy raw_rel;
haftmann@30951
   467
    val th = Goal.prove ctxt [] [] (Logic.mk_classrel (c1, c2)) (K tac) handle ERROR msg =>
haftmann@30951
   468
      cat_error msg ("The error(s) above occurred while trying to prove class relation " ^
haftmann@30951
   469
        quote (Syntax.string_of_classrel ctxt [c1, c2]));
haftmann@30951
   470
  in
haftmann@30951
   471
    thy
haftmann@30951
   472
    |> PureThy.add_thms [((Binding.name
haftmann@30951
   473
        (prefix classrel_prefix (Logic.name_classrel (c1, c2))), th), [])]
haftmann@30951
   474
    |-> (fn [th'] => add_classrel th')
haftmann@30951
   475
  end;
haftmann@30951
   476
haftmann@30951
   477
fun prove_arity raw_arity tac thy =
haftmann@30951
   478
  let
haftmann@30951
   479
    val ctxt = ProofContext.init thy;
wenzelm@35669
   480
    val arity = ProofContext.cert_arity ctxt raw_arity;
haftmann@30951
   481
    val names = map (prefix arity_prefix) (Logic.name_arities arity);
haftmann@30951
   482
    val props = Logic.mk_arities arity;
haftmann@30951
   483
    val ths = Goal.prove_multi ctxt [] [] props
haftmann@30951
   484
      (fn _ => Goal.precise_conjunction_tac (length props) 1 THEN tac) handle ERROR msg =>
haftmann@30951
   485
        cat_error msg ("The error(s) above occurred while trying to prove type arity " ^
haftmann@30951
   486
          quote (Syntax.string_of_arity ctxt arity));
haftmann@30951
   487
  in
haftmann@30951
   488
    thy
haftmann@30951
   489
    |> PureThy.add_thms (map (rpair []) (map Binding.name names ~~ ths))
haftmann@30951
   490
    |-> fold add_arity
haftmann@30951
   491
  end;
haftmann@30951
   492
haftmann@30951
   493
wenzelm@19511
   494
wenzelm@19511
   495
(** class definitions **)
wenzelm@19511
   496
wenzelm@23421
   497
fun split_defined n eq =
wenzelm@23421
   498
  let
wenzelm@23421
   499
    val intro =
wenzelm@23421
   500
      (eq RS Drule.equal_elim_rule2)
wenzelm@23421
   501
      |> Conjunction.curry_balanced n
wenzelm@23421
   502
      |> n = 0 ? Thm.eq_assumption 1;
wenzelm@23421
   503
    val dests =
wenzelm@23421
   504
      if n = 0 then []
wenzelm@23421
   505
      else
wenzelm@23421
   506
        (eq RS Drule.equal_elim_rule1)
wenzelm@32765
   507
        |> Balanced_Tree.dest (fn th =>
wenzelm@23421
   508
          (th RS Conjunction.conjunctionD1, th RS Conjunction.conjunctionD2)) n;
wenzelm@23421
   509
  in (intro, dests) end;
wenzelm@23421
   510
wenzelm@24964
   511
fun define_class (bclass, raw_super) raw_params raw_specs thy =
wenzelm@19511
   512
  let
wenzelm@19511
   513
    val ctxt = ProofContext.init thy;
wenzelm@24964
   514
    val pp = Syntax.pp ctxt;
wenzelm@19511
   515
wenzelm@19511
   516
wenzelm@24964
   517
    (* class *)
wenzelm@19511
   518
wenzelm@30344
   519
    val bconst = Binding.map_name Logic.const_of_class bclass;
wenzelm@30344
   520
    val class = Sign.full_name thy bclass;
wenzelm@24731
   521
    val super = Sign.minimize_sort thy (Sign.certify_sort thy raw_super);
wenzelm@19511
   522
wenzelm@24964
   523
    fun check_constraint (a, S) =
wenzelm@24964
   524
      if Sign.subsort thy (super, S) then ()
wenzelm@24964
   525
      else error ("Sort constraint of type variable " ^
wenzelm@32966
   526
        setmp_CRITICAL show_sorts true (Pretty.string_of_typ pp) (TFree (a, S)) ^
wenzelm@24964
   527
        " needs to be weaker than " ^ Pretty.string_of_sort pp super);
wenzelm@24964
   528
wenzelm@24964
   529
wenzelm@24964
   530
    (* params *)
wenzelm@24964
   531
wenzelm@24964
   532
    val params = raw_params |> map (fn p =>
wenzelm@24964
   533
      let
wenzelm@24964
   534
        val T = Sign.the_const_type thy p;
wenzelm@24964
   535
        val _ =
wenzelm@24964
   536
          (case Term.add_tvarsT T [] of
wenzelm@24964
   537
            [((a, _), S)] => check_constraint (a, S)
wenzelm@24964
   538
          | _ => error ("Exactly one type variable expected in class parameter " ^ quote p));
wenzelm@24964
   539
        val T' = Term.map_type_tvar (fn _ => TFree (Name.aT, [class])) T;
wenzelm@24964
   540
      in (p, T') end);
wenzelm@24964
   541
wenzelm@24964
   542
wenzelm@24964
   543
    (* axioms *)
wenzelm@24964
   544
wenzelm@19511
   545
    fun prep_axiom t =
wenzelm@19511
   546
      (case Term.add_tfrees t [] of
wenzelm@24964
   547
        [(a, S)] => check_constraint (a, S)
wenzelm@24964
   548
      | [] => ()
wenzelm@24964
   549
      | _ => error ("Multiple type variables in class axiom:\n" ^ Pretty.string_of_term pp t);
wenzelm@24964
   550
      t
wenzelm@24964
   551
      |> Term.map_types (Term.map_atyps (fn TFree _ => Term.aT [] | U => U))
wenzelm@24964
   552
      |> Logic.close_form);
wenzelm@19511
   553
wenzelm@24681
   554
    val axiomss = map (map (prep_axiom o Sign.cert_prop thy) o snd) raw_specs;
haftmann@22745
   555
    val name_atts = map fst raw_specs;
wenzelm@19511
   556
wenzelm@19511
   557
wenzelm@19511
   558
    (* definition *)
wenzelm@19511
   559
wenzelm@35854
   560
    val conjs = Logic.mk_of_sort (Term.aT [], super) @ flat axiomss;
wenzelm@19511
   561
    val class_eq =
wenzelm@31943
   562
      Logic.mk_equals (Logic.mk_of_class (Term.aT [], class), Logic.mk_conjunction_balanced conjs);
wenzelm@19511
   563
wenzelm@19511
   564
    val ([def], def_thy) =
wenzelm@19511
   565
      thy
wenzelm@19511
   566
      |> Sign.primitive_class (bclass, super)
wenzelm@35238
   567
      |> PureThy.add_defs false [((Thm.def_binding bconst, class_eq), [])];
wenzelm@19511
   568
    val (raw_intro, (raw_classrel, raw_axioms)) =
wenzelm@23421
   569
      split_defined (length conjs) def ||> chop (length super);
wenzelm@19392
   570
wenzelm@19418
   571
wenzelm@19511
   572
    (* facts *)
wenzelm@19511
   573
wenzelm@19511
   574
    val class_triv = Thm.class_triv def_thy class;
wenzelm@19511
   575
    val ([(_, [intro]), (_, classrel), (_, axioms)], facts_thy) =
wenzelm@19511
   576
      def_thy
wenzelm@35201
   577
      |> Sign.qualified_path true bconst
haftmann@27691
   578
      |> PureThy.note_thmss ""
wenzelm@36326
   579
        [((Binding.name "intro", []), [([Drule.export_without_context raw_intro], [])]),
wenzelm@36326
   580
         ((Binding.name "super", []), [(map Drule.export_without_context raw_classrel, [])]),
wenzelm@36326
   581
         ((Binding.name "axioms", []),
wenzelm@35021
   582
           [(map (fn th => Drule.export_without_context (class_triv RS th)) raw_axioms, [])])]
haftmann@27691
   583
      ||> Sign.restore_naming def_thy;
wenzelm@19511
   584
wenzelm@24847
   585
wenzelm@19511
   586
    (* result *)
wenzelm@19511
   587
wenzelm@36329
   588
    val axclass = make_axclass (def, intro, axioms, params);
wenzelm@19511
   589
    val result_thy =
wenzelm@19511
   590
      facts_thy
wenzelm@36417
   591
      |> fold (#2 oo put_trancl_classrel) (map (pair class) super ~~ classrel)
wenzelm@35201
   592
      |> Sign.qualified_path false bconst
wenzelm@36417
   593
      |> PureThy.note_thmss "" (name_atts ~~ map Thm.simple_fact (unflat axiomss axioms)) |> #2
wenzelm@19511
   594
      |> Sign.restore_naming facts_thy
wenzelm@36329
   595
      |> map_axclasses (Symtab.update (class, axclass))
wenzelm@36329
   596
      |> map_params (fold (fn (x, _) => add_param pp (x, class)) params);
wenzelm@19511
   597
wenzelm@19511
   598
  in (class, result_thy) end;
wenzelm@19511
   599
wenzelm@19511
   600
wenzelm@19531
   601
wenzelm@19511
   602
(** axiomatizations **)
wenzelm@19511
   603
wenzelm@19511
   604
local
wenzelm@19511
   605
wenzelm@36417
   606
(*old-style axioms*)
wenzelm@35896
   607
fun add_axiom (b, prop) =
wenzelm@35896
   608
  Thm.add_axiom (b, prop) #->
wenzelm@36106
   609
  (fn (_, thm) => PureThy.add_thm ((b, Drule.export_without_context thm), []));
wenzelm@35896
   610
wenzelm@20628
   611
fun axiomatize prep mk name add raw_args thy =
wenzelm@20628
   612
  let
wenzelm@20628
   613
    val args = prep thy raw_args;
wenzelm@20628
   614
    val specs = mk args;
wenzelm@20628
   615
    val names = name args;
haftmann@29579
   616
  in
haftmann@29579
   617
    thy
wenzelm@35896
   618
    |> fold_map add_axiom (map Binding.name names ~~ specs)
haftmann@29579
   619
    |-> fold add
haftmann@29579
   620
  end;
wenzelm@19511
   621
wenzelm@19511
   622
fun ax_classrel prep =
wenzelm@20628
   623
  axiomatize (map o prep) (map Logic.mk_classrel)
wenzelm@20628
   624
    (map (prefix classrel_prefix o Logic.name_classrel)) add_classrel;
wenzelm@19511
   625
wenzelm@19511
   626
fun ax_arity prep =
wenzelm@35669
   627
  axiomatize (prep o ProofContext.init) Logic.mk_arities
wenzelm@20628
   628
    (map (prefix arity_prefix) o Logic.name_arities) add_arity;
wenzelm@19511
   629
wenzelm@19706
   630
fun class_const c =
wenzelm@19706
   631
  (Logic.const_of_class c, Term.itselfT (Term.aT []) --> propT);
wenzelm@19706
   632
wenzelm@19511
   633
fun ax_class prep_class prep_classrel (bclass, raw_super) thy =
wenzelm@19511
   634
  let
wenzelm@30344
   635
    val class = Sign.full_name thy bclass;
wenzelm@24731
   636
    val super = map (prep_class thy) raw_super |> Sign.minimize_sort thy;
wenzelm@19511
   637
  in
wenzelm@19511
   638
    thy
wenzelm@19511
   639
    |> Sign.primitive_class (bclass, super)
wenzelm@19511
   640
    |> ax_classrel prep_classrel (map (fn c => (class, c)) super)
wenzelm@19706
   641
    |> Theory.add_deps "" (class_const class) (map class_const super)
wenzelm@19511
   642
  end;
wenzelm@19511
   643
wenzelm@19511
   644
in
wenzelm@19511
   645
wenzelm@24929
   646
val axiomatize_class = ax_class Sign.certify_class cert_classrel;
wenzelm@35669
   647
val axiomatize_class_cmd = ax_class (ProofContext.read_class o ProofContext.init) read_classrel;
wenzelm@24929
   648
val axiomatize_classrel = ax_classrel cert_classrel;
wenzelm@24929
   649
val axiomatize_classrel_cmd = ax_classrel read_classrel;
wenzelm@35669
   650
val axiomatize_arity = ax_arity ProofContext.cert_arity;
wenzelm@35669
   651
val axiomatize_arity_cmd = ax_arity ProofContext.read_arity;
wenzelm@19511
   652
wenzelm@19511
   653
end;
wenzelm@19511
   654
wenzelm@19511
   655
end;