src/Pure/Tools/class_package.ML
author haftmann
Mon, 02 Oct 2006 23:00:51 +0200
changeset 20835 27d049062b56
parent 20703 f3f2b1091ea0
child 20843 a5343075bdc5
permissions -rw-r--r--
tuned
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
18168
d35daf321b8a class_package - operational view on type classes
haftmann
parents:
diff changeset
     1
(*  Title:      Pure/Tools/class_package.ML
d35daf321b8a class_package - operational view on type classes
haftmann
parents:
diff changeset
     2
    ID:         $Id$
d35daf321b8a class_package - operational view on type classes
haftmann
parents:
diff changeset
     3
    Author:     Florian Haftmann, TU Muenchen
d35daf321b8a class_package - operational view on type classes
haftmann
parents:
diff changeset
     4
19468
0afdd5023bfc Sign.arity_sorts;
wenzelm
parents: 19456
diff changeset
     5
Type classes derived from primitive axclasses and locales.
18168
d35daf321b8a class_package - operational view on type classes
haftmann
parents:
diff changeset
     6
*)
d35daf321b8a class_package - operational view on type classes
haftmann
parents:
diff changeset
     7
d35daf321b8a class_package - operational view on type classes
haftmann
parents:
diff changeset
     8
signature CLASS_PACKAGE =
d35daf321b8a class_package - operational view on type classes
haftmann
parents:
diff changeset
     9
sig
20428
67fa1c6ba89e refinements
haftmann
parents: 20403
diff changeset
    10
  val class: bstring -> class list -> Element.context Locale.element list -> theory
20289
ba7a7c56bed5 normalized Proof.context/method type aliases;
wenzelm
parents: 20265
diff changeset
    11
    -> Proof.context * theory
20428
67fa1c6ba89e refinements
haftmann
parents: 20403
diff changeset
    12
  val class_i: bstring -> class list -> Element.context_i Locale.element list -> theory
20289
ba7a7c56bed5 normalized Proof.context/method type aliases;
wenzelm
parents: 20265
diff changeset
    13
    -> Proof.context * theory
20601
3e1caf5a07c6 classical arity syntax
haftmann
parents: 20548
diff changeset
    14
  val instance_arity: ((bstring * string list) * string) list
19136
00ade10f611d some refinements
haftmann
parents: 19134
diff changeset
    15
    -> bstring * Attrib.src list -> ((bstring * Attrib.src list) * string) list
18575
9ccfd1d1e874 substantial additions using locales
haftmann
parents: 18550
diff changeset
    16
    -> theory -> Proof.state
20601
3e1caf5a07c6 classical arity syntax
haftmann
parents: 20548
diff changeset
    17
  val instance_arity_i: ((bstring * sort list) * sort) list
19136
00ade10f611d some refinements
haftmann
parents: 19134
diff changeset
    18
    -> bstring * attribute list -> ((bstring * attribute list) * term) list
18575
9ccfd1d1e874 substantial additions using locales
haftmann
parents: 18550
diff changeset
    19
    -> theory -> Proof.state
20182
79c9ff40d760 tactic for prove_instance_arity now gets definition theorems as arguments
haftmann
parents: 20175
diff changeset
    20
  val prove_instance_arity: (thm list -> tactic) -> ((string * sort list) * sort) list
19136
00ade10f611d some refinements
haftmann
parents: 19134
diff changeset
    21
    -> bstring * attribute list -> ((bstring * attribute list) * term) list
19110
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
    22
    -> theory -> theory
19150
1457d810b408 class package and codegen refinements
haftmann
parents: 19136
diff changeset
    23
  val instance_sort: string * string -> theory -> Proof.state
1457d810b408 class package and codegen refinements
haftmann
parents: 19136
diff changeset
    24
  val instance_sort_i: class * sort -> theory -> Proof.state
1457d810b408 class package and codegen refinements
haftmann
parents: 19136
diff changeset
    25
  val prove_instance_sort: tactic -> class * sort -> theory -> theory
18168
d35daf321b8a class_package - operational view on type classes
haftmann
parents:
diff changeset
    26
18755
eb3733779aa8 slight steps forward
haftmann
parents: 18728
diff changeset
    27
  val certify_class: theory -> class -> class
19110
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
    28
  val certify_sort: theory -> sort -> sort
20385
2f52b5aba086 code cleanup, instance_subsort now working
haftmann
parents: 20368
diff changeset
    29
  val read_class: theory -> xstring -> class
19110
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
    30
  val read_sort: theory -> string -> sort
20465
95f6d354b0ed proper project_sort
haftmann
parents: 20455
diff changeset
    31
  val operational_algebra: theory -> (sort -> sort) * Sorts.algebra
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18670
diff changeset
    32
  val the_consts_sign: theory -> class -> string * (string * typ) list
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18670
diff changeset
    33
  val the_inst_sign: theory -> class * string -> (string * sort) list * (string * typ) list
20175
0a8ca32f6e64 class package and codegen refinements
haftmann
parents: 20106
diff changeset
    34
  val assume_arities_of_sort: theory -> ((string * sort list) * sort) list -> typ * sort -> bool
