author | berghofe |
Tue, 17 Jul 2007 16:05:34 +0200 | |
changeset 23831 | 64e6e5c738a1 |
parent 23351 | 3702086a15a3 |
child 23919 | af871d13e320 |
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 |
|
22353 | 10 |
val fork_mixfix: bool -> string option -> mixfix -> mixfix * mixfix |
22302 | 11 |
|
22737 | 12 |
val axclass_cmd: bstring * xstring list |
13 |
-> ((bstring * Attrib.src list) * string list) list -> theory -> class * theory |
|
22385 | 14 |
val class: bstring -> class list -> Element.context_i Locale.element list |
15 |
-> string list -> theory -> string * Proof.context |
|
16 |
val class_cmd: bstring -> string list -> Element.context Locale.element list |
|
17 |
-> string list -> theory -> string * Proof.context |
|
22331 | 18 |
val instance_arity: arity list -> ((bstring * Attrib.src list) * term) list |
22302 | 19 |
-> theory -> Proof.state |
22331 | 20 |
val instance_arity_cmd: (bstring * string list * string) list |
22302 | 21 |
-> ((bstring * Attrib.src list) * string) list |
18575 | 22 |
-> theory -> Proof.state |
22331 | 23 |
val prove_instance_arity: tactic -> arity list |
22302 | 24 |
-> ((bstring * Attrib.src list) * term) list |
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
25 |
-> theory -> theory |
22321 | 26 |
val instance_class: class * class -> theory -> Proof.state |
27 |
val instance_class_cmd: string * string -> theory -> Proof.state |
|
21954 | 28 |
val instance_sort: class * sort -> theory -> Proof.state |
22321 | 29 |
val instance_sort_cmd: string * string -> theory -> Proof.state |
19150 | 30 |
val prove_instance_sort: tactic -> class * sort -> theory -> theory |
18168 | 31 |
|
23184 | 32 |
val INTERPRET_DEF: bool ref |
22302 | 33 |
val class_of_locale: theory -> string -> class option |
23087 | 34 |
val add_const_in_class: string -> (string * term) * Syntax.mixfix |
35 |
-> theory -> theory |
|
21553 | 36 |
|
18702 | 37 |
val print_classes: theory -> unit |
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
38 |
val intro_classes_tac: thm list -> tactic |
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
39 |
val default_intro_classes_tac: thm list -> tactic |
18168 | 40 |
end; |
41 |
||
20182
79c9ff40d760
tactic for prove_instance_arity now gets definition theorems as arguments
haftmann
parents:
20175
diff
changeset
|
42 |
structure ClassPackage : CLASS_PACKAGE = |
18168 | 43 |
struct |
44 |
||
22302 | 45 |
(** auxiliary **) |
18168 | 46 |
|
22353 | 47 |
fun fork_mixfix is_loc some_class mx = |
22302 | 48 |
let |
49 |
val mx' = Syntax.unlocalize_mixfix mx; |
|
22737 | 50 |
val mx_global = if is_some some_class orelse (is_loc andalso mx = mx') |
51 |
then NoSyn else mx'; |
|
22353 | 52 |
val mx_local = if is_loc then mx else NoSyn; |
53 |
in (mx_global, mx_local) end; |
|
22302 | 54 |
|
22737 | 55 |
fun axclass_cmd (class, raw_superclasses) raw_specs thy = |
56 |
let |
|
57 |
val ctxt = ProofContext.init thy; |
|
58 |
val superclasses = map (Sign.read_class thy) raw_superclasses; |
|
59 |
val name_atts = map ((apsnd o map) (Attrib.attribute thy) o fst) raw_specs; |
|
60 |
val axiomss = ProofContext.read_propp (ctxt, map (map (rpair []) o snd) raw_specs) |
|
61 |
|> snd |
|
62 |
|> (map o map) fst; |
|
63 |
in AxClass.define_class (class, superclasses) [] (name_atts ~~ axiomss) thy end; |
|
64 |
||
22302 | 65 |
|
66 |
(** AxClasses with implicit parameter handling **) |
|
67 |
||
68 |
(* AxClass instances *) |
|
18168 | 69 |
|
21954 | 70 |
local |
19456 | 71 |
|
21954 | 72 |
fun gen_instance mk_prop add_thm after_qed insts thy = |
73 |
let |
|
74 |
fun after_qed' results = |
|
75 |
ProofContext.theory ((fold o fold) add_thm results #> after_qed); |
|
76 |
in |
|
77 |
thy |
|
78 |
|> ProofContext.init |
|
79 |
|> Proof.theorem_i NONE after_qed' ((map (fn t => [(t, [])]) o maps (mk_prop thy)) insts) |
|
80 |
end; |
|
18168 | 81 |
|
21954 | 82 |
in |
83 |
||
84 |
val axclass_instance_arity = |
|
85 |
gen_instance (Logic.mk_arities oo Sign.cert_arity) AxClass.add_arity; |
|
86 |
val axclass_instance_sort = |
|
87 |
gen_instance (single oo (Logic.mk_classrel oo AxClass.cert_classrel)) |
|
88 |
AxClass.add_classrel I o single; |
|
89 |
||
23087 | 90 |
end; (*local*) |
18575 | 91 |
|
19038 | 92 |
|
21954 | 93 |
(* introducing axclasses with implicit parameter handling *) |
19038 | 94 |
|
22385 | 95 |
fun axclass_params (name, raw_superclasses) raw_consts raw_dep_axioms other_consts thy = |
21924 | 96 |
let |
21954 | 97 |
val superclasses = map (Sign.certify_class thy) raw_superclasses; |
98 |
val consts = (map o apfst o apsnd) (Sign.certify_typ thy) raw_consts; |
|
22997 | 99 |
val prefix = Logic.const_of_class name; |
100 |
fun mk_const_name c = NameSpace.map_base (NameSpace.append prefix) |
|
101 |
(Sign.full_name thy c); |
|
21954 | 102 |
fun add_const ((c, ty), syn) = |
22930 | 103 |
Sign.add_consts_authentic [(c, ty, syn)] |
22997 | 104 |
#> pair (mk_const_name c, ty); |
21954 | 105 |
fun mk_axioms cs thy = |
106 |
raw_dep_axioms thy cs |
|
107 |
|> (map o apsnd o map) (Sign.cert_prop thy) |
|
108 |
|> rpair thy; |
|
109 |
fun add_constraint class (c, ty) = |
|
22997 | 110 |
Sign.add_const_constraint_i (c, SOME |
111 |
(Term.map_type_tfree (fn (v, _) => TFree (v, [class])) ty)); |
|
21924 | 112 |
in |
21954 | 113 |
thy |
22997 | 114 |
|> Theory.add_path prefix |
21954 | 115 |
|> fold_map add_const consts |
22997 | 116 |
||> Theory.restore_naming thy |
21954 | 117 |
|-> (fn cs => mk_axioms cs |
22997 | 118 |
#-> (fn axioms_prop => AxClass.define_class (name, superclasses) |
119 |
(map fst cs @ other_consts) axioms_prop |
|
21954 | 120 |
#-> (fn class => `(fn thy => AxClass.get_definition thy class) |
121 |
#-> (fn {intro, axioms, ...} => fold (add_constraint class) cs |
|
22353 | 122 |
#> pair (class, ((intro, (map Thm.prop_of axioms, axioms)), cs)))))) |
21924 | 123 |
end; |
124 |
||
18168 | 125 |
|
21954 | 126 |
(* instances with implicit parameter handling *) |
19038 | 127 |
|
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
128 |
local |
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
129 |
|
20175 | 130 |
fun gen_read_def thy prep_att read_def ((raw_name, raw_atts), raw_t) = |
19038 | 131 |
let |
19957 | 132 |
val (_, t) = read_def thy (raw_name, raw_t); |
133 |
val ((c, ty), _) = Sign.cert_def (Sign.pp thy) t; |
|
134 |
val atts = map (prep_att thy) raw_atts; |
|
22507 | 135 |
val insts = Consts.typargs (Sign.consts_of thy) (c, ty); |
19957 | 136 |
val name = case raw_name |
20385 | 137 |
of "" => NONE |
138 |
| _ => SOME raw_name; |
|
20175 | 139 |
in (c, (insts, ((name, t), atts))) end; |
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
140 |
|
22507 | 141 |
fun read_def_cmd thy = gen_read_def thy Attrib.intern_src Theory.read_axm; |
21954 | 142 |
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
|
143 |
|
22302 | 144 |
fun gen_instance_arity prep_arity read_def do_proof raw_arities raw_defs theory = |
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
145 |
let |
22331 | 146 |
val arities = map (prep_arity theory) raw_arities; |
20175 | 147 |
val _ = if null arities then error "at least one arity must be given" else (); |
148 |
val _ = case (duplicates (op =) o map #1) arities |
|
149 |
of [] => () |
|
150 |
| dupl_tycos => error ("type constructors occur more than once in arities: " |
|
22423 | 151 |
^ (commas o map quote) dupl_tycos); |
21954 | 152 |
val super_sort = (Graph.all_succs o #classes o Sorts.rep_algebra o Sign.classes_of) theory |
20175 | 153 |
fun get_consts_class tyco ty class = |
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
154 |
let |
21954 | 155 |
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
|
156 |
val subst_ty = map_type_tfree (K ty); |
19957 | 157 |
in |
21894 | 158 |
map (fn (c, ty) => (c, ((tyco, class), subst_ty ty))) cs |
19957 | 159 |
end; |
20175 | 160 |
fun get_consts_sort (tyco, asorts, sort) = |
19957 | 161 |
let |
20192
956cd30ef3be
renamed Name.give_names to Name.names and moved Name.alphanum to Symbol.alphanum
haftmann
parents:
20191
diff
changeset
|
162 |
val ty = Type (tyco, map (fn (v, sort) => TVar ((v, 0), sort)) (Name.names Name.context "'a" asorts)) |
21954 | 163 |
in maps (get_consts_class tyco ty) (super_sort sort) end; |
20175 | 164 |
val cs = maps get_consts_sort arities; |
20385 | 165 |
fun mk_typnorm thy (ty, ty_sc) = |
166 |
case try (Sign.typ_match thy (Logic.varifyT ty_sc, ty)) Vartab.empty |
|
167 |
of SOME env => SOME (Logic.varifyT #> Envir.typ_subst_TVars env #> Logic.unvarifyT) |
|
168 |
| NONE => NONE; |
|
20175 | 169 |
fun read_defs defs cs thy_read = |
170 |
let |
|
19957 | 171 |
fun read raw_def cs = |
172 |
let |
|
20385 | 173 |
val (c, (inst, ((name_opt, t), atts))) = read_def thy_read raw_def; |
174 |
val ty = Consts.instance (Sign.consts_of thy_read) (c, inst); |
|
20175 | 175 |
val ((tyco, class), ty') = case AList.lookup (op =) cs c |
22507 | 176 |
of NONE => error ("illegal definition for constant " ^ quote c) |
19966 | 177 |
| SOME class_ty => class_ty; |
20385 | 178 |
val name = case name_opt |
20628 | 179 |
of NONE => Thm.def_name (Logic.name_arity (tyco, [], c)) |
20385 | 180 |
| SOME name => name; |
181 |
val t' = case mk_typnorm thy_read (ty', ty) |
|
22507 | 182 |
of NONE => error ("illegal definition for constant " ^ |
23135 | 183 |
quote (c ^ "::" ^ setmp show_sorts true |
184 |
(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
|
185 |
| SOME norm => map_types norm t |
20175 | 186 |
in (((class, tyco), ((name, t'), atts)), AList.delete (op =) c cs) end; |
19957 | 187 |
in fold_map read defs cs end; |
22331 | 188 |
val (defs, _) = read_defs raw_defs cs |
189 |
(fold Sign.primitive_arity arities (Theory.copy theory)); |
|
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
190 |
fun get_remove_contraint c thy = |
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
191 |
let |
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
192 |
val ty = Sign.the_const_constraint thy c; |
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
193 |
in |
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
194 |
thy |
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
195 |
|> Sign.add_const_constraint_i (c, NONE) |
20385 | 196 |
|> pair (c, Logic.unvarifyT ty) |
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
197 |
end; |
19966 | 198 |
fun add_defs defs thy = |
199 |
thy |
|
22302 | 200 |
|> PureThy.add_defs_i true (map ((apsnd o map) (Attrib.attribute thy) o snd) defs) |
19966 | 201 |
|-> (fn thms => pair (map fst defs ~~ thms)); |
22737 | 202 |
fun after_qed cs defs thy = |
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
203 |
thy |
22737 | 204 |
|> fold Sign.add_const_constraint_i (map (apsnd SOME) cs) |
205 |
|> fold (CodegenData.add_func false o snd) defs; |
|
19038 | 206 |
in |
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
207 |
theory |
20385 | 208 |
|> fold_map get_remove_contraint (map fst cs |> distinct (op =)) |
19966 | 209 |
||>> add_defs defs |
22737 | 210 |
|-> (fn (cs, defs) => do_proof (after_qed cs defs) arities) |
19038 | 211 |
end; |
212 |
||
22321 | 213 |
fun instance_arity_e' do_proof = gen_instance_arity Sign.read_arity read_def_cmd do_proof; |
22302 | 214 |
fun instance_arity' do_proof = gen_instance_arity Sign.cert_arity read_def do_proof; |
21924 | 215 |
fun tactic_proof tac after_qed arities = |
216 |
fold (fn arity => AxClass.prove_arity arity tac) arities |
|
20175 | 217 |
#> after_qed; |
19110
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 |
in |
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
220 |
|
22321 | 221 |
val instance_arity_cmd = instance_arity_e' axclass_instance_arity; |
21954 | 222 |
val instance_arity = instance_arity' axclass_instance_arity; |
223 |
val prove_instance_arity = instance_arity' o tactic_proof; |
|
224 |
||
23087 | 225 |
end; (*local*) |
21954 | 226 |
|
227 |
||
228 |
||
229 |
(** combining locales and axclasses **) |
|
230 |
||
231 |
(* theory data *) |
|
232 |
||
233 |
datatype class_data = ClassData of { |
|
234 |
locale: string, |
|
235 |
consts: (string * string) list |
|
236 |
(*locale parameter ~> toplevel theory constant*), |
|
23087 | 237 |
v: string option, |
22524 | 238 |
intro: thm, |
239 |
propnames: string list |
|
22302 | 240 |
(*for ad-hoc instance_in*) |
21954 | 241 |
}; |
242 |
||
243 |
fun rep_classdata (ClassData c) = c; |
|
244 |
||
22302 | 245 |
fun merge_pair f1 f2 ((x1, y1), (x2, y2)) = (f1 (x1, x2), f2 (y1, y2)); |
246 |
||
22846 | 247 |
structure ClassData = TheoryDataFun |
248 |
( |
|
249 |
type T = class_data Graph.T * class Symtab.table (*locale name ~> class name*); |
|
250 |
val empty = (Graph.empty, Symtab.empty); |
|
251 |
val copy = I; |
|
252 |
val extend = I; |
|
253 |
fun merge _ = merge_pair (Graph.merge (K true)) (Symtab.merge (K true)); |
|
21954 | 254 |
); |
255 |
||
256 |
||
257 |
(* queries *) |
|
258 |
||
22302 | 259 |
val lookup_class_data = Option.map rep_classdata oo try o Graph.get_node o fst o ClassData.get; |
260 |
fun class_of_locale thy = Symtab.lookup ((snd o ClassData.get) thy); |
|
261 |
||
21954 | 262 |
fun the_class_data thy class = |
263 |
case lookup_class_data thy class |
|
264 |
of NONE => error ("undeclared class " ^ quote class) |
|
265 |
| SOME data => data; |
|
266 |
||
22302 | 267 |
val ancestry = Graph.all_succs o fst o ClassData.get; |
21954 | 268 |
|
22331 | 269 |
fun param_map thy = |
21954 | 270 |
let |
22302 | 271 |
fun params thy class = |
272 |
let |
|
273 |
val const_typs = (#params o AxClass.get_definition thy) class; |
|
274 |
val const_names = (#consts o the_class_data thy) class; |
|
275 |
in |
|
276 |
(map o apsnd) (fn c => (c, (the o AList.lookup (op =) const_typs) c)) const_names |
|
277 |
end; |
|
278 |
in maps (params thy) o ancestry thy end; |
|
21954 | 279 |
|
22524 | 280 |
fun these_intros thy = |
281 |
Graph.fold (fn (_, (data, _)) => insert Thm.eq_thm ((#intro o rep_classdata) data)) |
|
282 |
((fst o ClassData.get) thy) []; |
|
23087 | 283 |
|
21954 | 284 |
val the_propnames = #propnames oo the_class_data; |
285 |
||
286 |
fun print_classes thy = |
|
287 |
let |
|
288 |
val algebra = Sign.classes_of thy; |
|
289 |
val arities = |
|
290 |
Symtab.empty |
|
291 |
|> Symtab.fold (fn (tyco, arities) => fold (fn (class, _) => |
|
292 |
Symtab.map_default (class, []) (insert (op =) tyco)) arities) |
|
293 |
((#arities o Sorts.rep_algebra) algebra); |
|
294 |
val the_arities = these o Symtab.lookup arities; |
|
295 |
fun mk_arity class tyco = |
|
296 |
let |
|
297 |
val Ss = Sorts.mg_domain algebra tyco [class]; |
|
298 |
in Sign.pretty_arity thy (tyco, Ss, [class]) end; |
|
299 |
fun mk_param (c, ty) = Pretty.str (Sign.extern_const thy c ^ " :: " |
|
300 |
^ setmp show_sorts false (Sign.string_of_typ thy o Type.strip_sorts) ty); |
|
301 |
fun mk_entry class = (Pretty.block o Pretty.fbreaks o map_filter I) [ |
|
302 |
(SOME o Pretty.str) ("class " ^ class ^ ":"), |
|
303 |
(SOME o Pretty.block) [Pretty.str "supersort: ", |
|
304 |
(Sign.pretty_sort thy o Sign.certify_sort thy o Sign.super_classes thy) class], |
|
305 |
Option.map (Pretty.str o prefix "locale: " o #locale) (lookup_class_data thy class), |
|
306 |
((fn [] => NONE | ps => (SOME o Pretty.block o Pretty.fbreaks) (Pretty.str "parameters:" :: ps)) o map mk_param |
|
307 |
o these o Option.map #params o try (AxClass.get_definition thy)) class, |
|
308 |
(SOME o Pretty.block o Pretty.breaks) [ |
|
309 |
Pretty.str "instances:", |
|
310 |
Pretty.list "" "" (map (mk_arity class) (the_arities class)) |
|
311 |
] |
|
312 |
] |
|
313 |
in |
|
314 |
(Pretty.writeln o Pretty.chunks o separate (Pretty.str "") o map mk_entry o Sorts.all_classes) |
|
315 |
algebra |
|
316 |
end; |
|
317 |
||
318 |
||
319 |
(* updaters *) |
|
320 |
||
23087 | 321 |
fun add_class_data ((class, superclasses), (locale, consts, v, intro, propnames)) = |
22302 | 322 |
ClassData.map (fn (gr, tab) => ( |
323 |
gr |
|
324 |
|> Graph.new_node (class, ClassData { |
|
21954 | 325 |
locale = locale, |
326 |
consts = consts, |
|
23087 | 327 |
v = v, |
22524 | 328 |
intro = intro, |
23087 | 329 |
propnames = propnames} |
330 |
) |
|
22302 | 331 |
|> fold (curry Graph.add_edge class) superclasses, |
332 |
tab |
|
333 |
|> Symtab.update (locale, class) |
|
334 |
)); |
|
335 |
||
21954 | 336 |
|
337 |
(* tactics and methods *) |
|
338 |
||
22524 | 339 |
fun intro_classes_tac facts st = |
340 |
let |
|
341 |
val thy = Thm.theory_of_thm st; |
|
342 |
val classes = Sign.all_classes thy; |
|
343 |
val class_trivs = map (Thm.class_triv thy) classes; |
|
344 |
val class_intros = these_intros thy; |
|
345 |
fun add_axclass_intro class = |
|
346 |
case try (AxClass.get_definition thy) class of SOME {intro, ...} => cons intro | _ => I; |
|
347 |
val axclass_intros = fold add_axclass_intro classes []; |
|
348 |
in |
|
349 |
st |
|
350 |
|> ((ALLGOALS (Method.insert_tac facts THEN' |
|
351 |
REPEAT_ALL_NEW (resolve_tac (class_trivs @ class_intros @ axclass_intros)))) |
|
352 |
THEN Tactic.distinct_subgoals_tac) |
|
353 |
end; |
|
354 |
||
21954 | 355 |
fun default_intro_classes_tac [] = intro_classes_tac [] |
356 |
| default_intro_classes_tac _ = Tactical.no_tac; (*no error message!*) |
|
357 |
||
358 |
fun default_tac rules ctxt facts = |
|
359 |
HEADGOAL (Method.some_rule_tac rules ctxt facts) ORELSE |
|
360 |
default_intro_classes_tac facts; |
|
361 |
||
362 |
val _ = Context.add_setup (Method.add_methods |
|
22737 | 363 |
[("intro_classes", Method.no_args (Method.METHOD intro_classes_tac), |
21954 | 364 |
"back-chain introduction rules of classes"), |
365 |
("default", Method.thms_ctxt_args (Method.METHOD oo default_tac), |
|
366 |
"apply some intro/elim rule")]); |
|
367 |
||
22423 | 368 |
|
22321 | 369 |
(* tactical interfaces to locale commands *) |
21954 | 370 |
|
22321 | 371 |
fun prove_interpretation tac prfx_atts expr insts thy = |
372 |
thy |
|
23184 | 373 |
|> Locale.interpretation_i I prfx_atts expr insts |
23351 | 374 |
|> Proof.global_terminal_proof (Method.Basic (K (Method.SIMPLE_METHOD tac), Position.none), NONE) |
22321 | 375 |
|> ProofContext.theory_of; |
21954 | 376 |
|
22069 | 377 |
fun prove_interpretation_in tac after_qed (name, expr) thy = |
378 |
thy |
|
379 |
|> Locale.interpretation_in_locale (ProofContext.theory after_qed) (name, expr) |
|
23351 | 380 |
|> Proof.global_terminal_proof (Method.Basic (K (Method.SIMPLE_METHOD tac), Position.none), NONE) |
22069 | 381 |
|> ProofContext.theory_of; |
382 |
||
383 |
fun instance_sort' do_proof (class, sort) theory = |
|
384 |
let |
|
385 |
val loc_name = (#locale o the_class_data theory) class; |
|
386 |
val loc_expr = |
|
387 |
(Locale.Merge o map (Locale.Locale o #locale o the_class_data theory)) sort; |
|
388 |
val const_names = (map (NameSpace.base o snd) |
|
389 |
o maps (#consts o the_class_data theory) |
|
22302 | 390 |
o ancestry theory) [class]; |
22069 | 391 |
fun get_thms thy = |
22302 | 392 |
ancestry thy sort |
22069 | 393 |
|> AList.make (the_propnames thy) |
394 |
|> map (apsnd (map (NameSpace.append (Logic.const_of_class loc_name)))) |
|
395 |
|> map_filter (fn (superclass, thm_names) => |
|
396 |
case map_filter (try (PureThy.get_thm thy o Name)) thm_names |
|
397 |
of [] => NONE |
|
398 |
| thms => SOME (superclass, thms)); |
|
399 |
fun after_qed thy = |
|
400 |
thy |
|
401 |
|> `get_thms |
|
402 |
|-> fold (fn (supclass, thms) => I |
|
403 |
AxClass.prove_classrel (class, supclass) |
|
404 |
(ALLGOALS (K (intro_classes_tac [])) THEN |
|
405 |
(ALLGOALS o ProofContext.fact_tac) thms)) |
|
406 |
in |
|
407 |
theory |
|
408 |
|> do_proof after_qed (loc_name, loc_expr) |
|
409 |
end; |
|
410 |
||
22074 | 411 |
val prove_instance_sort = instance_sort' o prove_interpretation_in; |
412 |
||
22069 | 413 |
|
22524 | 414 |
(* constructing class introduction rules from axclass and locale rules *) |
22069 | 415 |
|
22524 | 416 |
fun class_intro thy locale class sups = |
417 |
let |
|
418 |
fun OF_LAST thm1 thm2 = |
|
419 |
let |
|
420 |
val n = (length o Logic.strip_imp_prems o prop_of) thm2; |
|
421 |
in (thm1 RSN (n, thm2)) end; |
|
422 |
fun prem_inclass t = |
|
423 |
case Logic.strip_imp_prems t |
|
424 |
of ofcls :: _ => try Logic.dest_inclass ofcls |
|
425 |
| [] => NONE; |
|
426 |
val typ = TVar ((AxClass.param_tyvarname, 0), Sign.super_classes thy class); |
|
427 |
fun strip_ofclass class thm = |
|
428 |
thm OF (fst o AxClass.of_sort thy (typ, [class])) AxClass.cache; |
|
429 |
fun strip_all_ofclass thm = |
|
430 |
case (prem_inclass o Thm.prop_of) thm |
|
431 |
of SOME (_, class) => thm |> strip_ofclass class |> strip_all_ofclass |
|
432 |
| NONE => thm; |
|
433 |
fun class_elim class = |
|
434 |
case (map Drule.unconstrainTs o #axioms o AxClass.get_definition thy) class |
|
435 |
of [thm] => SOME thm |
|
436 |
| [] => NONE; |
|
437 |
val pred_intro = case Locale.intros thy locale |
|
438 |
of ([ax_intro], [intro]) => intro |> OF_LAST ax_intro |> SOME |
|
439 |
| ([intro], []) => SOME intro |
|
440 |
| ([], [intro]) => SOME intro |
|
441 |
| _ => NONE; |
|
442 |
val pred_intro' = pred_intro |
|
443 |
|> Option.map (fn intro => intro OF map_filter class_elim sups); |
|
444 |
val class_intro = (#intro o AxClass.get_definition thy) class; |
|
445 |
val raw_intro = case pred_intro' |
|
446 |
of SOME pred_intro => class_intro |> OF_LAST pred_intro |
|
447 |
| NONE => class_intro; |
|
448 |
in |
|
449 |
raw_intro |
|
450 |
|> Drule.instantiate' [SOME (Thm.ctyp_of thy typ)] [] |
|
451 |
|> strip_all_ofclass |
|
452 |
|> Thm.strip_shyps |
|
453 |
|> Drule.unconstrainTs |
|
454 |
end; |
|
455 |
||
456 |
||
457 |
(* classes and instances *) |
|
22385 | 458 |
|
23184 | 459 |
fun mk_instT class = Symtab.empty |
460 |
|> Symtab.update (AxClass.param_tyvarname, TFree (AxClass.param_tyvarname, [class])); |
|
461 |
||
462 |
fun mk_inst class param_names cs = |
|
463 |
Symtab.empty |
|
464 |
|> fold2 (fn v => fn (c, ty) => Symtab.update (v, Const |
|
465 |
(c, Term.map_type_tfree (fn (v, _) => TFree (v, [class])) ty))) param_names cs; |
|
466 |
||
22069 | 467 |
local |
468 |
||
22737 | 469 |
fun read_param thy raw_t = |
470 |
let |
|
471 |
val t = Sign.read_term thy raw_t |
|
472 |
in case try dest_Const t |
|
473 |
of SOME (c, _) => c |
|
474 |
| NONE => error ("Not a constant: " ^ Sign.string_of_term thy t) |
|
475 |
end; |
|
476 |
||
22473 | 477 |
fun gen_class add_locale prep_class prep_param bname |
478 |
raw_supclasses raw_elems raw_other_consts thy = |
|
21954 | 479 |
let |
22321 | 480 |
(*FIXME need proper concept for reading locale statements*) |
481 |
fun subst_classtyvar (_, _) = |
|
482 |
TFree (AxClass.param_tyvarname, []) |
|
483 |
| subst_classtyvar (v, sort) = |
|
484 |
error ("Sort constraint illegal in type class, for type variable " ^ v ^ "::" ^ Sign.string_of_sort thy sort); |
|
485 |
(*val subst_classtyvars = Element.map_ctxt {name = I, var = I, term = I, |
|
486 |
typ = Term.map_type_tfree subst_classtyvar, fact = I, attrib = I};*) |
|
22385 | 487 |
val other_consts = map (prep_param thy) raw_other_consts; |
22423 | 488 |
val (elems, includes) = fold_rev (fn Locale.Elem e => apfst (cons e) |
489 |
| Locale.Expr i => apsnd (cons i)) raw_elems ([], []); |
|
21954 | 490 |
val supclasses = map (prep_class thy) raw_supclasses; |
22524 | 491 |
val sups = filter (is_some o lookup_class_data thy) supclasses |
492 |
|> Sign.certify_sort thy; |
|
22473 | 493 |
val supsort = Sign.certify_sort thy supclasses; |
494 |
val suplocales = map (Locale.Locale o #locale o the_class_data thy) sups; |
|
22423 | 495 |
val supexpr = Locale.Merge (suplocales @ includes); |
496 |
val supparams = (map fst o Locale.parameters_of_expr thy) |
|
497 |
(Locale.Merge suplocales); |
|
22473 | 498 |
val supconsts = AList.make (the o AList.lookup (op =) (param_map thy sups)) |
499 |
(map fst supparams); |
|
22321 | 500 |
(*val elems_constrains = map |
501 |
(Element.Constrains o apsnd (Term.map_type_tfree subst_classtyvar)) supparams;*) |
|
22473 | 502 |
fun mk_tyvar (_, sort) = TFree (AxClass.param_tyvarname, |
503 |
if Sign.subsort thy (supsort, sort) then sort else error |
|
504 |
("Sort " ^ Sign.string_of_sort thy sort |
|
505 |
^ " is less general than permitted least general sort " |
|
506 |
^ Sign.string_of_sort thy supsort)); |
|
22321 | 507 |
fun extract_params thy name_locale = |
22473 | 508 |
let |
22321 | 509 |
val params = Locale.parameters_of thy name_locale; |
23087 | 510 |
val v = case (maps typ_tfrees o map (snd o fst)) params |
511 |
of (v, _) :: _ => SOME v |
|
512 |
| _ => NONE; |
|
22321 | 513 |
in |
23087 | 514 |
(v, (map (fst o fst) params, params |
22473 | 515 |
|> (map o apfst o apsnd o Term.map_type_tfree) mk_tyvar |
22353 | 516 |
|> (map o apsnd) (fork_mixfix true NONE #> fst) |
22321 | 517 |
|> chop (length supconsts) |
23087 | 518 |
|> snd)) |
21954 | 519 |
end; |
520 |
fun extract_assumes name_locale params thy cs = |
|
521 |
let |
|
522 |
val consts = supconsts @ (map (fst o fst) params ~~ cs); |
|
523 |
fun subst (Free (c, ty)) = |
|
22048 | 524 |
Const ((fst o the o AList.lookup (op =) consts) c, ty) |
525 |
| subst t = t; |
|
21954 | 526 |
fun prep_asm ((name, atts), ts) = |
22423 | 527 |
((NameSpace.base name, map (Attrib.attribute thy) atts), |
528 |
(map o map_aterms) subst ts); |
|
21954 | 529 |
in |
22182 | 530 |
Locale.global_asms_of thy name_locale |
21954 | 531 |
|> map prep_asm |
532 |
end; |
|
22048 | 533 |
fun extract_axiom_names thy name_locale = |
534 |
name_locale |
|
22182 | 535 |
|> Locale.global_asms_of thy |
22524 | 536 |
|> map (NameSpace.base o fst o fst) (*FIXME - is finally dropped*); |
537 |
fun note_intro name_axclass class_intro = |
|
538 |
PureThy.note_thmss_qualified "" ((Logic.const_of_class o NameSpace.base) name_axclass) |
|
539 |
[(("intro", []), [([class_intro], [])])] |
|
540 |
#> snd |
|
21954 | 541 |
in |
542 |
thy |
|
23018
1d29bc31b0cb
no special treatment in naming of locale predicates stemming form classes
haftmann
parents:
22997
diff
changeset
|
543 |
|> add_locale (SOME "") bname supexpr ((*elems_constrains @*) elems) |
21954 | 544 |
|-> (fn name_locale => ProofContext.theory_result ( |
22321 | 545 |
`(fn thy => extract_params thy name_locale) |
23087 | 546 |
#-> (fn (v, (param_names, params)) => |
22385 | 547 |
axclass_params (bname, supsort) params (extract_assumes name_locale params) other_consts |
22302 | 548 |
#-> (fn (name_axclass, ((_, (ax_terms, ax_axioms)), consts)) => |
22524 | 549 |
`(fn thy => class_intro thy name_locale name_axclass sups) |
550 |
##>> `(fn thy => extract_axiom_names thy name_locale) |
|
551 |
#-> (fn (class_intro, axiom_names) => |
|
22473 | 552 |
add_class_data ((name_axclass, sups), |
23087 | 553 |
(name_locale, map (fst o fst) params ~~ map fst consts, v, |
554 |
class_intro, axiom_names)) |
|
23184 | 555 |
(*FIXME: class_data should already contain data relevant |
556 |
for interpretation; use this later for class target*) |
|
557 |
(*FIXME: general export_fixes which may be parametrized |
|
558 |
with pieces of an emerging class*) |
|
22524 | 559 |
#> note_intro name_axclass class_intro |
22321 | 560 |
#> prove_interpretation ((ALLGOALS o ProofContext.fact_tac) ax_axioms) |
22524 | 561 |
((false, Logic.const_of_class bname), []) (Locale.Locale name_locale) |
23184 | 562 |
((mk_instT name_axclass, mk_inst name_axclass param_names (map snd supconsts @ consts)), []) |
21954 | 563 |
#> pair name_axclass |
22048 | 564 |
))))) |
21954 | 565 |
end; |
566 |
||
567 |
in |
|
568 |
||
22737 | 569 |
val class_cmd = gen_class Locale.add_locale Sign.intern_class read_param; |
22473 | 570 |
val class = gen_class Locale.add_locale_i Sign.certify_class (K I); |
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
571 |
|
20455 | 572 |
end; (*local*) |
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
573 |
|
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
574 |
local |
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
575 |
|
21954 | 576 |
fun gen_instance_sort prep_class prep_sort (raw_class, raw_sort) theory = |
577 |
let |
|
578 |
val class = prep_class theory raw_class; |
|
579 |
val sort = prep_sort theory raw_sort; |
|
22321 | 580 |
in |
21954 | 581 |
theory |
582 |
|> instance_sort' (Locale.interpretation_in_locale o ProofContext.theory) (class, sort) |
|
22321 | 583 |
end; |
584 |
||
585 |
fun gen_instance_class prep_class (raw_class, raw_superclass) theory = |
|
586 |
let |
|
587 |
val class = prep_class theory raw_class; |
|
588 |
val superclass = prep_class theory raw_superclass; |
|
589 |
in |
|
590 |
theory |
|
591 |
|> axclass_instance_sort (class, superclass) |
|
21954 | 592 |
end; |
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
593 |
|
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
594 |
in |
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
595 |
|
22321 | 596 |
val instance_sort_cmd = gen_instance_sort Sign.read_class Sign.read_sort; |
21954 | 597 |
val instance_sort = gen_instance_sort Sign.certify_class Sign.certify_sort; |
22321 | 598 |
val instance_class_cmd = gen_instance_class Sign.read_class; |
599 |
val instance_class = gen_instance_class Sign.certify_class; |
|
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
19102
diff
changeset
|
600 |
|
23087 | 601 |
end; (*local*) |
19038 | 602 |
|
19957 | 603 |
|
23087 | 604 |
(** class target **) |
21954 | 605 |
|
23184 | 606 |
val INTERPRET_DEF = ref false; |
607 |
||
23087 | 608 |
fun export_fixes thy class = |
21954 | 609 |
let |
23087 | 610 |
val v = (#v o the_class_data thy) class; |
611 |
val constrain_sort = curry (Sorts.inter_sort (Sign.classes_of thy)) [class]; |
|
612 |
val subst_typ = Term.map_type_tfree (fn var as (w, sort) => |
|
613 |
if SOME w = v then TFree (w, constrain_sort sort) else TFree var); |
|
614 |
val consts = param_map thy [class]; |
|
615 |
fun subst_aterm (t as Free (v, ty)) = (case AList.lookup (op =) consts v |
|
616 |
of SOME (c, _) => Const (c, ty) |
|
617 |
| NONE => t) |
|
618 |
| subst_aterm t = t; |
|
619 |
in map_types subst_typ #> Term.map_aterms subst_aterm end; |
|
22353 | 620 |
|
23087 | 621 |
fun add_const_in_class class ((c, rhs), syn) thy = |
22353 | 622 |
let |
22423 | 623 |
val prfx = (Logic.const_of_class o NameSpace.base) class; |
23087 | 624 |
fun mk_name inject c = |
22423 | 625 |
let |
626 |
val n1 = Sign.full_name thy c; |
|
627 |
val n2 = NameSpace.qualifier n1; |
|
628 |
val n3 = NameSpace.base n1; |
|
23087 | 629 |
in NameSpace.implode (n2 :: inject @ [n3]) end; |
630 |
val abbr' = mk_name [prfx, prfx] c; |
|
631 |
val rhs' = export_fixes thy class rhs; |
|
632 |
val ty' = Term.fastype_of rhs'; |
|
633 |
val def = (c, Logic.mk_equals (Const (mk_name [prfx] c, ty'), rhs')); |
|
634 |
val (syn', _) = fork_mixfix true NONE syn; |
|
23184 | 635 |
fun interpret def = |
636 |
let |
|
637 |
val def' = symmetric def |
|
638 |
val tac = (ALLGOALS o ProofContext.fact_tac) [def']; |
|
639 |
val name_locale = (#locale o the_class_data thy) class; |
|
640 |
val def_eq = (Logic.dest_equals o Thm.prop_of) def'; |
|
641 |
val (params, consts) = split_list (param_map thy [class]); |
|
642 |
in |
|
643 |
prove_interpretation tac ((false, prfx), []) (Locale.Locale name_locale) |
|
644 |
((mk_instT class, mk_inst class params consts), [def_eq]) |
|
645 |
end; |
|
21954 | 646 |
in |
22302 | 647 |
thy |
23087 | 648 |
|> Sign.hide_consts_i true [abbr'] |
649 |
|> Sign.add_path prfx |
|
650 |
|> Sign.add_consts_authentic [(c, ty', syn')] |
|
651 |
|> Sign.parent_path |
|
652 |
|> Sign.sticky_prefix prfx |
|
653 |
|> PureThy.add_defs_i false [(def, [])] |
|
23184 | 654 |
|-> (fn [def] => ! INTERPRET_DEF ? interpret def) |
23087 | 655 |
|> Sign.restore_naming thy |
21954 | 656 |
end; |
657 |
||
20455 | 658 |
end; |