author | kleing |
Tue, 13 May 2003 08:59:21 +0200 | |
changeset 14024 | 213dcc39358f |
parent 13339 | 0f89104dd377 |
permissions | -rw-r--r-- |
12197 | 1 |
(* Title: ZF/UNITY/ListPlus.ML |
2 |
ID: $Id$ |
|
3 |
Author: Sidi O Ehmety, Cambridge University Computer Laboratory |
|
4 |
Copyright 2001 University of Cambridge |
|
5 |
||
6 |
More about lists |
|
7 |
||
8 |
*) |
|
9 |
||
10 |
(*** more theorems about lists ***) |
|
11 |
||
12 |
(** td and tl **) |
|
13 |
||
14 |
(** length **) |
|
15 |
||
16 |
Goal "xs:list(A) ==> length(xs)=0 <-> xs=Nil"; |
|
17 |
by (etac list.induct 1); |
|
18 |
by Auto_tac; |
|
19 |
qed "length_is_0_iff"; |
|
20 |
Addsimps [length_is_0_iff]; |
|
21 |
||
22 |
Goal "xs:list(A) ==> 0 = length(xs) <-> xs=Nil"; |
|
23 |
by (etac list.induct 1); |
|
24 |
by Auto_tac; |
|
25 |
qed "length_is_0_iff2"; |
|
26 |
Addsimps [length_is_0_iff2]; |
|
27 |
||
28 |
Goal "xs:list(A) ==> length(tl(xs)) = length(xs) #- 1"; |
|
29 |
by (etac list.induct 1); |
|
30 |
by Auto_tac; |
|
31 |
qed "length_tl"; |
|
32 |
Addsimps [length_tl]; |
|
33 |
||
34 |
Goal "xs:list(A) ==> 0<length(xs) <-> xs ~= Nil"; |
|
35 |
by (etac list.induct 1); |
|
36 |
by Auto_tac; |
|
37 |
qed "length_greater_0_iff"; |
|
38 |
||
39 |
Goal "xs:list(A) ==> length(xs)=succ(n) <-> (EX y ys. xs=Cons(y, ys) & length(ys)=n)"; |
|
40 |
by (etac list.induct 1); |
|
41 |
by Auto_tac; |
|
42 |
qed "length_succ_iff"; |
|
43 |
||
44 |
(** more theorems about append **) |
|
45 |
Goal "xs:list(A) ==> (xs@ys = Nil) <-> (xs=Nil & ys = Nil)"; |
|
46 |
by (etac list.induct 1); |
|
47 |
by Auto_tac; |
|
48 |
qed "append_is_Nil_iff"; |
|
49 |
Addsimps [append_is_Nil_iff]; |
|
50 |
||
51 |
Goal "xs:list(A) ==> (Nil = xs@ys) <-> (xs=Nil & ys = Nil)"; |
|
52 |
by (etac list.induct 1); |
|
53 |
by Auto_tac; |
|
54 |
qed "append_is_Nil_iff2"; |
|
55 |
Addsimps [append_is_Nil_iff2]; |
|
56 |
||
57 |
Goal "xs:list(A) ==> (xs@ys = xs) <-> (ys = Nil)"; |
|
58 |
by (etac list.induct 1); |
|
59 |
by Auto_tac; |
|
60 |
qed "append_left_is_self_iff"; |
|
61 |
Addsimps [append_left_is_self_iff]; |
|
62 |
||
63 |
Goal "xs:list(A) ==> (xs = xs@ys) <-> (ys = Nil)"; |
|
64 |
by (etac list.induct 1); |
|
65 |
by Auto_tac; |
|
66 |
qed "append_left_is_self_iff2"; |
|
67 |
Addsimps [append_left_is_self_iff2]; |
|
68 |
||
69 |
Goal "[| xs:list(A); ys:list(A); zs:list(A) |] ==> \ |
|
70 |
\ length(ys)=length(zs) --> (xs@ys=zs <-> (xs=Nil & ys=zs))"; |
|
71 |
by (etac list.induct 1); |
|
72 |
by (auto_tac (claset(), simpset() addsimps [length_app, length_type])); |
|
73 |
qed_spec_mp "append_left_is_Nil_iff"; |
|
74 |
Addsimps [append_left_is_Nil_iff]; |
|
75 |
||
76 |
Goal "[| xs:list(A); ys:list(A); zs:list(A) |] ==> \ |
|
77 |
\ length(ys)=length(zs) --> (zs=ys@xs <-> (xs=Nil & ys=zs))"; |
|
78 |
by (etac list.induct 1); |
|
79 |
by (auto_tac (claset(), simpset() addsimps [length_app, length_type])); |
|
80 |
qed_spec_mp "append_left_is_Nil_iff2"; |
|
81 |
Addsimps [append_left_is_Nil_iff2]; |
|
82 |
||
83 |
Goal "xs:list(A) ==> ALL ys:list(A). \ |
|
84 |
\ length(xs)=length(ys) --> (xs@us = ys@vs) <-> (xs=ys & us=vs)"; |
|
85 |
by (etac list.induct 1); |
|
86 |
by (Asm_simp_tac 1); |
|
87 |
by (Clarify_tac 1); |
|
88 |
by (eres_inst_tac [("a", "ys")] list.elim 1); |
|
89 |
by (ALLGOALS(Asm_full_simp_tac)); |
|
90 |
qed_spec_mp "append_eq_append_iff"; |
|
91 |
||
92 |
||
93 |
Goal "xs:list(A) ==> \ |
|
94 |
\ ALL ys:list(A). ALL us:list(A). ALL vs:list(A). \ |
|
95 |
\ length(us) = length(vs) --> (xs@us = ys@vs) --> (xs=ys & us=vs)"; |
|
96 |
by (induct_tac "xs" 1); |
|
97 |
by (ALLGOALS(Clarify_tac)); |
|
98 |
by (asm_full_simp_tac (simpset() addsimps [length_type, length_app]) 1); |
|
99 |
by (eres_inst_tac [("a", "ys")] list.elim 1); |
|
100 |
by (Asm_full_simp_tac 1); |
|
101 |
by (subgoal_tac "Cons(a, l) @ us =vs" 1); |
|
102 |
by (dtac (rotate_prems 4 (append_left_is_Nil_iff RS iffD1)) 1); |
|
103 |
by (ALLGOALS(asm_full_simp_tac (simpset() addsimps [length_type]))); |
|
104 |
by Auto_tac; |
|
105 |
qed_spec_mp "append_eq_append"; |
|
106 |
||
107 |
Goal "[| xs:list(A); ys:list(A); us:list(A); vs:list(A); length(us)=length(vs) |] \ |
|
108 |
\ ==> xs@us = ys@vs <-> (xs=ys & us=vs)"; |
|
109 |
by (rtac iffI 1); |
|
110 |
by (rtac append_eq_append 1); |
|
111 |
by Auto_tac; |
|
112 |
qed "append_eq_append_iff2"; |
|
113 |
Addsimps [append_eq_append_iff, append_eq_append_iff2]; |
|
114 |
||
115 |
Goal "[| xs:list(A); ys:list(A); zs:list(A) |] ==> xs@ys=xs@zs <-> ys=zs"; |
|
116 |
by (Asm_simp_tac 1); |
|
117 |
qed "append_self_iff"; |
|
118 |
Addsimps [append_self_iff]; |
|
119 |
||
120 |
Goal "[| xs:list(A); ys:list(A); zs:list(A) |] ==> ys@xs=zs@xs <-> ys=zs"; |
|
121 |
by (Asm_simp_tac 1); |
|
122 |
qed "append_self_iff2"; |
|
123 |
Addsimps [append_self_iff2]; |
|
124 |
||
125 |
(* Can also be proved from append_eq_append_iff2 |
|
126 |
if we add two more hypotheses x:A and y:A *) |
|
127 |
Goal "xs:list(A) ==> ALL ys:list(A). xs@[x] = ys@[y] <-> (xs = ys & x=y)"; |
|
128 |
by (etac list.induct 1); |
|
129 |
by (ALLGOALS(Clarify_tac)); |
|
130 |
by (ALLGOALS(eres_inst_tac [("a", "ys")] list.elim)); |
|
131 |
by Auto_tac; |
|
132 |
qed_spec_mp "append1_eq_iff"; |
|
133 |
Addsimps [append1_eq_iff]; |
|
134 |
||
135 |
Goal "[| xs:list(A); ys:list(A) |] ==> (xs@ys = ys) <-> (xs=Nil)"; |
|
136 |
by (Asm_simp_tac 1); |
|
137 |
qed "append_right_is_self_iff"; |
|
138 |
Addsimps [append_right_is_self_iff]; |
|
139 |
||
140 |
Goal "[| xs:list(A); ys:list(A) |] ==> (ys = xs@ys) <-> (xs=Nil)"; |
|
141 |
by (rtac iffI 1); |
|
142 |
by (dtac sym 1); |
|
143 |
by (ALLGOALS(Asm_full_simp_tac)); |
|
144 |
qed "append_right_is_self_iff2"; |
|
145 |
Addsimps [append_right_is_self_iff2]; |
|
146 |
||
147 |
Goal "xs:list(A) ==> xs ~= Nil --> hd(xs @ ys) = hd(xs)"; |
|
148 |
by (induct_tac "xs" 1); |
|
149 |
by Auto_tac; |
|
150 |
qed_spec_mp "hd_append"; |
|
151 |
Addsimps [hd_append]; |
|
152 |
||
153 |
Goal "xs:list(A) ==> xs~=Nil --> tl(xs @ ys) = tl(xs)@ys"; |
|
154 |
by (induct_tac "xs" 1); |
|
155 |
by Auto_tac; |
|
156 |
qed_spec_mp "tl_append"; |
|
157 |
Addsimps [tl_append]; |
|
158 |
||
159 |
(** rev **) |
|
160 |
Goal "xs:list(A) ==> (rev(xs) = Nil <-> xs = Nil)"; |
|
161 |
by (etac list.induct 1); |
|
162 |
by Auto_tac; |
|
163 |
qed "rev_is_Nil_iff"; |
|
164 |
Addsimps [rev_is_Nil_iff]; |
|
165 |
||
166 |
Goal "xs:list(A) ==> (Nil = rev(xs) <-> xs = Nil)"; |
|
167 |
by (etac list.induct 1); |
|
168 |
by Auto_tac; |
|
169 |
qed "Nil_is_rev_iff"; |
|
170 |
Addsimps [Nil_is_rev_iff]; |
|
171 |
||
172 |
Goal "xs:list(A) ==> ALL ys:list(A). rev(xs)=rev(ys) <-> xs=ys"; |
|
173 |
by (etac list.induct 1); |
|
174 |
by (Force_tac 1); |
|
175 |
by (Clarify_tac 1); |
|
176 |
by (eres_inst_tac [("a", "ys")] list.elim 1); |
|
177 |
by Auto_tac; |
|
178 |
qed_spec_mp "rev_is_rev_iff"; |
|
179 |
Addsimps [rev_is_rev_iff]; |
|
180 |
||
181 |
Goal "xs:list(A) ==> \ |
|
182 |
\ (xs=Nil --> P) --> (ALL ys:list(A). ALL y:A. xs =ys@[y] -->P)-->P"; |
|
183 |
by (etac list_append_induct 1); |
|
184 |
by Auto_tac; |
|
185 |
qed_spec_mp "rev_list_elim_aux"; |
|
186 |
||
187 |
bind_thm("rev_list_elim", impI RS ballI RS ballI RSN(3, rev_list_elim_aux)); |
|
188 |
||
189 |
(** more theorems about drop **) |
|
190 |
Goal "n:nat ==> ALL xs:list(A). length(drop(n, xs)) = length(xs) #- n"; |
|
191 |
by (etac nat_induct 1); |
|
192 |
by (ALLGOALS(Clarify_tac)); |
|
193 |
by (etac list.elim 2); |
|
194 |
by Auto_tac; |
|
195 |
qed_spec_mp "length_drop"; |
|
196 |
Addsimps [length_drop]; |
|
197 |
||
198 |
Goal "n:nat ==> ALL xs:list(A). length(xs) le n --> drop(n, xs)=Nil"; |
|
199 |
by (etac nat_induct 1); |
|
200 |
by (ALLGOALS(Clarify_tac)); |
|
201 |
by (etac list.elim 2); |
|
202 |
by Auto_tac; |
|
203 |
qed_spec_mp "drop_all"; |
|
204 |
Addsimps [drop_all]; |
|
205 |
||
206 |
(** take **) |
|
207 |
||
208 |
Goalw [take_def] |
|
209 |
"xs:list(A) ==> take(0, xs) = Nil"; |
|
210 |
by (etac list.induct 1); |
|
211 |
by Auto_tac; |
|
212 |
qed "take_0"; |
|
213 |
Addsimps [take_0]; |
|
214 |
||
215 |
Goalw [take_def] |
|
216 |
"n:nat ==> take(succ(n), Cons(a, xs)) = Cons(a, take(n, xs))"; |
|
217 |
by (Asm_simp_tac 1); |
|
218 |
qed "take_succ_Cons"; |
|
219 |
Addsimps [take_succ_Cons]; |
|
220 |
||
221 |
(* Needed for proving take_all *) |
|
222 |
Goalw [take_def] |
|
223 |
"n:nat ==> take(n, Nil) = Nil"; |
|
224 |
by Auto_tac; |
|
225 |
qed "take_Nil"; |
|
226 |
Addsimps [take_Nil]; |
|
227 |
||
228 |
Goal "n:nat ==> ALL xs:list(A). length(xs) le n --> take(n, xs) = xs"; |
|
229 |
by (etac nat_induct 1); |
|
230 |
by (ALLGOALS(Clarify_tac)); |
|
231 |
by (etac list.elim 2); |
|
232 |
by Auto_tac; |
|
233 |
qed_spec_mp "take_all"; |
|
234 |
Addsimps [take_all]; |
|
235 |
||
236 |
Goal "xs:list(A) ==> ALL n:nat. take(n, xs):list(A)"; |
|
237 |
by (etac list.induct 1); |
|
238 |
by (Clarify_tac 2); |
|
239 |
by (etac natE 2); |
|
240 |
by Auto_tac; |
|
241 |
qed_spec_mp "take_type"; |
|
242 |
||
243 |
Goal "xs:list(A) ==> \ |
|
244 |
\ ALL ys:list(A). ALL n:nat. take(n, xs @ ys) = \ |
|
245 |
\ take(n, xs) @ take(n #- length(xs), ys)"; |
|
246 |
by (etac list.induct 1); |
|
247 |
by (Clarify_tac 2); |
|
248 |
by (etac natE 2); |
|
249 |
by Auto_tac; |
|
250 |
qed_spec_mp "take_append"; |
|
251 |
Addsimps [take_append]; |
|
252 |
||
253 |
(** nth **) |
|
254 |
||
255 |
Goalw [nth_def] "nth(0, Cons(a, l))= a"; |
|
256 |
by Auto_tac; |
|
257 |
qed "nth_0"; |
|
258 |
AddIffs [nth_0]; |
|
259 |
||
260 |
Goalw [nth_def] |
|
261 |
"n:nat ==> nth(succ(n), Cons(a, l)) = nth(n, l)"; |
|
262 |
by (Asm_simp_tac 1); |
|
263 |
qed "nth_Cons"; |
|
264 |
Addsimps [nth_Cons]; |
|
265 |
||
266 |
Goal "xs:list(A) ==> ALL n:nat. n < length(xs) --> nth(n, xs):A"; |
|
267 |
by (etac list.induct 1); |
|
268 |
by (ALLGOALS(Clarify_tac)); |
|
269 |
by (etac natE 2); |
|
270 |
by (ALLGOALS(Asm_full_simp_tac)); |
|
271 |
qed_spec_mp "nth_type"; |
|
272 |
||
273 |
Goal |
|
274 |
"xs:list(A) ==> ALL n:nat. \ |
|
275 |
\ nth(n, xs @ ys) = (if n < length(xs) then nth(n,xs) \ |
|
276 |
\ else nth(n #- length(xs),ys))"; |
|
277 |
by (induct_tac "xs" 1); |
|
278 |
by (Clarify_tac 2); |
|
279 |
by (etac natE 2); |
|
280 |
by (ALLGOALS(Asm_full_simp_tac)); |
|
281 |
qed_spec_mp "nth_append"; |
|
282 |
||
283 |
(* Other theorems about lists *) |
|
284 |
||
285 |
Goal "xs:list(A) ==> (xs~=Nil) <-> (EX y:A. EX ys:list(A). xs=Cons(y,ys))"; |
|
286 |
by (induct_tac "xs" 1); |
|
287 |
by Auto_tac; |
|
288 |
qed "neq_Nil_iff"; |
|
289 |
||
290 |
Goalw [Ball_def] |
|
291 |
"k:nat ==> \ |
|
292 |
\ ALL xs:list(A). (ALL ys:list(A). k le length(xs) --> k le length(ys) --> \ |
|
293 |
\ (ALL i:nat. i < k --> nth(i,xs)= nth(i,ys))--> take(k, xs) = take(k,ys))"; |
|
294 |
by (induct_tac "k" 1); |
|
295 |
by (ALLGOALS (asm_simp_tac (simpset() addsimps |
|
296 |
[lt_succ_eq_0_disj, all_conj_distrib]))); |
|
297 |
by (Clarify_tac 1); |
|
298 |
(*Both lists must be non-empty*) |
|
299 |
by (case_tac "xa=Nil" 1); |
|
300 |
by (case_tac "xb=Nil" 2); |
|
301 |
by (Clarify_tac 1); |
|
302 |
by (ALLGOALS(asm_full_simp_tac (simpset() addsimps [neq_Nil_iff]))); |
|
303 |
by (Clarify_tac 1); |
|
304 |
(*prenexing's needed, not miniscoping*) |
|
305 |
by (Asm_simp_tac 1); |
|
306 |
by (rtac conjI 1); |
|
307 |
by (Force_tac 1); |
|
308 |
by (ALLGOALS (full_simp_tac (simpset() addsimps (all_simps RL [iff_sym]) |
|
309 |
delsimps (all_simps)))); |
|
310 |
by (dres_inst_tac [("x", "ys"), ("x1", "ysa")] (spec RS spec) 1); |
|
311 |
by Auto_tac; |
|
312 |
qed_spec_mp "nth_take_lemma"; |
|
313 |
||
314 |
Goal "[| xs:list(A); ys:list(A); length(xs) = length(ys); \ |
|
315 |
\ ALL i:nat. i < length(xs) --> nth(i,xs) = nth(i,ys) |] \ |
|
316 |
\ ==> xs = ys"; |
|
317 |
by (subgoal_tac "length(xs) le length(ys)" 1); |
|
318 |
by (forw_inst_tac [("ys", "ys")] (rotate_prems 1 nth_take_lemma) 1); |
|
319 |
by (ALLGOALS(Asm_simp_tac)); |
|
320 |
by (ALLGOALS (asm_full_simp_tac (simpset() addsimps [take_all]))); |
|
321 |
qed_spec_mp "nth_equalityI"; |
|
322 |
||
323 |
(*The famous take-lemma*) |
|
324 |
||
325 |
Goal "[| xs:list(A); ys:list(A); (ALL i:nat. take(i, xs) = take(i,ys)) |] ==> xs = ys"; |
|
326 |
by (case_tac "length(xs) le length(ys)" 1); |
|
327 |
by (dres_inst_tac [("x", "length(ys)")] bspec 1); |
|
328 |
by (dtac not_lt_imp_le 3); |
|
329 |
by (subgoal_tac "length(ys) le length(xs)" 5); |
|
330 |
by (res_inst_tac [("j", "succ(length(ys))")] le_trans 6); |
|
331 |
by (rtac leI 6); |
|
332 |
by (dres_inst_tac [("x", "length(xs)")] bspec 5); |
|
333 |
by (ALLGOALS(asm_full_simp_tac (simpset() addsimps [length_type,take_all]))); |
|
334 |
qed_spec_mp "take_equalityI"; |
|
335 |
||
336 |
(** More on lists **) |
|
337 |
||
13339
0f89104dd377
Fixed quantified variable name preservation for ball and bex (bounded quants)
paulson
parents:
12197
diff
changeset
|
338 |
Goal "n:nat ==> ALL i:nat. ALL xs:list(A). n #+ i le length(xs) \ |
0f89104dd377
Fixed quantified variable name preservation for ball and bex (bounded quants)
paulson
parents:
12197
diff
changeset
|
339 |
\ --> nth(i, drop(n, xs)) = nth(n #+ i, xs)"; |
12197 | 340 |
by (induct_tac "n" 1); |
13339
0f89104dd377
Fixed quantified variable name preservation for ball and bex (bounded quants)
paulson
parents:
12197
diff
changeset
|
341 |
by (Asm_full_simp_tac 1); |
12197 | 342 |
by (Clarify_tac 1); |
13339
0f89104dd377
Fixed quantified variable name preservation for ball and bex (bounded quants)
paulson
parents:
12197
diff
changeset
|
343 |
by (etac list.elim 1); |
0f89104dd377
Fixed quantified variable name preservation for ball and bex (bounded quants)
paulson
parents:
12197
diff
changeset
|
344 |
by Auto_tac; |
12197 | 345 |
qed_spec_mp "nth_drop"; |
346 |
||
347 |
||
348 |
||
349 |