author | haftmann |
Tue, 09 Jan 2007 19:08:59 +0100 | |
changeset 22048 | 990b5077590d |
parent 21954 | ffeb00290397 |
child 22069 | 8668c056c507 |
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 |
|
21954 | 10 |
val class: bstring -> class list -> Element.context_i Locale.element list -> theory -> |
20964 | 11 |
string * Proof.context |
21954 | 12 |
val instance_arity: ((bstring * sort list) * sort) list |
21924 | 13 |
-> ((bstring * attribute list) * term) list |
18575 | 14 |
-> theory -> Proof.state |
21924 | 15 |
val prove_instance_arity: tactic -> ((string * sort list) * sort) list |
16 |
-> ((bstring * attribute list) * term) list |
|
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
17 |
-> theory -> theory |
21954 | 18 |
val instance_sort: class * sort -> theory -> Proof.state |
19150 | 19 |
val prove_instance_sort: tactic -> class * sort -> theory -> theory |
18168 | 20 |
|
20175 | 21 |
val assume_arities_of_sort: theory -> ((string * sort list) * sort) list -> typ * sort -> bool |
22 |
val assume_arities_thy: theory -> ((string * sort list) * sort) list -> (theory -> 'a) -> 'a |
|
23 |
(*'a must not keep any reference to theory*) |
|
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
24 |
|
21954 | 25 |
(* experimental class target *) |
21553 | 26 |
val add_def: class * thm -> theory -> theory |
27 |
val export_typ: theory -> class -> typ -> typ |
|
28 |
val export_def: theory -> class -> term -> term |
|
29 |
val export_thm: theory -> class -> thm -> thm |
|
30 |
||
18702 | 31 |
val print_classes: theory -> unit |
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
32 |
val intro_classes_tac: thm list -> tactic |
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
33 |
val default_intro_classes_tac: thm list -> tactic |
18168 | 34 |
end; |
35 |
||
20182
79c9ff40d760
tactic for prove_instance_arity now gets definition theorems as arguments
haftmann
parents:
20175
diff
changeset
|
36 |
structure ClassPackage : CLASS_PACKAGE = |
18168 | 37 |
struct |
38 |
||
21954 | 39 |
(** axclasses with implicit parameter handling **) |
18168 | 40 |
|
21954 | 41 |
(* axclass instances *) |
18168 | 42 |
|
21954 | 43 |
local |
19456 | 44 |
|
21954 | 45 |
fun gen_instance mk_prop add_thm after_qed insts thy = |
46 |
let |
|
47 |
fun after_qed' results = |
|
48 |
ProofContext.theory ((fold o fold) add_thm results #> after_qed); |
|
49 |
in |
|
50 |
thy |
|
51 |
|> ProofContext.init |
|
52 |
|> Proof.theorem_i NONE after_qed' ((map (fn t => [(t, [])]) o maps (mk_prop thy)) insts) |
|
53 |
end; |
|
18168 | 54 |
|
21954 | 55 |
in |
56 |
||
57 |
val axclass_instance_arity = |
|
58 |
gen_instance (Logic.mk_arities oo Sign.cert_arity) AxClass.add_arity; |
|
59 |
val axclass_instance_sort = |
|
60 |
gen_instance (single oo (Logic.mk_classrel oo AxClass.cert_classrel)) |
|
61 |
AxClass.add_classrel I o single; |
|
62 |
||
63 |
end; (* local *) |
|
18575 | 64 |
|
19038 | 65 |
|
21954 | 66 |
(* introducing axclasses with implicit parameter handling *) |
19038 | 67 |
|
21954 | 68 |
fun axclass_params (name, raw_superclasses) raw_consts raw_dep_axioms thy = |
21924 | 69 |
let |
21954 | 70 |
val superclasses = map (Sign.certify_class thy) raw_superclasses; |
71 |
val consts = (map o apfst o apsnd) (Sign.certify_typ thy) raw_consts; |
|
72 |
fun add_const ((c, ty), syn) = |
|
73 |
Sign.add_consts_authentic [(c, ty, syn)] #> pair (Sign.full_name thy c, ty); |
|
74 |
fun mk_axioms cs thy = |
|
75 |
raw_dep_axioms thy cs |
|
76 |
|> (map o apsnd o map) (Sign.cert_prop thy) |
|
77 |
|> rpair thy; |
|
78 |
fun add_constraint class (c, ty) = |
|
79 |
Sign.add_const_constraint_i (c, SOME (Term.map_type_tfree (fn (v, _) => TFree (v, [class])) ty)); |
|
21924 | 80 |
in |
21954 | 81 |
thy |
82 |
|> fold_map add_const consts |
|
83 |
|-> (fn cs => mk_axioms cs |
|
84 |
#-> (fn axioms => AxClass.define_class_i (name, superclasses) (map fst cs) axioms |
|
85 |
#-> (fn class => `(fn thy => AxClass.get_definition thy class) |
|
86 |
#-> (fn {intro, axioms, ...} => fold (add_constraint class) cs |
|
87 |
#> pair (class, ((intro, axioms), cs)))))) |
|
21924 | 88 |
end; |
89 |
||
18168 | 90 |
|
21954 | 91 |
(* contexts with arity assumptions *) |
20175 | 92 |
|
93 |
fun assume_arities_of_sort thy arities ty_sort = |
|
94 |
let |
|
95 |
val pp = Sign.pp thy; |
|
96 |
val algebra = Sign.classes_of thy |
|
97 |
|> fold (fn ((tyco, asorts), sort) => |
|
98 |
Sorts.add_arities pp (tyco, map (fn class => (class, asorts)) sort)) arities; |
|
99 |
in Sorts.of_sort algebra ty_sort end; |
|
100 |
||
101 |
fun assume_arities_thy thy arities f = |
|
102 |
let |
|
103 |
val thy_read = (fold (fn ((tyco, asorts), sort) |
|
104 |
=> Sign.primitive_arity (tyco, asorts, sort)) arities o Theory.copy) thy |
|
105 |
in f thy_read end; |
|
106 |
||
20455 | 107 |
|
21954 | 108 |
(* instances with implicit parameter handling *) |
19038 | 109 |
|
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
110 |
local |
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
111 |
|
20175 | 112 |
fun gen_read_def thy prep_att read_def ((raw_name, raw_atts), raw_t) = |
19038 | 113 |
let |
19957 | 114 |
val (_, t) = read_def thy (raw_name, raw_t); |
115 |
val ((c, ty), _) = Sign.cert_def (Sign.pp thy) t; |
|
116 |
val atts = map (prep_att thy) raw_atts; |
|
20175 | 117 |
val insts = (Consts.typargs (Sign.consts_of thy) (c, ty)) |
19957 | 118 |
val name = case raw_name |
20385 | 119 |
of "" => NONE |
120 |
| _ => SOME raw_name; |
|
20175 | 121 |
in (c, (insts, ((name, t), atts))) end; |
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
122 |
|
21954 | 123 |
fun read_def_e thy = gen_read_def thy Attrib.attribute read_axm; |
124 |
fun read_def thy = gen_read_def thy (K I) (K I); |
|
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
125 |
|
21924 | 126 |
fun gen_instance_arity prep_arity prep_att read_def do_proof raw_arities raw_defs theory = |
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
127 |
let |
20175 | 128 |
fun prep_arity' ((tyco, asorts), sort) = prep_arity theory (tyco, asorts, sort); |
129 |
val arities = map prep_arity' raw_arities; |
|
130 |
val arities_pair = map (fn (tyco, asorts, sort) => ((tyco, asorts), sort)) arities; |
|
131 |
val _ = if null arities then error "at least one arity must be given" else (); |
|
132 |
val _ = case (duplicates (op =) o map #1) arities |
|
133 |
of [] => () |
|
134 |
| dupl_tycos => error ("type constructors occur more than once in arities: " |
|
135 |
^ (commas o map quote) dupl_tycos); |
|
21954 | 136 |
val super_sort = (Graph.all_succs o #classes o Sorts.rep_algebra o Sign.classes_of) theory |
20175 | 137 |
fun get_consts_class tyco ty class = |
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
138 |
let |
21954 | 139 |
val cs = (these o Option.map snd o try (AxClass.params_of_class theory)) class; |
21463
42dd50268c8b
completed class parameter handling in axclass.ML
haftmann
parents:
21444
diff
changeset
|
140 |
val subst_ty = map_type_tfree (K ty); |
19957 | 141 |
in |
21894 | 142 |
map (fn (c, ty) => (c, ((tyco, class), subst_ty ty))) cs |
19957 | 143 |
end; |
20175 | 144 |
fun get_consts_sort (tyco, asorts, sort) = |
19957 | 145 |
let |
20192
956cd30ef3be
renamed Name.give_names to Name.names and moved Name.alphanum to Symbol.alphanum
haftmann
parents:
20191
diff
changeset
|
146 |
val ty = Type (tyco, map (fn (v, sort) => TVar ((v, 0), sort)) (Name.names Name.context "'a" asorts)) |
21954 | 147 |
in maps (get_consts_class tyco ty) (super_sort sort) end; |
20175 | 148 |
val cs = maps get_consts_sort arities; |
20385 | 149 |
fun mk_typnorm thy (ty, ty_sc) = |
150 |
case try (Sign.typ_match thy (Logic.varifyT ty_sc, ty)) Vartab.empty |
|
151 |
of SOME env => SOME (Logic.varifyT #> Envir.typ_subst_TVars env #> Logic.unvarifyT) |
|
152 |
| NONE => NONE; |
|
20175 | 153 |
fun read_defs defs cs thy_read = |
154 |
let |
|
19957 | 155 |
fun read raw_def cs = |
156 |
let |
|
20385 | 157 |
val (c, (inst, ((name_opt, t), atts))) = read_def thy_read raw_def; |
158 |
val ty = Consts.instance (Sign.consts_of thy_read) (c, inst); |
|
20175 | 159 |
val ((tyco, class), ty') = case AList.lookup (op =) cs c |
19957 | 160 |
of NONE => error ("superfluous definition for constant " ^ quote c) |
19966 | 161 |
| SOME class_ty => class_ty; |
20385 | 162 |
val name = case name_opt |
20628 | 163 |
of NONE => Thm.def_name (Logic.name_arity (tyco, [], c)) |
20385 | 164 |
| SOME name => name; |
165 |
val t' = case mk_typnorm thy_read (ty', ty) |
|
19966 | 166 |
of NONE => error ("superfluous definition for constant " ^ |
167 |
quote c ^ "::" ^ Sign.string_of_typ thy_read ty) |
|
20548
8ef25fe585a8
renamed Term.map_term_types to Term.map_types (cf. Term.fold_types);
wenzelm
parents:
20465
diff
changeset
|
168 |
| SOME norm => map_types norm t |
20175 | 169 |
in (((class, tyco), ((name, t'), atts)), AList.delete (op =) c cs) end; |
19957 | 170 |
in fold_map read defs cs end; |
20175 | 171 |
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
|
172 |
fun get_remove_contraint c thy = |
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
173 |
let |
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
174 |
val ty = Sign.the_const_constraint thy c; |
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
175 |
in |
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
176 |
thy |
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
177 |
|> Sign.add_const_constraint_i (c, NONE) |
20385 | 178 |
|> pair (c, Logic.unvarifyT ty) |
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
179 |
end; |
19966 | 180 |
fun add_defs defs thy = |
181 |
thy |
|
182 |
|> PureThy.add_defs_i true (map snd defs) |
|
183 |
|-> (fn thms => pair (map fst defs ~~ thms)); |
|
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
184 |
fun after_qed cs thy = |
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
185 |
thy |
19412 | 186 |
|> fold Sign.add_const_constraint_i (map (apsnd SOME) cs); |
19038 | 187 |
in |
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
188 |
theory |
20385 | 189 |
|> fold_map get_remove_contraint (map fst cs |> distinct (op =)) |
19966 | 190 |
||>> add_defs defs |
21954 | 191 |
|-> (fn (cs, _) => do_proof (after_qed cs) arities) |
19038 | 192 |
end; |
193 |
||
21954 | 194 |
fun instance_arity_e' do_proof = gen_instance_arity Sign.read_arity Attrib.attribute |
195 |
read_def_e do_proof; |
|
196 |
fun instance_arity' do_proof = gen_instance_arity Sign.cert_arity (K I) |
|
19957 | 197 |
read_def do_proof; |
21924 | 198 |
fun tactic_proof tac after_qed arities = |
199 |
fold (fn arity => AxClass.prove_arity arity tac) arities |
|
20175 | 200 |
#> after_qed; |
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
201 |
|
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
202 |
in |
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
203 |
|
21954 | 204 |
val instance_arity_e = instance_arity_e' axclass_instance_arity; |
205 |
val instance_arity = instance_arity' axclass_instance_arity; |
|
206 |
val prove_instance_arity = instance_arity' o tactic_proof; |
|
207 |
||
208 |
end; (* local *) |
|
209 |
||
210 |
||
211 |
||
212 |
(** combining locales and axclasses **) |
|
213 |
||
214 |
(* theory data *) |
|
215 |
||
216 |
datatype class_data = ClassData of { |
|
217 |
locale: string, |
|
218 |
consts: (string * string) list |
|
219 |
(*locale parameter ~> toplevel theory constant*), |
|
220 |
propnames: string list, |
|
221 |
defs: thm list |
|
222 |
(*for experimental class target*) |
|
223 |
}; |
|
224 |
||
225 |
fun rep_classdata (ClassData c) = c; |
|
226 |
||
227 |
structure ClassData = TheoryDataFun ( |
|
228 |
struct |
|
229 |
val name = "Pure/classes"; |
|
230 |
type T = class_data Graph.T; |
|
231 |
val empty = Graph.empty; |
|
232 |
val copy = I; |
|
233 |
val extend = I; |
|
234 |
fun merge _ = Graph.merge (K true); |
|
235 |
fun print _ _ = (); |
|
236 |
end |
|
237 |
); |
|
238 |
||
239 |
val _ = Context.add_setup ClassData.init; |
|
240 |
||
241 |
||
242 |
(* queries *) |
|
243 |
||
244 |
val lookup_class_data = Option.map rep_classdata oo try o Graph.get_node o ClassData.get; |
|
245 |
val is_class = can o Graph.get_node o ClassData.get; |
|
246 |
fun the_class_data thy class = |
|
247 |
case lookup_class_data thy class |
|
248 |
of NONE => error ("undeclared class " ^ quote class) |
|
249 |
| SOME data => data; |
|
250 |
||
251 |
fun the_ancestry thy = Graph.all_succs (ClassData.get thy); |
|
252 |
||
253 |
fun the_parm_map thy class = |
|
254 |
let |
|
255 |
val const_typs = (#params o AxClass.get_definition thy) class; |
|
256 |
val const_names = (#consts o the_class_data thy) class; |
|
257 |
in |
|
258 |
map (apsnd (fn c => (c, (the o AList.lookup (op =) const_typs) c))) const_names |
|
259 |
end; |
|
260 |
||
261 |
val the_propnames = #propnames oo the_class_data; |
|
262 |
||
263 |
fun print_classes thy = |
|
264 |
let |
|
265 |
val algebra = Sign.classes_of thy; |
|
266 |
val arities = |
|
267 |
Symtab.empty |
|
268 |
|> Symtab.fold (fn (tyco, arities) => fold (fn (class, _) => |
|
269 |
Symtab.map_default (class, []) (insert (op =) tyco)) arities) |
|
270 |
((#arities o Sorts.rep_algebra) algebra); |
|
271 |
val the_arities = these o Symtab.lookup arities; |
|
272 |
fun mk_arity class tyco = |
|
273 |
let |
|
274 |
val Ss = Sorts.mg_domain algebra tyco [class]; |
|
275 |
in Sign.pretty_arity thy (tyco, Ss, [class]) end; |
|
276 |
fun mk_param (c, ty) = Pretty.str (Sign.extern_const thy c ^ " :: " |
|
277 |
^ setmp show_sorts false (Sign.string_of_typ thy o Type.strip_sorts) ty); |
|
278 |
fun mk_entry class = (Pretty.block o Pretty.fbreaks o map_filter I) [ |
|
279 |
(SOME o Pretty.str) ("class " ^ class ^ ":"), |
|
280 |
(SOME o Pretty.block) [Pretty.str "supersort: ", |
|
281 |
(Sign.pretty_sort thy o Sign.certify_sort thy o Sign.super_classes thy) class], |
|
282 |
Option.map (Pretty.str o prefix "locale: " o #locale) (lookup_class_data thy class), |
|
283 |
((fn [] => NONE | ps => (SOME o Pretty.block o Pretty.fbreaks) (Pretty.str "parameters:" :: ps)) o map mk_param |
|
284 |
o these o Option.map #params o try (AxClass.get_definition thy)) class, |
|
285 |
(SOME o Pretty.block o Pretty.breaks) [ |
|
286 |
Pretty.str "instances:", |
|
287 |
Pretty.list "" "" (map (mk_arity class) (the_arities class)) |
|
288 |
] |
|
289 |
] |
|
290 |
in |
|
291 |
(Pretty.writeln o Pretty.chunks o separate (Pretty.str "") o map mk_entry o Sorts.all_classes) |
|
292 |
algebra |
|
293 |
end; |
|
294 |
||
295 |
||
296 |
(* updaters *) |
|
297 |
||
298 |
fun add_class_data ((class, superclasses), (locale, consts, propnames)) = |
|
299 |
ClassData.map ( |
|
300 |
Graph.new_node (class, ClassData { |
|
301 |
locale = locale, |
|
302 |
consts = consts, |
|
303 |
propnames = propnames, |
|
304 |
defs = []}) |
|
305 |
#> fold (curry Graph.add_edge class) superclasses |
|
306 |
); |
|
307 |
||
308 |
fun add_def (class, thm) = |
|
309 |
(ClassData.map o Graph.map_node class) |
|
310 |
(fn ClassData { locale, |
|
311 |
consts, propnames, defs } => ClassData { locale = locale, |
|
312 |
consts = consts, propnames = propnames, defs = thm :: defs }); |
|
313 |
||
314 |
||
315 |
(* tactics and methods *) |
|
316 |
||
317 |
fun intro_classes_tac facts st = |
|
318 |
(ALLGOALS (Method.insert_tac facts THEN' |
|
319 |
REPEAT_ALL_NEW (resolve_tac (AxClass.class_intros (Thm.theory_of_thm st)))) |
|
320 |
THEN Tactic.distinct_subgoals_tac) st; |
|
321 |
||
322 |
fun default_intro_classes_tac [] = intro_classes_tac [] |
|
323 |
| default_intro_classes_tac _ = Tactical.no_tac; (*no error message!*) |
|
324 |
||
325 |
fun default_tac rules ctxt facts = |
|
326 |
HEADGOAL (Method.some_rule_tac rules ctxt facts) ORELSE |
|
327 |
default_intro_classes_tac facts; |
|
328 |
||
329 |
val _ = Context.add_setup (Method.add_methods |
|
330 |
[("intro_classes", Method.no_args (Method.METHOD intro_classes_tac), |
|
331 |
"back-chain introduction rules of classes"), |
|
332 |
("default", Method.thms_ctxt_args (Method.METHOD oo default_tac), |
|
333 |
"apply some intro/elim rule")]); |
|
334 |
||
335 |
||
336 |
(* classes and instances *) |
|
337 |
||
338 |
local |
|
339 |
||
340 |
fun add_axclass (name, supsort) params axs thy = |
|
341 |
let |
|
342 |
val (c, thy') = thy |
|
343 |
|> AxClass.define_class_i (name, supsort) params axs; |
|
344 |
val {intro, axioms, ...} = AxClass.get_definition thy' c; |
|
345 |
in ((c, (intro, axioms)), thy') end; |
|
346 |
||
347 |
fun certify_class thy class = |
|
348 |
tap (the_class_data thy) (Sign.certify_class thy class); |
|
349 |
||
350 |
fun read_class thy = |
|
351 |
certify_class thy o Sign.intern_class thy; |
|
352 |
||
353 |
(*FIXME proper locale interface*) |
|
354 |
fun prove_interpretation (prfx, atts) expr insts tac thy = |
|
355 |
let |
|
356 |
fun ad_hoc_term (Const (c, ty)) = |
|
357 |
let |
|
358 |
val p = setmp show_types true (setmp show_sorts true (setmp print_mode [] (Sign.pretty_typ thy))) ty; |
|
359 |
val s = c ^ "::" ^ Pretty.output p; |
|
360 |
in s end |
|
361 |
| ad_hoc_term t = |
|
362 |
let |
|
363 |
val p = setmp show_types true (setmp show_sorts true (setmp print_mode [] (Sign.pretty_term thy))) t; |
|
364 |
val s = Pretty.output p; |
|
365 |
in s end; |
|
366 |
in |
|
367 |
thy |
|
368 |
|> Locale.interpretation I (prfx, atts) expr (map (Option.map ad_hoc_term) insts) |
|
369 |
|> Proof.global_terminal_proof (Method.Basic (fn _ => Method.SIMPLE_METHOD tac), NONE) |
|
370 |
|> ProofContext.theory_of |
|
371 |
end; |
|
372 |
||
373 |
fun gen_class add_locale prep_class bname raw_supclasses raw_elems thy = |
|
374 |
let |
|
375 |
val elems = fold_rev (fn Locale.Elem e => cons e | _ => I) raw_elems []; |
|
376 |
val supclasses = map (prep_class thy) raw_supclasses; |
|
377 |
val supsort = |
|
378 |
supclasses |
|
379 |
|> Sign.certify_sort thy |
|
380 |
|> (fn [] => Sign.defaultS thy | S => S); (* FIXME Why syntax defaultS? *) |
|
381 |
val supexpr = Locale.Merge (map (Locale.Locale o #locale o the_class_data thy) supclasses); |
|
382 |
val supconsts = AList.make |
|
383 |
(the o AList.lookup (op =) ((maps (the_parm_map thy) o the_ancestry thy) supclasses)) |
|
384 |
((map (fst o fst) o Locale.parameters_of_expr thy) supexpr); |
|
385 |
fun check_locale thy name_locale = |
|
386 |
let |
|
387 |
val tfrees = |
|
388 |
[] |
|
389 |
|> fold (fold Term.add_tfrees o snd) (Locale.global_asms_of thy name_locale) |
|
390 |
|> fold (Term.add_tfreesT o snd o fst) (Locale.parameters_of thy name_locale); |
|
391 |
in case tfrees |
|
392 |
of [(_, _)] => () |
|
393 |
(*| [(_, sort)] => error ("Additional sort constraint on class variable: " |
|
394 |
^ Sign.string_of_sort thy sort) FIXME what to do about this?*) |
|
395 |
| [] => error ("No type variable in class specification") |
|
396 |
| tfrees => error ("More than one type variable in class specification: " ^ |
|
397 |
(commas o map fst) tfrees) |
|
398 |
end; |
|
399 |
fun extract_params thy name_locale = |
|
400 |
Locale.parameters_of thy name_locale |
|
401 |
|> (map o apfst o apsnd o Term.map_type_tfree) (K (TFree (AxClass.param_tyvarname, []))) |
|
402 |
|> (map o apsnd) (TheoryTarget.fork_mixfix false true #> fst) |
|
403 |
|> chop (length supconsts) |
|
404 |
|> snd; |
|
405 |
fun extract_assumes name_locale params thy cs = |
|
406 |
let |
|
407 |
val consts = supconsts @ (map (fst o fst) params ~~ cs); |
|
408 |
(*FIXME is this type handling correct?*) |
|
409 |
fun subst (Free (c, ty)) = |
|
22048 | 410 |
Const ((fst o the o AList.lookup (op =) consts) c, ty) |
411 |
| subst t = t; |
|
21954 | 412 |
fun prep_asm ((name, atts), ts) = |
413 |
(*FIXME*) |
|
414 |
((NameSpace.base name, map (Attrib.attribute thy) atts), (map o map_aterms) subst ts); |
|
415 |
in |
|
416 |
Locale.local_asms_of thy name_locale |
|
417 |
|> map prep_asm |
|
418 |
end; |
|
22048 | 419 |
fun extract_axiom_names thy name_locale = |
420 |
name_locale |
|
421 |
|> Locale.local_asms_of thy |
|
422 |
|> map (NameSpace.base o fst o fst) (*FIXME*) |
|
21954 | 423 |
fun mk_const thy class (c, ty) = |
424 |
Const (c, Term.map_type_tfree (fn (v, _) => TFree (v, [class])) ty); |
|
425 |
in |
|
426 |
thy |
|
427 |
|> add_locale bname supexpr elems |
|
428 |
|-> (fn name_locale => ProofContext.theory_result ( |
|
429 |
tap (fn thy => check_locale thy name_locale) |
|
430 |
#> `(fn thy => extract_params thy name_locale) |
|
431 |
#-> (fn params => |
|
432 |
axclass_params (bname, supsort) params (extract_assumes name_locale params) |
|
433 |
#-> (fn (name_axclass, ((_, ax_axioms), consts)) => |
|
22048 | 434 |
`(fn thy => extract_axiom_names thy name_locale) |
435 |
#-> (fn axiom_names => |
|
436 |
add_class_data ((name_axclass, supclasses), |
|
437 |
(name_locale, map (fst o fst) params ~~ map fst consts, axiom_names)) |
|
21954 | 438 |
#> prove_interpretation (Logic.const_of_class bname, []) |
439 |
(Locale.Locale name_locale) (map (SOME o mk_const thy name_axclass) (map snd supconsts @ consts)) |
|
440 |
((ALLGOALS o ProofContext.fact_tac) ax_axioms) |
|
441 |
#> pair name_axclass |
|
22048 | 442 |
))))) |
21954 | 443 |
end; |
444 |
||
22048 | 445 |
(*fun gen_class add_locale prep_class bname raw_supclasses raw_elems thy = |
21954 | 446 |
let |
447 |
val elems = fold_rev (fn Locale.Elem e => cons e | _ => I) raw_elems []; |
|
448 |
val supclasses = map (prep_class thy) raw_supclasses; |
|
449 |
val supsort = |
|
450 |
supclasses |
|
451 |
|> Sign.certify_sort thy |
|
452 |
|> (fn [] => Sign.defaultS thy | S => S); (* FIXME Why syntax defaultS? *) |
|
453 |
val expr = Locale.Merge (map (Locale.Locale o #locale o the_class_data thy) supclasses); |
|
454 |
val mapp_sup = AList.make |
|
455 |
(the o AList.lookup (op =) ((maps (the_parm_map thy) o the_ancestry thy) supclasses)) |
|
456 |
((map (fst o fst) o Locale.parameters_of_expr thy) expr); |
|
457 |
fun extract_consts thy name_locale = |
|
458 |
let |
|
459 |
val tfrees = |
|
460 |
[] |
|
461 |
|> fold (fold Term.add_tfrees o snd) (Locale.global_asms_of thy name_locale) |
|
462 |
|> fold (Term.add_tfreesT o snd o fst) (Locale.parameters_of thy name_locale); |
|
463 |
val _ = case tfrees |
|
464 |
of [(_, _)] => () |
|
465 |
(*| [(_, sort)] => error ("Additional sort constraint on class variable: " |
|
466 |
^ Sign.string_of_sort thy sort) FIXME what to do about this?*) |
|
467 |
| [] => error ("No type variable in class specification") |
|
468 |
| tfrees => error ("More than one type variable in class specification: " ^ |
|
469 |
(commas o map fst) tfrees); |
|
470 |
val consts = |
|
471 |
Locale.parameters_of thy name_locale |
|
472 |
|> (map o apfst o apsnd o Term.map_type_tfree) (K (TFree (AxClass.param_tyvarname, []))) |
|
473 |
|> (map o apsnd) (TheoryTarget.fork_mixfix false true #> fst); |
|
474 |
in chop (length mapp_sup) consts |> snd end; |
|
475 |
fun add_consts raw_cs_this thy = |
|
476 |
let |
|
477 |
fun add_global_const ((c, ty), syn) thy = |
|
478 |
((c, (Sign.full_name thy c, ty)), |
|
479 |
thy |
|
480 |
|> Sign.add_consts_authentic |
|
481 |
[(c, ty |> Term.map_type_tfree (fn (v, _) => TFree (v, Sign.defaultS thy)), syn)]); |
|
482 |
in |
|
483 |
thy |
|
484 |
|> fold_map add_global_const raw_cs_this |
|
485 |
end; |
|
486 |
fun extract_assumes thy name_locale cs_mapp = |
|
487 |
let |
|
488 |
val subst_assume = |
|
489 |
map_aterms (fn Free (c, ty) => Const ((fst o the o AList.lookup (op =) cs_mapp) c, ty) |
|
490 |
| t => t) |
|
491 |
fun prep_asm ((name, atts), ts) = |
|
492 |
(*FIXME*) |
|
493 |
((NameSpace.base name, map (Attrib.attribute thy) atts), map subst_assume ts) |
|
494 |
in |
|
495 |
(map prep_asm o Locale.local_asms_of thy) name_locale |
|
496 |
end; |
|
497 |
fun add_global_constraint class (_, (c, ty)) thy = |
|
498 |
thy |
|
499 |
|> Sign.add_const_constraint_i (c, SOME (Term.map_type_tfree (fn (v, _) => TFree (v, [class])) ty)); |
|
500 |
fun mk_const thy class (c, ty) = |
|
501 |
Const (c, Term.map_type_tfree (fn (v, _) => TFree (v, [class])) ty); |
|
502 |
in |
|
503 |
thy |
|
504 |
|> add_locale bname expr elems |
|
505 |
|-> (fn name_locale => ProofContext.theory |
|
506 |
(`(fn thy => extract_consts thy name_locale) |
|
507 |
#-> (fn raw_cs_this => |
|
508 |
add_consts raw_cs_this |
|
509 |
#-> (fn mapp_this => |
|
510 |
`(fn thy => extract_assumes thy name_locale (mapp_sup @ mapp_this)) |
|
511 |
#-> (fn loc_axioms => |
|
512 |
add_axclass (bname, supsort) (map (fst o snd) mapp_this) loc_axioms |
|
513 |
#-> (fn (name_axclass, (_, ax_axioms)) => |
|
514 |
fold (add_global_constraint name_axclass) mapp_this |
|
515 |
#> add_class_data ((name_axclass, supclasses), (name_locale, map (apsnd fst) mapp_this, |
|
516 |
map (fst o fst) loc_axioms)) |
|
517 |
#> prove_interpretation (Logic.const_of_class bname, []) |
|
518 |
(Locale.Locale name_locale) (map (SOME o mk_const thy name_axclass) (map snd (mapp_sup @ mapp_this))) |
|
519 |
((ALLGOALS o ProofContext.fact_tac) ax_axioms) |
|
520 |
))))) #> pair name_locale) |
|
22048 | 521 |
end;*) |
21954 | 522 |
|
523 |
in |
|
524 |
||
525 |
val class_e = gen_class (Locale.add_locale false) read_class; |
|
526 |
val class = gen_class (Locale.add_locale_i false) certify_class; |
|
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
527 |
|
20455 | 528 |
end; (*local*) |
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
529 |
|
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
530 |
local |
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
531 |
|
20368
2461a0485623
global goals/qeds: after_qed operates on Proof.context (potentially local_theory);
wenzelm
parents:
20353
diff
changeset
|
532 |
fun prove_interpretation_in tac after_qed (name, expr) thy = |
19136 | 533 |
thy |
20368
2461a0485623
global goals/qeds: after_qed operates on Proof.context (potentially local_theory);
wenzelm
parents:
20353
diff
changeset
|
534 |
|> Locale.interpretation_in_locale (ProofContext.theory after_qed) (name, expr) |
19136 | 535 |
|> 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
|
536 |
|> ProofContext.theory_of; |
19038 | 537 |
|
20385 | 538 |
(*FIXME very ad-hoc, needs proper locale interface*) |
21954 | 539 |
fun instance_sort' do_proof (class, sort) theory = |
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
540 |
let |
21924 | 541 |
val loc_name = (#locale o the_class_data theory) class; |
19468 | 542 |
val loc_expr = |
21924 | 543 |
(Locale.Merge o map (Locale.Locale o #locale o the_class_data theory)) sort; |
21954 | 544 |
val const_names = (map (NameSpace.base o snd) |
21924 | 545 |
o maps (#consts o the_class_data theory) |
19966 | 546 |
o the_ancestry theory) [class]; |
20106 | 547 |
fun get_thms thy = |
548 |
the_ancestry thy sort |
|
549 |
|> AList.make (the_propnames thy) |
|
21751 | 550 |
|> map (apsnd (map (NameSpace.append (Logic.const_of_class loc_name)))) |
20106 | 551 |
|> map_filter (fn (superclass, thm_names) => |
552 |
case map_filter (try (PureThy.get_thm thy o Name)) thm_names |
|
553 |
of [] => NONE |
|
554 |
| thms => SOME (superclass, thms)); |
|
555 |
fun after_qed thy = |
|
556 |
thy |
|
557 |
|> `get_thms |
|
558 |
|-> fold (fn (supclass, thms) => I |
|
559 |
AxClass.prove_classrel (class, supclass) |
|
560 |
(ALLGOALS (K (intro_classes_tac [])) THEN |
|
561 |
(ALLGOALS o ProofContext.fact_tac) thms)) |
|
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
562 |
in |
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
563 |
theory |
19136 | 564 |
|> do_proof after_qed (loc_name, loc_expr) |
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
565 |
end; |
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
566 |
|
21954 | 567 |
val prove_instance_sort = instance_sort' o prove_interpretation_in; |
568 |
||
569 |
fun gen_instance_sort prep_class prep_sort (raw_class, raw_sort) theory = |
|
570 |
let |
|
571 |
val class = prep_class theory raw_class; |
|
572 |
val sort = prep_sort theory raw_sort; |
|
573 |
in if is_class theory class andalso forall (is_class theory) sort then |
|
574 |
theory |
|
575 |
|> instance_sort' (Locale.interpretation_in_locale o ProofContext.theory) (class, sort) |
|
576 |
else case sort |
|
577 |
of [class'] => |
|
578 |
theory |
|
579 |
|> axclass_instance_sort (class, class') |
|
580 |
| _ => error ("Exactly one class expected: " ^ Sign.string_of_sort theory sort) |
|
581 |
end; |
|
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
582 |
|
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
583 |
in |
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
584 |
|
21954 | 585 |
val instance_sort_e = gen_instance_sort Sign.read_class Sign.read_sort; |
586 |
val instance_sort = gen_instance_sort Sign.certify_class Sign.certify_sort; |
|
587 |
val prove_instance_sort = prove_instance_sort; |
|
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
588 |
|
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
589 |
end; (* local *) |
19038 | 590 |
|
19957 | 591 |
|
21954 | 592 |
(** experimental class target **) |
593 |
||
594 |
fun export_typ thy loc = |
|
595 |
let |
|
596 |
val class = loc (*FIXME*); |
|
597 |
val (v, _) = AxClass.params_of_class thy class; |
|
598 |
in |
|
599 |
Term.map_type_tfree (fn var as (w, sort) => |
|
600 |
if w = v then TFree (w, Sorts.inter_sort (Sign.classes_of thy) |
|
601 |
(sort, [class])) else TFree var) |
|
602 |
end; |
|
603 |
||
604 |
fun export_def thy loc = |
|
605 |
let |
|
606 |
val class = loc (*FIXME*); |
|
607 |
val consts = the_parm_map thy class; |
|
608 |
val subst_typ = Term.map_type_tfree (fn var as (w, sort) => |
|
609 |
if w = AxClass.param_tyvarname then TFree (w, Sorts.inter_sort (Sign.classes_of thy) |
|
610 |
(sort, [class])) else TFree var) |
|
611 |
fun subst (t as Free (v, _)) = (case AList.lookup (op =) consts v |
|
612 |
of SOME c_ty => Const c_ty |
|
613 |
| NONE => t) |
|
614 |
| subst t = t; |
|
615 |
in |
|
616 |
Term.map_aterms subst #> map_types subst_typ |
|
617 |
end; |
|
618 |
||
619 |
fun export_thm thy loc = |
|
620 |
let |
|
621 |
val class = loc (*FIXME*); |
|
622 |
val thms = (#defs o the_class_data thy) class; |
|
623 |
in |
|
624 |
MetaSimplifier.rewrite_rule thms |
|
625 |
end; |
|
626 |
||
627 |
||
628 |
||
20455 | 629 |
(** toplevel interface **) |
18515 | 630 |
|
631 |
local |
|
632 |
||
633 |
structure P = OuterParse |
|
634 |
and K = OuterKeyword |
|
635 |
||
636 |
in |
|
637 |
||
20385 | 638 |
val (classK, instanceK, print_classesK) = ("class", "instance", "print_classes") |
18515 | 639 |
|
20385 | 640 |
val class_subP = P.name -- Scan.repeat (P.$$$ "+" |-- P.name) >> (op ::); |
20428 | 641 |
val class_bodyP = P.!!! (Scan.repeat1 P.locale_element); |
20385 | 642 |
|
20601 | 643 |
val parse_arity = |
644 |
(P.xname --| P.$$$ "::" -- P.!!! P.arity) |
|
19136 | 645 |
>> (fn (tyco, (asorts, sort)) => ((tyco, asorts), sort)); |
18911 | 646 |
|
18515 | 647 |
val classP = |
20385 | 648 |
OuterSyntax.command classK "define operational type class" K.thy_decl ( |
18849
05a16861d3a5
added three times overloaded Isar instance command
haftmann
parents:
18829
diff
changeset
|
649 |
P.name --| P.$$$ "=" |
19280
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
19253
diff
changeset
|
650 |
-- ( |
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
19253
diff
changeset
|
651 |
class_subP --| P.$$$ "+" -- class_bodyP |
5091dc43817b
slight improvement in serializer, stub for code generator theorems added
haftmann
parents:
19253
diff
changeset
|
652 |
|| class_subP >> rpair [] |
20964 | 653 |
|| class_bodyP >> pair []) |
654 |
-- P.opt_begin |
|
655 |
>> (fn ((bname, (supclasses, elems)), begin) => |
|
21805 | 656 |
Toplevel.begin_local_theory begin |
21954 | 657 |
(class_e bname supclasses elems #-> TheoryTarget.begin true))); |
18515 | 658 |
|
18575 | 659 |
val instanceP = |
18849
05a16861d3a5
added three times overloaded Isar instance command
haftmann
parents:
18829
diff
changeset
|
660 |
OuterSyntax.command instanceK "prove type arity or subclass relation" K.thy_goal (( |
21954 | 661 |
P.xname -- ((P.$$$ "\\<subseteq>" || P.$$$ "<") |-- P.!!! P.xname) |
662 |
>> instance_sort_e |
|
21924 | 663 |
|| P.and_list1 parse_arity -- Scan.repeat (P.opt_thm_name ":" -- P.prop) |
21954 | 664 |
>> (fn (arities, defs) => instance_arity_e arities defs) |
18849
05a16861d3a5
added three times overloaded Isar instance command
haftmann
parents:
18829
diff
changeset
|
665 |
) >> (Toplevel.print oo Toplevel.theory_to_proof)); |
18575 | 666 |
|
20385 | 667 |
val print_classesP = |
668 |
OuterSyntax.improper_command print_classesK "print classes of this theory" K.diag |
|
669 |
(Scan.succeed (Toplevel.no_timing o Toplevel.unknown_theory |
|
670 |
o Toplevel.keep (print_classes o Toplevel.theory_of))); |
|
671 |
||
672 |
val _ = OuterSyntax.add_parsers [classP, instanceP, print_classesP]; |
|
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
673 |
|
20455 | 674 |
end; (*local*) |
18515 | 675 |
|
20455 | 676 |
end; |