author | wenzelm |
Tue, 20 Oct 1998 16:36:21 +0200 | |
changeset 5695 | 898429dbb162 |
parent 5578 | 7de426cf179c |
child 6394 | 3d9fd50fcc43 |
permissions | -rw-r--r-- |
5177 | 1 |
(* Title: HOL/Tools/datatype_prop.ML |
2 |
ID: $Id$ |
|
3 |
Author: Stefan Berghofer |
|
4 |
Copyright 1998 TU Muenchen |
|
5 |
||
6 |
Characteristic properties of datatypes |
|
7 |
*) |
|
8 |
||
9 |
signature DATATYPE_PROP = |
|
10 |
sig |
|
11 |
val dtK : int |
|
12 |
val make_injs : (int * (string * DatatypeAux.dtyp list * |
|
13 |
(string * DatatypeAux.dtyp list) list)) list list -> (string * sort) list -> |
|
14 |
term list list |
|
15 |
val make_ind : (int * (string * DatatypeAux.dtyp list * |
|
16 |
(string * DatatypeAux.dtyp list) list)) list list -> (string * sort) list -> term |
|
17 |
val make_casedists : (int * (string * DatatypeAux.dtyp list * |
|
18 |
(string * DatatypeAux.dtyp list) list)) list list -> (string * sort) list -> term list |
|
19 |
val make_primrecs : string list -> (int * (string * DatatypeAux.dtyp list * |
|
20 |
(string * DatatypeAux.dtyp list) list)) list list -> (string * sort) list -> |
|
21 |
theory -> term list |
|
22 |
val make_cases : string list -> (int * (string * DatatypeAux.dtyp list * |
|
23 |
(string * DatatypeAux.dtyp list) list)) list list -> (string * sort) list -> |
|
24 |
theory -> term list list |
|
25 |
val make_distincts : string list -> (int * (string * DatatypeAux.dtyp list * |
|
26 |
(string * DatatypeAux.dtyp list) list)) list list -> (string * sort) list -> |
|
27 |
theory -> term list list |
|
28 |
val make_splits : string list -> (int * (string * DatatypeAux.dtyp list * |
|
29 |
(string * DatatypeAux.dtyp list) list)) list list -> (string * sort) list -> |
|
30 |
theory -> (term * term) list |
|
31 |
val make_case_trrules : string list -> (int * (string * DatatypeAux.dtyp list * |
|
32 |
(string * DatatypeAux.dtyp list) list)) list list -> ast Syntax.trrule list |
|
33 |
val make_size : string list -> (int * (string * DatatypeAux.dtyp list * |
|
34 |
(string * DatatypeAux.dtyp list) list)) list list -> (string * sort) list -> |
|
35 |
theory -> term list |
|
36 |
val make_case_congs : string list -> (int * (string * DatatypeAux.dtyp list * |
|
37 |
(string * DatatypeAux.dtyp list) list)) list list -> (string * sort) list -> |
|
38 |
theory -> term list |
|
39 |
val make_nchotomys : (int * (string * DatatypeAux.dtyp list * |
|
40 |
(string * DatatypeAux.dtyp list) list)) list list -> (string * sort) list -> term list |
|
41 |
end; |
|
42 |
||
43 |
structure DatatypeProp : DATATYPE_PROP = |
|
44 |
struct |
|
45 |
||
46 |
open DatatypeAux; |
|
47 |
||
48 |
(*the kind of distinctiveness axioms depends on number of constructors*) |
|
49 |
val dtK = 7; |
|
50 |
||
51 |
fun make_tnames Ts = |
|
52 |
let |
|
53 |
fun type_name (TFree (name, _)) = implode (tl (explode name)) |
|
54 |
| type_name (Type (name, _)) = |
|
55 |
let val name' = Sign.base_name name |
|
56 |
in if Syntax.is_identifier name' then name' else "x" |
|
57 |
end; |
|
58 |
||
59 |
fun index_vnames (vn::vns) tab = |
|
60 |
(case assoc (tab, vn) of |
|
61 |
None => if vn mem vns |
|
62 |
then (vn ^ "1") :: index_vnames vns ((vn, 2)::tab) |
|
63 |
else vn :: index_vnames vns tab |
|
64 |
| Some i => (vn ^ (string_of_int i)):: |
|
65 |
index_vnames vns ((vn, i + 1)::tab)) |
|
66 |
| index_vnames [] _ = [] |
|
67 |
||
68 |
in index_vnames (map type_name Ts) [] |
|
69 |
end; |
|
70 |
||
71 |
(** FIXME: move to hologic.ML ? **) |
|
72 |
val Not = Const ("Not", HOLogic.boolT --> HOLogic.boolT); |
|
73 |
||
74 |
(************************* injectivity of constructors ************************) |
|
75 |
||
76 |
fun make_injs descr sorts = |
|
77 |
let |
|
78 |
val descr' = flat descr; |
|
79 |
||
80 |
fun make_inj T ((cname, cargs), injs) = |
|
81 |
if null cargs then injs else |
|
82 |
let |
|
83 |
val Ts = map (typ_of_dtyp descr' sorts) cargs; |
|
84 |
val constr_t = Const (cname, Ts ---> T); |
|
85 |
val tnames = make_tnames Ts; |
|
86 |
val frees = map Free (tnames ~~ Ts); |
|
87 |
val frees' = map Free ((map ((op ^) o (rpair "'")) tnames) ~~ Ts); |
|
88 |
in (HOLogic.mk_Trueprop (HOLogic.mk_eq |
|
89 |
(HOLogic.mk_eq (list_comb (constr_t, frees), list_comb (constr_t, frees')), |
|
90 |
foldr1 (HOLogic.mk_binop "op &") |
|
91 |
(map HOLogic.mk_eq (frees ~~ frees')))))::injs |
|
92 |
end; |
|
93 |
||
94 |
in map (fn (d, T) => foldr (make_inj T) (#3 (snd d), [])) |
|
95 |
((hd descr) ~~ take (length (hd descr), get_rec_types descr' sorts)) |
|
96 |
end; |
|
97 |
||
98 |
(********************************* induction **********************************) |
|
99 |
||
100 |
fun make_ind descr sorts = |
|
101 |
let |
|
102 |
val descr' = flat descr; |
|
103 |
val recTs = get_rec_types descr' sorts; |
|
104 |
val pnames = if length descr' = 1 then ["P"] |
|
105 |
else map (fn i => "P" ^ string_of_int i) (1 upto length descr'); |
|
106 |
||
107 |
fun make_pred i T = |
|
108 |
let val T' = T --> HOLogic.boolT |
|
109 |
in Free (nth_elem (i, pnames), T') end; |
|
110 |
||
111 |
fun make_ind_prem k T (cname, cargs) = |
|
112 |
let |
|
113 |
val recs = filter is_rec_type cargs; |
|
114 |
val Ts = map (typ_of_dtyp descr' sorts) cargs; |
|
115 |
val recTs' = map (typ_of_dtyp descr' sorts) recs; |
|
116 |
val tnames = variantlist (make_tnames Ts, pnames); |
|
117 |
val rec_tnames = map fst (filter (is_rec_type o snd) (tnames ~~ cargs)); |
|
118 |
val frees = tnames ~~ Ts; |
|
119 |
val prems = map (fn ((r, s), T) => HOLogic.mk_Trueprop |
|
120 |
(make_pred (dest_DtRec r) T $ Free (s, T))) (recs ~~ rec_tnames ~~ recTs'); |
|
121 |
||
122 |
in list_all_free (frees, Logic.list_implies (prems, |
|
123 |
HOLogic.mk_Trueprop (make_pred k T $ |
|
124 |
list_comb (Const (cname, Ts ---> T), map Free frees)))) |
|
125 |
end; |
|
126 |
||
127 |
val prems = flat (map (fn ((i, (_, _, constrs)), T) => |
|
128 |
map (make_ind_prem i T) constrs) (descr' ~~ recTs)); |
|
129 |
val tnames = make_tnames recTs; |
|
130 |
val concl = HOLogic.mk_Trueprop (foldr1 (HOLogic.mk_binop "op &") |
|
131 |
(map (fn (((i, _), T), tname) => make_pred i T $ Free (tname, T)) |
|
132 |
(descr' ~~ recTs ~~ tnames))) |
|
133 |
||
134 |
in Logic.list_implies (prems, concl) end; |
|
135 |
||
136 |
(******************************* case distinction *****************************) |
|
137 |
||
138 |
fun make_casedists descr sorts = |
|
139 |
let |
|
140 |
val descr' = flat descr; |
|
141 |
||
142 |
fun make_casedist_prem T (cname, cargs) = |
|
143 |
let |
|
144 |
val Ts = map (typ_of_dtyp descr' sorts) cargs; |
|
145 |
val frees = variantlist (make_tnames Ts, ["P", "y"]) ~~ Ts; |
|
146 |
val free_ts = map Free frees |
|
147 |
in list_all_free (frees, Logic.mk_implies (HOLogic.mk_Trueprop |
|
148 |
(HOLogic.mk_eq (Free ("y", T), list_comb (Const (cname, Ts ---> T), free_ts))), |
|
149 |
HOLogic.mk_Trueprop (Free ("P", HOLogic.boolT)))) |
|
150 |
end; |
|
151 |
||
152 |
fun make_casedist ((_, (_, _, constrs)), T) = |
|
153 |
let val prems = map (make_casedist_prem T) constrs |
|
154 |
in Logic.list_implies (prems, HOLogic.mk_Trueprop (Free ("P", HOLogic.boolT))) |
|
155 |
end |
|
156 |
||
157 |
in map make_casedist |
|
158 |
((hd descr) ~~ take (length (hd descr), get_rec_types descr' sorts)) |
|
159 |
end; |
|
160 |
||
161 |
(*************** characteristic equations for primrec combinator **************) |
|
162 |
||
163 |
fun make_primrecs new_type_names descr sorts thy = |
|
164 |
let |
|
165 |
val sign = sign_of thy; |
|
166 |
||
167 |
val descr' = flat descr; |
|
168 |
val recTs = get_rec_types descr' sorts; |
|
5578
7de426cf179c
Package now chooses type variable names more carefully to
berghofe
parents:
5177
diff
changeset
|
169 |
val used = foldr add_typ_tfree_names (recTs, []); |
5177 | 170 |
|
5578
7de426cf179c
Package now chooses type variable names more carefully to
berghofe
parents:
5177
diff
changeset
|
171 |
val rec_result_Ts = map TFree (variantlist (replicate (length descr') "'t", used) ~~ |
7de426cf179c
Package now chooses type variable names more carefully to
berghofe
parents:
5177
diff
changeset
|
172 |
replicate (length descr') HOLogic.termS); |
5177 | 173 |
|
174 |
val reccomb_fn_Ts = flat (map (fn (i, (_, _, constrs)) => |
|
175 |
map (fn (_, cargs) => |
|
176 |
let |
|
177 |
val recs = filter is_rec_type cargs; |
|
178 |
val argTs = (map (typ_of_dtyp descr' sorts) cargs) @ |
|
179 |
(map (fn r => nth_elem (dest_DtRec r, rec_result_Ts)) recs) |
|
180 |
in argTs ---> nth_elem (i, rec_result_Ts) |
|
181 |
end) constrs) descr'); |
|
182 |
||
183 |
val rec_fns = map (uncurry (mk_Free "f")) |
|
184 |
(reccomb_fn_Ts ~~ (1 upto (length reccomb_fn_Ts))); |
|
185 |
||
186 |
val big_reccomb_name = (space_implode "_" new_type_names) ^ "_rec"; |
|
187 |
val reccomb_names = map (Sign.intern_const sign) |
|
188 |
(if length descr' = 1 then [big_reccomb_name] else |
|
189 |
(map ((curry (op ^) (big_reccomb_name ^ "_")) o string_of_int) |
|
190 |
(1 upto (length descr')))); |
|
191 |
val reccombs = map (fn ((name, T), T') => list_comb |
|
192 |
(Const (name, reccomb_fn_Ts @ [T] ---> T'), rec_fns)) |
|
193 |
(reccomb_names ~~ recTs ~~ rec_result_Ts); |
|
194 |
||
195 |
fun make_primrec T comb_t ((ts, f::fs), (cname, cargs)) = |
|
196 |
let |
|
197 |
val recs = filter is_rec_type cargs; |
|
198 |
val Ts = map (typ_of_dtyp descr' sorts) cargs; |
|
199 |
val recTs' = map (typ_of_dtyp descr' sorts) recs; |
|
200 |
val tnames = make_tnames Ts; |
|
201 |
val rec_tnames = map fst (filter (is_rec_type o snd) (tnames ~~ cargs)); |
|
202 |
val frees = map Free (tnames ~~ Ts); |
|
203 |
val frees' = map Free (rec_tnames ~~ recTs'); |
|
204 |
val reccombs' = map (fn (DtRec i) => nth_elem (i, reccombs)) recs |
|
205 |
||
206 |
in (ts @ [HOLogic.mk_Trueprop (HOLogic.mk_eq |
|
207 |
(comb_t $ list_comb (Const (cname, Ts ---> T), frees), |
|
208 |
list_comb (f, frees @ (map (uncurry ap) (reccombs' ~~ frees')))))], fs) |
|
209 |
end |
|
210 |
||
211 |
in fst (foldl (fn (x, ((dt, T), comb_t)) => |
|
212 |
foldl (make_primrec T comb_t) (x, #3 (snd dt))) |
|
213 |
(([], rec_fns), descr' ~~ recTs ~~ reccombs)) |
|
214 |
end; |
|
215 |
||
216 |
(****************** make terms of form t_case f1 ... fn *********************) |
|
217 |
||
218 |
fun make_case_combs new_type_names descr sorts thy fname = |
|
219 |
let |
|
220 |
val descr' = flat descr; |
|
221 |
val recTs = get_rec_types descr' sorts; |
|
5578
7de426cf179c
Package now chooses type variable names more carefully to
berghofe
parents:
5177
diff
changeset
|
222 |
val used = foldr add_typ_tfree_names (recTs, []); |
5177 | 223 |
val newTs = take (length (hd descr), recTs); |
5578
7de426cf179c
Package now chooses type variable names more carefully to
berghofe
parents:
5177
diff
changeset
|
224 |
val T' = TFree (variant used "'t", HOLogic.termS); |
5177 | 225 |
|
226 |
val case_fn_Ts = map (fn (i, (_, _, constrs)) => |
|
227 |
map (fn (_, cargs) => |
|
228 |
let val Ts = map (typ_of_dtyp descr' sorts) cargs |
|
229 |
in Ts ---> T' end) constrs) (hd descr); |
|
230 |
||
231 |
val case_names = map (fn s => |
|
232 |
Sign.intern_const (sign_of thy) (s ^ "_case")) new_type_names |
|
233 |
in |
|
234 |
map (fn ((name, Ts), T) => list_comb |
|
235 |
(Const (name, Ts @ [T] ---> T'), |
|
236 |
map (uncurry (mk_Free fname)) (Ts ~~ (1 upto length Ts)))) |
|
237 |
(case_names ~~ case_fn_Ts ~~ newTs) |
|
238 |
end; |
|
239 |
||
240 |
(**************** characteristic equations for case combinator ****************) |
|
241 |
||
242 |
fun make_cases new_type_names descr sorts thy = |
|
243 |
let |
|
244 |
val descr' = flat descr; |
|
245 |
val recTs = get_rec_types descr' sorts; |
|
246 |
val newTs = take (length (hd descr), recTs); |
|
247 |
||
248 |
fun make_case T comb_t ((cname, cargs), f) = |
|
249 |
let |
|
250 |
val Ts = map (typ_of_dtyp descr' sorts) cargs; |
|
251 |
val frees = map Free ((make_tnames Ts) ~~ Ts) |
|
252 |
in HOLogic.mk_Trueprop (HOLogic.mk_eq |
|
253 |
(comb_t $ list_comb (Const (cname, Ts ---> T), frees), |
|
254 |
list_comb (f, frees))) |
|
255 |
end |
|
256 |
||
257 |
in map (fn (((_, (_, _, constrs)), T), comb_t) => |
|
258 |
map (make_case T comb_t) (constrs ~~ (snd (strip_comb comb_t)))) |
|
259 |
((hd descr) ~~ newTs ~~ (make_case_combs new_type_names descr sorts thy "f")) |
|
260 |
end; |
|
261 |
||
262 |
(************************* distinctness of constructors ***********************) |
|
263 |
||
264 |
fun make_distincts new_type_names descr sorts thy = |
|
265 |
let |
|
266 |
val descr' = flat descr; |
|
267 |
val recTs = get_rec_types descr' sorts; |
|
268 |
val newTs = take (length (hd descr), recTs); |
|
269 |
||
270 |
(**** number of constructors < dtK : C_i ... ~= C_j ... ****) |
|
271 |
||
272 |
fun make_distincts_1 _ [] = [] |
|
273 |
| make_distincts_1 T ((cname, cargs)::constrs) = |
|
274 |
let |
|
275 |
val Ts = map (typ_of_dtyp descr' sorts) cargs; |
|
276 |
val frees = map Free ((make_tnames Ts) ~~ Ts); |
|
277 |
val t = list_comb (Const (cname, Ts ---> T), frees); |
|
278 |
||
279 |
fun make_distincts' [] = [] |
|
280 |
| make_distincts' ((cname', cargs')::constrs') = |
|
281 |
let |
|
282 |
val Ts' = map (typ_of_dtyp descr' sorts) cargs'; |
|
283 |
val frees' = map Free ((map ((op ^) o (rpair "'")) |
|
284 |
(make_tnames Ts')) ~~ Ts'); |
|
285 |
val t' = list_comb (Const (cname', Ts' ---> T), frees') |
|
286 |
in |
|
287 |
(HOLogic.mk_Trueprop (Not $ HOLogic.mk_eq (t, t'))):: |
|
288 |
(HOLogic.mk_Trueprop (Not $ HOLogic.mk_eq (t', t))):: |
|
289 |
(make_distincts' constrs') |
|
290 |
end |
|
291 |
||
292 |
in (make_distincts' constrs) @ (make_distincts_1 T constrs) |
|
293 |
end; |
|
294 |
||
295 |
(**** number of constructors >= dtK : t_ord C_i ... = i ****) |
|
296 |
||
297 |
fun make_distincts_2 T tname i constrs = |
|
298 |
let |
|
299 |
val ord_name = Sign.intern_const (sign_of thy) (tname ^ "_ord"); |
|
300 |
val ord_t = Const (ord_name, T --> HOLogic.natT) |
|
301 |
||
302 |
in (case constrs of |
|
303 |
[] => [Logic.mk_implies (HOLogic.mk_Trueprop (Not $ HOLogic.mk_eq |
|
304 |
(ord_t $ Free ("x", T), ord_t $ Free ("y", T))), |
|
305 |
HOLogic.mk_Trueprop (Not $ HOLogic.mk_eq |
|
306 |
(Free ("x", T), Free ("y", T))))] |
|
307 |
| ((cname, cargs)::constrs) => |
|
308 |
let |
|
309 |
val Ts = map (typ_of_dtyp descr' sorts) cargs; |
|
310 |
val frees = map Free ((make_tnames Ts) ~~ Ts); |
|
311 |
in |
|
312 |
(HOLogic.mk_Trueprop (HOLogic.mk_eq (ord_t $ |
|
313 |
list_comb (Const (cname, Ts ---> T), frees), HOLogic.mk_nat i))):: |
|
314 |
(make_distincts_2 T tname (i + 1) constrs) |
|
315 |
end) |
|
316 |
end; |
|
317 |
||
318 |
in map (fn (((_, (_, _, constrs)), T), tname) => |
|
319 |
if length constrs < dtK then make_distincts_1 T constrs |
|
320 |
else make_distincts_2 T tname 0 constrs) |
|
321 |
((hd descr) ~~ newTs ~~ new_type_names) |
|
322 |
end; |
|
323 |
||
324 |
(*************************** the "split" - equations **************************) |
|
325 |
||
326 |
fun make_splits new_type_names descr sorts thy = |
|
327 |
let |
|
328 |
val descr' = flat descr; |
|
329 |
val recTs = get_rec_types descr' sorts; |
|
5578
7de426cf179c
Package now chooses type variable names more carefully to
berghofe
parents:
5177
diff
changeset
|
330 |
val used' = foldr add_typ_tfree_names (recTs, []); |
5177 | 331 |
val newTs = take (length (hd descr), recTs); |
5578
7de426cf179c
Package now chooses type variable names more carefully to
berghofe
parents:
5177
diff
changeset
|
332 |
val T' = TFree (variant used' "'t", HOLogic.termS); |
5177 | 333 |
val P = Free ("P", T' --> HOLogic.boolT); |
334 |
||
335 |
fun make_split (((_, (_, _, constrs)), T), comb_t) = |
|
336 |
let |
|
337 |
val (_, fs) = strip_comb comb_t; |
|
338 |
val used = ["P", "x"] @ (map (fst o dest_Free) fs); |
|
339 |
||
340 |
fun process_constr (((cname, cargs), f), (t1s, t2s)) = |
|
341 |
let |
|
342 |
val Ts = map (typ_of_dtyp descr' sorts) cargs; |
|
343 |
val frees = map Free (variantlist (make_tnames Ts, used) ~~ Ts); |
|
344 |
val eqn = HOLogic.mk_eq (Free ("x", T), |
|
345 |
list_comb (Const (cname, Ts ---> T), frees)); |
|
346 |
val P' = P $ list_comb (f, frees) |
|
347 |
in ((foldr (fn (Free (s, T), t) => HOLogic.mk_all (s, T, t)) |
|
348 |
(frees, HOLogic.imp $ eqn $ P'))::t1s, |
|
349 |
(foldr (fn (Free (s, T), t) => HOLogic.mk_exists (s, T, t)) |
|
350 |
(frees, HOLogic.conj $ eqn $ (Not $ P')))::t2s) |
|
351 |
end; |
|
352 |
||
353 |
val (t1s, t2s) = foldr process_constr (constrs ~~ fs, ([], [])); |
|
354 |
val lhs = P $ (comb_t $ Free ("x", T)) |
|
355 |
in |
|
356 |
(HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs, mk_conj t1s)), |
|
357 |
HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs, Not $ mk_disj t2s))) |
|
358 |
end |
|
359 |
||
360 |
in map make_split ((hd descr) ~~ newTs ~~ |
|
361 |
(make_case_combs new_type_names descr sorts thy "f")) |
|
362 |
end; |
|
363 |
||
364 |
(************************ translation rules for case **************************) |
|
365 |
||
366 |
fun make_case_trrules new_type_names descr = |
|
367 |
let |
|
368 |
fun mk_asts i j ((cname, cargs)::constrs) = |
|
369 |
let |
|
370 |
val k = length cargs; |
|
371 |
val xs = map (fn i => Variable ("x" ^ string_of_int i)) (i upto i + k - 1); |
|
372 |
val t = Variable ("t" ^ string_of_int j); |
|
5695 | 373 |
val ast = Syntax.mk_appl (Constant "@case1") |
374 |
[Syntax.mk_appl (Constant (Sign.base_name cname)) xs, t]; |
|
5177 | 375 |
val ast' = foldr (fn (x, y) => |
5695 | 376 |
Syntax.mk_appl (Constant "_abs") [x, y]) (xs, t) |
5177 | 377 |
in |
378 |
(case constrs of |
|
379 |
[] => (ast, [ast']) |
|
380 |
| cs => let val (ast'', asts) = mk_asts (i + k) (j + 1) cs |
|
5695 | 381 |
in (Syntax.mk_appl (Constant "@case2") [ast, ast''], |
5177 | 382 |
ast'::asts) |
383 |
end) |
|
384 |
end; |
|
385 |
||
386 |
fun mk_trrule ((_, (_, _, constrs)), tname) = |
|
387 |
let val (ast, asts) = mk_asts 1 1 constrs |
|
388 |
in Syntax.ParsePrintRule |
|
5695 | 389 |
(Syntax.mk_appl (Constant "@case") [Variable "t", ast], |
390 |
Syntax.mk_appl (Constant (tname ^ "_case")) |
|
5177 | 391 |
(asts @ [Variable "t"])) |
392 |
end |
|
393 |
||
394 |
in |
|
395 |
map mk_trrule (hd descr ~~ new_type_names) |
|
396 |
end; |
|
397 |
||
398 |
(******************************* size functions *******************************) |
|
399 |
||
400 |
fun make_size new_type_names descr sorts thy = |
|
401 |
let |
|
402 |
val descr' = flat descr; |
|
403 |
val recTs = get_rec_types descr' sorts; |
|
404 |
||
405 |
val big_size_name = space_implode "_" new_type_names ^ "_size"; |
|
406 |
val size_name = Sign.intern_const (sign_of (the (get_thy "Arith" thy))) "size"; |
|
407 |
val size_names = replicate (length (hd descr)) size_name @ |
|
408 |
map (Sign.intern_const (sign_of thy)) |
|
409 |
(if length (flat (tl descr)) = 1 then [big_size_name] else |
|
410 |
map (fn i => big_size_name ^ "_" ^ string_of_int i) |
|
411 |
(1 upto length (flat (tl descr)))); |
|
412 |
val size_consts = map (fn (s, T) => |
|
413 |
Const (s, T --> HOLogic.natT)) (size_names ~~ recTs); |
|
414 |
||
415 |
val plus_t = Const ("op +", [HOLogic.natT, HOLogic.natT] ---> HOLogic.natT); |
|
416 |
||
417 |
fun make_size_eqn size_const T (cname, cargs) = |
|
418 |
let |
|
419 |
val recs = filter is_rec_type cargs; |
|
420 |
val Ts = map (typ_of_dtyp descr' sorts) cargs; |
|
421 |
val recTs = map (typ_of_dtyp descr' sorts) recs; |
|
422 |
val tnames = make_tnames Ts; |
|
423 |
val rec_tnames = map fst (filter (is_rec_type o snd) (tnames ~~ cargs)); |
|
424 |
val ts = map (fn ((r, s), T) => nth_elem (dest_DtRec r, size_consts) $ |
|
425 |
Free (s, T)) (recs ~~ rec_tnames ~~ recTs); |
|
426 |
val t = if ts = [] then HOLogic.zero else |
|
427 |
foldl1 (app plus_t) (ts @ [HOLogic.mk_nat 1]) |
|
428 |
in |
|
429 |
HOLogic.mk_Trueprop (HOLogic.mk_eq (size_const $ |
|
430 |
list_comb (Const (cname, Ts ---> T), map Free (tnames ~~ Ts)), t)) |
|
431 |
end |
|
432 |
||
433 |
in |
|
434 |
flat (map (fn (((_, (_, _, constrs)), size_const), T) => |
|
435 |
map (make_size_eqn size_const T) constrs) (descr' ~~ size_consts ~~ recTs)) |
|
436 |
end; |
|
437 |
||
438 |
(************************* additional rules for TFL ***************************) |
|
439 |
||
440 |
(*--------------------------------------------------------------------------- |
|
441 |
* Structure of case congruence theorem looks like this: |
|
442 |
* |
|
443 |
* (M = M') |
|
444 |
* ==> (!!x1,...,xk. (M' = C1 x1..xk) ==> (f1 x1..xk = g1 x1..xk)) |
|
445 |
* ==> ... |
|
446 |
* ==> (!!x1,...,xj. (M' = Cn x1..xj) ==> (fn x1..xj = gn x1..xj)) |
|
447 |
* ==> |
|
448 |
* (ty_case f1..fn M = ty_case g1..gn M') |
|
449 |
*---------------------------------------------------------------------------*) |
|
450 |
||
451 |
fun make_case_congs new_type_names descr sorts thy = |
|
452 |
let |
|
453 |
val case_combs = make_case_combs new_type_names descr sorts thy "f"; |
|
454 |
val case_combs' = make_case_combs new_type_names descr sorts thy "g"; |
|
455 |
||
456 |
fun mk_case_cong ((comb, comb'), (_, (_, _, constrs))) = |
|
457 |
let |
|
458 |
val Type ("fun", [T, _]) = fastype_of comb; |
|
459 |
val (_, fs) = strip_comb comb; |
|
460 |
val (_, gs) = strip_comb comb'; |
|
461 |
val used = ["M", "M'"] @ map (fst o dest_Free) (fs @ gs); |
|
462 |
val M = Free ("M", T); |
|
463 |
val M' = Free ("M'", T); |
|
464 |
||
465 |
fun mk_clause ((f, g), (cname, _)) = |
|
466 |
let |
|
467 |
val (Ts, _) = strip_type (fastype_of f); |
|
468 |
val tnames = variantlist (make_tnames Ts, used); |
|
469 |
val frees = map Free (tnames ~~ Ts) |
|
470 |
in |
|
471 |
list_all_free (tnames ~~ Ts, Logic.mk_implies |
|
472 |
(HOLogic.mk_Trueprop |
|
473 |
(HOLogic.mk_eq (M', list_comb (Const (cname, Ts ---> T), frees))), |
|
474 |
HOLogic.mk_Trueprop |
|
475 |
(HOLogic.mk_eq (list_comb (f, frees), list_comb (g, frees))))) |
|
476 |
end |
|
477 |
||
478 |
in |
|
479 |
Logic.list_implies (HOLogic.mk_Trueprop (HOLogic.mk_eq (M, M')) :: |
|
480 |
map mk_clause (fs ~~ gs ~~ constrs), |
|
481 |
HOLogic.mk_Trueprop (HOLogic.mk_eq (comb $ M, comb' $ M'))) |
|
482 |
end |
|
483 |
||
484 |
in |
|
485 |
map mk_case_cong (case_combs ~~ case_combs' ~~ hd descr) |
|
486 |
end; |
|
487 |
||
488 |
(*--------------------------------------------------------------------------- |
|
489 |
* Structure of exhaustion theorem looks like this: |
|
490 |
* |
|
491 |
* !v. (? y1..yi. v = C1 y1..yi) | ... | (? y1..yj. v = Cn y1..yj) |
|
492 |
*---------------------------------------------------------------------------*) |
|
493 |
||
494 |
fun make_nchotomys descr sorts = |
|
495 |
let |
|
496 |
val descr' = flat descr; |
|
497 |
val recTs = get_rec_types descr' sorts; |
|
498 |
val newTs = take (length (hd descr), recTs); |
|
499 |
||
500 |
fun mk_eqn T (cname, cargs) = |
|
501 |
let |
|
502 |
val Ts = map (typ_of_dtyp descr' sorts) cargs; |
|
503 |
val tnames = variantlist (make_tnames Ts, ["v"]); |
|
504 |
val frees = tnames ~~ Ts |
|
505 |
in |
|
506 |
foldr (fn ((s, T'), t) => HOLogic.mk_exists (s, T', t)) |
|
507 |
(frees, HOLogic.mk_eq (Free ("v", T), |
|
508 |
list_comb (Const (cname, Ts ---> T), map Free frees))) |
|
509 |
end |
|
510 |
||
511 |
in map (fn ((_, (_, _, constrs)), T) => |
|
512 |
HOLogic.mk_Trueprop (HOLogic.mk_all ("v", T, mk_disj (map (mk_eqn T) constrs)))) |
|
513 |
(hd descr ~~ newTs) |
|
514 |
end; |
|
515 |
||
516 |
end; |