src/Pure/Isar/class_target.ML
author haftmann
Mon Jan 05 15:36:24 2009 +0100 (2009-01-05 ago)
changeset 29358 efdfe5dfe008
parent 29333 src/Pure/Isar/class.ML@496b94152b55
child 29360 a5be60c3674e
permissions -rw-r--r--
rearranged target theories
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
  type raw_morphism = morphism * ((typ Vartab.table * typ list) *  (term Vartab.table * term list));
haftmann@29358
    11
  val register: class -> class list -> ((string * typ) * (string * typ)) list
haftmann@29358
    12
    -> sort -> term list -> raw_morphism
haftmann@29358
    13
    -> thm option -> thm option -> thm -> theory -> theory
haftmann@25485
    14
haftmann@29358
    15
  val begin: class list -> sort -> Proof.context -> Proof.context
haftmann@25311
    16
  val init: class -> theory -> Proof.context
wenzelm@28017
    17
  val declare: class -> Properties.T
haftmann@27690
    18
    -> (string * mixfix) * term -> theory -> theory
wenzelm@28017
    19
  val abbrev: class -> Syntax.mode -> Properties.T
wenzelm@25104
    20
    -> (string * mixfix) * term -> theory -> theory
haftmann@25083
    21
  val refresh_syntax: class -> Proof.context -> Proof.context
haftmann@25485
    22
haftmann@24589
    23
  val intro_classes_tac: thm list -> tactic
haftmann@26470
    24
  val default_intro_tac: Proof.context -> thm list -> tactic
haftmann@29358
    25
  val activate_class_morphism: theory -> class -> term list
haftmann@29358
    26
    -> raw_morphism -> morphism
haftmann@29358
    27
  val prove_class_interpretation: class -> term list -> (class * string) list
haftmann@29358
    28
    -> thm list -> thm list -> theory -> raw_morphism * theory
haftmann@29358
    29
  val prove_subclass_relation: class * class -> thm option -> theory -> theory
haftmann@25485
    30
haftmann@25485
    31
  val class_prefix: string -> string
haftmann@25485
    32
  val is_class: theory -> class -> bool
haftmann@26518
    33
  val these_params: theory -> sort -> (string * (class * (string * typ))) list
haftmann@29358
    34
  val these_defs: theory -> sort -> thm list
haftmann@29358
    35
  val base_sort: theory -> class -> sort
haftmann@29358
    36
  val rules: theory -> class -> thm option * thm
haftmann@24589
    37
  val print_classes: theory -> unit
haftmann@24423
    38
haftmann@25462
    39
  (*instances*)
haftmann@26247
    40
  val init_instantiation: string list * (string * sort) list * sort
haftmann@26247
    41
    -> theory -> local_theory
haftmann@26247
    42
  val instantiation_instance: (local_theory -> local_theory)
haftmann@26247
    43
    -> local_theory -> Proof.state
haftmann@26247
    44
  val prove_instantiation_instance: (Proof.context -> tactic)
haftmann@26247
    45
    -> local_theory -> local_theory
haftmann@28666
    46
  val prove_instantiation_exit: (Proof.context -> tactic)
haftmann@28666
    47
    -> local_theory -> theory
haftmann@28666
    48
  val prove_instantiation_exit_result: (morphism -> 'a -> 'b)
