author | clasohm |
Fri, 01 Dec 1995 12:03:13 +0100 | |
changeset 1376 | 92f83b9d17e1 |
parent 1266 | 3ae9fe3c0f68 |
child 1465 | 5d7a7e439cec |
permissions | -rw-r--r-- |
969 | 1 |
(* Title: HOL/ex/SList.ML |
2 |
ID: $Id$ |
|
3 |
Author: Lawrence C Paulson, Cambridge University Computer Laboratory |
|
4 |
Copyright 1993 University of Cambridge |
|
5 |
||
6 |
Definition of type 'a list by a least fixed point |
|
7 |
*) |
|
8 |
||
9 |
open SList; |
|
10 |
||
11 |
val list_con_defs = [NIL_def, CONS_def]; |
|
12 |
||
13 |
goal SList.thy "list(A) = {Numb(0)} <+> (A <*> list(A))"; |
|
14 |
let val rew = rewrite_rule list_con_defs in |
|
15 |
by (fast_tac (univ_cs addSIs (equalityI :: map rew list.intrs) |
|
16 |
addEs [rew list.elim]) 1) |
|
17 |
end; |
|
18 |
qed "list_unfold"; |
|
19 |
||
20 |
(*This justifies using list in other recursive type definitions*) |
|
21 |
goalw SList.thy list.defs "!!A B. A<=B ==> list(A) <= list(B)"; |
|
22 |
by (rtac lfp_mono 1); |
|
23 |
by (REPEAT (ares_tac basic_monos 1)); |
|
24 |
qed "list_mono"; |
|
25 |
||
26 |
(*Type checking -- list creates well-founded sets*) |
|
27 |
goalw SList.thy (list_con_defs @ list.defs) "list(sexp) <= sexp"; |
|
28 |
by (rtac lfp_lowerbound 1); |
|
29 |
by (fast_tac (univ_cs addIs sexp.intrs@[sexp_In0I,sexp_In1I]) 1); |
|
30 |
qed "list_sexp"; |
|
31 |
||
32 |
(* A <= sexp ==> list(A) <= sexp *) |
|
33 |
bind_thm ("list_subset_sexp", ([list_mono, list_sexp] MRS subset_trans)); |
|
34 |
||
35 |
(*Induction for the type 'a list *) |
|
36 |
val prems = goalw SList.thy [Nil_def,Cons_def] |
|
37 |
"[| P(Nil); \ |
|
38 |
\ !!x xs. P(xs) ==> P(x # xs) |] ==> P(l)"; |
|
39 |
by (rtac (Rep_list_inverse RS subst) 1); (*types force good instantiation*) |
|
40 |
by (rtac (Rep_list RS list.induct) 1); |
|
41 |
by (REPEAT (ares_tac prems 1 |
|
42 |
ORELSE eresolve_tac [rangeE, ssubst, Abs_list_inverse RS subst] 1)); |
|
1266 | 43 |
qed "list_induct2"; |
969 | 44 |
|
45 |
(*Perform induction on xs. *) |
|
46 |
fun list_ind_tac a M = |
|
1266 | 47 |
EVERY [res_inst_tac [("l",a)] list_induct2 M, |
969 | 48 |
rename_last_tac a ["1"] (M+1)]; |
49 |
||
50 |
(*** Isomorphisms ***) |
|
51 |
||
52 |
goal SList.thy "inj(Rep_list)"; |
|
53 |
by (rtac inj_inverseI 1); |
|
54 |
by (rtac Rep_list_inverse 1); |
|
55 |
qed "inj_Rep_list"; |
|
56 |
||
57 |
goal SList.thy "inj_onto Abs_list (list(range Leaf))"; |
|
58 |
by (rtac inj_onto_inverseI 1); |
|
59 |
by (etac Abs_list_inverse 1); |
|
60 |
qed "inj_onto_Abs_list"; |
|
61 |
||
62 |
(** Distinctness of constructors **) |
|
63 |
||
64 |
goalw SList.thy list_con_defs "CONS M N ~= NIL"; |
|
65 |
by (rtac In1_not_In0 1); |
|
66 |
qed "CONS_not_NIL"; |
|
67 |
bind_thm ("NIL_not_CONS", (CONS_not_NIL RS not_sym)); |
|
68 |
||
69 |
bind_thm ("CONS_neq_NIL", (CONS_not_NIL RS notE)); |
|
70 |
val NIL_neq_CONS = sym RS CONS_neq_NIL; |
|
71 |
||
72 |
goalw SList.thy [Nil_def,Cons_def] "x # xs ~= Nil"; |
|
73 |
by (rtac (CONS_not_NIL RS (inj_onto_Abs_list RS inj_onto_contraD)) 1); |
|
74 |
by (REPEAT (resolve_tac (list.intrs @ [rangeI, Rep_list]) 1)); |
|
75 |
qed "Cons_not_Nil"; |
|
76 |
||
77 |
bind_thm ("Nil_not_Cons", (Cons_not_Nil RS not_sym)); |
|
78 |
||
1266 | 79 |
bind_thm ("Cons_neq_Nil2", (Cons_not_Nil RS notE)); |
80 |
val Nil_neq_Cons = sym RS Cons_neq_Nil2; |
|
969 | 81 |
|
82 |
(** Injectiveness of CONS and Cons **) |
|
83 |
||
84 |
goalw SList.thy [CONS_def] "(CONS K M=CONS L N) = (K=L & M=N)"; |
|
85 |
by (fast_tac (HOL_cs addSEs [Scons_inject, make_elim In1_inject]) 1); |
|
86 |
qed "CONS_CONS_eq"; |
|
87 |
||
88 |
bind_thm ("CONS_inject", (CONS_CONS_eq RS iffD1 RS conjE)); |
|
89 |
||
90 |
(*For reasoning about abstract list constructors*) |
|
91 |
val list_cs = set_cs addIs [Rep_list] @ list.intrs |
|
92 |
addSEs [CONS_neq_NIL,NIL_neq_CONS,CONS_inject] |
|
93 |
addSDs [inj_onto_Abs_list RS inj_ontoD, |
|
94 |
inj_Rep_list RS injD, Leaf_inject]; |
|
95 |
||
96 |
goalw SList.thy [Cons_def] "(x#xs=y#ys) = (x=y & xs=ys)"; |
|
97 |
by (fast_tac list_cs 1); |
|
98 |
qed "Cons_Cons_eq"; |
|
1266 | 99 |
bind_thm ("Cons_inject2", (Cons_Cons_eq RS iffD1 RS conjE)); |
969 | 100 |
|
101 |
val [major] = goal SList.thy "CONS M N: list(A) ==> M: A & N: list(A)"; |
|
102 |
by (rtac (major RS setup_induction) 1); |
|
103 |
by (etac list.induct 1); |
|
104 |
by (ALLGOALS (fast_tac list_cs)); |
|
105 |
qed "CONS_D"; |
|
106 |
||
107 |
val prems = goalw SList.thy [CONS_def,In1_def] |
|
108 |
"CONS M N: sexp ==> M: sexp & N: sexp"; |
|
109 |
by (cut_facts_tac prems 1); |
|
110 |
by (fast_tac (set_cs addSDs [Scons_D]) 1); |
|
111 |
qed "sexp_CONS_D"; |
|
112 |
||
113 |
||
114 |
(*Basic ss with constructors and their freeness*) |
|
1266 | 115 |
Addsimps ([Cons_not_Nil, Nil_not_Cons, Cons_Cons_eq, CONS_not_NIL, |
116 |
NIL_not_CONS, CONS_CONS_eq] @ list.intrs); |
|
969 | 117 |
|
118 |
goal SList.thy "!!N. N: list(A) ==> !M. N ~= CONS M N"; |
|
119 |
by (etac list.induct 1); |
|
1266 | 120 |
by (ALLGOALS Asm_simp_tac); |
969 | 121 |
qed "not_CONS_self"; |
122 |
||
123 |
goal SList.thy "!x. l ~= x#l"; |
|
124 |
by (list_ind_tac "l" 1); |
|
1266 | 125 |
by (ALLGOALS Asm_simp_tac); |
126 |
qed "not_Cons_self2"; |
|
969 | 127 |
|
128 |
||
129 |
goal SList.thy "(xs ~= []) = (? y ys. xs = y#ys)"; |
|
130 |
by(list_ind_tac "xs" 1); |
|
1266 | 131 |
by(Simp_tac 1); |
132 |
by(Asm_simp_tac 1); |
|
969 | 133 |
by(REPEAT(resolve_tac [exI,refl,conjI] 1)); |
1266 | 134 |
qed "neq_Nil_conv2"; |
969 | 135 |
|
136 |
(** Conversion rules for List_case: case analysis operator **) |
|
137 |
||
138 |
goalw SList.thy [List_case_def,NIL_def] "List_case c h NIL = c"; |
|
139 |
by (rtac Case_In0 1); |
|
140 |
qed "List_case_NIL"; |
|
141 |
||
142 |
goalw SList.thy [List_case_def,CONS_def] "List_case c h (CONS M N) = h M N"; |
|
1266 | 143 |
by (simp_tac (!simpset addsimps [Split,Case_In1]) 1); |
969 | 144 |
qed "List_case_CONS"; |
145 |
||
146 |
(*** List_rec -- by wf recursion on pred_sexp ***) |
|
147 |
||
148 |
(* The trancl(pred_sexp) is essential because pred_sexp_CONS_I1,2 would not |
|
149 |
hold if pred_sexp^+ were changed to pred_sexp. *) |
|
150 |
||
151 |
val List_rec_unfold = [List_rec_def, wf_pred_sexp RS wf_trancl] MRS def_wfrec |
|
152 |
|> standard; |
|
153 |
||
154 |
(** pred_sexp lemmas **) |
|
155 |
||
156 |
goalw SList.thy [CONS_def,In1_def] |
|
972
e61b058d58d2
changed syntax of tuples from <..., ...> to (..., ...)
clasohm
parents:
969
diff
changeset
|
157 |
"!!M. [| M: sexp; N: sexp |] ==> (M, CONS M N) : pred_sexp^+"; |
1266 | 158 |
by (Asm_simp_tac 1); |
969 | 159 |
qed "pred_sexp_CONS_I1"; |
160 |
||
161 |
goalw SList.thy [CONS_def,In1_def] |
|
972
e61b058d58d2
changed syntax of tuples from <..., ...> to (..., ...)
clasohm
parents:
969
diff
changeset
|
162 |
"!!M. [| M: sexp; N: sexp |] ==> (N, CONS M N) : pred_sexp^+"; |
1266 | 163 |
by (Asm_simp_tac 1); |
969 | 164 |
qed "pred_sexp_CONS_I2"; |
165 |
||
166 |
val [prem] = goal SList.thy |
|
972
e61b058d58d2
changed syntax of tuples from <..., ...> to (..., ...)
clasohm
parents:
969
diff
changeset
|
167 |
"(CONS M1 M2, N) : pred_sexp^+ ==> \ |
e61b058d58d2
changed syntax of tuples from <..., ...> to (..., ...)
clasohm
parents:
969
diff
changeset
|
168 |
\ (M1,N) : pred_sexp^+ & (M2,N) : pred_sexp^+"; |
969 | 169 |
by (rtac (prem RS (pred_sexp_subset_Sigma RS trancl_subset_Sigma RS |
170 |
subsetD RS SigmaE2)) 1); |
|
171 |
by (etac (sexp_CONS_D RS conjE) 1); |
|
172 |
by (REPEAT (ares_tac [conjI, pred_sexp_CONS_I1, pred_sexp_CONS_I2, |
|
173 |
prem RSN (2, trans_trancl RS transD)] 1)); |
|
174 |
qed "pred_sexp_CONS_D"; |
|
175 |
||
176 |
(** Conversion rules for List_rec **) |
|
177 |
||
178 |
goal SList.thy "List_rec NIL c h = c"; |
|
179 |
by (rtac (List_rec_unfold RS trans) 1); |
|
1266 | 180 |
by (simp_tac (!simpset addsimps [List_case_NIL]) 1); |
969 | 181 |
qed "List_rec_NIL"; |
182 |
||
183 |
goal SList.thy "!!M. [| M: sexp; N: sexp |] ==> \ |
|
184 |
\ List_rec (CONS M N) c h = h M N (List_rec N c h)"; |
|
185 |
by (rtac (List_rec_unfold RS trans) 1); |
|
1266 | 186 |
by (asm_simp_tac (!simpset addsimps [List_case_CONS, pred_sexp_CONS_I2]) 1); |
969 | 187 |
qed "List_rec_CONS"; |
188 |
||
189 |
(*** list_rec -- by List_rec ***) |
|
190 |
||
191 |
val Rep_list_in_sexp = |
|
192 |
[range_Leaf_subset_sexp RS list_subset_sexp, Rep_list] MRS subsetD; |
|
193 |
||
194 |
local |
|
1266 | 195 |
val list_rec_simps = [List_rec_NIL, List_rec_CONS, |
196 |
Abs_list_inverse, Rep_list_inverse, |
|
197 |
Rep_list, rangeI, inj_Leaf, Inv_f_f, |
|
198 |
sexp.LeafI, Rep_list_in_sexp] |
|
969 | 199 |
in |
200 |
val list_rec_Nil = prove_goalw SList.thy [list_rec_def, Nil_def] |
|
201 |
"list_rec Nil c h = c" |
|
1266 | 202 |
(fn _=> [simp_tac (!simpset addsimps list_rec_simps) 1]); |
969 | 203 |
|
204 |
val list_rec_Cons = prove_goalw SList.thy [list_rec_def, Cons_def] |
|
205 |
"list_rec (a#l) c h = h a l (list_rec l c h)" |
|
1266 | 206 |
(fn _=> [simp_tac (!simpset addsimps list_rec_simps) 1]); |
969 | 207 |
end; |
208 |
||
1266 | 209 |
Addsimps [List_rec_NIL, List_rec_CONS, list_rec_Nil, list_rec_Cons]; |
969 | 210 |
|
211 |
||
212 |
(*Type checking. Useful?*) |
|
213 |
val major::A_subset_sexp::prems = goal SList.thy |
|
214 |
"[| M: list(A); \ |
|
215 |
\ A<=sexp; \ |
|
216 |
\ c: C(NIL); \ |
|
217 |
\ !!x y r. [| x: A; y: list(A); r: C(y) |] ==> h x y r: C(CONS x y) \ |
|
218 |
\ |] ==> List_rec M c h : C(M :: 'a item)"; |
|
219 |
val sexp_ListA_I = A_subset_sexp RS list_subset_sexp RS subsetD; |
|
220 |
val sexp_A_I = A_subset_sexp RS subsetD; |
|
221 |
by (rtac (major RS list.induct) 1); |
|
1266 | 222 |
by (ALLGOALS(asm_simp_tac (!simpset addsimps ([sexp_A_I,sexp_ListA_I]@prems)))); |
969 | 223 |
qed "List_rec_type"; |
224 |
||
225 |
(** Generalized map functionals **) |
|
226 |
||
227 |
goalw SList.thy [Rep_map_def] "Rep_map f Nil = NIL"; |
|
228 |
by (rtac list_rec_Nil 1); |
|
229 |
qed "Rep_map_Nil"; |
|
230 |
||
231 |
goalw SList.thy [Rep_map_def] |
|
232 |
"Rep_map f (x#xs) = CONS (f x) (Rep_map f xs)"; |
|
233 |
by (rtac list_rec_Cons 1); |
|
234 |
qed "Rep_map_Cons"; |
|
235 |
||
236 |
goalw SList.thy [Rep_map_def] "!!f. (!!x. f(x): A) ==> Rep_map f xs: list(A)"; |
|
1266 | 237 |
by (rtac list_induct2 1); |
238 |
by(ALLGOALS Asm_simp_tac); |
|
969 | 239 |
qed "Rep_map_type"; |
240 |
||
241 |
goalw SList.thy [Abs_map_def] "Abs_map g NIL = Nil"; |
|
242 |
by (rtac List_rec_NIL 1); |
|
243 |
qed "Abs_map_NIL"; |
|
244 |
||
245 |
val prems = goalw SList.thy [Abs_map_def] |
|
246 |
"[| M: sexp; N: sexp |] ==> \ |
|
247 |
\ Abs_map g (CONS M N) = g(M) # Abs_map g N"; |
|
248 |
by (REPEAT (resolve_tac (List_rec_CONS::prems) 1)); |
|
249 |
qed "Abs_map_CONS"; |
|
250 |
||
251 |
(*These 2 rules ease the use of primitive recursion. NOTE USE OF == *) |
|
252 |
val [rew] = goal SList.thy |
|
253 |
"[| !!xs. f(xs) == list_rec xs c h |] ==> f([]) = c"; |
|
254 |
by (rewtac rew); |
|
255 |
by (rtac list_rec_Nil 1); |
|
256 |
qed "def_list_rec_Nil"; |
|
257 |
||
258 |
val [rew] = goal SList.thy |
|
259 |
"[| !!xs. f(xs) == list_rec xs c h |] ==> f(x#xs) = h x xs (f xs)"; |
|
260 |
by (rewtac rew); |
|
261 |
by (rtac list_rec_Cons 1); |
|
262 |
qed "def_list_rec_Cons"; |
|
263 |
||
264 |
fun list_recs def = |
|
265 |
[standard (def RS def_list_rec_Nil), |
|
266 |
standard (def RS def_list_rec_Cons)]; |
|
267 |
||
268 |
(*** Unfolding the basic combinators ***) |
|
269 |
||
270 |
val [null_Nil,null_Cons] = list_recs null_def; |
|
271 |
val [_,hd_Cons] = list_recs hd_def; |
|
272 |
val [_,tl_Cons] = list_recs tl_def; |
|
273 |
val [ttl_Nil,ttl_Cons] = list_recs ttl_def; |
|
1266 | 274 |
val [append_Nil3,append_Cons] = list_recs append_def; |
969 | 275 |
val [mem_Nil, mem_Cons] = list_recs mem_def; |
276 |
val [map_Nil,map_Cons] = list_recs map_def; |
|
277 |
val [list_case_Nil,list_case_Cons] = list_recs list_case_def; |
|
278 |
val [filter_Nil,filter_Cons] = list_recs filter_def; |
|
279 |
val [list_all_Nil,list_all_Cons] = list_recs list_all_def; |
|
280 |
||
1266 | 281 |
Addsimps |
282 |
[null_Nil, ttl_Nil, |
|
969 | 283 |
mem_Nil, mem_Cons, |
284 |
list_case_Nil, list_case_Cons, |
|
1266 | 285 |
append_Nil3, append_Cons, |
969 | 286 |
map_Nil, map_Cons, |
287 |
list_all_Nil, list_all_Cons, |
|
288 |
filter_Nil, filter_Cons]; |
|
289 |
||
290 |
||
291 |
(** @ - append **) |
|
292 |
||
293 |
goal SList.thy "(xs@ys)@zs = xs@(ys@zs)"; |
|
294 |
by(list_ind_tac "xs" 1); |
|
1266 | 295 |
by(ALLGOALS Asm_simp_tac); |
296 |
qed "append_assoc2"; |
|
969 | 297 |
|
298 |
goal SList.thy "xs @ [] = xs"; |
|
299 |
by(list_ind_tac "xs" 1); |
|
1266 | 300 |
by(ALLGOALS Asm_simp_tac); |
301 |
qed "append_Nil4"; |
|
969 | 302 |
|
303 |
(** mem **) |
|
304 |
||
305 |
goal SList.thy "x mem (xs@ys) = (x mem xs | x mem ys)"; |
|
306 |
by(list_ind_tac "xs" 1); |
|
1266 | 307 |
by(ALLGOALS(asm_simp_tac (!simpset setloop (split_tac [expand_if])))); |
308 |
qed "mem_append2"; |
|
969 | 309 |
|
310 |
goal SList.thy "x mem [x:xs.P(x)] = (x mem xs & P(x))"; |
|
311 |
by(list_ind_tac "xs" 1); |
|
1266 | 312 |
by(ALLGOALS(asm_simp_tac (!simpset setloop (split_tac [expand_if])))); |
313 |
qed "mem_filter2"; |
|
969 | 314 |
|
315 |
(** list_all **) |
|
316 |
||
317 |
goal SList.thy "(Alls x:xs.True) = True"; |
|
318 |
by(list_ind_tac "xs" 1); |
|
1266 | 319 |
by(ALLGOALS Asm_simp_tac); |
320 |
qed "list_all_True2"; |
|
969 | 321 |
|
322 |
goal SList.thy "list_all p (xs@ys) = (list_all p xs & list_all p ys)"; |
|
323 |
by(list_ind_tac "xs" 1); |
|
1266 | 324 |
by(ALLGOALS Asm_simp_tac); |
325 |
qed "list_all_conj2"; |
|
969 | 326 |
|
327 |
goal SList.thy "(Alls x:xs.P(x)) = (!x. x mem xs --> P(x))"; |
|
328 |
by(list_ind_tac "xs" 1); |
|
1266 | 329 |
by(ALLGOALS(asm_simp_tac (!simpset setloop (split_tac [expand_if])))); |
969 | 330 |
by(fast_tac HOL_cs 1); |
1266 | 331 |
qed "list_all_mem_conv2"; |
969 | 332 |
|
333 |
||
334 |
(** The functional "map" **) |
|
335 |
||
1266 | 336 |
Addsimps [Rep_map_Nil, Rep_map_Cons, Abs_map_NIL, Abs_map_CONS]; |
969 | 337 |
|
338 |
val [major,A_subset_sexp,minor] = goal SList.thy |
|
339 |
"[| M: list(A); A<=sexp; !!z. z: A ==> f(g(z)) = z |] \ |
|
340 |
\ ==> Rep_map f (Abs_map g M) = M"; |
|
341 |
by (rtac (major RS list.induct) 1); |
|
1266 | 342 |
by (ALLGOALS (asm_simp_tac (!simpset addsimps [sexp_A_I,sexp_ListA_I,minor]))); |
969 | 343 |
qed "Abs_map_inverse"; |
344 |
||
345 |
(*Rep_map_inverse is obtained via Abs_Rep_map and map_ident*) |
|
346 |
||
347 |
(** list_case **) |
|
348 |
||
349 |
goal SList.thy |
|
350 |
"P(list_case a f xs) = ((xs=[] --> P(a)) & \ |
|
351 |
\ (!y ys. xs=y#ys --> P(f y ys)))"; |
|
352 |
by(list_ind_tac "xs" 1); |
|
1266 | 353 |
by(ALLGOALS Asm_simp_tac); |
969 | 354 |
by(fast_tac HOL_cs 1); |
1266 | 355 |
qed "expand_list_case2"; |
969 | 356 |
|
357 |
||
358 |
(** Additional mapping lemmas **) |
|
359 |
||
360 |
goal SList.thy "map (%x.x) xs = xs"; |
|
361 |
by (list_ind_tac "xs" 1); |
|
1266 | 362 |
by (ALLGOALS Asm_simp_tac); |
363 |
qed "map_ident2"; |
|
969 | 364 |
|
365 |
goal SList.thy "map f (xs@ys) = map f xs @ map f ys"; |
|
366 |
by (list_ind_tac "xs" 1); |
|
1266 | 367 |
by (ALLGOALS Asm_simp_tac); |
368 |
qed "map_append2"; |
|
969 | 369 |
|
370 |
goalw SList.thy [o_def] "map (f o g) xs = map f (map g xs)"; |
|
371 |
by (list_ind_tac "xs" 1); |
|
1266 | 372 |
by (ALLGOALS Asm_simp_tac); |
373 |
qed "map_compose2"; |
|
969 | 374 |
|
375 |
goal SList.thy "!!f. (!!x. f(x): sexp) ==> \ |
|
376 |
\ Abs_map g (Rep_map f xs) = map (%t. g(f(t))) xs"; |
|
377 |
by (list_ind_tac "xs" 1); |
|
1266 | 378 |
by(ALLGOALS(asm_simp_tac(!simpset addsimps |
969 | 379 |
[Rep_map_type,list_sexp RS subsetD]))); |
380 |
qed "Abs_Rep_map"; |
|
381 |
||
1266 | 382 |
Addsimps [append_Nil4, map_ident2]; |