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