src/Pure/Isar/class.ML
author wenzelm
Thu Oct 11 16:05:44 2007 +0200 (2007-10-11)
changeset 24968 f9bafc868847
parent 24949 5f00e3532418
child 24981 4ec3f95190bf
permissions -rw-r--r--
replaced Sign.add_consts_authentic by Sign.declare_const;
replaced read_param by Sign.read_const, which is independent of syntax;
added define_class_params (from axclass.ML);
haftmann@24218
     1
(*  Title:      Pure/Isar/class.ML
haftmann@24218
     2
    ID:         $Id$
haftmann@24218
     3
    Author:     Florian Haftmann, TU Muenchen
haftmann@24218
     4
haftmann@24218
     5
Type classes derived from primitive axclasses and locales.
haftmann@24218
     6
*)
haftmann@24218
     7
haftmann@24218
     8
signature CLASS =
haftmann@24218
     9
sig
haftmann@24218
    10
  val fork_mixfix: bool -> string option -> mixfix -> mixfix * mixfix
haftmann@24218
    11
haftmann@24218
    12
  val axclass_cmd: bstring * xstring list
haftmann@24589
    13
    -> ((bstring * Attrib.src list) * string list) list
haftmann@24589
    14
    -> theory -> class * theory
haftmann@24589
    15
  val classrel_cmd: xstring * xstring -> theory -> Proof.state
haftmann@24914
    16
haftmann@24748
    17
  val class: bool -> bstring -> class list -> Element.context_i Locale.element list
haftmann@24218
    18
    -> string list -> theory -> string * Proof.context
haftmann@24748
    19
  val class_cmd: bool -> bstring -> xstring list -> Element.context Locale.element list
haftmann@24589
    20
    -> xstring list -> theory -> string * Proof.context
haftmann@24914
    21
  val init: class -> Proof.context -> Proof.context;
haftmann@24423
    22
  val add_const_in_class: string -> (string * term) * Syntax.mixfix
haftmann@24914
    23
    -> theory -> string * theory
haftmann@24914
    24
  val add_abbrev_in_class: string -> Syntax.mode -> (string * term) * mixfix
haftmann@24914
    25
    -> theory -> string * theory
haftmann@24914
    26
  val remove_constraint: sort -> string -> Proof.context -> Proof.context
haftmann@24589
    27
  val intro_classes_tac: thm list -> tactic
haftmann@24589
    28
  val default_intro_classes_tac: thm list -> tactic
haftmann@24589
    29
  val class_of_locale: theory -> string -> class option
haftmann@24914
    30
  val locale_of_class: theory -> class -> string
haftmann@24914
    31
  val local_syntax: theory -> class -> bool
haftmann@24589
    32
  val print_classes: theory -> unit
haftmann@24423
    33
haftmann@24589
    34
  val instance_arity: (theory -> theory) -> arity list -> theory -> Proof.state
haftmann@24589
    35
  val instance: arity list -> ((bstring * Attrib.src list) * term) list
haftmann@24423
    36
    -> (thm list -> theory -> theory)
haftmann@24218
    37
    -> theory -> Proof.state
haftmann@24589
    38
  val instance_cmd: (bstring * xstring list * xstring) list
haftmann@24589
    39
    -> ((bstring * Attrib.src list) * xstring) list
haftmann@24423
    40
    -> (thm list -> theory -> theory)
haftmann@24218
    41
    -> theory -> Proof.state
haftmann@24589
    42
  val prove_instance: tactic -> arity list
haftmann@24218
    43
    -> ((bstring * Attrib.src list) * term) list
haftmann@24423
    44
    -> theory -> thm list * theory
haftmann@24423
    45
  val unoverload: theory -> conv
haftmann@24423
    46
  val overload: theory -> conv
haftmann@24423
    47
  val unoverload_const: theory -> string * typ -> string
haftmann@24423
    48
  val inst_const: theory -> string * string -> string
haftmann@24423
    49
  val param_const: theory -> string -> (string * string) option
haftmann@24435
    50
  val params_of_sort: theory -> sort -> (string * (string * typ)) list
haftmann@24218
    51
end;
haftmann@24218
    52
haftmann@24218
    53
structure Class : CLASS =
haftmann@24218
    54
struct
haftmann@24218
    55
haftmann@24218
    56
(** auxiliary **)
haftmann@24218
    57
haftmann@24218
    58
fun fork_mixfix is_loc some_class mx =
haftmann@24218
    59
  let
haftmann@24218
    60
    val mx' = Syntax.unlocalize_mixfix mx;
