| author | wenzelm |
| Wed, 02 Aug 2006 22:27:02 +0200 | |
| changeset 20308 | ddb7e7129481 |
| parent 20289 | ba7a7c56bed5 |
| child 20353 | d73e49780ef2 |
| permissions | -rw-r--r-- |
| 18168 | 1 |
(* Title: Pure/Tools/class_package.ML |
2 |
ID: $Id$ |
|
3 |
Author: Florian Haftmann, TU Muenchen |
|
4 |
||
| 19468 | 5 |
Type classes derived from primitive axclasses and locales. |
| 18168 | 6 |
*) |
7 |
||
8 |
signature CLASS_PACKAGE = |
|
9 |
sig |
|
| 19150 | 10 |
val class: bstring -> class list -> Element.context list -> theory |
| 20289 | 11 |
-> Proof.context * theory |
| 19150 | 12 |
val class_i: bstring -> class list -> Element.context_i list -> theory |
| 20289 | 13 |
-> Proof.context * theory |
| 20175 | 14 |
(*FIXME: in _i variants, bstring should be bstring option*) |
15 |
val instance_arity: ((xstring * string list) * string) list |
|
| 19136 | 16 |
-> bstring * Attrib.src list -> ((bstring * Attrib.src list) * string) list |
| 18575 | 17 |
-> theory -> Proof.state |
| 20175 | 18 |
val instance_arity_i: ((string * sort list) * sort) list |
| 19136 | 19 |
-> bstring * attribute list -> ((bstring * attribute list) * term) list |
| 18575 | 20 |
-> theory -> Proof.state |
|
20182
79c9ff40d760
tactic for prove_instance_arity now gets definition theorems as arguments
haftmann
parents:
20175
diff
changeset
|
21 |
val prove_instance_arity: (thm list -> tactic) -> ((string * sort list) * sort) list |
| 19136 | 22 |
-> bstring * attribute list -> ((bstring * attribute list) * term) list |
|
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
23 |
-> theory -> theory |
| 19150 | 24 |
val instance_sort: string * string -> theory -> Proof.state |
25 |
val instance_sort_i: class * sort -> theory -> Proof.state |
|
26 |
val prove_instance_sort: tactic -> class * sort -> theory -> theory |
|
| 18168 | 27 |
|
|
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
28 |
val intern_class: theory -> xstring -> class |
|
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
29 |
val intern_sort: theory -> sort -> sort |
|
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
30 |
val extern_class: theory -> class -> xstring |
|
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
31 |
val extern_sort: theory -> sort -> sort |
| 18755 | 32 |
val certify_class: theory -> class -> class |
|
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
33 |
val certify_sort: theory -> sort -> sort |
|
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
34 |
val read_sort: theory -> string -> sort |
| 18884 | 35 |
val operational_sort_of: theory -> sort -> sort |
| 20191 | 36 |
val operational_algebra: theory -> Sorts.algebra |
| 18702 | 37 |
val the_superclasses: theory -> class -> class list |
38 |
val the_consts_sign: theory -> class -> string * (string * typ) list |
|
39 |
val the_inst_sign: theory -> class * string -> (string * sort) list * (string * typ) list |
|
| 20175 | 40 |
val assume_arities_of_sort: theory -> ((string * sort list) * sort) list -> typ * sort -> bool |
41 |
val assume_arities_thy: theory -> ((string * sort list) * sort) list -> (theory -> 'a) -> 'a |
|
42 |
(*'a must not keep any reference to theory*) |
|
|
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
43 |
|
| 18702 | 44 |
val print_classes: theory -> unit |
|
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
45 |
val intro_classes_tac: thm list -> tactic |
|
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
46 |
val default_intro_classes_tac: thm list -> tactic |
| 18168 | 47 |
|
48 |
type sortcontext = (string * sort) list |
|
| 18884 | 49 |
datatype classlookup = Instance of (class * string) * classlookup list list |
| 19253 | 50 |
| Lookup of class list * (string * (int * int)) |
|
19953
2f54a51f1801
class package refinements, slight code generation refinements
haftmann
parents:
19929
diff
changeset
|
51 |
val sortcontext_of_typ: theory -> typ -> sortcontext |
|
2f54a51f1801
class package refinements, slight code generation refinements
haftmann
parents:
19929
diff
changeset
|
52 |
val sortlookup: theory -> sort * typ -> classlookup list |
|
2f54a51f1801
class package refinements, slight code generation refinements
haftmann
parents:
19929
diff
changeset
|
53 |
val sortlookups_const: theory -> string * typ -> classlookup list list |
| 18168 | 54 |
end; |
55 |
||
|
20182
79c9ff40d760
tactic for prove_instance_arity now gets definition theorems as arguments
haftmann
parents:
20175
diff
changeset
|
56 |
structure ClassPackage : CLASS_PACKAGE = |
| 18168 | 57 |
struct |
58 |
||
59 |
||
| 19957 | 60 |
(* auxiliary *) |
61 |
||
62 |
fun instantiations_of thy (ty, ty') = |
|
63 |
let |
|
64 |
val vartab = typ_tvars ty; |
|
65 |
fun prep_vartab (v, (_, ty)) = |
|
66 |
case (the o AList.lookup (op =) vartab) v |
|
67 |
of [] => NONE |
|
68 |
| sort => SOME ((v, sort), ty); |
|
69 |
in case try (Sign.typ_match thy (ty, ty')) Vartab.empty |
|
70 |
of NONE => NONE |
|
71 |
| SOME vartab => |
|
72 |
SOME ((map_filter prep_vartab o Vartab.dest) vartab) |
|
73 |
end; |
|
74 |
||
75 |
||
| 18708 | 76 |
(* theory data *) |
| 18168 | 77 |
|
| 19456 | 78 |
datatype class_data = ClassData of {
|
| 18575 | 79 |
name_locale: string, |
80 |
name_axclass: string, |
|
81 |
var: string, |
|
| 19966 | 82 |
consts: (string * (string * typ)) list, |
| 19957 | 83 |
(*locale parameter ~> toplevel constant*) |
| 19966 | 84 |
propnames: string list |
85 |
} * thm list Symtab.table; |
|
| 18168 | 86 |
|
| 19456 | 87 |
fun rep_classdata (ClassData c) = c; |
88 |
||
| 18575 | 89 |
structure ClassData = TheoryDataFun ( |
| 18168 | 90 |
struct |
91 |
val name = "Pure/classes"; |
|
| 19966 | 92 |
type T = class_data Symtab.table; |
93 |
val empty = Symtab.empty; |
|
| 18168 | 94 |
val copy = I; |
95 |
val extend = I; |
|
| 19966 | 96 |
fun merge _ = Symtab.join (fn _ => fn (ClassData (classd, instd1), ClassData (_, instd2)) => |
97 |
(ClassData (classd, Symtab.merge (K true) (instd1, instd2)))); |
|
98 |
fun print thy data = |
|
| 18575 | 99 |
let |
| 19966 | 100 |
fun pretty_class (name, ClassData ({name_locale, name_axclass, var, consts, ...}, _)) =
|
| 18575 | 101 |
(Pretty.block o Pretty.fbreaks) [ |
102 |
Pretty.str ("class " ^ name ^ ":"),
|
|
103 |
Pretty.str ("locale: " ^ name_locale),
|
|
104 |
Pretty.str ("axclass: " ^ name_axclass),
|
|
105 |
Pretty.str ("class variable: " ^ var),
|
|
106 |
(Pretty.block o Pretty.fbreaks) ( |
|
107 |
Pretty.str "constants: " |
|
|
19280
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
19253
diff
changeset
|
108 |
:: map (fn (_, (c, ty)) => Pretty.str (c ^ " :: " ^ Sign.string_of_typ thy ty)) consts |
| 18575 | 109 |
) |
110 |
] |
|
111 |
in |
|
| 19966 | 112 |
(Pretty.writeln o Pretty.chunks o map pretty_class o Symtab.dest) data |
| 18575 | 113 |
end; |
| 18168 | 114 |
end |
115 |
); |
|
116 |
||
| 18708 | 117 |
val _ = Context.add_setup ClassData.init; |
| 18575 | 118 |
val print_classes = ClassData.print; |
119 |
||
| 19038 | 120 |
|
121 |
(* queries *) |
|
122 |
||
| 19966 | 123 |
val lookup_class_data = Option.map rep_classdata oo Symtab.lookup o ClassData.get; |
| 18168 | 124 |
|
| 19038 | 125 |
fun the_class_data thy class = |
| 18168 | 126 |
case lookup_class_data thy class |
| 18755 | 127 |
of NONE => error ("undeclared operational class " ^ quote class)
|
| 18168 | 128 |
| SOME data => data; |
129 |
||
|
19280
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
19253
diff
changeset
|
130 |
val is_class = is_some oo lookup_class_data; |
|
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
19253
diff
changeset
|
131 |
|
|
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
19253
diff
changeset
|
132 |
fun is_operational_class thy cls = |
| 18702 | 133 |
lookup_class_data thy cls |
| 19966 | 134 |
|> Option.map (not o null o #consts o fst) |
| 18702 | 135 |
|> the_default false; |
| 18168 | 136 |
|
| 20191 | 137 |
fun operational_algebra thy = |
138 |
Sorts.project_algebra (Sign.pp thy) |
|
139 |
(is_operational_class thy) (Sign.classes_of thy); |
|
140 |
||
|
19953
2f54a51f1801
class package refinements, slight code generation refinements
haftmann
parents:
19929
diff
changeset
|
141 |
fun operational_sort_of thy = |
| 18360 | 142 |
let |
|
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
143 |
fun get_sort class = |
|
19280
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
19253
diff
changeset
|
144 |
if is_operational_class thy class |
|
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
145 |
then [class] |
| 19412 | 146 |
else operational_sort_of thy (Sign.super_classes thy class); |
|
19953
2f54a51f1801
class package refinements, slight code generation refinements
haftmann
parents:
19929
diff
changeset
|
147 |
in Sign.certify_sort thy o maps get_sort end; |
| 18168 | 148 |
|
| 18702 | 149 |
fun the_superclasses thy class = |
| 18515 | 150 |
if is_class thy class |
151 |
then |
|
| 19412 | 152 |
Sign.super_classes thy class |
| 18884 | 153 |
|> operational_sort_of thy |
| 18515 | 154 |
else |
| 20175 | 155 |
error ("no operational class: " ^ class);
|
| 18168 | 156 |
|
| 19038 | 157 |
fun the_ancestry thy classes = |
158 |
let |
|
159 |
fun ancestry class anc = |
|
160 |
anc |
|
161 |
|> cons class |
|
162 |
|> fold ancestry (the_superclasses thy class); |
|
163 |
in fold ancestry classes [] end; |
|
164 |
||
165 |
fun subst_clsvar v ty_subst = |
|
166 |
map_type_tfree (fn u as (w, _) => |
|
167 |
if w = v then ty_subst else TFree u); |
|
168 |
||
| 19966 | 169 |
val the_parm_map = #consts o fst oo the_class_data; |
|
19280
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
19253
diff
changeset
|
170 |
|
| 18702 | 171 |
fun the_consts_sign thy class = |
| 18168 | 172 |
let |
| 19966 | 173 |
val data = (fst o the_class_data thy) class |
|
19280
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
19253
diff
changeset
|
174 |
in (#var data, (map snd o #consts) data) end; |
| 18702 | 175 |
|
176 |
fun the_inst_sign thy (class, tyco) = |
|
| 18168 | 177 |
let |
|
19280
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
19253
diff
changeset
|
178 |
val _ = if is_operational_class thy class then () else error ("no operational class: " ^ class);
|
| 19957 | 179 |
val asorts = Sign.arity_sorts thy tyco [class]; |
| 19966 | 180 |
val (clsvar, const_sign) = the_consts_sign thy class; |
| 18702 | 181 |
fun add_var sort used = |
| 20175 | 182 |
let val v = hd (Name.invents used "'a" 1); |
183 |
in ((v, sort), Name.declare v used) end; |
|
| 18702 | 184 |
val (vsorts, _) = |
| 20175 | 185 |
Name.context |
186 |
|> Name.declare clsvar |
|
187 |
|> fold (fn (_, ty) => fold Name.declare |
|
| 18702 | 188 |
((map (fst o fst) o typ_tvars) ty @ (map fst o typ_tfrees) ty)) const_sign |
| 19957 | 189 |
|> fold_map add_var asorts; |
|
19953
2f54a51f1801
class package refinements, slight code generation refinements
haftmann
parents:
19929
diff
changeset
|
190 |
val ty_inst = Type (tyco, map TFree vsorts); |
| 18702 | 191 |
val inst_signs = map (apsnd (subst_clsvar clsvar ty_inst)) const_sign; |
192 |
in (vsorts, inst_signs) end; |
|
| 18168 | 193 |
|
| 19966 | 194 |
val the_propnames = #propnames o fst oo the_class_data; |
195 |
||
| 18168 | 196 |
|
| 19038 | 197 |
(* updaters *) |
198 |
||
| 19966 | 199 |
fun add_class_data (class, (name_locale, name_axclass, var, consts, propnames)) = |
| 19957 | 200 |
ClassData.map ( |
| 19966 | 201 |
Symtab.update_new (class, ClassData ({
|
| 19957 | 202 |
name_locale = name_locale, |
203 |
name_axclass = name_axclass, |
|
204 |
var = var, |
|
| 19966 | 205 |
consts = consts, |
206 |
propnames = propnames}, Symtab.empty)) |
|
| 19957 | 207 |
); |
| 19038 | 208 |
|
| 19966 | 209 |
fun add_inst_def ((class, tyco), thm) = |
210 |
ClassData.map ( |
|
211 |
Symtab.map_entry class (fn ClassData (classd, instd) => |
|
212 |
ClassData (classd, Symtab.insert_list eq_thm (tyco, thm) instd)) |
|
213 |
); |
|
| 19038 | 214 |
|
| 20106 | 215 |
|
| 19038 | 216 |
(* name handling *) |
217 |
||
218 |
fun certify_class thy class = |
|
|
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
219 |
(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
|
220 |
|
|
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
221 |
fun certify_sort thy sort = |
|
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
222 |
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
|
223 |
|
|
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
224 |
fun intern_class thy = |
| 19957 | 225 |
certify_class thy o Sign.intern_class thy; |
|
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
226 |
|
|
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
227 |
fun intern_sort thy = |
|
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
228 |
certify_sort thy o Sign.intern_sort thy; |
|
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
229 |
|
|
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
230 |
fun extern_class thy = |
|
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
231 |
Sign.extern_class thy o certify_class thy; |
|
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
232 |
|
|
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
233 |
fun extern_sort thy = |
|
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
234 |
Sign.extern_sort thy o certify_sort thy; |
| 19038 | 235 |
|
|
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
236 |
fun read_sort thy = |
|
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
237 |
certify_sort thy o Sign.read_sort thy; |
|
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
238 |
|
|
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
239 |
|
| 20175 | 240 |
(* contexts with arity assumptions *) |
241 |
||
242 |
fun assume_arities_of_sort thy arities ty_sort = |
|
243 |
let |
|
244 |
val pp = Sign.pp thy; |
|
245 |
val algebra = Sign.classes_of thy |
|
246 |
|> fold (fn ((tyco, asorts), sort) => |
|
247 |
Sorts.add_arities pp (tyco, map (fn class => (class, asorts)) sort)) arities; |
|
248 |
in Sorts.of_sort algebra ty_sort end; |
|
249 |
||
250 |
fun assume_arities_thy thy arities f = |
|
251 |
let |
|
252 |
val thy_read = (fold (fn ((tyco, asorts), sort) |
|
253 |
=> Sign.primitive_arity (tyco, asorts, sort)) arities o Theory.copy) thy |
|
254 |
in f thy_read end; |
|
255 |
||
|
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
256 |
(* tactics and methods *) |
|
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
257 |
|
|
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
258 |
fun intro_classes_tac facts st = |
|
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
259 |
(ALLGOALS (Method.insert_tac facts THEN' |
| 19928 | 260 |
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
|
261 |
THEN Tactic.distinct_subgoals_tac) st; |
|
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
262 |
|
|
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
263 |
fun default_intro_classes_tac [] = intro_classes_tac [] |
|
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
264 |
| default_intro_classes_tac _ = Tactical.no_tac; (*no error message!*) |
|
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
265 |
|
|
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
266 |
fun default_tac rules ctxt facts = |
|
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
267 |
HEADGOAL (Method.some_rule_tac rules ctxt facts) ORELSE |
|
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
268 |
default_intro_classes_tac facts; |
| 19038 | 269 |
|
| 19468 | 270 |
val _ = Context.add_setup (Method.add_methods |
271 |
[("intro_classes", Method.no_args (Method.METHOD intro_classes_tac),
|
|
272 |
"back-chain introduction rules of classes"), |
|
273 |
("default", Method.thms_ctxt_args (Method.METHOD oo default_tac),
|
|
274 |
"apply some intro/elim rule")]); |
|
275 |
||
| 19038 | 276 |
|
| 19246 | 277 |
(* axclass instances *) |
278 |
||
279 |
local |
|
280 |
||
| 20175 | 281 |
fun gen_instance mk_prop add_thm after_qed insts thy = |
| 19246 | 282 |
thy |
283 |
|> ProofContext.init |
|
| 19412 | 284 |
|> Proof.theorem_i PureThy.internalK NONE (after_qed oo (fold o fold) add_thm) NONE ("", [])
|
| 20175 | 285 |
((map (fn t => (("", []), [(t, [])])) o maps (mk_prop thy)) insts);
|
| 19246 | 286 |
|
287 |
in |
|
288 |
||
289 |
val axclass_instance_arity = |
|
| 19412 | 290 |
gen_instance (Logic.mk_arities oo Sign.read_arity) AxClass.add_arity; |
| 19246 | 291 |
val axclass_instance_arity_i = |
| 19412 | 292 |
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
|
293 |
val axclass_instance_sort = |
|
79c9ff40d760
tactic for prove_instance_arity now gets definition theorems as arguments
haftmann
parents:
20175
diff
changeset
|
294 |
gen_instance (single oo (Logic.mk_classrel oo AxClass.read_classrel)) AxClass.add_classrel I o single; |
| 19246 | 295 |
|
296 |
end; |
|
297 |
||
298 |
||
| 19038 | 299 |
(* classes and instances *) |
300 |
||
301 |
local |
|
302 |
||
| 19957 | 303 |
fun add_axclass_i (name, supsort) params axs thy = |
|
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
304 |
let |
| 19395 | 305 |
val (c, thy') = thy |
| 19957 | 306 |
|> AxClass.define_class_i (name, supsort) params axs; |
| 19518 | 307 |
val {intro, axioms, ...} = AxClass.get_definition thy' c;
|
| 19395 | 308 |
in ((c, (intro, axioms)), thy') end; |
|
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
309 |
|
|
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
310 |
fun prove_interpretation_i (prfx, atts) expr insts tac thy = |
| 19038 | 311 |
let |
|
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
312 |
fun ad_hoc_term NONE = NONE |
|
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
313 |
| ad_hoc_term (SOME (Const (c, ty))) = |
|
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
314 |
let |
|
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
315 |
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
|
316 |
val s = c ^ "::" ^ Pretty.output p; |
|
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
317 |
in SOME s end |
|
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
318 |
| ad_hoc_term (SOME t) = |
|
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
319 |
let |
|
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
320 |
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
|
321 |
val s = Pretty.output p; |
|
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
322 |
in SOME s end; |
|
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
323 |
in |
|
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
324 |
thy |
|
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
325 |
|> Locale.interpretation (prfx, atts) expr (map ad_hoc_term insts) |
|
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
326 |
|> Proof.global_terminal_proof (Method.Basic (fn _ => Method.SIMPLE_METHOD tac), NONE) |
|
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
327 |
|-> (fn _ => I) |
|
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
328 |
end; |
|
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
329 |
|
| 19150 | 330 |
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
|
331 |
let |
|
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
332 |
val supclasses = map (prep_class thy) raw_supclasses; |
| 19038 | 333 |
val supsort = |
334 |
supclasses |
|
| 19966 | 335 |
|> map (#name_axclass o fst o the_class_data thy) |
| 19468 | 336 |
|> Sign.certify_sort thy |
| 19038 | 337 |
|> null ? K (Sign.defaultS thy); |
| 19966 | 338 |
val expr = (Locale.Merge o map (Locale.Locale o #name_locale o fst o the_class_data thy)) supclasses; |
|
19280
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
19253
diff
changeset
|
339 |
val mapp_sup = AList.make |
| 19468 | 340 |
(the o AList.lookup (op =) ((flat o map (the_parm_map thy) o the_ancestry thy) supclasses)) |
|
19280
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
19253
diff
changeset
|
341 |
((map (fst o fst) o Locale.parameters_of_expr thy) expr); |
| 19038 | 342 |
fun extract_tyvar_consts thy name_locale = |
343 |
let |
|
| 20175 | 344 |
fun extract_classvar ((c, ty), _) w = |
345 |
(case add_typ_tfrees (ty, []) |
|
346 |
of [(v, _)] => (case w |
|
347 |
of SOME u => if u = v then w else error ("additonal type variable in type signature of constant " ^ quote c)
|
|
348 |
| NONE => SOME v) |
|
349 |
| [] => error ("no type variable in type signature of constant " ^ quote c)
|
|
350 |
| _ => 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
|
351 |
val consts1 = |
| 19038 | 352 |
Locale.parameters_of thy name_locale |
353 |
|> map (apsnd Syntax.unlocalize_mixfix) |
|
| 20175 | 354 |
val SOME v = fold extract_classvar consts1 NONE; |
|
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
355 |
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
|
356 |
in (v, chop (length mapp_sup) consts2) end; |
|
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
357 |
fun add_consts v raw_cs_sup raw_cs_this thy = |
|
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
358 |
let |
|
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
359 |
fun add_global_const ((c, ty), syn) thy = |
| 20265 | 360 |
((c, (Sign.full_name thy c, ty)), |
361 |
thy |
|
362 |
|> Sign.add_consts_i [(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
|
363 |
in |
|
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
364 |
thy |
|
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
365 |
|> fold_map add_global_const raw_cs_this |
|
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
366 |
end; |
|
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
367 |
fun extract_assumes thy name_locale cs_mapp = |
|
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
368 |
let |
|
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
369 |
val subst_assume = |
|
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
370 |
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
|
371 |
| t => t) |
|
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
372 |
fun prep_asm ((name, atts), ts) = |
| 19957 | 373 |
((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
|
374 |
in |
|
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
375 |
(map prep_asm o Locale.local_asms_of thy) name_locale |
|
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
376 |
end; |
|
19280
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
19253
diff
changeset
|
377 |
fun add_global_constraint v class (_, (c, ty)) thy = |
| 19038 | 378 |
thy |
| 19136 | 379 |
|> 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
|
380 |
fun mk_const thy class v (c, ty) = |
|
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
381 |
Const (c, subst_clsvar v (TFree (v, [class])) ty); |
| 19038 | 382 |
in |
383 |
thy |
|
|
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
384 |
|> add_locale bname expr raw_elems |
| 19928 | 385 |
|-> (fn (name_locale, ctxt) => |
| 19038 | 386 |
`(fn thy => extract_tyvar_consts thy name_locale) |
|
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
387 |
#-> (fn (v, (raw_cs_sup, raw_cs_this)) => |
|
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
388 |
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
|
389 |
#-> (fn mapp_this => |
|
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
19253
diff
changeset
|
390 |
`(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
|
391 |
#-> (fn loc_axioms => |
| 19957 | 392 |
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
|
393 |
#-> (fn (name_axclass, (_, ax_axioms)) => |
|
19280
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
19253
diff
changeset
|
394 |
fold (add_global_constraint v name_axclass) mapp_this |
| 19966 | 395 |
#> add_class_data (name_locale, (name_locale, name_axclass, v, mapp_this, |
396 |
map (fst o fst) loc_axioms)) |
|
|
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
397 |
#> prove_interpretation_i (NameSpace.base name_locale, []) |
|
19280
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
19253
diff
changeset
|
398 |
(Locale.Locale name_locale) (map (SOME o mk_const thy name_axclass v) (map snd (mapp_sup @ mapp_this))) |
| 19929 | 399 |
((ALLGOALS o ProofContext.fact_tac) ax_axioms) |
|
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
400 |
#> pair ctxt |
|
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
401 |
))))) |
| 19038 | 402 |
end; |
403 |
||
404 |
in |
|
405 |
||
| 19928 | 406 |
val class = gen_class (Locale.add_locale false) intern_class; |
407 |
val class_i = gen_class (Locale.add_locale_i false) certify_class; |
|
| 19038 | 408 |
|
409 |
end; (* local *) |
|
410 |
||
|
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
411 |
local |
|
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
412 |
|
| 20175 | 413 |
fun gen_read_def thy prep_att read_def ((raw_name, raw_atts), raw_t) = |
| 19038 | 414 |
let |
| 19957 | 415 |
val (_, t) = read_def thy (raw_name, raw_t); |
416 |
val ((c, ty), _) = Sign.cert_def (Sign.pp thy) t; |
|
417 |
val atts = map (prep_att thy) raw_atts; |
|
| 20175 | 418 |
val insts = (Consts.typargs (Sign.consts_of thy) (c, ty)) |
419 |
fun flat_typ (Type (tyco, tys)) = tyco :: maps flat_typ tys |
|
420 |
| flat_typ _ = []; |
|
| 19957 | 421 |
val name = case raw_name |
| 20175 | 422 |
of "" => let |
423 |
val tycos = maps flat_typ insts; |
|
424 |
val names = map NameSpace.base (c :: tycos); |
|
425 |
in Thm.def_name (space_implode "_" names) end |
|
| 19957 | 426 |
| _ => raw_name; |
| 20175 | 427 |
in (c, (insts, ((name, t), atts))) end; |
|
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
428 |
|
| 19957 | 429 |
fun read_def thy = gen_read_def thy Attrib.attribute read_axm; |
430 |
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
|
431 |
|
| 20175 | 432 |
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
|
433 |
let |
| 20175 | 434 |
fun prep_arity' ((tyco, asorts), sort) = prep_arity theory (tyco, asorts, sort); |
435 |
val arities = map prep_arity' raw_arities; |
|
436 |
val arities_pair = map (fn (tyco, asorts, sort) => ((tyco, asorts), sort)) arities; |
|
437 |
val _ = if null arities then error "at least one arity must be given" else (); |
|
438 |
val _ = case (duplicates (op =) o map #1) arities |
|
439 |
of [] => () |
|
440 |
| dupl_tycos => error ("type constructors occur more than once in arities: "
|
|
441 |
^ (commas o map quote) dupl_tycos); |
|
| 19136 | 442 |
val name = case raw_name |
| 20175 | 443 |
of "" => Thm.def_name ((space_implode "_" o map NameSpace.base) |
444 |
(maps (fn (tyco, _, sort) => sort @ [tyco]) |
|
445 |
(sort (fn ((tyco1, _, _), (tyco2, _, _)) => string_ord (tyco1, tyco2)) arities))) |
|
| 19136 | 446 |
| _ => raw_name; |
447 |
val atts = map (prep_att theory) raw_atts; |
|
| 20175 | 448 |
fun already_defined (c, ty_inst) = |
449 |
is_some (find_first (fn (_, { lhs = [ty_inst'], ...}) =>
|
|
450 |
Sign.typ_instance theory (ty_inst', ty_inst) orelse Sign.typ_instance theory (ty_inst, ty_inst')) |
|
451 |
(Defs.specifications_of (Theory.defs_of theory) c)); |
|
452 |
fun get_consts_class tyco ty class = |
|
|
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
453 |
let |
| 19966 | 454 |
val data = (fst o the_class_data theory) class; |
| 19957 | 455 |
val subst_ty = map_type_tfree (fn (v, sort) => |
| 20175 | 456 |
if #var data = v then ty else TVar ((v, 0), sort)); |
| 19957 | 457 |
in |
458 |
(map_filter (fn (_, (c, ty)) => |
|
| 20175 | 459 |
if already_defined (c, ty) |
460 |
then NONE else SOME ((c, ((tyco, class), subst_ty ty)))) o #consts) data |
|
| 19957 | 461 |
end; |
| 20175 | 462 |
fun get_consts_sort (tyco, asorts, sort) = |
| 19957 | 463 |
let |
|
20192
956cd30ef3be
renamed Name.give_names to Name.names and moved Name.alphanum to Symbol.alphanum
haftmann
parents:
20191
diff
changeset
|
464 |
val ty = Type (tyco, map (fn (v, sort) => TVar ((v, 0), sort)) (Name.names Name.context "'a" asorts)) |
| 20175 | 465 |
in maps (get_consts_class tyco ty) (the_ancestry theory sort) end; |
466 |
val cs = maps get_consts_sort arities; |
|
467 |
fun read_defs defs cs thy_read = |
|
468 |
let |
|
| 19957 | 469 |
fun read raw_def cs = |
470 |
let |
|
| 20175 | 471 |
val (c, (inst, ((_, t), atts))) = read_def thy_read raw_def; |
472 |
val ty = Logic.varifyT (Consts.instance (Sign.consts_of thy_read) (c, inst)); |
|
473 |
val ((tyco, class), ty') = case AList.lookup (op =) cs c |
|
| 19957 | 474 |
of NONE => error ("superfluous definition for constant " ^ quote c)
|
| 19966 | 475 |
| SOME class_ty => class_ty; |
| 20175 | 476 |
val name = Thm.def_name (NameSpace.base c ^ "_" ^ NameSpace.base tyco); |
477 |
val t' = case instantiations_of thy_read (ty, ty') |
|
| 19966 | 478 |
of NONE => error ("superfluous definition for constant " ^
|
479 |
quote c ^ "::" ^ Sign.string_of_typ thy_read ty) |
|
480 |
| SOME insttab => |
|
| 20175 | 481 |
map_term_types |
482 |
(Logic.unvarifyT o Term.instantiateT insttab o Logic.varifyT) t |
|
483 |
in (((class, tyco), ((name, t'), atts)), AList.delete (op =) c cs) end; |
|
| 19957 | 484 |
in fold_map read defs cs end; |
| 20175 | 485 |
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
|
486 |
fun get_remove_contraint c thy = |
|
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 ty = Sign.the_const_constraint thy c; |
|
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
489 |
in |
|
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
490 |
thy |
|
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
491 |
|> Sign.add_const_constraint_i (c, NONE) |
| 19806 | 492 |
|> pair (c, Logic.legacy_unvarifyT ty) |
|
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
493 |
end; |
| 19966 | 494 |
fun add_defs defs thy = |
495 |
thy |
|
496 |
|> PureThy.add_defs_i true (map snd defs) |
|
497 |
|-> (fn thms => pair (map fst defs ~~ thms)); |
|
498 |
fun note_all thy = |
|
| 20175 | 499 |
(*FIXME: should avoid binding duplicated names*) |
| 19966 | 500 |
let |
| 20175 | 501 |
val thms = maps (fn (tyco, _, sort) => maps (fn class => |
502 |
Symtab.lookup_list |
|
503 |
((snd o the_class_data thy) class) tyco) (the_ancestry thy sort)) arities; |
|
| 19966 | 504 |
in |
505 |
thy |
|
506 |
|> PureThy.note_thmss_i PureThy.internalK [((name, atts), [(thms, [])])] |
|
507 |
|> snd |
|
508 |
end; |
|
|
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
509 |
fun after_qed cs thy = |
|
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
510 |
thy |
| 19412 | 511 |
|> fold Sign.add_const_constraint_i (map (apsnd SOME) cs); |
| 19038 | 512 |
in |
|
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
513 |
theory |
|
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
514 |
|> fold_map get_remove_contraint (map fst cs) |
| 19966 | 515 |
||>> add_defs defs |
| 20076 | 516 |
|-> (fn (cs, def_thms) => |
| 20175 | 517 |
fold add_inst_def def_thms |
| 19966 | 518 |
#> note_all |
|
20182
79c9ff40d760
tactic for prove_instance_arity now gets definition theorems as arguments
haftmann
parents:
20175
diff
changeset
|
519 |
#> do_proof (map snd def_thms) (after_qed cs) arities) |
| 19038 | 520 |
end; |
521 |
||
| 19957 | 522 |
fun instance_arity' do_proof = gen_instance_arity Sign.read_arity Attrib.attribute |
523 |
read_def do_proof; |
|
524 |
fun instance_arity_i' do_proof = gen_instance_arity Sign.cert_arity (K I) |
|
525 |
read_def_i do_proof; |
|
|
20182
79c9ff40d760
tactic for prove_instance_arity now gets definition theorems as arguments
haftmann
parents:
20175
diff
changeset
|
526 |
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
|
527 |
fold (fn arity => AxClass.prove_arity arity (tac def_thms)) arities |
| 20175 | 528 |
#> after_qed; |
|
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
529 |
|
|
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
530 |
in |
|
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
531 |
|
|
20182
79c9ff40d760
tactic for prove_instance_arity now gets definition theorems as arguments
haftmann
parents:
20175
diff
changeset
|
532 |
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
|
533 |
val instance_arity_i = instance_arity_i' (K axclass_instance_arity_i); |
| 19150 | 534 |
val prove_instance_arity = instance_arity_i' o tactic_proof; |
|
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
535 |
|
|
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
536 |
end; (* local *) |
|
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
537 |
|
|
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
538 |
local |
|
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
539 |
|
| 19966 | 540 |
fun add_interpretation_in (after_qed : theory -> theory) (name, expr) thy = |
| 19136 | 541 |
thy |
542 |
|> Locale.interpretation_in_locale (name, expr); |
|
543 |
||
| 19966 | 544 |
fun prove_interpretation_in tac (after_qed : theory -> theory) (name, expr) thy = |
| 19136 | 545 |
thy |
546 |
|> Locale.interpretation_in_locale (name, expr) |
|
547 |
|> Proof.global_terminal_proof (Method.Basic (fn _ => Method.SIMPLE_METHOD tac), NONE) |
|
| 19966 | 548 |
|> snd |
549 |
|> after_qed; |
|
| 19038 | 550 |
|
| 19150 | 551 |
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
|
552 |
let |
|
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
553 |
val class = prep_class theory raw_class; |
|
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
554 |
val sort = prep_sort theory raw_sort; |
| 19966 | 555 |
val loc_name = (#name_locale o fst o the_class_data theory) class; |
| 19468 | 556 |
val loc_expr = |
| 19966 | 557 |
(Locale.Merge o map (Locale.Locale o #name_locale o fst o the_class_data theory)) sort; |
558 |
val const_names = (map (NameSpace.base o fst o snd) |
|
559 |
o maps (#consts o fst o the_class_data theory) |
|
560 |
o the_ancestry theory) [class]; |
|
| 20106 | 561 |
fun get_thms thy = |
562 |
the_ancestry thy sort |
|
563 |
|> AList.make (the_propnames thy) |
|
564 |
|> map (apsnd (map (NameSpace.append (loc_name)))) |
|
565 |
|> map_filter (fn (superclass, thm_names) => |
|
566 |
case map_filter (try (PureThy.get_thm thy o Name)) thm_names |
|
567 |
of [] => NONE |
|
568 |
| thms => SOME (superclass, thms)); |
|
569 |
fun after_qed thy = |
|
570 |
thy |
|
571 |
|> `get_thms |
|
572 |
|-> fold (fn (supclass, thms) => I |
|
573 |
AxClass.prove_classrel (class, supclass) |
|
574 |
(ALLGOALS (K (intro_classes_tac [])) THEN |
|
575 |
(ALLGOALS o ProofContext.fact_tac) thms)) |
|
|
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
576 |
in |
|
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
577 |
theory |
| 19136 | 578 |
|> do_proof after_qed (loc_name, loc_expr) |
|
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
579 |
end; |
|
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
580 |
|
| 19150 | 581 |
fun instance_sort' do_proof = gen_instance_sort intern_class read_sort do_proof; |
582 |
fun instance_sort_i' do_proof = gen_instance_sort certify_class certify_sort do_proof; |
|
| 19136 | 583 |
val setup_proof = add_interpretation_in; |
584 |
val tactic_proof = prove_interpretation_in; |
|
|
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
585 |
|
|
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
586 |
in |
|
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
587 |
|
| 19150 | 588 |
val instance_sort = instance_sort' setup_proof; |
589 |
val instance_sort_i = instance_sort_i' setup_proof; |
|
590 |
val prove_instance_sort = instance_sort_i' o tactic_proof; |
|
|
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
591 |
|
|
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
592 |
end; (* local *) |
| 19038 | 593 |
|
| 19957 | 594 |
|
| 18168 | 595 |
(* extracting dictionary obligations from types *) |
596 |
||
597 |
type sortcontext = (string * sort) list; |
|
598 |
||
| 20265 | 599 |
fun sortcontext_of_typ thy ty = fold_atyps |
600 |
(fn TFree (a, S) => |
|
601 |
(case operational_sort_of thy S of |
|
602 |
[] => I |
|
603 |
| S' => insert (op =) (a, S')) |
|
604 |
| T => raise TYPE ("Illegal schematic type variable", [T], [])) ty []
|
|
605 |
|> sort_wrt fst; (* FIXME really required?!? *) |
|
| 18168 | 606 |
|
| 18884 | 607 |
datatype classlookup = Instance of (class * string) * classlookup list list |
| 19253 | 608 |
| Lookup of class list * (string * (int * int)) |
| 19213 | 609 |
|
610 |
fun pretty_lookup' (Instance ((class, tyco), lss)) = |
|
611 |
(Pretty.block o Pretty.breaks) ( |
|
612 |
Pretty.enum "," "{" "}" [Pretty.str class, Pretty.str tyco]
|
|
613 |
:: map pretty_lookup lss |
|
614 |
) |
|
| 19253 | 615 |
| pretty_lookup' (Lookup (classes, (v, (i, j)))) = |
|
19953
2f54a51f1801
class package refinements, slight code generation refinements
haftmann
parents:
19929
diff
changeset
|
616 |
Pretty.enum " <" "[" "]" (map Pretty.str classes @ |
|
2f54a51f1801
class package refinements, slight code generation refinements
haftmann
parents:
19929
diff
changeset
|
617 |
[Pretty.str (v ^ "!" ^ string_of_int i ^ "/" ^ string_of_int j)]) |
| 19213 | 618 |
and pretty_lookup ls = (Pretty.enum "," "(" ")" o map pretty_lookup') ls;
|
| 18168 | 619 |
|
|
19953
2f54a51f1801
class package refinements, slight code generation refinements
haftmann
parents:
19929
diff
changeset
|
620 |
fun sortlookup thy (sort_decl, typ_ctxt) = |
| 18168 | 621 |
let |
|
19953
2f54a51f1801
class package refinements, slight code generation refinements
haftmann
parents:
19929
diff
changeset
|
622 |
val pp = Sign.pp thy; |
|
2f54a51f1801
class package refinements, slight code generation refinements
haftmann
parents:
19929
diff
changeset
|
623 |
val algebra = Sorts.project_algebra pp (is_operational_class thy) |
|
2f54a51f1801
class package refinements, slight code generation refinements
haftmann
parents:
19929
diff
changeset
|
624 |
(Sign.classes_of thy); |
|
2f54a51f1801
class package refinements, slight code generation refinements
haftmann
parents:
19929
diff
changeset
|
625 |
fun classrel (l as Lookup (classes, p), _) class = |
|
2f54a51f1801
class package refinements, slight code generation refinements
haftmann
parents:
19929
diff
changeset
|
626 |
Lookup (class :: classes, p) |
|
2f54a51f1801
class package refinements, slight code generation refinements
haftmann
parents:
19929
diff
changeset
|
627 |
| classrel (Instance ((_, tyco), lss), _) class = |
|
2f54a51f1801
class package refinements, slight code generation refinements
haftmann
parents:
19929
diff
changeset
|
628 |
Instance ((class, tyco), lss); |
|
2f54a51f1801
class package refinements, slight code generation refinements
haftmann
parents:
19929
diff
changeset
|
629 |
fun constructor tyco lss class = |
|
2f54a51f1801
class package refinements, slight code generation refinements
haftmann
parents:
19929
diff
changeset
|
630 |
Instance ((class, tyco), (map o map) fst lss) |
|
2f54a51f1801
class package refinements, slight code generation refinements
haftmann
parents:
19929
diff
changeset
|
631 |
fun variable (TFree (v, sort)) = |
|
2f54a51f1801
class package refinements, slight code generation refinements
haftmann
parents:
19929
diff
changeset
|
632 |
map_index (fn (n, class) => (Lookup ([], (v, (n, length sort))), class)) |
|
2f54a51f1801
class package refinements, slight code generation refinements
haftmann
parents:
19929
diff
changeset
|
633 |
(operational_sort_of thy sort) |
|
2f54a51f1801
class package refinements, slight code generation refinements
haftmann
parents:
19929
diff
changeset
|
634 |
| variable (TVar _) = error "TVar encountered while deriving sortlookup"; |
| 18864 | 635 |
in |
|
19953
2f54a51f1801
class package refinements, slight code generation refinements
haftmann
parents:
19929
diff
changeset
|
636 |
Sorts.of_sort_derivation pp algebra |
|
2f54a51f1801
class package refinements, slight code generation refinements
haftmann
parents:
19929
diff
changeset
|
637 |
{classrel = classrel, constructor = constructor, variable = variable}
|
|
2f54a51f1801
class package refinements, slight code generation refinements
haftmann
parents:
19929
diff
changeset
|
638 |
(typ_ctxt, operational_sort_of thy sort_decl) |
| 18864 | 639 |
end; |
640 |
||
|
19953
2f54a51f1801
class package refinements, slight code generation refinements
haftmann
parents:
19929
diff
changeset
|
641 |
fun sortlookups_const thy (c, typ_ctxt) = |
| 18864 | 642 |
let |
| 20106 | 643 |
val typ_decl = case AxClass.class_of_param thy c |
|
19953
2f54a51f1801
class package refinements, slight code generation refinements
haftmann
parents:
19929
diff
changeset
|
644 |
of NONE => Sign.the_const_type thy c |
|
2f54a51f1801
class package refinements, slight code generation refinements
haftmann
parents:
19929
diff
changeset
|
645 |
| SOME class => case the_consts_sign thy class of (v, cs) => |
|
2f54a51f1801
class package refinements, slight code generation refinements
haftmann
parents:
19929
diff
changeset
|
646 |
(Logic.legacy_varifyT o subst_clsvar v (TFree (v, [class]))) |
|
2f54a51f1801
class package refinements, slight code generation refinements
haftmann
parents:
19929
diff
changeset
|
647 |
((the o AList.lookup (op =) cs) c) |
| 18168 | 648 |
in |
| 19957 | 649 |
instantiations_of thy (typ_decl, typ_ctxt) |
650 |
|> the |
|
651 |
|> map (fn ((_, sort), ty) => sortlookup thy (sort, ty)) |
|
|
19953
2f54a51f1801
class package refinements, slight code generation refinements
haftmann
parents:
19929
diff
changeset
|
652 |
|> filter_out null |
| 18168 | 653 |
end; |
654 |
||
| 18884 | 655 |
|
| 18515 | 656 |
(* toplevel interface *) |
657 |
||
658 |
local |
|
659 |
||
660 |
structure P = OuterParse |
|
661 |
and K = OuterKeyword |
|
662 |
||
663 |
in |
|
664 |
||
|
18849
05a16861d3a5
added three times overloaded Isar instance command
haftmann
parents:
18829
diff
changeset
|
665 |
val (classK, instanceK) = ("class", "instance")
|
| 18515 | 666 |
|
| 20106 | 667 |
fun wrap_add_instance_sort ((class, sort), use_interp) thy = |
668 |
if use_interp |
|
| 19966 | 669 |
andalso forall (is_some o lookup_class_data thy) (Sign.read_sort thy sort) |
670 |
then |
|
671 |
instance_sort (class, sort) thy |
|
672 |
else |
|
| 20106 | 673 |
axclass_instance_sort (class, sort) thy; |
| 19136 | 674 |
|
| 18911 | 675 |
val parse_inst = |
| 19136 | 676 |
(Scan.optional (P.$$$ "(" |-- P.!!! (P.list1 P.sort --| P.$$$ ")")) [] -- P.xname --| P.$$$ "::" -- P.sort)
|
677 |
>> (fn ((asorts, tyco), sort) => ((tyco, asorts), sort)) |
|
678 |
|| (P.xname --| P.$$$ "::" -- P.!!! P.arity) |
|
679 |
>> (fn (tyco, (asorts, sort)) => ((tyco, asorts), sort)); |
|
| 18911 | 680 |
|
| 19038 | 681 |
val locale_val = |
682 |
(P.locale_expr -- |
|
683 |
Scan.optional (P.$$$ "+" |-- P.!!! (Scan.repeat1 P.context_element)) [] || |
|
684 |
Scan.repeat1 P.context_element >> pair Locale.empty); |
|
685 |
||
|
19280
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
19253
diff
changeset
|
686 |
val class_subP = P.name -- Scan.repeat (P.$$$ "+" |-- P.name) >> (op ::); |
|
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
19253
diff
changeset
|
687 |
val class_bodyP = P.!!! (Scan.repeat1 P.context_element); |
|
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
19253
diff
changeset
|
688 |
|
| 18515 | 689 |
val classP = |
|
18849
05a16861d3a5
added three times overloaded Isar instance command
haftmann
parents:
18829
diff
changeset
|
690 |
OuterSyntax.command classK "operational type classes" K.thy_decl ( |
|
05a16861d3a5
added three times overloaded Isar instance command
haftmann
parents:
18829
diff
changeset
|
691 |
P.name --| P.$$$ "=" |
|
19280
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
19253
diff
changeset
|
692 |
-- ( |
|
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
19253
diff
changeset
|
693 |
class_subP --| P.$$$ "+" -- class_bodyP |
|
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
19253
diff
changeset
|
694 |
|| class_subP >> rpair [] |
|
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
19253
diff
changeset
|
695 |
|| class_bodyP >> pair [] |
|
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
19253
diff
changeset
|
696 |
) >> (Toplevel.theory_context |
|
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
19253
diff
changeset
|
697 |
o (fn (bname, (supclasses, elems)) => class bname supclasses elems))); |
| 18515 | 698 |
|
| 18575 | 699 |
val instanceP = |
|
18849
05a16861d3a5
added three times overloaded Isar instance command
haftmann
parents:
18829
diff
changeset
|
700 |
OuterSyntax.command instanceK "prove type arity or subclass relation" K.thy_goal (( |
| 20106 | 701 |
P.xname -- ((P.$$$ "\\<subseteq>" || P.$$$ "<") |-- P.!!! P.xname) -- P.opt_keyword "open" >> wrap_add_instance_sort |
| 20175 | 702 |
|| P.opt_thm_name ":" -- (P.and_list1 parse_inst -- Scan.repeat (P.opt_thm_name ":" -- P.prop)) |
703 |
>> (fn (("", []), ([((tyco, asorts), sort)], [])) => axclass_instance_arity I [(tyco, asorts, sort)]
|
|
704 |
| (natts, (arities, defs)) => instance_arity arities natts defs) |
|
|
18849
05a16861d3a5
added three times overloaded Isar instance command
haftmann
parents:
18829
diff
changeset
|
705 |
) >> (Toplevel.print oo Toplevel.theory_to_proof)); |
| 18575 | 706 |
|
|
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
707 |
val _ = OuterSyntax.add_parsers [classP, instanceP]; |
|
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
708 |
|
| 18515 | 709 |
end; (* local *) |
710 |
||
| 18168 | 711 |
end; (* struct *) |