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