haftmann@24901
    61
    val mx_global = if not is_loc orelse (is_some some_class andalso not (mx = mx'))
haftmann@24901
    62
      then mx' else NoSyn;
haftmann@24218
    63
    val mx_local = if is_loc then mx else NoSyn;
haftmann@24218
    64
  in (mx_global, mx_local) end;
haftmann@24218
    65
haftmann@24589
    66
fun prove_interpretation tac prfx_atts expr insts =
haftmann@24589
    67
  Locale.interpretation_i I prfx_atts expr insts
haftmann@24589
    68
  #> Proof.global_terminal_proof
haftmann@24589
    69
      (Method.Basic (K (Method.SIMPLE_METHOD tac), Position.none), NONE)
haftmann@24589
    70
  #> ProofContext.theory_of;
haftmann@24589
    71
haftmann@24589
    72
fun OF_LAST thm1 thm2 =
haftmann@24589
    73
  let
haftmann@24589
    74
    val n = (length o Logic.strip_imp_prems o prop_of) thm2;
haftmann@24589
    75
  in (thm1 RSN (n, thm2)) end;
haftmann@24589
    76
haftmann@24589
    77
fun strip_all_ofclass thy sort =
haftmann@24589
    78
  let
wenzelm@24847
    79
    val typ = TVar ((Name.aT, 0), sort);
haftmann@24589
    80
    fun prem_inclass t =
haftmann@24589
    81
      case Logic.strip_imp_prems t
haftmann@24589
    82
       of ofcls :: _ => try Logic.dest_inclass ofcls
haftmann@24589
    83
        | [] => NONE;
haftmann@24589
    84
    fun strip_ofclass class thm =
haftmann@24589
    85
      thm OF (fst o AxClass.of_sort thy (typ, [class])) AxClass.cache;
haftmann@24589
    86
    fun strip thm = case (prem_inclass o Thm.prop_of) thm
haftmann@24589
    87
     of SOME (_, class) => thm |> strip_ofclass class |> strip
haftmann@24589
    88
      | NONE => thm;
haftmann@24589
    89
  in strip end;
haftmann@24589
    90
haftmann@24589
    91
haftmann@24589
    92
(** axclass command **)
haftmann@24589
    93
haftmann@24218
    94
fun axclass_cmd (class, raw_superclasses) raw_specs thy =
haftmann@24218
    95
  let
haftmann@24218
    96
    val ctxt = ProofContext.init thy;
haftmann@24218
    97
    val superclasses = map (Sign.read_class thy) raw_superclasses;
haftmann@24589
    98
    val name_atts = map ((apsnd o map) (Attrib.attribute thy) o fst)
haftmann@24589
    99
      raw_specs;
haftmann@24589
   100
    val axiomss = ProofContext.read_propp (ctxt, map (map (rpair []) o snd)
haftmann@24589
   101
          raw_specs)
haftmann@24218
   102
      |> snd
haftmann@24218
   103
      |> (map o map) fst;
haftmann@24589
   104
  in
haftmann@24589
   105
    AxClass.define_class (class, superclasses) []
haftmann@24589
   106
      (name_atts ~~ axiomss) thy
haftmann@24589
   107
  end;
haftmann@24218
   108
haftmann@24218
   109
local
haftmann@24218
   110
haftmann@24218
   111
fun gen_instance mk_prop add_thm after_qed insts thy =
haftmann@24218
   112
  let
haftmann@24218
   113
    fun after_qed' results =
haftmann@24218
   114
      ProofContext.theory ((fold o fold) add_thm results #> after_qed);
haftmann@24218
   115
  in
haftmann@24218
   116
    thy
haftmann@24218
   117
    |> ProofContext.init
haftmann@24589
   118
    |> Proof.theorem_i NONE after_qed' ((map (fn t => [(t, [])])
haftmann@24589
   119
        o maps (mk_prop thy)) insts)
haftmann@24218
   120
  end;
haftmann@24218
   121
haftmann@24218
   122
in
haftmann@24218
   123
haftmann@24589
   124
val instance_arity =
haftmann@24218
   125
  gen_instance (Logic.mk_arities oo Sign.cert_arity) AxClass.add_arity;
haftmann@24589
   126
val classrel =
haftmann@24218
   127
  gen_instance (single oo (Logic.mk_classrel oo AxClass.cert_classrel))
haftmann@24218
   128
    AxClass.add_classrel I o single;
haftmann@24589
   129
val classrel_cmd =
haftmann@24589
   130
  gen_instance (single oo (Logic.mk_classrel oo AxClass.read_classrel))
haftmann@24589
   131
    AxClass.add_classrel I o single;
haftmann@24218
   132
haftmann@24218
   133
end; (*local*)
haftmann@24218
   134
haftmann@24218
   135
haftmann@24589
   136
(** explicit constants for overloaded definitions **)
haftmann@24304
   137
haftmann@24304
   138
structure InstData = TheoryDataFun
haftmann@24304
   139
(
haftmann@24423
   140
  type T = (string * thm) Symtab.table Symtab.table * (string * string) Symtab.table;
haftmann@24423
   141
    (*constant name ~> type constructor ~> (constant name, equation),
haftmann@24423
   142
        constant name ~> (constant name, type constructor)*)
haftmann@24423
   143
  val empty = (Symtab.empty, Symtab.empty);
haftmann@24304
   144
  val copy = I;
haftmann@24304
   145
  val extend = I;
haftmann@24423
   146
  fun merge _ ((taba1, tabb1), (taba2, tabb2)) =
haftmann@24423
   147
    (Symtab.join (K (Symtab.merge (K true))) (taba1, taba2),
haftmann@24423
   148
      Symtab.merge (K true) (tabb1, tabb2));
haftmann@24304
   149
);
haftmann@24304
   150
haftmann@24589
   151
fun inst_thms f thy = (Symtab.fold (Symtab.fold (cons o f o snd o snd) o snd) o fst)
haftmann@24589
   152
    (InstData.get thy) [];
haftmann@24589
   153
fun add_inst (c, tyco) inst = (InstData.map o apfst
haftmann@24589
   154
      o Symtab.map_default (c, Symtab.empty)) (Symtab.update_new (tyco, inst))
haftmann@24423
   155
  #> (InstData.map o apsnd) (Symtab.update_new (fst inst, (c, tyco)));
haftmann@24304
   156
haftmann@24589
   157
fun unoverload thy = MetaSimplifier.rewrite false (inst_thms I thy);
haftmann@24589
   158
fun overload thy = MetaSimplifier.rewrite false (inst_thms symmetric thy);
haftmann@24304
   159
haftmann@24304
   160
fun inst_const thy (c, tyco) =
haftmann@24423
   161
  (fst o the o Symtab.lookup ((the o Symtab.lookup (fst (InstData.get thy))) c)) tyco;
haftmann@24423
   162
fun unoverload_const thy (c_ty as (c, _)) =
haftmann@24423
   163
  case AxClass.class_of_param thy c
haftmann@24423
   164
   of SOME class => (case Sign.const_typargs thy c_ty
haftmann@24589
   165
       of [Type (tyco, _)] => (case Symtab.lookup
haftmann@24589
   166
           ((the o Symtab.lookup (fst (InstData.get thy))) c) tyco
haftmann@24423
   167
             of SOME (c, _) => c
haftmann@24423
   168
              | NONE => c)
haftmann@24423
   169
        | [_] => c)
haftmann@24423
   170
    | NONE => c;
haftmann@24423
   171
haftmann@24423
   172
val param_const = Symtab.lookup o snd o InstData.get;
haftmann@24304
   173
haftmann@24304
   174
fun add_inst_def (class, tyco) (c, ty) thy =
haftmann@24304
   175
  let
haftmann@24304
   176
    val tyco_base = NameSpace.base tyco;
haftmann@24304
   177
    val name_inst = NameSpace.base class ^ "_" ^ tyco_base ^ "_inst";
haftmann@24304
   178
    val c_inst_base = NameSpace.base c ^ "_" ^ tyco_base;
haftmann@24304
   179
  in
haftmann@24304
   180
    thy
haftmann@24304
   181
    |> Sign.sticky_prefix name_inst
wenzelm@24968
   182
    |> Sign.declare_const [] (c_inst_base, ty, Syntax.NoSyn)
wenzelm@24968
   183
    |-> (fn const as Const (c_inst, _) =>
wenzelm@24968
   184
      PureThy.add_defs_i false
wenzelm@24968
   185
        [((Thm.def_name c_inst_base, Logic.mk_equals (const, Const (c, ty))), [])]
wenzelm@24968
   186
      #-> (fn [thm] => add_inst (c, tyco) (c_inst, Thm.symmetric thm)))
haftmann@24435
   187
    |> Sign.restore_naming thy
haftmann@24304
   188
  end;
haftmann@24304
   189
haftmann@24304
   190
fun add_inst_def' (class, tyco) (c, ty) thy =
haftmann@24423
   191
  if case Symtab.lookup (fst (InstData.get thy)) c
haftmann@24304
   192
   of NONE => true
haftmann@24304
   193
    | SOME tab => is_none (Symtab.lookup tab tyco)
haftmann@24304
   194
  then add_inst_def (class, tyco) (c, Logic.unvarifyT ty) thy
haftmann@24304
   195
  else thy;
haftmann@24304
   196
haftmann@24304
   197
fun add_def ((class, tyco), ((name, prop), atts)) thy =
haftmann@24304
   198
  let
haftmann@24589
   199
    val ((lhs as Const (c, ty), args), rhs) =
haftmann@24589
   200
      (apfst Term.strip_comb o Logic.dest_equals) prop;
haftmann@24701
   201
    fun (*add_inst' def ([], (Const (c_inst, ty))) =
haftmann@24423
   202
          if forall (fn TFree _ => true | _ => false) (Sign.const_typargs thy (c_inst, ty))
haftmann@24304
   203
          then add_inst (c, tyco) (c_inst, def)
haftmann@24304
   204
          else add_inst_def (class, tyco) (c, ty)
haftmann@24701
   205
      |*) add_inst' _ t = add_inst_def (class, tyco) (c, ty);
haftmann@24304
   206
  in
haftmann@24304
   207
    thy
haftmann@24304
   208
    |> PureThy.add_defs_i true [((name, prop), map (Attrib.attribute thy) atts)]
haftmann@24304
   209
    |-> (fn [def] => add_inst' def (args, rhs) #> pair def)
haftmann@24304
   210
  end;
haftmann@24304
   211
haftmann@24304
   212
haftmann@24589
   213
(** instances with implicit parameter handling **)
haftmann@24218
   214
haftmann@24218
   215
local
haftmann@24218
   216
wenzelm@24707
   217
fun gen_read_def thy prep_att parse_prop ((raw_name, raw_atts), raw_t) =
haftmann@24218
   218
  let
wenzelm@24707
   219
    val ctxt = ProofContext.init thy;
wenzelm@24707
   220
    val t = parse_prop ctxt raw_t |> Syntax.check_prop ctxt;
haftmann@24218
   221
    val ((c, ty), _) = Sign.cert_def (Sign.pp thy) t;
haftmann@24218
   222
    val atts = map (prep_att thy) raw_atts;
haftmann@24218
   223
    val insts = Consts.typargs (Sign.consts_of thy) (c, ty);
haftmann@24218
   224
    val name = case raw_name
haftmann@24218
   225
     of "" => NONE
haftmann@24218
   226
      | _ => SOME raw_name;
haftmann@24218
   227
  in (c, (insts, ((name, t), atts))) end;
haftmann@24218
   228
wenzelm@24707
   229
fun read_def_cmd thy = gen_read_def thy Attrib.intern_src Syntax.parse_prop;
haftmann@24218
   230
fun read_def thy = gen_read_def thy (K I) (K I);
haftmann@24218
   231
haftmann@24589
   232
fun gen_instance prep_arity read_def do_proof raw_arities raw_defs after_qed theory =
haftmann@24218
   233
  let
haftmann@24218
   234
    val arities = map (prep_arity theory) raw_arities;
haftmann@24218
   235
    val _ = if null arities then error "at least one arity must be given" else ();
haftmann@24218
   236
    val _ = case (duplicates (op =) o map #1) arities
haftmann@24218
   237
     of [] => ()
haftmann@24218
   238
      | dupl_tycos => error ("type constructors occur more than once in arities: "
haftmann@24218
   239
          ^ (commas o map quote) dupl_tycos);
haftmann@24836
   240
    fun get_remove_constraint c thy =
haftmann@24836
   241
      let
haftmann@24836
   242
        val ty = Sign.the_const_constraint thy c;
haftmann@24836
   243
      in
haftmann@24836
   244
        thy
haftmann@24836
   245
        |> Sign.add_const_constraint (c, NONE)
haftmann@24836
   246
        |> pair (c, Logic.unvarifyT ty)
haftmann@24836
   247
      end;
haftmann@24218
   248
    fun get_consts_class tyco ty class =
haftmann@24218
   249
      let
wenzelm@24968
   250
        val cs = (these o try (#params o AxClass.get_info theory)) class;
haftmann@24218
   251
        val subst_ty = map_type_tfree (K ty);
haftmann@24218
   252
      in
haftmann@24304
   253
        map (fn (c, ty) => (c, ((class, tyco), subst_ty ty))) cs
haftmann@24218
   254
      end;
haftmann@24218
   255
    fun get_consts_sort (tyco, asorts, sort) =
haftmann@24218
   256
      let
haftmann@24589
   257
        val ty = Type (tyco, map (fn (v, sort) => TVar ((v, 0), sort))
wenzelm@24847
   258
          (Name.names Name.context Name.aT asorts))
wenzelm@24731
   259
      in maps (get_consts_class tyco ty) (Sign.complete_sort theory sort) end;
haftmann@24218
   260
    val cs = maps get_consts_sort arities;
haftmann@24218
   261
    fun mk_typnorm thy (ty, ty_sc) =
haftmann@24218
   262
      case try (Sign.typ_match thy (Logic.varifyT ty_sc, ty)) Vartab.empty
haftmann@24218
   263
       of SOME env => SOME (Logic.varifyT #> Envir.typ_subst_TVars env #> Logic.unvarifyT)
haftmann@24218
   264
        | NONE => NONE;
haftmann@24218
   265
    fun read_defs defs cs thy_read =
haftmann@24218
   266
      let
haftmann@24218
   267
        fun read raw_def cs =
haftmann@24218
   268
          let
haftmann@24218
   269
            val (c, (inst, ((name_opt, t), atts))) = read_def thy_read raw_def;
haftmann@24218
   270
            val ty = Consts.instance (Sign.consts_of thy_read) (c, inst);
haftmann@24304
   271
            val ((class, tyco), ty') = case AList.lookup (op =) cs c
haftmann@24218
   272
             of NONE => error ("illegal definition for constant " ^ quote c)
haftmann@24218
   273
              | SOME class_ty => class_ty;
haftmann@24218
   274
            val name = case name_opt
haftmann@24218
   275
             of NONE => Thm.def_name (Logic.name_arity (tyco, [], c))
haftmann@24218
   276
              | SOME name => name;
haftmann@24218
   277
            val t' = case mk_typnorm thy_read (ty', ty)
haftmann@24218
   278
             of NONE => error ("illegal definition for constant " ^
haftmann@24218
   279
                  quote (c ^ "::" ^ setmp show_sorts true
haftmann@24218
   280
                    (Sign.string_of_typ thy_read) ty))
haftmann@24218
   281
              | SOME norm => map_types norm t
haftmann@24218
   282
          in (((class, tyco), ((name, t'), atts)), AList.delete (op =) c cs) end;
haftmann@24218
   283
      in fold_map read defs cs end;
haftmann@24304
   284
    val (defs, other_cs) = read_defs raw_defs cs
haftmann@24218
   285
      (fold Sign.primitive_arity arities (Theory.copy theory));
haftmann@24423
   286
    fun after_qed' cs defs =
wenzelm@24766
   287
      fold Sign.add_const_constraint (map (apsnd SOME) cs)
haftmann@24423
   288
      #> after_qed defs;
haftmann@24218
   289
  in
haftmann@24218
   290
    theory
haftmann@24748
   291
    |> fold_map get_remove_constraint (map fst cs |> distinct (op =))
haftmann@24304
   292
    ||>> fold_map add_def defs
haftmann@24304
   293
    ||> fold (fn (c, ((class, tyco), ty)) => add_inst_def' (class, tyco) (c, ty)) other_cs
haftmann@24423
   294
    |-> (fn (cs, defs) => do_proof (after_qed' cs defs) arities defs)
haftmann@24218
   295
  end;
haftmann@24218
   296
haftmann@24423
   297
fun tactic_proof tac f arities defs =
haftmann@24218
   298
  fold (fn arity => AxClass.prove_arity arity tac) arities
haftmann@24423
   299
  #> f
haftmann@24423
   300
  #> pair defs;
haftmann@24218
   301
haftmann@24218
   302
in
haftmann@24218
   303
haftmann@24589
   304
val instance = gen_instance Sign.cert_arity read_def
haftmann@24589
   305
  (fn f => fn arities => fn defs => instance_arity f arities);
haftmann@24589
   306
val instance_cmd = gen_instance Sign.read_arity read_def_cmd
haftmann@24589
   307
  (fn f => fn arities => fn defs => instance_arity f arities);
haftmann@24589
   308
fun prove_instance tac arities defs =
haftmann@24589
   309
  gen_instance Sign.cert_arity read_def
haftmann@24589
   310
    (tactic_proof tac) arities defs (K I);
haftmann@24218
   311
haftmann@24218
   312
end; (*local*)
haftmann@24218
   313
haftmann@24218
   314
haftmann@24218
   315
haftmann@24589
   316
(** class data **)
haftmann@24218
   317
haftmann@24218
   318
datatype class_data = ClassData of {
haftmann@24218
   319
  locale: string,
haftmann@24218
   320
  consts: (string * string) list
haftmann@24836
   321
    (*locale parameter ~> constant name*),
haftmann@24748
   322
  local_sort: sort,
haftmann@24589
   323
  inst: typ Symtab.table * term Symtab.table
haftmann@24589
   324
    (*canonical interpretation*),
haftmann@24657
   325
  intro: thm,
haftmann@24748
   326
  local_syntax: bool,
haftmann@24657
   327
  defs: thm list,
haftmann@24914
   328
  operations: (string * (term * int) option) list,
haftmann@24836
   329
    (*constant name ~> (locale term, instantiaton index of class typ)*)
haftmann@24914
   330
  constraints: (string * typ) list
haftmann@24657
   331
};
haftmann@24218
   332
haftmann@24657
   333
fun rep_class_data (ClassData d) = d;
haftmann@24914
   334
fun mk_class_data ((locale, consts, local_sort, inst, intro, local_syntax),
haftmann@24914
   335
    (defs, (operations, constraints))) =
haftmann@24914
   336
  ClassData { locale = locale, consts = consts, local_sort = local_sort, inst = inst,
haftmann@24914
   337
    intro = intro, local_syntax = local_syntax, defs = defs, operations = operations,
haftmann@24914
   338
    constraints = constraints };
haftmann@24914
   339
fun map_class_data f (ClassData { locale, consts, local_sort, inst, intro,
haftmann@24914
   340
    local_syntax, defs, operations, constraints }) =
haftmann@24914
   341
  mk_class_data (f ((locale, consts, local_sort, inst, intro, local_syntax),
haftmann@24914
   342
    (defs, (operations, constraints))));
haftmann@24914
   343
fun merge_class_data _ (ClassData { locale = locale, consts = consts,
haftmann@24914
   344
    local_sort = local_sort, inst = inst, intro = intro, local_syntax = local_syntax,
haftmann@24914
   345
    defs = defs1, operations = operations1, constraints = constraints1 },
haftmann@24748
   346
  ClassData { locale = _, consts = _, local_sort = _, inst = _, intro = _, local_syntax = _,
haftmann@24914
   347
    defs = defs2, operations = operations2, constraints = constraints2 }) =
haftmann@24748
   348
  mk_class_data ((locale, consts, local_sort, inst, intro, local_syntax),
haftmann@24914
   349
    (Thm.merge_thms (defs1, defs2),
haftmann@24914
   350
      (AList.merge (op =) (K true) (operations1, operations2),
haftmann@24914
   351
      AList.merge (op =) (K true) (constraints1, constraints2))));
haftmann@24218
   352
haftmann@24218
   353
fun merge_pair f1 f2 ((x1, y1), (x2, y2)) = (f1 (x1, x2), f2 (y1, y2));
haftmann@24218
   354
haftmann@24218
   355
structure ClassData = TheoryDataFun
haftmann@24218
   356
(
haftmann@24657
   357
  type T = class_data Graph.T * class Symtab.table
haftmann@24657
   358
    (*locale name ~> class name*);
haftmann@24218
   359
  val empty = (Graph.empty, Symtab.empty);
haftmann@24218
   360
  val copy = I;
haftmann@24218
   361
  val extend = I;
haftmann@24657
   362
  fun merge _ = merge_pair (Graph.join merge_class_data) (Symtab.merge (K true));
haftmann@24218
   363
);
haftmann@24218
   364
haftmann@24218
   365
haftmann@24218
   366
(* queries *)
haftmann@24218
   367
haftmann@24657
   368
val lookup_class_data = Option.map rep_class_data oo try o Graph.get_node
haftmann@24589
   369
  o fst o ClassData.get;
haftmann@24218
   370
fun class_of_locale thy = Symtab.lookup ((snd o ClassData.get) thy);
haftmann@24218
   371
haftmann@24589
   372
fun the_class_data thy class = case lookup_class_data thy class
haftmann@24589
   373
 of NONE => error ("undeclared class " ^ quote class)
haftmann@24589
   374
  | SOME data => data;
haftmann@24218
   375
haftmann@24914
   376
val locale_of_class = #locale oo the_class_data;
haftmann@24914
   377
haftmann@24218
   378
val ancestry = Graph.all_succs o fst o ClassData.get;
haftmann@24218
   379
haftmann@24435
   380
fun params_of_sort thy =
haftmann@24218
   381
  let
haftmann@24218
   382
    fun params class =
haftmann@24218
   383
      let
wenzelm@24930
   384
        val const_typs = (#params o AxClass.get_info thy) class;
haftmann@24657
   385
        val const_names = (#consts o the_class_data thy) class;
haftmann@24218
   386
      in
haftmann@24218
   387
        (map o apsnd) (fn c => (c, (the o AList.lookup (op =) const_typs) c)) const_names
haftmann@24218
   388
      end;
haftmann@24218
   389
  in maps params o ancestry thy end;
haftmann@24218
   390
haftmann@24657
   391
fun these_defs thy = maps (these o Option.map #defs o lookup_class_data thy) o ancestry thy;
haftmann@24218
   392
haftmann@24218
   393
fun these_intros thy =
haftmann@24657
   394
  Graph.fold (fn (_, (data, _)) => insert Thm.eq_thm ((#intro o rep_class_data) data))
haftmann@24218
   395
    ((fst o ClassData.get) thy) [];
haftmann@24218
   396
haftmann@24836
   397
fun these_operations thy =
haftmann@24836
   398
  maps (#operations o the_class_data thy) o ancestry thy;
haftmann@24657
   399
haftmann@24914
   400
fun these_constraints thy =
haftmann@24914
   401
  maps (#constraints o the_class_data thy) o ancestry thy;
haftmann@24914
   402
haftmann@24836
   403
fun local_operation thy = Option.join oo AList.lookup (op =) o these_operations thy;
haftmann@24657
   404
haftmann@24901
   405
fun sups_local_sort thy sort =
haftmann@24901
   406
  let
haftmann@24901
   407
    val sups = filter (is_some o lookup_class_data thy) sort
haftmann@24901
   408
      |> Sign.minimize_sort thy;
haftmann@24901
   409
    val local_sort = case sups
haftmann@24901
   410
     of sup :: _ => #local_sort (the_class_data thy sup)
haftmann@24901
   411
      | [] => sort;
haftmann@24901
   412
  in (sups, local_sort) end;
haftmann@24901
   413
haftmann@24836
   414
fun local_syntax thy = #local_syntax o the_class_data thy;
haftmann@24748
   415
haftmann@24218
   416
fun print_classes thy =
haftmann@24218
   417
  let
wenzelm@24920
   418
    val ctxt = ProofContext.init thy;
wenzelm@24920
   419
haftmann@24218
   420
    val algebra = Sign.classes_of thy;
haftmann@24218
   421
    val arities =
haftmann@24218
   422
      Symtab.empty
haftmann@24218
   423
      |> Symtab.fold (fn (tyco, arities) => fold (fn (class, _) =>
haftmann@24218
   424
           Symtab.map_default (class, []) (insert (op =) tyco)) arities)
haftmann@24218
   425
             ((#arities o Sorts.rep_algebra) algebra);
haftmann@24218
   426
    val the_arities = these o Symtab.lookup arities;
haftmann@24218
   427
    fun mk_arity class tyco =
haftmann@24218
   428
      let
haftmann@24218
   429
        val Ss = Sorts.mg_domain algebra tyco [class];
wenzelm@24920
   430
      in Syntax.pretty_arity ctxt (tyco, Ss, [class]) end;
haftmann@24218
   431
    fun mk_param (c, ty) = Pretty.str (Sign.extern_const thy c ^ " :: "
wenzelm@24920
   432
      ^ setmp show_sorts false (Syntax.string_of_typ ctxt o Type.strip_sorts) ty);
haftmann@24218
   433
    fun mk_entry class = (Pretty.block o Pretty.fbreaks o map_filter I) [
haftmann@24218
   434
      (SOME o Pretty.str) ("class " ^ class ^ ":"),
haftmann@24218
   435
      (SOME o Pretty.block) [Pretty.str "supersort: ",
wenzelm@24920
   436
        (Syntax.pretty_sort ctxt o Sign.minimize_sort thy o Sign.super_classes thy) class],
haftmann@24657
   437
      Option.map (Pretty.str o prefix "locale: " o #locale) (lookup_class_data thy class),
haftmann@24218
   438
      ((fn [] => NONE | ps => (SOME o Pretty.block o Pretty.fbreaks) (Pretty.str "parameters:" :: ps)) o map mk_param
wenzelm@24930
   439
        o these o Option.map #params o try (AxClass.get_info thy)) class,
haftmann@24218
   440
      (SOME o Pretty.block o Pretty.breaks) [
haftmann@24218
   441
        Pretty.str "instances:",
haftmann@24218
   442
        Pretty.list "" "" (map (mk_arity class) (the_arities class))
haftmann@24218
   443
      ]
haftmann@24218
   444
    ]
haftmann@24218
   445
  in
haftmann@24589
   446
    (Pretty.writeln o Pretty.chunks o separate (Pretty.str "")
haftmann@24589
   447
      o map mk_entry o Sorts.all_classes) algebra
haftmann@24218
   448
  end;
haftmann@24218
   449
haftmann@24218
   450
haftmann@24218
   451
(* updaters *)
haftmann@24218
   452
haftmann@24748
   453
fun add_class_data ((class, superclasses), (locale, consts, local_sort, inst, intro, local_syntax)) =
haftmann@24218
   454
  ClassData.map (fn (gr, tab) => (
haftmann@24218
   455
    gr
haftmann@24914
   456
    |> Graph.new_node (class, mk_class_data ((locale, (map o pairself) fst consts,
haftmann@24914
   457
        local_sort, inst, intro, local_syntax),
haftmann@24914
   458
          ([], (map (apfst fst o apsnd (SOME o rpair 0 o Free) o swap) consts, map snd consts))))
haftmann@24218
   459
    |> fold (curry Graph.add_edge class) superclasses,
haftmann@24218
   460
    tab
haftmann@24218
   461
    |> Symtab.update (locale, class)
haftmann@24218
   462
  ));
haftmann@24218
   463
haftmann@24901
   464
fun register_const class (entry, def) =
haftmann@24657
   465
  (ClassData.map o apfst o Graph.map_node class o map_class_data o apsnd)
haftmann@24914
   466
    (fn (defs, (operations, constraints)) =>
haftmann@24914
   467
      (def :: defs, (apsnd (SOME o snd) entry :: operations, apsnd fst entry :: constraints)));
haftmann@24836
   468
haftmann@24914
   469
fun register_abbrev class (abbrev, ty) =
haftmann@24836
   470
  (ClassData.map o apfst o Graph.map_node class o map_class_data o apsnd o apsnd)
haftmann@24914
   471
    (fn (operations, constraints) => ((abbrev, NONE) :: operations, (abbrev, ty) :: constraints));
haftmann@24218
   472
haftmann@24589
   473
haftmann@24589
   474
(** rule calculation, tactics and methods **)
haftmann@24589
   475
haftmann@24589
   476
fun class_intro thy locale class sups =
haftmann@24589
   477
  let
haftmann@24589
   478
    fun class_elim class =
wenzelm@24930
   479
      case (map Drule.unconstrainTs o #axioms o AxClass.get_info thy) class
haftmann@24589
   480
       of [thm] => SOME thm
haftmann@24589
   481
        | [] => NONE;
haftmann@24589
   482
    val pred_intro = case Locale.intros thy locale
haftmann@24589
   483
     of ([ax_intro], [intro]) => intro |> OF_LAST ax_intro |> SOME
haftmann@24589
   484
      | ([intro], []) => SOME intro
haftmann@24589
   485
      | ([], [intro]) => SOME intro
haftmann@24589
   486
      | _ => NONE;
haftmann@24589
   487
    val pred_intro' = pred_intro
haftmann@24589
   488
      |> Option.map (fn intro => intro OF map_filter class_elim sups);
wenzelm@24930
   489
    val class_intro = (#intro o AxClass.get_info thy) class;
haftmann@24589
   490
    val raw_intro = case pred_intro'
haftmann@24589
   491
     of SOME pred_intro => class_intro |> OF_LAST pred_intro
haftmann@24589
   492
      | NONE => class_intro;
haftmann@24589
   493
    val sort = Sign.super_classes thy class;
wenzelm@24847
   494
    val typ = TVar ((Name.aT, 0), sort);
haftmann@24589
   495
    val defs = these_defs thy sups;
haftmann@24589
   496
  in
haftmann@24589
   497
    raw_intro
haftmann@24589
   498
    |> Drule.instantiate' [SOME (Thm.ctyp_of thy typ)] []
haftmann@24589
   499
    |> strip_all_ofclass thy sort
haftmann@24589
   500
    |> Thm.strip_shyps
haftmann@24589
   501
    |> MetaSimplifier.rewrite_rule defs
haftmann@24589
   502
    |> Drule.unconstrainTs
haftmann@24589
   503
  end;
haftmann@24589
   504
haftmann@24589
   505
fun class_interpretation class facts defs thy =
haftmann@24589
   506
  let
haftmann@24657
   507
    val { locale, inst, ... } = the_class_data thy class;
haftmann@24589
   508
    val tac = (ALLGOALS o ProofContext.fact_tac) facts;
haftmann@24589
   509
    val prfx = Logic.const_of_class (NameSpace.base class);
haftmann@24589
   510
  in
haftmann@24589
   511
    prove_interpretation tac ((false, prfx), []) (Locale.Locale locale)
haftmann@24589
   512
      (inst, defs) thy
haftmann@24589
   513
  end;
haftmann@24589
   514
haftmann@24589
   515
fun interpretation_in_rule thy (class1, class2) =
haftmann@24589
   516
  let
haftmann@24589
   517
    fun mk_axioms class =
haftmann@24589
   518
      let
haftmann@24657
   519
        val { locale, inst = (_, insttab), ... } = the_class_data thy class;
haftmann@24589
   520
      in
haftmann@24589
   521
        Locale.global_asms_of thy locale
haftmann@24589
   522
        |> maps snd
haftmann@24589
   523
        |> (map o map_aterms) (fn Free (s, _) => (the o Symtab.lookup insttab) s | t => t)
wenzelm@24847
   524
        |> (map o map_types o map_atyps) (fn TFree _ => TFree (Name.aT, [class1]) | T => T)
haftmann@24589
   525
        |> map (ObjectLogic.ensure_propT thy)
haftmann@24589
   526
      end;
haftmann@24589
   527
    val (prems, concls) = pairself mk_axioms (class1, class2);
haftmann@24589
   528
  in
haftmann@24589
   529
    Goal.prove_global thy [] prems (Logic.mk_conjunction_list concls)
haftmann@24589
   530
      (Locale.intro_locales_tac true (ProofContext.init thy))
haftmann@24589
   531
  end;
haftmann@24218
   532
haftmann@24218
   533
fun intro_classes_tac facts st =
haftmann@24218
   534
  let
haftmann@24218
   535
    val thy = Thm.theory_of_thm st;
haftmann@24218
   536
    val classes = Sign.all_classes thy;
haftmann@24218
   537
    val class_trivs = map (Thm.class_triv thy) classes;
haftmann@24218
   538
    val class_intros = these_intros thy;
wenzelm@24930
   539
    val axclass_intros = map_filter (try (#intro o AxClass.get_info thy)) classes;
haftmann@24218
   540
  in
wenzelm@24930
   541
    (ALLGOALS (Method.insert_tac facts THEN'
wenzelm@24930
   542
      REPEAT_ALL_NEW (resolve_tac (class_trivs @ class_intros @ axclass_intros)))
wenzelm@24930
   543
    THEN Tactic.distinct_subgoals_tac) st
haftmann@24218
   544
  end;
haftmann@24218
   545
haftmann@24218
   546
fun default_intro_classes_tac [] = intro_classes_tac []
wenzelm@24930
   547
  | default_intro_classes_tac _ = no_tac;
haftmann@24218
   548
haftmann@24218
   549
fun default_tac rules ctxt facts =
haftmann@24218
   550
  HEADGOAL (Method.some_rule_tac rules ctxt facts) ORELSE
haftmann@24218
   551
    default_intro_classes_tac facts;
haftmann@24218
   552
haftmann@24218
   553
val _ = Context.add_setup (Method.add_methods
haftmann@24218
   554
 [("intro_classes", Method.no_args (Method.METHOD intro_classes_tac),
haftmann@24218
   555
    "back-chain introduction rules of classes"),
haftmann@24218
   556
  ("default", Method.thms_ctxt_args (Method.METHOD oo default_tac),
haftmann@24218
   557
    "apply some intro/elim rule")]);
haftmann@24218
   558
haftmann@24218
   559
haftmann@24589
   560
(** classes and class target **)
haftmann@24218
   561
haftmann@24901
   562
(* class context initialization *)
haftmann@24748
   563
haftmann@24914
   564
fun remove_constraint local_sort c ctxt =
haftmann@24914
   565
  let
haftmann@24914
   566
    val ty = ProofContext.the_const_constraint ctxt c;
haftmann@24914
   567
    val ty' = map_atyps (fn ty as TFree (v, _) => if v = Name.aT
haftmann@24914
   568
      then TFree (v, local_sort) else ty | ty => ty) ty;
haftmann@24914
   569
  in
haftmann@24914
   570
    ctxt
haftmann@24914
   571
    |> ProofContext.add_const_constraint (c, SOME ty')
haftmann@24914
   572
  end;
haftmann@24914
   573
haftmann@24914
   574
fun sort_term_check sups local_sort ts ctxt =
haftmann@24748
   575
  let
haftmann@24901
   576
    val thy = ProofContext.theory_of ctxt;
haftmann@24901
   577
    val local_operation = local_operation thy sups;
haftmann@24914
   578
    val constraints = these_constraints thy sups;
haftmann@24901
   579
    val consts = ProofContext.consts_of ctxt;
haftmann@24901
   580
    fun check_typ (c, ty) (t', idx) = case nth (Consts.typargs consts (c, ty)) idx
haftmann@24901
   581
     of TFree (v, _) => if v = Name.aT
haftmann@24901
   582
          then apfst (AList.update (op =) ((c, ty), t')) else I
haftmann@24901
   583
      | TVar (vi, _) => if TypeInfer.is_param vi
haftmann@24901
   584
          then apfst (AList.update (op =) ((c, ty), t'))
haftmann@24901
   585
            #> apsnd (insert (op =) vi) else I
haftmann@24901
   586
      | _ => I;
haftmann@24901
   587
    fun add_const (Const (c, ty)) = (case local_operation c
haftmann@24901
   588
         of SOME (t', idx) => check_typ (c, ty) (t', idx)
haftmann@24901
   589
          | NONE => I)
haftmann@24901
   590
      | add_const _ = I;
haftmann@24901
   591
    val (cs, typarams) = (fold o fold_aterms) add_const ts ([], []);
haftmann@24901
   592
    val ts' = map (map_aterms
haftmann@24901
   593
        (fn t as Const (c, ty) => the_default t (AList.lookup (op =) cs (c, ty)) | t => t)
haftmann@24901
   594
      #> (map_types o map_type_tvar) (fn var as (vi, _) => if member (op =) typarams vi
haftmann@24901
   595
           then TFree (Name.aT, local_sort) else TVar var)) ts;
haftmann@24901
   596
    val ctxt' = fold (ProofContext.add_const_constraint o apsnd SOME) constraints ctxt;
haftmann@24901
   597
  in (ts', ctxt') end;
haftmann@24748
   598
haftmann@24914
   599
fun init_class_ctxt sups local_sort ctxt =
haftmann@24914
   600
  let
haftmann@24914
   601
    val operations = these_operations (ProofContext.theory_of ctxt) sups;
haftmann@24914
   602
  in
haftmann@24914
   603
    ctxt
haftmann@24914
   604
    |> Variable.declare_term
haftmann@24914
   605
        (Logic.mk_type (TFree (Name.aT, local_sort)))
haftmann@24914
   606
    |> fold (remove_constraint local_sort o fst) operations
haftmann@24914
   607
    |> Context.proof_map (Syntax.add_term_check 50 "class"
haftmann@24914
   608
        (sort_term_check sups local_sort))
haftmann@24914
   609
  end;
haftmann@24901
   610
haftmann@24901
   611
fun init class ctxt =
haftmann@24914
   612
  init_class_ctxt [class]
haftmann@24914
   613
    ((#local_sort o the_class_data (ProofContext.theory_of ctxt)) class) ctxt;
haftmann@24914
   614
haftmann@24748
   615
haftmann@24589
   616
(* class definition *)
haftmann@24218
   617
haftmann@24218
   618
local
haftmann@24218
   619
haftmann@24748
   620
fun gen_class_spec prep_class prep_expr process_expr thy raw_supclasses raw_includes_elems =
haftmann@24218
   621
  let
haftmann@24748
   622
    val supclasses = map (prep_class thy) raw_supclasses;
haftmann@24901
   623
    val (sups, local_sort) = sups_local_sort thy supclasses;
haftmann@24748
   624
    val supsort = Sign.minimize_sort thy supclasses;
haftmann@24748
   625
    val suplocales = map (Locale.Locale o #locale o the_class_data thy) sups;
haftmann@24748
   626
    val (raw_elems, includes) = fold_rev (fn Locale.Elem e => apfst (cons e)
haftmann@24748
   627
      | Locale.Expr i => apsnd (cons (prep_expr thy i))) raw_includes_elems ([], []);
haftmann@24748
   628
    val supexpr = Locale.Merge suplocales;
haftmann@24748
   629
    val supparams = (map fst o Locale.parameters_of_expr thy) supexpr;
haftmann@24748
   630
    val supconsts = AList.make (the o AList.lookup (op =) (params_of_sort thy sups))
haftmann@24748
   631
      (map fst supparams);
haftmann@24748
   632
    val mergeexpr = Locale.Merge (suplocales @ includes);
haftmann@24748
   633
    val constrain = Element.Constrains ((map o apsnd o map_atyps)
wenzelm@24847
   634
      (fn TFree (_, sort) => TFree (Name.aT, sort)) supparams);
haftmann@24748
   635
  in
haftmann@24748
   636
    ProofContext.init thy
haftmann@24748
   637
    |> Locale.cert_expr supexpr [constrain]
haftmann@24748
   638
    |> snd
haftmann@24914
   639
    |> init_class_ctxt sups local_sort
haftmann@24748
   640
    |> process_expr Locale.empty raw_elems
haftmann@24748
   641
    |> fst
haftmann@24836
   642
    |> (fn elems => ((((sups, supconsts), (supsort, local_sort, mergeexpr)),
haftmann@24748
   643
          (*FIXME*) if null includes then constrain :: elems else elems)))
haftmann@24748
   644
  end;
haftmann@24748
   645
haftmann@24748
   646
val read_class_spec = gen_class_spec Sign.intern_class Locale.intern_expr Locale.read_expr;
haftmann@24748
   647
val check_class_spec = gen_class_spec (K I) (K I) Locale.cert_expr;
haftmann@24748
   648
wenzelm@24968
   649
wenzelm@24968
   650
fun define_class_params (name, raw_superclasses) raw_consts raw_dep_axioms other_consts thy =
wenzelm@24968
   651
  let
wenzelm@24968
   652
    val superclasses = map (Sign.certify_class thy) raw_superclasses;
wenzelm@24968
   653
    val consts = (map o apfst o apsnd) (Sign.certify_typ thy) raw_consts;
wenzelm@24968
   654
    fun add_const ((c, ty), syn) = Sign.declare_const [] (c, ty, syn) #>> Term.dest_Const;
wenzelm@24968
   655
    fun mk_axioms cs thy =
wenzelm@24968
   656
      raw_dep_axioms thy cs
wenzelm@24968
   657
      |> (map o apsnd o map) (Sign.cert_prop thy)
wenzelm@24968
   658
      |> rpair thy;
wenzelm@24968
   659
    fun add_constraint class (c, ty) =
wenzelm@24968
   660
      Sign.add_const_constraint (c, SOME
wenzelm@24968
   661
        (Term.map_type_tfree (fn (v, _) => TFree (v, [class])) ty));
wenzelm@24968
   662
  in
wenzelm@24968
   663
    thy
wenzelm@24968
   664
    |> Sign.add_path (Logic.const_of_class name)
wenzelm@24968
   665
    |> fold_map add_const consts
wenzelm@24968
   666
    ||> Sign.restore_naming thy
wenzelm@24968
   667
    |-> (fn cs => mk_axioms cs
wenzelm@24968
   668
    #-> (fn axioms_prop => AxClass.define_class (name, superclasses)
wenzelm@24968
   669
           (map fst cs @ other_consts) axioms_prop
wenzelm@24968
   670
    #-> (fn class => `(fn thy => AxClass.get_info thy class)
wenzelm@24968
   671
    #-> (fn {axioms, ...} => fold (add_constraint class) cs
wenzelm@24968
   672
    #> pair (class, (cs, axioms))))))
wenzelm@24968
   673
  end;
wenzelm@24968
   674
haftmann@24748
   675
fun gen_class prep_spec prep_param local_syntax bname
haftmann@24748
   676
    raw_supclasses raw_includes_elems raw_other_consts thy =
haftmann@24748
   677
  let
haftmann@24836
   678
    val (((sups, supconsts), (supsort, local_sort, mergeexpr)), elems) =
haftmann@24748
   679
      prep_spec thy raw_supclasses raw_includes_elems;
wenzelm@24968
   680
    val other_consts = map (tap (Sign.the_const_type thy) o prep_param thy) raw_other_consts;
haftmann@24589
   681
    fun mk_instT class = Symtab.empty
wenzelm@24847
   682
      |> Symtab.update (Name.aT, TFree (Name.aT, [class]));
haftmann@24589
   683
    fun mk_inst class param_names cs =
haftmann@24589
   684
      Symtab.empty
haftmann@24589
   685
      |> fold2 (fn v => fn (c, ty) => Symtab.update (v, Const
haftmann@24589
   686
           (c, Term.map_type_tfree (fn (v, _) => TFree (v, [class])) ty))) param_names cs;
haftmann@24218
   687
    fun extract_params thy name_locale =
haftmann@24218
   688
      let
haftmann@24218
   689
        val params = Locale.parameters_of thy name_locale;
haftmann@24748
   690
        val _ = if Sign.subsort thy (supsort, local_sort) then () else error
haftmann@24748
   691
          ("Sort " ^ Sign.string_of_sort thy local_sort
haftmann@24748
   692
            ^ " is less general than permitted least general sort "
haftmann@24748
   693
            ^ Sign.string_of_sort thy supsort);
haftmann@24218
   694
      in
haftmann@24901
   695
        (map fst params, params
wenzelm@24847
   696
        |> (map o apfst o apsnd o Term.map_type_tfree) (K (TFree (Name.aT, local_sort)))
haftmann@24901
   697
        |> (map o apsnd) (fork_mixfix true (SOME "") #> fst)
haftmann@24218
   698
        |> chop (length supconsts)
haftmann@24901
   699
        |> snd)
haftmann@24218
   700
      end;
haftmann@24218
   701
    fun extract_assumes name_locale params thy cs =
haftmann@24218
   702
      let
haftmann@24218
   703
        val consts = supconsts @ (map (fst o fst) params ~~ cs);
haftmann@24218
   704
        fun subst (Free (c, ty)) =
haftmann@24218
   705
              Const ((fst o the o AList.lookup (op =) consts) c, ty)
haftmann@24218
   706
          | subst t = t;
haftmann@24218
   707
        fun prep_asm ((name, atts), ts) =
haftmann@24218
   708
          ((NameSpace.base name, map (Attrib.attribute thy) atts),
haftmann@24589
   709
            (map o map_aterms) subst ts);
haftmann@24218
   710
      in
haftmann@24218
   711
        Locale.global_asms_of thy name_locale
haftmann@24218
   712
        |> map prep_asm
haftmann@24218
   713
      end;
haftmann@24218
   714
    fun note_intro name_axclass class_intro =
haftmann@24218
   715
      PureThy.note_thmss_qualified "" ((Logic.const_of_class o NameSpace.base) name_axclass)
haftmann@24218
   716
        [(("intro", []), [([class_intro], [])])]
haftmann@24218
   717
      #> snd
haftmann@24218
   718
  in
haftmann@24218
   719
    thy
haftmann@24748
   720
    |> Locale.add_locale_i (SOME "") bname mergeexpr elems
haftmann@24218
   721
    |-> (fn name_locale => ProofContext.theory_result (
haftmann@24218
   722
      `(fn thy => extract_params thy name_locale)
haftmann@24901
   723
      #-> (fn (globals, params) =>
wenzelm@24968
   724
        define_class_params (bname, supsort) params
haftmann@24589
   725
          (extract_assumes name_locale params) other_consts
haftmann@24589
   726
      #-> (fn (name_axclass, (consts, axioms)) =>
haftmann@24218
   727
        `(fn thy => class_intro thy name_locale name_axclass sups)
haftmann@24218
   728
      #-> (fn class_intro =>
haftmann@24218
   729
        add_class_data ((name_axclass, sups),
haftmann@24914
   730
          (name_locale, map fst params ~~ consts, local_sort,
haftmann@24657
   731
            (mk_instT name_axclass, mk_inst name_axclass (map fst globals)
haftmann@24748
   732
              (map snd supconsts @ consts)), class_intro, local_syntax))
haftmann@24218
   733
      #> note_intro name_axclass class_intro
haftmann@24589
   734
      #> class_interpretation name_axclass axioms []
haftmann@24218
   735
      #> pair name_axclass
haftmann@24218
   736
      )))))
haftmann@24218
   737
  end;
haftmann@24218
   738
haftmann@24218
   739
in
haftmann@24218
   740
wenzelm@24968
   741
val class_cmd = gen_class read_class_spec ((#1 o Term.dest_Const) oo Sign.read_const);
haftmann@24748
   742
val class = gen_class check_class_spec (K I);
haftmann@24218
   743
haftmann@24218
   744
end; (*local*)
haftmann@24218
   745
haftmann@24218
   746
haftmann@24589
   747
(* definition in class target *)
haftmann@24218
   748
haftmann@24218
   749
fun export_fixes thy class =
haftmann@24218
   750
  let
haftmann@24435
   751
    val consts = params_of_sort thy [class];
haftmann@24218
   752
    fun subst_aterm (t as Free (v, ty)) = (case AList.lookup (op =) consts v
haftmann@24218
   753
         of SOME (c, _) => Const (c, ty)
haftmann@24218
   754
          | NONE => t)
haftmann@24218
   755
      | subst_aterm t = t;
haftmann@24657
   756
  in Term.map_aterms subst_aterm end;
haftmann@24218
   757
haftmann@24901
   758
fun mk_operation_entry thy (c, rhs) =
haftmann@24901
   759
  let
haftmann@24914
   760
    val ty = fastype_of rhs;
haftmann@24914
   761
    val typargs = Sign.const_typargs thy (c, ty);
haftmann@24901
   762
    val typidx = find_index (fn TFree (w, _) => Name.aT = w | _ => false) typargs;
haftmann@24914
   763
  in (c, (ty, (rhs, typidx))) end;
haftmann@24901
   764
haftmann@24218
   765
fun add_const_in_class class ((c, rhs), syn) thy =
haftmann@24218
   766
  let
haftmann@24218
   767
    val prfx = (Logic.const_of_class o NameSpace.base) class;
haftmann@24657
   768
    fun mk_name c =
haftmann@24218
   769
      let
haftmann@24218
   770
        val n1 = Sign.full_name thy c;
haftmann@24218
   771
        val n2 = NameSpace.qualifier n1;
haftmann@24218
   772
        val n3 = NameSpace.base n1;
haftmann@24657
   773
      in NameSpace.implode [n2, prfx, n3] end;
haftmann@24657
   774
    val constrain_sort = curry (Sorts.inter_sort (Sign.classes_of thy)) [class];
haftmann@24748
   775
    val rhs' = export_fixes thy class rhs;
haftmann@24657
   776
    val subst_typ = Term.map_type_tfree (fn var as (w, sort) =>
wenzelm@24847
   777
      if w = Name.aT then TFree (w, constrain_sort sort) else TFree var);
haftmann@24218
   778
    val ty' = Term.fastype_of rhs';
haftmann@24657
   779
    val ty'' = subst_typ ty';
haftmann@24657
   780
    val c' = mk_name c;
haftmann@24657
   781
    val def = (c, Logic.mk_equals (Const (c', ty'), rhs'));
haftmann@24901
   782
    val (syn', _) = fork_mixfix true (SOME class) syn;
haftmann@24657
   783
    fun interpret def thy =
haftmann@24218
   784
      let
haftmann@24589
   785
        val def' = symmetric def;
haftmann@24218
   786
        val def_eq = Thm.prop_of def';
haftmann@24901
   787
        val entry = mk_operation_entry thy (c', rhs);
haftmann@24218
   788
      in
haftmann@24657
   789
        thy
haftmann@24657
   790
        |> class_interpretation class [def'] [def_eq]
haftmann@24901
   791
        |> register_const class (entry, def')
haftmann@24218
   792
      end;
haftmann@24218
   793
  in
haftmann@24218
   794
    thy
haftmann@24218
   795
    |> Sign.add_path prfx
wenzelm@24968
   796
    |> Sign.declare_const [] (c, ty', syn') |> snd
haftmann@24218
   797
    |> Sign.parent_path
haftmann@24218
   798
    |> Sign.sticky_prefix prfx
haftmann@24218
   799
    |> PureThy.add_defs_i false [(def, [])]
haftmann@24218
   800
    |-> (fn [def] => interpret def)
wenzelm@24766
   801
    |> Sign.add_const_constraint (c', SOME ty'')
haftmann@24218
   802
    |> Sign.restore_naming thy
haftmann@24914
   803
    |> pair c'
haftmann@24218
   804
  end;
haftmann@24218
   805
haftmann@24901
   806
haftmann@24901
   807
(* abbreviation in class target *)
haftmann@24901
   808
haftmann@24901
   809
fun add_abbrev_in_class class prmode ((c, rhs), syn) thy =
haftmann@24836
   810
  let
haftmann@24901
   811
    val prfx = (Logic.const_of_class o NameSpace.base) class;
haftmann@24901
   812
    fun mk_name c =
haftmann@24901
   813
      let
haftmann@24901
   814
        val n1 = Sign.full_name thy c;
haftmann@24901
   815
        val n2 = NameSpace.qualifier n1;
haftmann@24901
   816
        val n3 = NameSpace.base n1;
haftmann@24901
   817
      in NameSpace.implode [n2, prfx, prfx, n3] end;
haftmann@24901
   818
    val c' = mk_name c;
haftmann@24901
   819
    val rhs' = export_fixes thy class rhs;
haftmann@24901
   820
    val ty' = fastype_of rhs';
haftmann@24836
   821
  in
haftmann@24836
   822
    thy
wenzelm@24949
   823
    |> Sign.notation true prmode [(Const (c', ty'), syn)]
haftmann@24914
   824
    |> register_abbrev class (c', ty')
haftmann@24914
   825
    |> pair c'
haftmann@24836
   826
  end;
haftmann@24836
   827
haftmann@24218
   828
end;