| author | kleing |
| Tue, 11 Jun 2002 22:53:19 +0200 | |
| changeset 13209 | e62a6bd3f085 |
| parent 13175 | 81082cfa5618 |
| permissions | -rw-r--r-- |
| 1461 | 1 |
(* Title: ZF/List.ML |
| 0 | 2 |
ID: $Id$ |
| 1461 | 3 |
Author: Lawrence C Paulson, Cambridge University Computer Laboratory |
| 0 | 4 |
Copyright 1993 University of Cambridge |
5 |
||
6 |
Datatype definition of Lists |
|
7 |
*) |
|
8 |
||
| 516 | 9 |
(*** Aspects of the datatype definition ***) |
| 0 | 10 |
|
|
13175
81082cfa5618
new definition of "apply" and new simprule "beta_if"
paulson
parents:
13160
diff
changeset
|
11 |
Addsimps list.intrs; |
|
81082cfa5618
new definition of "apply" and new simprule "beta_if"
paulson
parents:
13160
diff
changeset
|
12 |
|
| 0 | 13 |
(*An elimination rule, for type-checking*) |
| 9907 | 14 |
bind_thm ("ConsE", list.mk_cases "Cons(a,l) : list(A)");
|
| 0 | 15 |
|
16 |
(*Proving freeness results*) |
|
| 9907 | 17 |
bind_thm ("Cons_iff", list.mk_free "Cons(a,l)=Cons(a',l') <-> a=a' & l=l'");
|
18 |
bind_thm ("Nil_Cons_iff", list.mk_free "~ Nil=Cons(a,l)");
|
|
| 0 | 19 |
|
| 5067 | 20 |
Goal "list(A) = {0} + (A * list(A))";
|
| 12789 | 21 |
by (blast_tac (claset() addSIs (map (rewrite_rule list.con_defs) list.intrs) |
22 |
addEs [rewrite_rule list.con_defs list.elim]) 1); |
|
| 760 | 23 |
qed "list_unfold"; |
| 435 | 24 |
|
| 0 | 25 |
(** Lemmas to justify using "list" in other recursive type definitions **) |
26 |
||
| 5137 | 27 |
Goalw list.defs "A<=B ==> list(A) <= list(B)"; |
| 0 | 28 |
by (rtac lfp_mono 1); |
| 516 | 29 |
by (REPEAT (rtac list.bnd_mono 1)); |
| 0 | 30 |
by (REPEAT (ares_tac (univ_mono::basic_monos) 1)); |
| 760 | 31 |
qed "list_mono"; |
| 0 | 32 |
|
33 |
(*There is a similar proof by list induction.*) |
|
| 5067 | 34 |
Goalw (list.defs@list.con_defs) "list(univ(A)) <= univ(A)"; |
| 0 | 35 |
by (rtac lfp_lowerbound 1); |
36 |
by (rtac (A_subset_univ RS univ_mono) 2); |
|
| 4091 | 37 |
by (blast_tac (claset() addSIs [zero_in_univ, Inl_in_univ, Inr_in_univ, |
| 6112 | 38 |
Pair_in_univ]) 1); |
| 760 | 39 |
qed "list_univ"; |
| 0 | 40 |
|
|
908
1f99a44c10cb
Updated comment about list_subset_univ and list_into_univ.
lcp
parents:
782
diff
changeset
|
41 |
(*These two theorems justify datatypes involving list(nat), list(A), ...*) |
| 6112 | 42 |
bind_thm ("list_subset_univ", [list_mono, list_univ] MRS subset_trans);
|
| 0 | 43 |
|
| 5137 | 44 |
Goal "[| l: list(A); A <= univ(B) |] ==> l: univ(B)"; |
| 435 | 45 |
by (REPEAT (ares_tac [list_subset_univ RS subsetD] 1)); |
| 760 | 46 |
qed "list_into_univ"; |
| 435 | 47 |
|
| 5321 | 48 |
val major::prems = Goal |
| 0 | 49 |
"[| l: list(A); \ |
|
15
6c6d2f6e3185
ex/{bin.ML,comb.ML,prop.ML}: replaced NewSext by Syntax.simple_sext
lcp
parents:
6
diff
changeset
|
50 |
\ c: C(Nil); \ |
|
6c6d2f6e3185
ex/{bin.ML,comb.ML,prop.ML}: replaced NewSext by Syntax.simple_sext
lcp
parents:
6
diff
changeset
|
51 |
\ !!x y. [| x: A; y: list(A) |] ==> h(x,y): C(Cons(x,y)) \ |
|
6c6d2f6e3185
ex/{bin.ML,comb.ML,prop.ML}: replaced NewSext by Syntax.simple_sext
lcp
parents:
6
diff
changeset
|
52 |
\ |] ==> list_case(c,h,l) : C(l)"; |
| 516 | 53 |
by (rtac (major RS list.induct) 1); |
|
6053
8a1059aa01f0
new inductive, datatype and primrec packages, etc.
paulson
parents:
5529
diff
changeset
|
54 |
by (ALLGOALS (asm_simp_tac (simpset() addsimps prems))); |
| 760 | 55 |
qed "list_case_type"; |
| 0 | 56 |
|
57 |
||
| 516 | 58 |
(*** List functions ***) |
59 |
||
| 5137 | 60 |
Goal "l: list(A) ==> tl(l) : list(A)"; |
| 6065 | 61 |
by (exhaust_tac "l" 1); |
| 4091 | 62 |
by (ALLGOALS (asm_simp_tac (simpset() addsimps list.intrs))); |
| 760 | 63 |
qed "tl_type"; |
| 516 | 64 |
|
65 |
(** drop **) |
|
66 |
||
| 6070 | 67 |
Goal "i:nat ==> drop(i, Nil) = Nil"; |
68 |
by (induct_tac "i" 1); |
|
| 2469 | 69 |
by (ALLGOALS Asm_simp_tac); |
| 760 | 70 |
qed "drop_Nil"; |
| 516 | 71 |
|
| 6070 | 72 |
Goal "i:nat ==> drop(succ(i), Cons(a,l)) = drop(i,l)"; |
| 2493 | 73 |
by (rtac sym 1); |
| 6070 | 74 |
by (induct_tac "i" 1); |
| 2469 | 75 |
by (Simp_tac 1); |
76 |
by (Asm_simp_tac 1); |
|
| 760 | 77 |
qed "drop_succ_Cons"; |
| 516 | 78 |
|
| 6070 | 79 |
Addsimps [drop_Nil, drop_succ_Cons]; |
| 2469 | 80 |
|
| 6070 | 81 |
Goal "[| i:nat; l: list(A) |] ==> drop(i,l) : list(A)"; |
82 |
by (induct_tac "i" 1); |
|
| 4091 | 83 |
by (ALLGOALS (asm_simp_tac (simpset() addsimps [tl_type]))); |
| 760 | 84 |
qed "drop_type"; |
| 516 | 85 |
|
| 6070 | 86 |
Delsimps [drop_SUCC]; |
87 |
||
| 516 | 88 |
|
|
6053
8a1059aa01f0
new inductive, datatype and primrec packages, etc.
paulson
parents:
5529
diff
changeset
|
89 |
(** Type checking -- proved by induction, as usual **) |
| 516 | 90 |
|
| 5321 | 91 |
val prems = Goal |
| 516 | 92 |
"[| l: list(A); \ |
93 |
\ c: C(Nil); \ |
|
94 |
\ !!x y r. [| x:A; y: list(A); r: C(y) |] ==> h(x,y,r): C(Cons(x,y)) \ |
|
|
6053
8a1059aa01f0
new inductive, datatype and primrec packages, etc.
paulson
parents:
5529
diff
changeset
|
95 |
\ |] ==> list_rec(c,h,l) : C(l)"; |
| 6065 | 96 |
by (cut_facts_tac prems 1); |
97 |
by (induct_tac "l" 1); |
|
| 4091 | 98 |
by (ALLGOALS (asm_simp_tac (simpset() addsimps prems))); |
| 760 | 99 |
qed "list_rec_type"; |
| 516 | 100 |
|
101 |
(** map **) |
|
102 |
||
| 11145 | 103 |
val prems = Goalw [thm "map_list_def"] |
| 516 | 104 |
"[| l: list(A); !!x. x: A ==> h(x): B |] ==> map(h,l) : list(B)"; |
105 |
by (REPEAT (ares_tac (prems @ list.intrs @ [list_rec_type]) 1)); |
|
| 760 | 106 |
qed "map_type"; |
| 516 | 107 |
|
| 5321 | 108 |
Goal "l: list(A) ==> map(h,l) : list({h(u). u:A})";
|
109 |
by (etac map_type 1); |
|
| 516 | 110 |
by (etac RepFunI 1); |
| 760 | 111 |
qed "map_type2"; |
| 516 | 112 |
|
113 |
(** length **) |
|
114 |
||
| 11145 | 115 |
Goalw [thm "length_list_def"] |
|
5147
825877190618
More tidying and removal of "\!\!... from Goal commands
paulson
parents:
5137
diff
changeset
|
116 |
"l: list(A) ==> length(l) : nat"; |
| 516 | 117 |
by (REPEAT (ares_tac [list_rec_type, nat_0I, nat_succI] 1)); |
| 760 | 118 |
qed "length_type"; |
| 516 | 119 |
|
120 |
(** app **) |
|
121 |
||
| 11145 | 122 |
Goalw [thm "op @_list_def"] |
|
5147
825877190618
More tidying and removal of "\!\!... from Goal commands
paulson
parents:
5137
diff
changeset
|
123 |
"[| xs: list(A); ys: list(A) |] ==> xs@ys : list(A)"; |
| 516 | 124 |
by (REPEAT (ares_tac [list_rec_type, list.Cons_I] 1)); |
| 760 | 125 |
qed "app_type"; |
| 516 | 126 |
|
127 |
(** rev **) |
|
128 |
||
| 11145 | 129 |
Goalw [thm "rev_list_def"] |
|
5147
825877190618
More tidying and removal of "\!\!... from Goal commands
paulson
parents:
5137
diff
changeset
|
130 |
"xs: list(A) ==> rev(xs) : list(A)"; |
| 516 | 131 |
by (REPEAT (ares_tac (list.intrs @ [list_rec_type, app_type]) 1)); |
| 760 | 132 |
qed "rev_type"; |
| 516 | 133 |
|
134 |
||
135 |
(** flat **) |
|
136 |
||
| 11145 | 137 |
Goalw [thm "flat_list_def"] |
|
5147
825877190618
More tidying and removal of "\!\!... from Goal commands
paulson
parents:
5137
diff
changeset
|
138 |
"ls: list(list(A)) ==> flat(ls) : list(A)"; |
| 516 | 139 |
by (REPEAT (ares_tac (list.intrs @ [list_rec_type, app_type]) 1)); |
| 760 | 140 |
qed "flat_type"; |
| 516 | 141 |
|
142 |
||
| 1926 | 143 |
(** set_of_list **) |
144 |
||
| 11145 | 145 |
Goalw [thm "set_of_list_list_def"] |
|
5147
825877190618
More tidying and removal of "\!\!... from Goal commands
paulson
parents:
5137
diff
changeset
|
146 |
"l: list(A) ==> set_of_list(l) : Pow(A)"; |
| 2033 | 147 |
by (etac list_rec_type 1); |
| 3016 | 148 |
by (ALLGOALS (Blast_tac)); |
| 1926 | 149 |
qed "set_of_list_type"; |
150 |
||
|
5147
825877190618
More tidying and removal of "\!\!... from Goal commands
paulson
parents:
5137
diff
changeset
|
151 |
Goal "xs: list(A) ==> \ |
| 1926 | 152 |
\ set_of_list (xs@ys) = set_of_list(xs) Un set_of_list(ys)"; |
153 |
by (etac list.induct 1); |
|
| 4091 | 154 |
by (ALLGOALS (asm_simp_tac (simpset() addsimps [Un_cons]))); |
| 1926 | 155 |
qed "set_of_list_append"; |
156 |
||
157 |
||
| 516 | 158 |
(** list_add **) |
159 |
||
| 11145 | 160 |
Goalw [thm "list_add_list_def"] |
|
5147
825877190618
More tidying and removal of "\!\!... from Goal commands
paulson
parents:
5137
diff
changeset
|
161 |
"xs: list(nat) ==> list_add(xs) : nat"; |
| 516 | 162 |
by (REPEAT (ares_tac [list_rec_type, nat_0I, add_type] 1)); |
| 760 | 163 |
qed "list_add_type"; |
| 516 | 164 |
|
| 9907 | 165 |
bind_thms ("list_typechecks",
|
| 516 | 166 |
list.intrs @ |
167 |
[list_rec_type, map_type, map_type2, app_type, length_type, |
|
| 9907 | 168 |
rev_type, flat_type, list_add_type]); |
| 516 | 169 |
|
| 6153 | 170 |
AddTCs list_typechecks; |
| 516 | 171 |
|
172 |
||
173 |
(*** theorems about map ***) |
|
174 |
||
| 5321 | 175 |
Goal "l: list(A) ==> map(%u. u, l) = l"; |
| 6065 | 176 |
by (induct_tac "l" 1); |
| 3016 | 177 |
by (ALLGOALS Asm_simp_tac); |
| 760 | 178 |
qed "map_ident"; |
| 6112 | 179 |
Addsimps [map_ident]; |
| 516 | 180 |
|
| 5321 | 181 |
Goal "l: list(A) ==> map(h, map(j,l)) = map(%u. h(j(u)), l)"; |
| 6065 | 182 |
by (induct_tac "l" 1); |
| 3016 | 183 |
by (ALLGOALS Asm_simp_tac); |
| 760 | 184 |
qed "map_compose"; |
| 516 | 185 |
|
| 5321 | 186 |
Goal "xs: list(A) ==> map(h, xs@ys) = map(h,xs) @ map(h,ys)"; |
| 6065 | 187 |
by (induct_tac "xs" 1); |
| 3016 | 188 |
by (ALLGOALS Asm_simp_tac); |
| 760 | 189 |
qed "map_app_distrib"; |
| 516 | 190 |
|
| 5321 | 191 |
Goal "ls: list(list(A)) ==> map(h, flat(ls)) = flat(map(map(h),ls))"; |
| 6065 | 192 |
by (induct_tac "ls" 1); |
| 4091 | 193 |
by (ALLGOALS (asm_simp_tac (simpset() addsimps [map_app_distrib]))); |
| 760 | 194 |
qed "map_flat"; |
| 516 | 195 |
|
| 5321 | 196 |
Goal "l: list(A) ==> \ |
|
6053
8a1059aa01f0
new inductive, datatype and primrec packages, etc.
paulson
parents:
5529
diff
changeset
|
197 |
\ list_rec(c, d, map(h,l)) = \ |
|
8a1059aa01f0
new inductive, datatype and primrec packages, etc.
paulson
parents:
5529
diff
changeset
|
198 |
\ list_rec(c, %x xs r. d(h(x), map(h,xs), r), l)"; |
| 6065 | 199 |
by (induct_tac "l" 1); |
| 3016 | 200 |
by (ALLGOALS Asm_simp_tac); |
| 760 | 201 |
qed "list_rec_map"; |
| 516 | 202 |
|
203 |
(** theorems about list(Collect(A,P)) -- used in ex/term.ML **) |
|
204 |
||
205 |
(* c : list(Collect(B,P)) ==> c : list(B) *) |
|
| 6112 | 206 |
bind_thm ("list_CollectD", Collect_subset RS list_mono RS subsetD);
|
| 516 | 207 |
|
| 5321 | 208 |
Goal "l: list({x:A. h(x)=j(x)}) ==> map(h,l) = map(j,l)";
|
| 6065 | 209 |
by (induct_tac "l" 1); |
| 3016 | 210 |
by (ALLGOALS Asm_simp_tac); |
| 760 | 211 |
qed "map_list_Collect"; |
| 516 | 212 |
|
213 |
(*** theorems about length ***) |
|
214 |
||
| 5321 | 215 |
Goal "xs: list(A) ==> length(map(h,xs)) = length(xs)"; |
| 6065 | 216 |
by (induct_tac "xs" 1); |
| 3016 | 217 |
by (ALLGOALS Asm_simp_tac); |
| 760 | 218 |
qed "length_map"; |
| 516 | 219 |
|
|
9491
1a36151ee2fc
natify, a coercion to reduce the number of type constraints in arithmetic
paulson
parents:
6153
diff
changeset
|
220 |
Goal "[| xs: list(A); ys: list(A) |] \ |
|
1a36151ee2fc
natify, a coercion to reduce the number of type constraints in arithmetic
paulson
parents:
6153
diff
changeset
|
221 |
\ ==> length(xs@ys) = length(xs) #+ length(ys)"; |
| 6065 | 222 |
by (induct_tac "xs" 1); |
| 3016 | 223 |
by (ALLGOALS Asm_simp_tac); |
| 760 | 224 |
qed "length_app"; |
| 516 | 225 |
|
| 5321 | 226 |
Goal "xs: list(A) ==> length(rev(xs)) = length(xs)"; |
| 6065 | 227 |
by (induct_tac "xs" 1); |
| 6112 | 228 |
by (ALLGOALS (asm_simp_tac (simpset() addsimps [length_app]))); |
| 760 | 229 |
qed "length_rev"; |
| 516 | 230 |
|
| 5321 | 231 |
Goal "ls: list(list(A)) ==> length(flat(ls)) = list_add(map(length,ls))"; |
| 6065 | 232 |
by (induct_tac "ls" 1); |
| 4091 | 233 |
by (ALLGOALS (asm_simp_tac (simpset() addsimps [length_app]))); |
| 760 | 234 |
qed "length_flat"; |
| 516 | 235 |
|
236 |
(** Length and drop **) |
|
237 |
||
238 |
(*Lemma for the inductive step of drop_length*) |
|
|
5147
825877190618
More tidying and removal of "\!\!... from Goal commands
paulson
parents:
5137
diff
changeset
|
239 |
Goal "xs: list(A) ==> \ |
| 516 | 240 |
\ ALL x. EX z zs. drop(length(xs), Cons(x,xs)) = Cons(z,zs)"; |
241 |
by (etac list.induct 1); |
|
| 2469 | 242 |
by (ALLGOALS Asm_simp_tac); |
| 6112 | 243 |
qed_spec_mp "drop_length_Cons"; |
| 516 | 244 |
|
| 6112 | 245 |
Goal "l: list(A) ==> ALL i:length(l). (EX z zs. drop(i,l) = Cons(z,zs))"; |
| 516 | 246 |
by (etac list.induct 1); |
| 2469 | 247 |
by (ALLGOALS Asm_simp_tac); |
| 6112 | 248 |
by Safe_tac; |
| 516 | 249 |
by (etac drop_length_Cons 1); |
250 |
by (rtac natE 1); |
|
251 |
by (etac ([asm_rl, length_type, Ord_nat] MRS Ord_trans) 1); |
|
252 |
by (assume_tac 1); |
|
| 3016 | 253 |
by (ALLGOALS Asm_simp_tac); |
| 4091 | 254 |
by (ALLGOALS (blast_tac (claset() addIs [succ_in_naturalD, length_type]))); |
| 6112 | 255 |
qed_spec_mp "drop_length"; |
| 516 | 256 |
|
257 |
||
258 |
(*** theorems about app ***) |
|
259 |
||
| 5321 | 260 |
Goal "xs: list(A) ==> xs@Nil=xs"; |
261 |
by (etac list.induct 1); |
|
| 3016 | 262 |
by (ALLGOALS Asm_simp_tac); |
| 760 | 263 |
qed "app_right_Nil"; |
| 6112 | 264 |
Addsimps [app_right_Nil]; |
| 516 | 265 |
|
| 5321 | 266 |
Goal "xs: list(A) ==> (xs@ys)@zs = xs@(ys@zs)"; |
| 6065 | 267 |
by (induct_tac "xs" 1); |
| 3016 | 268 |
by (ALLGOALS Asm_simp_tac); |
| 760 | 269 |
qed "app_assoc"; |
| 516 | 270 |
|
| 5321 | 271 |
Goal "ls: list(list(A)) ==> flat(ls@ms) = flat(ls)@flat(ms)"; |
| 6065 | 272 |
by (induct_tac "ls" 1); |
| 4091 | 273 |
by (ALLGOALS (asm_simp_tac (simpset() addsimps [app_assoc]))); |
| 760 | 274 |
qed "flat_app_distrib"; |
| 516 | 275 |
|
276 |
(*** theorems about rev ***) |
|
277 |
||
| 5321 | 278 |
Goal "l: list(A) ==> rev(map(h,l)) = map(h,rev(l))"; |
| 6065 | 279 |
by (induct_tac "l" 1); |
| 4091 | 280 |
by (ALLGOALS (asm_simp_tac (simpset() addsimps [map_app_distrib]))); |
| 760 | 281 |
qed "rev_map_distrib"; |
| 516 | 282 |
|
283 |
(*Simplifier needs the premises as assumptions because rewriting will not |
|
284 |
instantiate the variable ?A in the rules' typing conditions; note that |
|
285 |
rev_type does not instantiate ?A. Only the premises do. |
|
286 |
*) |
|
|
5147
825877190618
More tidying and removal of "\!\!... from Goal commands
paulson
parents:
5137
diff
changeset
|
287 |
Goal "[| xs: list(A); ys: list(A) |] ==> rev(xs@ys) = rev(ys)@rev(xs)"; |
| 516 | 288 |
by (etac list.induct 1); |
| 6112 | 289 |
by (ALLGOALS (asm_simp_tac (simpset() addsimps [app_assoc]))); |
| 760 | 290 |
qed "rev_app_distrib"; |
| 516 | 291 |
|
| 5321 | 292 |
Goal "l: list(A) ==> rev(rev(l))=l"; |
| 6065 | 293 |
by (induct_tac "l" 1); |
| 4091 | 294 |
by (ALLGOALS (asm_simp_tac (simpset() addsimps [rev_app_distrib]))); |
| 760 | 295 |
qed "rev_rev_ident"; |
| 6112 | 296 |
Addsimps [rev_rev_ident]; |
| 516 | 297 |
|
| 5321 | 298 |
Goal "ls: list(list(A)) ==> rev(flat(ls)) = flat(map(rev,rev(ls)))"; |
| 6065 | 299 |
by (induct_tac "ls" 1); |
| 6112 | 300 |
by (ALLGOALS |
301 |
(asm_simp_tac (simpset() addsimps |
|
302 |
[map_app_distrib, flat_app_distrib, rev_app_distrib]))); |
|
| 760 | 303 |
qed "rev_flat"; |
| 516 | 304 |
|
305 |
||
306 |
(*** theorems about list_add ***) |
|
307 |
||
| 5321 | 308 |
Goal "[| xs: list(nat); ys: list(nat) |] ==> \ |
| 516 | 309 |
\ list_add(xs@ys) = list_add(ys) #+ list_add(xs)"; |
| 6065 | 310 |
by (induct_tac "xs" 1); |
| 9548 | 311 |
by (ALLGOALS Asm_simp_tac); |
| 760 | 312 |
qed "list_add_app"; |
| 516 | 313 |
|
| 5321 | 314 |
Goal "l: list(nat) ==> list_add(rev(l)) = list_add(l)"; |
| 6065 | 315 |
by (induct_tac "l" 1); |
| 9548 | 316 |
by (ALLGOALS (asm_simp_tac (simpset() addsimps [list_add_app]))); |
| 760 | 317 |
qed "list_add_rev"; |
| 516 | 318 |
|
| 5321 | 319 |
Goal "ls: list(list(nat)) ==> list_add(flat(ls)) = list_add(map(list_add,ls))"; |
| 6065 | 320 |
by (induct_tac "ls" 1); |
| 4091 | 321 |
by (ALLGOALS (asm_simp_tac (simpset() addsimps [list_add_app]))); |
| 516 | 322 |
by (REPEAT (ares_tac [refl, list_add_type, map_type, add_commute] 1)); |
| 760 | 323 |
qed "list_add_flat"; |
| 516 | 324 |
|
325 |
(** New induction rule **) |
|
326 |
||
| 5321 | 327 |
val major::prems = Goal |
| 516 | 328 |
"[| l: list(A); \ |
329 |
\ P(Nil); \ |
|
330 |
\ !!x y. [| x: A; y: list(A); P(y) |] ==> P(y @ [x]) \ |
|
331 |
\ |] ==> P(l)"; |
|
332 |
by (rtac (major RS rev_rev_ident RS subst) 1); |
|
333 |
by (rtac (major RS rev_type RS list.induct) 1); |
|
| 4091 | 334 |
by (ALLGOALS (asm_simp_tac (simpset() addsimps prems))); |
| 760 | 335 |
qed "list_append_induct"; |
| 516 | 336 |
|
| 12789 | 337 |
|
338 |
(*** Thanks to Sidi Ehmety for these results about min, take, etc. ***) |
|
339 |
||
340 |
(** min **) |
|
341 |
(* Min theorems are also true for i, j ordinals *) |
|
342 |
Goalw [min_def] "[| i:nat; j:nat |] ==> min(i,j)=min(j,i)"; |
|
343 |
by (auto_tac (claset() addSDs [not_lt_imp_le] |
|
344 |
addDs [lt_not_sym] |
|
345 |
addIs [le_anti_sym], simpset())); |
|
346 |
qed "min_sym"; |
|
347 |
||
348 |
Goalw [min_def] "[| i:nat; j:nat |] ==> min(i,j):nat"; |
|
349 |
by Auto_tac; |
|
350 |
qed "min_type"; |
|
351 |
AddTCs [min_type]; |
|
352 |
Addsimps [min_type]; |
|
353 |
||
354 |
Goalw [min_def] "i:nat ==> min(0,i) = 0"; |
|
355 |
by (auto_tac (claset() addDs [not_lt_imp_le], simpset())); |
|
356 |
qed "min_0"; |
|
357 |
Addsimps [min_0]; |
|
358 |
||
359 |
Goalw [min_def] "i:nat ==> min(i, 0) = 0"; |
|
360 |
by (auto_tac (claset() addDs [not_lt_imp_le], simpset())); |
|
361 |
qed "min_02"; |
|
362 |
Addsimps [min_02]; |
|
363 |
||
364 |
Goalw [min_def] "[| i:nat; j:nat; k:nat |] ==> i<min(j,k) <-> i<j & i<k"; |
|
365 |
by (auto_tac (claset() addSDs [not_lt_imp_le] |
|
366 |
addIs [lt_trans2, lt_trans], simpset())); |
|
367 |
qed "lt_min_iff"; |
|
368 |
||
369 |
Goalw [min_def] |
|
370 |
"[| i:nat; j:nat |] ==> min(succ(i), succ(j))= succ(min(i, j))"; |
|
371 |
by Auto_tac; |
|
372 |
qed "min_succ_succ"; |
|
373 |
Addsimps [min_succ_succ]; |
|
374 |
||
375 |
(*** more theorems about lists ***) |
|
376 |
||
377 |
Goal "xs:list(A) ==> (xs~=Nil)<->(EX y:A. EX ys:list(A). xs=Cons(y,ys))"; |
|
378 |
by (induct_tac "xs" 1); |
|
379 |
by Auto_tac; |
|
380 |
qed "neq_Nil_iff"; |
|
381 |
||
382 |
(** filter **) |
|
383 |
||
384 |
Goal "xs:list(A) ==> filter(P, xs@ys) = filter(P, xs) @ filter(P, ys)"; |
|
385 |
by (induct_tac "xs" 1); |
|
386 |
by Auto_tac; |
|
387 |
qed "filter_append"; |
|
388 |
Addsimps [filter_append]; |
|
389 |
||
390 |
Goal "xs:list(A) ==> filter(P, xs):list(A)"; |
|
391 |
by (induct_tac "xs" 1); |
|
392 |
by Auto_tac; |
|
393 |
qed "filter_type"; |
|
394 |
||
395 |
Goal "xs:list(A) ==> length(filter(P, xs)) le length(xs)"; |
|
396 |
by (induct_tac "xs" 1); |
|
397 |
by Auto_tac; |
|
398 |
by (res_inst_tac [("j", "length(l)")] le_trans 1);
|
|
399 |
by (auto_tac (claset(), simpset() addsimps [le_iff])); |
|
400 |
qed "length_filter"; |
|
401 |
||
402 |
Goal "xs:list(A) ==> set_of_list(filter(P,xs)) <= set_of_list(xs)"; |
|
403 |
by (induct_tac "xs" 1); |
|
404 |
by Auto_tac; |
|
405 |
qed "filter_is_subset"; |
|
406 |
||
407 |
Goal "xs:list(A) ==> filter(%p. False, xs) = Nil"; |
|
408 |
by (induct_tac "xs" 1); |
|
409 |
by Auto_tac; |
|
410 |
qed "filter_False"; |
|
411 |
||
412 |
Goal "xs:list(A) ==> filter(%p. True, xs) = xs"; |
|
413 |
by (induct_tac "xs" 1); |
|
414 |
by Auto_tac; |
|
415 |
qed "filter_True"; |
|
416 |
Addsimps [filter_False, filter_True]; |
|
417 |
||
418 |
(** length **) |
|
419 |
||
420 |
Goal "xs:list(A) ==> length(xs)=0 <-> xs=Nil"; |
|
421 |
by (etac list.induct 1); |
|
422 |
by Auto_tac; |
|
423 |
qed "length_is_0_iff"; |
|
424 |
Addsimps [length_is_0_iff]; |
|
425 |
||
426 |
Goal "xs:list(A) ==> 0 = length(xs) <-> xs=Nil"; |
|
427 |
by (etac list.induct 1); |
|
428 |
by Auto_tac; |
|
429 |
qed "length_is_0_iff2"; |
|
430 |
Addsimps [length_is_0_iff2]; |
|
431 |
||
432 |
Goal "xs:list(A) ==> length(tl(xs)) = length(xs) #- 1"; |
|
433 |
by (etac list.induct 1); |
|
434 |
by Auto_tac; |
|
435 |
qed "length_tl"; |
|
436 |
Addsimps [length_tl]; |
|
437 |
||
438 |
Goal "xs:list(A) ==> 0<length(xs) <-> xs ~= Nil"; |
|
439 |
by (etac list.induct 1); |
|
440 |
by Auto_tac; |
|
441 |
qed "length_greater_0_iff"; |
|
442 |
||
443 |
Goal "xs:list(A) ==> length(xs)=succ(n) <-> (EX y ys. xs=Cons(y, ys) & length(ys)=n)"; |
|
444 |
by (etac list.induct 1); |
|
445 |
by Auto_tac; |
|
446 |
qed "length_succ_iff"; |
|
447 |
||
448 |
(** more theorems about append **) |
|
449 |
||
450 |
Goal "xs:list(A) ==> (xs@ys = Nil) <-> (xs=Nil & ys = Nil)"; |
|
451 |
by (etac list.induct 1); |
|
452 |
by Auto_tac; |
|
453 |
qed "append_is_Nil_iff"; |
|
454 |
Addsimps [append_is_Nil_iff]; |
|
455 |
||
456 |
Goal "xs:list(A) ==> (Nil = xs@ys) <-> (xs=Nil & ys = Nil)"; |
|
457 |
by (etac list.induct 1); |
|
458 |
by Auto_tac; |
|
459 |
qed "append_is_Nil_iff2"; |
|
460 |
Addsimps [append_is_Nil_iff2]; |
|
461 |
||
462 |
Goal "xs:list(A) ==> (xs@ys = xs) <-> (ys = Nil)"; |
|
463 |
by (etac list.induct 1); |
|
464 |
by Auto_tac; |
|
465 |
qed "append_left_is_self_iff"; |
|
466 |
Addsimps [append_left_is_self_iff]; |
|
467 |
||
468 |
Goal "xs:list(A) ==> (xs = xs@ys) <-> (ys = Nil)"; |
|
469 |
by (etac list.induct 1); |
|
470 |
by Auto_tac; |
|
471 |
qed "append_left_is_self_iff2"; |
|
472 |
Addsimps [append_left_is_self_iff2]; |
|
473 |
||
| 13143 | 474 |
(*TOO SLOW as a default simprule!*) |
| 12789 | 475 |
Goal "[| xs:list(A); ys:list(A); zs:list(A) |] ==> \ |
476 |
\ length(ys)=length(zs) --> (xs@ys=zs <-> (xs=Nil & ys=zs))"; |
|
477 |
by (etac list.induct 1); |
|
478 |
by (auto_tac (claset(), simpset() addsimps [length_app])); |
|
479 |
qed_spec_mp "append_left_is_Nil_iff"; |
|
480 |
||
| 13143 | 481 |
(*TOO SLOW as a default simprule!*) |
| 12789 | 482 |
Goal "[| xs:list(A); ys:list(A); zs:list(A) |] ==> \ |
483 |
\ length(ys)=length(zs) --> (zs=ys@xs <-> (xs=Nil & ys=zs))"; |
|
484 |
by (etac list.induct 1); |
|
485 |
by (auto_tac (claset(), simpset() addsimps [length_app])); |
|
486 |
qed_spec_mp "append_left_is_Nil_iff2"; |
|
487 |
||
488 |
Goal "xs:list(A) ==> ALL ys:list(A). \ |
|
489 |
\ length(xs)=length(ys) --> (xs@us = ys@vs) <-> (xs=ys & us=vs)"; |
|
490 |
by (etac list.induct 1); |
|
491 |
by (Asm_simp_tac 1); |
|
492 |
by (Clarify_tac 1); |
|
493 |
by (eres_inst_tac [("a", "ys")] list.elim 1);
|
|
494 |
by (ALLGOALS(Asm_full_simp_tac)); |
|
495 |
qed_spec_mp "append_eq_append_iff"; |
|
496 |
||
497 |
||
498 |
Goal "xs:list(A) ==> \ |
|
499 |
\ ALL ys:list(A). ALL us:list(A). ALL vs:list(A). \ |
|
500 |
\ length(us) = length(vs) --> (xs@us = ys@vs) --> (xs=ys & us=vs)"; |
|
501 |
by (induct_tac "xs" 1); |
|
502 |
by (ALLGOALS(Clarify_tac)); |
|
503 |
by (asm_full_simp_tac (simpset() addsimps [length_app]) 1); |
|
504 |
by (eres_inst_tac [("a", "ys")] list.elim 1);
|
|
505 |
by (Asm_full_simp_tac 1); |
|
506 |
by (subgoal_tac "Cons(a, l) @ us =vs" 1); |
|
507 |
by (dtac (rotate_prems 4 (append_left_is_Nil_iff RS iffD1)) 1); |
|
508 |
by Auto_tac; |
|
509 |
qed_spec_mp "append_eq_append"; |
|
510 |
||
511 |
Goal "[| xs:list(A); ys:list(A); us:list(A); vs:list(A); length(us)=length(vs) |] \ |
|
512 |
\ ==> xs@us = ys@vs <-> (xs=ys & us=vs)"; |
|
513 |
by (rtac iffI 1); |
|
514 |
by (rtac append_eq_append 1); |
|
515 |
by Auto_tac; |
|
516 |
qed "append_eq_append_iff2"; |
|
517 |
Addsimps [append_eq_append_iff, append_eq_append_iff2]; |
|
518 |
||
519 |
Goal "[| xs:list(A); ys:list(A); zs:list(A) |] ==> xs@ys=xs@zs <-> ys=zs"; |
|
520 |
by (Asm_simp_tac 1); |
|
521 |
qed "append_self_iff"; |
|
522 |
Addsimps [append_self_iff]; |
|
523 |
||
524 |
Goal "[| xs:list(A); ys:list(A); zs:list(A) |] ==> ys@xs=zs@xs <-> ys=zs"; |
|
525 |
by (Asm_simp_tac 1); |
|
526 |
qed "append_self_iff2"; |
|
527 |
Addsimps [append_self_iff2]; |
|
528 |
||
529 |
(* Can also be proved from append_eq_append_iff2, |
|
530 |
but the proof requires two more hypotheses: x:A and y:A *) |
|
531 |
Goal "xs:list(A) ==> ALL ys:list(A). xs@[x] = ys@[y] <-> (xs = ys & x=y)"; |
|
532 |
by (etac list.induct 1); |
|
533 |
by (ALLGOALS(Clarify_tac)); |
|
534 |
by (ALLGOALS(eres_inst_tac [("a", "ys")] list.elim));
|
|
535 |
by Auto_tac; |
|
536 |
qed_spec_mp "append1_eq_iff"; |
|
537 |
Addsimps [append1_eq_iff]; |
|
538 |
||
539 |
Goal "[| xs:list(A); ys:list(A) |] ==> (xs@ys = ys) <-> (xs=Nil)"; |
|
| 13143 | 540 |
by (asm_simp_tac (simpset() addsimps [append_left_is_Nil_iff]) 1); |
| 12789 | 541 |
qed "append_right_is_self_iff"; |
542 |
Addsimps [append_right_is_self_iff]; |
|
543 |
||
544 |
Goal "[| xs:list(A); ys:list(A) |] ==> (ys = xs@ys) <-> (xs=Nil)"; |
|
545 |
by (rtac iffI 1); |
|
546 |
by (dtac sym 1); |
|
547 |
by (ALLGOALS(Asm_full_simp_tac)); |
|
548 |
qed "append_right_is_self_iff2"; |
|
549 |
Addsimps [append_right_is_self_iff2]; |
|
550 |
||
551 |
Goal "xs:list(A) ==> xs ~= Nil --> hd(xs @ ys) = hd(xs)"; |
|
552 |
by (induct_tac "xs" 1); |
|
553 |
by Auto_tac; |
|
554 |
qed_spec_mp "hd_append"; |
|
555 |
Addsimps [hd_append]; |
|
556 |
||
557 |
Goal "xs:list(A) ==> xs~=Nil --> tl(xs @ ys) = tl(xs)@ys"; |
|
558 |
by (induct_tac "xs" 1); |
|
559 |
by Auto_tac; |
|
560 |
qed_spec_mp "tl_append"; |
|
561 |
Addsimps [tl_append]; |
|
562 |
||
563 |
(** rev **) |
|
564 |
Goal "xs:list(A) ==> (rev(xs) = Nil <-> xs = Nil)"; |
|
565 |
by (etac list.induct 1); |
|
566 |
by Auto_tac; |
|
567 |
qed "rev_is_Nil_iff"; |
|
568 |
Addsimps [rev_is_Nil_iff]; |
|
569 |
||
570 |
Goal "xs:list(A) ==> (Nil = rev(xs) <-> xs = Nil)"; |
|
571 |
by (etac list.induct 1); |
|
572 |
by Auto_tac; |
|
573 |
qed "Nil_is_rev_iff"; |
|
574 |
Addsimps [Nil_is_rev_iff]; |
|
575 |
||
576 |
Goal "xs:list(A) ==> ALL ys:list(A). rev(xs)=rev(ys) <-> xs=ys"; |
|
577 |
by (etac list.induct 1); |
|
578 |
by (Force_tac 1); |
|
579 |
by (Clarify_tac 1); |
|
580 |
by (eres_inst_tac [("a", "ys")] list.elim 1);
|
|
581 |
by Auto_tac; |
|
582 |
qed_spec_mp "rev_is_rev_iff"; |
|
583 |
Addsimps [rev_is_rev_iff]; |
|
584 |
||
585 |
Goal "xs:list(A) ==> \ |
|
586 |
\ (xs=Nil --> P) --> (ALL ys:list(A). ALL y:A. xs =ys@[y] -->P)-->P"; |
|
587 |
by (etac list_append_induct 1); |
|
588 |
by Auto_tac; |
|
589 |
qed_spec_mp "rev_list_elim_aux"; |
|
590 |
||
591 |
bind_thm("rev_list_elim", impI RS ballI RS ballI RSN(3, rev_list_elim_aux));
|
|
592 |
||
593 |
(** more theorems about drop **) |
|
594 |
||
595 |
Goal "n:nat ==> ALL xs:list(A). length(drop(n, xs)) = length(xs) #- n"; |
|
596 |
by (etac nat_induct 1); |
|
597 |
by (auto_tac (claset() addEs [list.elim], simpset())); |
|
598 |
qed_spec_mp "length_drop"; |
|
599 |
Addsimps [length_drop]; |
|
600 |
||
601 |
Goal "n:nat ==> ALL xs:list(A). length(xs) le n --> drop(n, xs)=Nil"; |
|
602 |
by (etac nat_induct 1); |
|
603 |
by (auto_tac (claset() addEs [list.elim], simpset())); |
|
604 |
qed_spec_mp "drop_all"; |
|
605 |
Addsimps [drop_all]; |
|
606 |
||
607 |
Goal "n:nat ==> ALL xs:list(A). drop(n, xs@ys) = \ |
|
608 |
\ drop(n,xs) @ drop(n #- length(xs), ys)"; |
|
609 |
by (induct_tac "n" 1); |
|
610 |
by (auto_tac (claset() addEs [list.elim], simpset())); |
|
611 |
qed_spec_mp "drop_append"; |
|
612 |
||
613 |
Goal "m:nat ==> \ |
|
614 |
\ ALL xs:list(A). ALL n:nat. drop(n, drop(m, xs))=drop(n #+ m, xs)"; |
|
615 |
by (induct_tac "m" 1); |
|
616 |
by (auto_tac (claset() addEs [list.elim], simpset())); |
|
617 |
qed "drop_drop"; |
|
618 |
||
619 |
(** take **) |
|
620 |
||
621 |
Goalw [take_def] |
|
622 |
"xs:list(A) ==> take(0, xs) = Nil"; |
|
623 |
by (etac list.induct 1); |
|
624 |
by Auto_tac; |
|
625 |
qed "take_0"; |
|
626 |
Addsimps [take_0]; |
|
627 |
||
628 |
Goalw [take_def] |
|
629 |
"n:nat ==> take(succ(n), Cons(a, xs)) = Cons(a, take(n, xs))"; |
|
630 |
by (Asm_simp_tac 1); |
|
631 |
qed "take_succ_Cons"; |
|
632 |
Addsimps [take_succ_Cons]; |
|
633 |
||
634 |
(* Needed for proving take_all *) |
|
635 |
Goalw [take_def] |
|
636 |
"n:nat ==> take(n, Nil) = Nil"; |
|
637 |
by Auto_tac; |
|
638 |
qed "take_Nil"; |
|
639 |
Addsimps [take_Nil]; |
|
640 |
||
641 |
Goal "n:nat ==> ALL xs:list(A). length(xs) le n --> take(n, xs) = xs"; |
|
642 |
by (etac nat_induct 1); |
|
643 |
by (auto_tac (claset() addEs [list.elim], simpset())); |
|
644 |
qed_spec_mp "take_all"; |
|
645 |
Addsimps [take_all]; |
|
646 |
||
647 |
Goal "xs:list(A) ==> ALL n:nat. take(n, xs):list(A)"; |
|
648 |
by (etac list.induct 1); |
|
649 |
by (Clarify_tac 2); |
|
650 |
by (etac natE 2); |
|
651 |
by Auto_tac; |
|
652 |
qed_spec_mp "take_type"; |
|
653 |
||
654 |
Goal "xs:list(A) ==> \ |
|
655 |
\ ALL ys:list(A). ALL n:nat. take(n, xs @ ys) = \ |
|
656 |
\ take(n, xs) @ take(n #- length(xs), ys)"; |
|
657 |
by (etac list.induct 1); |
|
658 |
by (Clarify_tac 2); |
|
659 |
by (etac natE 2); |
|
660 |
by Auto_tac; |
|
661 |
qed_spec_mp "take_append"; |
|
662 |
Addsimps [take_append]; |
|
663 |
||
664 |
Goal |
|
665 |
"m:nat ==> \ |
|
666 |
\ ALL xs:list(A). ALL n:nat. take(n, take(m,xs))= take(min(n, m), xs)"; |
|
667 |
by (induct_tac "m" 1); |
|
668 |
by Auto_tac; |
|
669 |
by (eres_inst_tac [("a", "xs")] list.elim 1);
|
|
670 |
by (auto_tac (claset(), simpset() addsimps [take_Nil])); |
|
671 |
by (rotate_tac 1 1); |
|
672 |
by (etac natE 1); |
|
673 |
by (auto_tac (claset() addIs [take_0, take_type], simpset())); |
|
674 |
qed_spec_mp "take_take"; |
|
675 |
||
676 |
(** nth **) |
|
677 |
||
678 |
Goalw [nth_def] "nth(0, Cons(a, l))= a"; |
|
679 |
by Auto_tac; |
|
680 |
qed "nth_0"; |
|
681 |
Addsimps [nth_0]; |
|
682 |
||
683 |
Goalw [nth_def] |
|
684 |
"n:nat ==> nth(succ(n), Cons(a, l)) = nth(n, l)"; |
|
685 |
by (Asm_simp_tac 1); |
|
686 |
qed "nth_Cons"; |
|
687 |
Addsimps [nth_Cons]; |
|
688 |
||
689 |
Goal "xs:list(A) ==> ALL n:nat. n < length(xs) --> nth(n, xs):A"; |
|
690 |
by (etac list.induct 1); |
|
691 |
by (ALLGOALS(Clarify_tac)); |
|
692 |
by (etac natE 2); |
|
693 |
by (ALLGOALS(Asm_full_simp_tac)); |
|
694 |
qed_spec_mp "nth_type"; |
|
695 |
||
696 |
AddTCs [nth_type]; |
|
697 |
Addsimps [nth_type]; |
|
698 |
||
699 |
Goal |
|
700 |
"xs:list(A) ==> ALL n:nat. \ |
|
701 |
\ nth(n, xs @ ys) = (if n < length(xs) then nth(n,xs) \ |
|
702 |
\ else nth(n #- length(xs),ys))"; |
|
703 |
by (induct_tac "xs" 1); |
|
704 |
by (Clarify_tac 2); |
|
705 |
by (etac natE 2); |
|
706 |
by (ALLGOALS(Asm_full_simp_tac)); |
|
707 |
qed_spec_mp "nth_append"; |
|
708 |
||
| 12825 | 709 |
Goal "xs:list(A) \ |
710 |
\ ==> set_of_list(xs) = {x:A. EX i:nat. i<length(xs) & x=nth(i, xs)}";
|
|
| 12789 | 711 |
by (induct_tac "xs" 1); |
712 |
by (ALLGOALS(Asm_simp_tac)); |
|
713 |
by (rtac equalityI 1); |
|
714 |
by Auto_tac; |
|
715 |
by (res_inst_tac [("x", "0")] bexI 1);
|
|
716 |
by Auto_tac; |
|
717 |
by (etac natE 1); |
|
718 |
by Auto_tac; |
|
719 |
qed "set_of_list_conv_nth"; |
|
720 |
||
721 |
(* Other theorems about lists *) |
|
722 |
||
723 |
Goalw [Ball_def] |
|
724 |
"k:nat ==> \ |
|
725 |
\ ALL xs:list(A). (ALL ys:list(A). k le length(xs) --> k le length(ys) --> \ |
|
726 |
\ (ALL i:nat. i<k --> nth(i,xs)= nth(i,ys))--> take(k, xs) = take(k,ys))"; |
|
727 |
by (induct_tac "k" 1); |
|
728 |
by (ALLGOALS (asm_simp_tac (simpset() addsimps |
|
729 |
[lt_succ_eq_0_disj, all_conj_distrib]))); |
|
730 |
by (Clarify_tac 1); |
|
731 |
(*Both lists must be non-empty*) |
|
732 |
by (case_tac "xa=Nil" 1); |
|
733 |
by (case_tac "xb=Nil" 2); |
|
734 |
by (Clarify_tac 1); |
|
735 |
by (ALLGOALS(asm_full_simp_tac (simpset() addsimps [neq_Nil_iff]))); |
|
736 |
by (Clarify_tac 1); |
|
737 |
(*prenexing's needed, not miniscoping*) |
|
738 |
by (Asm_simp_tac 1); |
|
739 |
by (rtac conjI 1); |
|
740 |
by (Force_tac 1); |
|
741 |
by (ALLGOALS (full_simp_tac (simpset() addsimps (all_simps RL [iff_sym]) |
|
742 |
delsimps (all_simps)))); |
|
743 |
by (dres_inst_tac [("x", "ys"), ("x1", "ysa")] (spec RS spec) 1);
|
|
744 |
by (Clarify_tac 1); |
|
745 |
by Auto_tac; |
|
746 |
qed_spec_mp "nth_take_lemma"; |
|
747 |
||
748 |
Goal "[| xs:list(A); ys:list(A); length(xs) = length(ys); \ |
|
749 |
\ ALL i:nat. i < length(xs) --> nth(i,xs) = nth(i,ys) |] \ |
|
750 |
\ ==> xs = ys"; |
|
751 |
by (subgoal_tac "length(xs) le length(ys)" 1); |
|
752 |
by (forw_inst_tac [("ys", "ys")] (rotate_prems 1 nth_take_lemma) 1);
|
|
753 |
by (ALLGOALS(Asm_simp_tac)); |
|
754 |
by (ALLGOALS (asm_full_simp_tac (simpset() addsimps [take_all]))); |
|
755 |
qed_spec_mp "nth_equalityI"; |
|
756 |
||
757 |
(*The famous take-lemma*) |
|
758 |
||
759 |
Goal "[| xs:list(A); ys:list(A); (ALL i:nat. take(i, xs) = take(i,ys)) |] ==> xs = ys"; |
|
760 |
by (case_tac "length(xs) le length(ys)" 1); |
|
761 |
by (dres_inst_tac [("x", "length(ys)")] bspec 1);
|
|
762 |
by (dtac not_lt_imp_le 3); |
|
763 |
by (subgoal_tac "length(ys) le length(xs)" 5); |
|
764 |
by (res_inst_tac [("j", "succ(length(ys))")] le_trans 6);
|
|
765 |
by (rtac leI 6); |
|
766 |
by (dres_inst_tac [("x", "length(xs)")] bspec 5);
|
|
767 |
by (ALLGOALS(asm_full_simp_tac (simpset() addsimps [take_all]))); |
|
768 |
qed_spec_mp "take_equalityI"; |
|
769 |
||
770 |
Goal "n:nat ==> ALL i:nat. ALL xs:list(A). n #+ i le length(xs) \ |
|
771 |
\ --> nth(i, drop(n, xs)) = nth(n #+ i, xs)"; |
|
772 |
by (induct_tac "n" 1); |
|
773 |
by (ALLGOALS(Asm_full_simp_tac)); |
|
774 |
by (Clarify_tac 1); |
|
775 |
by (case_tac "xb=Nil" 1); |
|
776 |
by (ALLGOALS(asm_full_simp_tac (simpset() addsimps [neq_Nil_iff]))); |
|
777 |
by (Clarify_tac 1); |
|
778 |
by (auto_tac (claset() addSEs [ConsE], simpset())); |
|
779 |
qed_spec_mp "nth_drop"; |
|
780 |
||
781 |
(** zip **) |
|
782 |
||
783 |
Goal "l:list(A) ==> l:list(set_of_list(l))"; |
|
784 |
by (induct_tac "l" 1); |
|
785 |
by Auto_tac; |
|
786 |
by (res_inst_tac [("A1", "set_of_list(l)")] (list_mono RS subsetD) 1);
|
|
787 |
by Auto_tac; |
|
788 |
qed "list_on_set_of_list"; |
|
789 |
||
790 |
Goal "A<=C ==> ziprel(A, B) <= ziprel(C,B)"; |
|
791 |
by (Clarify_tac 1); |
|
792 |
by (forward_tac [ziprel.dom_subset RS subsetD] 1); |
|
793 |
by (Clarify_tac 1); |
|
794 |
by (etac ziprel.induct 1); |
|
795 |
by (auto_tac (claset() addIs [list_mono RS subsetD] |
|
796 |
addSIs ziprel.intrs, simpset())); |
|
797 |
qed "ziprel_mono1"; |
|
798 |
||
799 |
Goal "B<=C ==> ziprel(A, B) <= ziprel(A,C)"; |
|
800 |
by (Clarify_tac 1); |
|
801 |
by (forward_tac [ziprel.dom_subset RS subsetD] 1); |
|
802 |
by (Clarify_tac 1); |
|
803 |
by (etac ziprel.induct 1); |
|
804 |
by (auto_tac (claset() addIs [list_mono RS subsetD] |
|
805 |
addSIs ziprel.intrs, simpset())); |
|
806 |
qed "ziprel_mono2"; |
|
807 |
||
808 |
Goal "[| A<=C; B<=D |] ==> ziprel(A, B) <= ziprel(C,D)"; |
|
809 |
by (rtac subset_trans 1); |
|
810 |
by (rtac ziprel_mono1 2); |
|
811 |
by (rtac ziprel_mono2 1); |
|
812 |
by Auto_tac; |
|
813 |
qed "ziprel_mono"; |
|
814 |
||
815 |
(* ziprel is a function *) |
|
816 |
||
817 |
Goal "<xs, ys, zs>:ziprel(A,B) \ |
|
818 |
\ ==> ALL ks. <xs, ys, ks>:ziprel(A, B) --> ks=zs"; |
|
819 |
by (etac ziprel.induct 1); |
|
820 |
by (ALLGOALS(Clarify_tac)); |
|
821 |
by (rotate_tac 1 3); |
|
822 |
by (ALLGOALS(etac ziprel.elim)); |
|
823 |
by Safe_tac; |
|
824 |
(* These hypotheses make Auto_tac loop! *) |
|
825 |
by (thin_tac "ALL k. ?P(k)" 3); |
|
826 |
by (thin_tac "ALL k. ?P(k)" 4); |
|
827 |
by Auto_tac; |
|
828 |
qed "ziprel_is_fun"; |
|
829 |
||
830 |
Goal "ys:list(B) ==> ALL xs:list(A). EX zs. <xs, ys, zs>:ziprel(A,B)"; |
|
831 |
by (induct_tac "ys" 1); |
|
832 |
by (auto_tac (claset() addIs ziprel.intrs, simpset())); |
|
833 |
by (eres_inst_tac [("a", "xs")] list.elim 1);
|
|
834 |
by (auto_tac (claset() addIs ziprel.intrs, simpset())); |
|
835 |
qed_spec_mp "ziprel_exist"; |
|
836 |
||
837 |
Goalw [zip_def] |
|
838 |
"[|xs:list(A); ys:list(B) |] ==> <xs, ys, zip(xs,ys)>:ziprel(A,B)"; |
|
839 |
by (rtac theI2 1); |
|
840 |
by (asm_full_simp_tac (simpset() addsimps [set_of_list_append]) 2); |
|
841 |
by (REPEAT(dtac set_of_list_type 2)); |
|
842 |
by (rtac (ziprel_mono RS subsetD) 2); |
|
843 |
by (Blast_tac 3); |
|
844 |
by (dtac list_on_set_of_list 1); |
|
845 |
by (dtac list_on_set_of_list 1); |
|
846 |
by (subgoal_tac "xs:list(set_of_list(xs@ys)) & \ |
|
847 |
\ ys:list(set_of_list(xs@ys))" 1); |
|
848 |
by (auto_tac (claset() addIs [list_mono RS subsetD], |
|
849 |
simpset() addsimps [set_of_list_append])); |
|
850 |
by (rtac (ziprel_is_fun RS spec RS mp) 2); |
|
851 |
by (rtac ziprel_exist 1); |
|
852 |
by Auto_tac; |
|
853 |
qed "zip_in_ziprel"; |
|
854 |
||
855 |
Goal |
|
856 |
"<xs, ys, zs>:ziprel(A,B) ==> zip(xs, ys)=zs"; |
|
857 |
by (rtac (ziprel_is_fun RS spec RS mp) 1); |
|
858 |
by (Blast_tac 1); |
|
859 |
by (blast_tac (claset() addDs [ziprel.dom_subset RS subsetD] |
|
860 |
addIs [zip_in_ziprel]) 1); |
|
861 |
qed "zip_eq"; |
|
862 |
||
863 |
(* zip equations *) |
|
864 |
||
865 |
Goal "ys:list(A) ==> zip(Nil, ys)=Nil"; |
|
866 |
by (res_inst_tac [("A", "A")] zip_eq 1);
|
|
867 |
by (auto_tac (claset() addIs ziprel.intrs, simpset())); |
|
868 |
qed "zip_Nil"; |
|
869 |
Addsimps [zip_Nil]; |
|
870 |
||
871 |
Goal "xs:list(A) ==> zip(xs, Nil)=Nil"; |
|
872 |
by (res_inst_tac [("A", "A")] zip_eq 1);
|
|
873 |
by (auto_tac (claset() addIs ziprel.intrs, simpset())); |
|
874 |
qed "zip_Nil2"; |
|
875 |
Addsimps [zip_Nil2]; |
|
876 |
||
877 |
Goal "[| xs:list(A); ys:list(B); x:A; y:B |] ==> \ |
|
878 |
\ zip(Cons(x,xs), Cons(y, ys)) = Cons(<x,y>, zip(xs, ys))"; |
|
879 |
by (res_inst_tac [("A", "A")] zip_eq 1);
|
|
880 |
by (forw_inst_tac [("ys", "ys")] zip_in_ziprel 1);
|
|
881 |
by (auto_tac (claset() addIs ziprel.intrs, simpset())); |
|
882 |
qed "zip_Cons_Cons"; |
|
883 |
Addsimps [zip_Cons_Cons]; |
|
884 |
||
885 |
Goal "xs:list(A) ==> ALL ys:list(B). zip(xs, ys):list(A*B)"; |
|
886 |
by (induct_tac "xs" 1); |
|
887 |
by (Simp_tac 1); |
|
888 |
by (Clarify_tac 1); |
|
889 |
by (eres_inst_tac [("a", "ys")] list.elim 1);
|
|
890 |
by Auto_tac; |
|
891 |
qed_spec_mp "zip_type"; |
|
892 |
||
893 |
AddTCs [zip_type]; |
|
894 |
Addsimps [zip_type]; |
|
895 |
||
896 |
(* zip length *) |
|
897 |
Goalw [min_def] "xs:list(A) ==> ALL ys:list(B). length(zip(xs,ys)) = \ |
|
898 |
\ min(length(xs), length(ys))"; |
|
899 |
by (induct_tac "xs" 1); |
|
900 |
by (Clarify_tac 2); |
|
901 |
by (eres_inst_tac [("a", "ys")] list.elim 2);
|
|
902 |
by Auto_tac; |
|
903 |
qed_spec_mp "length_zip"; |
|
904 |
Addsimps [length_zip]; |
|
905 |
||
906 |
AddTCs [take_type]; |
|
907 |
Addsimps [take_type]; |
|
908 |
||
909 |
Goal "[| ys:list(A); zs:list(B) |] ==> ALL xs:list(A). zip(xs @ ys, zs) = \ |
|
910 |
\ zip(xs, take(length(xs), zs)) @ zip(ys, drop(length(xs),zs))"; |
|
911 |
by (induct_tac "zs" 1); |
|
912 |
by (Clarify_tac 2); |
|
913 |
by (eres_inst_tac [("a", "xs")] list.elim 2);
|
|
914 |
by Auto_tac; |
|
915 |
qed_spec_mp "zip_append1"; |
|
916 |
||
917 |
Goal |
|
918 |
"[| xs:list(A); zs:list(B) |] ==> ALL ys:list(B). zip(xs, ys@zs) = \ |
|
919 |
\ zip(take(length(ys), xs), ys) @ zip(drop(length(ys), xs), zs)"; |
|
920 |
by (induct_tac "xs" 1); |
|
921 |
by (Clarify_tac 2); |
|
922 |
by (eres_inst_tac [("a", "ys")] list.elim 2);
|
|
923 |
by Auto_tac; |
|
924 |
qed_spec_mp "zip_append2"; |
|
925 |
||
926 |
Goal |
|
927 |
"[| length(xs) = length(us); length(ys) = length(vs); \ |
|
928 |
\ xs:list(A); us:list(B); ys:list(A); vs:list(B) |] ==> \ |
|
929 |
\ zip(xs@ys,us@vs) = zip(xs, us) @ zip(ys, vs)"; |
|
930 |
by (asm_simp_tac (simpset() addsimps |
|
931 |
[zip_append1, drop_append, diff_self_eq_0]) 1); |
|
932 |
qed_spec_mp "zip_append"; |
|
933 |
Addsimps [zip_append]; |
|
934 |
||
935 |
Goal "ys:list(B) ==> ALL xs:list(A). \ |
|
936 |
\ length(xs) = length(ys) --> zip(rev(xs), rev(ys)) = rev(zip(xs, ys))"; |
|
937 |
by (induct_tac "ys" 1); |
|
938 |
by (Clarify_tac 2); |
|
939 |
by (eres_inst_tac [("a", "xs")] list.elim 2);
|
|
940 |
by (auto_tac (claset(), simpset() addsimps [length_rev])); |
|
941 |
qed_spec_mp "zip_rev"; |
|
942 |
Addsimps [zip_rev]; |
|
943 |
||
944 |
Goal |
|
945 |
"ys:list(B) ==> ALL i:nat. ALL xs:list(A). \ |
|
946 |
\ i < length(xs) --> i < length(ys) --> \ |
|
947 |
\ nth(i,zip(xs, ys)) = <nth(i,xs),nth(i, ys)>"; |
|
948 |
by (induct_tac "ys" 1); |
|
949 |
by (Clarify_tac 2); |
|
950 |
by (eres_inst_tac [("a", "xs")] list.elim 2);
|
|
951 |
by (auto_tac (claset() addEs [natE], simpset())); |
|
952 |
qed_spec_mp "nth_zip"; |
|
953 |
Addsimps [nth_zip]; |
|
954 |
||
955 |
Goal "[| xs:list(A); ys:list(B); i:nat |] \ |
|
956 |
\ ==> set_of_list(zip(xs, ys)) = \ |
|
957 |
\ {<x, y>:A*B. EX i:nat. i < min(length(xs), length(ys)) \
|
|
958 |
\ & x=nth(i, xs) & y=nth(i, ys)}"; |
|
959 |
by (force_tac (claset() addSIs [Collect_cong], |
|
960 |
simpset() addsimps [lt_min_iff, set_of_list_conv_nth]) 1); |
|
961 |
qed_spec_mp "set_of_list_zip"; |
|
962 |
||
963 |
(** list_update **) |
|
964 |
||
965 |
Goalw [list_update_def] "i:nat ==>list_update(Nil, i, v) = Nil"; |
|
966 |
by Auto_tac; |
|
967 |
qed "list_update_Nil"; |
|
968 |
Addsimps [list_update_Nil]; |
|
969 |
||
970 |
Goalw [list_update_def] |
|
971 |
"list_update(Cons(x, xs), 0, v)= Cons(v, xs)"; |
|
972 |
by Auto_tac; |
|
973 |
qed "list_update_Cons_0"; |
|
974 |
Addsimps [list_update_Cons_0]; |
|
975 |
||
976 |
Goalw [list_update_def] |
|
977 |
"n:nat ==>\ |
|
978 |
\ list_update(Cons(x, xs), succ(n), v)= Cons(x, list_update(xs, n, v))"; |
|
979 |
by Auto_tac; |
|
980 |
qed "list_update_Cons_succ"; |
|
981 |
Addsimps [list_update_Cons_succ]; |
|
982 |
||
983 |
Goal "[| xs:list(A); v:A |] ==> ALL n:nat. list_update(xs, n, v):list(A)"; |
|
984 |
by (induct_tac "xs" 1); |
|
985 |
by (Simp_tac 1); |
|
986 |
by (Clarify_tac 1); |
|
987 |
by (etac natE 1); |
|
988 |
by Auto_tac; |
|
989 |
qed_spec_mp "list_update_type"; |
|
990 |
Addsimps [list_update_type]; |
|
991 |
AddTCs [list_update_type]; |
|
992 |
||
993 |
Goal "xs:list(A) ==> ALL i:nat. length(list_update(xs, i, v))=length(xs)"; |
|
994 |
by (induct_tac "xs" 1); |
|
995 |
by (Simp_tac 1); |
|
996 |
by (Clarify_tac 1); |
|
997 |
by (etac natE 1); |
|
998 |
by Auto_tac; |
|
999 |
qed_spec_mp "length_list_update"; |
|
1000 |
Addsimps [length_list_update]; |
|
1001 |
||
1002 |
Goal "[| xs:list(A) |] ==> ALL i:nat. ALL j:nat. i < length(xs) --> \ |
|
1003 |
\ nth(j, list_update(xs, i, x)) = (if i=j then x else nth(j, xs))"; |
|
1004 |
by (induct_tac "xs" 1); |
|
1005 |
by (Simp_tac 1); |
|
1006 |
by (Clarify_tac 1); |
|
1007 |
by (etac natE 1); |
|
1008 |
by (ALLGOALS(Asm_full_simp_tac)); |
|
1009 |
by (ALLGOALS(Clarify_tac)); |
|
1010 |
by (etac natE 1); |
|
1011 |
by (ALLGOALS(Asm_full_simp_tac)); |
|
1012 |
by Safe_tac; |
|
1013 |
by (etac natE 2); |
|
1014 |
by (ALLGOALS(asm_full_simp_tac (simpset() addsimps [nth_Cons]))); |
|
1015 |
qed_spec_mp "nth_list_update"; |
|
1016 |
||
1017 |
Goal "[| i < length(xs); xs:list(A); i:nat |] \ |
|
1018 |
\ ==> nth(i, list_update(xs, i,x)) = x"; |
|
1019 |
by (asm_simp_tac (simpset() addsimps [nth_list_update]) 1); |
|
1020 |
qed "nth_list_update_eq"; |
|
1021 |
Addsimps [nth_list_update_eq]; |
|
1022 |
||
1023 |
Goal "xs:list(A) ==> ALL i:nat. ALL j:nat. i ~= j \ |
|
1024 |
\ --> nth(j, list_update(xs, i,x)) = nth(j, xs)"; |
|
1025 |
by (induct_tac "xs" 1); |
|
1026 |
by (Simp_tac 1); |
|
1027 |
by (Clarify_tac 1); |
|
1028 |
by (etac natE 1); |
|
1029 |
by (etac natE 2); |
|
1030 |
by (ALLGOALS(Asm_full_simp_tac)); |
|
1031 |
by (etac natE 1); |
|
1032 |
by (ALLGOALS(asm_full_simp_tac (simpset() addsimps [nth_Cons]))); |
|
1033 |
qed_spec_mp "nth_list_update_neq"; |
|
1034 |
Addsimps [nth_list_update_neq]; |
|
1035 |
||
1036 |
Goal "xs:list(A) ==> ALL i:nat. i < length(xs)\ |
|
1037 |
\ --> list_update(list_update(xs, i, x), i, y) = list_update(xs, i,y)"; |
|
1038 |
by (induct_tac "xs" 1); |
|
1039 |
by (Simp_tac 1); |
|
1040 |
by (Clarify_tac 1); |
|
1041 |
by (etac natE 1); |
|
1042 |
by Auto_tac; |
|
1043 |
qed_spec_mp "list_update_overwrite"; |
|
1044 |
Addsimps [list_update_overwrite]; |
|
1045 |
||
1046 |
Goal "xs:list(A) ==> ALL i:nat. i < length(xs) --> \ |
|
1047 |
\ (list_update(xs, i, x) = xs) <-> (nth(i, xs) = x)"; |
|
1048 |
by (induct_tac "xs" 1); |
|
1049 |
by (Simp_tac 1); |
|
1050 |
by (Clarify_tac 1); |
|
1051 |
by (etac natE 1); |
|
1052 |
by Auto_tac; |
|
1053 |
qed_spec_mp "list_update_same_conv"; |
|
1054 |
||
1055 |
Goal "ys:list(B) ==> ALL i:nat. ALL xy:A*B. ALL xs:list(A). \ |
|
1056 |
\ length(xs) = length(ys) --> \ |
|
1057 |
\ list_update(zip(xs, ys), i, xy) = zip(list_update(xs, i, fst(xy)), \ |
|
1058 |
\ list_update(ys, i, snd(xy)))"; |
|
1059 |
by (induct_tac "ys" 1); |
|
1060 |
by Auto_tac; |
|
1061 |
by (eres_inst_tac [("a", "xc")] list.elim 1);
|
|
1062 |
by (auto_tac (claset() addEs [natE], simpset())); |
|
1063 |
qed_spec_mp "update_zip"; |
|
1064 |
||
1065 |
Goal "xs:list(A) ==> ALL i:nat. set_of_list(list_update(xs, i, x)) \ |
|
1066 |
\ <= cons(x, set_of_list(xs))"; |
|
1067 |
by (induct_tac "xs" 1); |
|
1068 |
by (asm_full_simp_tac (simpset() addsimps []) 1); |
|
1069 |
by (rtac ballI 1); |
|
1070 |
by (etac natE 1); |
|
1071 |
by (ALLGOALS(Asm_full_simp_tac)); |
|
1072 |
by Auto_tac; |
|
1073 |
qed_spec_mp "set_update_subset_cons"; |
|
1074 |
||
1075 |
Goal "[| set_of_list(xs) <= A; xs:list(A); x:A; i:nat|] \ |
|
1076 |
\ ==> set_of_list(list_update(xs, i,x)) <= A"; |
|
1077 |
by (rtac subset_trans 1); |
|
1078 |
by (rtac set_update_subset_cons 1); |
|
1079 |
by Auto_tac; |
|
1080 |
qed "set_of_list_update_subsetI"; |
|
1081 |
||
1082 |
(** upt **) |
|
1083 |
||
1084 |
Goal "[| i:nat; j:nat |] \ |
|
1085 |
\ ==> upt(i,j) = (if i<j then Cons(i, upt(succ(i), j)) else Nil)"; |
|
1086 |
by (induct_tac "j" 1); |
|
1087 |
by Auto_tac; |
|
1088 |
by (dtac not_lt_imp_le 1); |
|
1089 |
by (auto_tac (claset() addIs [le_anti_sym], simpset())); |
|
1090 |
qed "upt_rec"; |
|
1091 |
||
1092 |
Goal "[| j le i; i:nat; j:nat |] ==> upt(i,j) = Nil"; |
|
1093 |
by (stac upt_rec 1); |
|
1094 |
by Auto_tac; |
|
1095 |
by (auto_tac (claset(), simpset() addsimps [le_iff])); |
|
1096 |
by (dtac (lt_asym RS notE) 1); |
|
1097 |
by Auto_tac; |
|
1098 |
qed "upt_conv_Nil"; |
|
1099 |
Addsimps [upt_conv_Nil]; |
|
1100 |
||
1101 |
(*Only needed if upt_Suc is deleted from the simpset*) |
|
1102 |
Goal "[| i le j; i:nat; j:nat |] ==> upt(i,succ(j)) = upt(i, j)@[j]"; |
|
1103 |
by (Asm_simp_tac 1); |
|
1104 |
qed "upt_succ_append"; |
|
1105 |
||
1106 |
Goal "[| i<j; i:nat; j:nat |] ==> upt(i,j) = Cons(i,upt(succ(i),j))"; |
|
1107 |
by (rtac trans 1); |
|
1108 |
by (stac upt_rec 1); |
|
1109 |
by (rtac refl 4); |
|
1110 |
by Auto_tac; |
|
1111 |
qed "upt_conv_Cons"; |
|
1112 |
||
1113 |
Goal "[| i:nat; j:nat |] ==> upt(i,j):list(nat)"; |
|
1114 |
by (induct_tac "j" 1); |
|
1115 |
by Auto_tac; |
|
1116 |
qed "upt_type"; |
|
1117 |
||
1118 |
AddTCs [upt_type]; |
|
1119 |
Addsimps [upt_type]; |
|
1120 |
||
1121 |
(*LOOPS as a simprule, since j<=j*) |
|
1122 |
Goal "[| i le j; i:nat; j:nat; k:nat |] ==> \ |
|
1123 |
\ upt(i, j #+k) = upt(i,j)@upt(j,j#+k)"; |
|
1124 |
by (induct_tac "k" 1); |
|
1125 |
by (auto_tac (claset(), simpset() addsimps [app_assoc, app_type])); |
|
1126 |
by (res_inst_tac [("j", "j")] le_trans 1);
|
|
1127 |
by Auto_tac; |
|
1128 |
qed "upt_add_eq_append"; |
|
1129 |
||
1130 |
Goal "[| i:nat; j:nat |] ==>length(upt(i,j)) = j #- i"; |
|
1131 |
by (induct_tac "j" 1); |
|
1132 |
by (rtac sym 2); |
|
1133 |
by (auto_tac (claset() addSDs [not_lt_imp_le], |
|
1134 |
simpset() addsimps [length_app, diff_succ, diff_is_0_iff])); |
|
1135 |
qed "length_upt"; |
|
1136 |
Addsimps [length_upt]; |
|
1137 |
||
| 13160 | 1138 |
Goal "[| i:nat; j:nat; k:nat |] ==> i #+ k < j --> nth(k, upt(i,j)) = i #+ k"; |
| 12789 | 1139 |
by (induct_tac "j" 1); |
1140 |
by (Asm_simp_tac 1); |
|
| 13160 | 1141 |
by (asm_full_simp_tac (simpset() addsimps [nth_append,le_iff] |
| 12789 | 1142 |
addsplits [nat_diff_split]) 1); |
1143 |
by Safe_tac; |
|
1144 |
by (auto_tac (claset() addSDs [not_lt_imp_le], |
|
1145 |
simpset() addsimps [nth_append, diff_self_eq_0, |
|
1146 |
less_diff_conv, add_commute])); |
|
1147 |
by (dres_inst_tac [("j", "x")] lt_trans2 1);
|
|
1148 |
by Auto_tac; |
|
1149 |
qed_spec_mp "nth_upt"; |
|
1150 |
Addsimps [nth_upt]; |
|
1151 |
||
1152 |
Goal "[| m:nat; n:nat |] ==> \ |
|
1153 |
\ ALL i:nat. i #+ m le n --> take(m, upt(i,n)) = upt(i,i#+m)"; |
|
1154 |
by (induct_tac "m" 1); |
|
1155 |
by (asm_simp_tac (simpset() addsimps [take_0]) 1); |
|
1156 |
by (Clarify_tac 1); |
|
1157 |
by (stac upt_rec 1); |
|
1158 |
by (rtac sym 3); |
|
1159 |
by (stac upt_rec 3); |
|
1160 |
by (ALLGOALS(asm_full_simp_tac (simpset() delsimps (thms"upt.simps")))); |
|
1161 |
by (res_inst_tac [("j", "succ(i #+ x)")] lt_trans2 1);
|
|
1162 |
by Auto_tac; |
|
1163 |
qed_spec_mp "take_upt"; |
|
1164 |
Addsimps [take_upt]; |
|
1165 |
||
1166 |
Goal "[| m:nat; n:nat |] ==> map(succ, upt(m,n))= upt(succ(m), succ(n))"; |
|
1167 |
by (induct_tac "n" 1); |
|
1168 |
by (auto_tac (claset(), simpset() addsimps [map_app_distrib])); |
|
1169 |
qed "map_succ_upt"; |
|
1170 |
||
1171 |
Goal "xs:list(A) ==> \ |
|
1172 |
\ ALL n:nat. n < length(xs) --> nth(n, map(f, xs)) = f(nth(n, xs))"; |
|
1173 |
by (induct_tac "xs" 1); |
|
1174 |
by (Asm_full_simp_tac 1); |
|
1175 |
by (rtac ballI 1); |
|
1176 |
by (induct_tac "n" 1); |
|
1177 |
by Auto_tac; |
|
1178 |
qed_spec_mp "nth_map"; |
|
1179 |
Addsimps [nth_map]; |
|
1180 |
||
1181 |
Goal "[| m:nat; n:nat |] ==> \ |
|
1182 |
\ ALL i:nat. i < n #- m --> nth(i, map(f, upt(m,n))) = f(m #+ i)"; |
|
1183 |
by (res_inst_tac [("n", "m"), ("m", "n")] diff_induct 1);
|
|
1184 |
by (stac (map_succ_upt RS sym) 5); |
|
1185 |
by (ALLGOALS(Asm_full_simp_tac)); |
|
1186 |
by (ALLGOALS(Clarify_tac)); |
|
1187 |
by (subgoal_tac "xa < length(upt(0, x))" 1); |
|
1188 |
by (Asm_simp_tac 2); |
|
1189 |
by (subgoal_tac "xa < length(upt(y, x))" 2); |
|
1190 |
by (ALLGOALS(asm_full_simp_tac (simpset() addsimps [map_compose, |
|
1191 |
nth_map, add_commute, less_diff_conv]))); |
|
1192 |
by (res_inst_tac [("j", "succ(xa #+ y)")] lt_trans2 1);
|
|
1193 |
by Auto_tac; |
|
1194 |
qed_spec_mp "nth_map_upt"; |
|
1195 |
||
1196 |
(** sublist (a generalization of nth to sets) **) |
|
1197 |
||
1198 |
Goalw [sublist_def] "xs:list(A) ==>sublist(xs, 0) =Nil"; |
|
1199 |
by Auto_tac; |
|
1200 |
qed "sublist_0"; |
|
1201 |
||
1202 |
Goalw [sublist_def] "sublist(Nil, A) = Nil"; |
|
1203 |
by Auto_tac; |
|
1204 |
qed "sublist_Nil"; |
|
1205 |
||
1206 |
AddTCs [filter_type]; |
|
1207 |
Addsimps [filter_type]; |
|
1208 |
||
1209 |
Goal "[| xs:list(B); i:nat |] ==>\ |
|
1210 |
\ map(fst, filter(%p. snd(p):A, zip(xs, upt(i,i #+ length(xs))))) = \ |
|
1211 |
\ map(fst, filter(%p. snd(p):nat & snd(p) #+ i:A, zip(xs,upt(0,length(xs)))))"; |
|
1212 |
by (etac list_append_induct 1); |
|
1213 |
by (Asm_simp_tac 1); |
|
1214 |
by (auto_tac (claset(), simpset() addsimps |
|
1215 |
[add_commute, length_app, filter_append, map_app_distrib])); |
|
1216 |
qed "sublist_shift_lemma"; |
|
1217 |
||
1218 |
Goalw [sublist_def] |
|
1219 |
"xs:list(B) ==> sublist(xs, A):list(B)"; |
|
1220 |
by (induct_tac "xs" 1); |
|
1221 |
by (auto_tac (claset(), simpset() addsimps [filter_append, map_app_distrib])); |
|
1222 |
by (auto_tac (claset() addIs [map_type,filter_type,zip_type], simpset())); |
|
1223 |
qed "sublist_type"; |
|
1224 |
||
1225 |
AddTCs [sublist_type]; |
|
1226 |
Addsimps [sublist_type]; |
|
1227 |
||
1228 |
Goal "[| i:nat; j:nat |] ==> upt(0, i #+ j) = upt(0, i) @ upt(i, i #+ j)"; |
|
1229 |
by (asm_simp_tac (simpset() addsimps |
|
1230 |
[inst "i" "0" upt_add_eq_append, nat_0_le]) 1); |
|
1231 |
qed "upt_add_eq_append2"; |
|
1232 |
||
1233 |
Goalw [sublist_def] |
|
1234 |
"[| xs:list(B); ys:list(B) |] ==> \ |
|
1235 |
\ sublist(xs@ys, A) = sublist(xs, A) @ sublist(ys, {j:nat. j #+ length(xs): A})";
|
|
1236 |
by (eres_inst_tac [("l", "ys")] list_append_induct 1);
|
|
1237 |
by (Asm_simp_tac 1); |
|
1238 |
by (asm_simp_tac (simpset() addsimps [upt_add_eq_append2, |
|
1239 |
zip_append, length_app, app_assoc RS sym]) 1); |
|
1240 |
by (auto_tac (claset(), simpset() addsimps [sublist_shift_lemma, |
|
1241 |
length_type, map_app_distrib, app_assoc])); |
|
1242 |
by (ALLGOALS(asm_full_simp_tac (simpset() addsimps [add_commute]))); |
|
1243 |
qed "sublist_append"; |
|
1244 |
||
1245 |
Addsimps [sublist_0, sublist_Nil]; |
|
1246 |
||
1247 |
Goal "[| xs:list(B); x:B |] ==> \ |
|
1248 |
\ sublist(Cons(x, xs), A) = (if 0:A then [x] else []) @ sublist(xs, {j:nat. succ(j) : A})";
|
|
1249 |
by (eres_inst_tac [("l", "xs")] list_append_induct 1);
|
|
1250 |
by (asm_simp_tac (simpset() addsimps [sublist_def]) 1); |
|
1251 |
by (asm_simp_tac (simpset() delsimps (thms "op @.simps") |
|
1252 |
addsimps [(hd (tl (thms "op @.simps"))) RS sym, sublist_append]) 1); |
|
1253 |
by Auto_tac; |
|
1254 |
qed "sublist_Cons"; |
|
1255 |
||
1256 |
Goal "sublist([x], A) = (if 0 : A then [x] else [])"; |
|
1257 |
by (simp_tac (simpset() addsimps [sublist_Cons]) 1); |
|
1258 |
qed "sublist_singleton"; |
|
1259 |
Addsimps [sublist_singleton]; |
|
1260 |
||
1261 |
Goalw [less_than_def] |
|
1262 |
"[| xs:list(A); n:nat |] ==> sublist(xs, less_than(n)) = take(n,xs)"; |
|
1263 |
by (eres_inst_tac [("l", "xs")] list_append_induct 1);
|
|
1264 |
by (asm_simp_tac (simpset() addsplits [nat_diff_split] |
|
1265 |
addsimps [sublist_append]) 2); |
|
1266 |
by Auto_tac; |
|
1267 |
by (subgoal_tac "n #- length(y) = 0" 1); |
|
1268 |
by (Asm_simp_tac 1); |
|
1269 |
by (auto_tac (claset() addSDs [not_lt_imp_le], |
|
1270 |
simpset() addsimps [diff_is_0_iff])); |
|
1271 |
qed "sublist_upt_eq_take"; |
|
1272 |
Addsimps [sublist_upt_eq_take]; |
|
1273 |