src/Pure/Isar/class.ML
author wenzelm
Sun May 03 20:04:38 2015 +0200 (2015-05-03)
changeset 60249 09377954133b
parent 60246 1f9cd721ece2
child 60337 c7ca6bb006b0
permissions -rw-r--r--
tuned output to resemble input syntax more closely;
haftmann@38379
     1
(*  Title:      Pure/Isar/class.ML
haftmann@24218
     2
    Author:     Florian Haftmann, TU Muenchen
haftmann@24218
     3
haftmann@38379
     4
Type classes derived from primitive axclasses and locales.
haftmann@24218
     5
*)
haftmann@24218
     6
haftmann@38379
     7
signature CLASS =
haftmann@24218
     8
sig
haftmann@25462
     9
  (*classes*)
haftmann@29526
    10
  val is_class: theory -> class -> bool
haftmann@29526
    11
  val these_params: theory -> sort -> (string * (class * (string * typ))) list
haftmann@38379
    12
  val base_sort: theory -> class -> sort
haftmann@38379
    13
  val rules: theory -> class -> thm option * thm
haftmann@38379
    14
  val these_defs: theory -> sort -> thm list
haftmann@38379
    15
  val these_operations: theory -> sort
haftmann@38379
    16
    -> (string * (class * (typ * term))) list
wenzelm@42359
    17
  val print_classes: Proof.context -> unit
haftmann@25311
    18
  val init: class -> theory -> Proof.context
haftmann@38379
    19
  val begin: class list -> sort -> Proof.context -> Proof.context
haftmann@57067
    20
  val const: class -> (binding * mixfix) * term -> term list * term list -> local_theory -> local_theory
haftmann@57161
    21
  val abbrev: class -> Syntax.mode -> (binding * mixfix) * term -> term -> term list * term list -> local_theory -> local_theory
haftmann@38379
    22
  val redeclare_operations: theory -> sort -> Proof.context -> Proof.context
haftmann@29526
    23
  val class_prefix: string -> string
haftmann@38379
    24
  val register: class -> class list -> ((string * typ) * (string * typ)) list
haftmann@38379
    25
    -> sort -> morphism -> morphism -> thm option -> thm option -> thm
haftmann@38379
    26
    -> theory -> theory
haftmann@25485
    27
haftmann@25462
    28
  (*instances*)
haftmann@38379
    29
  val instantiation: string list * (string * sort) list * sort -> theory -> local_theory
haftmann@26247
    30
  val instantiation_instance: (local_theory -> local_theory)
haftmann@26247
    31
    -> local_theory -> Proof.state
haftmann@26247
    32
  val prove_instantiation_instance: (Proof.context -> tactic)
haftmann@26247
    33
    -> local_theory -> local_theory
haftmann@28666
    34
  val prove_instantiation_exit: (Proof.context -> tactic)
haftmann@28666
    35
    -> local_theory -> theory
haftmann@28666
    36
  val prove_instantiation_exit_result: (morphism -> 'a -> 'b)
