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