haftmann@28666
    49
    -> (Proof.context -> 'b -> tactic) -> 'a -> local_theory -> 'b * theory
haftmann@25485
    50
  val conclude_instantiation: local_theory -> local_theory
haftmann@25603
    51
  val instantiation_param: local_theory -> string -> string option
haftmann@25485
    52
  val confirm_declaration: string -> local_theory -> local_theory
haftmann@25603
    53
  val pretty_instantiation: local_theory -> Pretty.T
haftmann@26259
    54
  val type_name: string -> string
haftmann@25485
    55
haftmann@25462
    56
  (*old axclass layer*)
haftmann@25462
    57
  val axclass_cmd: bstring * xstring list
wenzelm@28084
    58
    -> (Attrib.binding * string list) list
haftmann@25462
    59
    -> theory -> class * theory
haftmann@25462
    60
  val classrel_cmd: xstring * xstring -> theory -> Proof.state
haftmann@25462
    61
haftmann@25462
    62
  (*old instance layer*)
haftmann@25536
    63
  val instance_arity: (theory -> theory) -> arity -> theory -> Proof.state
haftmann@25536
    64
  val instance_arity_cmd: bstring * xstring list * xstring -> theory -> Proof.state
haftmann@24218
    65
end;
haftmann@24218
    66
haftmann@29358
    67
structure Class_Target : CLASS_TARGET =
haftmann@24218
    68
struct
haftmann@24218
    69
haftmann@29133
    70
(*temporary adaption code to mediate between old and new locale code*)
haftmann@29133
    71
haftmann@29133
    72
structure Old_Locale =
haftmann@29133
    73
struct
haftmann@29133
    74
haftmann@29133
    75
val intro_locales_tac = Locale.intro_locales_tac; (*already forked!*)
haftmann@29133
    76
haftmann@29133
    77
val interpretation = Locale.interpretation;
haftmann@29133
    78
val interpretation_in_locale = Locale.interpretation_in_locale;
haftmann@29133
    79
val get_interpret_morph = Locale.get_interpret_morph;
haftmann@29133
    80
val Locale = Locale.Locale;
haftmann@29133
    81
val extern = Locale.extern;
haftmann@29133
    82
val intros = Locale.intros;
haftmann@29133
    83
val dests = Locale.dests;
haftmann@29133
    84
val init = Locale.init;
haftmann@29133
    85
val Merge = Locale.Merge;
haftmann@29133
    86
val parameters_of_expr = Locale.parameters_of_expr;
haftmann@29133
    87
val empty = Locale.empty;
haftmann@29133
    88
val cert_expr = Locale.cert_expr;
haftmann@29133
    89
val read_expr = Locale.read_expr;
haftmann@29133
    90
val parameters_of = Locale.parameters_of;
haftmann@29133
    91
val add_locale = Locale.add_locale;
haftmann@29133
    92
haftmann@29133
    93
end;
haftmann@29133
    94
haftmann@29333
    95
(*structure New_Locale =
haftmann@29133
    96
struct
haftmann@29133
    97
haftmann@29133
    98
val intro_locales_tac = Locale.intro_locales_tac; (*already forked!*)
haftmann@29133
    99
haftmann@29133
   100
val interpretation = Locale.interpretation; (*!*)
haftmann@29133
   101
val interpretation_in_locale = Locale.interpretation_in_locale; (*!*)
haftmann@29133
   102
val get_interpret_morph = Locale.get_interpret_morph; (*!*)
haftmann@29133
   103
fun Locale loc = ([(loc, ("", Expression.Positional []))], []);
haftmann@29133
   104
val extern = NewLocale.extern;
haftmann@29133
   105
val intros = Locale.intros; (*!*)
haftmann@29133
   106
val dests = Locale.dests; (*!*)
haftmann@29133
   107
val init = NewLocale.init;
haftmann@29133
   108
fun Merge locs = (map (fn loc => (loc, ("", Expression.Positional []))) locs, []);
haftmann@29133
   109
val parameters_of_expr = Locale.parameters_of_expr; (*!*)
haftmann@29133
   110
val empty = ([], []);
haftmann@29133
   111
val cert_expr = Locale.cert_expr; (*!"*)
haftmann@29133
   112
val read_expr = Locale.read_expr; (*!"*)
haftmann@29133
   113
val parameters_of = NewLocale.params_of; (*why typ option?*)
haftmann@29133
   114
val add_locale = Expression.add_locale;
haftmann@29133
   115
haftmann@29333
   116
end;*)
haftmann@29133
   117
haftmann@29133
   118
structure Locale = Old_Locale;
haftmann@29133
   119
haftmann@29133
   120
(*proper code again*)
haftmann@29133
   121
haftmann@29133
   122
haftmann@24218
   123
(** auxiliary **)
haftmann@24218
   124
haftmann@25002
   125
fun prove_interpretation tac prfx_atts expr inst =
haftmann@28822
   126
  Locale.interpretation I prfx_atts expr inst
haftmann@28715
   127
  ##> Proof.global_terminal_proof
ballarin@27761
   128
      (Method.Basic (fn ctxt => Method.SIMPLE_METHOD (tac ctxt), Position.none), NONE)
haftmann@28715
   129
  ##> ProofContext.theory_of;
haftmann@24589
   130
haftmann@25195
   131
fun prove_interpretation_in tac after_qed (name, expr) =
haftmann@25195
   132
  Locale.interpretation_in_locale
haftmann@25195
   133
      (ProofContext.theory after_qed) (name, expr)
haftmann@25195
   134
  #> Proof.global_terminal_proof
haftmann@25195
   135
      (Method.Basic (K (Method.SIMPLE_METHOD tac), Position.none), NONE)
haftmann@25195
   136
  #> ProofContext.theory_of;
haftmann@25195
   137
haftmann@28715
   138
val class_prefix = Logic.const_of_class o Sign.base_name;
haftmann@28715
   139
haftmann@28822
   140
fun class_name_morphism class =
haftmann@28941
   141
  Binding.map_prefix (K (Binding.add_prefix false (class_prefix class)));
haftmann@28822
   142
haftmann@29358
   143
type raw_morphism = morphism * ((typ Vartab.table * typ list) *  (term Vartab.table * term list));
haftmann@29358
   144
haftmann@28715
   145
fun activate_class_morphism thy class inst morphism =
haftmann@28822
   146
  Locale.get_interpret_morph thy (class_name_morphism class) (class, "") morphism class inst;
haftmann@28715
   147
haftmann@24589
   148
haftmann@25485
   149
(** primitive axclass and instance commands **)
haftmann@24589
   150
haftmann@24218
   151
fun axclass_cmd (class, raw_superclasses) raw_specs thy =
haftmann@24218
   152
  let
haftmann@24218
   153
    val ctxt = ProofContext.init thy;
haftmann@24218
   154
    val superclasses = map (Sign.read_class thy) raw_superclasses;
haftmann@24589
   155
    val name_atts = map ((apsnd o map) (Attrib.attribute thy) o fst)
haftmann@24589
   156
      raw_specs;
haftmann@24589
   157
    val axiomss = ProofContext.read_propp (ctxt, map (map (rpair []) o snd)
haftmann@24589
   158
          raw_specs)
haftmann@24218
   159
      |> snd
haftmann@24218
   160
      |> (map o map) fst;
haftmann@24589
   161
  in
haftmann@24589
   162
    AxClass.define_class (class, superclasses) []
haftmann@24589
   163
      (name_atts ~~ axiomss) thy
haftmann@24589
   164
  end;
haftmann@24218
   165
haftmann@24218
   166
local
haftmann@24218
   167
haftmann@24218
   168
fun gen_instance mk_prop add_thm after_qed insts thy =
haftmann@24218
   169
  let
haftmann@24218
   170
    fun after_qed' results =
haftmann@24218
   171
      ProofContext.theory ((fold o fold) add_thm results #> after_qed);
haftmann@24218
   172
  in
haftmann@24218
   173
    thy
haftmann@24218
   174
    |> ProofContext.init
haftmann@24589
   175
    |> Proof.theorem_i NONE after_qed' ((map (fn t => [(t, [])])
haftmann@25536
   176
        o mk_prop thy) insts)
haftmann@24218
   177
  end;
haftmann@24218
   178
haftmann@24218
   179
in
haftmann@24218
   180
haftmann@24589
   181
val instance_arity =
haftmann@24218
   182
  gen_instance (Logic.mk_arities oo Sign.cert_arity) AxClass.add_arity;
haftmann@25502
   183
val instance_arity_cmd =
haftmann@25502
   184
  gen_instance (Logic.mk_arities oo Sign.read_arity) AxClass.add_arity I;
haftmann@24589
   185
val classrel =
haftmann@25536
   186
  gen_instance (single oo (Logic.mk_classrel oo AxClass.cert_classrel)) AxClass.add_classrel I;
haftmann@24589
   187
val classrel_cmd =
haftmann@25536
   188
  gen_instance (single oo (Logic.mk_classrel oo AxClass.read_classrel)) AxClass.add_classrel I;
haftmann@24218
   189
haftmann@24218
   190
end; (*local*)
haftmann@24218
   191
haftmann@24218
   192
haftmann@24589
   193
(** class data **)
haftmann@24218
   194
haftmann@24218
   195
datatype class_data = ClassData of {
haftmann@28715
   196
haftmann@28715
   197
  (* static part *)
haftmann@24218
   198
  consts: (string * string) list
haftmann@24836
   199
    (*locale parameter ~> constant name*),
haftmann@25062
   200
  base_sort: sort,
haftmann@28715
   201
  inst: term list
haftmann@25083
   202
    (*canonical interpretation*),
haftmann@28715
   203
  morphism: Morphism.morphism * ((typ Vartab.table * typ list) * (term Vartab.table * term list))
haftmann@28715
   204
    (*morphism cookie of canonical interpretation*),
haftmann@25618
   205
  assm_intro: thm option,
haftmann@25618
   206
  of_class: thm,
haftmann@25618
   207
  axiom: thm option,
haftmann@28715
   208
  
haftmann@28715
   209
  (* dynamic part *)
haftmann@24657
   210
  defs: thm list,
haftmann@25368
   211
  operations: (string * (class * (typ * term))) list
haftmann@28715
   212
haftmann@24657
   213
};
haftmann@24218
   214
haftmann@24657
   215
fun rep_class_data (ClassData d) = d;
haftmann@25618
   216
fun mk_class_data ((consts, base_sort, inst, morphism, assm_intro, of_class, axiom),
haftmann@25368
   217
    (defs, operations)) =
haftmann@25062
   218
  ClassData { consts = consts, base_sort = base_sort, inst = inst,
wenzelm@26463
   219
    morphism = morphism, assm_intro = assm_intro, of_class = of_class, axiom = axiom,
haftmann@25618
   220
    defs = defs, operations = operations };
haftmann@28715
   221
fun map_class_data f (ClassData { consts, base_sort, inst, morphism, assm_intro,
haftmann@28715
   222
    of_class, axiom, defs, operations }) =
haftmann@25618
   223
  mk_class_data (f ((consts, base_sort, inst, morphism, assm_intro, of_class, axiom),
haftmann@25368
   224
    (defs, operations)));
haftmann@25038
   225
fun merge_class_data _ (ClassData { consts = consts,
haftmann@25618
   226
    base_sort = base_sort, inst = inst, morphism = morphism, assm_intro = assm_intro,
haftmann@25618
   227
    of_class = of_class, axiom = axiom, defs = defs1, operations = operations1 },
haftmann@25618
   228
  ClassData { consts = _, base_sort = _, inst = _, morphism = _, assm_intro = _,
haftmann@25618
   229
    of_class = _, axiom = _, defs = defs2, operations = operations2 }) =
haftmann@25618
   230
  mk_class_data ((consts, base_sort, inst, morphism, assm_intro, of_class, axiom),
haftmann@24914
   231
    (Thm.merge_thms (defs1, defs2),
haftmann@25368
   232
      AList.merge (op =) (K true) (operations1, operations2)));
haftmann@24218
   233
haftmann@24218
   234
structure ClassData = TheoryDataFun
haftmann@24218
   235
(
haftmann@25038
   236
  type T = class_data Graph.T
haftmann@25038
   237
  val empty = Graph.empty;
haftmann@24218
   238
  val copy = I;
haftmann@24218
   239
  val extend = I;
haftmann@25038
   240
  fun merge _ = Graph.join merge_class_data;
haftmann@24218
   241
);
haftmann@24218
   242
haftmann@24218
   243
haftmann@24218
   244
(* queries *)
haftmann@24218
   245
haftmann@25038
   246
val lookup_class_data = Option.map rep_class_data oo try o Graph.get_node o ClassData.get;
haftmann@24218
   247
haftmann@24589
   248
fun the_class_data thy class = case lookup_class_data thy class
wenzelm@25020
   249
 of NONE => error ("Undeclared class " ^ quote class)
haftmann@24589
   250
  | SOME data => data;
haftmann@24218
   251
haftmann@25038
   252
val is_class = is_some oo lookup_class_data;
haftmann@25038
   253
haftmann@25038
   254
val ancestry = Graph.all_succs o ClassData.get;
haftmann@24218
   255
haftmann@28715
   256
fun the_inst thy = #inst o the_class_data thy;
haftmann@28715
   257
haftmann@25002
   258
fun these_params thy =
haftmann@24218
   259
  let
haftmann@24218
   260
    fun params class =
haftmann@24218
   261
      let
wenzelm@24930
   262
        val const_typs = (#params o AxClass.get_info thy) class;
haftmann@24657
   263
        val const_names = (#consts o the_class_data thy) class;
haftmann@24218
   264
      in
haftmann@26518
   265
        (map o apsnd)
haftmann@26518
   266
          (fn c => (class, (c, (the o AList.lookup (op =) const_typs) c))) const_names
haftmann@24218
   267
      end;
haftmann@24218
   268
  in maps params o ancestry thy end;
haftmann@24218
   269
haftmann@25618
   270
fun these_assm_intros thy =
haftmann@25618
   271
  Graph.fold (fn (_, (data, _)) => fold (insert Thm.eq_thm)
haftmann@25618
   272
    ((the_list o #assm_intro o rep_class_data) data)) (ClassData.get thy) [];
haftmann@24218
   273
haftmann@28715
   274
fun these_defs thy = maps (these o Option.map #defs o lookup_class_data thy) o ancestry thy;
haftmann@28715
   275
haftmann@24836
   276
fun these_operations thy =
haftmann@24836
   277
  maps (#operations o the_class_data thy) o ancestry thy;
haftmann@24657
   278
haftmann@29358
   279
val base_sort = #base_sort oo the_class_data;
haftmann@29358
   280
haftmann@29358
   281
fun rules thy class = let
haftmann@29358
   282
    val { axiom, of_class, ... } = the_class_data thy class
haftmann@29358
   283
  in (axiom, of_class) end;
haftmann@29358
   284
haftmann@28715
   285
fun morphism thy class =
haftmann@28715
   286
  let
haftmann@28715
   287
    val { inst, morphism, ... } = the_class_data thy class;
haftmann@28715
   288
  in activate_class_morphism thy class inst morphism end;
haftmann@28715
   289
haftmann@24218
   290
fun print_classes thy =
haftmann@24218
   291
  let
wenzelm@24920
   292
    val ctxt = ProofContext.init thy;
haftmann@24218
   293
    val algebra = Sign.classes_of thy;
haftmann@24218
   294
    val arities =
haftmann@24218
   295
      Symtab.empty
haftmann@24218
   296
      |> Symtab.fold (fn (tyco, arities) => fold (fn (class, _) =>
haftmann@24218
   297
           Symtab.map_default (class, []) (insert (op =) tyco)) arities)
haftmann@24218
   298
             ((#arities o Sorts.rep_algebra) algebra);
haftmann@24218
   299
    val the_arities = these o Symtab.lookup arities;
haftmann@24218
   300
    fun mk_arity class tyco =
haftmann@24218
   301
      let
haftmann@24218
   302
        val Ss = Sorts.mg_domain algebra tyco [class];
wenzelm@24920
   303
      in Syntax.pretty_arity ctxt (tyco, Ss, [class]) end;
haftmann@24218
   304
    fun mk_param (c, ty) = Pretty.str (Sign.extern_const thy c ^ " :: "
wenzelm@24920
   305
      ^ setmp show_sorts false (Syntax.string_of_typ ctxt o Type.strip_sorts) ty);
haftmann@24218
   306
    fun mk_entry class = (Pretty.block o Pretty.fbreaks o map_filter I) [
haftmann@25062
   307
      (SOME o Pretty.str) ("class " ^ Sign.extern_class thy class ^ ":"),
haftmann@24218
   308
      (SOME o Pretty.block) [Pretty.str "supersort: ",
wenzelm@24920
   309
        (Syntax.pretty_sort ctxt o Sign.minimize_sort thy o Sign.super_classes thy) class],
haftmann@25062
   310
      if is_class thy class then (SOME o Pretty.str)
haftmann@25062
   311
        ("locale: " ^ Locale.extern thy class) else NONE,
haftmann@25062
   312
      ((fn [] => NONE | ps => (SOME o Pretty.block o Pretty.fbreaks)
haftmann@25062
   313
          (Pretty.str "parameters:" :: ps)) o map mk_param
wenzelm@24930
   314
        o these o Option.map #params o try (AxClass.get_info thy)) class,
haftmann@24218
   315
      (SOME o Pretty.block o Pretty.breaks) [
haftmann@24218
   316
        Pretty.str "instances:",
haftmann@24218
   317
        Pretty.list "" "" (map (mk_arity class) (the_arities class))
haftmann@24218
   318
      ]
haftmann@24218
   319
    ]
haftmann@24218
   320
  in
haftmann@24589
   321
    (Pretty.writeln o Pretty.chunks o separate (Pretty.str "")
haftmann@24589
   322
      o map mk_entry o Sorts.all_classes) algebra
haftmann@24218
   323
  end;
haftmann@24218
   324
haftmann@24218
   325
haftmann@24218
   326
(* updaters *)
haftmann@24218
   327
haftmann@29358
   328
fun register class superclasses params base_sort inst phi
haftmann@29358
   329
    axiom assm_intro of_class thy =
haftmann@25002
   330
  let
haftmann@25368
   331
    val operations = map (fn (v_ty as (_, ty), (c, _)) =>
haftmann@25683
   332
      (c, (class, (ty, Free v_ty)))) params;
haftmann@25038
   333
    val add_class = Graph.new_node (class,
haftmann@25683
   334
        mk_class_data (((map o pairself) fst params, base_sort,
haftmann@28715
   335
          inst, phi, assm_intro, of_class, axiom), ([], operations)))
haftmann@25002
   336
      #> fold (curry Graph.add_edge class) superclasses;
haftmann@25618
   337
  in ClassData.map add_class thy end;
haftmann@24218
   338
haftmann@25368
   339
fun register_operation class (c, (t, some_def)) thy =
haftmann@25062
   340
  let
haftmann@29358
   341
    val base_sort = base_sort thy class;
haftmann@26518
   342
    val prep_typ = map_type_tvar
haftmann@26518
   343
      (fn (vi as (v, _), sort) => if Name.aT = v
haftmann@25368
   344
        then TFree (v, base_sort) else TVar (vi, sort));
haftmann@25368
   345
    val t' = map_types prep_typ t;
haftmann@25368
   346
    val ty' = Term.fastype_of t';
haftmann@25062
   347
  in
haftmann@25062
   348
    thy
haftmann@25062
   349
    |> (ClassData.map o Graph.map_node class o map_class_data o apsnd)
haftmann@25368
   350
      (fn (defs, operations) =>
haftmann@25096
   351
        (fold cons (the_list some_def) defs,
haftmann@25368
   352
          (c, (class, (ty', t'))) :: operations))
haftmann@25062
   353
  end;
haftmann@24218
   354
haftmann@24589
   355
haftmann@29358
   356
(** tactics and methods **)
haftmann@24589
   357
haftmann@29358
   358
fun prove_class_interpretation class inst consts hyp_facts def_facts thy =
haftmann@25062
   359
  let
haftmann@29358
   360
    val constraints = map (fn (class, c) => map_atyps (K (TFree (Name.aT,
haftmann@29358
   361
      [class]))) (Sign.the_const_type thy c)) consts;
haftmann@29358
   362
    val no_constraints = map (map_atyps (K (TFree (Name.aT, [])))) constraints;
haftmann@29358
   363
    fun add_constraint c T = Sign.add_const_constraint (c, SOME T);
haftmann@29358
   364
    fun tac ctxt = ALLGOALS (ProofContext.fact_tac (hyp_facts @ def_facts)
haftmann@29358
   365
      ORELSE' (fn n => SELECT_GOAL (Locale.intro_locales_tac false ctxt []) n));
haftmann@29358
   366
    val prfx = class_prefix class;
haftmann@29358
   367
  in
haftmann@29358
   368
    thy
haftmann@29358
   369
    |> fold2 add_constraint (map snd consts) no_constraints
haftmann@29358
   370
    |> prove_interpretation tac (class_name_morphism class) (Locale.Locale class)
haftmann@29358
   371
          (map SOME inst, map (pair (Attrib.empty_binding) o Thm.prop_of) def_facts)
haftmann@29358
   372
    ||> fold2 add_constraint (map snd consts) constraints
haftmann@29358
   373
  end;
haftmann@24218
   374
haftmann@29358
   375
fun prove_subclass_relation (sub, sup) some_thm thy =
haftmann@25618
   376
  let
haftmann@29358
   377
    val of_class = (snd o rules thy) sup;
haftmann@27684
   378
    val intro = case some_thm
haftmann@27684
   379
     of SOME thm => Drule.standard' (of_class OF [Drule.standard' thm])
haftmann@27684
   380
      | NONE => Thm.instantiate ([pairself (Thm.ctyp_of thy o TVar o pair (Name.aT, 0))
haftmann@27684
   381
          ([], [sub])], []) of_class;
haftmann@29358
   382
    val classrel = (intro OF (the_list o fst o rules thy) sub)
haftmann@27684
   383
      |> Thm.close_derivation;
haftmann@25618
   384
  in
haftmann@25618
   385
    thy
haftmann@25618
   386
    |> AxClass.add_classrel classrel
haftmann@27684
   387
    |> prove_interpretation_in (ALLGOALS (ProofContext.fact_tac (the_list some_thm)))
haftmann@25618
   388
         I (sub, Locale.Locale sup)
haftmann@25618
   389
    |> ClassData.map (Graph.add_edge (sub, sup))
haftmann@25618
   390
  end;
haftmann@25618
   391
haftmann@24218
   392
fun intro_classes_tac facts st =
haftmann@24218
   393
  let
haftmann@24218
   394
    val thy = Thm.theory_of_thm st;
haftmann@24218
   395
    val classes = Sign.all_classes thy;
haftmann@24218
   396
    val class_trivs = map (Thm.class_triv thy) classes;
haftmann@25618
   397
    val class_intros = map_filter (try (#intro o AxClass.get_info thy)) classes;
haftmann@25618
   398
    val assm_intros = these_assm_intros thy;
haftmann@24218
   399
  in
haftmann@25618
   400
    Method.intros_tac (class_trivs @ class_intros @ assm_intros) facts st
haftmann@24218
   401
  end;
haftmann@24218
   402
haftmann@26470
   403
fun default_intro_tac ctxt [] =
ballarin@29029
   404
      intro_classes_tac [] ORELSE NewLocale.intro_locales_tac true ctxt [] ORELSE
ballarin@29029
   405
      Locale.intro_locales_tac true ctxt []
haftmann@26470
   406
  | default_intro_tac _ _ = no_tac;
haftmann@24218
   407
haftmann@24218
   408
fun default_tac rules ctxt facts =
haftmann@24218
   409
  HEADGOAL (Method.some_rule_tac rules ctxt facts) ORELSE
haftmann@26470
   410
    default_intro_tac ctxt facts;
haftmann@24218
   411
wenzelm@26463
   412
val _ = Context.>> (Context.map_theory
wenzelm@26463
   413
  (Method.add_methods
wenzelm@26463
   414
   [("intro_classes", Method.no_args (Method.METHOD intro_classes_tac),
wenzelm@26463
   415
      "back-chain introduction rules of classes"),
wenzelm@26463
   416
    ("default", Method.thms_ctxt_args (Method.METHOD oo default_tac),
wenzelm@26463
   417
      "apply some intro/elim rule")]));
wenzelm@26463
   418
haftmann@24218
   419
haftmann@24589
   420
(** classes and class target **)
haftmann@24218
   421
haftmann@25002
   422
(* class context syntax *)
haftmann@24748
   423
haftmann@26238
   424
fun synchronize_class_syntax sups base_sort ctxt =
haftmann@24914
   425
  let
wenzelm@25344
   426
    val thy = ProofContext.theory_of ctxt;
haftmann@26596
   427
    val algebra = Sign.classes_of thy;
haftmann@25083
   428
    val operations = these_operations thy sups;
haftmann@26518
   429
    fun subst_class_typ sort = map_type_tfree (K (TVar ((Name.aT, 0), sort)));
haftmann@26518
   430
    val primary_constraints =
haftmann@25368
   431
      (map o apsnd) (subst_class_typ base_sort o fst o snd) operations;
haftmann@26518
   432
    val secondary_constraints =
haftmann@25368
   433
      (map o apsnd) (fn (class, (ty, _)) => subst_class_typ [class] ty) operations;
wenzelm@25318
   434
    fun declare_const (c, _) =
wenzelm@25318
   435
      let val b = Sign.base_name c
wenzelm@25344
   436
      in Sign.intern_const thy b = c ? Variable.declare_const (b, c) end;
haftmann@26518
   437
    fun improve (c, ty) = (case AList.lookup (op =) primary_constraints c
haftmann@26238
   438
     of SOME ty' => (case try (Type.raw_match (ty', ty)) Vartab.empty
haftmann@26238
   439
         of SOME tyenv => (case Vartab.lookup tyenv (Name.aT, 0)
haftmann@26596
   440
             of SOME (_, ty' as TVar (tvar as (vi, sort))) =>
haftmann@26238
   441
                  if TypeInfer.is_param vi
haftmann@26596
   442
                    andalso Sorts.sort_le algebra (base_sort, sort)
haftmann@26596
   443
                      then SOME (ty', TFree (Name.aT, base_sort))
haftmann@26596
   444
                      else NONE
haftmann@26238
   445
              | _ => NONE)
haftmann@26238
   446
          | NONE => NONE)
haftmann@26238
   447
      | NONE => NONE)
haftmann@26238
   448
    fun subst (c, ty) = Option.map snd (AList.lookup (op =) operations c);
haftmann@25368
   449
    val unchecks = map (fn (c, (_, (ty, t))) => (t, Const (c, ty))) operations;
haftmann@25083
   450
  in
haftmann@25083
   451
    ctxt
haftmann@26518
   452
    |> fold declare_const primary_constraints
haftmann@26518
   453
    |> Overloading.map_improvable_syntax (K (((primary_constraints, secondary_constraints),
haftmann@26730
   454
        (((improve, subst), true), unchecks)), false))
haftmann@26518
   455
    |> Overloading.set_primary_constraints
haftmann@25083
   456
  end;
haftmann@25083
   457
haftmann@25083
   458
fun refresh_syntax class ctxt =
haftmann@25002
   459
  let
haftmann@25002
   460
    val thy = ProofContext.theory_of ctxt;
haftmann@29358
   461
    val base_sort = base_sort thy class;
haftmann@26238
   462
  in synchronize_class_syntax [class] base_sort ctxt end;
haftmann@25002
   463
haftmann@29358
   464
fun begin sups base_sort ctxt =
haftmann@25083
   465
  ctxt
haftmann@25083
   466
  |> Variable.declare_term
haftmann@25083
   467
      (Logic.mk_type (TFree (Name.aT, base_sort)))
haftmann@26238
   468
  |> synchronize_class_syntax sups base_sort
haftmann@26238
   469
  |> Overloading.add_improvable_syntax;
haftmann@24901
   470
haftmann@25311
   471
fun init class thy =
haftmann@25311
   472
  thy
haftmann@25311
   473
  |> Locale.init class
haftmann@29358
   474
  |> begin [class] (base_sort thy class);
haftmann@24914
   475
haftmann@24748
   476
haftmann@27690
   477
(* class target *)
haftmann@27690
   478
haftmann@27690
   479
fun declare class pos ((c, mx), dict) thy =
haftmann@27690
   480
  let
haftmann@27690
   481
    val prfx = class_prefix class;
haftmann@27690
   482
    val thy' = thy |> Sign.add_path prfx;
haftmann@27690
   483
    val phi = morphism thy' class;
haftmann@28715
   484
    val inst = the_inst thy' class;
haftmann@28715
   485
    val params = map (apsnd fst o snd) (these_params thy' [class]);
haftmann@27690
   486
haftmann@28965
   487
    val c' = Sign.full_bname thy' c;
haftmann@27690
   488
    val dict' = Morphism.term phi dict;
haftmann@27690
   489
    val dict_def = map_types Logic.unvarifyT dict';
haftmann@27690
   490
    val ty' = Term.fastype_of dict_def;
haftmann@27690
   491
    val ty'' = Type.strip_sorts ty';
haftmann@27690
   492
    val def_eq = Logic.mk_equals (Const (c', ty'), dict_def);
wenzelm@28674
   493
    fun get_axiom thy = ((Thm.varifyT o Thm.axiom thy) c', thy);
haftmann@27690
   494
  in
haftmann@27690
   495
    thy'
haftmann@28965
   496
    |> Sign.declare_const pos ((Binding.name c, ty''), mx) |> snd
haftmann@27690
   497
    |> Thm.add_def false false (c, def_eq)
haftmann@27690
   498
    |>> Thm.symmetric
haftmann@27690
   499
    ||>> get_axiom
haftmann@28715
   500
    |-> (fn (def, def') => prove_class_interpretation class inst params [] [def]
haftmann@28715
   501
      #> snd
haftmann@28715
   502
        (*assumption: interpretation cookie does not change
haftmann@28715
   503
          by adding equations to interpretation*)
haftmann@27690
   504
      #> register_operation class (c', (dict', SOME (Thm.symmetric def')))
haftmann@27690
   505
      #> PureThy.store_thm (c ^ "_raw", def')
haftmann@27690
   506
      #> snd)
haftmann@27690
   507
    |> Sign.restore_naming thy
haftmann@27690
   508
    |> Sign.add_const_constraint (c', SOME ty')
haftmann@27690
   509
  end;
haftmann@27690
   510
haftmann@27690
   511
fun abbrev class prmode pos ((c, mx), rhs) thy =
haftmann@27690
   512
  let
haftmann@27690
   513
    val prfx = class_prefix class;
haftmann@27690
   514
    val thy' = thy |> Sign.add_path prfx;
haftmann@27690
   515
haftmann@27690
   516
    val unchecks = map (fn (c, (_, (ty, t))) => (t, Const (c, ty)))
haftmann@27690
   517
      (these_operations thy [class]);
haftmann@28965
   518
    val c' = Sign.full_bname thy' c;
haftmann@27690
   519
    val rhs' = Pattern.rewrite_term thy unchecks [] rhs;
haftmann@27690
   520
    val rhs'' = map_types Logic.varifyT rhs';
haftmann@27690
   521
    val ty' = Term.fastype_of rhs';
haftmann@27690
   522
  in
haftmann@27690
   523
    thy'
haftmann@28965
   524
    |> Sign.add_abbrev (#1 prmode) pos (Binding.name c, map_types Type.strip_sorts rhs'') |> snd
haftmann@27690
   525
    |> Sign.add_const_constraint (c', SOME ty')
haftmann@27690
   526
    |> Sign.notation true prmode [(Const (c', ty'), mx)]
haftmann@27690
   527
    |> not (#1 prmode = PrintMode.input) ? register_operation class (c', (rhs', NONE))
haftmann@27690
   528
    |> Sign.restore_naming thy
haftmann@27690
   529
  end;
haftmann@27690
   530
haftmann@27690
   531
haftmann@25462
   532
(** instantiation target **)
haftmann@25462
   533
haftmann@25462
   534
(* bookkeeping *)
haftmann@25462
   535
haftmann@25462
   536
datatype instantiation = Instantiation of {
haftmann@25864
   537
  arities: string list * (string * sort) list * sort,
haftmann@25462
   538
  params: ((string * string) * (string * typ)) list
haftmann@25603
   539
    (*(instantiation parameter, type constructor), (local instantiation parameter, typ)*)
haftmann@25462
   540
}
haftmann@25462
   541
haftmann@25462
   542
structure Instantiation = ProofDataFun
haftmann@25462
   543
(
haftmann@25462
   544
  type T = instantiation
haftmann@25536
   545
  fun init _ = Instantiation { arities = ([], [], []), params = [] };
haftmann@25462
   546
);
haftmann@25462
   547
haftmann@25485
   548
fun mk_instantiation (arities, params) =
haftmann@25485
   549
  Instantiation { arities = arities, params = params };
haftmann@25514
   550
fun get_instantiation lthy = case Instantiation.get (LocalTheory.target_of lthy)
haftmann@25485
   551
 of Instantiation data => data;
haftmann@25514
   552
fun map_instantiation f = (LocalTheory.target o Instantiation.map)
haftmann@25514
   553
  (fn Instantiation { arities, params } => mk_instantiation (f (arities, params)));
haftmann@25462
   554
haftmann@25514
   555
fun the_instantiation lthy = case get_instantiation lthy
haftmann@25536
   556
 of { arities = ([], [], []), ... } => error "No instantiation target"
haftmann@25485
   557
  | data => data;
haftmann@25462
   558
haftmann@25485
   559
val instantiation_params = #params o get_instantiation;
haftmann@25462
   560
haftmann@25514
   561
fun instantiation_param lthy v = instantiation_params lthy
haftmann@25462
   562
  |> find_first (fn (_, (v', _)) => v = v')
haftmann@25462
   563
  |> Option.map (fst o fst);
haftmann@25462
   564
haftmann@25462
   565
haftmann@25462
   566
(* syntax *)
haftmann@25462
   567
haftmann@26238
   568
fun synchronize_inst_syntax ctxt =
haftmann@25462
   569
  let
haftmann@26259
   570
    val Instantiation { arities = (_, _, sort), params = params } = Instantiation.get ctxt;
haftmann@26238
   571
    val thy = ProofContext.theory_of ctxt;
haftmann@26238
   572
    fun subst (c, ty) = case AxClass.inst_tyco_of thy (c, ty)
haftmann@26238
   573
         of SOME tyco => (case AList.lookup (op =) params (c, tyco)
haftmann@26238
   574
             of SOME (v_ty as (_, ty)) => SOME (ty, Free v_ty)
haftmann@26238
   575
              | NONE => NONE)
haftmann@26238
   576
          | NONE => NONE;
haftmann@26238
   577
    val unchecks =
haftmann@26238
   578
      map (fn ((c, _), v_ty as (_, ty)) => (Free v_ty, Const (c, ty))) params;
haftmann@26238
   579
  in
haftmann@26238
   580
    ctxt
haftmann@26259
   581
    |> Overloading.map_improvable_syntax
haftmann@26730
   582
         (fn (((primary_constraints, _), (((improve, _), _), _)), _) =>
haftmann@26730
   583
            (((primary_constraints, []), (((improve, subst), false), unchecks)), false))
haftmann@26238
   584
  end;
haftmann@25462
   585
haftmann@25462
   586
haftmann@25462
   587
(* target *)
haftmann@25462
   588
haftmann@25485
   589
val sanatize_name = (*FIXME*)
haftmann@25485
   590
  let
haftmann@25574
   591
    fun is_valid s = Symbol.is_ascii_letter s orelse Symbol.is_ascii_digit s
haftmann@25574
   592
      orelse s = "'" orelse s = "_";
haftmann@25485
   593
    val is_junk = not o is_valid andf Symbol.is_regular;
haftmann@25485
   594
    val junk = Scan.many is_junk;
haftmann@25485
   595
    val scan_valids = Symbol.scanner "Malformed input"
haftmann@25485
   596
      ((junk |--
haftmann@25485
   597
        (Scan.optional (Scan.one Symbol.is_ascii_letter) "x" ^^ (Scan.many is_valid >> implode)
haftmann@25485
   598
        --| junk))
wenzelm@25999
   599
      ::: Scan.repeat ((Scan.many1 is_valid >> implode) --| junk));
haftmann@25485
   600
  in
haftmann@25485
   601
    explode #> scan_valids #> implode
haftmann@25485
   602
  end;
haftmann@25485
   603
haftmann@26259
   604
fun type_name "*" = "prod"
haftmann@26259
   605
  | type_name "+" = "sum"
haftmann@26259
   606
  | type_name s = sanatize_name (NameSpace.base s); (*FIXME*)
haftmann@26259
   607
haftmann@26518
   608
fun resort_terms pp algebra consts constraints ts =
haftmann@26518
   609
  let
haftmann@26518
   610
    fun matchings (Const (c_ty as (c, _))) = (case constraints c
haftmann@26518
   611
         of NONE => I
haftmann@26518
   612
          | SOME sorts => fold2 (curry (Sorts.meet_sort algebra))
haftmann@26518
   613
              (Consts.typargs consts c_ty) sorts)
haftmann@26518
   614
      | matchings _ = I
haftmann@26518
   615
    val tvartab = (fold o fold_aterms) matchings ts Vartab.empty
wenzelm@26642
   616
      handle Sorts.CLASS_ERROR e => error (Sorts.class_error pp e);
haftmann@27089
   617
    val inst = map_type_tvar
haftmann@27089
   618
      (fn (vi, sort) => TVar (vi, the_default sort (Vartab.lookup tvartab vi)));
haftmann@26518
   619
  in if Vartab.is_empty tvartab then NONE else SOME ((map o map_types) inst ts) end;
haftmann@26518
   620
haftmann@25864
   621
fun init_instantiation (tycos, vs, sort) thy =
haftmann@25462
   622
  let
haftmann@25536
   623
    val _ = if null tycos then error "At least one arity must be given" else ();
haftmann@26518
   624
    val params = these_params thy sort;
haftmann@26518
   625
    fun get_param tyco (param, (_, (c, ty))) = if can (AxClass.param_of_inst thy) (c, tyco)
haftmann@25603
   626
      then NONE else SOME ((c, tyco),
haftmann@25864
   627
        (param ^ "_" ^ type_name tyco, map_atyps (K (Type (tyco, map TFree vs))) ty));
haftmann@26518
   628
    val inst_params = map_product get_param tycos params |> map_filter I;
haftmann@26518
   629
    val primary_constraints = map (apsnd
haftmann@26518
   630
      (map_atyps (K (TVar ((Name.aT, 0), [])))) o snd o snd) params;
wenzelm@26939
   631
    val pp = Syntax.pp_global thy;
haftmann@26518
   632
    val algebra = Sign.classes_of thy
haftmann@26518
   633
      |> fold (fn tyco => Sorts.add_arities pp
haftmann@26518
   634
            (tyco, map (fn class => (class, map snd vs)) sort)) tycos;
haftmann@26518
   635
    val consts = Sign.consts_of thy;
haftmann@26518
   636
    val improve_constraints = AList.lookup (op =)
haftmann@26518
   637
      (map (fn (_, (class, (c, _))) => (c, [[class]])) params);
haftmann@26518
   638
    fun resort_check ts ctxt = case resort_terms pp algebra consts improve_constraints ts
haftmann@26518
   639
     of NONE => NONE
haftmann@26518
   640
      | SOME ts' => SOME (ts', ctxt);
haftmann@26518
   641
    fun improve (c, ty) = case AxClass.inst_tyco_of thy (c, ty)
haftmann@26329
   642
     of SOME tyco => (case AList.lookup (op =) inst_params (c, tyco)
haftmann@26518
   643
         of SOME (_, ty') => if Type.raw_instance (ty', ty) then SOME (ty, ty') else NONE
haftmann@26259
   644
          | NONE => NONE)
haftmann@26259
   645
      | NONE => NONE;
haftmann@25485
   646
  in
haftmann@25485
   647
    thy
haftmann@25485
   648
    |> ProofContext.init
haftmann@26329
   649
    |> Instantiation.put (mk_instantiation ((tycos, vs, sort), inst_params))
wenzelm@27281
   650
    |> fold (Variable.declare_typ o TFree) vs
haftmann@26329
   651
    |> fold (Variable.declare_names o Free o snd) inst_params
haftmann@26259
   652
    |> (Overloading.map_improvable_syntax o apfst)
haftmann@26329
   653
         (fn ((_, _), ((_, subst), unchecks)) =>
haftmann@26730
   654
            ((primary_constraints, []), (((improve, K NONE), false), [])))
haftmann@26259
   655
    |> Overloading.add_improvable_syntax
haftmann@26518
   656
    |> Context.proof_map (Syntax.add_term_check 0 "resorting" resort_check)
haftmann@26238
   657
    |> synchronize_inst_syntax
haftmann@25485
   658
  end;
haftmann@25485
   659
haftmann@26238
   660
fun confirm_declaration c = (map_instantiation o apsnd)
haftmann@26238
   661
  (filter_out (fn (_, (c', _)) => c' = c))
haftmann@26238
   662
  #> LocalTheory.target synchronize_inst_syntax
haftmann@26238
   663
haftmann@25485
   664
fun gen_instantiation_instance do_proof after_qed lthy =
haftmann@25485
   665
  let
haftmann@25864
   666
    val (tycos, vs, sort) = (#arities o the_instantiation) lthy;
haftmann@25864
   667
    val arities_proof = maps (fn tyco => Logic.mk_arities (tyco, map snd vs, sort)) tycos;
haftmann@25462
   668
    fun after_qed' results =
haftmann@25462
   669
      LocalTheory.theory (fold (AxClass.add_arity o Thm.varifyT) results)
haftmann@25462
   670
      #> after_qed;
haftmann@25462
   671
  in
haftmann@25462
   672
    lthy
haftmann@25462
   673
    |> do_proof after_qed' arities_proof
haftmann@25462
   674
  end;
haftmann@25462
   675
haftmann@25485
   676
val instantiation_instance = gen_instantiation_instance (fn after_qed => fn ts =>
haftmann@25462
   677
  Proof.theorem_i NONE (after_qed o map the_single) (map (fn t => [(t, [])]) ts));
haftmann@25462
   678
haftmann@25485
   679
fun prove_instantiation_instance tac = gen_instantiation_instance (fn after_qed =>
haftmann@25502
   680
  fn ts => fn lthy => after_qed (map (fn t => Goal.prove lthy [] [] t
haftmann@25502
   681
    (fn {context, ...} => tac context)) ts) lthy) I;
haftmann@25462
   682
haftmann@28666
   683
fun prove_instantiation_exit tac = prove_instantiation_instance tac
haftmann@28666
   684
  #> LocalTheory.exit_global;
haftmann@28666
   685
haftmann@28666
   686
fun prove_instantiation_exit_result f tac x lthy =
haftmann@28666
   687
  let
haftmann@28666
   688
    val phi = ProofContext.export_morphism lthy
haftmann@28666
   689
      (ProofContext.init (ProofContext.theory_of lthy));
haftmann@28666
   690
    val y = f phi x;
haftmann@28666
   691
  in
haftmann@28666
   692
    lthy
haftmann@28666
   693
    |> prove_instantiation_exit (fn ctxt => tac ctxt y)
haftmann@28666
   694
    |> pair y
haftmann@28666
   695
  end;
haftmann@28666
   696
haftmann@25462
   697
fun conclude_instantiation lthy =
haftmann@25462
   698
  let
haftmann@25485
   699
    val { arities, params } = the_instantiation lthy;
haftmann@25864
   700
    val (tycos, vs, sort) = arities;
haftmann@25462
   701
    val thy = ProofContext.theory_of lthy;
haftmann@25597
   702
    val _ = map (fn tyco => if Sign.of_sort thy
haftmann@25864
   703
        (Type (tyco, map TFree vs), sort)
haftmann@25462
   704
      then () else error ("Missing instance proof for type " ^ quote (Sign.extern_type thy tyco)))
haftmann@25597
   705
        tycos;
haftmann@25597
   706
  in lthy end;
haftmann@25462
   707
haftmann@25603
   708
fun pretty_instantiation lthy =
haftmann@25603
   709
  let
haftmann@25603
   710
    val { arities, params } = the_instantiation lthy;
haftmann@25864
   711
    val (tycos, vs, sort) = arities;
haftmann@25603
   712
    val thy = ProofContext.theory_of lthy;
haftmann@25864
   713
    fun pr_arity tyco = Syntax.pretty_arity lthy (tyco, map snd vs, sort);
haftmann@25603
   714
    fun pr_param ((c, _), (v, ty)) =
haftmann@25864
   715
      (Pretty.block o Pretty.breaks) [Pretty.str v, Pretty.str "==",
wenzelm@26939
   716
        (Pretty.str o Sign.extern_const thy) c, Pretty.str "::", Syntax.pretty_typ_global thy ty];
haftmann@25603
   717
  in
haftmann@25603
   718
    (Pretty.block o Pretty.fbreaks)
haftmann@25603
   719
      (Pretty.str "instantiation" :: map pr_arity tycos @ map pr_param params)
haftmann@25603
   720
  end;
haftmann@25603
   721
haftmann@24218
   722
end;
haftmann@25683
   723