src/Pure/Isar/class.ML
author wenzelm
Wed Jun 22 16:04:03 2016 +0200 (2016-06-22 ago)
changeset 63347 e344dc82f6c2
parent 62939 ef8d840f39fb
child 66337 a849ce33923d
permissions -rw-r--r--
report class parameters within instantiation;
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
wenzelm@63347
    15
  val these_operations: theory -> sort -> (string * (class * ((typ * term) * bool))) list
wenzelm@42359
    16
  val print_classes: Proof.context -> unit
haftmann@25311
    17
  val init: class -> theory -> Proof.context
haftmann@38379
    18
  val begin: class list -> sort -> Proof.context -> Proof.context
wenzelm@63347
    19
  val const: class -> (binding * mixfix) * term -> term list * term list ->
wenzelm@63347
    20
    local_theory -> local_theory
wenzelm@63347
    21
  val abbrev: class -> Syntax.mode -> (binding * mixfix) * term -> local_theory ->
wenzelm@63347
    22
    (term * term) * local_theory
haftmann@38379
    23
  val redeclare_operations: theory -> sort -> Proof.context -> Proof.context
haftmann@29526
    24
  val class_prefix: string -> string
wenzelm@63347
    25
  val register: class -> class list -> ((string * typ) * (string * typ)) list ->
wenzelm@63347
    26
    sort -> morphism -> morphism -> thm option -> thm option -> thm -> 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
wenzelm@60618
    51
  val standard_intro_classes_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@60348
    77
  operations: (string * (class * ((typ * term) * bool))) 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@63347
   163
fun pretty_param ctxt (c, ty) =
wenzelm@63347
   164
  Pretty.block
wenzelm@63347
   165
   [Name_Space.pretty ctxt (Proof_Context.const_space ctxt) c, Pretty.str " ::",
wenzelm@63347
   166
    Pretty.brk 1, Syntax.pretty_typ ctxt ty];
wenzelm@63347
   167
wenzelm@42359
   168
fun print_classes ctxt =
haftmann@24218
   169
  let
wenzelm@42360
   170
    val thy = Proof_Context.theory_of ctxt;
haftmann@24218
   171
    val algebra = Sign.classes_of thy;
wenzelm@51510
   172
wenzelm@51510
   173
    val class_space = Proof_Context.class_space ctxt;
wenzelm@51510
   174
    val type_space = Proof_Context.type_space ctxt;
wenzelm@51510
   175
haftmann@24218
   176
    val arities =
haftmann@24218
   177
      Symtab.empty
haftmann@24218
   178
      |> Symtab.fold (fn (tyco, arities) => fold (fn (class, _) =>
haftmann@24218
   179
           Symtab.map_default (class, []) (insert (op =) tyco)) arities)
wenzelm@36328
   180
             (Sorts.arities_of algebra);
wenzelm@51510
   181
wenzelm@51510
   182
    fun prt_supersort class =
wenzelm@51510
   183
      Syntax.pretty_sort ctxt (Sign.minimize_sort thy (Sign.super_classes thy class));
wenzelm@51510
   184
wenzelm@51510
   185
    fun prt_arity class tyco =
haftmann@24218
   186
      let
haftmann@24218
   187
        val Ss = Sorts.mg_domain algebra tyco [class];
wenzelm@24920
   188
      in Syntax.pretty_arity ctxt (tyco, Ss, [class]) end;
wenzelm@51510
   189
wenzelm@63347
   190
    fun prt_param (c, ty) = pretty_param ctxt (c, Type.strip_sorts_dummy ty);
wenzelm@51510
   191
wenzelm@51510
   192
    fun prt_entry class =
wenzelm@51510
   193
      Pretty.block
wenzelm@55763
   194
        ([Pretty.keyword1 "class", Pretty.brk 1,
wenzelm@53539
   195
          Name_Space.pretty ctxt class_space class, Pretty.str ":", Pretty.fbrk,
wenzelm@53539
   196
          Pretty.block [Pretty.str "supersort: ", prt_supersort class]] @
wenzelm@51685
   197
          (case try (Axclass.get_info thy) class of
wenzelm@51510
   198
            NONE => []
wenzelm@51510
   199
          | SOME {params, ...} =>
wenzelm@51510
   200
              [Pretty.fbrk, Pretty.big_list "parameters:" (map prt_param params)]) @
wenzelm@51510
   201
          (case Symtab.lookup arities class of
wenzelm@51510
   202
            NONE => []
wenzelm@51510
   203
          | SOME ars =>
wenzelm@51510
   204
              [Pretty.fbrk, Pretty.big_list "instances:"
wenzelm@51510
   205
                (map (prt_arity class) (sort (Name_Space.extern_ord ctxt type_space) ars))]));
haftmann@24218
   206
  in
wenzelm@51510
   207
    Sorts.all_classes algebra
wenzelm@51510
   208
    |> sort (Name_Space.extern_ord ctxt class_space)
wenzelm@51510
   209
    |> map prt_entry
wenzelm@56334
   210
    |> Pretty.writeln_chunks2
haftmann@24218
   211
  end;
haftmann@24218
   212
haftmann@24218
   213
haftmann@24218
   214
(* updaters *)
haftmann@24218
   215
haftmann@32850
   216
fun register class sups params base_sort base_morph export_morph
haftmann@52636
   217
    some_axiom some_assm_intro of_class thy =
haftmann@25002
   218
  let
haftmann@25368
   219
    val operations = map (fn (v_ty as (_, ty), (c, _)) =>
haftmann@60348
   220
      (c, (class, ((ty, Free v_ty), false)))) params;
haftmann@25038
   221
    val add_class = Graph.new_node (class,
wenzelm@59058
   222
        make_class_data (((map o apply2) fst params, base_sort,
wenzelm@61085
   223
          base_morph, export_morph, Option.map Thm.trim_context some_assm_intro,
wenzelm@61085
   224
          Thm.trim_context of_class, Option.map Thm.trim_context some_axiom), ([], operations)))
