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