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