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