src/Pure/Isar/class_target.ML
author wenzelm
Fri Nov 13 21:11:15 2009 +0100 (2009-11-13)
changeset 33671 4b0f2599ed48
parent 33522 737589bb9bb8
child 33967 e191b400a8e0
permissions -rw-r--r--
modernized structure Local_Theory;
haftmann@29358
     1
(*  Title:      Pure/Isar/class_target.ML
haftmann@24218
     2
    Author:     Florian Haftmann, TU Muenchen
haftmann@24218
     3
haftmann@29358
     4
Type classes derived from primitive axclasses and locales - mechanisms.
haftmann@24218
     5
*)
haftmann@24218
     6
haftmann@29358
     7
signature CLASS_TARGET =
haftmann@24218
     8
sig
haftmann@25462
     9
  (*classes*)
haftmann@29358
    10
  val register: class -> class list -> ((string * typ) * (string * typ)) list
haftmann@32850
    11
    -> sort -> morphism -> morphism -> thm option -> thm option -> thm
haftmann@29526
    12
    -> theory -> theory
haftmann@25485
    13
haftmann@29526
    14
  val is_class: theory -> class -> bool
haftmann@29526
    15
  val base_sort: theory -> class -> sort
haftmann@29526
    16
  val rules: theory -> class -> thm option * thm
haftmann@29526
    17
  val these_params: theory -> sort -> (string * (class * (string * typ))) list
haftmann@29526
    18
  val these_defs: theory -> sort -> thm list
haftmann@29577
    19
  val these_operations: theory -> sort -> (string * (class * (typ * term))) list
haftmann@29526
    20
  val print_classes: theory -> unit
haftmann@29526
    21
haftmann@29358
    22
  val begin: class list -> sort -> Proof.context -> Proof.context
haftmann@25311
    23
  val init: class -> theory -> Proof.context
wenzelm@33173
    24
  val declare: class -> (binding * mixfix) * term -> theory -> theory
wenzelm@33173
    25
  val abbrev: class -> Syntax.mode -> (binding * mixfix) * term -> theory -> theory
haftmann@29526
    26
  val class_prefix: string -> string
haftmann@25083
    27
  val refresh_syntax: class -> Proof.context -> Proof.context
haftmann@29632
    28
  val redeclare_operations: theory -> sort -> Proof.context -> Proof.context
haftmann@25485
    29
haftmann@25462
    30
  (*instances*)
haftmann@26247
    31
  val init_instantiation: string list * (string * sort) list * sort
haftmann@26247
    32
    -> theory -> local_theory
haftmann@31869
    33
  val instance_arity_cmd: xstring list * xstring list * xstring -> theory -> Proof.state
haftmann@26247
    34
  val instantiation_instance: (local_theory -> local_theory)
haftmann@26247
    35
    -> local_theory -> Proof.state
haftmann@26247
    36
  val prove_instantiation_instance: (Proof.context -> tactic)
haftmann@26247
    37
    -> local_theory -> local_theory
haftmann@28666
    38
  val prove_instantiation_exit: (Proof.context -> tactic)
haftmann@28666
    39
    -> local_theory -> theory
haftmann@28666
    40
  val prove_instantiation_exit_result: (morphism -> 'a -> 'b)
