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