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