haftmann@29526
   225
      #> fold (curry Graph.add_edge class) sups;
wenzelm@46919
   226
  in Class_Data.map add_class thy end;
haftmann@24218
   227
wenzelm@46919
   228
fun activate_defs class thms thy =
wenzelm@46919
   229
  (case Element.eq_morphism thy thms of
wenzelm@46919
   230
    SOME eq_morph => fold (fn cls => fn thy =>
ballarin@38107
   231
      Context.theory_map (Locale.amend_registration (cls, base_morphism thy cls)
ballarin@38107
   232
        (eq_morph, true) (export_morphism thy cls)) thy) (heritage thy [class]) thy
wenzelm@46919
   233
  | NONE => thy);
haftmann@29526
   234
haftmann@60348
   235
fun register_operation class (c, t) input_only thy =
haftmann@25062
   236
  let
haftmann@29358
   237
    val base_sort = base_sort thy class;
haftmann@29439
   238
    val prep_typ = map_type_tfree
haftmann@29439
   239
      (fn (v, sort) => if Name.aT = v
haftmann@29439
   240
        then TFree (v, base_sort) else TVar ((v, 0), sort));
haftmann@25368
   241
    val t' = map_types prep_typ t;
haftmann@25368
   242
    val ty' = Term.fastype_of t';
haftmann@25062
   243
  in
haftmann@25062
   244
    thy
haftmann@57173
   245
    |> (Class_Data.map o Graph.map_node class o map_class_data o apsnd o apsnd)
haftmann@60348
   246
        (cons (c, (class, ((ty', t'), input_only))))
haftmann@57173
   247
  end;
haftmann@57173
   248
haftmann@57173
   249
fun register_def class def_thm thy =
haftmann@57173
   250
  let
wenzelm@61085
   251
    val sym_thm = Thm.trim_context (Thm.symmetric def_thm)
haftmann@57173
   252
  in
haftmann@57173
   253
    thy
haftmann@57173
   254
    |> (Class_Data.map o Graph.map_node class o map_class_data o apsnd o apfst)
haftmann@57173
   255
        (cons sym_thm)
haftmann@57173
   256
    |> activate_defs class [sym_thm]
haftmann@25062
   257
  end;
haftmann@24218
   258
haftmann@24218
   259
haftmann@24589
   260
(** classes and class target **)
haftmann@24218
   261
haftmann@25002
   262
(* class context syntax *)
haftmann@24748
   263
haftmann@60349
   264
fun make_rewrite t c_ty =
haftmann@60349
   265
  let
haftmann@60349
   266
    val (vs, t') = strip_abs t;
haftmann@60349
   267
    val vts = map snd vs
haftmann@60349
   268
      |> Name.invent_names Name.context Name.uu
haftmann@60349
   269
      |> map (fn (v, T) => Var ((v, 0), T));
haftmann@60349
   270
  in (betapplys (t, vts), betapplys (Const c_ty, vts)) end;
haftmann@60349
   271
haftmann@60348
   272
fun these_unchecks thy =
haftmann@60349
   273
  these_operations thy
haftmann@60349
   274
  #> map_filter (fn (c, (_, ((ty, t), input_only))) =>
haftmann@60349
   275
    if input_only then NONE else SOME (make_rewrite t (c, ty)));
haftmann@60349
   276
haftmann@60349
   277
fun these_unchecks_reversed thy =
haftmann@60349
   278
  these_operations thy
haftmann@60349
   279
  #> map (fn (c, (_, ((ty, t), _))) => (Const (c, ty), t));
haftmann@29577
   280
haftmann@29632
   281
fun redeclare_const thy c =
wenzelm@30364
   282
  let val b = Long_Name.base_name c
haftmann@29632
   283
  in Sign.intern_const thy b = c ? Variable.declare_const (b, c) end;
haftmann@29632
   284
haftmann@29577
   285
fun synchronize_class_syntax sort base_sort ctxt =
haftmann@24914
   286
  let
wenzelm@42360
   287
    val thy = Proof_Context.theory_of ctxt;
haftmann@26596
   288
    val algebra = Sign.classes_of thy;
haftmann@29577
   289
    val operations = these_operations thy sort;
haftmann@26518
   290
    fun subst_class_typ sort = map_type_tfree (K (TVar ((Name.aT, 0), sort)));
haftmann@26518
   291
    val primary_constraints =
haftmann@60348
   292
      (map o apsnd) (subst_class_typ base_sort o fst o fst o snd) operations;
haftmann@26518
   293
    val secondary_constraints =
haftmann@60348
   294
      (map o apsnd) (fn (class, ((ty, _), _)) => subst_class_typ [class] ty) operations;
wenzelm@46919
   295
    fun improve (c, ty) =