haftmann@28666
    41
    -> (Proof.context -> 'b -> tactic) -> 'a -> local_theory -> 'b * theory
haftmann@25485
    42
  val conclude_instantiation: local_theory -> local_theory
haftmann@30519
    43
  val instantiation_param: local_theory -> binding -> string option
haftmann@30519
    44
  val confirm_declaration: binding -> local_theory -> local_theory
haftmann@25603
    45
  val pretty_instantiation: local_theory -> Pretty.T
haftmann@31869
    46
  val read_multi_arity: theory -> xstring list * xstring list * xstring
haftmann@31869
    47
    -> string list * (string * sort) list * sort
haftmann@26259
    48
  val type_name: string -> string
haftmann@25485
    49
haftmann@31635
    50
  (*subclasses*)
haftmann@31635
    51
  val register_subclass: class * class -> morphism option -> Element.witness option
haftmann@31635
    52
    -> morphism -> theory -> theory
haftmann@31635
    53
  val classrel: class * class -> theory -> Proof.state
haftmann@31635
    54
  val classrel_cmd: xstring * xstring -> theory -> Proof.state
haftmann@31635
    55
haftmann@31635
    56
  (*tactics*)
haftmann@29526
    57
  val intro_classes_tac: thm list -> tactic
haftmann@29526
    58
  val default_intro_tac: Proof.context -> thm list -> tactic
haftmann@29526
    59
haftmann@25462
    60
  (*old axclass layer*)
wenzelm@30344
    61
  val axclass_cmd: binding * xstring list
wenzelm@28084
    62
    -> (Attrib.binding * string list) list
haftmann@25462
    63
    -> theory -> class * theory
haftmann@24218
    64
end;
haftmann@24218
    65
haftmann@29358
    66
structure Class_Target : CLASS_TARGET =
haftmann@24218
    67
struct
haftmann@24218
    68
haftmann@24589
    69
(** class data **)
haftmann@24218
    70
haftmann@24218
    71
datatype class_data = ClassData of {
haftmann@28715
    72
haftmann@28715
    73
  (* static part *)
haftmann@24218
    74
  consts: (string * string) list
haftmann@24836
    75
    (*locale parameter ~> constant name*),
haftmann@25062
    76
  base_sort: sort,
haftmann@29545
    77
  base_morph: morphism
haftmann@29439
    78
    (*static part of canonical morphism*),
haftmann@32850
    79
  export_morph: morphism,
haftmann@25618
    80
  assm_intro: thm option,
haftmann@25618
    81
  of_class: thm,
haftmann@25618
    82
  axiom: thm option,
haftmann@28715
    83
  
haftmann@28715
    84
  (* dynamic part *)
haftmann@24657
    85
  defs: thm list,
haftmann@25368
    86
  operations: (string * (class * (typ * term))) list
haftmann@28715
    87
haftmann@24657
    88
};
haftmann@24218
    89
haftmann@32850
    90
fun make_class_data ((consts, base_sort, base_morph, export_morph, assm_intro, of_class, axiom),
haftmann@25368
    91
    (defs, operations)) =
haftmann@29526
    92
  ClassData { consts = consts, base_sort = base_sort,
haftmann@32850
    93
    base_morph = base_morph, export_morph = export_morph, assm_intro = assm_intro,
haftmann@32850
    94
    of_class = of_class, axiom = axiom, defs = defs, operations = operations };
haftmann@32850
    95
fun map_class_data f (ClassData { consts, base_sort, base_morph, export_morph, assm_intro,
haftmann@28715
    96
    of_class, axiom, defs, operations }) =
haftmann@32850
    97
  make_class_data (f ((consts, base_sort, base_morph, export_morph, assm_intro, of_class, axiom),
haftmann@25368
    98
    (defs, operations)));
haftmann@25038
    99
fun merge_class_data _ (ClassData { consts = consts,
haftmann@32850
   100
    base_sort = base_sort, base_morph = base_morph, export_morph = export_morph, assm_intro = assm_intro,
haftmann@25618
   101
    of_class = of_class, axiom = axiom, defs = defs1, operations = operations1 },
haftmann@32850
   102
  ClassData { consts = _, base_sort = _, base_morph = _, export_morph = _, assm_intro = _,
haftmann@25618
   103
    of_class = _, axiom = _, defs = defs2, operations = operations2 }) =
haftmann@32850
   104
  make_class_data ((consts, base_sort, base_morph, export_morph, assm_intro, of_class, axiom),
haftmann@24914
   105
    (Thm.merge_thms (defs1, defs2),
haftmann@25368
   106
      AList.merge (op =) (K true) (operations1, operations2)));
haftmann@24218
   107
wenzelm@33522
   108
structure ClassData = Theory_Data
haftmann@24218
   109
(
haftmann@25038
   110
  type T = class_data Graph.T
haftmann@25038
   111
  val empty = Graph.empty;
haftmann@24218
   112
  val extend = I;
wenzelm@33522
   113
  val merge = Graph.join merge_class_data;
haftmann@24218
   114
);
haftmann@24218
   115
haftmann@24218
   116
haftmann@24218
   117
(* queries *)
haftmann@24218
   118
haftmann@31599
   119
fun lookup_class_data thy class = case try (Graph.get_node (ClassData.get thy)) class
haftmann@31599
   120
 of SOME (ClassData data) => SOME data
haftmann@31599
   121
  | NONE => NONE;
haftmann@24218
   122
haftmann@24589
   123
fun the_class_data thy class = case lookup_class_data thy class
wenzelm@25020
   124
 of NONE => error ("Undeclared class " ^ quote class)
haftmann@24589
   125
  | SOME data => data;
haftmann@24218
   126
haftmann@25038
   127
val is_class = is_some oo lookup_class_data;
haftmann@25038
   128
haftmann@25038
   129
val ancestry = Graph.all_succs o ClassData.get;
haftmann@29509
   130
val heritage = Graph.all_preds o ClassData.get;
haftmann@29509
   131
haftmann@25002
   132
fun these_params thy =
haftmann@24218
   133
  let
haftmann@24218
   134
    fun params class =
haftmann@24218
   135
      let
wenzelm@24930
   136
        val const_typs = (#params o AxClass.get_info thy) class;
haftmann@24657
   137
        val const_names = (#consts o the_class_data thy) class;
haftmann@24218
   138
      in
haftmann@26518
   139
        (map o apsnd)
haftmann@26518
   140
          (fn c => (class, (c, (the o AList.lookup (op =) const_typs) c))) const_names
haftmann@24218
   141
      end;
haftmann@24218
   142
  in maps params o ancestry thy end;
haftmann@24218
   143
haftmann@29526
   144
val base_sort = #base_sort oo the_class_data;
haftmann@29526
   145
haftmann@29526
   146
fun rules thy class =
haftmann@29526
   147
  let val { axiom, of_class, ... } = the_class_data thy class
haftmann@29526
   148
  in (axiom, of_class) end;
haftmann@29526
   149
haftmann@29526
   150
fun all_assm_intros thy =
haftmann@31599
   151
  Graph.fold (fn (_, (ClassData { assm_intro, ... }, _)) => fold (insert Thm.eq_thm)
haftmann@31599
   152
    (the_list assm_intro)) (ClassData.get thy) [];
haftmann@24218
   153
haftmann@29526
   154
fun these_defs thy = maps (#defs o the_class_data thy) o ancestry thy;
haftmann@29526
   155
fun these_operations thy = maps (#operations o the_class_data thy) o ancestry thy;
haftmann@29358
   156
haftmann@29526
   157
val base_morphism = #base_morph oo the_class_data;
haftmann@29526
   158
fun morphism thy class = base_morphism thy class
haftmann@29526
   159
  $> Element.eq_morphism thy (these_defs thy [class]);
haftmann@32850
   160
val export_morphism = #export_morph oo the_class_data;
haftmann@28715
   161
haftmann@24218
   162
fun print_classes thy =
haftmann@24218
   163
  let
wenzelm@24920
   164
    val ctxt = ProofContext.init thy;
haftmann@24218
   165
    val algebra = Sign.classes_of thy;
haftmann@24218
   166
    val arities =
haftmann@24218
   167
      Symtab.empty
haftmann@24218
   168
      |> Symtab.fold (fn (tyco, arities) => fold (fn (class, _) =>
haftmann@24218
   169
           Symtab.map_default (class, []) (insert (op =) tyco)) arities)
haftmann@24218
   170
             ((#arities o Sorts.rep_algebra) algebra);
haftmann@24218
   171
    val the_arities = these o Symtab.lookup arities;
haftmann@24218
   172
    fun mk_arity class tyco =
haftmann@24218
   173
      let
haftmann@24218
   174
        val Ss = Sorts.mg_domain algebra tyco [class];
wenzelm@24920
   175
      in Syntax.pretty_arity ctxt (tyco, Ss, [class]) end;
haftmann@24218
   176
    fun mk_param (c, ty) = Pretty.str (Sign.extern_const thy c ^ " :: "
wenzelm@32966
   177
      ^ setmp_CRITICAL show_sorts false (Syntax.string_of_typ ctxt o Type.strip_sorts) ty);
haftmann@24218
   178
    fun mk_entry class = (Pretty.block o Pretty.fbreaks o map_filter I) [
haftmann@25062
   179
      (SOME o Pretty.str) ("class " ^ Sign.extern_class thy class ^ ":"),
haftmann@24218
   180
      (SOME o Pretty.block) [Pretty.str "supersort: ",
wenzelm@24920
   181
        (Syntax.pretty_sort ctxt o Sign.minimize_sort thy o Sign.super_classes thy) class],
haftmann@25062
   182
      ((fn [] => NONE | ps => (SOME o Pretty.block o Pretty.fbreaks)
haftmann@25062
   183
          (Pretty.str "parameters:" :: ps)) o map mk_param
wenzelm@24930
   184
        o these o Option.map #params o try (AxClass.get_info thy)) class,
haftmann@24218
   185
      (SOME o Pretty.block o Pretty.breaks) [
haftmann@24218
   186
        Pretty.str "instances:",
haftmann@24218
   187
        Pretty.list "" "" (map (mk_arity class) (the_arities class))
haftmann@24218
   188
      ]
haftmann@24218
   189
    ]
haftmann@24218
   190
  in
haftmann@24589
   191
    (Pretty.writeln o Pretty.chunks o separate (Pretty.str "")
haftmann@24589
   192
      o map mk_entry o Sorts.all_classes) algebra
haftmann@24218
   193
  end;
haftmann@24218
   194
haftmann@24218
   195
haftmann@24218
   196
(* updaters *)
haftmann@24218
   197
haftmann@32850
   198
fun register class sups params base_sort base_morph export_morph
haftmann@29358
   199
    axiom assm_intro of_class thy =
haftmann@25002
   200
  let
haftmann@25368
   201
    val operations = map (fn (v_ty as (_, ty), (c, _)) =>
haftmann@25683
   202
      (c, (class, (ty, Free v_ty)))) params;
haftmann@25038
   203
    val add_class = Graph.new_node (class,
haftmann@31599
   204
        make_class_data (((map o pairself) fst params, base_sort,
haftmann@32850
   205
          base_morph, export_morph, assm_intro, of_class, axiom), ([], operations)))
haftmann@29526
   206
      #> fold (curry Graph.add_edge class) sups;
haftmann@25618
   207
  in ClassData.map add_class thy end;
haftmann@24218
   208
haftmann@29526
   209
fun activate_defs class thms thy =
haftmann@29526
   210
  let
haftmann@29526
   211
    val eq_morph = Element.eq_morphism thy thms;
haftmann@32850
   212
    fun amend cls thy = Locale.amend_registration (cls, base_morphism thy cls)
haftmann@32850
   213
      (eq_morph, true) (export_morphism thy cls) thy;
haftmann@29526
   214
  in fold amend (heritage thy [class]) thy end;
haftmann@29526
   215
haftmann@25368
   216
fun register_operation class (c, (t, some_def)) thy =
haftmann@25062
   217
  let
haftmann@29358
   218
    val base_sort = base_sort thy class;
haftmann@29439
   219
    val prep_typ = map_type_tfree
haftmann@29439
   220
      (fn (v, sort) => if Name.aT = v
haftmann@29439
   221
        then TFree (v, base_sort) else TVar ((v, 0), sort));
haftmann@25368
   222
    val t' = map_types prep_typ t;
haftmann@25368
   223
    val ty' = Term.fastype_of t';
haftmann@25062
   224
  in
haftmann@25062
   225
    thy
haftmann@25062
   226
    |> (ClassData.map o Graph.map_node class o map_class_data o apsnd)
haftmann@25368
   227
      (fn (defs, operations) =>
haftmann@25096
   228
        (fold cons (the_list some_def) defs,
haftmann@25368
   229
          (c, (class, (ty', t'))) :: operations))
haftmann@29526
   230
    |> is_some some_def ? activate_defs class (the_list some_def)
haftmann@25062
   231
  end;
haftmann@24218
   232
haftmann@29558
   233
fun register_subclass (sub, sup) some_dep_morph some_wit export thy =
haftmann@25618
   234
  let
haftmann@29558
   235
    val intros = (snd o rules thy) sup :: map_filter I
haftmann@29558
   236
      [Option.map (Drule.standard' o Element.conclude_witness) some_wit,
haftmann@29558
   237
        (fst o rules thy) sub];
haftmann@29610
   238
    val tac = EVERY (map (TRYALL o Tactic.rtac) intros);
wenzelm@32970
   239
    val classrel = Skip_Proof.prove_global thy [] [] (Logic.mk_classrel (sub, sup))
haftmann@29558
   240
      (K tac);
haftmann@29509
   241
    val diff_sort = Sign.complete_sort thy [sup]
haftmann@31012
   242
      |> subtract (op =) (Sign.complete_sort thy [sub])
haftmann@31012
   243
      |> filter (is_class thy);
haftmann@32074
   244
    val add_dependency = case some_dep_morph
haftmann@32074
   245
     of SOME dep_morph => Locale.add_dependency sub
haftmann@32074
   246
          (sup, dep_morph $> Element.satisfy_morphism (the_list some_wit)) export
haftmann@32074
   247
      | NONE => I
haftmann@25618
   248
  in
haftmann@25618
   249
    thy
haftmann@25618
   250
    |> AxClass.add_classrel classrel
haftmann@25618
   251
    |> ClassData.map (Graph.add_edge (sub, sup))
haftmann@29526
   252
    |> activate_defs sub (these_defs thy diff_sort)
haftmann@32074
   253
    |> add_dependency
haftmann@24218
   254
  end;
haftmann@24218
   255
haftmann@24218
   256
haftmann@24589
   257
(** classes and class target **)
haftmann@24218
   258
haftmann@25002
   259
(* class context syntax *)
haftmann@24748
   260
haftmann@29577
   261
fun these_unchecks thy =
haftmann@29577
   262
  map (fn (c, (_, (ty, t))) => (t, Const (c, ty))) o these_operations thy;
haftmann@29577
   263
haftmann@29632
   264
fun redeclare_const thy c =
wenzelm@30364
   265
  let val b = Long_Name.base_name c
haftmann@29632
   266
  in Sign.intern_const thy b = c ? Variable.declare_const (b, c) end;
haftmann@29632
   267
haftmann@29577
   268
fun synchronize_class_syntax sort base_sort ctxt =
haftmann@24914
   269
  let
wenzelm@25344
   270
    val thy = ProofContext.theory_of ctxt;
haftmann@26596
   271
    val algebra = Sign.classes_of thy;
haftmann@29577
   272
    val operations = these_operations thy sort;
haftmann@26518
   273
    fun subst_class_typ sort = map_type_tfree (K (TVar ((Name.aT, 0), sort)));
haftmann@26518
   274
    val primary_constraints =
haftmann@25368
   275
      (map o apsnd) (subst_class_typ base_sort o fst o snd) operations;
haftmann@26518
   276
    val secondary_constraints =
haftmann@25368
   277
      (map o apsnd) (fn (class, (ty, _)) => subst_class_typ [class] ty) operations;
haftmann@26518
   278
    fun improve (c, ty) = (case AList.lookup (op =) primary_constraints c
haftmann@26238
   279
     of SOME ty' => (case try (Type.raw_match (ty', ty)) Vartab.empty
haftmann@26238
   280
         of SOME tyenv => (case Vartab.lookup tyenv (Name.aT, 0)
haftmann@26596
   281
             of SOME (_, ty' as TVar (tvar as (vi, sort))) =>
haftmann@26238
   282
                  if TypeInfer.is_param vi
haftmann@26596
   283
                    andalso Sorts.sort_le algebra (base_sort, sort)
haftmann@26596
   284
                      then SOME (ty', TFree (Name.aT, base_sort))
haftmann@26596
   285
                      else NONE
haftmann@26238
   286
              | _ => NONE)
haftmann@26238
   287
          | NONE => NONE)
haftmann@26238
   288
      | NONE => NONE)
haftmann@26238
   289
    fun subst (c, ty) = Option.map snd (AList.lookup (op =) operations c);
haftmann@29577
   290
    val unchecks = these_unchecks thy sort;
haftmann@25083
   291
  in
haftmann@25083
   292
    ctxt
haftmann@29632
   293
    |> fold (redeclare_const thy o fst) primary_constraints
haftmann@26518
   294
    |> Overloading.map_improvable_syntax (K (((primary_constraints, secondary_constraints),
haftmann@26730
   295
        (((improve, subst), true), unchecks)), false))
haftmann@26518
   296
    |> Overloading.set_primary_constraints
haftmann@25083
   297
  end;
haftmann@25083
   298
haftmann@25083
   299
fun refresh_syntax class ctxt =
haftmann@25002
   300
  let
haftmann@25002
   301
    val thy = ProofContext.theory_of ctxt;
haftmann@29358
   302
    val base_sort = base_sort thy class;
haftmann@26238
   303
  in synchronize_class_syntax [class] base_sort ctxt end;
haftmann@25002
   304
haftmann@29632
   305
fun redeclare_operations thy sort =
haftmann@29632
   306
  fold (redeclare_const thy o fst) (these_operations thy sort);
haftmann@29632
   307
haftmann@29577
   308
fun begin sort base_sort ctxt =
haftmann@25083
   309
  ctxt
haftmann@25083
   310
  |> Variable.declare_term
haftmann@25083
   311
      (Logic.mk_type (TFree (Name.aT, base_sort)))
haftmann@29577
   312
  |> synchronize_class_syntax sort base_sort
haftmann@26238
   313
  |> Overloading.add_improvable_syntax;
haftmann@24901
   314
haftmann@25311
   315
fun init class thy =
haftmann@25311
   316
  thy
haftmann@29509
   317
  |> Locale.init class
haftmann@29358
   318
  |> begin [class] (base_sort thy class);
haftmann@24914
   319
haftmann@24748
   320
haftmann@27690
   321
(* class target *)
haftmann@27690
   322
wenzelm@30364
   323
val class_prefix = Logic.const_of_class o Long_Name.base_name;
haftmann@29526
   324
wenzelm@33173
   325
fun declare class ((c, mx), dict) thy =
haftmann@27690
   326
  let
haftmann@29577
   327
    val morph = morphism thy class;
wenzelm@30344
   328
    val b = Morphism.binding morph c;
wenzelm@30344
   329
    val b_def = Morphism.binding morph (Binding.suffix_name "_dict" b);
haftmann@29577
   330
    val c' = Sign.full_name thy b;
haftmann@29439
   331
    val dict' = Morphism.term morph dict;
haftmann@29439
   332
    val ty' = Term.fastype_of dict';
haftmann@29577
   333
    val def_eq = Logic.mk_equals (Const (c', ty'), dict')
haftmann@29577
   334
      |> map_types Type.strip_sorts;
haftmann@27690
   335
  in
haftmann@29577
   336
    thy
wenzelm@33173
   337
    |> Sign.declare_const ((b, Type.strip_sorts ty'), mx)
haftmann@29577
   338
    |> snd
haftmann@29577
   339
    |> Thm.add_def false false (b_def, def_eq)
haftmann@29577
   340
    |>> Thm.varifyT
haftmann@29577
   341
    |-> (fn def_thm => PureThy.store_thm (b_def, def_thm)
haftmann@29577
   342
      #> snd
haftmann@29577
   343
      #> register_operation class (c', (dict', SOME (Thm.symmetric def_thm))))
haftmann@27690
   344
    |> Sign.add_const_constraint (c', SOME ty')
haftmann@27690
   345
  end;
haftmann@27690
   346
wenzelm@33173
   347
fun abbrev class prmode ((c, mx), rhs) thy =
haftmann@27690
   348
  let
haftmann@29577
   349
    val morph = morphism thy class;
haftmann@29577
   350
    val unchecks = these_unchecks thy [class];
wenzelm@30344
   351
    val b = Morphism.binding morph c;
haftmann@29577
   352
    val c' = Sign.full_name thy b;
haftmann@27690
   353
    val rhs' = Pattern.rewrite_term thy unchecks [] rhs;
haftmann@27690
   354
    val ty' = Term.fastype_of rhs';
haftmann@31697
   355
    val rhs'' = map_types Logic.varifyT rhs';
haftmann@27690
   356
  in
haftmann@29577
   357
    thy
wenzelm@33173
   358
    |> Sign.add_abbrev (#1 prmode) (b, rhs'')
haftmann@29577
   359
    |> snd
haftmann@27690
   360
    |> Sign.add_const_constraint (c', SOME ty')
haftmann@27690
   361
    |> Sign.notation true prmode [(Const (c', ty'), mx)]
haftmann@27690
   362
    |> not (#1 prmode = PrintMode.input) ? register_operation class (c', (rhs', NONE))
haftmann@27690
   363
  end;
haftmann@27690
   364
haftmann@27690
   365
haftmann@31635
   366
(* simple subclasses *)
haftmann@31635
   367
haftmann@31635
   368
local
haftmann@31635
   369
haftmann@31635
   370
fun gen_classrel mk_prop classrel thy =
haftmann@31635
   371
  let
haftmann@31635
   372
    fun after_qed results =
haftmann@31635
   373
      ProofContext.theory ((fold o fold) AxClass.add_classrel results);
haftmann@31635
   374
  in
haftmann@31635
   375
    thy
haftmann@31635
   376
    |> ProofContext.init
haftmann@31635
   377
    |> Proof.theorem_i NONE after_qed [[(mk_prop thy classrel, [])]]
haftmann@31635
   378
  end;
haftmann@31635
   379
haftmann@31635
   380
in
haftmann@31635
   381
haftmann@31635
   382
val classrel =
haftmann@31635
   383
  gen_classrel (Logic.mk_classrel oo AxClass.cert_classrel);
haftmann@31635
   384
val classrel_cmd =
haftmann@31635
   385
  gen_classrel (Logic.mk_classrel oo AxClass.read_classrel);
haftmann@31635
   386
haftmann@31635
   387
end; (*local*)
haftmann@31635
   388
haftmann@31635
   389
haftmann@25462
   390
(** instantiation target **)
haftmann@25462
   391
haftmann@25462
   392
(* bookkeeping *)
haftmann@25462
   393
haftmann@25462
   394
datatype instantiation = Instantiation of {
haftmann@25864
   395
  arities: string list * (string * sort) list * sort,
haftmann@25462
   396
  params: ((string * string) * (string * typ)) list
haftmann@25603
   397
    (*(instantiation parameter, type constructor), (local instantiation parameter, typ)*)
haftmann@25462
   398
}
haftmann@25462
   399
wenzelm@33519
   400
structure Instantiation = Proof_Data
haftmann@25462
   401
(
haftmann@25462
   402
  type T = instantiation
haftmann@25536
   403
  fun init _ = Instantiation { arities = ([], [], []), params = [] };
haftmann@25462
   404
);
haftmann@25462
   405
haftmann@25485
   406
fun mk_instantiation (arities, params) =
haftmann@25485
   407
  Instantiation { arities = arities, params = params };
wenzelm@33671
   408
fun get_instantiation lthy = case Instantiation.get (Local_Theory.target_of lthy)
haftmann@25485
   409
 of Instantiation data => data;
wenzelm@33671
   410
fun map_instantiation f = (Local_Theory.target o Instantiation.map)
haftmann@25514
   411
  (fn Instantiation { arities, params } => mk_instantiation (f (arities, params)));
haftmann@25462
   412
haftmann@25514
   413
fun the_instantiation lthy = case get_instantiation lthy
haftmann@25536
   414
 of { arities = ([], [], []), ... } => error "No instantiation target"
haftmann@25485
   415
  | data => data;
haftmann@25462
   416
haftmann@25485
   417
val instantiation_params = #params o get_instantiation;
haftmann@25462
   418
haftmann@30519
   419
fun instantiation_param lthy b = instantiation_params lthy
haftmann@30519
   420
  |> find_first (fn (_, (v, _)) => Binding.name_of b = v)
haftmann@25462
   421
  |> Option.map (fst o fst);
haftmann@25462
   422
haftmann@31869
   423
fun read_multi_arity thy (raw_tycos, raw_sorts, raw_sort) =
haftmann@31869
   424
  let
haftmann@31869
   425
    val all_arities = map (fn raw_tyco => Sign.read_arity thy
haftmann@31869
   426
      (raw_tyco, raw_sorts, raw_sort)) raw_tycos;
haftmann@31869
   427
    val tycos = map #1 all_arities;
haftmann@31869
   428
    val (_, sorts, sort) = hd all_arities;
haftmann@31869
   429
    val vs = Name.names Name.context Name.aT sorts;
haftmann@31869
   430
  in (tycos, vs, sort) end;
haftmann@31869
   431
haftmann@25462
   432
haftmann@25462
   433
(* syntax *)
haftmann@25462
   434
haftmann@26238
   435
fun synchronize_inst_syntax ctxt =
haftmann@25462
   436
  let
haftmann@26259
   437
    val Instantiation { arities = (_, _, sort), params = params } = Instantiation.get ctxt;
haftmann@31249
   438
haftmann@31249
   439
    val lookup_inst_param = AxClass.lookup_inst_param (Sign.consts_of (ProofContext.theory_of ctxt)) params;
haftmann@31249
   440
    fun subst (c, ty) = case lookup_inst_param (c, ty)
haftmann@31249
   441
     of SOME (v_ty as (_, ty)) => SOME (ty, Free v_ty)
haftmann@31249
   442
      | NONE => NONE;
haftmann@26238
   443
    val unchecks =
haftmann@26238
   444
      map (fn ((c, _), v_ty as (_, ty)) => (Free v_ty, Const (c, ty))) params;
haftmann@26238
   445
  in
haftmann@26238
   446
    ctxt
haftmann@26259
   447
    |> Overloading.map_improvable_syntax
haftmann@26730
   448
         (fn (((primary_constraints, _), (((improve, _), _), _)), _) =>
haftmann@26730
   449
            (((primary_constraints, []), (((improve, subst), false), unchecks)), false))
haftmann@26238
   450
  end;
haftmann@25462
   451
haftmann@25462
   452
haftmann@25462
   453
(* target *)
haftmann@25462
   454
haftmann@25485
   455
val sanatize_name = (*FIXME*)
haftmann@25485
   456
  let
haftmann@25574
   457
    fun is_valid s = Symbol.is_ascii_letter s orelse Symbol.is_ascii_digit s
haftmann@25574
   458
      orelse s = "'" orelse s = "_";
haftmann@25485
   459
    val is_junk = not o is_valid andf Symbol.is_regular;
haftmann@25485
   460
    val junk = Scan.many is_junk;
haftmann@25485
   461
    val scan_valids = Symbol.scanner "Malformed input"
haftmann@25485
   462
      ((junk |--
haftmann@25485
   463
        (Scan.optional (Scan.one Symbol.is_ascii_letter) "x" ^^ (Scan.many is_valid >> implode)
haftmann@25485
   464
        --| junk))
wenzelm@25999
   465
      ::: Scan.repeat ((Scan.many1 is_valid >> implode) --| junk));
haftmann@25485
   466
  in
haftmann@25485
   467
    explode #> scan_valids #> implode
haftmann@25485
   468
  end;
haftmann@25485
   469
haftmann@26259
   470
fun type_name "*" = "prod"
haftmann@26259
   471
  | type_name "+" = "sum"
wenzelm@30364
   472
  | type_name s = sanatize_name (Long_Name.base_name s);
haftmann@26259
   473
haftmann@26518
   474
fun resort_terms pp algebra consts constraints ts =
haftmann@26518
   475
  let
haftmann@26518
   476
    fun matchings (Const (c_ty as (c, _))) = (case constraints c
haftmann@26518
   477
         of NONE => I
haftmann@26518
   478
          | SOME sorts => fold2 (curry (Sorts.meet_sort algebra))
haftmann@26518
   479
              (Consts.typargs consts c_ty) sorts)
haftmann@26518
   480
      | matchings _ = I
haftmann@26518
   481
    val tvartab = (fold o fold_aterms) matchings ts Vartab.empty
wenzelm@26642
   482
      handle Sorts.CLASS_ERROR e => error (Sorts.class_error pp e);
haftmann@27089
   483
    val inst = map_type_tvar
haftmann@27089
   484
      (fn (vi, sort) => TVar (vi, the_default sort (Vartab.lookup tvartab vi)));
haftmann@26518
   485
  in if Vartab.is_empty tvartab then NONE else SOME ((map o map_types) inst ts) end;
haftmann@26518
   486
haftmann@25864
   487
fun init_instantiation (tycos, vs, sort) thy =
haftmann@25462
   488
  let
haftmann@25536
   489
    val _ = if null tycos then error "At least one arity must be given" else ();
haftmann@31249
   490
    val class_params = these_params thy (filter (can (AxClass.get_info thy)) sort);
haftmann@29632
   491
    fun get_param tyco (param, (_, (c, ty))) =
haftmann@29632
   492
      if can (AxClass.param_of_inst thy) (c, tyco)
haftmann@25603
   493
      then NONE else SOME ((c, tyco),
haftmann@25864
   494
        (param ^ "_" ^ type_name tyco, map_atyps (K (Type (tyco, map TFree vs))) ty));
haftmann@31249
   495
    val params = map_product get_param tycos class_params |> map_filter I;
haftmann@26518
   496
    val primary_constraints = map (apsnd
haftmann@31249
   497
      (map_atyps (K (TVar ((Name.aT, 0), [])))) o snd o snd) class_params;
wenzelm@26939
   498
    val pp = Syntax.pp_global thy;
haftmann@26518
   499
    val algebra = Sign.classes_of thy
haftmann@26518
   500
      |> fold (fn tyco => Sorts.add_arities pp
haftmann@26518
   501
            (tyco, map (fn class => (class, map snd vs)) sort)) tycos;
haftmann@26518
   502
    val consts = Sign.consts_of thy;
haftmann@26518
   503
    val improve_constraints = AList.lookup (op =)
haftmann@31249
   504
      (map (fn (_, (class, (c, _))) => (c, [[class]])) class_params);
haftmann@26518
   505
    fun resort_check ts ctxt = case resort_terms pp algebra consts improve_constraints ts
haftmann@26518
   506
     of NONE => NONE
haftmann@26518
   507
      | SOME ts' => SOME (ts', ctxt);
haftmann@31249
   508
    val lookup_inst_param = AxClass.lookup_inst_param consts params;
haftmann@31210
   509
    val typ_instance = Type.typ_instance (Sign.tsig_of thy);
haftmann@31249
   510
    fun improve (c, ty) = case lookup_inst_param (c, ty)
haftmann@31249
   511
     of SOME (_, ty') => if typ_instance (ty', ty) then SOME (ty, ty') else NONE
haftmann@26259
   512
      | NONE => NONE;
haftmann@25485
   513
  in
haftmann@25485
   514
    thy
haftmann@32379
   515
    |> Theory.checkpoint
haftmann@25485
   516
    |> ProofContext.init
haftmann@31249
   517
    |> Instantiation.put (mk_instantiation ((tycos, vs, sort), params))
wenzelm@27281
   518
    |> fold (Variable.declare_typ o TFree) vs
haftmann@31249
   519
    |> fold (Variable.declare_names o Free o snd) params
haftmann@26259
   520
    |> (Overloading.map_improvable_syntax o apfst)
haftmann@29969
   521
         (K ((primary_constraints, []), (((improve, K NONE), false), [])))
haftmann@26259
   522
    |> Overloading.add_improvable_syntax
haftmann@26518
   523
    |> Context.proof_map (Syntax.add_term_check 0 "resorting" resort_check)
haftmann@26238
   524
    |> synchronize_inst_syntax
haftmann@25485
   525
  end;
haftmann@25485
   526
haftmann@30519
   527
fun confirm_declaration b = (map_instantiation o apsnd)
haftmann@30519
   528
  (filter_out (fn (_, (c', _)) => c' = Binding.name_of b))
wenzelm@33671
   529
  #> Local_Theory.target synchronize_inst_syntax
haftmann@26238
   530
haftmann@25485
   531
fun gen_instantiation_instance do_proof after_qed lthy =
haftmann@25485
   532
  let
haftmann@25864
   533
    val (tycos, vs, sort) = (#arities o the_instantiation) lthy;
haftmann@25864
   534
    val arities_proof = maps (fn tyco => Logic.mk_arities (tyco, map snd vs, sort)) tycos;
haftmann@25462
   535
    fun after_qed' results =
wenzelm@33671
   536
      Local_Theory.theory (fold (AxClass.add_arity o Thm.varifyT) results)
haftmann@25462
   537
      #> after_qed;
haftmann@25462
   538
  in
haftmann@25462
   539
    lthy
haftmann@25462
   540
    |> do_proof after_qed' arities_proof
haftmann@25462
   541
  end;
haftmann@25462
   542
haftmann@25485
   543
val instantiation_instance = gen_instantiation_instance (fn after_qed => fn ts =>
haftmann@25462
   544
  Proof.theorem_i NONE (after_qed o map the_single) (map (fn t => [(t, [])]) ts));
haftmann@25462
   545
haftmann@25485
   546
fun prove_instantiation_instance tac = gen_instantiation_instance (fn after_qed =>
haftmann@25502
   547
  fn ts => fn lthy => after_qed (map (fn t => Goal.prove lthy [] [] t
haftmann@25502
   548
    (fn {context, ...} => tac context)) ts) lthy) I;
haftmann@25462
   549
haftmann@28666
   550
fun prove_instantiation_exit tac = prove_instantiation_instance tac
wenzelm@33671
   551
  #> Local_Theory.exit_global;
haftmann@28666
   552
haftmann@28666
   553
fun prove_instantiation_exit_result f tac x lthy =
haftmann@28666
   554
  let
haftmann@29439
   555
    val morph = ProofContext.export_morphism lthy
haftmann@28666
   556
      (ProofContext.init (ProofContext.theory_of lthy));
haftmann@29439
   557
    val y = f morph x;
haftmann@28666
   558
  in
haftmann@28666
   559
    lthy
haftmann@28666
   560
    |> prove_instantiation_exit (fn ctxt => tac ctxt y)
haftmann@28666
   561
    |> pair y
haftmann@28666
   562
  end;
haftmann@28666
   563
haftmann@25462
   564
fun conclude_instantiation lthy =
haftmann@25462
   565
  let
haftmann@25485
   566
    val { arities, params } = the_instantiation lthy;
haftmann@25864
   567
    val (tycos, vs, sort) = arities;
haftmann@25462
   568
    val thy = ProofContext.theory_of lthy;
haftmann@25597
   569
    val _ = map (fn tyco => if Sign.of_sort thy
haftmann@25864
   570
        (Type (tyco, map TFree vs), sort)
haftmann@25462
   571
      then () else error ("Missing instance proof for type " ^ quote (Sign.extern_type thy tyco)))
haftmann@25597
   572
        tycos;
haftmann@25597
   573
  in lthy end;
haftmann@25462
   574
haftmann@25603
   575
fun pretty_instantiation lthy =
haftmann@25603
   576
  let
haftmann@25603
   577
    val { arities, params } = the_instantiation lthy;
haftmann@25864
   578
    val (tycos, vs, sort) = arities;
haftmann@25603
   579
    val thy = ProofContext.theory_of lthy;
haftmann@25864
   580
    fun pr_arity tyco = Syntax.pretty_arity lthy (tyco, map snd vs, sort);
haftmann@25603
   581
    fun pr_param ((c, _), (v, ty)) =
haftmann@25864
   582
      (Pretty.block o Pretty.breaks) [Pretty.str v, Pretty.str "==",
wenzelm@26939
   583
        (Pretty.str o Sign.extern_const thy) c, Pretty.str "::", Syntax.pretty_typ_global thy ty];
haftmann@25603
   584
  in
haftmann@25603
   585
    (Pretty.block o Pretty.fbreaks)
haftmann@25603
   586
      (Pretty.str "instantiation" :: map pr_arity tycos @ map pr_param params)
haftmann@25603
   587
  end;
haftmann@25603
   588
haftmann@29526
   589
haftmann@31635
   590
(* simplified instantiation interface with no class parameter *)
haftmann@31635
   591
haftmann@31869
   592
fun instance_arity_cmd raw_arities thy =
haftmann@31635
   593
  let
haftmann@31869
   594
    val (tycos, vs, sort) = read_multi_arity thy raw_arities;
haftmann@31869
   595
    val sorts = map snd vs;
haftmann@31869
   596
    val arities = maps (fn tyco => Logic.mk_arities (tyco, sorts, sort)) tycos;
haftmann@31635
   597
    fun after_qed results = ProofContext.theory
haftmann@31635
   598
      ((fold o fold) AxClass.add_arity results);
haftmann@31635
   599
  in
haftmann@31635
   600
    thy
haftmann@31635
   601
    |> ProofContext.init
haftmann@31869
   602
    |> Proof.theorem_i NONE after_qed (map (fn t => [(t, [])]) arities)
haftmann@31635
   603
  end;
haftmann@31635
   604
haftmann@31635
   605
haftmann@29526
   606
(** tactics and methods **)
haftmann@29526
   607
haftmann@29526
   608
fun intro_classes_tac facts st =
haftmann@29526
   609
  let
haftmann@29526
   610
    val thy = Thm.theory_of_thm st;
haftmann@29526
   611
    val classes = Sign.all_classes thy;
haftmann@29526
   612
    val class_trivs = map (Thm.class_triv thy) classes;
haftmann@29526
   613
    val class_intros = map_filter (try (#intro o AxClass.get_info thy)) classes;
haftmann@29526
   614
    val assm_intros = all_assm_intros thy;
haftmann@29526
   615
  in
haftmann@29526
   616
    Method.intros_tac (class_trivs @ class_intros @ assm_intros) facts st
haftmann@29526
   617
  end;
haftmann@29526
   618
haftmann@29526
   619
fun default_intro_tac ctxt [] =
haftmann@29577
   620
      intro_classes_tac [] ORELSE Locale.intro_locales_tac true ctxt []
haftmann@29526
   621
  | default_intro_tac _ _ = no_tac;
haftmann@29526
   622
haftmann@29526
   623
fun default_tac rules ctxt facts =
haftmann@29526
   624
  HEADGOAL (Method.some_rule_tac rules ctxt facts) ORELSE
haftmann@29526
   625
    default_intro_tac ctxt facts;
haftmann@29526
   626
haftmann@29526
   627
val _ = Context.>> (Context.map_theory
wenzelm@30515
   628
 (Method.setup (Binding.name "intro_classes") (Scan.succeed (K (METHOD intro_classes_tac)))
wenzelm@30515
   629
    "back-chain introduction rules of classes" #>
wenzelm@30515
   630
  Method.setup (Binding.name "default") (Attrib.thms >> (METHOD oo default_tac))
wenzelm@30515
   631
    "apply some intro/elim rule"));
haftmann@29526
   632
haftmann@31635
   633
haftmann@31635
   634
(** old axclass command **)
haftmann@31635
   635
haftmann@31635
   636
fun axclass_cmd (class, raw_superclasses) raw_specs thy =
haftmann@31635
   637
  let
haftmann@31635
   638
    val _ = Output.legacy_feature "command \"axclass\" deprecated; use \"class\" instead.";
haftmann@31635
   639
    val ctxt = ProofContext.init thy;
haftmann@31635
   640
    val superclasses = map (Sign.read_class thy) raw_superclasses;
haftmann@31635
   641
    val name_atts = map ((apsnd o map) (Attrib.attribute thy) o fst)
haftmann@31635
   642
      raw_specs;
haftmann@31635
   643
    val axiomss = ProofContext.read_propp (ctxt, map (map (rpair []) o snd)
haftmann@31635
   644
          raw_specs)
haftmann@31635
   645
      |> snd
haftmann@31635
   646
      |> (map o map) fst;
haftmann@31635
   647
  in
haftmann@31635
   648
    AxClass.define_class (class, superclasses) []
haftmann@31635
   649
      (name_atts ~~ axiomss) thy
haftmann@31635
   650
  end;
haftmann@31635
   651
haftmann@24218
   652
end;
haftmann@25683
   653