0a8ca32f6e64 class package and codegen refinements
haftmann
parents: 20106
diff changeset
    35
  val assume_arities_thy: theory -> ((string * sort list) * sort) list -> (theory -> 'a) -> 'a
0a8ca32f6e64 class package and codegen refinements
haftmann
parents: 20106
diff changeset
    36
    (*'a must not keep any reference to theory*)
19110
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
    37
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18670
diff changeset
    38
  val print_classes: theory -> unit
19110
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
    39
  val intro_classes_tac: thm list -> tactic
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
    40
  val default_intro_classes_tac: thm list -> tactic
18168
d35daf321b8a class_package - operational view on type classes
haftmann
parents:
diff changeset
    41
end;
d35daf321b8a class_package - operational view on type classes
haftmann
parents:
diff changeset
    42
20182
79c9ff40d760 tactic for prove_instance_arity now gets definition theorems as arguments
haftmann
parents: 20175
diff changeset
    43
structure ClassPackage : CLASS_PACKAGE =
18168
d35daf321b8a class_package - operational view on type classes
haftmann
parents:
diff changeset
    44
struct
d35daf321b8a class_package - operational view on type classes
haftmann
parents:
diff changeset
    45
d35daf321b8a class_package - operational view on type classes
haftmann
parents:
diff changeset
    46
20455
e671d9eac6c8 removed some dictionary stuff
haftmann
parents: 20428
diff changeset
    47
(** theory data **)
18168
d35daf321b8a class_package - operational view on type classes
haftmann
parents:
diff changeset
    48
19456
b5bfd2d17dd3 more precise data structure
haftmann
parents: 19434
diff changeset
    49
datatype class_data = ClassData of {
18575
9ccfd1d1e874 substantial additions using locales
haftmann
parents: 18550
diff changeset
    50
  name_locale: string,
9ccfd1d1e874 substantial additions using locales
haftmann
parents: 18550
diff changeset
    51
  name_axclass: string,
9ccfd1d1e874 substantial additions using locales
haftmann
parents: 18550
diff changeset
    52
  var: string,
19966
88bbe97ed0b0 refinement in instance command
haftmann
parents: 19957
diff changeset
    53
  consts: (string * (string * typ)) list,
20385
2f52b5aba086 code cleanup, instance_subsort now working
haftmann
parents: 20368
diff changeset
    54
    (*locale parameter ~> toplevel theory constant*)
19966
88bbe97ed0b0 refinement in instance command
haftmann
parents: 19957
diff changeset
    55
  propnames: string list
88bbe97ed0b0 refinement in instance command
haftmann
parents: 19957
diff changeset
    56
} * thm list Symtab.table;
18168
d35daf321b8a class_package - operational view on type classes
haftmann
parents:
diff changeset
    57
19456
b5bfd2d17dd3 more precise data structure
haftmann
parents: 19434
diff changeset
    58
fun rep_classdata (ClassData c) = c;
b5bfd2d17dd3 more precise data structure
haftmann
parents: 19434
diff changeset
    59
18575
9ccfd1d1e874 substantial additions using locales
haftmann
parents: 18550
diff changeset
    60
structure ClassData = TheoryDataFun (
18168
d35daf321b8a class_package - operational view on type classes
haftmann
parents:
diff changeset
    61
  struct
d35daf321b8a class_package - operational view on type classes
haftmann
parents:
diff changeset
    62
    val name = "Pure/classes";
19966
88bbe97ed0b0 refinement in instance command
haftmann
parents: 19957
diff changeset
    63
    type T = class_data Symtab.table;
88bbe97ed0b0 refinement in instance command
haftmann
parents: 19957
diff changeset
    64
    val empty = Symtab.empty;
18168
d35daf321b8a class_package - operational view on type classes
haftmann
parents:
diff changeset
    65
    val copy = I;
d35daf321b8a class_package - operational view on type classes
haftmann
parents:
diff changeset
    66
    val extend = I;
19966
88bbe97ed0b0 refinement in instance command
haftmann
parents: 19957
diff changeset
    67
    fun merge _ = Symtab.join (fn _ => fn (ClassData (classd, instd1), ClassData (_, instd2)) =>
88bbe97ed0b0 refinement in instance command
haftmann
parents: 19957
diff changeset
    68
      (ClassData (classd, Symtab.merge (K true) (instd1, instd2))));
88bbe97ed0b0 refinement in instance command
haftmann
parents: 19957
diff changeset
    69
    fun print thy data =
18575
9ccfd1d1e874 substantial additions using locales
haftmann
parents: 18550
diff changeset
    70
      let
19966
88bbe97ed0b0 refinement in instance command
haftmann
parents: 19957
diff changeset
    71
        fun pretty_class (name, ClassData ({name_locale, name_axclass, var, consts, ...}, _)) =
18575
9ccfd1d1e874 substantial additions using locales
haftmann
parents: 18550
diff changeset
    72
          (Pretty.block o Pretty.fbreaks) [
9ccfd1d1e874 substantial additions using locales
haftmann
parents: 18550
diff changeset
    73
            Pretty.str ("class " ^ name ^ ":"),
9ccfd1d1e874 substantial additions using locales
haftmann
parents: 18550
diff changeset
    74
            Pretty.str ("locale: " ^ name_locale),
9ccfd1d1e874 substantial additions using locales
haftmann
parents: 18550
diff changeset
    75
            Pretty.str ("axclass: " ^ name_axclass),
9ccfd1d1e874 substantial additions using locales
haftmann
parents: 18550
diff changeset
    76
            Pretty.str ("class variable: " ^ var),
9ccfd1d1e874 substantial additions using locales
haftmann
parents: 18550
diff changeset
    77
            (Pretty.block o Pretty.fbreaks) (
9ccfd1d1e874 substantial additions using locales
haftmann
parents: 18550
diff changeset
    78
              Pretty.str "constants: "
19280
5091dc43817b slight improvement in serializer, stub for code generator theorems added
haftmann
parents: 19253
diff changeset
    79
              :: map (fn (_, (c, ty)) => Pretty.str (c ^ " :: " ^ Sign.string_of_typ thy ty)) consts
18575
9ccfd1d1e874 substantial additions using locales
haftmann
parents: 18550
diff changeset
    80
            )
9ccfd1d1e874 substantial additions using locales
haftmann
parents: 18550
diff changeset
    81
          ]
9ccfd1d1e874 substantial additions using locales
haftmann
parents: 18550
diff changeset
    82
      in
19966
88bbe97ed0b0 refinement in instance command
haftmann
parents: 19957
diff changeset
    83
        (Pretty.writeln o Pretty.chunks o map pretty_class o Symtab.dest) data
18575
9ccfd1d1e874 substantial additions using locales
haftmann
parents: 18550
diff changeset
    84
      end;
18168
d35daf321b8a class_package - operational view on type classes
haftmann
parents:
diff changeset
    85
  end
d35daf321b8a class_package - operational view on type classes
haftmann
parents:
diff changeset
    86
);
d35daf321b8a class_package - operational view on type classes
haftmann
parents:
diff changeset
    87
18708
4b3dadb4fe33 setup: theory -> theory;
wenzelm
parents: 18702
diff changeset
    88
val _ = Context.add_setup ClassData.init;
18575
9ccfd1d1e874 substantial additions using locales
haftmann
parents: 18550
diff changeset
    89
val print_classes = ClassData.print;
9ccfd1d1e874 substantial additions using locales
haftmann
parents: 18550
diff changeset
    90
19038
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 18963
diff changeset
    91
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 18963
diff changeset
    92
(* queries *)
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 18963
diff changeset
    93
19966
88bbe97ed0b0 refinement in instance command
haftmann
parents: 19957
diff changeset
    94
val lookup_class_data = Option.map rep_classdata oo Symtab.lookup o ClassData.get;
18168
d35daf321b8a class_package - operational view on type classes
haftmann
parents:
diff changeset
    95
19038
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 18963
diff changeset
    96
fun the_class_data thy class =
18168
d35daf321b8a class_package - operational view on type classes
haftmann
parents:
diff changeset
    97
  case lookup_class_data thy class
20455
e671d9eac6c8 removed some dictionary stuff
haftmann
parents: 20428
diff changeset
    98
    of NONE => error ("undeclared constructive class " ^ quote class)
18168
d35daf321b8a class_package - operational view on type classes
haftmann
parents:
diff changeset
    99
     | SOME data => data;
d35daf321b8a class_package - operational view on type classes
haftmann
parents:
diff changeset
   100
19280
5091dc43817b slight improvement in serializer, stub for code generator theorems added
haftmann
parents: 19253
diff changeset
   101
fun is_operational_class thy cls =
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18670
diff changeset
   102
  lookup_class_data thy cls
19966
88bbe97ed0b0 refinement in instance command
haftmann
parents: 19957
diff changeset
   103
  |> Option.map (not o null o #consts o fst)
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18670
diff changeset
   104
  |> the_default false;
18168
d35daf321b8a class_package - operational view on type classes
haftmann
parents:
diff changeset
   105
20455
e671d9eac6c8 removed some dictionary stuff
haftmann
parents: 20428
diff changeset
   106
fun the_ancestry thy classes =
18360
a2c9506b62a7 improved class handling
haftmann
parents: 18335
diff changeset
   107
  let
20455
e671d9eac6c8 removed some dictionary stuff
haftmann
parents: 20428
diff changeset
   108
    fun proj_class class =
19280
5091dc43817b slight improvement in serializer, stub for code generator theorems added
haftmann
parents: 19253
diff changeset
   109
      if is_operational_class thy class
19110
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   110
      then [class]
20455
e671d9eac6c8 removed some dictionary stuff
haftmann
parents: 20428
diff changeset
   111
      else (Sign.certify_sort thy o maps proj_class o Sign.super_classes thy) class;
19038
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 18963
diff changeset
   112
    fun ancestry class anc =
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 18963
diff changeset
   113
      anc
20385
2f52b5aba086 code cleanup, instance_subsort now working
haftmann
parents: 20368
diff changeset
   114
      |> insert (op =) class
20455
e671d9eac6c8 removed some dictionary stuff
haftmann
parents: 20428
diff changeset
   115
      |> fold ancestry ((maps proj_class o Sign.super_classes thy) class);
19038
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 18963
diff changeset
   116
  in fold ancestry classes [] end;
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 18963
diff changeset
   117
20455
e671d9eac6c8 removed some dictionary stuff
haftmann
parents: 20428
diff changeset
   118
val the_parm_map = #consts o fst oo the_class_data;
e671d9eac6c8 removed some dictionary stuff
haftmann
parents: 20428
diff changeset
   119
e671d9eac6c8 removed some dictionary stuff
haftmann
parents: 20428
diff changeset
   120
val the_propnames = #propnames o fst oo the_class_data;
e671d9eac6c8 removed some dictionary stuff
haftmann
parents: 20428
diff changeset
   121
19038
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 18963
diff changeset
   122
fun subst_clsvar v ty_subst =
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 18963
diff changeset
   123
  map_type_tfree (fn u as (w, _) =>
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 18963
diff changeset
   124
    if w = v then ty_subst else TFree u);
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 18963
diff changeset
   125
18168
d35daf321b8a class_package - operational view on type classes
haftmann
parents:
diff changeset
   126
19038
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 18963
diff changeset
   127
(* updaters *)
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 18963
diff changeset
   128
19966
88bbe97ed0b0 refinement in instance command
haftmann
parents: 19957
diff changeset
   129
fun add_class_data (class, (name_locale, name_axclass, var, consts, propnames)) =
19957
91ba241a1678 reduced code, better instance command
haftmann
parents: 19953
diff changeset
   130
  ClassData.map (
19966
88bbe97ed0b0 refinement in instance command
haftmann
parents: 19957
diff changeset
   131
    Symtab.update_new (class, ClassData ({
19957
91ba241a1678 reduced code, better instance command
haftmann
parents: 19953
diff changeset
   132
      name_locale = name_locale,
91ba241a1678 reduced code, better instance command
haftmann
parents: 19953
diff changeset
   133
      name_axclass = name_axclass,
91ba241a1678 reduced code, better instance command
haftmann
parents: 19953
diff changeset
   134
      var = var,
19966
88bbe97ed0b0 refinement in instance command
haftmann
parents: 19957
diff changeset
   135
      consts = consts,
88bbe97ed0b0 refinement in instance command
haftmann
parents: 19957
diff changeset
   136
      propnames = propnames}, Symtab.empty))
19957
91ba241a1678 reduced code, better instance command
haftmann
parents: 19953
diff changeset
   137
  );
19038
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 18963
diff changeset
   138
19966
88bbe97ed0b0 refinement in instance command
haftmann
parents: 19957
diff changeset
   139
fun add_inst_def ((class, tyco), thm) =
88bbe97ed0b0 refinement in instance command
haftmann
parents: 19957
diff changeset
   140
  ClassData.map (
88bbe97ed0b0 refinement in instance command
haftmann
parents: 19957
diff changeset
   141
    Symtab.map_entry class (fn ClassData (classd, instd) =>
88bbe97ed0b0 refinement in instance command
haftmann
parents: 19957
diff changeset
   142
      ClassData (classd, Symtab.insert_list eq_thm (tyco, thm) instd))
88bbe97ed0b0 refinement in instance command
haftmann
parents: 19957
diff changeset
   143
  );
19038
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 18963
diff changeset
   144
20106
a3d4b4eb35b9 adaptions in class_package
haftmann
parents: 20076
diff changeset
   145
20385
2f52b5aba086 code cleanup, instance_subsort now working
haftmann
parents: 20368
diff changeset
   146
(* certification and reading *)
19038
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 18963
diff changeset
   147
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 18963
diff changeset
   148
fun certify_class thy class =
19110
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   149
  (fn class => (the_class_data thy class; class)) (Sign.certify_class thy class);
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   150
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   151
fun certify_sort thy sort =
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   152
  map (fn class => (the_class_data thy class; class)) (Sign.certify_sort thy sort);
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   153
20385
2f52b5aba086 code cleanup, instance_subsort now working
haftmann
parents: 20368
diff changeset
   154
fun read_class thy =
19957
91ba241a1678 reduced code, better instance command
haftmann
parents: 19953
diff changeset
   155
  certify_class thy o Sign.intern_class thy;
19110
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   156
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   157
fun read_sort thy =
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   158
  certify_sort thy o Sign.read_sort thy;
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   159
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   160
20455
e671d9eac6c8 removed some dictionary stuff
haftmann
parents: 20428
diff changeset
   161
e671d9eac6c8 removed some dictionary stuff
haftmann
parents: 20428
diff changeset
   162
(** contexts with arity assumptions **)
20175
0a8ca32f6e64 class package and codegen refinements
haftmann
parents: 20106
diff changeset
   163
0a8ca32f6e64 class package and codegen refinements
haftmann
parents: 20106
diff changeset
   164
fun assume_arities_of_sort thy arities ty_sort =
0a8ca32f6e64 class package and codegen refinements
haftmann
parents: 20106
diff changeset
   165
  let
0a8ca32f6e64 class package and codegen refinements
haftmann
parents: 20106
diff changeset
   166
    val pp = Sign.pp thy;
0a8ca32f6e64 class package and codegen refinements
haftmann
parents: 20106
diff changeset
   167
    val algebra = Sign.classes_of thy
0a8ca32f6e64 class package and codegen refinements
haftmann
parents: 20106
diff changeset
   168
      |> fold (fn ((tyco, asorts), sort) =>
0a8ca32f6e64 class package and codegen refinements
haftmann
parents: 20106
diff changeset
   169
           Sorts.add_arities pp (tyco, map (fn class => (class, asorts)) sort)) arities;
0a8ca32f6e64 class package and codegen refinements
haftmann
parents: 20106
diff changeset
   170
  in Sorts.of_sort algebra ty_sort end;
0a8ca32f6e64 class package and codegen refinements
haftmann
parents: 20106
diff changeset
   171
0a8ca32f6e64 class package and codegen refinements
haftmann
parents: 20106
diff changeset
   172
fun assume_arities_thy thy arities f =
0a8ca32f6e64 class package and codegen refinements
haftmann
parents: 20106
diff changeset
   173
  let
0a8ca32f6e64 class package and codegen refinements
haftmann
parents: 20106
diff changeset
   174
    val thy_read = (fold (fn ((tyco, asorts), sort)
0a8ca32f6e64 class package and codegen refinements
haftmann
parents: 20106
diff changeset
   175
      => Sign.primitive_arity (tyco, asorts, sort)) arities o Theory.copy) thy
0a8ca32f6e64 class package and codegen refinements
haftmann
parents: 20106
diff changeset
   176
  in f thy_read end;
0a8ca32f6e64 class package and codegen refinements
haftmann
parents: 20106
diff changeset
   177
20455
e671d9eac6c8 removed some dictionary stuff
haftmann
parents: 20428
diff changeset
   178
e671d9eac6c8 removed some dictionary stuff
haftmann
parents: 20428
diff changeset
   179
e671d9eac6c8 removed some dictionary stuff
haftmann
parents: 20428
diff changeset
   180
(** tactics and methods **)
19110
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   181
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   182
fun intro_classes_tac facts st =
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   183
  (ALLGOALS (Method.insert_tac facts THEN'
19928
cb8472f4c5fd switched to open locales for classes
haftmann
parents: 19806
diff changeset
   184
      REPEAT_ALL_NEW (resolve_tac (AxClass.class_intros (Thm.theory_of_thm st))))
19110
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   185
    THEN Tactic.distinct_subgoals_tac) st;
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   186
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   187
fun default_intro_classes_tac [] = intro_classes_tac []
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   188
  | default_intro_classes_tac _ = Tactical.no_tac;    (*no error message!*)
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   189
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   190
fun default_tac rules ctxt facts =
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   191
  HEADGOAL (Method.some_rule_tac rules ctxt facts) ORELSE
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   192
    default_intro_classes_tac facts;
19038
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 18963
diff changeset
   193
19468
0afdd5023bfc Sign.arity_sorts;
wenzelm
parents: 19456
diff changeset
   194
val _ = Context.add_setup (Method.add_methods
0afdd5023bfc Sign.arity_sorts;
wenzelm
parents: 19456
diff changeset
   195
 [("intro_classes", Method.no_args (Method.METHOD intro_classes_tac),
0afdd5023bfc Sign.arity_sorts;
wenzelm
parents: 19456
diff changeset
   196
    "back-chain introduction rules of classes"),
0afdd5023bfc Sign.arity_sorts;
wenzelm
parents: 19456
diff changeset
   197
  ("default", Method.thms_ctxt_args (Method.METHOD oo default_tac),
0afdd5023bfc Sign.arity_sorts;
wenzelm
parents: 19456
diff changeset
   198
    "apply some intro/elim rule")]);
0afdd5023bfc Sign.arity_sorts;
wenzelm
parents: 19456
diff changeset
   199
19038
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 18963
diff changeset
   200
20455
e671d9eac6c8 removed some dictionary stuff
haftmann
parents: 20428
diff changeset
   201
e671d9eac6c8 removed some dictionary stuff
haftmann
parents: 20428
diff changeset
   202
(** axclass instances **)
19246
aa45f5e456d3 added axclass_instance_XXX (from axclass.ML);
wenzelm
parents: 19233
diff changeset
   203
aa45f5e456d3 added axclass_instance_XXX (from axclass.ML);
wenzelm
parents: 19233
diff changeset
   204
local
aa45f5e456d3 added axclass_instance_XXX (from axclass.ML);
wenzelm
parents: 19233
diff changeset
   205
20175
0a8ca32f6e64 class package and codegen refinements
haftmann
parents: 20106
diff changeset
   206
fun gen_instance mk_prop add_thm after_qed insts thy =
20368
2461a0485623 global goals/qeds: after_qed operates on Proof.context (potentially local_theory);
wenzelm
parents: 20353
diff changeset
   207
  let
2461a0485623 global goals/qeds: after_qed operates on Proof.context (potentially local_theory);
wenzelm
parents: 20353
diff changeset
   208
    fun after_qed' results =
2461a0485623 global goals/qeds: after_qed operates on Proof.context (potentially local_theory);
wenzelm
parents: 20353
diff changeset
   209
      ProofContext.theory ((fold o fold) add_thm results #> after_qed);
2461a0485623 global goals/qeds: after_qed operates on Proof.context (potentially local_theory);
wenzelm
parents: 20353
diff changeset
   210
  in
2461a0485623 global goals/qeds: after_qed operates on Proof.context (potentially local_theory);
wenzelm
parents: 20353
diff changeset
   211
    thy
2461a0485623 global goals/qeds: after_qed operates on Proof.context (potentially local_theory);
wenzelm
parents: 20353
diff changeset
   212
    |> ProofContext.init
2461a0485623 global goals/qeds: after_qed operates on Proof.context (potentially local_theory);
wenzelm
parents: 20353
diff changeset
   213
    |> Proof.theorem_i PureThy.internalK NONE after_qed' NONE ("", [])
2461a0485623 global goals/qeds: after_qed operates on Proof.context (potentially local_theory);
wenzelm
parents: 20353
diff changeset
   214
      ((map (fn t => (("", []), [(t, [])])) o maps (mk_prop thy)) insts)
2461a0485623 global goals/qeds: after_qed operates on Proof.context (potentially local_theory);
wenzelm
parents: 20353
diff changeset
   215
  end;
19246
aa45f5e456d3 added axclass_instance_XXX (from axclass.ML);
wenzelm
parents: 19233
diff changeset
   216
aa45f5e456d3 added axclass_instance_XXX (from axclass.ML);
wenzelm
parents: 19233
diff changeset
   217
in
aa45f5e456d3 added axclass_instance_XXX (from axclass.ML);
wenzelm
parents: 19233
diff changeset
   218
aa45f5e456d3 added axclass_instance_XXX (from axclass.ML);
wenzelm
parents: 19233
diff changeset
   219
val axclass_instance_arity =
19412
cc08bcabdcd2 classes: prefer thy operations;
wenzelm
parents: 19395
diff changeset
   220
  gen_instance (Logic.mk_arities oo Sign.read_arity) AxClass.add_arity;
19246
aa45f5e456d3 added axclass_instance_XXX (from axclass.ML);
wenzelm
parents: 19233
diff changeset
   221
val axclass_instance_arity_i =
19412
cc08bcabdcd2 classes: prefer thy operations;
wenzelm
parents: 19395
diff changeset
   222
  gen_instance (Logic.mk_arities oo Sign.cert_arity) AxClass.add_arity;
20182
79c9ff40d760 tactic for prove_instance_arity now gets definition theorems as arguments
haftmann
parents: 20175
diff changeset
   223
val axclass_instance_sort =      
79c9ff40d760 tactic for prove_instance_arity now gets definition theorems as arguments
haftmann
parents: 20175
diff changeset
   224
  gen_instance (single oo (Logic.mk_classrel oo AxClass.read_classrel)) AxClass.add_classrel I o single;
19246
aa45f5e456d3 added axclass_instance_XXX (from axclass.ML);
wenzelm
parents: 19233
diff changeset
   225
aa45f5e456d3 added axclass_instance_XXX (from axclass.ML);
wenzelm
parents: 19233
diff changeset
   226
end;
aa45f5e456d3 added axclass_instance_XXX (from axclass.ML);
wenzelm
parents: 19233
diff changeset
   227
aa45f5e456d3 added axclass_instance_XXX (from axclass.ML);
wenzelm
parents: 19233
diff changeset
   228
20455
e671d9eac6c8 removed some dictionary stuff
haftmann
parents: 20428
diff changeset
   229
e671d9eac6c8 removed some dictionary stuff
haftmann
parents: 20428
diff changeset
   230
(** classes and instances **)
19038
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 18963
diff changeset
   231
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 18963
diff changeset
   232
local
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 18963
diff changeset
   233
19957
91ba241a1678 reduced code, better instance command
haftmann
parents: 19953
diff changeset
   234
fun add_axclass_i (name, supsort) params axs thy =
19110
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   235
  let
19395
edf92521e8d3 simplified AxClass.add_axclass interface;
wenzelm
parents: 19340
diff changeset
   236
    val (c, thy') = thy
19957
91ba241a1678 reduced code, better instance command
haftmann
parents: 19953
diff changeset
   237
      |> AxClass.define_class_i (name, supsort) params axs;
19518
5204c73a4d46 AxClass.define_class_i, AxClass.get_definition;
wenzelm
parents: 19482
diff changeset
   238
    val {intro, axioms, ...} = AxClass.get_definition thy' c;
19395
edf92521e8d3 simplified AxClass.add_axclass interface;
wenzelm
parents: 19340
diff changeset
   239
  in ((c, (intro, axioms)), thy') end;
19110
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   240
20385
2f52b5aba086 code cleanup, instance_subsort now working
haftmann
parents: 20368
diff changeset
   241
(*FIXME proper locale interface*)
19110
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   242
fun prove_interpretation_i (prfx, atts) expr insts tac thy =
19038
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 18963
diff changeset
   243
  let
20368
2461a0485623 global goals/qeds: after_qed operates on Proof.context (potentially local_theory);
wenzelm
parents: 20353
diff changeset
   244
    fun ad_hoc_term (Const (c, ty)) =
19110
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   245
          let
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   246
            val p = setmp show_types true (setmp show_sorts true (setmp print_mode [] (Sign.pretty_typ thy))) ty;
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   247
            val s = c ^ "::" ^ Pretty.output p;
20368
2461a0485623 global goals/qeds: after_qed operates on Proof.context (potentially local_theory);
wenzelm
parents: 20353
diff changeset
   248
          in s end
2461a0485623 global goals/qeds: after_qed operates on Proof.context (potentially local_theory);
wenzelm
parents: 20353
diff changeset
   249
      | ad_hoc_term t =
19110
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   250
          let
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   251
            val p = setmp show_types true (setmp show_sorts true (setmp print_mode [] (Sign.pretty_term thy))) t;
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   252
            val s = Pretty.output p;
20368
2461a0485623 global goals/qeds: after_qed operates on Proof.context (potentially local_theory);
wenzelm
parents: 20353
diff changeset
   253
          in s end;
19110
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   254
  in
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   255
    thy
20368
2461a0485623 global goals/qeds: after_qed operates on Proof.context (potentially local_theory);
wenzelm
parents: 20353
diff changeset
   256
    |> Locale.interpretation I (prfx, atts) expr (map (Option.map ad_hoc_term) insts)
19110
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   257
    |> Proof.global_terminal_proof (Method.Basic (fn _ => Method.SIMPLE_METHOD tac), NONE)
20368
2461a0485623 global goals/qeds: after_qed operates on Proof.context (potentially local_theory);
wenzelm
parents: 20353
diff changeset
   258
    |> ProofContext.theory_of
19110
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   259
  end;
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   260
19150
1457d810b408 class package and codegen refinements
haftmann
parents: 19136
diff changeset
   261
fun gen_class add_locale prep_class bname raw_supclasses raw_elems thy =
19110
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   262
  let
20428
67fa1c6ba89e refinements
haftmann
parents: 20403
diff changeset
   263
    val (elems, exprs) = fold_rev (fn Locale.Elem e => apfst (cons e)
67fa1c6ba89e refinements
haftmann
parents: 20403
diff changeset
   264
                                   | Locale.Expr e => apsnd (cons e))
67fa1c6ba89e refinements
haftmann
parents: 20403
diff changeset
   265
      raw_elems ([], []);
19110
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   266
    val supclasses = map (prep_class thy) raw_supclasses;
19038
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 18963
diff changeset
   267
    val supsort =
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 18963
diff changeset
   268
      supclasses
19966
88bbe97ed0b0 refinement in instance command
haftmann
parents: 19957
diff changeset
   269
      |> map (#name_axclass o fst o the_class_data thy)
19468
0afdd5023bfc Sign.arity_sorts;
wenzelm
parents: 19456
diff changeset
   270
      |> Sign.certify_sort thy
19038
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 18963
diff changeset
   271
      |> null ? K (Sign.defaultS thy);
20428
67fa1c6ba89e refinements
haftmann
parents: 20403
diff changeset
   272
    val expr_supclasses = Locale.Merge
67fa1c6ba89e refinements
haftmann
parents: 20403
diff changeset
   273
      (map (Locale.Locale o #name_locale o fst o the_class_data thy) supclasses);
67fa1c6ba89e refinements
haftmann
parents: 20403
diff changeset
   274
    val expr = Locale.Merge (map (Locale.Locale o #name_locale o fst o the_class_data thy) supclasses
67fa1c6ba89e refinements
haftmann
parents: 20403
diff changeset
   275
      @ exprs);
19280
5091dc43817b slight improvement in serializer, stub for code generator theorems added
haftmann
parents: 19253
diff changeset
   276
    val mapp_sup = AList.make
19468
0afdd5023bfc Sign.arity_sorts;
wenzelm
parents: 19456
diff changeset
   277
      (the o AList.lookup (op =) ((flat o map (the_parm_map thy) o the_ancestry thy) supclasses))
20428
67fa1c6ba89e refinements
haftmann
parents: 20403
diff changeset
   278
      ((map (fst o fst) o Locale.parameters_of_expr thy) expr_supclasses);
19038
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 18963
diff changeset
   279
    fun extract_tyvar_consts thy name_locale =
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 18963
diff changeset
   280
      let
20175
0a8ca32f6e64 class package and codegen refinements
haftmann
parents: 20106
diff changeset
   281
        fun extract_classvar ((c, ty), _) w =
20428
67fa1c6ba89e refinements
haftmann
parents: 20403
diff changeset
   282
          (case Term.add_tfreesT ty []
20175
0a8ca32f6e64 class package and codegen refinements
haftmann
parents: 20106
diff changeset
   283
           of [(v, _)] => (case w
20385
2f52b5aba086 code cleanup, instance_subsort now working
haftmann
parents: 20368
diff changeset
   284
               of SOME u => if u = v then w else error ("Additonal type variable in type signature of constant " ^ quote c)
20175
0a8ca32f6e64 class package and codegen refinements
haftmann
parents: 20106
diff changeset
   285
                | NONE => SOME v)
20385
2f52b5aba086 code cleanup, instance_subsort now working
haftmann
parents: 20368
diff changeset
   286
            | [] => error ("No type variable in type signature of constant " ^ quote c)
2f52b5aba086 code cleanup, instance_subsort now working
haftmann
parents: 20368
diff changeset
   287
            | _ => error ("More than one type variable in type signature of constant " ^ quote c));
19110
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   288
        val consts1 =
19038
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 18963
diff changeset
   289
          Locale.parameters_of thy name_locale
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 18963
diff changeset
   290
          |> map (apsnd Syntax.unlocalize_mixfix)
20175
0a8ca32f6e64 class package and codegen refinements
haftmann
parents: 20106
diff changeset
   291
        val SOME v = fold extract_classvar consts1 NONE;
19110
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   292
        val consts2 = map ((apfst o apsnd) (subst_clsvar v (TFree (v, [])))) consts1;
19280
5091dc43817b slight improvement in serializer, stub for code generator theorems added
haftmann
parents: 19253
diff changeset
   293
      in (v, chop (length mapp_sup) consts2) end;
19110
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   294
    fun add_consts v raw_cs_sup raw_cs_this thy =
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   295
      let
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   296
        fun add_global_const ((c, ty), syn) thy =
20265
219a996d8bde add_consts: proper Sign.full_name;
wenzelm
parents: 20192
diff changeset
   297
          ((c, (Sign.full_name thy c, ty)),
219a996d8bde add_consts: proper Sign.full_name;
wenzelm
parents: 20192
diff changeset
   298
            thy
20703
f3f2b1091ea0 adding constants the modern way
haftmann
parents: 20628
diff changeset
   299
            |> Sign.add_consts_authentic [(c, ty |> subst_clsvar v (TFree (v, Sign.defaultS thy)), syn)]);
19110
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   300
      in
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   301
        thy
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   302
        |> fold_map add_global_const raw_cs_this
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   303
      end;
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   304
    fun extract_assumes thy name_locale cs_mapp =
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   305
      let
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   306
        val subst_assume =
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   307
          map_aterms (fn Free (c, ty) => Const ((fst o the o AList.lookup (op =) cs_mapp) c, ty)
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   308
                       | t => t)
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   309
        fun prep_asm ((name, atts), ts) =
19957
91ba241a1678 reduced code, better instance command
haftmann
parents: 19953
diff changeset
   310
          ((NameSpace.base name, map (Attrib.attribute thy) atts), map subst_assume ts)
19110
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   311
      in
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   312
        (map prep_asm o Locale.local_asms_of thy) name_locale
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   313
      end;
19280
5091dc43817b slight improvement in serializer, stub for code generator theorems added
haftmann
parents: 19253
diff changeset
   314
    fun add_global_constraint v class (_, (c, ty)) thy =
19038
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 18963
diff changeset
   315
      thy
19136
00ade10f611d some refinements
haftmann
parents: 19134
diff changeset
   316
      |> Sign.add_const_constraint_i (c, SOME (subst_clsvar v (TFree (v, [class])) ty));
19110
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   317
    fun mk_const thy class v (c, ty) =
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   318
      Const (c, subst_clsvar v (TFree (v, [class])) ty);
19038
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 18963
diff changeset
   319
  in
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 18963
diff changeset
   320
    thy
20428
67fa1c6ba89e refinements
haftmann
parents: 20403
diff changeset
   321
    |> add_locale bname expr elems
19928
cb8472f4c5fd switched to open locales for classes
haftmann
parents: 19806
diff changeset
   322
    |-> (fn (name_locale, ctxt) =>
19038
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 18963
diff changeset
   323
          `(fn thy => extract_tyvar_consts thy name_locale)
19110
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   324
    #-> (fn (v, (raw_cs_sup, raw_cs_this)) =>
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   325
          add_consts v raw_cs_sup raw_cs_this
19280
5091dc43817b slight improvement in serializer, stub for code generator theorems added
haftmann
parents: 19253
diff changeset
   326
    #-> (fn mapp_this =>
5091dc43817b slight improvement in serializer, stub for code generator theorems added
haftmann
parents: 19253
diff changeset
   327
          `(fn thy => extract_assumes thy name_locale (mapp_sup @ mapp_this))
19110
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   328
    #-> (fn loc_axioms =>
19957
91ba241a1678 reduced code, better instance command
haftmann
parents: 19953
diff changeset
   329
          add_axclass_i (bname, supsort) (map (fst o snd) mapp_this) loc_axioms
19110
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   330
    #-> (fn (name_axclass, (_, ax_axioms)) =>
19280
5091dc43817b slight improvement in serializer, stub for code generator theorems added
haftmann
parents: 19253
diff changeset
   331
          fold (add_global_constraint v name_axclass) mapp_this
19966
88bbe97ed0b0 refinement in instance command
haftmann
parents: 19957
diff changeset
   332
    #> add_class_data (name_locale, (name_locale, name_axclass, v, mapp_this,
88bbe97ed0b0 refinement in instance command
haftmann
parents: 19957
diff changeset
   333
         map (fst o fst) loc_axioms))
20601
3e1caf5a07c6 classical arity syntax
haftmann
parents: 20548
diff changeset
   334
    #> prove_interpretation_i (bname, [])
19280
5091dc43817b slight improvement in serializer, stub for code generator theorems added
haftmann
parents: 19253
diff changeset
   335
          (Locale.Locale name_locale) (map (SOME o mk_const thy name_axclass v) (map snd (mapp_sup @ mapp_this)))
19929
5c882c3e610b switched to open locales for classes
haftmann
parents: 19928
diff changeset
   336
          ((ALLGOALS o ProofContext.fact_tac) ax_axioms)
19110
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   337
    #> pair ctxt
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   338
    )))))
19038
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 18963
diff changeset
   339
  end;
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 18963
diff changeset
   340
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 18963
diff changeset
   341
in
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 18963
diff changeset
   342
20385
2f52b5aba086 code cleanup, instance_subsort now working
haftmann
parents: 20368
diff changeset
   343
val class = gen_class (Locale.add_locale false) read_class;
19928
cb8472f4c5fd switched to open locales for classes
haftmann
parents: 19806
diff changeset
   344
val class_i = gen_class (Locale.add_locale_i false) certify_class;
19038
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 18963
diff changeset
   345
20455
e671d9eac6c8 removed some dictionary stuff
haftmann
parents: 20428
diff changeset
   346
end; (*local*)
19038
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 18963
diff changeset
   347
19110
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   348
local
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   349
20175
0a8ca32f6e64 class package and codegen refinements
haftmann
parents: 20106
diff changeset
   350
fun gen_read_def thy prep_att read_def ((raw_name, raw_atts), raw_t) =
19038
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 18963
diff changeset
   351
  let
19957
91ba241a1678 reduced code, better instance command
haftmann
parents: 19953
diff changeset
   352
    val (_, t) = read_def thy (raw_name, raw_t);
91ba241a1678 reduced code, better instance command
haftmann
parents: 19953
diff changeset
   353
    val ((c, ty), _) = Sign.cert_def (Sign.pp thy) t;
91ba241a1678 reduced code, better instance command
haftmann
parents: 19953
diff changeset
   354
    val atts = map (prep_att thy) raw_atts;
20175
0a8ca32f6e64 class package and codegen refinements
haftmann
parents: 20106
diff changeset
   355
    val insts = (Consts.typargs (Sign.consts_of thy) (c, ty))
19957
91ba241a1678 reduced code, better instance command
haftmann
parents: 19953
diff changeset
   356
    val name = case raw_name
20385
2f52b5aba086 code cleanup, instance_subsort now working
haftmann
parents: 20368
diff changeset
   357
     of "" => NONE
2f52b5aba086 code cleanup, instance_subsort now working
haftmann
parents: 20368
diff changeset
   358
      | _ => SOME raw_name;
20175
0a8ca32f6e64 class package and codegen refinements
haftmann
parents: 20106
diff changeset
   359
  in (c, (insts, ((name, t), atts))) end;
19110
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   360
19957
91ba241a1678 reduced code, better instance command
haftmann
parents: 19953
diff changeset
   361
fun read_def thy = gen_read_def thy Attrib.attribute read_axm;
91ba241a1678 reduced code, better instance command
haftmann
parents: 19953
diff changeset
   362
fun read_def_i thy = gen_read_def thy (K I) (K I);
19110
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   363
20175
0a8ca32f6e64 class package and codegen refinements
haftmann
parents: 20106
diff changeset
   364
fun gen_instance_arity prep_arity prep_att read_def do_proof raw_arities (raw_name, raw_atts) raw_defs theory =
19110
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   365
  let
20175
0a8ca32f6e64 class package and codegen refinements
haftmann
parents: 20106
diff changeset
   366
    fun prep_arity' ((tyco, asorts), sort) = prep_arity theory (tyco, asorts, sort);
0a8ca32f6e64 class package and codegen refinements
haftmann
parents: 20106
diff changeset
   367
    val arities = map prep_arity' raw_arities;
0a8ca32f6e64 class package and codegen refinements
haftmann
parents: 20106
diff changeset
   368
    val arities_pair = map (fn (tyco, asorts, sort) => ((tyco, asorts), sort)) arities;
0a8ca32f6e64 class package and codegen refinements
haftmann
parents: 20106
diff changeset
   369
    val _ = if null arities then error "at least one arity must be given" else ();
0a8ca32f6e64 class package and codegen refinements
haftmann
parents: 20106
diff changeset
   370
    val _ = case (duplicates (op =) o map #1) arities
0a8ca32f6e64 class package and codegen refinements
haftmann
parents: 20106
diff changeset
   371
     of [] => ()
0a8ca32f6e64 class package and codegen refinements
haftmann
parents: 20106
diff changeset
   372
      | dupl_tycos => error ("type constructors occur more than once in arities: "
0a8ca32f6e64 class package and codegen refinements
haftmann
parents: 20106
diff changeset
   373
        ^ (commas o map quote) dupl_tycos);
20393
df3252bbc0e6 fixed bug in sortcontext extraction
haftmann
parents: 20385
diff changeset
   374
    val (bind_always, name) = case raw_name
df3252bbc0e6 fixed bug in sortcontext extraction
haftmann
parents: 20385
diff changeset
   375
     of "" => (false, Thm.def_name ((space_implode "_" o map NameSpace.base)
20175
0a8ca32f6e64 class package and codegen refinements
haftmann
parents: 20106
diff changeset
   376
                (maps (fn (tyco, _, sort) => sort @ [tyco])
20393
df3252bbc0e6 fixed bug in sortcontext extraction
haftmann
parents: 20385
diff changeset
   377
                (sort (fn ((tyco1, _, _), (tyco2, _, _)) => string_ord (tyco1, tyco2)) arities))))
df3252bbc0e6 fixed bug in sortcontext extraction
haftmann
parents: 20385
diff changeset
   378
      | _ => (true, raw_name);
19136
00ade10f611d some refinements
haftmann
parents: 19134
diff changeset
   379
    val atts = map (prep_att theory) raw_atts;
20175
0a8ca32f6e64 class package and codegen refinements
haftmann
parents: 20106
diff changeset
   380
    fun already_defined (c, ty_inst) = 
0a8ca32f6e64 class package and codegen refinements
haftmann
parents: 20106
diff changeset
   381
      is_some (find_first (fn (_, { lhs = [ty_inst'], ...}) =>
0a8ca32f6e64 class package and codegen refinements
haftmann
parents: 20106
diff changeset
   382
          Sign.typ_instance theory (ty_inst', ty_inst) orelse Sign.typ_instance theory (ty_inst, ty_inst'))
0a8ca32f6e64 class package and codegen refinements
haftmann
parents: 20106
diff changeset
   383
        (Defs.specifications_of (Theory.defs_of theory) c));
0a8ca32f6e64 class package and codegen refinements
haftmann
parents: 20106
diff changeset
   384
    fun get_consts_class tyco ty class =
19110
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   385
      let
19966
88bbe97ed0b0 refinement in instance command
haftmann
parents: 19957
diff changeset
   386
        val data = (fst o the_class_data theory) class;
19957
91ba241a1678 reduced code, better instance command
haftmann
parents: 19953
diff changeset
   387
        val subst_ty = map_type_tfree (fn (v, sort) =>
20175
0a8ca32f6e64 class package and codegen refinements
haftmann
parents: 20106
diff changeset
   388
          if #var data = v then ty else TVar ((v, 0), sort));
19957
91ba241a1678 reduced code, better instance command
haftmann
parents: 19953
diff changeset
   389
      in
91ba241a1678 reduced code, better instance command
haftmann
parents: 19953
diff changeset
   390
        (map_filter (fn (_, (c, ty)) =>
20175
0a8ca32f6e64 class package and codegen refinements
haftmann
parents: 20106
diff changeset
   391
          if already_defined (c, ty)
0a8ca32f6e64 class package and codegen refinements
haftmann
parents: 20106
diff changeset
   392
          then NONE else SOME ((c, ((tyco, class), subst_ty ty)))) o #consts) data
19957
91ba241a1678 reduced code, better instance command
haftmann
parents: 19953
diff changeset
   393
      end;
20175
0a8ca32f6e64 class package and codegen refinements
haftmann
parents: 20106
diff changeset
   394
    fun get_consts_sort (tyco, asorts, sort) =
19957
91ba241a1678 reduced code, better instance command
haftmann
parents: 19953
diff changeset
   395
      let
20192
956cd30ef3be renamed Name.give_names to Name.names and moved Name.alphanum to Symbol.alphanum
haftmann
parents: 20191
diff changeset
   396
        val ty = Type (tyco, map (fn (v, sort) => TVar ((v, 0), sort)) (Name.names Name.context "'a" asorts))
20175
0a8ca32f6e64 class package and codegen refinements
haftmann
parents: 20106
diff changeset
   397
      in maps (get_consts_class tyco ty) (the_ancestry theory sort) end;
0a8ca32f6e64 class package and codegen refinements
haftmann
parents: 20106
diff changeset
   398
    val cs = maps get_consts_sort arities;
20385
2f52b5aba086 code cleanup, instance_subsort now working
haftmann
parents: 20368
diff changeset
   399
    fun mk_typnorm thy (ty, ty_sc) =
2f52b5aba086 code cleanup, instance_subsort now working
haftmann
parents: 20368
diff changeset
   400
      case try (Sign.typ_match thy (Logic.varifyT ty_sc, ty)) Vartab.empty
2f52b5aba086 code cleanup, instance_subsort now working
haftmann
parents: 20368
diff changeset
   401
       of SOME env => SOME (Logic.varifyT #> Envir.typ_subst_TVars env #> Logic.unvarifyT)
2f52b5aba086 code cleanup, instance_subsort now working
haftmann
parents: 20368
diff changeset
   402
        | NONE => NONE;
20175
0a8ca32f6e64 class package and codegen refinements
haftmann
parents: 20106
diff changeset
   403
    fun read_defs defs cs thy_read =
0a8ca32f6e64 class package and codegen refinements
haftmann
parents: 20106
diff changeset
   404
      let
19957
91ba241a1678 reduced code, better instance command
haftmann
parents: 19953
diff changeset
   405
        fun read raw_def cs =
91ba241a1678 reduced code, better instance command
haftmann
parents: 19953
diff changeset
   406
          let
20385
2f52b5aba086 code cleanup, instance_subsort now working
haftmann
parents: 20368
diff changeset
   407
            val (c, (inst, ((name_opt, t), atts))) = read_def thy_read raw_def;
2f52b5aba086 code cleanup, instance_subsort now working
haftmann
parents: 20368
diff changeset
   408
            val ty = Consts.instance (Sign.consts_of thy_read) (c, inst);
20175
0a8ca32f6e64 class package and codegen refinements
haftmann
parents: 20106
diff changeset
   409
            val ((tyco, class), ty') = case AList.lookup (op =) cs c
19957
91ba241a1678 reduced code, better instance command
haftmann
parents: 19953
diff changeset
   410
             of NONE => error ("superfluous definition for constant " ^ quote c)
19966
88bbe97ed0b0 refinement in instance command
haftmann
parents: 19957
diff changeset
   411
              | SOME class_ty => class_ty;
20385
2f52b5aba086 code cleanup, instance_subsort now working
haftmann
parents: 20368
diff changeset
   412
            val name = case name_opt
20628
b15b6f05d145 Logic.name_classrel/arities;
wenzelm
parents: 20601
diff changeset
   413
             of NONE => Thm.def_name (Logic.name_arity (tyco, [], c))
20385
2f52b5aba086 code cleanup, instance_subsort now working
haftmann
parents: 20368
diff changeset
   414
              | SOME name => name;
2f52b5aba086 code cleanup, instance_subsort now working
haftmann
parents: 20368
diff changeset
   415
            val t' = case mk_typnorm thy_read (ty', ty)
19966
88bbe97ed0b0 refinement in instance command
haftmann
parents: 19957
diff changeset
   416
             of NONE => error ("superfluous definition for constant " ^
88bbe97ed0b0 refinement in instance command
haftmann
parents: 19957
diff changeset
   417
                  quote c ^ "::" ^ Sign.string_of_typ thy_read ty)
20548
8ef25fe585a8 renamed Term.map_term_types to Term.map_types (cf. Term.fold_types);
wenzelm
parents: 20465
diff changeset
   418
              | SOME norm => map_types norm t
20175
0a8ca32f6e64 class package and codegen refinements
haftmann
parents: 20106
diff changeset
   419
          in (((class, tyco), ((name, t'), atts)), AList.delete (op =) c cs) end;
19957
91ba241a1678 reduced code, better instance command
haftmann
parents: 19953
diff changeset
   420
      in fold_map read defs cs end;
20175
0a8ca32f6e64 class package and codegen refinements
haftmann
parents: 20106
diff changeset
   421
    val (defs, _) = assume_arities_thy theory arities_pair (read_defs raw_defs cs);
19110
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   422
    fun get_remove_contraint c thy =
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   423
      let
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   424
        val ty = Sign.the_const_constraint thy c;
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   425
      in
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   426
        thy
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   427
        |> Sign.add_const_constraint_i (c, NONE)
20385
2f52b5aba086 code cleanup, instance_subsort now working
haftmann
parents: 20368
diff changeset
   428
        |> pair (c, Logic.unvarifyT ty)
19110
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   429
      end;
19966
88bbe97ed0b0 refinement in instance command
haftmann
parents: 19957
diff changeset
   430
    fun add_defs defs thy =
88bbe97ed0b0 refinement in instance command
haftmann
parents: 19957
diff changeset
   431
      thy
88bbe97ed0b0 refinement in instance command
haftmann
parents: 19957
diff changeset
   432
      |> PureThy.add_defs_i true (map snd defs)
88bbe97ed0b0 refinement in instance command
haftmann
parents: 19957
diff changeset
   433
      |-> (fn thms => pair (map fst defs ~~ thms));
88bbe97ed0b0 refinement in instance command
haftmann
parents: 19957
diff changeset
   434
    fun note_all thy =
20175
0a8ca32f6e64 class package and codegen refinements
haftmann
parents: 20106
diff changeset
   435
      (*FIXME: should avoid binding duplicated names*)
19966
88bbe97ed0b0 refinement in instance command
haftmann
parents: 19957
diff changeset
   436
      let
20393
df3252bbc0e6 fixed bug in sortcontext extraction
haftmann
parents: 20385
diff changeset
   437
        val bind = bind_always orelse not (can (PureThy.get_thms thy) (Name name));
20175
0a8ca32f6e64 class package and codegen refinements
haftmann
parents: 20106
diff changeset
   438
        val thms = maps (fn (tyco, _, sort) => maps (fn class =>
0a8ca32f6e64 class package and codegen refinements
haftmann
parents: 20106
diff changeset
   439
          Symtab.lookup_list
0a8ca32f6e64 class package and codegen refinements
haftmann
parents: 20106
diff changeset
   440
            ((snd o the_class_data thy) class) tyco) (the_ancestry thy sort)) arities;
20393
df3252bbc0e6 fixed bug in sortcontext extraction
haftmann
parents: 20385
diff changeset
   441
      in if bind then
19966
88bbe97ed0b0 refinement in instance command
haftmann
parents: 19957
diff changeset
   442
        thy
20601
3e1caf5a07c6 classical arity syntax
haftmann
parents: 20548
diff changeset
   443
        |> PureThy.note_thmss_i (*qualified*) PureThy.internalK [((name, atts), [(thms, [])])]
19966
88bbe97ed0b0 refinement in instance command
haftmann
parents: 19957
diff changeset
   444
        |> snd
20393
df3252bbc0e6 fixed bug in sortcontext extraction
haftmann
parents: 20385
diff changeset
   445
      else
df3252bbc0e6 fixed bug in sortcontext extraction
haftmann
parents: 20385
diff changeset
   446
        thy
19966
88bbe97ed0b0 refinement in instance command
haftmann
parents: 19957
diff changeset
   447
      end;
19110
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   448
    fun after_qed cs thy =
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   449
      thy
19412
cc08bcabdcd2 classes: prefer thy operations;
wenzelm
parents: 19395
diff changeset
   450
      |> fold Sign.add_const_constraint_i (map (apsnd SOME) cs);
19038
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 18963
diff changeset
   451
  in
19110
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   452
    theory
20385
2f52b5aba086 code cleanup, instance_subsort now working
haftmann
parents: 20368
diff changeset
   453
    |> fold_map get_remove_contraint (map fst cs |> distinct (op =))
19966
88bbe97ed0b0 refinement in instance command
haftmann
parents: 19957
diff changeset
   454
    ||>> add_defs defs
20076
def4ad161528 Name.invent_list;
wenzelm
parents: 19966
diff changeset
   455
    |-> (fn (cs, def_thms) =>
20175
0a8ca32f6e64 class package and codegen refinements
haftmann
parents: 20106
diff changeset
   456
       fold add_inst_def def_thms
19966
88bbe97ed0b0 refinement in instance command
haftmann
parents: 19957
diff changeset
   457
    #> note_all
20182
79c9ff40d760 tactic for prove_instance_arity now gets definition theorems as arguments
haftmann
parents: 20175
diff changeset
   458
    #> do_proof (map snd def_thms) (after_qed cs) arities)
19038
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 18963
diff changeset
   459
  end;
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 18963
diff changeset
   460
19957
91ba241a1678 reduced code, better instance command
haftmann
parents: 19953
diff changeset
   461
fun instance_arity' do_proof = gen_instance_arity Sign.read_arity Attrib.attribute
91ba241a1678 reduced code, better instance command
haftmann
parents: 19953
diff changeset
   462
  read_def do_proof;
91ba241a1678 reduced code, better instance command
haftmann
parents: 19953
diff changeset
   463
fun instance_arity_i' do_proof = gen_instance_arity Sign.cert_arity (K I)
91ba241a1678 reduced code, better instance command
haftmann
parents: 19953
diff changeset
   464
  read_def_i do_proof;
20182
79c9ff40d760 tactic for prove_instance_arity now gets definition theorems as arguments
haftmann
parents: 20175
diff changeset
   465
fun tactic_proof tac def_thms after_qed arities =
79c9ff40d760 tactic for prove_instance_arity now gets definition theorems as arguments
haftmann
parents: 20175
diff changeset
   466
  fold (fn arity => AxClass.prove_arity arity (tac def_thms)) arities
20175
0a8ca32f6e64 class package and codegen refinements
haftmann
parents: 20106
diff changeset
   467
  #> after_qed;
19110
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   468
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   469
in
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   470
20182
79c9ff40d760 tactic for prove_instance_arity now gets definition theorems as arguments
haftmann
parents: 20175
diff changeset
   471
val instance_arity = instance_arity' (K axclass_instance_arity_i);
79c9ff40d760 tactic for prove_instance_arity now gets definition theorems as arguments
haftmann
parents: 20175
diff changeset
   472
val instance_arity_i = instance_arity_i' (K axclass_instance_arity_i);
19150
1457d810b408 class package and codegen refinements
haftmann
parents: 19136
diff changeset
   473
val prove_instance_arity = instance_arity_i' o tactic_proof;
19110
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   474
20455
e671d9eac6c8 removed some dictionary stuff
haftmann
parents: 20428
diff changeset
   475
end; (*local*)
19110
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   476
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   477
local
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   478
20368
2461a0485623 global goals/qeds: after_qed operates on Proof.context (potentially local_theory);
wenzelm
parents: 20353
diff changeset
   479
fun prove_interpretation_in tac after_qed (name, expr) thy =
19136
00ade10f611d some refinements
haftmann
parents: 19134
diff changeset
   480
  thy
20368
2461a0485623 global goals/qeds: after_qed operates on Proof.context (potentially local_theory);
wenzelm
parents: 20353
diff changeset
   481
  |> Locale.interpretation_in_locale (ProofContext.theory after_qed) (name, expr)
19136
00ade10f611d some refinements
haftmann
parents: 19134
diff changeset
   482
  |> Proof.global_terminal_proof (Method.Basic (fn _ => Method.SIMPLE_METHOD tac), NONE)
20368
2461a0485623 global goals/qeds: after_qed operates on Proof.context (potentially local_theory);
wenzelm
parents: 20353
diff changeset
   483
  |> ProofContext.theory_of;
19038
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 18963
diff changeset
   484
20385
2f52b5aba086 code cleanup, instance_subsort now working
haftmann
parents: 20368
diff changeset
   485
(*FIXME very ad-hoc, needs proper locale interface*)
19150
1457d810b408 class package and codegen refinements
haftmann
parents: 19136
diff changeset
   486
fun gen_instance_sort prep_class prep_sort do_proof (raw_class, raw_sort) theory =
19110
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   487
  let
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   488
    val class = prep_class theory raw_class;
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   489
    val sort = prep_sort theory raw_sort;
19966
88bbe97ed0b0 refinement in instance command
haftmann
parents: 19957
diff changeset
   490
    val loc_name = (#name_locale o fst o the_class_data theory) class;
19468
0afdd5023bfc Sign.arity_sorts;
wenzelm
parents: 19456
diff changeset
   491
    val loc_expr =
19966
88bbe97ed0b0 refinement in instance command
haftmann
parents: 19957
diff changeset
   492
      (Locale.Merge o map (Locale.Locale o #name_locale o fst o the_class_data theory)) sort;
88bbe97ed0b0 refinement in instance command
haftmann
parents: 19957
diff changeset
   493
    val const_names = (map (NameSpace.base o fst o snd)
88bbe97ed0b0 refinement in instance command
haftmann
parents: 19957
diff changeset
   494
      o maps (#consts o fst o the_class_data theory)
88bbe97ed0b0 refinement in instance command
haftmann
parents: 19957
diff changeset
   495
      o the_ancestry theory) [class];
20106
a3d4b4eb35b9 adaptions in class_package
haftmann
parents: 20076
diff changeset
   496
    fun get_thms thy =
a3d4b4eb35b9 adaptions in class_package
haftmann
parents: 20076
diff changeset
   497
      the_ancestry thy sort
a3d4b4eb35b9 adaptions in class_package
haftmann
parents: 20076
diff changeset
   498
      |> AList.make (the_propnames thy)
a3d4b4eb35b9 adaptions in class_package
haftmann
parents: 20076
diff changeset
   499
      |> map (apsnd (map (NameSpace.append (loc_name))))
a3d4b4eb35b9 adaptions in class_package
haftmann
parents: 20076
diff changeset
   500
      |> map_filter (fn (superclass, thm_names) =>
a3d4b4eb35b9 adaptions in class_package
haftmann
parents: 20076
diff changeset
   501
          case map_filter (try (PureThy.get_thm thy o Name)) thm_names
a3d4b4eb35b9 adaptions in class_package
haftmann
parents: 20076
diff changeset
   502
           of [] => NONE
a3d4b4eb35b9 adaptions in class_package
haftmann
parents: 20076
diff changeset
   503
            | thms => SOME (superclass, thms));
a3d4b4eb35b9 adaptions in class_package
haftmann
parents: 20076
diff changeset
   504
    fun after_qed thy =
a3d4b4eb35b9 adaptions in class_package
haftmann
parents: 20076
diff changeset
   505
      thy
a3d4b4eb35b9 adaptions in class_package
haftmann
parents: 20076
diff changeset
   506
      |> `get_thms
a3d4b4eb35b9 adaptions in class_package
haftmann
parents: 20076
diff changeset
   507
      |-> fold (fn (supclass, thms) => I
a3d4b4eb35b9 adaptions in class_package
haftmann
parents: 20076
diff changeset
   508
            AxClass.prove_classrel (class, supclass)
a3d4b4eb35b9 adaptions in class_package
haftmann
parents: 20076
diff changeset
   509
              (ALLGOALS (K (intro_classes_tac [])) THEN
a3d4b4eb35b9 adaptions in class_package
haftmann
parents: 20076
diff changeset
   510
                (ALLGOALS o ProofContext.fact_tac) thms))
19110
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   511
  in
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   512
    theory
19136
00ade10f611d some refinements
haftmann
parents: 19134
diff changeset
   513
    |> do_proof after_qed (loc_name, loc_expr)
19110
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   514
  end;
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   515
20385
2f52b5aba086 code cleanup, instance_subsort now working
haftmann
parents: 20368
diff changeset
   516
fun instance_sort' do_proof = gen_instance_sort read_class read_sort do_proof;
19150
1457d810b408 class package and codegen refinements
haftmann
parents: 19136
diff changeset
   517
fun instance_sort_i' do_proof = gen_instance_sort certify_class certify_sort do_proof;
20368
2461a0485623 global goals/qeds: after_qed operates on Proof.context (potentially local_theory);
wenzelm
parents: 20353
diff changeset
   518
val setup_proof = Locale.interpretation_in_locale o ProofContext.theory;
19136
00ade10f611d some refinements
haftmann
parents: 19134
diff changeset
   519
val tactic_proof = prove_interpretation_in;
19110
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   520
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   521
in
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   522
19150
1457d810b408 class package and codegen refinements
haftmann
parents: 19136
diff changeset
   523
val instance_sort = instance_sort' setup_proof;
1457d810b408 class package and codegen refinements
haftmann
parents: 19136
diff changeset
   524
val instance_sort_i = instance_sort_i' setup_proof;
1457d810b408 class package and codegen refinements
haftmann
parents: 19136
diff changeset
   525
val prove_instance_sort = instance_sort_i' o tactic_proof;
19110
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   526
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   527
end; (* local *)
19038
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 18963
diff changeset
   528
19957
91ba241a1678 reduced code, better instance command
haftmann
parents: 19953
diff changeset
   529
18168
d35daf321b8a class_package - operational view on type classes
haftmann
parents:
diff changeset
   530
20455
e671d9eac6c8 removed some dictionary stuff
haftmann
parents: 20428
diff changeset
   531
(** code generation view **)
18168
d35daf321b8a class_package - operational view on type classes
haftmann
parents:
diff changeset
   532
20455
e671d9eac6c8 removed some dictionary stuff
haftmann
parents: 20428
diff changeset
   533
fun operational_algebra thy =
e671d9eac6c8 removed some dictionary stuff
haftmann
parents: 20428
diff changeset
   534
  Sorts.project_algebra (Sign.pp thy)
e671d9eac6c8 removed some dictionary stuff
haftmann
parents: 20428
diff changeset
   535
    (is_operational_class thy) (Sign.classes_of thy);
18168
d35daf321b8a class_package - operational view on type classes
haftmann
parents:
diff changeset
   536
20455
e671d9eac6c8 removed some dictionary stuff
haftmann
parents: 20428
diff changeset
   537
fun the_consts_sign thy class =
e671d9eac6c8 removed some dictionary stuff
haftmann
parents: 20428
diff changeset
   538
  let
e671d9eac6c8 removed some dictionary stuff
haftmann
parents: 20428
diff changeset
   539
    val _ = if is_operational_class thy class then () else error ("no operational class: " ^ quote class);
e671d9eac6c8 removed some dictionary stuff
haftmann
parents: 20428
diff changeset
   540
    val data = (fst o the_class_data thy) class
e671d9eac6c8 removed some dictionary stuff
haftmann
parents: 20428
diff changeset
   541
  in (#var data, (map snd o #consts) data) end;
19213
ee83040c3c84 first running version of type classes
haftmann
parents: 19150
diff changeset
   542
20455
e671d9eac6c8 removed some dictionary stuff
haftmann
parents: 20428
diff changeset
   543
fun the_inst_sign thy (class, tyco) =
18168
d35daf321b8a class_package - operational view on type classes
haftmann
parents:
diff changeset
   544
  let
20455
e671d9eac6c8 removed some dictionary stuff
haftmann
parents: 20428
diff changeset
   545
    val _ = if is_operational_class thy class then () else error ("no operational class: " ^ quote class);
e671d9eac6c8 removed some dictionary stuff
haftmann
parents: 20428
diff changeset
   546
    val asorts = Sign.arity_sorts thy tyco [class];
e671d9eac6c8 removed some dictionary stuff
haftmann
parents: 20428
diff changeset
   547
    val (clsvar, const_sign) = the_consts_sign thy class;
e671d9eac6c8 removed some dictionary stuff
haftmann
parents: 20428
diff changeset
   548
    fun add_var sort used =
e671d9eac6c8 removed some dictionary stuff
haftmann
parents: 20428
diff changeset
   549
      let val v = hd (Name.invents used "'a" 1);
e671d9eac6c8 removed some dictionary stuff
haftmann
parents: 20428
diff changeset
   550
      in ((v, sort), Name.declare v used) end;
e671d9eac6c8 removed some dictionary stuff
haftmann
parents: 20428
diff changeset
   551
    val (vsorts, _) =
e671d9eac6c8 removed some dictionary stuff
haftmann
parents: 20428
diff changeset
   552
      Name.context
e671d9eac6c8 removed some dictionary stuff
haftmann
parents: 20428
diff changeset
   553
      |> Name.declare clsvar
e671d9eac6c8 removed some dictionary stuff
haftmann
parents: 20428
diff changeset
   554
      |> fold (fn (_, ty) => fold Name.declare
e671d9eac6c8 removed some dictionary stuff
haftmann
parents: 20428
diff changeset
   555
           ((map (fst o fst) o typ_tvars) ty @ map fst (Term.add_tfreesT  ty []))) const_sign
e671d9eac6c8 removed some dictionary stuff
haftmann
parents: 20428
diff changeset
   556
      |> fold_map add_var asorts;
e671d9eac6c8 removed some dictionary stuff
haftmann
parents: 20428
diff changeset
   557
    val ty_inst = Type (tyco, map TFree vsorts);
e671d9eac6c8 removed some dictionary stuff
haftmann
parents: 20428
diff changeset
   558
    val inst_signs = map (apsnd (subst_clsvar clsvar ty_inst)) const_sign;
e671d9eac6c8 removed some dictionary stuff
haftmann
parents: 20428
diff changeset
   559
  in (vsorts, inst_signs) end;
18168
d35daf321b8a class_package - operational view on type classes
haftmann
parents:
diff changeset
   560
18884
df1e3c93c50a name clarifications
haftmann
parents: 18864
diff changeset
   561
20455
e671d9eac6c8 removed some dictionary stuff
haftmann
parents: 20428
diff changeset
   562
e671d9eac6c8 removed some dictionary stuff
haftmann
parents: 20428
diff changeset
   563
(** toplevel interface **)
18515
1cad5c2b2a0b substantial improvements in code generating
haftmann
parents: 18380
diff changeset
   564
1cad5c2b2a0b substantial improvements in code generating
haftmann
parents: 18380
diff changeset
   565
local
1cad5c2b2a0b substantial improvements in code generating
haftmann
parents: 18380
diff changeset
   566
1cad5c2b2a0b substantial improvements in code generating
haftmann
parents: 18380
diff changeset
   567
structure P = OuterParse
1cad5c2b2a0b substantial improvements in code generating
haftmann
parents: 18380
diff changeset
   568
and K = OuterKeyword
1cad5c2b2a0b substantial improvements in code generating
haftmann
parents: 18380
diff changeset
   569
1cad5c2b2a0b substantial improvements in code generating
haftmann
parents: 18380
diff changeset
   570
in
1cad5c2b2a0b substantial improvements in code generating
haftmann
parents: 18380
diff changeset
   571
20385
2f52b5aba086 code cleanup, instance_subsort now working
haftmann
parents: 20368
diff changeset
   572
val (classK, instanceK, print_classesK) = ("class", "instance", "print_classes")
18515
1cad5c2b2a0b substantial improvements in code generating
haftmann
parents: 18380
diff changeset
   573
20385
2f52b5aba086 code cleanup, instance_subsort now working
haftmann
parents: 20368
diff changeset
   574
fun wrap_add_instance_sort (class, sort) thy =
2f52b5aba086 code cleanup, instance_subsort now working
haftmann
parents: 20368
diff changeset
   575
  (if forall (is_some o lookup_class_data thy) (Sign.read_sort thy sort)
2f52b5aba086 code cleanup, instance_subsort now working
haftmann
parents: 20368
diff changeset
   576
  then instance_sort else axclass_instance_sort) (class, sort) thy;
19136
00ade10f611d some refinements
haftmann
parents: 19134
diff changeset
   577
20385
2f52b5aba086 code cleanup, instance_subsort now working
haftmann
parents: 20368
diff changeset
   578
val class_subP = P.name -- Scan.repeat (P.$$$ "+" |-- P.name) >> (op ::);
20428
67fa1c6ba89e refinements
haftmann
parents: 20403
diff changeset
   579
val class_bodyP = P.!!! (Scan.repeat1 P.locale_element);
20385
2f52b5aba086 code cleanup, instance_subsort now working
haftmann
parents: 20368
diff changeset
   580
20601
3e1caf5a07c6 classical arity syntax
haftmann
parents: 20548
diff changeset
   581
val parse_arity =
3e1caf5a07c6 classical arity syntax
haftmann
parents: 20548
diff changeset
   582
  (P.xname --| P.$$$ "::" -- P.!!! P.arity)
19136
00ade10f611d some refinements
haftmann
parents: 19134
diff changeset
   583
    >> (fn (tyco, (asorts, sort)) => ((tyco, asorts), sort));
18911
74edab16166f alternative syntax for instances
haftmann
parents: 18884
diff changeset
   584
18515
1cad5c2b2a0b substantial improvements in code generating
haftmann
parents: 18380
diff changeset
   585
val classP =
20385
2f52b5aba086 code cleanup, instance_subsort now working
haftmann
parents: 20368
diff changeset
   586
  OuterSyntax.command classK "define operational type class" K.thy_decl (
18849
05a16861d3a5 added three times overloaded Isar instance command
haftmann
parents: 18829
diff changeset
   587
    P.name --| P.$$$ "="
19280
5091dc43817b slight improvement in serializer, stub for code generator theorems added
haftmann
parents: 19253
diff changeset
   588
    -- (
5091dc43817b slight improvement in serializer, stub for code generator theorems added
haftmann
parents: 19253
diff changeset
   589
      class_subP --| P.$$$ "+" -- class_bodyP
5091dc43817b slight improvement in serializer, stub for code generator theorems added
haftmann
parents: 19253
diff changeset
   590
      || class_subP >> rpair []
5091dc43817b slight improvement in serializer, stub for code generator theorems added
haftmann
parents: 19253
diff changeset
   591
      || class_bodyP >> pair []
5091dc43817b slight improvement in serializer, stub for code generator theorems added
haftmann
parents: 19253
diff changeset
   592
    ) >> (Toplevel.theory_context
5091dc43817b slight improvement in serializer, stub for code generator theorems added
haftmann
parents: 19253
diff changeset
   593
          o (fn (bname, (supclasses, elems)) => class bname supclasses elems)));
18515
1cad5c2b2a0b substantial improvements in code generating
haftmann
parents: 18380
diff changeset
   594
18575
9ccfd1d1e874 substantial additions using locales
haftmann
parents: 18550
diff changeset
   595
val instanceP =
18849
05a16861d3a5 added three times overloaded Isar instance command
haftmann
parents: 18829
diff changeset
   596
  OuterSyntax.command instanceK "prove type arity or subclass relation" K.thy_goal ((
20385
2f52b5aba086 code cleanup, instance_subsort now working
haftmann
parents: 20368
diff changeset
   597
      P.xname -- ((P.$$$ "\\<subseteq>" || P.$$$ "<") |-- P.!!! P.xname) >> wrap_add_instance_sort
20601
3e1caf5a07c6 classical arity syntax
haftmann
parents: 20548
diff changeset
   598
      || P.opt_thm_name ":" -- (P.and_list1 parse_arity -- Scan.repeat (P.opt_thm_name ":" -- P.prop))
20175
0a8ca32f6e64 class package and codegen refinements
haftmann
parents: 20106
diff changeset
   599
           >> (fn (("", []), ([((tyco, asorts), sort)], [])) => axclass_instance_arity I [(tyco, asorts, sort)]
0a8ca32f6e64 class package and codegen refinements
haftmann
parents: 20106
diff changeset
   600
                | (natts, (arities, defs)) => instance_arity arities natts defs)
18849
05a16861d3a5 added three times overloaded Isar instance command
haftmann
parents: 18829
diff changeset
   601
    ) >> (Toplevel.print oo Toplevel.theory_to_proof));
18575
9ccfd1d1e874 substantial additions using locales
haftmann
parents: 18550
diff changeset
   602
20385
2f52b5aba086 code cleanup, instance_subsort now working
haftmann
parents: 20368
diff changeset
   603
val print_classesP =
2f52b5aba086 code cleanup, instance_subsort now working
haftmann
parents: 20368
diff changeset
   604
  OuterSyntax.improper_command print_classesK "print classes of this theory" K.diag
2f52b5aba086 code cleanup, instance_subsort now working
haftmann
parents: 20368
diff changeset
   605
    (Scan.succeed (Toplevel.no_timing o Toplevel.unknown_theory
2f52b5aba086 code cleanup, instance_subsort now working
haftmann
parents: 20368
diff changeset
   606
      o Toplevel.keep (print_classes o Toplevel.theory_of)));
2f52b5aba086 code cleanup, instance_subsort now working
haftmann
parents: 20368
diff changeset
   607
2f52b5aba086 code cleanup, instance_subsort now working
haftmann
parents: 20368
diff changeset
   608
val _ = OuterSyntax.add_parsers [classP, instanceP, print_classesP];
19110
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   609
20455
e671d9eac6c8 removed some dictionary stuff
haftmann
parents: 20428
diff changeset
   610
end; (*local*)
18515
1cad5c2b2a0b substantial improvements in code generating
haftmann
parents: 18380
diff changeset
   611
20455
e671d9eac6c8 removed some dictionary stuff
haftmann
parents: 20428
diff changeset
   612
end;