wenzelm@46919
   296
      (case AList.lookup (op =) primary_constraints c of
wenzelm@46919
   297
        SOME ty' =>
wenzelm@46919
   298
          (case try (Type.raw_match (ty', ty)) Vartab.empty of
wenzelm@46919
   299
            SOME tyenv =>
wenzelm@46919
   300
              (case Vartab.lookup tyenv (Name.aT, 0) of
wenzelm@46919
   301
                SOME (_, ty' as TVar (vi, sort)) =>
wenzelm@46919
   302
                  if Type_Infer.is_param vi andalso Sorts.sort_le algebra (base_sort, sort)
wenzelm@46919
   303
                  then SOME (ty', TFree (Name.aT, base_sort))
wenzelm@46919
   304
                  else NONE
haftmann@26238
   305
              | _ => NONE)
haftmann@26238
   306
          | NONE => NONE)
wenzelm@46919
   307
      | NONE => NONE);
haftmann@60348
   308
    fun subst (c, _) = Option.map (fst o snd) (AList.lookup (op =) operations c);
haftmann@29577
   309
    val unchecks = these_unchecks thy sort;
haftmann@25083
   310
  in
haftmann@25083
   311
    ctxt
haftmann@29632
   312
    |> fold (redeclare_const thy o fst) primary_constraints
haftmann@60340
   313
    |> Overloading.map_improvable_syntax (K {primary_constraints = primary_constraints,
haftmann@60340
   314
      secondary_constraints = secondary_constraints, improve = improve, subst = subst,
haftmann@60340
   315
      no_subst_in_abbrev_mode = true, unchecks = unchecks})
haftmann@26518
   316
    |> Overloading.set_primary_constraints
haftmann@25083
   317
  end;
haftmann@25083
   318
haftmann@54866
   319
fun synchronize_class_syntax_target class lthy =
haftmann@54866
   320
  lthy
haftmann@54866
   321
  |> Local_Theory.map_contexts
haftmann@54866
   322
      (K (synchronize_class_syntax [class] (base_sort (Proof_Context.theory_of lthy) class)));
haftmann@54866
   323
haftmann@29632
   324
fun redeclare_operations thy sort =
haftmann@29632
   325
  fold (redeclare_const thy o fst) (these_operations thy sort);
haftmann@29632
   326
haftmann@29577
   327
fun begin sort base_sort ctxt =
haftmann@25083
   328
  ctxt
haftmann@25083
   329
  |> Variable.declare_term
haftmann@25083
   330
      (Logic.mk_type (TFree (Name.aT, base_sort)))
haftmann@29577
   331
  |> synchronize_class_syntax sort base_sort
haftmann@39378
   332
  |> Overloading.activate_improvable_syntax;
haftmann@24901
   333
haftmann@25311
   334
fun init class thy =
haftmann@25311
   335
  thy
haftmann@29509
   336
  |> Locale.init class
haftmann@29358
   337
  |> begin [class] (base_sort thy class);
haftmann@24914
   338
haftmann@24748
   339
haftmann@27690
   340
(* class target *)
haftmann@27690
   341
wenzelm@30364
   342
val class_prefix = Logic.const_of_class o Long_Name.base_name;
haftmann@29526
   343
haftmann@57187
   344
fun guess_morphism_identity (b, rhs) phi1 phi2 =
haftmann@57187
   345
  let
haftmann@57187
   346
    (*FIXME proper concept to identify morphism instead of educated guess*)
wenzelm@58668
   347
    val name_of_binding = Name_Space.full_name Name_Space.global_naming;
haftmann@57187
   348
    val n1 = (name_of_binding o Morphism.binding phi1) b;
haftmann@57187
   349
    val n2 = (name_of_binding o Morphism.binding phi2) b;
haftmann@57187
   350
    val rhs1 = Morphism.term phi1 rhs;
haftmann@57187
   351
    val rhs2 = Morphism.term phi2 rhs;
haftmann@57187
   352
  in n1 = n2 andalso Term.aconv_untyped (rhs1, rhs2) end;
haftmann@57187
   353
haftmann@60347
   354
fun target_const class phi0 prmode (b, rhs) lthy =
haftmann@57189
   355
  let
haftmann@60347
   356
    val export = Variable.export_morphism lthy (Local_Theory.target_of lthy);
haftmann@60347
   357
    val guess_identity = guess_morphism_identity (b, rhs) export;
haftmann@60347
   358
    val guess_canonical = guess_morphism_identity (b, rhs) (export $> phi0);
haftmann@57189
   359
  in
haftmann@60347
   360
    lthy
haftmann@60347
   361
    |> Generic_Target.locale_target_const class
haftmann@57189
   362
      (not o (guess_identity orf guess_canonical)) prmode ((b, NoSyn), rhs)
haftmann@57189
   363
  end;
haftmann@57072
   364
haftmann@60347
   365
local
haftmann@60347
   366
haftmann@57119
   367
fun dangling_params_for lthy class (type_params, term_params) =
haftmann@27690
   368
  let
haftmann@57119
   369
    val class_param_names =
haftmann@57119
   370
      map fst (these_params (Proof_Context.theory_of lthy) [class]);
haftmann@57119
   371
    val dangling_term_params =
haftmann@57119
   372
      subtract (fn (v, Free (w, _)) => v = w | _ => false) class_param_names term_params;
haftmann@57161
   373
  in (type_params, dangling_term_params) end;
haftmann@57119
   374
haftmann@57119
   375
fun global_def (b, eq) thy =
haftmann@57119
   376
  thy
haftmann@57119
   377
  |> Thm.add_def_global false false (b, eq)
haftmann@57119
   378
  |>> (Thm.varifyT_global o snd)
haftmann@57119
   379
  |-> (fn def_thm => Global_Theory.store_thm (b, def_thm)
haftmann@57119
   380
      #> snd
haftmann@57119
   381
      #> pair def_thm);
haftmann@57119
   382
haftmann@57142
   383
fun canonical_const class phi dangling_params ((b, mx), rhs) thy =
haftmann@57119
   384
  let
haftmann@57147
   385
    val b_def = Binding.suffix_name "_dict" b;
haftmann@57147
   386
    val c = Sign.full_name thy b;
haftmann@57147
   387
    val ty = map Term.fastype_of dangling_params ---> Term.fastype_of rhs;
haftmann@57147
   388
    val def_eq = Logic.mk_equals (list_comb (Const (c, ty), dangling_params), rhs)
haftmann@29577
   389
      |> map_types Type.strip_sorts;
haftmann@27690
   390
  in
haftmann@29577
   391
    thy
haftmann@57147
   392
    |> Sign.declare_const_global ((b, Type.strip_sorts ty), mx)
haftmann@29577
   393
    |> snd
haftmann@57119
   394
    |> global_def (b_def, def_eq)
haftmann@57173
   395
    |-> (fn def_thm => register_def class def_thm)
haftmann@60348
   396
    |> null dangling_params ? register_operation class (c, rhs) false
haftmann@57147
   397
    |> Sign.add_const_constraint (c, SOME ty)
haftmann@27690
   398
  end;
haftmann@27690
   399
haftmann@38619
   400
in
haftmann@38619
   401
haftmann@57119
   402
fun const class ((b, mx), lhs) params lthy =
haftmann@57119
   403
  let
haftmann@57141
   404
    val phi = morphism (Proof_Context.theory_of lthy) class;
haftmann@57161
   405
    val dangling_params = map (Morphism.term phi) (uncurry append (dangling_params_for lthy class params));
haftmann@57119
   406
  in
haftmann@57119
   407
    lthy
haftmann@60339
   408
    |> target_const class phi Syntax.mode_default (b, lhs)
haftmann@57148
   409
    |> Local_Theory.raw_theory (canonical_const class phi dangling_params
haftmann@57148
   410
         ((Morphism.binding phi b, if null dangling_params then mx else NoSyn), Morphism.term phi lhs))
haftmann@57192
   411
    |> Generic_Target.standard_const (fn (this, other) => other <> 0 andalso this <> other)
haftmann@57148
   412
         Syntax.mode_default ((b, if null dangling_params then NoSyn else mx), lhs)
haftmann@57141
   413
    |> synchronize_class_syntax_target class
haftmann@57119
   414
  end;
haftmann@57072
   415
haftmann@60347
   416
end;
haftmann@60347
   417
haftmann@60347
   418
local
haftmann@60347
   419
haftmann@60347
   420
fun canonical_abbrev class phi prmode with_syntax ((b, mx), rhs) thy =
haftmann@57141
   421
  let
haftmann@60347
   422
    val c = Sign.full_name thy b;
haftmann@60347
   423
    val constrain = map_atyps (fn T as TFree (v, _) =>
haftmann@60347
   424
      if v = Name.aT then TFree (v, [class]) else T | T => T);
haftmann@60347
   425
    val rhs' = map_types constrain rhs;
haftmann@60347
   426
  in
haftmann@60347
   427
    thy
haftmann@60347
   428
    |> Sign.add_abbrev (#1 prmode) (b, Logic.varify_types_global rhs')
haftmann@60347
   429
    |> snd
haftmann@60347
   430
    |> with_syntax ? Sign.notation true prmode [(Const (c, fastype_of rhs), mx)]
haftmann@60347
   431
    |> with_syntax ? register_operation class (c, rhs)
haftmann@60348
   432
        (#1 prmode = Print_Mode.input)
haftmann@60347
   433
    |> Sign.add_const_constraint (c, SOME (fastype_of rhs'))
haftmann@60347
   434
  end;
haftmann@60347
   435
haftmann@60347
   436
fun canonical_abbrev_target class phi prmode ((b, mx), rhs) lthy =
haftmann@60347
   437
  let
haftmann@60347
   438
    val thy = Proof_Context.theory_of lthy;
haftmann@60349
   439
    val preprocess = perhaps (try (Pattern.rewrite_term_top thy (these_unchecks thy [class]) []));
haftmann@60347
   440
    val (global_rhs, (extra_tfrees, (type_params, term_params))) =
haftmann@60347
   441
      Generic_Target.export_abbrev lthy preprocess rhs;
haftmann@60347
   442
    val mx' = Generic_Target.check_mixfix_global (b, null term_params) mx;
haftmann@57141
   443
  in
haftmann@57141
   444
    lthy
haftmann@60347
   445
    |> Local_Theory.raw_theory (canonical_abbrev class phi prmode (null term_params)
haftmann@60347
   446
        ((Morphism.binding phi b, mx'), Logic.unvarify_types_global global_rhs))
haftmann@57141
   447
  end;
haftmann@38619
   448
haftmann@60347
   449
fun further_abbrev_target class phi prmode (b, mx) rhs params =
haftmann@60347
   450
  Generic_Target.background_abbrev (b, rhs) (snd params)
haftmann@60347
   451
  #-> (fn (lhs, _) => target_const class phi prmode (b, lhs)
haftmann@60347
   452
    #> Generic_Target.standard_const (fn (this, other) => other <> 0 andalso this <> other) prmode ((b, mx), lhs))
haftmann@60347
   453
haftmann@60347
   454
in
haftmann@60347
   455
haftmann@60347
   456
fun abbrev class prmode ((b, mx), rhs) lthy =
haftmann@60347
   457
  let
haftmann@60347
   458
    val thy = Proof_Context.theory_of lthy;
haftmann@60347
   459
    val phi = morphism thy class;
haftmann@60347
   460
    val rhs_generic =
haftmann@60349
   461
      perhaps (try (Pattern.rewrite_term_top thy (these_unchecks_reversed thy [class]) [])) rhs;
haftmann@60347
   462
  in
haftmann@60347
   463
    lthy
haftmann@60347
   464
    |> canonical_abbrev_target class phi prmode ((b, mx), rhs)
haftmann@60347
   465
    |> Generic_Target.abbrev (further_abbrev_target class phi) prmode ((b, mx), rhs_generic)
haftmann@60347
   466
    ||> synchronize_class_syntax_target class
haftmann@60347
   467
  end;
haftmann@60346
   468
haftmann@38619
   469
end;
haftmann@38619
   470
haftmann@27690
   471
haftmann@54866
   472
(* subclasses *)
haftmann@54866
   473
haftmann@54866
   474
fun register_subclass (sub, sup) some_dep_morph some_witn export lthy =
haftmann@54866
   475
  let
haftmann@54866
   476
    val thy = Proof_Context.theory_of lthy;
haftmann@54866
   477
    val intros = (snd o rules thy) sup :: map_filter I
wenzelm@54883
   478
      [Option.map (Drule.export_without_context_open o Element.conclude_witness lthy) some_witn,
haftmann@54866
   479
        (fst o rules thy) sub];
haftmann@54866
   480
    val classrel =
haftmann@54866
   481
      Goal.prove_sorry_global thy [] [] (Logic.mk_classrel (sub, sup))
wenzelm@59498
   482
        (fn {context = ctxt, ...} => EVERY (map (TRYALL o resolve_tac ctxt o single) intros));
haftmann@54866
   483
    val diff_sort = Sign.complete_sort thy [sup]
haftmann@54866
   484
      |> subtract (op =) (Sign.complete_sort thy [sub])
haftmann@54866
   485
      |> filter (is_class thy);
haftmann@54866
   486
    fun add_dependency some_wit = case some_dep_morph of
haftmann@54866
   487
        SOME dep_morph => Generic_Target.locale_dependency sub
haftmann@54866
   488
          (sup, dep_morph $> Element.satisfy_morphism (the_list some_witn)) NONE export
haftmann@54866
   489
      | NONE => I;
haftmann@54866
   490
  in
haftmann@54866
   491
    lthy
haftmann@54866
   492
    |> Local_Theory.raw_theory
haftmann@54866
   493
      (Axclass.add_classrel classrel
haftmann@54866
   494
      #> Class_Data.map (Graph.add_edge (sub, sup))
haftmann@54866
   495
      #> activate_defs sub (these_defs thy diff_sort))
haftmann@54866
   496
    |> add_dependency some_witn
haftmann@54866
   497
    |> synchronize_class_syntax_target sub
haftmann@54866
   498
  end;
haftmann@31635
   499
haftmann@31635
   500
local
haftmann@31635
   501
haftmann@31635
   502
fun gen_classrel mk_prop classrel thy =
haftmann@31635
   503
  let
haftmann@31635
   504
    fun after_qed results =
wenzelm@51685
   505
      Proof_Context.background_theory ((fold o fold) Axclass.add_classrel results);
haftmann@31635
   506
  in
haftmann@31635
   507
    thy
wenzelm@42360
   508
    |> Proof_Context.init_global
wenzelm@36323
   509
    |> Proof.theorem NONE after_qed [[(mk_prop thy classrel, [])]]
haftmann@31635
   510
  end;
haftmann@31635
   511
haftmann@31635
   512
in
haftmann@31635
   513
haftmann@31635
   514
val classrel =
wenzelm@51685
   515
  gen_classrel (Logic.mk_classrel oo Axclass.cert_classrel);
haftmann@31635
   516
val classrel_cmd =
wenzelm@51685
   517
  gen_classrel (Logic.mk_classrel oo Axclass.read_classrel);
haftmann@31635
   518
haftmann@31635
   519
end; (*local*)
haftmann@31635
   520
haftmann@31635
   521
haftmann@25462
   522
(** instantiation target **)
haftmann@25462
   523
haftmann@25462
   524
(* bookkeeping *)
haftmann@25462
   525
haftmann@25462
   526
datatype instantiation = Instantiation of {
haftmann@25864
   527
  arities: string list * (string * sort) list * sort,
haftmann@25462
   528
  params: ((string * string) * (string * typ)) list
haftmann@25603
   529
    (*(instantiation parameter, type constructor), (local instantiation parameter, typ)*)
haftmann@25462
   530
}
haftmann@25462
   531
wenzelm@59150
   532
fun make_instantiation (arities, params) =
wenzelm@59150
   533
  Instantiation {arities = arities, params = params};
wenzelm@59150
   534
wenzelm@59150
   535
val empty_instantiation = make_instantiation (([], [], []), []);
wenzelm@59150
   536
wenzelm@33519
   537
structure Instantiation = Proof_Data
haftmann@25462
   538
(
wenzelm@46919
   539
  type T = instantiation;
wenzelm@59150
   540
  fun init _ = empty_instantiation;
haftmann@25462
   541
);
haftmann@25462
   542
wenzelm@46919
   543
val get_instantiation =
wenzelm@46919
   544
  (fn Instantiation data => data) o Instantiation.get o Local_Theory.target_of;
haftmann@25462
   545
wenzelm@46919
   546
fun map_instantiation f =
wenzelm@46919
   547
  (Local_Theory.target o Instantiation.map)
wenzelm@59150
   548
    (fn Instantiation {arities, params} => make_instantiation (f (arities, params)));
wenzelm@46919
   549
wenzelm@46919
   550
fun the_instantiation lthy =
wenzelm@46919
   551
  (case get_instantiation lthy of
wenzelm@46919
   552
    {arities = ([], [], []), ...} => error "No instantiation target"
wenzelm@46919
   553
  | data => data);
haftmann@25462
   554
haftmann@25485
   555
val instantiation_params = #params o get_instantiation;
haftmann@25462
   556
haftmann@30519
   557
fun instantiation_param lthy b = instantiation_params lthy
haftmann@30519
   558
  |> find_first (fn (_, (v, _)) => Binding.name_of b = v)
haftmann@25462
   559
  |> Option.map (fst o fst);
haftmann@25462
   560
haftmann@31869
   561
fun read_multi_arity thy (raw_tycos, raw_sorts, raw_sort) =
haftmann@31869
   562
  let
wenzelm@42360
   563
    val ctxt = Proof_Context.init_global thy;
wenzelm@42360
   564
    val all_arities = map (fn raw_tyco => Proof_Context.read_arity ctxt
haftmann@31869
   565
      (raw_tyco, raw_sorts, raw_sort)) raw_tycos;
haftmann@31869
   566
    val tycos = map #1 all_arities;
haftmann@31869
   567
    val (_, sorts, sort) = hd all_arities;
wenzelm@43329
   568
    val vs = Name.invent_names Name.context Name.aT sorts;
haftmann@31869
   569
  in (tycos, vs, sort) end;
haftmann@31869
   570
haftmann@25462
   571
haftmann@25462
   572
(* syntax *)
haftmann@25462
   573
haftmann@26238
   574
fun synchronize_inst_syntax ctxt =
haftmann@25462
   575
  let
wenzelm@46919
   576
    val Instantiation {params, ...} = Instantiation.get ctxt;
haftmann@31249
   577
wenzelm@51685
   578
    val lookup_inst_param = Axclass.lookup_inst_param
wenzelm@42360
   579
      (Sign.consts_of (Proof_Context.theory_of ctxt)) params;
wenzelm@46919
   580
    fun subst (c, ty) =
wenzelm@46919
   581
      (case lookup_inst_param (c, ty) of
wenzelm@46919
   582
        SOME (v_ty as (_, ty)) => SOME (ty, Free v_ty)
wenzelm@46919
   583
      | NONE => NONE);
haftmann@26238
   584
    val unchecks =
haftmann@26238
   585
      map (fn ((c, _), v_ty as (_, ty)) => (Free v_ty, Const (c, ty))) params;
haftmann@26238
   586
  in
haftmann@26238
   587
    ctxt
haftmann@60340
   588
    |> Overloading.map_improvable_syntax (fn {primary_constraints, improve, ...} =>
haftmann@60340
   589
      {primary_constraints = primary_constraints, secondary_constraints = [],
haftmann@60340
   590
        improve = improve, subst = subst, no_subst_in_abbrev_mode = false,
haftmann@60340
   591
        unchecks = unchecks})
haftmann@26238
   592
  end;
haftmann@25462
   593
wenzelm@42385
   594
fun resort_terms ctxt algebra consts constraints ts =
haftmann@38382
   595
  let
wenzelm@42385
   596
    fun matchings (Const (c_ty as (c, _))) =
wenzelm@42385
   597
          (case constraints c of
wenzelm@42385
   598
            NONE => I
wenzelm@42385
   599
          | SOME sorts =>
wenzelm@42385
   600
              fold2 (curry (Sorts.meet_sort algebra)) (Consts.typargs consts c_ty) sorts)
wenzelm@42385
   601
      | matchings _ = I;
haftmann@38382
   602
    val tvartab = (fold o fold_aterms) matchings ts Vartab.empty
wenzelm@61262
   603
      handle Sorts.CLASS_ERROR e => error (Sorts.class_error (Context.Proof ctxt) e);
haftmann@38382
   604
    val inst = map_type_tvar
haftmann@38382
   605
      (fn (vi, sort) => TVar (vi, the_default sort (Vartab.lookup tvartab vi)));
wenzelm@42402
   606
  in if Vartab.is_empty tvartab then ts else (map o map_types) inst ts end;
haftmann@38382
   607
haftmann@25462
   608
haftmann@25462
   609
(* target *)
haftmann@25462
   610
wenzelm@63347
   611
fun define_overloaded (c, U) b (b_def, rhs) lthy =
wenzelm@63347
   612
  let
wenzelm@63347
   613
    val name = Binding.name_of b;
wenzelm@63347
   614
    val pos = Binding.pos_of b;
wenzelm@63347
   615
    val _ =
wenzelm@63347
   616
      if Context_Position.is_reported lthy pos then
wenzelm@63347
   617
        Position.report_text pos Markup.class_parameter
wenzelm@63347
   618
          (Pretty.string_of
wenzelm@63347
   619
            (Pretty.block [Pretty.keyword1 "class", Pretty.brk 1,
wenzelm@63347
   620
                Pretty.str "parameter", Pretty.brk 1, pretty_param lthy (c, U)]))
wenzelm@63347
   621
      else ();
wenzelm@63347
   622
  in
wenzelm@63347
   623
    lthy |> Local_Theory.background_theory_result
wenzelm@63347
   624
      (Axclass.declare_overloaded (c, U) ##>> Axclass.define_overloaded b_def (c, rhs))
wenzelm@63347
   625
    ||> (map_instantiation o apsnd) (filter_out (fn (_, (v', _)) => v' = name))
wenzelm@63347
   626
    ||> Local_Theory.map_contexts (K synchronize_inst_syntax)
wenzelm@63347
   627
  end;
haftmann@38382
   628
wenzelm@47289
   629
fun foundation (((b, U), mx), (b_def, rhs)) params lthy =
wenzelm@46919
   630
  (case instantiation_param lthy b of
wenzelm@46919
   631
    SOME c =>
wenzelm@63347
   632
      if Mixfix.is_empty mx then lthy |> define_overloaded (c, U) b (b_def, rhs)
wenzelm@62765
   633
      else error ("Illegal mixfix syntax for overloaded constant " ^ quote c)
haftmann@60343
   634
  | NONE => lthy |> Generic_Target.theory_target_foundation (((b, U), mx), (b_def, rhs)) params);
haftmann@38382
   635
haftmann@38382
   636
fun pretty lthy =
haftmann@26518
   637
  let
wenzelm@46919
   638
    val {arities = (tycos, vs, sort), params} = the_instantiation lthy;
haftmann@38382
   639
    fun pr_arity tyco = Syntax.pretty_arity lthy (tyco, map snd vs, sort);
haftmann@38382
   640
    fun pr_param ((c, _), (v, ty)) =
wenzelm@42359
   641
      Pretty.block (Pretty.breaks
wenzelm@55304
   642
        [Pretty.str v, Pretty.str "==", Proof_Context.pretty_const lthy c,
wenzelm@42359
   643
          Pretty.str "::", Syntax.pretty_typ lthy ty]);
wenzelm@60246
   644
  in
wenzelm@60246
   645
    [Pretty.block
wenzelm@60246
   646
      (Pretty.fbreaks (Pretty.keyword1 "instantiation" :: map pr_arity tycos @ map pr_param params))]
wenzelm@60246
   647
  end;
haftmann@26518
   648
haftmann@38382
   649
fun conclude lthy =
haftmann@38382
   650
  let
wenzelm@42359
   651
    val (tycos, vs, sort) = #arities (the_instantiation lthy);
wenzelm@42360
   652
    val thy = Proof_Context.theory_of lthy;
wenzelm@42359
   653
    val _ = tycos |> List.app (fn tyco =>
wenzelm@46919
   654
      if Sign.of_sort thy (Type (tyco, map TFree vs), sort) then ()
wenzelm@55304
   655
      else error ("Missing instance proof for type " ^ quote (Proof_Context.markup_type lthy tyco)));
haftmann@38382
   656
  in lthy end;
haftmann@38382
   657
haftmann@38382
   658
fun instantiation (tycos, vs, sort) thy =
haftmann@25462
   659
  let
haftmann@25536
   660
    val _ = if null tycos then error "At least one arity must be given" else ();
wenzelm@51685
   661
    val class_params = these_params thy (filter (can (Axclass.get_info thy)) sort);
haftmann@29632
   662
    fun get_param tyco (param, (_, (c, ty))) =
wenzelm@51685
   663
      if can (Axclass.param_of_inst thy) (c, tyco)
haftmann@25603
   664
      then NONE else SOME ((c, tyco),
wenzelm@46917
   665
        (param ^ "_" ^ Long_Name.base_name tyco, map_atyps (K (Type (tyco, map TFree vs))) ty));
haftmann@31249
   666
    val params = map_product get_param tycos class_params |> map_filter I;
haftmann@51578
   667
    val _ = if null params andalso forall (fn tyco => can (Sign.arity_sorts thy tyco) sort) tycos
haftmann@51578
   668
      then error "No parameters and no pending instance proof obligations in instantiation."
haftmann@51578
   669
      else ();
haftmann@26518
   670
    val primary_constraints = map (apsnd
haftmann@31249
   671
      (map_atyps (K (TVar ((Name.aT, 0), [])))) o snd o snd) class_params;
haftmann@26518
   672
    val algebra = Sign.classes_of thy
wenzelm@61262
   673
      |> fold (fn tyco => Sorts.add_arities (Context.Theory thy)
haftmann@26518
   674
            (tyco, map (fn class => (class, map snd vs)) sort)) tycos;
haftmann@26518
   675
    val consts = Sign.consts_of thy;
haftmann@26518
   676
    val improve_constraints = AList.lookup (op =)
haftmann@31249
   677
      (map (fn (_, (class, (c, _))) => (c, [[class]])) class_params);
wenzelm@42402
   678
    fun resort_check ctxt ts = resort_terms ctxt algebra consts improve_constraints ts;
wenzelm@51685
   679
    val lookup_inst_param = Axclass.lookup_inst_param consts params;
wenzelm@42388
   680
    fun improve (c, ty) =
wenzelm@42388
   681
      (case lookup_inst_param (c, ty) of
wenzelm@42388
   682
        SOME (_, ty') => if Sign.typ_instance thy (ty', ty) then SOME (ty, ty') else NONE
wenzelm@42388
   683
      | NONE => NONE);
haftmann@25485
   684
  in
haftmann@25485
   685
    thy
wenzelm@56056
   686
    |> Sign.change_begin
wenzelm@42360
   687
    |> Proof_Context.init_global
wenzelm@59150
   688
    |> Instantiation.put (make_instantiation ((tycos, vs, sort), params))
wenzelm@27281
   689
    |> fold (Variable.declare_typ o TFree) vs
haftmann@31249
   690
    |> fold (Variable.declare_names o Free o snd) params
haftmann@60340
   691
    |> (Overloading.map_improvable_syntax) (K {primary_constraints = primary_constraints,
haftmann@60340
   692
      secondary_constraints = [], improve = improve, subst = K NONE,
haftmann@60340
   693
      no_subst_in_abbrev_mode = false, unchecks = []})
haftmann@39378
   694
    |> Overloading.activate_improvable_syntax
wenzelm@45429
   695
    |> Context.proof_map (Syntax_Phases.term_check 0 "resorting" resort_check)
haftmann@26238
   696
    |> synchronize_inst_syntax
wenzelm@59876
   697
    |> Local_Theory.init (Sign.naming_of thy)
haftmann@38382
   698
       {define = Generic_Target.define foundation,
haftmann@60343
   699
        notes = Generic_Target.notes Generic_Target.theory_target_notes,
haftmann@60343
   700
        abbrev = Generic_Target.abbrev Generic_Target.theory_target_abbrev,
wenzelm@47279
   701
        declaration = K Generic_Target.theory_declaration,
haftmann@61858
   702
        theory_registration = Generic_Target.theory_registration,
haftmann@61858
   703
        locale_dependency = fn _ => error "Not possible in instantiation target",
haftmann@38382
   704
        pretty = pretty,
wenzelm@56056
   705
        exit = conclude #> Local_Theory.target_of #> Sign.change_end_local}
haftmann@25485
   706
  end;
haftmann@25485
   707
haftmann@38382
   708
fun instantiation_cmd arities thy =
haftmann@38382
   709
  instantiation (read_multi_arity thy arities) thy;
haftmann@26238
   710
haftmann@25485
   711
fun gen_instantiation_instance do_proof after_qed lthy =
haftmann@25485
   712
  let
haftmann@25864
   713
    val (tycos, vs, sort) = (#arities o the_instantiation) lthy;
haftmann@25864
   714
    val arities_proof = maps (fn tyco => Logic.mk_arities (tyco, map snd vs, sort)) tycos;
haftmann@25462
   715
    fun after_qed' results =
wenzelm@51685
   716
      Local_Theory.background_theory (fold (Axclass.add_arity o Thm.varifyT_global) results)
haftmann@25462
   717
      #> after_qed;
haftmann@25462
   718
  in
haftmann@25462
   719
    lthy
haftmann@25462
   720
    |> do_proof after_qed' arities_proof
haftmann@25462
   721
  end;
haftmann@25462
   722
haftmann@25485
   723
val instantiation_instance = gen_instantiation_instance (fn after_qed => fn ts =>
wenzelm@36323
   724
  Proof.theorem NONE (after_qed o map the_single) (map (fn t => [(t, [])]) ts));
haftmann@25462
   725
haftmann@25485
   726
fun prove_instantiation_instance tac = gen_instantiation_instance (fn after_qed =>
haftmann@25502
   727
  fn ts => fn lthy => after_qed (map (fn t => Goal.prove lthy [] [] t
haftmann@25502
   728
    (fn {context, ...} => tac context)) ts) lthy) I;
haftmann@25462
   729
haftmann@28666
   730
fun prove_instantiation_exit tac = prove_instantiation_instance tac
wenzelm@33671
   731
  #> Local_Theory.exit_global;
haftmann@28666
   732
haftmann@28666
   733
fun prove_instantiation_exit_result f tac x lthy =
haftmann@28666
   734
  let
wenzelm@42360
   735
    val morph = Proof_Context.export_morphism lthy
wenzelm@42360
   736
      (Proof_Context.init_global (Proof_Context.theory_of lthy));
haftmann@29439
   737
    val y = f morph x;
haftmann@28666
   738
  in
haftmann@28666
   739
    lthy
haftmann@28666
   740
    |> prove_instantiation_exit (fn ctxt => tac ctxt y)
haftmann@28666
   741
    |> pair y
haftmann@28666
   742
  end;
haftmann@28666
   743
haftmann@29526
   744
haftmann@31635
   745
(* simplified instantiation interface with no class parameter *)
haftmann@31635
   746
haftmann@31869
   747
fun instance_arity_cmd raw_arities thy =
haftmann@31635
   748
  let
haftmann@31869
   749
    val (tycos, vs, sort) = read_multi_arity thy raw_arities;
haftmann@31869
   750
    val sorts = map snd vs;
haftmann@31869
   751
    val arities = maps (fn tyco => Logic.mk_arities (tyco, sorts, sort)) tycos;
wenzelm@47078
   752
    fun after_qed results =
wenzelm@51685
   753
      Proof_Context.background_theory ((fold o fold) Axclass.add_arity results);
haftmann@31635
   754
  in
haftmann@31635
   755
    thy
wenzelm@42360
   756
    |> Proof_Context.init_global
wenzelm@36323
   757
    |> Proof.theorem NONE after_qed (map (fn t => [(t, [])]) arities)
haftmann@31635
   758
  end;
haftmann@31635
   759
haftmann@31635
   760
wenzelm@60618
   761
haftmann@29526
   762
(** tactics and methods **)
haftmann@29526
   763
wenzelm@59498
   764
fun intro_classes_tac ctxt facts st =
haftmann@29526
   765
  let
wenzelm@60816
   766
    val thy = Proof_Context.theory_of ctxt;
haftmann@29526
   767
    val classes = Sign.all_classes thy;
haftmann@29526
   768
    val class_trivs = map (Thm.class_triv thy) classes;
wenzelm@51685
   769
    val class_intros = map_filter (try (#intro o Axclass.get_info thy)) classes;
haftmann@29526
   770
    val assm_intros = all_assm_intros thy;
haftmann@29526
   771
  in
wenzelm@59498
   772
    Method.intros_tac ctxt (class_trivs @ class_intros @ assm_intros) facts st
haftmann@29526
   773
  end;
haftmann@29526
   774
wenzelm@60618
   775
fun standard_intro_classes_tac ctxt facts st =
wenzelm@60618
   776
  if null facts andalso not (Thm.no_prems st) then
wenzelm@60618
   777
    (intro_classes_tac ctxt [] ORELSE Locale.intro_locales_tac true ctxt []) st
wenzelm@60618
   778
  else no_tac st;
haftmann@29526
   779
wenzelm@60619
   780
fun standard_tac ctxt facts =
wenzelm@60619
   781
  HEADGOAL (Method.some_rule_tac ctxt [] facts) ORELSE
wenzelm@60618
   782
  standard_intro_classes_tac ctxt facts;
wenzelm@60618
   783
wenzelm@53171
   784
val _ = Theory.setup
wenzelm@59498
   785
 (Method.setup @{binding intro_classes} (Scan.succeed (METHOD o intro_classes_tac))
wenzelm@30515
   786
    "back-chain introduction rules of classes" #>
wenzelm@60619
   787
  Method.setup @{binding standard} (Scan.succeed (METHOD o standard_tac))
wenzelm@62939
   788
    "standard proof step: Pure intro/elim rule or class introduction");
haftmann@29526
   789
haftmann@59296
   790
wenzelm@60249
   791
haftmann@59296
   792
(** diagnostics **)
haftmann@59296
   793
wenzelm@60249
   794
fun pretty_specification thy class =
wenzelm@60249
   795
  if is_class thy class then
wenzelm@59383
   796
    let
wenzelm@60249
   797
      val class_ctxt = init class thy;
wenzelm@60249
   798
      val prt_class = Name_Space.pretty class_ctxt (Proof_Context.class_space class_ctxt);
wenzelm@60249
   799
wenzelm@60249
   800
      val super_classes = Sign.minimize_sort thy (Sign.super_classes thy class);
wenzelm@59383
   801
wenzelm@59383
   802
      val fix_args =
wenzelm@60249
   803
        #params (Axclass.get_info thy class)
wenzelm@59383
   804
        |> map (fn (c, T) => (Binding.name (Long_Name.base_name c), SOME T, NoSyn));
wenzelm@59383
   805
      val fixes = if null fix_args then [] else [Element.Fixes fix_args];
wenzelm@60249
   806
      val assumes = Locale.hyp_spec_of thy class;
wenzelm@59383
   807
wenzelm@59383
   808
      val header =
wenzelm@60249
   809
        [Pretty.keyword1 "class", Pretty.brk 1, prt_class class, Pretty.str " =", Pretty.brk 1] @
wenzelm@60249
   810
        Pretty.separate " +" (map prt_class super_classes) @
wenzelm@60249
   811
        (if null super_classes then [] else [Pretty.str " +"]);
wenzelm@59383
   812
      val body =
wenzelm@59385
   813
        if null fixes andalso null assumes then []
wenzelm@59385
   814
        else
wenzelm@59385
   815
          maps (Element.pretty_ctxt_no_attribs class_ctxt) (fixes @ assumes)
wenzelm@59385
   816
          |> maps (fn prt => [Pretty.fbrk, prt]);
haftmann@59423
   817
    in if null body then [] else [Pretty.block (header @ body)] end
haftmann@59423
   818
  else [];
haftmann@59296
   819
haftmann@24218
   820
end;