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