author | wenzelm |
Sat, 30 Dec 2006 16:08:06 +0100 | |
changeset 21966 | edab0ecfbd7c |
parent 21953 | ab834c5c3858 |
child 22385 | cc2be3315e72 |
permissions | -rw-r--r-- |
404
dd3d3d6467db
axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff
changeset
|
1 |
(* Title: Pure/axclass.ML |
dd3d3d6467db
axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff
changeset
|
2 |
ID: $Id$ |
dd3d3d6467db
axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff
changeset
|
3 |
Author: Markus Wenzel, TU Muenchen |
dd3d3d6467db
axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff
changeset
|
4 |
|
19511 | 5 |
Type classes as parameter records and predicates, with explicit |
6 |
definitions and proofs. |
|
404
dd3d3d6467db
axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff
changeset
|
7 |
*) |
dd3d3d6467db
axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff
changeset
|
8 |
|
dd3d3d6467db
axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff
changeset
|
9 |
signature AX_CLASS = |
3938 | 10 |
sig |
21463
42dd50268c8b
completed class parameter handling in axclass.ML
haftmann
parents:
20628
diff
changeset
|
11 |
val get_definition: theory -> class -> {def: thm, intro: thm, axioms: thm list, |
21925 | 12 |
params: (string * typ) list} |
19243
5dcb899a8486
moved read_class, read/cert_classrel/arity to sign.ML;
wenzelm
parents:
19134
diff
changeset
|
13 |
val class_intros: theory -> thm list |
20107 | 14 |
val class_of_param: theory -> string -> class option |
21463
42dd50268c8b
completed class parameter handling in axclass.ML
haftmann
parents:
20628
diff
changeset
|
15 |
val params_of_class: theory -> class -> string * (string * typ) list |
21953 | 16 |
val param_tyvarname: string |
19511 | 17 |
val print_axclasses: theory -> unit |
19405 | 18 |
val cert_classrel: theory -> class * class -> class * class |
19 |
val read_classrel: theory -> xstring * xstring -> class * class |
|
20 |
val add_classrel: thm -> theory -> theory |
|
21 |
val add_arity: thm -> theory -> theory |
|
22 |
val prove_classrel: class * class -> tactic -> theory -> theory |
|
19243
5dcb899a8486
moved read_class, read/cert_classrel/arity to sign.ML;
wenzelm
parents:
19134
diff
changeset
|
23 |
val prove_arity: string * sort list * sort -> tactic -> theory -> theory |
19511 | 24 |
val define_class: bstring * xstring list -> string list -> |
19418
03b01c9314fc
reworded add_axclass(_i): canonical specification format,
wenzelm
parents:
19405
diff
changeset
|
25 |
((bstring * Attrib.src list) * string list) list -> theory -> class * theory |
19511 | 26 |
val define_class_i: bstring * class list -> string list -> |
19418
03b01c9314fc
reworded add_axclass(_i): canonical specification format,
wenzelm
parents:
19405
diff
changeset
|
27 |
((bstring * attribute list) * term list) list -> theory -> class * theory |
19511 | 28 |
val axiomatize_class: bstring * xstring list -> theory -> theory |
29 |
val axiomatize_class_i: bstring * class list -> theory -> theory |
|
30 |
val axiomatize_classrel: (xstring * xstring) list -> theory -> theory |
|
31 |
val axiomatize_classrel_i: (class * class) list -> theory -> theory |
|
32 |
val axiomatize_arity: xstring * string list * string -> theory -> theory |
|
33 |
val axiomatize_arity_i: arity -> theory -> theory |
|
19574 | 34 |
type cache |
35 |
val cache: cache |
|
36 |
val of_sort: theory -> typ * sort -> cache -> thm list * cache (*exception Sorts.CLASS_ERROR*) |
|
3938 | 37 |
end; |
404
dd3d3d6467db
axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff
changeset
|
38 |
|
15801 | 39 |
structure AxClass: AX_CLASS = |
404
dd3d3d6467db
axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff
changeset
|
40 |
struct |
dd3d3d6467db
axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff
changeset
|
41 |
|
19405 | 42 |
(** theory data **) |
423 | 43 |
|
19405 | 44 |
(* class parameters (canonical order) *) |
423 | 45 |
|
19405 | 46 |
type param = string * class; |
423 | 47 |
|
19405 | 48 |
fun add_param pp ((x, c): param) params = |
49 |
(case AList.lookup (op =) params x of |
|
50 |
NONE => (x, c) :: params |
|
51 |
| SOME c' => error ("Duplicate class parameter " ^ quote x ^ |
|
52 |
" for " ^ Pretty.string_of_sort pp [c] ^ |
|
53 |
(if c = c' then "" else " and " ^ Pretty.string_of_sort pp [c']))); |
|
423 | 54 |
|
19405 | 55 |
fun merge_params _ ([], qs) = qs |
56 |
| merge_params pp (ps, qs) = |
|
57 |
fold_rev (fn q => if member (op =) ps q then I else add_param pp q) qs ps; |
|
423 | 58 |
|
59 |
||
19511 | 60 |
(* axclasses *) |
6379 | 61 |
|
19243
5dcb899a8486
moved read_class, read/cert_classrel/arity to sign.ML;
wenzelm
parents:
19134
diff
changeset
|
62 |
val introN = "intro"; |
19511 | 63 |
val superN = "super"; |
19243
5dcb899a8486
moved read_class, read/cert_classrel/arity to sign.ML;
wenzelm
parents:
19134
diff
changeset
|
64 |
val axiomsN = "axioms"; |
5dcb899a8486
moved read_class, read/cert_classrel/arity to sign.ML;
wenzelm
parents:
19134
diff
changeset
|
65 |
|
21463
42dd50268c8b
completed class parameter handling in axclass.ML
haftmann
parents:
20628
diff
changeset
|
66 |
val param_tyvarname = "'a"; |
42dd50268c8b
completed class parameter handling in axclass.ML
haftmann
parents:
20628
diff
changeset
|
67 |
|
19392 | 68 |
datatype axclass = AxClass of |
19460 | 69 |
{def: thm, |
19392 | 70 |
intro: thm, |
21463
42dd50268c8b
completed class parameter handling in axclass.ML
haftmann
parents:
20628
diff
changeset
|
71 |
axioms: thm list, |
21925 | 72 |
params: (string * typ) list}; |
19392 | 73 |
|
19460 | 74 |
type axclasses = axclass Symtab.table * param list; |
19392 | 75 |
|
21925 | 76 |
fun make_axclass ((def, intro, axioms), params) = |
77 |
AxClass {def = def, intro = intro, axioms = axioms, params = params}; |
|
19405 | 78 |
|
79 |
fun merge_axclasses pp ((tab1, params1), (tab2, params2)) : axclasses = |
|
80 |
(Symtab.merge (K true) (tab1, tab2), merge_params pp (params1, params2)); |
|
81 |
||
19392 | 82 |
|
83 |
(* instances *) |
|
84 |
||
20628 | 85 |
val classrel_prefix = "classrel_"; |
86 |
val arity_prefix = "arity_"; |
|
19511 | 87 |
|
19574 | 88 |
type instances = |
89 |
((class * class) * thm) list * |
|
90 |
((class * sort list) * thm) list Symtab.table; |
|
6379 | 91 |
|
19574 | 92 |
fun merge_instances ((classrel1, arities1): instances, (classrel2, arities2)) = |
93 |
(merge (eq_fst op =) (classrel1, classrel2), |
|
94 |
Symtab.join (K (merge (eq_fst op =))) (arities1, arities2)); |
|
19392 | 95 |
|
96 |
||
19511 | 97 |
(* setup data *) |
19392 | 98 |
|
99 |
structure AxClassData = TheoryDataFun |
|
16458 | 100 |
(struct |
19392 | 101 |
val name = "Pure/axclass"; |
19574 | 102 |
type T = axclasses * instances; |
103 |
val empty : T = ((Symtab.empty, []), ([], Symtab.empty)); |
|
104 |
val copy = I; |
|
105 |
val extend = I; |
|
106 |
fun merge pp ((axclasses1, instances1), (axclasses2, instances2)) = |
|
107 |
(merge_axclasses pp (axclasses1, axclasses2), (merge_instances (instances1, instances2))); |
|
19460 | 108 |
fun print _ _ = (); |
16458 | 109 |
end); |
6379 | 110 |
|
19392 | 111 |
val _ = Context.add_setup AxClassData.init; |
6379 | 112 |
|
113 |
||
19574 | 114 |
(* maintain axclasses *) |
19392 | 115 |
|
19574 | 116 |
val get_axclasses = #1 o AxClassData.get; |
117 |
fun map_axclasses f = AxClassData.map (apfst f); |
|
118 |
||
119 |
val lookup_def = Symtab.lookup o #1 o get_axclasses; |
|
6379 | 120 |
|
19511 | 121 |
fun get_definition thy c = |
122 |
(case lookup_def thy c of |
|
19392 | 123 |
SOME (AxClass info) => info |
21919 | 124 |
| NONE => error ("No such axclass: " ^ quote c)); |
6379 | 125 |
|
19123 | 126 |
fun class_intros thy = |
19392 | 127 |
let |
128 |
fun add_intro c = |
|
19511 | 129 |
(case lookup_def thy c of SOME (AxClass {intro, ...}) => cons intro | _ => I); |
21931
314f9e2a442c
replaced Sign.classes by Sign.all_classes (topologically sorted);
wenzelm
parents:
21925
diff
changeset
|
130 |
val classes = Sign.all_classes thy; |
19392 | 131 |
in map (Thm.class_triv thy) classes @ fold add_intro classes [] end; |
15876
a67343c6ab2a
sane interfaces for tactical instance proofs (do not expand defs of theory, proper handling of sort instances);
wenzelm
parents:
15853
diff
changeset
|
132 |
|
a67343c6ab2a
sane interfaces for tactical instance proofs (do not expand defs of theory, proper handling of sort instances);
wenzelm
parents:
15853
diff
changeset
|
133 |
|
19460 | 134 |
fun get_params thy pred = |
19574 | 135 |
let val params = #2 (get_axclasses thy); |
19460 | 136 |
in fold (fn (x, c) => if pred c then cons x else I) params [] end; |
137 |
||
138 |
fun params_of thy c = get_params thy (fn c' => c' = c); |
|
139 |
fun all_params_of thy S = get_params thy (fn c => Sign.subsort thy (S, [c])); |
|
140 |
||
20107 | 141 |
fun class_of_param thy = |
19955 | 142 |
AList.lookup (op =) (#2 (get_axclasses thy)); |
143 |
||
21463
42dd50268c8b
completed class parameter handling in axclass.ML
haftmann
parents:
20628
diff
changeset
|
144 |
fun params_of_class thy class = |
42dd50268c8b
completed class parameter handling in axclass.ML
haftmann
parents:
20628
diff
changeset
|
145 |
(param_tyvarname, (#params o get_definition thy) class); |
19460 | 146 |
|
21919 | 147 |
|
19511 | 148 |
(* maintain instances *) |
19503 | 149 |
|
19574 | 150 |
val get_instances = #2 o AxClassData.get; |
151 |
fun map_instances f = AxClassData.map (apsnd f); |
|
19528 | 152 |
|
153 |
||
19574 | 154 |
fun the_classrel thy (c1, c2) = |
155 |
(case AList.lookup (op =) (#1 (get_instances thy)) (c1, c2) of |
|
156 |
SOME th => Thm.transfer thy th |
|
157 |
| NONE => error ("Unproven class relation " ^ Sign.string_of_classrel thy [c1, c2])); |
|
158 |
||
159 |
fun put_classrel arg = map_instances (fn (classrel, arities) => |
|
160 |
(insert (eq_fst op =) arg classrel, arities)); |
|
19503 | 161 |
|
162 |
||
19574 | 163 |
fun the_arity thy a (c, Ss) = |
164 |
(case AList.lookup (op =) (Symtab.lookup_list (#2 (get_instances thy)) a) (c, Ss) of |
|
165 |
SOME th => Thm.transfer thy th |
|
166 |
| NONE => error ("Unproven type arity " ^ Sign.string_of_arity thy (a, Ss, [c]))); |
|
19503 | 167 |
|
19574 | 168 |
fun put_arity ((t, Ss, c), th) = map_instances (fn (classrel, arities) => |
169 |
(classrel, arities |> Symtab.insert_list (eq_fst op =) (t, ((c, Ss), th)))); |
|
19503 | 170 |
|
171 |
||
19511 | 172 |
(* print data *) |
19460 | 173 |
|
174 |
fun print_axclasses thy = |
|
175 |
let |
|
19574 | 176 |
val axclasses = #1 (get_axclasses thy); |
19460 | 177 |
val ctxt = ProofContext.init thy; |
178 |
||
21925 | 179 |
fun pretty_axclass (class, AxClass {def, intro, axioms, params}) = |
19460 | 180 |
Pretty.block (Pretty.fbreaks |
181 |
[Pretty.block |
|
19511 | 182 |
[Pretty.str "class ", ProofContext.pretty_sort ctxt [class], Pretty.str ":"], |
19460 | 183 |
Pretty.strs ("parameters:" :: params_of thy class), |
184 |
ProofContext.pretty_fact ctxt ("def", [def]), |
|
185 |
ProofContext.pretty_fact ctxt (introN, [intro]), |
|
186 |
ProofContext.pretty_fact ctxt (axiomsN, axioms)]); |
|
187 |
in Pretty.writeln (Pretty.chunks (map pretty_axclass (Symtab.dest axclasses))) end; |
|
188 |
||
189 |
||
404
dd3d3d6467db
axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff
changeset
|
190 |
|
19511 | 191 |
(** instances **) |
19418
03b01c9314fc
reworded add_axclass(_i): canonical specification format,
wenzelm
parents:
19405
diff
changeset
|
192 |
|
03b01c9314fc
reworded add_axclass(_i): canonical specification format,
wenzelm
parents:
19405
diff
changeset
|
193 |
(* class relations *) |
03b01c9314fc
reworded add_axclass(_i): canonical specification format,
wenzelm
parents:
19405
diff
changeset
|
194 |
|
19405 | 195 |
fun cert_classrel thy raw_rel = |
15876
a67343c6ab2a
sane interfaces for tactical instance proofs (do not expand defs of theory, proper handling of sort instances);
wenzelm
parents:
15853
diff
changeset
|
196 |
let |
19405 | 197 |
val (c1, c2) = pairself (Sign.certify_class thy) raw_rel; |
19511 | 198 |
val _ = Type.add_classrel (Sign.pp thy) (c1, c2) (Sign.tsig_of thy); |
19405 | 199 |
val _ = |
19460 | 200 |
(case subtract (op =) (all_params_of thy [c1]) (all_params_of thy [c2]) of |
19405 | 201 |
[] => () |
202 |
| xs => raise TYPE ("Class " ^ Sign.string_of_sort thy [c1] ^ " lacks parameter(s) " ^ |
|
203 |
commas_quote xs ^ " of " ^ Sign.string_of_sort thy [c2], [], [])); |
|
204 |
in (c1, c2) end; |
|
205 |
||
206 |
fun read_classrel thy raw_rel = |
|
207 |
cert_classrel thy (pairself (Sign.read_class thy) raw_rel) |
|
208 |
handle TYPE (msg, _, _) => error msg; |
|
209 |
||
210 |
||
211 |
(* primitive rules *) |
|
212 |
||
213 |
fun add_classrel th thy = |
|
214 |
let |
|
215 |
fun err () = raise THM ("add_classrel: malformed class relation", 0, [th]); |
|
216 |
val prop = Drule.plain_prop_of (Thm.transfer thy th); |
|
217 |
val rel = Logic.dest_classrel prop handle TERM _ => err (); |
|
218 |
val (c1, c2) = cert_classrel thy rel handle TYPE _ => err (); |
|
19574 | 219 |
in |
220 |
thy |
|
221 |
|> Sign.primitive_classrel (c1, c2) |
|
222 |
|> put_classrel ((c1, c2), Drule.unconstrainTs th) |
|
223 |
end; |
|
15876
a67343c6ab2a
sane interfaces for tactical instance proofs (do not expand defs of theory, proper handling of sort instances);
wenzelm
parents:
15853
diff
changeset
|
224 |
|
19405 | 225 |
fun add_arity th thy = |
15876
a67343c6ab2a
sane interfaces for tactical instance proofs (do not expand defs of theory, proper handling of sort instances);
wenzelm
parents:
15853
diff
changeset
|
226 |
let |
19528 | 227 |
fun err () = raise THM ("add_arity: malformed type arity", 0, [th]); |
19405 | 228 |
val prop = Drule.plain_prop_of (Thm.transfer thy th); |
19528 | 229 |
val (t, Ss, c) = Logic.dest_arity prop handle TERM _ => err (); |
230 |
val _ = if map (Sign.certify_sort thy) Ss = Ss then () else err (); |
|
19574 | 231 |
in |
232 |
thy |
|
233 |
|> Sign.primitive_arity (t, Ss, [c]) |
|
234 |
|> put_arity ((t, Ss, c), Drule.unconstrainTs th) |
|
235 |
end; |
|
404
dd3d3d6467db
axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff
changeset
|
236 |
|
dd3d3d6467db
axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff
changeset
|
237 |
|
19243
5dcb899a8486
moved read_class, read/cert_classrel/arity to sign.ML;
wenzelm
parents:
19134
diff
changeset
|
238 |
(* tactical proofs *) |
15876
a67343c6ab2a
sane interfaces for tactical instance proofs (do not expand defs of theory, proper handling of sort instances);
wenzelm
parents:
15853
diff
changeset
|
239 |
|
19405 | 240 |
fun prove_classrel raw_rel tac thy = |
15876
a67343c6ab2a
sane interfaces for tactical instance proofs (do not expand defs of theory, proper handling of sort instances);
wenzelm
parents:
15853
diff
changeset
|
241 |
let |
19405 | 242 |
val (c1, c2) = cert_classrel thy raw_rel; |
20049 | 243 |
val th = Goal.prove (ProofContext.init thy) [] [] |
244 |
(Logic.mk_classrel (c1, c2)) (fn _ => tac) handle ERROR msg => |
|
19243
5dcb899a8486
moved read_class, read/cert_classrel/arity to sign.ML;
wenzelm
parents:
19134
diff
changeset
|
245 |
cat_error msg ("The error(s) above occurred while trying to prove class relation " ^ |
5dcb899a8486
moved read_class, read/cert_classrel/arity to sign.ML;
wenzelm
parents:
19134
diff
changeset
|
246 |
quote (Sign.string_of_classrel thy [c1, c2])); |
19511 | 247 |
in |
248 |
thy |
|
20628 | 249 |
|> PureThy.add_thms [((prefix classrel_prefix (Logic.name_classrel (c1, c2)), th), [])] |
19511 | 250 |
|-> (fn [th'] => add_classrel th') |
251 |
end; |
|
404
dd3d3d6467db
axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff
changeset
|
252 |
|
19243
5dcb899a8486
moved read_class, read/cert_classrel/arity to sign.ML;
wenzelm
parents:
19134
diff
changeset
|
253 |
fun prove_arity raw_arity tac thy = |
15876
a67343c6ab2a
sane interfaces for tactical instance proofs (do not expand defs of theory, proper handling of sort instances);
wenzelm
parents:
15853
diff
changeset
|
254 |
let |
19243
5dcb899a8486
moved read_class, read/cert_classrel/arity to sign.ML;
wenzelm
parents:
19134
diff
changeset
|
255 |
val arity = Sign.cert_arity thy raw_arity; |
20628 | 256 |
val names = map (prefix arity_prefix) (Logic.name_arities arity); |
19405 | 257 |
val props = Logic.mk_arities arity; |
20049 | 258 |
val ths = Goal.prove_multi (ProofContext.init thy) [] [] props |
21687 | 259 |
(fn _ => Goal.precise_conjunction_tac (length props) 1 THEN tac) handle ERROR msg => |
19243
5dcb899a8486
moved read_class, read/cert_classrel/arity to sign.ML;
wenzelm
parents:
19134
diff
changeset
|
260 |
cat_error msg ("The error(s) above occurred while trying to prove type arity " ^ |
5dcb899a8486
moved read_class, read/cert_classrel/arity to sign.ML;
wenzelm
parents:
19134
diff
changeset
|
261 |
quote (Sign.string_of_arity thy arity)); |
19511 | 262 |
in |
263 |
thy |
|
20628 | 264 |
|> PureThy.add_thms (map (rpair []) (names ~~ ths)) |
19511 | 265 |
|-> fold add_arity |
266 |
end; |
|
267 |
||
268 |
||
269 |
||
270 |
(** class definitions **) |
|
271 |
||
272 |
local |
|
273 |
||
21463
42dd50268c8b
completed class parameter handling in axclass.ML
haftmann
parents:
20628
diff
changeset
|
274 |
fun read_param thy raw_t = |
42dd50268c8b
completed class parameter handling in axclass.ML
haftmann
parents:
20628
diff
changeset
|
275 |
let |
42dd50268c8b
completed class parameter handling in axclass.ML
haftmann
parents:
20628
diff
changeset
|
276 |
val t = Sign.read_term thy raw_t |
42dd50268c8b
completed class parameter handling in axclass.ML
haftmann
parents:
20628
diff
changeset
|
277 |
in case try dest_Const t |
42dd50268c8b
completed class parameter handling in axclass.ML
haftmann
parents:
20628
diff
changeset
|
278 |
of SOME (c, _) => c |
42dd50268c8b
completed class parameter handling in axclass.ML
haftmann
parents:
20628
diff
changeset
|
279 |
| NONE => error ("Not a constant: " ^ Sign.string_of_term thy t) |
42dd50268c8b
completed class parameter handling in axclass.ML
haftmann
parents:
20628
diff
changeset
|
280 |
end; |
42dd50268c8b
completed class parameter handling in axclass.ML
haftmann
parents:
20628
diff
changeset
|
281 |
|
42dd50268c8b
completed class parameter handling in axclass.ML
haftmann
parents:
20628
diff
changeset
|
282 |
fun def_class prep_class prep_att prep_param prep_propp |
42dd50268c8b
completed class parameter handling in axclass.ML
haftmann
parents:
20628
diff
changeset
|
283 |
(bclass, raw_super) raw_params raw_specs thy = |
19511 | 284 |
let |
285 |
val ctxt = ProofContext.init thy; |
|
286 |
val pp = ProofContext.pp ctxt; |
|
287 |
||
288 |
||
289 |
(* prepare specification *) |
|
290 |
||
291 |
val bconst = Logic.const_of_class bclass; |
|
292 |
val class = Sign.full_name thy bclass; |
|
293 |
val super = map (prep_class thy) raw_super |> Sign.certify_sort thy; |
|
294 |
||
295 |
fun prep_axiom t = |
|
296 |
(case Term.add_tfrees t [] of |
|
297 |
[(a, S)] => |
|
298 |
if Sign.subsort thy (super, S) then t |
|
299 |
else error ("Sort constraint of type variable " ^ |
|
300 |
setmp show_sorts true (Pretty.string_of_typ pp) (TFree (a, S)) ^ |
|
301 |
" needs to be weaker than " ^ Pretty.string_of_sort pp super) |
|
302 |
| [] => t |
|
303 |
| _ => error ("Multiple type variables in class axiom:\n" ^ Pretty.string_of_term pp t)) |
|
20548
8ef25fe585a8
renamed Term.map_term_types to Term.map_types (cf. Term.fold_types);
wenzelm
parents:
20260
diff
changeset
|
304 |
|> map_types (Term.map_atyps (fn TFree _ => Term.aT [] | U => U)) |
19511 | 305 |
|> Logic.close_form; |
306 |
||
19585 | 307 |
val axiomss = prep_propp (ctxt, map (map (rpair []) o snd) raw_specs) |
19511 | 308 |
|> snd |> map (map (prep_axiom o fst)); |
309 |
val name_atts = Attrib.map_specs (prep_att thy) raw_specs |> map fst; |
|
310 |
||
311 |
||
312 |
(* definition *) |
|
313 |
||
314 |
val conjs = map (curry Logic.mk_inclass (Term.aT [])) super @ flat axiomss; |
|
315 |
val class_eq = |
|
316 |
Logic.mk_equals (Logic.mk_inclass (Term.aT [], class), Logic.mk_conjunction_list conjs); |
|
317 |
||
318 |
val ([def], def_thy) = |
|
319 |
thy |
|
320 |
|> Sign.primitive_class (bclass, super) |
|
321 |
|> PureThy.add_defs_i false [((Thm.def_name bconst, class_eq), [])]; |
|
322 |
val (raw_intro, (raw_classrel, raw_axioms)) = |
|
323 |
(Conjunction.split_defined (length conjs) def) ||> chop (length super); |
|
19392 | 324 |
|
19418
03b01c9314fc
reworded add_axclass(_i): canonical specification format,
wenzelm
parents:
19405
diff
changeset
|
325 |
|
19511 | 326 |
(* facts *) |
327 |
||
328 |
val class_triv = Thm.class_triv def_thy class; |
|
329 |
val ([(_, [intro]), (_, classrel), (_, axioms)], facts_thy) = |
|
330 |
def_thy |
|
331 |
|> PureThy.note_thmss_qualified "" bconst |
|
332 |
[((introN, []), [([Drule.standard raw_intro], [])]), |
|
333 |
((superN, []), [(map Drule.standard raw_classrel, [])]), |
|
334 |
((axiomsN, []), [(map (fn th => Drule.standard (class_triv RS th)) raw_axioms, [])])]; |
|
335 |
||
21463
42dd50268c8b
completed class parameter handling in axclass.ML
haftmann
parents:
20628
diff
changeset
|
336 |
(* params *) |
42dd50268c8b
completed class parameter handling in axclass.ML
haftmann
parents:
20628
diff
changeset
|
337 |
|
42dd50268c8b
completed class parameter handling in axclass.ML
haftmann
parents:
20628
diff
changeset
|
338 |
val params = map (prep_param thy) raw_params; |
42dd50268c8b
completed class parameter handling in axclass.ML
haftmann
parents:
20628
diff
changeset
|
339 |
val params_typs = map (fn param => |
42dd50268c8b
completed class parameter handling in axclass.ML
haftmann
parents:
20628
diff
changeset
|
340 |
let |
42dd50268c8b
completed class parameter handling in axclass.ML
haftmann
parents:
20628
diff
changeset
|
341 |
val ty = Sign.the_const_type thy param; |
21893 | 342 |
val _ = case Term.typ_tvars ty |
343 |
of [_] => () |
|
21953 | 344 |
| _ => error ("Exactly one type variable required in parameter " ^ quote param); |
21893 | 345 |
val ty' = Term.map_type_tvar (fn _ => TFree (param_tyvarname, [class])) ty; |
21463
42dd50268c8b
completed class parameter handling in axclass.ML
haftmann
parents:
20628
diff
changeset
|
346 |
in (param, ty') end) params; |
19511 | 347 |
|
348 |
(* result *) |
|
349 |
||
21925 | 350 |
val axclass = make_axclass ((def, intro, axioms), params_typs); |
19511 | 351 |
val result_thy = |
352 |
facts_thy |
|
19574 | 353 |
|> fold put_classrel (map (pair class) super ~~ classrel) |
19511 | 354 |
|> Sign.add_path bconst |
355 |
|> PureThy.note_thmss_i "" (name_atts ~~ map Thm.simple_fact (unflat axiomss axioms)) |> snd |
|
356 |
|> Sign.restore_naming facts_thy |
|
19574 | 357 |
|> map_axclasses (fn (axclasses, parameters) => |
21919 | 358 |
(Symtab.update (class, axclass) axclasses, |
19574 | 359 |
fold (fn x => add_param pp (x, class)) params parameters)); |
19511 | 360 |
|
361 |
in (class, result_thy) end; |
|
362 |
||
363 |
in |
|
364 |
||
21463
42dd50268c8b
completed class parameter handling in axclass.ML
haftmann
parents:
20628
diff
changeset
|
365 |
val define_class = def_class Sign.read_class Attrib.attribute read_param ProofContext.read_propp; |
42dd50268c8b
completed class parameter handling in axclass.ML
haftmann
parents:
20628
diff
changeset
|
366 |
val define_class_i = def_class Sign.certify_class (K I) (K I) ProofContext.cert_propp; |
19511 | 367 |
|
368 |
end; |
|
369 |
||
370 |
||
19531 | 371 |
|
19511 | 372 |
(** axiomatizations **) |
373 |
||
374 |
local |
|
375 |
||
20628 | 376 |
fun axiomatize prep mk name add raw_args thy = |
377 |
let |
|
378 |
val args = prep thy raw_args; |
|
379 |
val specs = mk args; |
|
380 |
val names = name args; |
|
381 |
in thy |> PureThy.add_axioms_i (map (rpair []) (names ~~ specs)) |-> fold add end; |
|
19511 | 382 |
|
383 |
fun ax_classrel prep = |
|
20628 | 384 |
axiomatize (map o prep) (map Logic.mk_classrel) |
385 |
(map (prefix classrel_prefix o Logic.name_classrel)) add_classrel; |
|
19511 | 386 |
|
387 |
fun ax_arity prep = |
|
20628 | 388 |
axiomatize prep Logic.mk_arities |
389 |
(map (prefix arity_prefix) o Logic.name_arities) add_arity; |
|
19511 | 390 |
|
19706 | 391 |
fun class_const c = |
392 |
(Logic.const_of_class c, Term.itselfT (Term.aT []) --> propT); |
|
393 |
||
19511 | 394 |
fun ax_class prep_class prep_classrel (bclass, raw_super) thy = |
395 |
let |
|
396 |
val class = Sign.full_name thy bclass; |
|
397 |
val super = map (prep_class thy) raw_super |> Sign.certify_sort thy; |
|
398 |
in |
|
399 |
thy |
|
400 |
|> Sign.primitive_class (bclass, super) |
|
401 |
|> ax_classrel prep_classrel (map (fn c => (class, c)) super) |
|
19706 | 402 |
|> Theory.add_deps "" (class_const class) (map class_const super) |
19511 | 403 |
end; |
404 |
||
405 |
in |
|
406 |
||
407 |
val axiomatize_class = ax_class Sign.read_class read_classrel; |
|
408 |
val axiomatize_class_i = ax_class Sign.certify_class cert_classrel; |
|
409 |
val axiomatize_classrel = ax_classrel read_classrel; |
|
410 |
val axiomatize_classrel_i = ax_classrel cert_classrel; |
|
411 |
val axiomatize_arity = ax_arity Sign.read_arity; |
|
412 |
val axiomatize_arity_i = ax_arity Sign.cert_arity; |
|
413 |
||
414 |
end; |
|
415 |
||
416 |
||
417 |
||
418 |
(** explicit derivations -- cached **) |
|
419 |
||
19574 | 420 |
datatype cache = Types of (class * thm) list Typtab.table; |
421 |
val cache = Types Typtab.empty; |
|
422 |
||
19511 | 423 |
local |
19503 | 424 |
|
19574 | 425 |
fun lookup_type (Types cache) = AList.lookup (op =) o Typtab.lookup_list cache; |
426 |
fun insert_type T der (Types cache) = Types (Typtab.insert_list (eq_fst op =) (T, der) cache); |
|
19503 | 427 |
|
19522 | 428 |
fun derive_type _ (_, []) = [] |
429 |
| derive_type thy (typ, sort) = |
|
430 |
let |
|
19528 | 431 |
val vars = Term.fold_atyps |
19522 | 432 |
(fn T as TFree (_, S) => insert (eq_fst op =) (T, S) |
433 |
| T as TVar (_, S) => insert (eq_fst op =) (T, S) |
|
19528 | 434 |
| _ => I) typ []; |
19574 | 435 |
val hyps = vars |
436 |
|> map (fn (T, S) => (T, Drule.sort_triv thy (T, S) ~~ S)); |
|
19642 | 437 |
val ths = (typ, sort) |> Sorts.of_sort_derivation (Sign.pp thy) (Sign.classes_of thy) |
19574 | 438 |
{classrel = |
439 |
fn (th, c1) => fn c2 => th RS the_classrel thy (c1, c2), |
|
440 |
constructor = |
|
441 |
fn a => fn dom => fn c => |
|
442 |
let val Ss = map (map snd) dom and ths = maps (map fst) dom |
|
443 |
in ths MRS the_arity thy a (c, Ss) end, |
|
444 |
variable = |
|
445 |
the_default [] o AList.lookup (op =) hyps}; |
|
446 |
in ths end; |
|
19503 | 447 |
|
19511 | 448 |
in |
449 |
||
19574 | 450 |
fun of_sort thy (typ, sort) cache = |
451 |
let |
|
452 |
val sort' = filter (is_none o lookup_type cache typ) sort; |
|
453 |
val ths' = derive_type thy (typ, sort') |
|
454 |
handle ERROR msg => cat_error msg ("The error(s) above occurred for sort derivation: " ^ |
|
455 |
Sign.string_of_typ thy typ ^ " :: " ^ Sign.string_of_sort thy sort'); |
|
456 |
val cache' = cache |> fold (insert_type typ) (sort' ~~ ths'); |
|
457 |
val ths = |
|
458 |
sort |> map (fn c => |
|
459 |
Goal.finish (the (lookup_type cache' typ c) RS |
|
460 |
Goal.init (Thm.cterm_of thy (Logic.mk_inclass (typ, c)))) |
|
20260 | 461 |
|> Thm.adjust_maxidx_thm ~1); |
19574 | 462 |
in (ths, cache') end; |
19503 | 463 |
|
19511 | 464 |
end; |
19418
03b01c9314fc
reworded add_axclass(_i): canonical specification format,
wenzelm
parents:
19405
diff
changeset
|
465 |
|
15876
a67343c6ab2a
sane interfaces for tactical instance proofs (do not expand defs of theory, proper handling of sort instances);
wenzelm
parents:
15853
diff
changeset
|
466 |
end; |