haftmann@28666
    37
    -> (Proof.context -> 'b -> tactic) -> 'a -> local_theory -> 'b * theory
haftmann@31869
    38
  val read_multi_arity: theory -> xstring list * xstring list * xstring
haftmann@31869
    39
    -> string list * (string * sort) list * sort
haftmann@38348
    40
  val instantiation_cmd: xstring list * xstring list * xstring -> theory -> local_theory
haftmann@38377
    41
  val instance_arity_cmd: xstring list * xstring list * xstring -> theory -> Proof.state
haftmann@25485
    42
haftmann@31635
    43
  (*subclasses*)
haftmann@31635
    44
  val classrel: class * class -> theory -> Proof.state
haftmann@31635
    45
  val classrel_cmd: xstring * xstring -> theory -> Proof.state
haftmann@38379
    46
  val register_subclass: class * class -> morphism option -> Element.witness option
haftmann@54866
    47
    -> morphism -> local_theory -> local_theory
haftmann@31635
    48
haftmann@31635
    49
  (*tactics*)
wenzelm@59498
    50
  val intro_classes_tac: Proof.context -> thm list -> tactic
haftmann@29526
    51
  val default_intro_tac: Proof.context -> thm list -> tactic
haftmann@59296
    52
haftmann@59296
    53
  (*diagnostics*)
haftmann@59423
    54
  val pretty_specification: theory -> class -> Pretty.T list
haftmann@24218
    55
end;
haftmann@24218
    56
haftmann@38379
    57
structure Class: CLASS =
haftmann@24218
    58
struct
haftmann@24218
    59
haftmann@24589
    60
(** class data **)
haftmann@24218
    61
wenzelm@46919
    62
datatype class_data = Class_Data of {
haftmann@28715
    63
haftmann@28715
    64
  (* static part *)
haftmann@24218
    65
  consts: (string * string) list
haftmann@24836
    66
    (*locale parameter ~> constant name*),
haftmann@25062
    67
  base_sort: sort,
haftmann@29545
    68
  base_morph: morphism
haftmann@29439
    69
    (*static part of canonical morphism*),
haftmann@32850
    70
  export_morph: morphism,
haftmann@25618
    71
  assm_intro: thm option,
haftmann@25618
    72
  of_class: thm,
haftmann@25618
    73
  axiom: thm option,
wenzelm@42359
    74
haftmann@28715
    75
  (* dynamic part *)
haftmann@24657
    76
  defs: thm list,
haftmann@25368
    77
  operations: (string * (class * (typ * term))) list
haftmann@28715
    78
haftmann@48106
    79
  (* n.b.
haftmann@48106
    80
    params = logical parameters of class
haftmann@48106
    81
    operations = operations participating in user-space type system
haftmann@48106
    82
  *)
haftmann@24657
    83
};
haftmann@24218
    84
haftmann@32850
    85
fun make_class_data ((consts, base_sort, base_morph, export_morph, assm_intro, of_class, axiom),
haftmann@25368
    86
    (defs, operations)) =
wenzelm@46919
    87
  Class_Data {consts = consts, base_sort = base_sort,
haftmann@32850
    88
    base_morph = base_morph, export_morph = export_morph, assm_intro = assm_intro,
wenzelm@46919
    89
    of_class = of_class, axiom = axiom, defs = defs, operations = operations};
wenzelm@46919
    90
fun map_class_data f (Class_Data {consts, base_sort, base_morph, export_morph, assm_intro,
wenzelm@46919
    91
    of_class, axiom, defs, operations}) =
haftmann@32850
    92
  make_class_data (f ((consts, base_sort, base_morph, export_morph, assm_intro, of_class, axiom),
haftmann@25368
    93
    (defs, operations)));
wenzelm@46919
    94
fun merge_class_data _ (Class_Data {consts = consts,
haftmann@32850
    95
    base_sort = base_sort, base_morph = base_morph, export_morph = export_morph, assm_intro = assm_intro,
wenzelm@46919
    96
    of_class = of_class, axiom = axiom, defs = defs1, operations = operations1},
wenzelm@46919
    97
  Class_Data {consts = _, base_sort = _, base_morph = _, export_morph = _, assm_intro = _,
wenzelm@46919
    98
    of_class = _, axiom = _, defs = defs2, operations = operations2}) =
haftmann@32850
    99
  make_class_data ((consts, base_sort, base_morph, export_morph, assm_intro, of_class, axiom),
haftmann@24914
   100
    (Thm.merge_thms (defs1, defs2),
haftmann@25368
   101
      AList.merge (op =) (K true) (operations1, operations2)));
haftmann@24218
   102
wenzelm@46919
   103
structure Class_Data = Theory_Data
haftmann@24218
   104
(
haftmann@25038
   105
  type T = class_data Graph.T
haftmann@25038
   106
  val empty = Graph.empty;
haftmann@24218
   107
  val extend = I;
wenzelm@33522
   108
  val merge = Graph.join merge_class_data;
haftmann@24218
   109
);
haftmann@24218
   110
haftmann@24218
   111
haftmann@24218
   112
(* queries *)
haftmann@24218
   113
wenzelm@46919
   114
fun lookup_class_data thy class =
wenzelm@46919
   115
  (case try (Graph.get_node (Class_Data.get thy)) class of
wenzelm@46919
   116
    SOME (Class_Data data) => SOME data
wenzelm@46919
   117
  | NONE => NONE);
haftmann@24218
   118
wenzelm@46919
   119
fun the_class_data thy class =
wenzelm@46919
   120
  (case lookup_class_data thy class of
wenzelm@46919
   121
    NONE => error ("Undeclared class " ^ quote class)
wenzelm@46919
   122
  | SOME data => data);
haftmann@24218
   123
haftmann@25038
   124
val is_class = is_some oo lookup_class_data;
haftmann@25038
   125
wenzelm@46919
   126
val ancestry = Graph.all_succs o Class_Data.get;
wenzelm@46919
   127
val heritage = Graph.all_preds o Class_Data.get;
haftmann@29509
   128
haftmann@25002
   129
fun these_params thy =
haftmann@24218
   130
  let
haftmann@24218
   131
    fun params class =
haftmann@24218
   132
      let
wenzelm@51685
   133
        val const_typs = (#params o Axclass.get_info thy) class;
haftmann@24657
   134
        val const_names = (#consts o the_class_data thy) class;
haftmann@24218
   135
      in
haftmann@26518
   136
        (map o apsnd)
haftmann@26518
   137
          (fn c => (class, (c, (the o AList.lookup (op =) const_typs) c))) const_names
haftmann@24218
   138
      end;
haftmann@24218
   139
  in maps params o ancestry thy end;
haftmann@24218
   140
haftmann@29526
   141
val base_sort = #base_sort oo the_class_data;
haftmann@29526
   142
haftmann@29526
   143
fun rules thy class =
wenzelm@46919
   144
  let val {axiom, of_class, ...} = the_class_data thy class
haftmann@29526
   145
  in (axiom, of_class) end;
haftmann@29526
   146
haftmann@29526
   147
fun all_assm_intros thy =
wenzelm@46919
   148
  Graph.fold (fn (_, (Class_Data {assm_intro, ...}, _)) => fold (insert Thm.eq_thm)
wenzelm@46919
   149
    (the_list assm_intro)) (Class_Data.get thy) [];
haftmann@24218
   150
haftmann@29526
   151
fun these_defs thy = maps (#defs o the_class_data thy) o ancestry thy;
haftmann@29526
   152
fun these_operations thy = maps (#operations o the_class_data thy) o ancestry thy;
haftmann@29358
   153
haftmann@29526
   154
val base_morphism = #base_morph oo the_class_data;
wenzelm@47078
   155
wenzelm@46919
   156
fun morphism thy class =
wenzelm@46919
   157
  (case Element.eq_morphism thy (these_defs thy [class]) of
wenzelm@46919
   158
    SOME eq_morph => base_morphism thy class $> eq_morph
wenzelm@46919
   159
  | NONE => base_morphism thy class);
wenzelm@47078
   160
haftmann@32850
   161
val export_morphism = #export_morph oo the_class_data;
haftmann@28715
   162
wenzelm@42359
   163
fun print_classes ctxt =
haftmann@24218
   164
  let
wenzelm@42360
   165
    val thy = Proof_Context.theory_of ctxt;
haftmann@24218
   166
    val algebra = Sign.classes_of thy;
wenzelm@51510
   167
wenzelm@51510
   168
    val class_space = Proof_Context.class_space ctxt;
wenzelm@51510
   169
    val type_space = Proof_Context.type_space ctxt;
wenzelm@51510
   170
    val const_space = Proof_Context.const_space ctxt;
wenzelm@51510
   171
haftmann@24218
   172
    val arities =
haftmann@24218
   173
      Symtab.empty
haftmann@24218
   174
      |> Symtab.fold (fn (tyco, arities) => fold (fn (class, _) =>
haftmann@24218
   175
           Symtab.map_default (class, []) (insert (op =) tyco)) arities)
wenzelm@36328
   176
             (Sorts.arities_of algebra);
wenzelm@51510
   177
wenzelm@51510
   178
    fun prt_supersort class =
wenzelm@51510
   179
      Syntax.pretty_sort ctxt (Sign.minimize_sort thy (Sign.super_classes thy class));
wenzelm@51510
   180
wenzelm@51510
   181
    fun prt_arity class tyco =
haftmann@24218
   182
      let
haftmann@24218
   183
        val Ss = Sorts.mg_domain algebra tyco [class];
wenzelm@24920
   184
      in Syntax.pretty_arity ctxt (tyco, Ss, [class]) end;
wenzelm@51510
   185
wenzelm@51510
   186
    fun prt_param (c, ty) =
wenzelm@51510
   187
      Pretty.block
wenzelm@53539
   188
       [Name_Space.pretty ctxt const_space c, Pretty.str " ::",
wenzelm@51510
   189
        Pretty.brk 1, Syntax.pretty_typ ctxt (Type.strip_sorts_dummy ty)];
wenzelm@51510
   190
wenzelm@51510
   191
    fun prt_entry class =
wenzelm@51510
   192
      Pretty.block
wenzelm@55763
   193
        ([Pretty.keyword1 "class", Pretty.brk 1,
wenzelm@53539
   194
          Name_Space.pretty ctxt class_space class, Pretty.str ":", Pretty.fbrk,
wenzelm@53539
   195
          Pretty.block [Pretty.str "supersort: ", prt_supersort class]] @
wenzelm@51685
   196
          (case try (Axclass.get_info thy) class of
wenzelm@51510
   197
            NONE => []
wenzelm@51510
   198
          | SOME {params, ...} =>
wenzelm@51510
   199
              [Pretty.fbrk, Pretty.big_list "parameters:" (map prt_param params)]) @
wenzelm@51510
   200
          (case Symtab.lookup arities class of
wenzelm@51510
   201
            NONE => []
wenzelm@51510
   202
          | SOME ars =>
wenzelm@51510
   203
              [Pretty.fbrk, Pretty.big_list "instances:"
wenzelm@51510
   204
                (map (prt_arity class) (sort (Name_Space.extern_ord ctxt type_space) ars))]));
haftmann@24218
   205
  in
wenzelm@51510
   206
    Sorts.all_classes algebra
wenzelm@51510
   207
    |> sort (Name_Space.extern_ord ctxt class_space)
wenzelm@51510
   208
    |> map prt_entry
wenzelm@56334
   209
    |> Pretty.writeln_chunks2
haftmann@24218
   210
  end;
haftmann@24218
   211
haftmann@24218
   212
haftmann@24218
   213
(* updaters *)
haftmann@24218
   214
haftmann@32850
   215
fun register class sups params base_sort base_morph export_morph
haftmann@52636
   216
    some_axiom some_assm_intro of_class thy =
haftmann@25002
   217
  let
haftmann@25368
   218
    val operations = map (fn (v_ty as (_, ty), (c, _)) =>
haftmann@25683
   219
      (c, (class, (ty, Free v_ty)))) params;
haftmann@25038
   220
    val add_class = Graph.new_node (class,
wenzelm@59058
   221
        make_class_data (((map o apply2) fst params, base_sort,
haftmann@52636
   222
          base_morph, export_morph, some_assm_intro, of_class, some_axiom), ([], operations)))
haftmann@29526
   223
      #> fold (curry Graph.add_edge class) sups;
wenzelm@46919
   224
  in Class_Data.map add_class thy end;
haftmann@24218
   225
wenzelm@46919
   226
fun activate_defs class thms thy =
wenzelm@46919
   227
  (case Element.eq_morphism thy thms of
wenzelm@46919
   228
    SOME eq_morph => fold (fn cls => fn thy =>
ballarin@38107
   229
      Context.theory_map (Locale.amend_registration (cls, base_morphism thy cls)
ballarin@38107
   230
        (eq_morph, true) (export_morphism thy cls)) thy) (heritage thy [class]) thy
wenzelm@46919
   231
  | NONE => thy);
haftmann@29526
   232
haftmann@57173
   233
fun register_operation class (c, t) thy =
haftmann@25062
   234
  let
haftmann@29358
   235
    val base_sort = base_sort thy class;
haftmann@29439
   236
    val prep_typ = map_type_tfree
haftmann@29439
   237
      (fn (v, sort) => if Name.aT = v
haftmann@29439
   238
        then TFree (v, base_sort) else TVar ((v, 0), sort));
haftmann@25368
   239
    val t' = map_types prep_typ t;
haftmann@25368
   240
    val ty' = Term.fastype_of t';
haftmann@25062
   241
  in
haftmann@25062
   242
    thy
haftmann@57173
   243
    |> (Class_Data.map o Graph.map_node class o map_class_data o apsnd o apsnd)
haftmann@57173
   244
        (cons (c, (class, (ty', t'))))
haftmann@57173
   245
  end;
haftmann@57173
   246
haftmann@57173
   247
fun register_def class def_thm thy =
haftmann@57173
   248
  let
haftmann@57173
   249
    val sym_thm = Thm.symmetric def_thm
haftmann@57173
   250
  in
haftmann@57173
   251
    thy
haftmann@57173
   252
    |> (Class_Data.map o Graph.map_node class o map_class_data o apsnd o apfst)
haftmann@57173
   253
        (cons sym_thm)
haftmann@57173
   254
    |> activate_defs class [sym_thm]
haftmann@25062
   255
  end;
haftmann@24218
   256
haftmann@24218
   257
haftmann@24589
   258
(** classes and class target **)
haftmann@24218
   259
haftmann@25002
   260
(* class context syntax *)
haftmann@24748
   261
haftmann@35858
   262
fun these_unchecks thy = map (fn (c, (_, (ty, t))) => (t, Const (c, ty)))
haftmann@35858
   263
  o these_operations thy;
haftmann@29577
   264
haftmann@29632
   265
fun redeclare_const thy c =
wenzelm@30364
   266
  let val b = Long_Name.base_name c
haftmann@29632
   267
  in Sign.intern_const thy b = c ? Variable.declare_const (b, c) end;
haftmann@29632
   268
haftmann@29577
   269
fun synchronize_class_syntax sort base_sort ctxt =
haftmann@24914
   270
  let
wenzelm@42360
   271
    val thy = Proof_Context.theory_of ctxt;
haftmann@26596
   272
    val algebra = Sign.classes_of thy;
haftmann@29577
   273
    val operations = these_operations thy sort;
haftmann@26518
   274
    fun subst_class_typ sort = map_type_tfree (K (TVar ((Name.aT, 0), sort)));
haftmann@26518
   275
    val primary_constraints =
haftmann@25368
   276
      (map o apsnd) (subst_class_typ base_sort o fst o snd) operations;
haftmann@26518
   277
    val secondary_constraints =
haftmann@25368
   278
      (map o apsnd) (fn (class, (ty, _)) => subst_class_typ [class] ty) operations;
wenzelm@46919
   279
    fun improve (c, ty) =
wenzelm@46919
   280
      (case AList.lookup (op =) primary_constraints c of
wenzelm@46919
   281
        SOME ty' =>
wenzelm@46919
   282
          (case try (Type.raw_match (ty', ty)) Vartab.empty of
wenzelm@46919
   283
            SOME tyenv =>
wenzelm@46919
   284
              (case Vartab.lookup tyenv (Name.aT, 0) of
wenzelm@46919
   285
                SOME (_, ty' as TVar (vi, sort)) =>
wenzelm@46919
   286
                  if Type_Infer.is_param vi andalso Sorts.sort_le algebra (base_sort, sort)
wenzelm@46919
   287
                  then SOME (ty', TFree (Name.aT, base_sort))
wenzelm@46919
   288
                  else NONE
haftmann@26238
   289
              | _ => NONE)
haftmann@26238
   290
          | NONE => NONE)
wenzelm@46919
   291
      | NONE => NONE);
wenzelm@47005
   292
    fun subst (c, _) = Option.map snd (AList.lookup (op =) operations c);
haftmann@29577
   293
    val unchecks = these_unchecks thy sort;
haftmann@25083
   294
  in
haftmann@25083
   295
    ctxt
haftmann@29632
   296
    |> fold (redeclare_const thy o fst) primary_constraints
haftmann@26518
   297
    |> Overloading.map_improvable_syntax (K (((primary_constraints, secondary_constraints),
haftmann@26730
   298
        (((improve, subst), true), unchecks)), false))
haftmann@26518
   299
    |> Overloading.set_primary_constraints
haftmann@25083
   300
  end;
haftmann@25083
   301
haftmann@54866
   302
fun synchronize_class_syntax_target class lthy =
haftmann@54866
   303
  lthy
haftmann@54866
   304
  |> Local_Theory.map_contexts
haftmann@54866
   305
      (K (synchronize_class_syntax [class] (base_sort (Proof_Context.theory_of lthy) class)));
haftmann@54866
   306
haftmann@29632
   307
fun redeclare_operations thy sort =
haftmann@29632
   308
  fold (redeclare_const thy o fst) (these_operations thy sort);
haftmann@29632
   309
haftmann@29577
   310
fun begin sort base_sort ctxt =
haftmann@25083
   311
  ctxt
haftmann@25083
   312
  |> Variable.declare_term
haftmann@25083
   313
      (Logic.mk_type (TFree (Name.aT, base_sort)))
haftmann@29577
   314
  |> synchronize_class_syntax sort base_sort
haftmann@39378
   315
  |> Overloading.activate_improvable_syntax;
haftmann@24901
   316
haftmann@25311
   317
fun init class thy =
haftmann@25311
   318
  thy
haftmann@29509
   319
  |> Locale.init class
haftmann@29358
   320
  |> begin [class] (base_sort thy class);
haftmann@24914
   321
haftmann@24748
   322
haftmann@27690
   323
(* class target *)
haftmann@27690
   324
wenzelm@30364
   325
val class_prefix = Logic.const_of_class o Long_Name.base_name;
haftmann@29526
   326
wenzelm@47078
   327
local
wenzelm@47078
   328
haftmann@57187
   329
fun guess_morphism_identity (b, rhs) phi1 phi2 =
haftmann@57187
   330
  let
haftmann@57187
   331
    (*FIXME proper concept to identify morphism instead of educated guess*)
wenzelm@58668
   332
    val name_of_binding = Name_Space.full_name Name_Space.global_naming;
haftmann@57187
   333
    val n1 = (name_of_binding o Morphism.binding phi1) b;
haftmann@57187
   334
    val n2 = (name_of_binding o Morphism.binding phi2) b;
haftmann@57187
   335
    val rhs1 = Morphism.term phi1 rhs;
haftmann@57187
   336
    val rhs2 = Morphism.term phi2 rhs;
haftmann@57187
   337
  in n1 = n2 andalso Term.aconv_untyped (rhs1, rhs2) end;
haftmann@57187
   338
haftmann@57192
   339
fun target_const class phi0 prmode ((b, _), rhs) =
haftmann@57189
   340
  let
haftmann@57189
   341
    val guess_identity = guess_morphism_identity (b, rhs) Morphism.identity;
haftmann@57189
   342
    val guess_canonical = guess_morphism_identity (b, rhs) phi0;
haftmann@57189
   343
  in
haftmann@57192
   344
    Generic_Target.locale_target_const class
haftmann@57189
   345
      (not o (guess_identity orf guess_canonical)) prmode ((b, NoSyn), rhs)
haftmann@57189
   346
  end;
haftmann@57072
   347
haftmann@57119
   348
fun dangling_params_for lthy class (type_params, term_params) =
haftmann@27690
   349
  let
haftmann@57119
   350
    val class_param_names =
haftmann@57119
   351
      map fst (these_params (Proof_Context.theory_of lthy) [class]);
haftmann@57119
   352
    val dangling_term_params =
haftmann@57119
   353
      subtract (fn (v, Free (w, _)) => v = w | _ => false) class_param_names term_params;
haftmann@57161
   354
  in (type_params, dangling_term_params) end;
haftmann@57119
   355
haftmann@57119
   356
fun global_def (b, eq) thy =
haftmann@57119
   357
  thy
haftmann@57119
   358
  |> Thm.add_def_global false false (b, eq)
haftmann@57119
   359
  |>> (Thm.varifyT_global o snd)
haftmann@57119
   360
  |-> (fn def_thm => Global_Theory.store_thm (b, def_thm)
haftmann@57119
   361
      #> snd
haftmann@57119
   362
      #> pair def_thm);
haftmann@57119
   363
haftmann@57142
   364
fun canonical_const class phi dangling_params ((b, mx), rhs) thy =
haftmann@57119
   365
  let
haftmann@57147
   366
    val b_def = Binding.suffix_name "_dict" b;
haftmann@57147
   367
    val c = Sign.full_name thy b;
haftmann@57147
   368
    val ty = map Term.fastype_of dangling_params ---> Term.fastype_of rhs;
haftmann@57147
   369
    val def_eq = Logic.mk_equals (list_comb (Const (c, ty), dangling_params), rhs)
haftmann@29577
   370
      |> map_types Type.strip_sorts;
haftmann@27690
   371
  in
haftmann@29577
   372
    thy
haftmann@57147
   373
    |> Sign.declare_const_global ((b, Type.strip_sorts ty), mx)
haftmann@29577
   374
    |> snd
haftmann@57119
   375
    |> global_def (b_def, def_eq)
haftmann@57173
   376
    |-> (fn def_thm => register_def class def_thm)
haftmann@57173
   377
    |> null dangling_params ? register_operation class (c, rhs)
haftmann@57147
   378
    |> Sign.add_const_constraint (c, SOME ty)
haftmann@27690
   379
  end;
haftmann@27690
   380
haftmann@57161
   381
fun canonical_abbrev class phi prmode dangling_term_params ((b, mx), rhs) thy =
haftmann@27690
   382
  let
haftmann@29577
   383
    val unchecks = these_unchecks thy [class];
haftmann@57119
   384
    val rhs' = Pattern.rewrite_term thy unchecks [] rhs;
haftmann@57161
   385
    val c' = Sign.full_name thy b;
haftmann@57161
   386
    val ty' = map Term.fastype_of dangling_term_params ---> Term.fastype_of rhs';
haftmann@59911
   387
    val ty'' = Morphism.typ phi ty';
haftmann@59911
   388
    val rhs'' = map_types (Morphism.typ phi) (fold lambda dangling_term_params rhs');
haftmann@27690
   389
  in
haftmann@29577
   390
    thy
haftmann@59911
   391
    |> Sign.add_abbrev (#1 prmode) (b, Logic.varify_types_global rhs'')
haftmann@29577
   392
    |> snd
haftmann@59911
   393
    |> Sign.notation true prmode [(Const (c', ty''), mx)]
haftmann@57161
   394
    |> (null dangling_term_params andalso not (#1 prmode = Print_Mode.input))
haftmann@57173
   395
      ? register_operation class (c', rhs')
haftmann@57069
   396
    |> Sign.add_const_constraint (c', SOME ty')
haftmann@27690
   397
  end;
haftmann@27690
   398
haftmann@38619
   399
in
haftmann@38619
   400
haftmann@57119
   401
fun const class ((b, mx), lhs) params lthy =
haftmann@57119
   402
  let
haftmann@57141
   403
    val phi = morphism (Proof_Context.theory_of lthy) class;
haftmann@57161
   404
    val dangling_params = map (Morphism.term phi) (uncurry append (dangling_params_for lthy class params));
haftmann@57119
   405
  in
haftmann@57119
   406
    lthy
haftmann@57192
   407
    |> target_const class phi Syntax.mode_default ((b, mx), lhs)
haftmann@57148
   408
    |> Local_Theory.raw_theory (canonical_const class phi dangling_params
haftmann@57148
   409
         ((Morphism.binding phi b, if null dangling_params then mx else NoSyn), Morphism.term phi lhs))
haftmann@57192
   410
    |> Generic_Target.standard_const (fn (this, other) => other <> 0 andalso this <> other)
haftmann@57148
   411
         Syntax.mode_default ((b, if null dangling_params then NoSyn else mx), lhs)
haftmann@57141
   412
    |> synchronize_class_syntax_target class
haftmann@57119
   413
  end;
haftmann@57072
   414
haftmann@57161
   415
fun abbrev class prmode ((b, mx), lhs) rhs' params lthy =
haftmann@57141
   416
  let
haftmann@57141
   417
    val phi = morphism (Proof_Context.theory_of lthy) class;
haftmann@57161
   418
    val dangling_term_params = map (Morphism.term phi) (snd (dangling_params_for lthy class params));
haftmann@57141
   419
  in
haftmann@57141
   420
    lthy
haftmann@57192
   421
    |> target_const class phi prmode ((b, mx), lhs)
haftmann@57161
   422
    |> Local_Theory.raw_theory (canonical_abbrev class phi prmode dangling_term_params
haftmann@57161
   423
         ((Morphism.binding phi b, if null dangling_term_params then mx else NoSyn), rhs'))
haftmann@57192
   424
    |> Generic_Target.standard_const (fn (this, other) => other <> 0 andalso this <> other)
haftmann@57161
   425
         prmode ((b, if null dangling_term_params then NoSyn else mx), lhs)
haftmann@57141
   426
    |> synchronize_class_syntax_target class
haftmann@57141
   427
  end;
haftmann@38619
   428
haftmann@38619
   429
end;
haftmann@38619
   430
haftmann@27690
   431
haftmann@54866
   432
(* subclasses *)
haftmann@54866
   433
haftmann@54866
   434
fun register_subclass (sub, sup) some_dep_morph some_witn export lthy =
haftmann@54866
   435
  let
haftmann@54866
   436
    val thy = Proof_Context.theory_of lthy;
haftmann@54866
   437
    val intros = (snd o rules thy) sup :: map_filter I
wenzelm@54883
   438
      [Option.map (Drule.export_without_context_open o Element.conclude_witness lthy) some_witn,
haftmann@54866
   439
        (fst o rules thy) sub];
haftmann@54866
   440
    val classrel =
haftmann@54866
   441
      Goal.prove_sorry_global thy [] [] (Logic.mk_classrel (sub, sup))
wenzelm@59498
   442
        (fn {context = ctxt, ...} => EVERY (map (TRYALL o resolve_tac ctxt o single) intros));
haftmann@54866
   443
    val diff_sort = Sign.complete_sort thy [sup]
haftmann@54866
   444
      |> subtract (op =) (Sign.complete_sort thy [sub])
haftmann@54866
   445
      |> filter (is_class thy);
haftmann@54866
   446
    fun add_dependency some_wit = case some_dep_morph of
haftmann@54866
   447
        SOME dep_morph => Generic_Target.locale_dependency sub
haftmann@54866
   448
          (sup, dep_morph $> Element.satisfy_morphism (the_list some_witn)) NONE export
haftmann@54866
   449
      | NONE => I;
haftmann@54866
   450
  in
haftmann@54866
   451
    lthy
haftmann@54866
   452
    |> Local_Theory.raw_theory
haftmann@54866
   453
      (Axclass.add_classrel classrel
haftmann@54866
   454
      #> Class_Data.map (Graph.add_edge (sub, sup))
haftmann@54866
   455
      #> activate_defs sub (these_defs thy diff_sort))
haftmann@54866
   456
    |> add_dependency some_witn
haftmann@54866
   457
    |> synchronize_class_syntax_target sub
haftmann@54866
   458
  end;
haftmann@31635
   459
haftmann@31635
   460
local
haftmann@31635
   461
haftmann@31635
   462
fun gen_classrel mk_prop classrel thy =
haftmann@31635
   463
  let
haftmann@31635
   464
    fun after_qed results =
wenzelm@51685
   465
      Proof_Context.background_theory ((fold o fold) Axclass.add_classrel results);
haftmann@31635
   466
  in
haftmann@31635
   467
    thy
wenzelm@42360
   468
    |> Proof_Context.init_global
wenzelm@36323
   469
    |> Proof.theorem NONE after_qed [[(mk_prop thy classrel, [])]]
haftmann@31635
   470
  end;
haftmann@31635
   471
haftmann@31635
   472
in
haftmann@31635
   473
haftmann@31635
   474
val classrel =
wenzelm@51685
   475
  gen_classrel (Logic.mk_classrel oo Axclass.cert_classrel);
haftmann@31635
   476
val classrel_cmd =
wenzelm@51685
   477
  gen_classrel (Logic.mk_classrel oo Axclass.read_classrel);
haftmann@31635
   478
haftmann@31635
   479
end; (*local*)
haftmann@31635
   480
haftmann@31635
   481
haftmann@25462
   482
(** instantiation target **)
haftmann@25462
   483
haftmann@25462
   484
(* bookkeeping *)
haftmann@25462
   485
haftmann@25462
   486
datatype instantiation = Instantiation of {
haftmann@25864
   487
  arities: string list * (string * sort) list * sort,
haftmann@25462
   488
  params: ((string * string) * (string * typ)) list
haftmann@25603
   489
    (*(instantiation parameter, type constructor), (local instantiation parameter, typ)*)
haftmann@25462
   490
}
haftmann@25462
   491
wenzelm@59150
   492
fun make_instantiation (arities, params) =
wenzelm@59150
   493
  Instantiation {arities = arities, params = params};
wenzelm@59150
   494
wenzelm@59150
   495
val empty_instantiation = make_instantiation (([], [], []), []);
wenzelm@59150
   496
wenzelm@33519
   497
structure Instantiation = Proof_Data
haftmann@25462
   498
(
wenzelm@46919
   499
  type T = instantiation;
wenzelm@59150
   500
  fun init _ = empty_instantiation;
haftmann@25462
   501
);
haftmann@25462
   502
wenzelm@46919
   503
val get_instantiation =
wenzelm@46919
   504
  (fn Instantiation data => data) o Instantiation.get o Local_Theory.target_of;
haftmann@25462
   505
wenzelm@46919
   506
fun map_instantiation f =
wenzelm@46919
   507
  (Local_Theory.target o Instantiation.map)
wenzelm@59150
   508
    (fn Instantiation {arities, params} => make_instantiation (f (arities, params)));
wenzelm@46919
   509
wenzelm@46919
   510
fun the_instantiation lthy =
wenzelm@46919
   511
  (case get_instantiation lthy of
wenzelm@46919
   512
    {arities = ([], [], []), ...} => error "No instantiation target"
wenzelm@46919
   513
  | data => data);
haftmann@25462
   514
haftmann@25485
   515
val instantiation_params = #params o get_instantiation;
haftmann@25462
   516
haftmann@30519
   517
fun instantiation_param lthy b = instantiation_params lthy
haftmann@30519
   518
  |> find_first (fn (_, (v, _)) => Binding.name_of b = v)
haftmann@25462
   519
  |> Option.map (fst o fst);
haftmann@25462
   520
haftmann@31869
   521
fun read_multi_arity thy (raw_tycos, raw_sorts, raw_sort) =
haftmann@31869
   522
  let
wenzelm@42360
   523
    val ctxt = Proof_Context.init_global thy;
wenzelm@42360
   524
    val all_arities = map (fn raw_tyco => Proof_Context.read_arity ctxt
haftmann@31869
   525
      (raw_tyco, raw_sorts, raw_sort)) raw_tycos;
haftmann@31869
   526
    val tycos = map #1 all_arities;
haftmann@31869
   527
    val (_, sorts, sort) = hd all_arities;
wenzelm@43329
   528
    val vs = Name.invent_names Name.context Name.aT sorts;
haftmann@31869
   529
  in (tycos, vs, sort) end;
haftmann@31869
   530
haftmann@25462
   531
haftmann@25462
   532
(* syntax *)
haftmann@25462
   533
haftmann@26238
   534
fun synchronize_inst_syntax ctxt =
haftmann@25462
   535
  let
wenzelm@46919
   536
    val Instantiation {params, ...} = Instantiation.get ctxt;
haftmann@31249
   537
wenzelm@51685
   538
    val lookup_inst_param = Axclass.lookup_inst_param
wenzelm@42360
   539
      (Sign.consts_of (Proof_Context.theory_of ctxt)) params;
wenzelm@46919
   540
    fun subst (c, ty) =
wenzelm@46919
   541
      (case lookup_inst_param (c, ty) of
wenzelm@46919
   542
        SOME (v_ty as (_, ty)) => SOME (ty, Free v_ty)
wenzelm@46919
   543
      | NONE => NONE);
haftmann@26238
   544
    val unchecks =
haftmann@26238
   545
      map (fn ((c, _), v_ty as (_, ty)) => (Free v_ty, Const (c, ty))) params;
haftmann@26238
   546
  in
haftmann@26238
   547
    ctxt
haftmann@26259
   548
    |> Overloading.map_improvable_syntax
wenzelm@46919
   549
      (fn (((primary_constraints, _), (((improve, _), _), _)), _) =>
wenzelm@46919
   550
          (((primary_constraints, []), (((improve, subst), false), unchecks)), false))
haftmann@26238
   551
  end;
haftmann@25462
   552
wenzelm@42385
   553
fun resort_terms ctxt algebra consts constraints ts =
haftmann@38382
   554
  let
wenzelm@42385
   555
    fun matchings (Const (c_ty as (c, _))) =
wenzelm@42385
   556
          (case constraints c of
wenzelm@42385
   557
            NONE => I
wenzelm@42385
   558
          | SOME sorts =>
wenzelm@42385
   559
              fold2 (curry (Sorts.meet_sort algebra)) (Consts.typargs consts c_ty) sorts)
wenzelm@42385
   560
      | matchings _ = I;
haftmann@38382
   561
    val tvartab = (fold o fold_aterms) matchings ts Vartab.empty
wenzelm@47005
   562
      handle Sorts.CLASS_ERROR e => error (Sorts.class_error (Context.pretty ctxt) e);
haftmann@38382
   563
    val inst = map_type_tvar
haftmann@38382
   564
      (fn (vi, sort) => TVar (vi, the_default sort (Vartab.lookup tvartab vi)));
wenzelm@42402
   565
  in if Vartab.is_empty tvartab then ts else (map o map_types) inst ts end;
haftmann@38382
   566
haftmann@25462
   567
haftmann@25462
   568
(* target *)
haftmann@25462
   569
wenzelm@47078
   570
fun define_overloaded (c, U) v (b_def, rhs) =
wenzelm@51685
   571
  Local_Theory.background_theory_result (Axclass.declare_overloaded (c, U)
wenzelm@51685
   572
  ##>> Axclass.define_overloaded b_def (c, rhs))
haftmann@38382
   573
  ##> (map_instantiation o apsnd) (filter_out (fn (_, (v', _)) => v' = v))
wenzelm@47245
   574
  ##> Local_Theory.map_contexts (K synchronize_inst_syntax);
haftmann@38382
   575
wenzelm@47289
   576
fun foundation (((b, U), mx), (b_def, rhs)) params lthy =
wenzelm@46919
   577
  (case instantiation_param lthy b of
wenzelm@46919
   578
    SOME c =>
wenzelm@46919
   579
      if mx <> NoSyn then error ("Illegal mixfix syntax for overloaded constant " ^ quote c)
wenzelm@46919
   580
      else lthy |> define_overloaded (c, U) (Binding.name_of b) (b_def, rhs)
wenzelm@47289
   581
  | NONE => lthy |> Generic_Target.theory_foundation (((b, U), mx), (b_def, rhs)) params);
haftmann@38382
   582
haftmann@38382
   583
fun pretty lthy =
haftmann@26518
   584
  let
wenzelm@46919
   585
    val {arities = (tycos, vs, sort), params} = the_instantiation lthy;
haftmann@38382
   586
    fun pr_arity tyco = Syntax.pretty_arity lthy (tyco, map snd vs, sort);
haftmann@38382
   587
    fun pr_param ((c, _), (v, ty)) =
wenzelm@42359
   588
      Pretty.block (Pretty.breaks
wenzelm@55304
   589
        [Pretty.str v, Pretty.str "==", Proof_Context.pretty_const lthy c,
wenzelm@42359
   590
          Pretty.str "::", Syntax.pretty_typ lthy ty]);
wenzelm@60246
   591
  in
wenzelm@60246
   592
    [Pretty.block
wenzelm@60246
   593
      (Pretty.fbreaks (Pretty.keyword1 "instantiation" :: map pr_arity tycos @ map pr_param params))]
wenzelm@60246
   594
  end;
haftmann@26518
   595
haftmann@38382
   596
fun conclude lthy =
haftmann@38382
   597
  let
wenzelm@42359
   598
    val (tycos, vs, sort) = #arities (the_instantiation lthy);
wenzelm@42360
   599
    val thy = Proof_Context.theory_of lthy;
wenzelm@42359
   600
    val _ = tycos |> List.app (fn tyco =>
wenzelm@46919
   601
      if Sign.of_sort thy (Type (tyco, map TFree vs), sort) then ()
wenzelm@55304
   602
      else error ("Missing instance proof for type " ^ quote (Proof_Context.markup_type lthy tyco)));
haftmann@38382
   603
  in lthy end;
haftmann@38382
   604
haftmann@38382
   605
fun instantiation (tycos, vs, sort) thy =
haftmann@25462
   606
  let
haftmann@25536
   607
    val _ = if null tycos then error "At least one arity must be given" else ();
wenzelm@51685
   608
    val class_params = these_params thy (filter (can (Axclass.get_info thy)) sort);
haftmann@29632
   609
    fun get_param tyco (param, (_, (c, ty))) =
wenzelm@51685
   610
      if can (Axclass.param_of_inst thy) (c, tyco)
haftmann@25603
   611
      then NONE else SOME ((c, tyco),
wenzelm@46917
   612
        (param ^ "_" ^ Long_Name.base_name tyco, map_atyps (K (Type (tyco, map TFree vs))) ty));
haftmann@31249
   613
    val params = map_product get_param tycos class_params |> map_filter I;
haftmann@51578
   614
    val _ = if null params andalso forall (fn tyco => can (Sign.arity_sorts thy tyco) sort) tycos
haftmann@51578
   615
      then error "No parameters and no pending instance proof obligations in instantiation."
haftmann@51578
   616
      else ();
haftmann@26518
   617
    val primary_constraints = map (apsnd
haftmann@31249
   618
      (map_atyps (K (TVar ((Name.aT, 0), [])))) o snd o snd) class_params;
haftmann@26518
   619
    val algebra = Sign.classes_of thy
wenzelm@47005
   620
      |> fold (fn tyco => Sorts.add_arities (Context.pretty_global thy)
haftmann@26518
   621
            (tyco, map (fn class => (class, map snd vs)) sort)) tycos;
haftmann@26518
   622
    val consts = Sign.consts_of thy;
haftmann@26518
   623
    val improve_constraints = AList.lookup (op =)
haftmann@31249
   624
      (map (fn (_, (class, (c, _))) => (c, [[class]])) class_params);
wenzelm@42402
   625
    fun resort_check ctxt ts = resort_terms ctxt algebra consts improve_constraints ts;
wenzelm@51685
   626
    val lookup_inst_param = Axclass.lookup_inst_param consts params;
wenzelm@42388
   627
    fun improve (c, ty) =
wenzelm@42388
   628
      (case lookup_inst_param (c, ty) of
wenzelm@42388
   629
        SOME (_, ty') => if Sign.typ_instance thy (ty', ty) then SOME (ty, ty') else NONE
wenzelm@42388
   630
      | NONE => NONE);
haftmann@25485
   631
  in
haftmann@25485
   632
    thy
wenzelm@56056
   633
    |> Sign.change_begin
wenzelm@42360
   634
    |> Proof_Context.init_global
wenzelm@59150
   635
    |> Instantiation.put (make_instantiation ((tycos, vs, sort), params))
wenzelm@27281
   636
    |> fold (Variable.declare_typ o TFree) vs
haftmann@31249
   637
    |> fold (Variable.declare_names o Free o snd) params
haftmann@26259
   638
    |> (Overloading.map_improvable_syntax o apfst)
haftmann@29969
   639
         (K ((primary_constraints, []), (((improve, K NONE), false), [])))
haftmann@39378
   640
    |> Overloading.activate_improvable_syntax
wenzelm@45429
   641
    |> Context.proof_map (Syntax_Phases.term_check 0 "resorting" resort_check)
haftmann@26238
   642
    |> synchronize_inst_syntax
wenzelm@59876
   643
    |> Local_Theory.init (Sign.naming_of thy)
haftmann@38382
   644
       {define = Generic_Target.define foundation,
wenzelm@47250
   645
        notes = Generic_Target.notes Generic_Target.theory_notes,
wenzelm@47286
   646
        abbrev = Generic_Target.abbrev Generic_Target.theory_abbrev,
wenzelm@47279
   647
        declaration = K Generic_Target.theory_declaration,
haftmann@56723
   648
        subscription = Generic_Target.theory_registration,
haftmann@38382
   649
        pretty = pretty,
wenzelm@56056
   650
        exit = conclude #> Local_Theory.target_of #> Sign.change_end_local}
haftmann@25485
   651
  end;
haftmann@25485
   652
haftmann@38382
   653
fun instantiation_cmd arities thy =
haftmann@38382
   654
  instantiation (read_multi_arity thy arities) thy;
haftmann@26238
   655
haftmann@25485
   656
fun gen_instantiation_instance do_proof after_qed lthy =
haftmann@25485
   657
  let
haftmann@25864
   658
    val (tycos, vs, sort) = (#arities o the_instantiation) lthy;
haftmann@25864
   659
    val arities_proof = maps (fn tyco => Logic.mk_arities (tyco, map snd vs, sort)) tycos;
haftmann@25462
   660
    fun after_qed' results =
wenzelm@51685
   661
      Local_Theory.background_theory (fold (Axclass.add_arity o Thm.varifyT_global) results)
haftmann@25462
   662
      #> after_qed;
haftmann@25462
   663
  in
haftmann@25462
   664
    lthy
haftmann@25462
   665
    |> do_proof after_qed' arities_proof
haftmann@25462
   666
  end;
haftmann@25462
   667
haftmann@25485
   668
val instantiation_instance = gen_instantiation_instance (fn after_qed => fn ts =>
wenzelm@36323
   669
  Proof.theorem NONE (after_qed o map the_single) (map (fn t => [(t, [])]) ts));
haftmann@25462
   670
haftmann@25485
   671
fun prove_instantiation_instance tac = gen_instantiation_instance (fn after_qed =>
haftmann@25502
   672
  fn ts => fn lthy => after_qed (map (fn t => Goal.prove lthy [] [] t
haftmann@25502
   673
    (fn {context, ...} => tac context)) ts) lthy) I;
haftmann@25462
   674
haftmann@28666
   675
fun prove_instantiation_exit tac = prove_instantiation_instance tac
wenzelm@33671
   676
  #> Local_Theory.exit_global;
haftmann@28666
   677
haftmann@28666
   678
fun prove_instantiation_exit_result f tac x lthy =
haftmann@28666
   679
  let
wenzelm@42360
   680
    val morph = Proof_Context.export_morphism lthy
wenzelm@42360
   681
      (Proof_Context.init_global (Proof_Context.theory_of lthy));
haftmann@29439
   682
    val y = f morph x;
haftmann@28666
   683
  in
haftmann@28666
   684
    lthy
haftmann@28666
   685
    |> prove_instantiation_exit (fn ctxt => tac ctxt y)
haftmann@28666
   686
    |> pair y
haftmann@28666
   687
  end;
haftmann@28666
   688
haftmann@29526
   689
haftmann@31635
   690
(* simplified instantiation interface with no class parameter *)
haftmann@31635
   691
haftmann@31869
   692
fun instance_arity_cmd raw_arities thy =
haftmann@31635
   693
  let
haftmann@31869
   694
    val (tycos, vs, sort) = read_multi_arity thy raw_arities;
haftmann@31869
   695
    val sorts = map snd vs;
haftmann@31869
   696
    val arities = maps (fn tyco => Logic.mk_arities (tyco, sorts, sort)) tycos;
wenzelm@47078
   697
    fun after_qed results =
wenzelm@51685
   698
      Proof_Context.background_theory ((fold o fold) Axclass.add_arity results);
haftmann@31635
   699
  in
haftmann@31635
   700
    thy
wenzelm@42360
   701
    |> Proof_Context.init_global
wenzelm@36323
   702
    |> Proof.theorem NONE after_qed (map (fn t => [(t, [])]) arities)
haftmann@31635
   703
  end;
haftmann@31635
   704
haftmann@31635
   705
haftmann@29526
   706
(** tactics and methods **)
haftmann@29526
   707
wenzelm@59498
   708
fun intro_classes_tac ctxt facts st =
haftmann@29526
   709
  let
haftmann@29526
   710
    val thy = Thm.theory_of_thm st;
haftmann@29526
   711
    val classes = Sign.all_classes thy;
haftmann@29526
   712
    val class_trivs = map (Thm.class_triv thy) classes;
wenzelm@51685
   713
    val class_intros = map_filter (try (#intro o Axclass.get_info thy)) classes;
haftmann@29526
   714
    val assm_intros = all_assm_intros thy;
haftmann@29526
   715
  in
wenzelm@59498
   716
    Method.intros_tac ctxt (class_trivs @ class_intros @ assm_intros) facts st
haftmann@29526
   717
  end;
haftmann@29526
   718
haftmann@29526
   719
fun default_intro_tac ctxt [] =
wenzelm@39438
   720
      COND Thm.no_prems no_tac
wenzelm@59498
   721
        (intro_classes_tac ctxt [] ORELSE Locale.intro_locales_tac true ctxt [])
haftmann@29526
   722
  | default_intro_tac _ _ = no_tac;
haftmann@29526
   723
haftmann@29526
   724
fun default_tac rules ctxt facts =
wenzelm@54742
   725
  HEADGOAL (Method.some_rule_tac ctxt rules facts) ORELSE
haftmann@29526
   726
    default_intro_tac ctxt facts;
haftmann@29526
   727
wenzelm@53171
   728
val _ = Theory.setup
wenzelm@59498
   729
 (Method.setup @{binding intro_classes} (Scan.succeed (METHOD o intro_classes_tac))
wenzelm@30515
   730
    "back-chain introduction rules of classes" #>
wenzelm@56204
   731
  Method.setup @{binding default} (Attrib.thms >> (METHOD oo default_tac))
wenzelm@53171
   732
    "apply some intro/elim rule");
haftmann@29526
   733
haftmann@59296
   734
wenzelm@60249
   735
haftmann@59296
   736
(** diagnostics **)
haftmann@59296
   737
wenzelm@60249
   738
fun pretty_specification thy class =
wenzelm@60249
   739
  if is_class thy class then
wenzelm@59383
   740
    let
wenzelm@60249
   741
      val class_ctxt = init class thy;
wenzelm@60249
   742
      val prt_class = Name_Space.pretty class_ctxt (Proof_Context.class_space class_ctxt);
wenzelm@60249
   743
wenzelm@60249
   744
      val super_classes = Sign.minimize_sort thy (Sign.super_classes thy class);
wenzelm@59383
   745
wenzelm@59383
   746
      val fix_args =
wenzelm@60249
   747
        #params (Axclass.get_info thy class)
wenzelm@59383
   748
        |> map (fn (c, T) => (Binding.name (Long_Name.base_name c), SOME T, NoSyn));
wenzelm@59383
   749
      val fixes = if null fix_args then [] else [Element.Fixes fix_args];
wenzelm@60249
   750
      val assumes = Locale.hyp_spec_of thy class;
wenzelm@59383
   751
wenzelm@59383
   752
      val header =
wenzelm@60249
   753
        [Pretty.keyword1 "class", Pretty.brk 1, prt_class class, Pretty.str " =", Pretty.brk 1] @
wenzelm@60249
   754
        Pretty.separate " +" (map prt_class super_classes) @
wenzelm@60249
   755
        (if null super_classes then [] else [Pretty.str " +"]);
wenzelm@59383
   756
      val body =
wenzelm@59385
   757
        if null fixes andalso null assumes then []
wenzelm@59385
   758
        else
wenzelm@59385
   759
          maps (Element.pretty_ctxt_no_attribs class_ctxt) (fixes @ assumes)
wenzelm@59385
   760
          |> maps (fn prt => [Pretty.fbrk, prt]);
haftmann@59423
   761
    in if null body then [] else [Pretty.block (header @ body)] end
haftmann@59423
   762
  else [];
haftmann@59296
   763
haftmann@24218
   764
end;