| author | paulson | 
| Thu, 17 Jun 1999 10:35:01 +0200 | |
| changeset 6833 | 15d6c121d75f | 
| parent 6831 | 799859f2e657 | 
| child 7028 | 6ea3b385e731 | 
| permissions | -rw-r--r-- | 
| 1465 | 1 | (* Title: HOL/List | 
| 923 | 2 | ID: $Id$ | 
| 1465 | 3 | Author: Tobias Nipkow | 
| 923 | 4 | Copyright 1994 TU Muenchen | 
| 5 | ||
| 6 | List lemmas | |
| 7 | *) | |
| 8 | ||
| 4935 | 9 | Goal "!x. xs ~= x#xs"; | 
| 3040 
7d48671753da
Introduced a generic "induct_tac" which picks up the right induction scheme
 nipkow parents: 
3011diff
changeset | 10 | by (induct_tac "xs" 1); | 
| 5316 | 11 | by Auto_tac; | 
| 2608 | 12 | qed_spec_mp "not_Cons_self"; | 
| 3574 | 13 | bind_thm("not_Cons_self2",not_Cons_self RS not_sym);
 | 
| 14 | Addsimps [not_Cons_self,not_Cons_self2]; | |
| 923 | 15 | |
| 4935 | 16 | Goal "(xs ~= []) = (? y ys. xs = y#ys)"; | 
| 3040 
7d48671753da
Introduced a generic "induct_tac" which picks up the right induction scheme
 nipkow parents: 
3011diff
changeset | 17 | by (induct_tac "xs" 1); | 
| 5316 | 18 | by Auto_tac; | 
| 923 | 19 | qed "neq_Nil_conv"; | 
| 20 | ||
| 4830 | 21 | (* Induction over the length of a list: *) | 
| 4935 | 22 | val [prem] = Goal | 
| 4911 | 23 | "(!!xs. (!ys. length ys < length xs --> P ys) ==> P xs) ==> P(xs)"; | 
| 5132 | 24 | by (rtac measure_induct 1 THEN etac prem 1); | 
| 4911 | 25 | qed "length_induct"; | 
| 26 | ||
| 923 | 27 | |
| 3468 | 28 | (** "lists": the list-forming operator over sets **) | 
| 3342 
ec3b55fcb165
New operator "lists" for formalizing sets of lists
 paulson parents: 
3292diff
changeset | 29 | |
| 5043 | 30 | Goalw lists.defs "A<=B ==> lists A <= lists B"; | 
| 3342 
ec3b55fcb165
New operator "lists" for formalizing sets of lists
 paulson parents: 
3292diff
changeset | 31 | by (rtac lfp_mono 1); | 
| 
ec3b55fcb165
New operator "lists" for formalizing sets of lists
 paulson parents: 
3292diff
changeset | 32 | by (REPEAT (ares_tac basic_monos 1)); | 
| 
ec3b55fcb165
New operator "lists" for formalizing sets of lists
 paulson parents: 
3292diff
changeset | 33 | qed "lists_mono"; | 
| 3196 | 34 | |
| 6141 | 35 | val listsE = lists.mk_cases "x#l : lists A"; | 
| 3468 | 36 | AddSEs [listsE]; | 
| 37 | AddSIs lists.intrs; | |
| 38 | ||
| 5043 | 39 | Goal "l: lists A ==> l: lists B --> l: lists (A Int B)"; | 
| 3468 | 40 | by (etac lists.induct 1); | 
| 41 | by (ALLGOALS Blast_tac); | |
| 42 | qed_spec_mp "lists_IntI"; | |
| 43 | ||
| 4935 | 44 | Goal "lists (A Int B) = lists A Int lists B"; | 
| 4423 | 45 | by (rtac (mono_Int RS equalityI) 1); | 
| 4089 | 46 | by (simp_tac (simpset() addsimps [mono_def, lists_mono]) 1); | 
| 47 | by (blast_tac (claset() addSIs [lists_IntI]) 1); | |
| 3468 | 48 | qed "lists_Int_eq"; | 
| 49 | Addsimps [lists_Int_eq]; | |
| 50 | ||
| 3196 | 51 | |
| 4643 | 52 | (** Case analysis **) | 
| 53 | section "Case analysis"; | |
| 2608 | 54 | |
| 4935 | 55 | val prems = Goal "[| P([]); !!x xs. P(x#xs) |] ==> P(xs)"; | 
| 3457 | 56 | by (induct_tac "xs" 1); | 
| 57 | by (REPEAT(resolve_tac prems 1)); | |
| 2608 | 58 | qed "list_cases"; | 
| 59 | ||
| 4935 | 60 | Goal "(xs=[] --> P([])) & (!y ys. xs=y#ys --> P(y#ys)) --> P(xs)"; | 
| 3040 
7d48671753da
Introduced a generic "induct_tac" which picks up the right induction scheme
 nipkow parents: 
3011diff
changeset | 61 | by (induct_tac "xs" 1); | 
| 2891 | 62 | by (Blast_tac 1); | 
| 63 | by (Blast_tac 1); | |
| 2608 | 64 | bind_thm("list_eq_cases",
 | 
| 65 | impI RSN (2,allI RSN (2,allI RSN (2,impI RS (conjI RS (result() RS mp)))))); | |
| 66 | ||
| 3860 | 67 | (** length **) | 
| 68 | (* needs to come before "@" because of thm append_eq_append_conv *) | |
| 69 | ||
| 70 | section "length"; | |
| 71 | ||
| 4935 | 72 | Goal "length(xs@ys) = length(xs)+length(ys)"; | 
| 3860 | 73 | by (induct_tac "xs" 1); | 
| 5316 | 74 | by Auto_tac; | 
| 3860 | 75 | qed"length_append"; | 
| 76 | Addsimps [length_append]; | |
| 77 | ||
| 5129 | 78 | Goal "length (map f xs) = length xs"; | 
| 79 | by (induct_tac "xs" 1); | |
| 5316 | 80 | by Auto_tac; | 
| 3860 | 81 | qed "length_map"; | 
| 82 | Addsimps [length_map]; | |
| 83 | ||
| 4935 | 84 | Goal "length(rev xs) = length(xs)"; | 
| 3860 | 85 | by (induct_tac "xs" 1); | 
| 5316 | 86 | by Auto_tac; | 
| 3860 | 87 | qed "length_rev"; | 
| 88 | Addsimps [length_rev]; | |
| 89 | ||
| 5043 | 90 | Goal "xs ~= [] ==> length(tl xs) = (length xs) - 1"; | 
| 4423 | 91 | by (exhaust_tac "xs" 1); | 
| 5316 | 92 | by Auto_tac; | 
| 3896 
ee8ebb74ec00
Various new lemmas. Improved conversion of equations to rewrite rules:
 nipkow parents: 
3860diff
changeset | 93 | qed "length_tl"; | 
| 
ee8ebb74ec00
Various new lemmas. Improved conversion of equations to rewrite rules:
 nipkow parents: 
3860diff
changeset | 94 | Addsimps [length_tl]; | 
| 
ee8ebb74ec00
Various new lemmas. Improved conversion of equations to rewrite rules:
 nipkow parents: 
3860diff
changeset | 95 | |
| 4935 | 96 | Goal "(length xs = 0) = (xs = [])"; | 
| 3860 | 97 | by (induct_tac "xs" 1); | 
| 5316 | 98 | by Auto_tac; | 
| 3860 | 99 | qed "length_0_conv"; | 
| 100 | AddIffs [length_0_conv]; | |
| 101 | ||
| 4935 | 102 | Goal "(0 = length xs) = (xs = [])"; | 
| 3860 | 103 | by (induct_tac "xs" 1); | 
| 5316 | 104 | by Auto_tac; | 
| 3860 | 105 | qed "zero_length_conv"; | 
| 106 | AddIffs [zero_length_conv]; | |
| 107 | ||
| 4935 | 108 | Goal "(0 < length xs) = (xs ~= [])"; | 
| 3860 | 109 | by (induct_tac "xs" 1); | 
| 5316 | 110 | by Auto_tac; | 
| 3860 | 111 | qed "length_greater_0_conv"; | 
| 112 | AddIffs [length_greater_0_conv]; | |
| 113 | ||
| 5296 | 114 | Goal "(length xs = Suc n) = (? y ys. xs = y#ys & length ys = n)"; | 
| 115 | by (induct_tac "xs" 1); | |
| 6813 
bf90f86502b2
many new lemmas about take & drop, incl the famous take-lemma
 paulson parents: 
6794diff
changeset | 116 | by Auto_tac; | 
| 5296 | 117 | qed "length_Suc_conv"; | 
| 118 | ||
| 923 | 119 | (** @ - append **) | 
| 120 | ||
| 3467 | 121 | section "@ - append"; | 
| 122 | ||
| 4935 | 123 | Goal "(xs@ys)@zs = xs@(ys@zs)"; | 
| 3040 
7d48671753da
Introduced a generic "induct_tac" which picks up the right induction scheme
 nipkow parents: 
3011diff
changeset | 124 | by (induct_tac "xs" 1); | 
| 5316 | 125 | by Auto_tac; | 
| 923 | 126 | qed "append_assoc"; | 
| 2512 | 127 | Addsimps [append_assoc]; | 
| 923 | 128 | |
| 4935 | 129 | Goal "xs @ [] = xs"; | 
| 3040 
7d48671753da
Introduced a generic "induct_tac" which picks up the right induction scheme
 nipkow parents: 
3011diff
changeset | 130 | by (induct_tac "xs" 1); | 
| 5316 | 131 | by Auto_tac; | 
| 923 | 132 | qed "append_Nil2"; | 
| 2512 | 133 | Addsimps [append_Nil2]; | 
| 923 | 134 | |
| 4935 | 135 | Goal "(xs@ys = []) = (xs=[] & ys=[])"; | 
| 3040 
7d48671753da
Introduced a generic "induct_tac" which picks up the right induction scheme
 nipkow parents: 
3011diff
changeset | 136 | by (induct_tac "xs" 1); | 
| 5316 | 137 | by Auto_tac; | 
| 2608 | 138 | qed "append_is_Nil_conv"; | 
| 139 | AddIffs [append_is_Nil_conv]; | |
| 140 | ||
| 4935 | 141 | Goal "([] = xs@ys) = (xs=[] & ys=[])"; | 
| 3040 
7d48671753da
Introduced a generic "induct_tac" which picks up the right induction scheme
 nipkow parents: 
3011diff
changeset | 142 | by (induct_tac "xs" 1); | 
| 5316 | 143 | by Auto_tac; | 
| 2608 | 144 | qed "Nil_is_append_conv"; | 
| 145 | AddIffs [Nil_is_append_conv]; | |
| 923 | 146 | |
| 4935 | 147 | Goal "(xs @ ys = xs) = (ys=[])"; | 
| 3574 | 148 | by (induct_tac "xs" 1); | 
| 5316 | 149 | by Auto_tac; | 
| 3574 | 150 | qed "append_self_conv"; | 
| 151 | ||
| 4935 | 152 | Goal "(xs = xs @ ys) = (ys=[])"; | 
| 3574 | 153 | by (induct_tac "xs" 1); | 
| 5316 | 154 | by Auto_tac; | 
| 3574 | 155 | qed "self_append_conv"; | 
| 156 | AddIffs [append_self_conv,self_append_conv]; | |
| 157 | ||
| 4935 | 158 | Goal "!ys. length xs = length ys | length us = length vs \ | 
| 3860 | 159 | \ --> (xs@us = ys@vs) = (xs=ys & us=vs)"; | 
| 4423 | 160 | by (induct_tac "xs" 1); | 
| 161 | by (rtac allI 1); | |
| 162 | by (exhaust_tac "ys" 1); | |
| 163 | by (Asm_simp_tac 1); | |
| 5641 | 164 | by (Force_tac 1); | 
| 4423 | 165 | by (rtac allI 1); | 
| 166 | by (exhaust_tac "ys" 1); | |
| 5641 | 167 | by (Force_tac 1); | 
| 4423 | 168 | by (Asm_simp_tac 1); | 
| 3860 | 169 | qed_spec_mp "append_eq_append_conv"; | 
| 170 | Addsimps [append_eq_append_conv]; | |
| 171 | ||
| 4935 | 172 | Goal "(xs @ ys = xs @ zs) = (ys=zs)"; | 
| 3896 
ee8ebb74ec00
Various new lemmas. Improved conversion of equations to rewrite rules:
 nipkow parents: 
3860diff
changeset | 173 | by (Simp_tac 1); | 
| 
ee8ebb74ec00
Various new lemmas. Improved conversion of equations to rewrite rules:
 nipkow parents: 
3860diff
changeset | 174 | qed "same_append_eq"; | 
| 3860 | 175 | |
| 4935 | 176 | Goal "(xs @ [x] = ys @ [y]) = (xs = ys & x = y)"; | 
| 3896 
ee8ebb74ec00
Various new lemmas. Improved conversion of equations to rewrite rules:
 nipkow parents: 
3860diff
changeset | 177 | by (Simp_tac 1); | 
| 
ee8ebb74ec00
Various new lemmas. Improved conversion of equations to rewrite rules:
 nipkow parents: 
3860diff
changeset | 178 | qed "append1_eq_conv"; | 
| 2608 | 179 | |
| 4935 | 180 | Goal "(ys @ xs = zs @ xs) = (ys=zs)"; | 
| 3896 
ee8ebb74ec00
Various new lemmas. Improved conversion of equations to rewrite rules:
 nipkow parents: 
3860diff
changeset | 181 | by (Simp_tac 1); | 
| 
ee8ebb74ec00
Various new lemmas. Improved conversion of equations to rewrite rules:
 nipkow parents: 
3860diff
changeset | 182 | qed "append_same_eq"; | 
| 2608 | 183 | |
| 3896 
ee8ebb74ec00
Various new lemmas. Improved conversion of equations to rewrite rules:
 nipkow parents: 
3860diff
changeset | 184 | AddSIs | 
| 
ee8ebb74ec00
Various new lemmas. Improved conversion of equations to rewrite rules:
 nipkow parents: 
3860diff
changeset | 185 | [same_append_eq RS iffD2, append1_eq_conv RS iffD2, append_same_eq RS iffD2]; | 
| 
ee8ebb74ec00
Various new lemmas. Improved conversion of equations to rewrite rules:
 nipkow parents: 
3860diff
changeset | 186 | AddSDs | 
| 
ee8ebb74ec00
Various new lemmas. Improved conversion of equations to rewrite rules:
 nipkow parents: 
3860diff
changeset | 187 | [same_append_eq RS iffD1, append1_eq_conv RS iffD1, append_same_eq RS iffD1]; | 
| 3571 | 188 | |
| 4935 | 189 | Goal "(xs @ ys = ys) = (xs=[])"; | 
| 5132 | 190 | by (cut_inst_tac [("zs","[]")] append_same_eq 1);
 | 
| 5316 | 191 | by Auto_tac; | 
| 4647 | 192 | qed "append_self_conv2"; | 
| 193 | ||
| 4935 | 194 | Goal "(ys = xs @ ys) = (xs=[])"; | 
| 5132 | 195 | by (simp_tac (simpset() addsimps | 
| 4647 | 196 |      [simplify (simpset()) (read_instantiate[("ys","[]")]append_same_eq)]) 1);
 | 
| 5132 | 197 | by (Blast_tac 1); | 
| 4647 | 198 | qed "self_append_conv2"; | 
| 199 | AddIffs [append_self_conv2,self_append_conv2]; | |
| 200 | ||
| 4935 | 201 | Goal "xs ~= [] --> hd xs # tl xs = xs"; | 
| 3457 | 202 | by (induct_tac "xs" 1); | 
| 5316 | 203 | by Auto_tac; | 
| 2608 | 204 | qed_spec_mp "hd_Cons_tl"; | 
| 205 | Addsimps [hd_Cons_tl]; | |
| 923 | 206 | |
| 4935 | 207 | Goal "hd(xs@ys) = (if xs=[] then hd ys else hd xs)"; | 
| 3040 
7d48671753da
Introduced a generic "induct_tac" which picks up the right induction scheme
 nipkow parents: 
3011diff
changeset | 208 | by (induct_tac "xs" 1); | 
| 5316 | 209 | by Auto_tac; | 
| 1327 
6c29cfab679c
added new arithmetic lemmas and the functions take and drop.
 nipkow parents: 
1301diff
changeset | 210 | qed "hd_append"; | 
| 923 | 211 | |
| 5043 | 212 | Goal "xs ~= [] ==> hd(xs @ ys) = hd xs"; | 
| 4089 | 213 | by (asm_simp_tac (simpset() addsimps [hd_append] | 
| 5183 | 214 | addsplits [list.split]) 1); | 
| 3571 | 215 | qed "hd_append2"; | 
| 216 | Addsimps [hd_append2]; | |
| 217 | ||
| 4935 | 218 | Goal "tl(xs@ys) = (case xs of [] => tl(ys) | z#zs => zs@ys)"; | 
| 5183 | 219 | by (simp_tac (simpset() addsplits [list.split]) 1); | 
| 2608 | 220 | qed "tl_append"; | 
| 221 | ||
| 5043 | 222 | Goal "xs ~= [] ==> tl(xs @ ys) = (tl xs) @ ys"; | 
| 4089 | 223 | by (asm_simp_tac (simpset() addsimps [tl_append] | 
| 5183 | 224 | addsplits [list.split]) 1); | 
| 3571 | 225 | qed "tl_append2"; | 
| 226 | Addsimps [tl_append2]; | |
| 227 | ||
| 5272 | 228 | (* trivial rules for solving @-equations automatically *) | 
| 229 | ||
| 230 | Goal "xs = ys ==> xs = [] @ ys"; | |
| 5318 | 231 | by (Asm_simp_tac 1); | 
| 5272 | 232 | qed "eq_Nil_appendI"; | 
| 233 | ||
| 234 | Goal "[| x#xs1 = ys; xs = xs1 @ zs |] ==> x#xs = ys@zs"; | |
| 5318 | 235 | by (dtac sym 1); | 
| 236 | by (Asm_simp_tac 1); | |
| 5272 | 237 | qed "Cons_eq_appendI"; | 
| 238 | ||
| 239 | Goal "[| xs@xs1 = zs; ys = xs1 @ us |] ==> xs@ys = zs@us"; | |
| 5318 | 240 | by (dtac sym 1); | 
| 241 | by (Asm_simp_tac 1); | |
| 5272 | 242 | qed "append_eq_appendI"; | 
| 243 | ||
| 4830 | 244 | |
| 5427 | 245 | (*** | 
| 246 | Simplification procedure for all list equalities. | |
| 247 | Currently only tries to rearranges @ to see if | |
| 248 | - both lists end in a singleton list, | |
| 249 | - or both lists end in the same list. | |
| 250 | ***) | |
| 251 | local | |
| 252 | ||
| 253 | val list_eq_pattern = | |
| 6394 | 254 |   Thm.read_cterm (Theory.sign_of List.thy) ("(xs::'a list) = ys",HOLogic.boolT);
 | 
| 5427 | 255 | |
| 256 | fun last (cons as Const("List.list.op #",_) $ _ $ xs) =
 | |
| 257 |       (case xs of Const("List.list.[]",_) => cons | _ => last xs)
 | |
| 258 |   | last (Const("List.op @",_) $ _ $ ys) = last ys
 | |
| 259 | | last t = t; | |
| 260 | ||
| 261 | fun list1 (Const("List.list.op #",_) $ _ $ Const("List.list.[]",_)) = true
 | |
| 262 | | list1 _ = false; | |
| 263 | ||
| 264 | fun butlast ((cons as Const("List.list.op #",_) $ x) $ xs) =
 | |
| 265 |       (case xs of Const("List.list.[]",_) => xs | _ => cons $ butlast xs)
 | |
| 266 |   | butlast ((app as Const("List.op @",_) $ xs) $ ys) = app $ butlast ys
 | |
| 267 |   | butlast xs = Const("List.list.[]",fastype_of xs);
 | |
| 268 | ||
| 269 | val rearr_tac = | |
| 270 | simp_tac (HOL_basic_ss addsimps [append_assoc,append_Nil,append_Cons]); | |
| 271 | ||
| 272 | fun list_eq sg _ (F as (eq as Const(_,eqT)) $ lhs $ rhs) = | |
| 273 | let | |
| 274 | val lastl = last lhs and lastr = last rhs | |
| 275 | fun rearr conv = | |
| 276 | let val lhs1 = butlast lhs and rhs1 = butlast rhs | |
| 277 | val Type(_,listT::_) = eqT | |
| 278 | val appT = [listT,listT] ---> listT | |
| 279 |           val app = Const("List.op @",appT)
 | |
| 280 | val F2 = eq $ (app$lhs1$lastl) $ (app$rhs1$lastr) | |
| 281 | val ct = cterm_of sg (HOLogic.mk_Trueprop(HOLogic.mk_eq(F,F2))) | |
| 282 | val thm = prove_goalw_cterm [] ct (K [rearr_tac 1]) | |
| 283 | handle ERROR => | |
| 284 |             error("The error(s) above occurred while trying to prove " ^
 | |
| 285 | string_of_cterm ct) | |
| 286 | in Some((conv RS (thm RS trans)) RS eq_reflection) end | |
| 287 | ||
| 288 | in if list1 lastl andalso list1 lastr | |
| 289 | then rearr append1_eq_conv | |
| 290 | else | |
| 291 | if lastl aconv lastr | |
| 292 | then rearr append_same_eq | |
| 293 | else None | |
| 294 | end; | |
| 295 | in | |
| 296 | val list_eq_simproc = mk_simproc "list_eq" [list_eq_pattern] list_eq; | |
| 297 | end; | |
| 298 | ||
| 299 | Addsimprocs [list_eq_simproc]; | |
| 300 | ||
| 301 | ||
| 2608 | 302 | (** map **) | 
| 303 | ||
| 3467 | 304 | section "map"; | 
| 305 | ||
| 5278 | 306 | Goal "(!x. x : set xs --> f x = g x) --> map f xs = map g xs"; | 
| 3457 | 307 | by (induct_tac "xs" 1); | 
| 5316 | 308 | by Auto_tac; | 
| 2608 | 309 | bind_thm("map_ext", impI RS (allI RS (result() RS mp)));
 | 
| 310 | ||
| 4935 | 311 | Goal "map (%x. x) = (%xs. xs)"; | 
| 2608 | 312 | by (rtac ext 1); | 
| 3040 
7d48671753da
Introduced a generic "induct_tac" which picks up the right induction scheme
 nipkow parents: 
3011diff
changeset | 313 | by (induct_tac "xs" 1); | 
| 5316 | 314 | by Auto_tac; | 
| 2608 | 315 | qed "map_ident"; | 
| 316 | Addsimps[map_ident]; | |
| 317 | ||
| 4935 | 318 | Goal "map f (xs@ys) = map f xs @ map f ys"; | 
| 3040 
7d48671753da
Introduced a generic "induct_tac" which picks up the right induction scheme
 nipkow parents: 
3011diff
changeset | 319 | by (induct_tac "xs" 1); | 
| 5316 | 320 | by Auto_tac; | 
| 2608 | 321 | qed "map_append"; | 
| 322 | Addsimps[map_append]; | |
| 323 | ||
| 4935 | 324 | Goalw [o_def] "map (f o g) xs = map f (map g xs)"; | 
| 3040 
7d48671753da
Introduced a generic "induct_tac" which picks up the right induction scheme
 nipkow parents: 
3011diff
changeset | 325 | by (induct_tac "xs" 1); | 
| 5316 | 326 | by Auto_tac; | 
| 2608 | 327 | qed "map_compose"; | 
| 328 | Addsimps[map_compose]; | |
| 329 | ||
| 4935 | 330 | Goal "rev(map f xs) = map f (rev xs)"; | 
| 3040 
7d48671753da
Introduced a generic "induct_tac" which picks up the right induction scheme
 nipkow parents: 
3011diff
changeset | 331 | by (induct_tac "xs" 1); | 
| 5316 | 332 | by Auto_tac; | 
| 2608 | 333 | qed "rev_map"; | 
| 334 | ||
| 3589 
244daa75f890
Added function `replicate' and lemmas map_cong and set_replicate.
 nipkow parents: 
3586diff
changeset | 335 | (* a congruence rule for map: *) | 
| 6451 | 336 | Goal "xs=ys ==> (!x. x : set ys --> f x = g x) --> map f xs = map g ys"; | 
| 4423 | 337 | by (hyp_subst_tac 1); | 
| 338 | by (induct_tac "ys" 1); | |
| 5316 | 339 | by Auto_tac; | 
| 6451 | 340 | bind_thm("map_cong", impI RSN (2,allI RSN (2, result() RS mp)));
 | 
| 3589 
244daa75f890
Added function `replicate' and lemmas map_cong and set_replicate.
 nipkow parents: 
3586diff
changeset | 341 | |
| 4935 | 342 | Goal "(map f xs = []) = (xs = [])"; | 
| 4423 | 343 | by (induct_tac "xs" 1); | 
| 5316 | 344 | by Auto_tac; | 
| 3860 | 345 | qed "map_is_Nil_conv"; | 
| 346 | AddIffs [map_is_Nil_conv]; | |
| 347 | ||
| 4935 | 348 | Goal "([] = map f xs) = (xs = [])"; | 
| 4423 | 349 | by (induct_tac "xs" 1); | 
| 5316 | 350 | by Auto_tac; | 
| 3860 | 351 | qed "Nil_is_map_conv"; | 
| 352 | AddIffs [Nil_is_map_conv]; | |
| 353 | ||
| 354 | ||
| 1169 | 355 | (** rev **) | 
| 356 | ||
| 3467 | 357 | section "rev"; | 
| 358 | ||
| 4935 | 359 | Goal "rev(xs@ys) = rev(ys) @ rev(xs)"; | 
| 3040 
7d48671753da
Introduced a generic "induct_tac" which picks up the right induction scheme
 nipkow parents: 
3011diff
changeset | 360 | by (induct_tac "xs" 1); | 
| 5316 | 361 | by Auto_tac; | 
| 1169 | 362 | qed "rev_append"; | 
| 2512 | 363 | Addsimps[rev_append]; | 
| 1169 | 364 | |
| 4935 | 365 | Goal "rev(rev l) = l"; | 
| 3040 
7d48671753da
Introduced a generic "induct_tac" which picks up the right induction scheme
 nipkow parents: 
3011diff
changeset | 366 | by (induct_tac "l" 1); | 
| 5316 | 367 | by Auto_tac; | 
| 1169 | 368 | qed "rev_rev_ident"; | 
| 2512 | 369 | Addsimps[rev_rev_ident]; | 
| 1169 | 370 | |
| 4935 | 371 | Goal "(rev xs = []) = (xs = [])"; | 
| 4423 | 372 | by (induct_tac "xs" 1); | 
| 5316 | 373 | by Auto_tac; | 
| 3860 | 374 | qed "rev_is_Nil_conv"; | 
| 375 | AddIffs [rev_is_Nil_conv]; | |
| 376 | ||
| 4935 | 377 | Goal "([] = rev xs) = (xs = [])"; | 
| 4423 | 378 | by (induct_tac "xs" 1); | 
| 5316 | 379 | by Auto_tac; | 
| 3860 | 380 | qed "Nil_is_rev_conv"; | 
| 381 | AddIffs [Nil_is_rev_conv]; | |
| 382 | ||
| 6820 | 383 | Goal "!ys. (rev xs = rev ys) = (xs = ys)"; | 
| 6831 | 384 | by (induct_tac "xs" 1); | 
| 6820 | 385 | by (Force_tac 1); | 
| 6831 | 386 | by (rtac allI 1); | 
| 387 | by (exhaust_tac "ys" 1); | |
| 6820 | 388 | by (Asm_simp_tac 1); | 
| 389 | by (Force_tac 1); | |
| 390 | qed_spec_mp "rev_is_rev_conv"; | |
| 391 | AddIffs [rev_is_rev_conv]; | |
| 392 | ||
| 4935 | 393 | val prems = Goal "[| P []; !!x xs. P xs ==> P(xs@[x]) |] ==> P xs"; | 
| 5132 | 394 | by (stac (rev_rev_ident RS sym) 1); | 
| 6162 | 395 | by (res_inst_tac [("list", "rev xs")] list.induct 1);
 | 
| 5132 | 396 | by (ALLGOALS Simp_tac); | 
| 397 | by (resolve_tac prems 1); | |
| 398 | by (eresolve_tac prems 1); | |
| 4935 | 399 | qed "rev_induct"; | 
| 400 | ||
| 5272 | 401 | fun rev_induct_tac xs = res_inst_tac [("xs",xs)] rev_induct;
 | 
| 402 | ||
| 4935 | 403 | Goal "(xs = [] --> P) --> (!ys y. xs = ys@[y] --> P) --> P"; | 
| 5132 | 404 | by (res_inst_tac [("xs","xs")] rev_induct 1);
 | 
| 5316 | 405 | by Auto_tac; | 
| 4935 | 406 | bind_thm ("rev_exhaust",
 | 
| 407 | impI RSN (2,allI RSN (2,allI RSN (2,impI RS (result() RS mp RS mp))))); | |
| 408 | ||
| 2608 | 409 | |
| 3465 | 410 | (** set **) | 
| 1812 | 411 | |
| 3467 | 412 | section "set"; | 
| 413 | ||
| 5296 | 414 | qed_goal "finite_set" thy "finite (set xs)" | 
| 415 | (K [induct_tac "xs" 1, Auto_tac]); | |
| 416 | Addsimps[finite_set]; | |
| 417 | AddSIs[finite_set]; | |
| 418 | ||
| 4935 | 419 | Goal "set (xs@ys) = (set xs Un set ys)"; | 
| 3040 
7d48671753da
Introduced a generic "induct_tac" which picks up the right induction scheme
 nipkow parents: 
3011diff
changeset | 420 | by (induct_tac "xs" 1); | 
| 5316 | 421 | by Auto_tac; | 
| 3647 
a64c8fbcd98f
Renamed theorems of the form set_of_list_XXX to set_XXX
 paulson parents: 
3589diff
changeset | 422 | qed "set_append"; | 
| 
a64c8fbcd98f
Renamed theorems of the form set_of_list_XXX to set_XXX
 paulson parents: 
3589diff
changeset | 423 | Addsimps[set_append]; | 
| 1812 | 424 | |
| 4935 | 425 | Goal "set l <= set (x#l)"; | 
| 5316 | 426 | by Auto_tac; | 
| 3647 
a64c8fbcd98f
Renamed theorems of the form set_of_list_XXX to set_XXX
 paulson parents: 
3589diff
changeset | 427 | qed "set_subset_Cons"; | 
| 1936 | 428 | |
| 4935 | 429 | Goal "(set xs = {}) = (xs = [])";
 | 
| 3457 | 430 | by (induct_tac "xs" 1); | 
| 5316 | 431 | by Auto_tac; | 
| 3647 
a64c8fbcd98f
Renamed theorems of the form set_of_list_XXX to set_XXX
 paulson parents: 
3589diff
changeset | 432 | qed "set_empty"; | 
| 
a64c8fbcd98f
Renamed theorems of the form set_of_list_XXX to set_XXX
 paulson parents: 
3589diff
changeset | 433 | Addsimps [set_empty]; | 
| 2608 | 434 | |
| 4935 | 435 | Goal "set(rev xs) = set(xs)"; | 
| 3457 | 436 | by (induct_tac "xs" 1); | 
| 5316 | 437 | by Auto_tac; | 
| 3647 
a64c8fbcd98f
Renamed theorems of the form set_of_list_XXX to set_XXX
 paulson parents: 
3589diff
changeset | 438 | qed "set_rev"; | 
| 
a64c8fbcd98f
Renamed theorems of the form set_of_list_XXX to set_XXX
 paulson parents: 
3589diff
changeset | 439 | Addsimps [set_rev]; | 
| 2608 | 440 | |
| 4935 | 441 | Goal "set(map f xs) = f``(set xs)"; | 
| 3457 | 442 | by (induct_tac "xs" 1); | 
| 5316 | 443 | by Auto_tac; | 
| 3647 
a64c8fbcd98f
Renamed theorems of the form set_of_list_XXX to set_XXX
 paulson parents: 
3589diff
changeset | 444 | qed "set_map"; | 
| 
a64c8fbcd98f
Renamed theorems of the form set_of_list_XXX to set_XXX
 paulson parents: 
3589diff
changeset | 445 | Addsimps [set_map]; | 
| 2608 | 446 | |
| 6433 | 447 | Goal "set(filter P xs) = {x. x : set xs & P x}";
 | 
| 6813 
bf90f86502b2
many new lemmas about take & drop, incl the famous take-lemma
 paulson parents: 
6794diff
changeset | 448 | by (induct_tac "xs" 1); | 
| 
bf90f86502b2
many new lemmas about take & drop, incl the famous take-lemma
 paulson parents: 
6794diff
changeset | 449 | by Auto_tac; | 
| 6433 | 450 | qed "set_filter"; | 
| 451 | Addsimps [set_filter]; | |
| 452 | (* | |
| 5443 
e2459d18ff47
changed constants mem and list_all to mere translations
 oheimb parents: 
5427diff
changeset | 453 | Goal "(x : set (filter P xs)) = (x : set xs & P x)"; | 
| 4605 | 454 | by (induct_tac "xs" 1); | 
| 5316 | 455 | by Auto_tac; | 
| 4605 | 456 | qed "in_set_filter"; | 
| 457 | Addsimps [in_set_filter]; | |
| 6433 | 458 | *) | 
| 459 | Goal "set[i..j(] = {k. i <= k & k < j}";
 | |
| 6813 
bf90f86502b2
many new lemmas about take & drop, incl the famous take-lemma
 paulson parents: 
6794diff
changeset | 460 | by (induct_tac "j" 1); | 
| 
bf90f86502b2
many new lemmas about take & drop, incl the famous take-lemma
 paulson parents: 
6794diff
changeset | 461 | by Auto_tac; | 
| 
bf90f86502b2
many new lemmas about take & drop, incl the famous take-lemma
 paulson parents: 
6794diff
changeset | 462 | by (arith_tac 1); | 
| 6433 | 463 | qed "set_upt"; | 
| 464 | Addsimps [set_upt]; | |
| 465 | ||
| 466 | Goal "!i < size xs. set(xs[i := x]) <= insert x (set xs)"; | |
| 6813 
bf90f86502b2
many new lemmas about take & drop, incl the famous take-lemma
 paulson parents: 
6794diff
changeset | 467 | by (induct_tac "xs" 1); | 
| 
bf90f86502b2
many new lemmas about take & drop, incl the famous take-lemma
 paulson parents: 
6794diff
changeset | 468 | by (Simp_tac 1); | 
| 
bf90f86502b2
many new lemmas about take & drop, incl the famous take-lemma
 paulson parents: 
6794diff
changeset | 469 | by (asm_simp_tac (simpset() addsplits [nat.split]) 1); | 
| 
bf90f86502b2
many new lemmas about take & drop, incl the famous take-lemma
 paulson parents: 
6794diff
changeset | 470 | by (Blast_tac 1); | 
| 6433 | 471 | qed_spec_mp "set_list_update_subset"; | 
| 4605 | 472 | |
| 5272 | 473 | Goal "(x : set xs) = (? ys zs. xs = ys@x#zs)"; | 
| 5318 | 474 | by (induct_tac "xs" 1); | 
| 475 | by (Simp_tac 1); | |
| 476 | by (Asm_simp_tac 1); | |
| 477 | by (rtac iffI 1); | |
| 478 | by (blast_tac (claset() addIs [eq_Nil_appendI,Cons_eq_appendI]) 1); | |
| 479 | by (REPEAT(etac exE 1)); | |
| 480 | by (exhaust_tac "ys" 1); | |
| 5316 | 481 | by Auto_tac; | 
| 5272 | 482 | qed "in_set_conv_decomp"; | 
| 483 | ||
| 484 | (* eliminate `lists' in favour of `set' *) | |
| 485 | ||
| 486 | Goal "(xs : lists A) = (!x : set xs. x : A)"; | |
| 5318 | 487 | by (induct_tac "xs" 1); | 
| 5316 | 488 | by Auto_tac; | 
| 5272 | 489 | qed "in_lists_conv_set"; | 
| 490 | ||
| 491 | bind_thm("in_listsD",in_lists_conv_set RS iffD1);
 | |
| 492 | AddSDs [in_listsD]; | |
| 493 | bind_thm("in_listsI",in_lists_conv_set RS iffD2);
 | |
| 494 | AddSIs [in_listsI]; | |
| 1812 | 495 | |
| 5518 | 496 | (** mem **) | 
| 497 | ||
| 498 | section "mem"; | |
| 499 | ||
| 500 | Goal "(x mem xs) = (x: set xs)"; | |
| 501 | by (induct_tac "xs" 1); | |
| 502 | by Auto_tac; | |
| 503 | qed "set_mem_eq"; | |
| 504 | ||
| 505 | ||
| 923 | 506 | (** list_all **) | 
| 507 | ||
| 3467 | 508 | section "list_all"; | 
| 509 | ||
| 5518 | 510 | Goal "list_all P xs = (!x:set xs. P x)"; | 
| 511 | by (induct_tac "xs" 1); | |
| 512 | by Auto_tac; | |
| 513 | qed "list_all_conv"; | |
| 514 | ||
| 5443 
e2459d18ff47
changed constants mem and list_all to mere translations
 oheimb parents: 
5427diff
changeset | 515 | Goal "list_all P (xs@ys) = (list_all P xs & list_all P ys)"; | 
| 3040 
7d48671753da
Introduced a generic "induct_tac" which picks up the right induction scheme
 nipkow parents: 
3011diff
changeset | 516 | by (induct_tac "xs" 1); | 
| 5316 | 517 | by Auto_tac; | 
| 2512 | 518 | qed "list_all_append"; | 
| 519 | Addsimps [list_all_append]; | |
| 923 | 520 | |
| 521 | ||
| 2608 | 522 | (** filter **) | 
| 923 | 523 | |
| 3467 | 524 | section "filter"; | 
| 525 | ||
| 4935 | 526 | Goal "filter P (xs@ys) = filter P xs @ filter P ys"; | 
| 3457 | 527 | by (induct_tac "xs" 1); | 
| 5316 | 528 | by Auto_tac; | 
| 2608 | 529 | qed "filter_append"; | 
| 530 | Addsimps [filter_append]; | |
| 531 | ||
| 4935 | 532 | Goal "filter (%x. True) xs = xs"; | 
| 4605 | 533 | by (induct_tac "xs" 1); | 
| 5316 | 534 | by Auto_tac; | 
| 4605 | 535 | qed "filter_True"; | 
| 536 | Addsimps [filter_True]; | |
| 537 | ||
| 4935 | 538 | Goal "filter (%x. False) xs = []"; | 
| 4605 | 539 | by (induct_tac "xs" 1); | 
| 5316 | 540 | by Auto_tac; | 
| 4605 | 541 | qed "filter_False"; | 
| 542 | Addsimps [filter_False]; | |
| 543 | ||
| 4935 | 544 | Goal "length (filter P xs) <= length xs"; | 
| 3457 | 545 | by (induct_tac "xs" 1); | 
| 5316 | 546 | by Auto_tac; | 
| 4605 | 547 | qed "length_filter"; | 
| 5443 
e2459d18ff47
changed constants mem and list_all to mere translations
 oheimb parents: 
5427diff
changeset | 548 | Addsimps[length_filter]; | 
| 2608 | 549 | |
| 5443 
e2459d18ff47
changed constants mem and list_all to mere translations
 oheimb parents: 
5427diff
changeset | 550 | Goal "set (filter P xs) <= set xs"; | 
| 
e2459d18ff47
changed constants mem and list_all to mere translations
 oheimb parents: 
5427diff
changeset | 551 | by Auto_tac; | 
| 
e2459d18ff47
changed constants mem and list_all to mere translations
 oheimb parents: 
5427diff
changeset | 552 | qed "filter_is_subset"; | 
| 
e2459d18ff47
changed constants mem and list_all to mere translations
 oheimb parents: 
5427diff
changeset | 553 | Addsimps [filter_is_subset]; | 
| 
e2459d18ff47
changed constants mem and list_all to mere translations
 oheimb parents: 
5427diff
changeset | 554 | |
| 2608 | 555 | |
| 3467 | 556 | section "concat"; | 
| 557 | ||
| 4935 | 558 | Goal "concat(xs@ys) = concat(xs)@concat(ys)"; | 
| 3040 
7d48671753da
Introduced a generic "induct_tac" which picks up the right induction scheme
 nipkow parents: 
3011diff
changeset | 559 | by (induct_tac "xs" 1); | 
| 5316 | 560 | by Auto_tac; | 
| 2608 | 561 | qed"concat_append"; | 
| 562 | Addsimps [concat_append]; | |
| 2512 | 563 | |
| 4935 | 564 | Goal "(concat xss = []) = (!xs:set xss. xs=[])"; | 
| 4423 | 565 | by (induct_tac "xss" 1); | 
| 5316 | 566 | by Auto_tac; | 
| 3896 
ee8ebb74ec00
Various new lemmas. Improved conversion of equations to rewrite rules:
 nipkow parents: 
3860diff
changeset | 567 | qed "concat_eq_Nil_conv"; | 
| 
ee8ebb74ec00
Various new lemmas. Improved conversion of equations to rewrite rules:
 nipkow parents: 
3860diff
changeset | 568 | AddIffs [concat_eq_Nil_conv]; | 
| 
ee8ebb74ec00
Various new lemmas. Improved conversion of equations to rewrite rules:
 nipkow parents: 
3860diff
changeset | 569 | |
| 4935 | 570 | Goal "([] = concat xss) = (!xs:set xss. xs=[])"; | 
| 4423 | 571 | by (induct_tac "xss" 1); | 
| 5316 | 572 | by Auto_tac; | 
| 3896 
ee8ebb74ec00
Various new lemmas. Improved conversion of equations to rewrite rules:
 nipkow parents: 
3860diff
changeset | 573 | qed "Nil_eq_concat_conv"; | 
| 
ee8ebb74ec00
Various new lemmas. Improved conversion of equations to rewrite rules:
 nipkow parents: 
3860diff
changeset | 574 | AddIffs [Nil_eq_concat_conv]; | 
| 
ee8ebb74ec00
Various new lemmas. Improved conversion of equations to rewrite rules:
 nipkow parents: 
3860diff
changeset | 575 | |
| 4935 | 576 | Goal "set(concat xs) = Union(set `` set xs)"; | 
| 3467 | 577 | by (induct_tac "xs" 1); | 
| 5316 | 578 | by Auto_tac; | 
| 3647 
a64c8fbcd98f
Renamed theorems of the form set_of_list_XXX to set_XXX
 paulson parents: 
3589diff
changeset | 579 | qed"set_concat"; | 
| 
a64c8fbcd98f
Renamed theorems of the form set_of_list_XXX to set_XXX
 paulson parents: 
3589diff
changeset | 580 | Addsimps [set_concat]; | 
| 3467 | 581 | |
| 4935 | 582 | Goal "map f (concat xs) = concat (map (map f) xs)"; | 
| 3467 | 583 | by (induct_tac "xs" 1); | 
| 5316 | 584 | by Auto_tac; | 
| 3467 | 585 | qed "map_concat"; | 
| 586 | ||
| 4935 | 587 | Goal "filter p (concat xs) = concat (map (filter p) xs)"; | 
| 3467 | 588 | by (induct_tac "xs" 1); | 
| 5316 | 589 | by Auto_tac; | 
| 3467 | 590 | qed"filter_concat"; | 
| 591 | ||
| 4935 | 592 | Goal "rev(concat xs) = concat (map rev (rev xs))"; | 
| 3467 | 593 | by (induct_tac "xs" 1); | 
| 5316 | 594 | by Auto_tac; | 
| 2608 | 595 | qed "rev_concat"; | 
| 923 | 596 | |
| 597 | (** nth **) | |
| 598 | ||
| 3467 | 599 | section "nth"; | 
| 600 | ||
| 6408 | 601 | Goal "(x#xs)!0 = x"; | 
| 602 | by Auto_tac; | |
| 603 | qed "nth_Cons_0"; | |
| 604 | Addsimps [nth_Cons_0]; | |
| 5644 | 605 | |
| 6408 | 606 | Goal "(x#xs)!(Suc n) = xs!n"; | 
| 607 | by Auto_tac; | |
| 608 | qed "nth_Cons_Suc"; | |
| 609 | Addsimps [nth_Cons_Suc]; | |
| 610 | ||
| 611 | Delsimps (thms "nth.simps"); | |
| 612 | ||
| 613 | Goal "!n. (xs@ys)!n = (if n < length xs then xs!n else ys!(n - length xs))"; | |
| 614 | by (induct_tac "xs" 1); | |
| 3457 | 615 | by (Asm_simp_tac 1); | 
| 616 | by (rtac allI 1); | |
| 6408 | 617 | by (exhaust_tac "n" 1); | 
| 5316 | 618 | by Auto_tac; | 
| 2608 | 619 | qed_spec_mp "nth_append"; | 
| 620 | ||
| 4935 | 621 | Goal "!n. n < length xs --> (map f xs)!n = f(xs!n)"; | 
| 3040 
7d48671753da
Introduced a generic "induct_tac" which picks up the right induction scheme
 nipkow parents: 
3011diff
changeset | 622 | by (induct_tac "xs" 1); | 
| 1301 | 623 | (* case [] *) | 
| 624 | by (Asm_full_simp_tac 1); | |
| 625 | (* case x#xl *) | |
| 626 | by (rtac allI 1); | |
| 5183 | 627 | by (induct_tac "n" 1); | 
| 5316 | 628 | by Auto_tac; | 
| 1485 
240cc98b94a7
Added qed_spec_mp to avoid renaming of bound vars in 'th RS spec'
 nipkow parents: 
1465diff
changeset | 629 | qed_spec_mp "nth_map"; | 
| 1301 | 630 | Addsimps [nth_map]; | 
| 631 | ||
| 5518 | 632 | Goal "!n. n < length xs --> Ball (set xs) P --> P(xs!n)"; | 
| 3040 
7d48671753da
Introduced a generic "induct_tac" which picks up the right induction scheme
 nipkow parents: 
3011diff
changeset | 633 | by (induct_tac "xs" 1); | 
| 1301 | 634 | (* case [] *) | 
| 635 | by (Simp_tac 1); | |
| 636 | (* case x#xl *) | |
| 637 | by (rtac allI 1); | |
| 5183 | 638 | by (induct_tac "n" 1); | 
| 5316 | 639 | by Auto_tac; | 
| 5518 | 640 | qed_spec_mp "list_ball_nth"; | 
| 1301 | 641 | |
| 5518 | 642 | Goal "!n. n < length xs --> xs!n : set xs"; | 
| 3040 
7d48671753da
Introduced a generic "induct_tac" which picks up the right induction scheme
 nipkow parents: 
3011diff
changeset | 643 | by (induct_tac "xs" 1); | 
| 1301 | 644 | (* case [] *) | 
| 645 | by (Simp_tac 1); | |
| 646 | (* case x#xl *) | |
| 647 | by (rtac allI 1); | |
| 5183 | 648 | by (induct_tac "n" 1); | 
| 1301 | 649 | (* case 0 *) | 
| 650 | by (Asm_full_simp_tac 1); | |
| 651 | (* case Suc x *) | |
| 4686 | 652 | by (Asm_full_simp_tac 1); | 
| 1485 
240cc98b94a7
Added qed_spec_mp to avoid renaming of bound vars in 'th RS spec'
 nipkow parents: 
1465diff
changeset | 653 | qed_spec_mp "nth_mem"; | 
| 1301 | 654 | Addsimps [nth_mem]; | 
| 655 | ||
| 5518 | 656 | |
| 5077 
71043526295f
* HOL/List: new function list_update written xs[i:=v] that updates the i-th
 nipkow parents: 
5043diff
changeset | 657 | (** list update **) | 
| 
71043526295f
* HOL/List: new function list_update written xs[i:=v] that updates the i-th
 nipkow parents: 
5043diff
changeset | 658 | |
| 
71043526295f
* HOL/List: new function list_update written xs[i:=v] that updates the i-th
 nipkow parents: 
5043diff
changeset | 659 | section "list update"; | 
| 
71043526295f
* HOL/List: new function list_update written xs[i:=v] that updates the i-th
 nipkow parents: 
5043diff
changeset | 660 | |
| 
71043526295f
* HOL/List: new function list_update written xs[i:=v] that updates the i-th
 nipkow parents: 
5043diff
changeset | 661 | Goal "!i. length(xs[i:=x]) = length xs"; | 
| 
71043526295f
* HOL/List: new function list_update written xs[i:=v] that updates the i-th
 nipkow parents: 
5043diff
changeset | 662 | by (induct_tac "xs" 1); | 
| 
71043526295f
* HOL/List: new function list_update written xs[i:=v] that updates the i-th
 nipkow parents: 
5043diff
changeset | 663 | by (Simp_tac 1); | 
| 5183 | 664 | by (asm_full_simp_tac (simpset() addsplits [nat.split]) 1); | 
| 5077 
71043526295f
* HOL/List: new function list_update written xs[i:=v] that updates the i-th
 nipkow parents: 
5043diff
changeset | 665 | qed_spec_mp "length_list_update"; | 
| 
71043526295f
* HOL/List: new function list_update written xs[i:=v] that updates the i-th
 nipkow parents: 
5043diff
changeset | 666 | Addsimps [length_list_update]; | 
| 
71043526295f
* HOL/List: new function list_update written xs[i:=v] that updates the i-th
 nipkow parents: 
5043diff
changeset | 667 | |
| 5644 | 668 | Goal "!i j. i < length xs --> (xs[i:=x])!j = (if i=j then x else xs!j)"; | 
| 6162 | 669 | by (induct_tac "xs" 1); | 
| 670 | by (Simp_tac 1); | |
| 671 | by (auto_tac (claset(), simpset() addsimps [nth_Cons] addsplits [nat.split])); | |
| 5644 | 672 | qed_spec_mp "nth_list_update"; | 
| 673 | ||
| 6433 | 674 | Goal "!i. i < size xs --> xs[i:=x, i:=y] = xs[i:=y]"; | 
| 6813 
bf90f86502b2
many new lemmas about take & drop, incl the famous take-lemma
 paulson parents: 
6794diff
changeset | 675 | by (induct_tac "xs" 1); | 
| 
bf90f86502b2
many new lemmas about take & drop, incl the famous take-lemma
 paulson parents: 
6794diff
changeset | 676 | by (Simp_tac 1); | 
| 
bf90f86502b2
many new lemmas about take & drop, incl the famous take-lemma
 paulson parents: 
6794diff
changeset | 677 | by (asm_simp_tac (simpset() addsplits [nat.split]) 1); | 
| 6433 | 678 | qed_spec_mp "list_update_overwrite"; | 
| 679 | Addsimps [list_update_overwrite]; | |
| 680 | ||
| 681 | Goal "!i < length xs. (xs[i := x] = xs) = (xs!i = x)"; | |
| 6813 
bf90f86502b2
many new lemmas about take & drop, incl the famous take-lemma
 paulson parents: 
6794diff
changeset | 682 | by (induct_tac "xs" 1); | 
| 
bf90f86502b2
many new lemmas about take & drop, incl the famous take-lemma
 paulson parents: 
6794diff
changeset | 683 | by (Simp_tac 1); | 
| 
bf90f86502b2
many new lemmas about take & drop, incl the famous take-lemma
 paulson parents: 
6794diff
changeset | 684 | by (simp_tac (simpset() addsplits [nat.split]) 1); | 
| 
bf90f86502b2
many new lemmas about take & drop, incl the famous take-lemma
 paulson parents: 
6794diff
changeset | 685 | by (Blast_tac 1); | 
| 6433 | 686 | qed_spec_mp "list_update_same_conv"; | 
| 687 | ||
| 5077 
71043526295f
* HOL/List: new function list_update written xs[i:=v] that updates the i-th
 nipkow parents: 
5043diff
changeset | 688 | |
| 3896 
ee8ebb74ec00
Various new lemmas. Improved conversion of equations to rewrite rules:
 nipkow parents: 
3860diff
changeset | 689 | (** last & butlast **) | 
| 1327 
6c29cfab679c
added new arithmetic lemmas and the functions take and drop.
 nipkow parents: 
1301diff
changeset | 690 | |
| 5644 | 691 | section "last / butlast"; | 
| 692 | ||
| 4935 | 693 | Goal "last(xs@[x]) = x"; | 
| 4423 | 694 | by (induct_tac "xs" 1); | 
| 5316 | 695 | by Auto_tac; | 
| 3896 
ee8ebb74ec00
Various new lemmas. Improved conversion of equations to rewrite rules:
 nipkow parents: 
3860diff
changeset | 696 | qed "last_snoc"; | 
| 
ee8ebb74ec00
Various new lemmas. Improved conversion of equations to rewrite rules:
 nipkow parents: 
3860diff
changeset | 697 | Addsimps [last_snoc]; | 
| 
ee8ebb74ec00
Various new lemmas. Improved conversion of equations to rewrite rules:
 nipkow parents: 
3860diff
changeset | 698 | |
| 4935 | 699 | Goal "butlast(xs@[x]) = xs"; | 
| 4423 | 700 | by (induct_tac "xs" 1); | 
| 5316 | 701 | by Auto_tac; | 
| 3896 
ee8ebb74ec00
Various new lemmas. Improved conversion of equations to rewrite rules:
 nipkow parents: 
3860diff
changeset | 702 | qed "butlast_snoc"; | 
| 
ee8ebb74ec00
Various new lemmas. Improved conversion of equations to rewrite rules:
 nipkow parents: 
3860diff
changeset | 703 | Addsimps [butlast_snoc]; | 
| 
ee8ebb74ec00
Various new lemmas. Improved conversion of equations to rewrite rules:
 nipkow parents: 
3860diff
changeset | 704 | |
| 4935 | 705 | Goal "length(butlast xs) = length xs - 1"; | 
| 706 | by (res_inst_tac [("xs","xs")] rev_induct 1);
 | |
| 5316 | 707 | by Auto_tac; | 
| 4643 | 708 | qed "length_butlast"; | 
| 709 | Addsimps [length_butlast]; | |
| 710 | ||
| 5278 | 711 | Goal "!ys. butlast (xs@ys) = (if ys=[] then butlast xs else xs@butlast ys)"; | 
| 4423 | 712 | by (induct_tac "xs" 1); | 
| 5316 | 713 | by Auto_tac; | 
| 3896 
ee8ebb74ec00
Various new lemmas. Improved conversion of equations to rewrite rules:
 nipkow parents: 
3860diff
changeset | 714 | qed_spec_mp "butlast_append"; | 
| 
ee8ebb74ec00
Various new lemmas. Improved conversion of equations to rewrite rules:
 nipkow parents: 
3860diff
changeset | 715 | |
| 4935 | 716 | Goal "x:set(butlast xs) --> x:set xs"; | 
| 4423 | 717 | by (induct_tac "xs" 1); | 
| 5316 | 718 | by Auto_tac; | 
| 3896 
ee8ebb74ec00
Various new lemmas. Improved conversion of equations to rewrite rules:
 nipkow parents: 
3860diff
changeset | 719 | qed_spec_mp "in_set_butlastD"; | 
| 
ee8ebb74ec00
Various new lemmas. Improved conversion of equations to rewrite rules:
 nipkow parents: 
3860diff
changeset | 720 | |
| 5448 
40a09282ba14
in_set_butlast_appendI supersedes in_set_butlast_appendI1,2
 paulson parents: 
5443diff
changeset | 721 | Goal "x:set(butlast xs) | x:set(butlast ys) ==> x:set(butlast(xs@ys))"; | 
| 
40a09282ba14
in_set_butlast_appendI supersedes in_set_butlast_appendI1,2
 paulson parents: 
5443diff
changeset | 722 | by (auto_tac (claset() addDs [in_set_butlastD], | 
| 
40a09282ba14
in_set_butlast_appendI supersedes in_set_butlast_appendI1,2
 paulson parents: 
5443diff
changeset | 723 | simpset() addsimps [butlast_append])); | 
| 
40a09282ba14
in_set_butlast_appendI supersedes in_set_butlast_appendI1,2
 paulson parents: 
5443diff
changeset | 724 | qed "in_set_butlast_appendI"; | 
| 3902 | 725 | |
| 2608 | 726 | (** take & drop **) | 
| 727 | section "take & drop"; | |
| 1327 
6c29cfab679c
added new arithmetic lemmas and the functions take and drop.
 nipkow parents: 
1301diff
changeset | 728 | |
| 4935 | 729 | Goal "take 0 xs = []"; | 
| 3040 
7d48671753da
Introduced a generic "induct_tac" which picks up the right induction scheme
 nipkow parents: 
3011diff
changeset | 730 | by (induct_tac "xs" 1); | 
| 5316 | 731 | by Auto_tac; | 
| 1327 
6c29cfab679c
added new arithmetic lemmas and the functions take and drop.
 nipkow parents: 
1301diff
changeset | 732 | qed "take_0"; | 
| 
6c29cfab679c
added new arithmetic lemmas and the functions take and drop.
 nipkow parents: 
1301diff
changeset | 733 | |
| 4935 | 734 | Goal "drop 0 xs = xs"; | 
| 3040 
7d48671753da
Introduced a generic "induct_tac" which picks up the right induction scheme
 nipkow parents: 
3011diff
changeset | 735 | by (induct_tac "xs" 1); | 
| 5316 | 736 | by Auto_tac; | 
| 2608 | 737 | qed "drop_0"; | 
| 738 | ||
| 4935 | 739 | Goal "take (Suc n) (x#xs) = x # take n xs"; | 
| 1552 | 740 | by (Simp_tac 1); | 
| 1419 
a6a034a47a71
defined take/drop by induction over list rather than nat.
 nipkow parents: 
1327diff
changeset | 741 | qed "take_Suc_Cons"; | 
| 1327 
6c29cfab679c
added new arithmetic lemmas and the functions take and drop.
 nipkow parents: 
1301diff
changeset | 742 | |
| 4935 | 743 | Goal "drop (Suc n) (x#xs) = drop n xs"; | 
| 2608 | 744 | by (Simp_tac 1); | 
| 745 | qed "drop_Suc_Cons"; | |
| 746 | ||
| 747 | Delsimps [take_Cons,drop_Cons]; | |
| 748 | Addsimps [take_0,take_Suc_Cons,drop_0,drop_Suc_Cons]; | |
| 749 | ||
| 4935 | 750 | Goal "!xs. length(take n xs) = min (length xs) n"; | 
| 5183 | 751 | by (induct_tac "n" 1); | 
| 5316 | 752 | by Auto_tac; | 
| 3457 | 753 | by (exhaust_tac "xs" 1); | 
| 5316 | 754 | by Auto_tac; | 
| 2608 | 755 | qed_spec_mp "length_take"; | 
| 756 | Addsimps [length_take]; | |
| 923 | 757 | |
| 4935 | 758 | Goal "!xs. length(drop n xs) = (length xs - n)"; | 
| 5183 | 759 | by (induct_tac "n" 1); | 
| 5316 | 760 | by Auto_tac; | 
| 3457 | 761 | by (exhaust_tac "xs" 1); | 
| 5316 | 762 | by Auto_tac; | 
| 2608 | 763 | qed_spec_mp "length_drop"; | 
| 764 | Addsimps [length_drop]; | |
| 765 | ||
| 4935 | 766 | Goal "!xs. length xs <= n --> take n xs = xs"; | 
| 5183 | 767 | by (induct_tac "n" 1); | 
| 5316 | 768 | by Auto_tac; | 
| 3457 | 769 | by (exhaust_tac "xs" 1); | 
| 5316 | 770 | by Auto_tac; | 
| 2608 | 771 | qed_spec_mp "take_all"; | 
| 923 | 772 | |
| 4935 | 773 | Goal "!xs. length xs <= n --> drop n xs = []"; | 
| 5183 | 774 | by (induct_tac "n" 1); | 
| 5316 | 775 | by Auto_tac; | 
| 3457 | 776 | by (exhaust_tac "xs" 1); | 
| 5316 | 777 | by Auto_tac; | 
| 2608 | 778 | qed_spec_mp "drop_all"; | 
| 779 | ||
| 5278 | 780 | Goal "!xs. take n (xs @ ys) = (take n xs @ take (n - length xs) ys)"; | 
| 5183 | 781 | by (induct_tac "n" 1); | 
| 5316 | 782 | by Auto_tac; | 
| 3457 | 783 | by (exhaust_tac "xs" 1); | 
| 5316 | 784 | by Auto_tac; | 
| 2608 | 785 | qed_spec_mp "take_append"; | 
| 786 | Addsimps [take_append]; | |
| 787 | ||
| 4935 | 788 | Goal "!xs. drop n (xs@ys) = drop n xs @ drop (n - length xs) ys"; | 
| 5183 | 789 | by (induct_tac "n" 1); | 
| 5316 | 790 | by Auto_tac; | 
| 3457 | 791 | by (exhaust_tac "xs" 1); | 
| 5316 | 792 | by Auto_tac; | 
| 2608 | 793 | qed_spec_mp "drop_append"; | 
| 794 | Addsimps [drop_append]; | |
| 795 | ||
| 4935 | 796 | Goal "!xs n. take n (take m xs) = take (min n m) xs"; | 
| 5183 | 797 | by (induct_tac "m" 1); | 
| 5316 | 798 | by Auto_tac; | 
| 3457 | 799 | by (exhaust_tac "xs" 1); | 
| 5316 | 800 | by Auto_tac; | 
| 5183 | 801 | by (exhaust_tac "na" 1); | 
| 5316 | 802 | by Auto_tac; | 
| 2608 | 803 | qed_spec_mp "take_take"; | 
| 804 | ||
| 4935 | 805 | Goal "!xs. drop n (drop m xs) = drop (n + m) xs"; | 
| 5183 | 806 | by (induct_tac "m" 1); | 
| 5316 | 807 | by Auto_tac; | 
| 3457 | 808 | by (exhaust_tac "xs" 1); | 
| 5316 | 809 | by Auto_tac; | 
| 2608 | 810 | qed_spec_mp "drop_drop"; | 
| 923 | 811 | |
| 4935 | 812 | Goal "!xs n. take n (drop m xs) = drop m (take (n + m) xs)"; | 
| 5183 | 813 | by (induct_tac "m" 1); | 
| 5316 | 814 | by Auto_tac; | 
| 3457 | 815 | by (exhaust_tac "xs" 1); | 
| 5316 | 816 | by Auto_tac; | 
| 2608 | 817 | qed_spec_mp "take_drop"; | 
| 818 | ||
| 6813 
bf90f86502b2
many new lemmas about take & drop, incl the famous take-lemma
 paulson parents: 
6794diff
changeset | 819 | Goal "!xs. take n xs @ drop n xs = xs"; | 
| 
bf90f86502b2
many new lemmas about take & drop, incl the famous take-lemma
 paulson parents: 
6794diff
changeset | 820 | by (induct_tac "n" 1); | 
| 
bf90f86502b2
many new lemmas about take & drop, incl the famous take-lemma
 paulson parents: 
6794diff
changeset | 821 | by Auto_tac; | 
| 
bf90f86502b2
many new lemmas about take & drop, incl the famous take-lemma
 paulson parents: 
6794diff
changeset | 822 | by (exhaust_tac "xs" 1); | 
| 
bf90f86502b2
many new lemmas about take & drop, incl the famous take-lemma
 paulson parents: 
6794diff
changeset | 823 | by Auto_tac; | 
| 
bf90f86502b2
many new lemmas about take & drop, incl the famous take-lemma
 paulson parents: 
6794diff
changeset | 824 | qed_spec_mp "append_take_drop_id"; | 
| 
bf90f86502b2
many new lemmas about take & drop, incl the famous take-lemma
 paulson parents: 
6794diff
changeset | 825 | |
| 4935 | 826 | Goal "!xs. take n (map f xs) = map f (take n xs)"; | 
| 5183 | 827 | by (induct_tac "n" 1); | 
| 5316 | 828 | by Auto_tac; | 
| 3457 | 829 | by (exhaust_tac "xs" 1); | 
| 5316 | 830 | by Auto_tac; | 
| 2608 | 831 | qed_spec_mp "take_map"; | 
| 832 | ||
| 4935 | 833 | Goal "!xs. drop n (map f xs) = map f (drop n xs)"; | 
| 5183 | 834 | by (induct_tac "n" 1); | 
| 5316 | 835 | by Auto_tac; | 
| 3457 | 836 | by (exhaust_tac "xs" 1); | 
| 5316 | 837 | by Auto_tac; | 
| 2608 | 838 | qed_spec_mp "drop_map"; | 
| 839 | ||
| 4935 | 840 | Goal "!n i. i < n --> (take n xs)!i = xs!i"; | 
| 3457 | 841 | by (induct_tac "xs" 1); | 
| 5316 | 842 | by Auto_tac; | 
| 3457 | 843 | by (exhaust_tac "n" 1); | 
| 844 | by (Blast_tac 1); | |
| 845 | by (exhaust_tac "i" 1); | |
| 5316 | 846 | by Auto_tac; | 
| 2608 | 847 | qed_spec_mp "nth_take"; | 
| 848 | Addsimps [nth_take]; | |
| 923 | 849 | |
| 4935 | 850 | Goal "!xs i. n + i <= length xs --> (drop n xs)!i = xs!(n+i)"; | 
| 5183 | 851 | by (induct_tac "n" 1); | 
| 5316 | 852 | by Auto_tac; | 
| 3457 | 853 | by (exhaust_tac "xs" 1); | 
| 5316 | 854 | by Auto_tac; | 
| 2608 | 855 | qed_spec_mp "nth_drop"; | 
| 856 | Addsimps [nth_drop]; | |
| 857 | ||
| 858 | (** takeWhile & dropWhile **) | |
| 859 | ||
| 3467 | 860 | section "takeWhile & dropWhile"; | 
| 861 | ||
| 4935 | 862 | Goal "takeWhile P xs @ dropWhile P xs = xs"; | 
| 3586 | 863 | by (induct_tac "xs" 1); | 
| 5316 | 864 | by Auto_tac; | 
| 3586 | 865 | qed "takeWhile_dropWhile_id"; | 
| 866 | Addsimps [takeWhile_dropWhile_id]; | |
| 867 | ||
| 4935 | 868 | Goal "x:set xs & ~P(x) --> takeWhile P (xs @ ys) = takeWhile P xs"; | 
| 3457 | 869 | by (induct_tac "xs" 1); | 
| 5316 | 870 | by Auto_tac; | 
| 2608 | 871 | bind_thm("takeWhile_append1", conjI RS (result() RS mp));
 | 
| 872 | Addsimps [takeWhile_append1]; | |
| 923 | 873 | |
| 4935 | 874 | Goal "(!x:set xs. P(x)) --> takeWhile P (xs @ ys) = xs @ takeWhile P ys"; | 
| 3457 | 875 | by (induct_tac "xs" 1); | 
| 5316 | 876 | by Auto_tac; | 
| 2608 | 877 | bind_thm("takeWhile_append2", ballI RS (result() RS mp));
 | 
| 878 | Addsimps [takeWhile_append2]; | |
| 1169 | 879 | |
| 4935 | 880 | Goal "x:set xs & ~P(x) --> dropWhile P (xs @ ys) = (dropWhile P xs)@ys"; | 
| 3457 | 881 | by (induct_tac "xs" 1); | 
| 5316 | 882 | by Auto_tac; | 
| 2608 | 883 | bind_thm("dropWhile_append1", conjI RS (result() RS mp));
 | 
| 884 | Addsimps [dropWhile_append1]; | |
| 885 | ||
| 4935 | 886 | Goal "(!x:set xs. P(x)) --> dropWhile P (xs @ ys) = dropWhile P ys"; | 
| 3457 | 887 | by (induct_tac "xs" 1); | 
| 5316 | 888 | by Auto_tac; | 
| 2608 | 889 | bind_thm("dropWhile_append2", ballI RS (result() RS mp));
 | 
| 890 | Addsimps [dropWhile_append2]; | |
| 891 | ||
| 4935 | 892 | Goal "x:set(takeWhile P xs) --> x:set xs & P x"; | 
| 3457 | 893 | by (induct_tac "xs" 1); | 
| 5316 | 894 | by Auto_tac; | 
| 3647 
a64c8fbcd98f
Renamed theorems of the form set_of_list_XXX to set_XXX
 paulson parents: 
3589diff
changeset | 895 | qed_spec_mp"set_take_whileD"; | 
| 2608 | 896 | |
| 6306 | 897 | (** zip **) | 
| 898 | section "zip"; | |
| 899 | ||
| 900 | Goal "zip [] ys = []"; | |
| 6813 
bf90f86502b2
many new lemmas about take & drop, incl the famous take-lemma
 paulson parents: 
6794diff
changeset | 901 | by (induct_tac "ys" 1); | 
| 6306 | 902 | by Auto_tac; | 
| 903 | qed "zip_Nil"; | |
| 904 | Addsimps [zip_Nil]; | |
| 905 | ||
| 906 | Goal "zip (x#xs) (y#ys) = (x,y)#zip xs ys"; | |
| 6813 
bf90f86502b2
many new lemmas about take & drop, incl the famous take-lemma
 paulson parents: 
6794diff
changeset | 907 | by (Simp_tac 1); | 
| 6306 | 908 | qed "zip_Cons_Cons"; | 
| 909 | Addsimps [zip_Cons_Cons]; | |
| 910 | ||
| 911 | Delsimps(tl (thms"zip.simps")); | |
| 4605 | 912 | |
| 5272 | 913 | |
| 914 | (** foldl **) | |
| 915 | section "foldl"; | |
| 916 | ||
| 917 | Goal "!a. foldl f a (xs @ ys) = foldl f (foldl f a xs) ys"; | |
| 5318 | 918 | by (induct_tac "xs" 1); | 
| 5316 | 919 | by Auto_tac; | 
| 5272 | 920 | qed_spec_mp "foldl_append"; | 
| 921 | Addsimps [foldl_append]; | |
| 922 | ||
| 923 | (* Note: `n <= foldl op+ n ns' looks simpler, but is more difficult to use | |
| 924 | because it requires an additional transitivity step | |
| 925 | *) | |
| 926 | Goal "!n::nat. m <= n --> m <= foldl op+ n ns"; | |
| 5318 | 927 | by (induct_tac "ns" 1); | 
| 6058 | 928 | by Auto_tac; | 
| 5272 | 929 | qed_spec_mp "start_le_sum"; | 
| 930 | ||
| 931 | Goal "n : set ns ==> n <= foldl op+ 0 ns"; | |
| 5758 
27a2b36efd95
corrected auto_tac (applications of unsafe wrappers)
 oheimb parents: 
5644diff
changeset | 932 | by (force_tac (claset() addIs [start_le_sum], | 
| 
27a2b36efd95
corrected auto_tac (applications of unsafe wrappers)
 oheimb parents: 
5644diff
changeset | 933 | simpset() addsimps [in_set_conv_decomp]) 1); | 
| 5272 | 934 | qed "elem_le_sum"; | 
| 935 | ||
| 936 | Goal "!m. (foldl op+ m ns = 0) = (m=0 & (!n : set ns. n=0))"; | |
| 5318 | 937 | by (induct_tac "ns" 1); | 
| 5316 | 938 | by Auto_tac; | 
| 5272 | 939 | qed_spec_mp "sum_eq_0_conv"; | 
| 940 | AddIffs [sum_eq_0_conv]; | |
| 941 | ||
| 5425 | 942 | (** upto **) | 
| 943 | ||
| 5427 | 944 | (* Does not terminate! *) | 
| 945 | Goal "[i..j(] = (if i<j then i#[Suc i..j(] else [])"; | |
| 6162 | 946 | by (induct_tac "j" 1); | 
| 5427 | 947 | by Auto_tac; | 
| 948 | qed "upt_rec"; | |
| 5425 | 949 | |
| 5427 | 950 | Goal "j<=i ==> [i..j(] = []"; | 
| 6162 | 951 | by (stac upt_rec 1); | 
| 952 | by (Asm_simp_tac 1); | |
| 5427 | 953 | qed "upt_conv_Nil"; | 
| 954 | Addsimps [upt_conv_Nil]; | |
| 955 | ||
| 956 | Goal "i<=j ==> [i..(Suc j)(] = [i..j(]@[j]"; | |
| 957 | by (Asm_simp_tac 1); | |
| 958 | qed "upt_Suc"; | |
| 959 | ||
| 960 | Goal "i<j ==> [i..j(] = i#[Suc i..j(]"; | |
| 6162 | 961 | by (rtac trans 1); | 
| 962 | by (stac upt_rec 1); | |
| 963 | by (rtac refl 2); | |
| 5427 | 964 | by (Asm_simp_tac 1); | 
| 965 | qed "upt_conv_Cons"; | |
| 966 | ||
| 967 | Goal "length [i..j(] = j-i"; | |
| 6162 | 968 | by (induct_tac "j" 1); | 
| 5427 | 969 | by (Simp_tac 1); | 
| 6162 | 970 | by (asm_simp_tac (simpset() addsimps [Suc_diff_le]) 1); | 
| 5427 | 971 | qed "length_upt"; | 
| 972 | Addsimps [length_upt]; | |
| 5425 | 973 | |
| 5427 | 974 | Goal "i+k < j --> [i..j(] ! k = i+k"; | 
| 6162 | 975 | by (induct_tac "j" 1); | 
| 976 | by (Simp_tac 1); | |
| 977 | by (asm_simp_tac (simpset() addsimps [nth_append,less_diff_conv]@add_ac) 1); | |
| 978 | by (Clarify_tac 1); | |
| 979 | by (subgoal_tac "n=i+k" 1); | |
| 980 | by (Asm_simp_tac 2); | |
| 981 | by (Asm_simp_tac 1); | |
| 5427 | 982 | qed_spec_mp "nth_upt"; | 
| 983 | Addsimps [nth_upt]; | |
| 5425 | 984 | |
| 6433 | 985 | Goal "!i. i+m <= n --> take m [i..n(] = [i..i+m(]"; | 
| 6813 
bf90f86502b2
many new lemmas about take & drop, incl the famous take-lemma
 paulson parents: 
6794diff
changeset | 986 | by (induct_tac "m" 1); | 
| 
bf90f86502b2
many new lemmas about take & drop, incl the famous take-lemma
 paulson parents: 
6794diff
changeset | 987 | by (Simp_tac 1); | 
| 
bf90f86502b2
many new lemmas about take & drop, incl the famous take-lemma
 paulson parents: 
6794diff
changeset | 988 | by (Clarify_tac 1); | 
| 
bf90f86502b2
many new lemmas about take & drop, incl the famous take-lemma
 paulson parents: 
6794diff
changeset | 989 | by (stac upt_rec 1); | 
| 
bf90f86502b2
many new lemmas about take & drop, incl the famous take-lemma
 paulson parents: 
6794diff
changeset | 990 | by (rtac sym 1); | 
| 
bf90f86502b2
many new lemmas about take & drop, incl the famous take-lemma
 paulson parents: 
6794diff
changeset | 991 | by (stac upt_rec 1); | 
| 
bf90f86502b2
many new lemmas about take & drop, incl the famous take-lemma
 paulson parents: 
6794diff
changeset | 992 | by (asm_simp_tac (simpset() delsimps (thms"upt.simps")) 1); | 
| 6433 | 993 | qed_spec_mp "take_upt"; | 
| 994 | Addsimps [take_upt]; | |
| 995 | ||
| 996 | Goal "!m i. i < n-m --> (map f [m..n(]) ! i = f(m+i)"; | |
| 6813 
bf90f86502b2
many new lemmas about take & drop, incl the famous take-lemma
 paulson parents: 
6794diff
changeset | 997 | by (induct_tac "n" 1); | 
| 
bf90f86502b2
many new lemmas about take & drop, incl the famous take-lemma
 paulson parents: 
6794diff
changeset | 998 | by (Simp_tac 1); | 
| 
bf90f86502b2
many new lemmas about take & drop, incl the famous take-lemma
 paulson parents: 
6794diff
changeset | 999 | by (Clarify_tac 1); | 
| 
bf90f86502b2
many new lemmas about take & drop, incl the famous take-lemma
 paulson parents: 
6794diff
changeset | 1000 | by (subgoal_tac "m < Suc n" 1); | 
| 
bf90f86502b2
many new lemmas about take & drop, incl the famous take-lemma
 paulson parents: 
6794diff
changeset | 1001 | by (arith_tac 2); | 
| 
bf90f86502b2
many new lemmas about take & drop, incl the famous take-lemma
 paulson parents: 
6794diff
changeset | 1002 | by (stac upt_rec 1); | 
| 
bf90f86502b2
many new lemmas about take & drop, incl the famous take-lemma
 paulson parents: 
6794diff
changeset | 1003 | by (asm_simp_tac (simpset() delsplits [split_if]) 1); | 
| 
bf90f86502b2
many new lemmas about take & drop, incl the famous take-lemma
 paulson parents: 
6794diff
changeset | 1004 | by (split_tac [split_if] 1); | 
| 
bf90f86502b2
many new lemmas about take & drop, incl the famous take-lemma
 paulson parents: 
6794diff
changeset | 1005 | by (rtac conjI 1); | 
| 
bf90f86502b2
many new lemmas about take & drop, incl the famous take-lemma
 paulson parents: 
6794diff
changeset | 1006 | by (simp_tac (simpset() addsimps [nth_Cons] addsplits [nat.split]) 1); | 
| 
bf90f86502b2
many new lemmas about take & drop, incl the famous take-lemma
 paulson parents: 
6794diff
changeset | 1007 | by (simp_tac (simpset() addsimps [nth_append] addsplits [nat.split]) 1); | 
| 
bf90f86502b2
many new lemmas about take & drop, incl the famous take-lemma
 paulson parents: 
6794diff
changeset | 1008 | by (Clarify_tac 1); | 
| 
bf90f86502b2
many new lemmas about take & drop, incl the famous take-lemma
 paulson parents: 
6794diff
changeset | 1009 | by (rtac conjI 1); | 
| 
bf90f86502b2
many new lemmas about take & drop, incl the famous take-lemma
 paulson parents: 
6794diff
changeset | 1010 | by (Clarify_tac 1); | 
| 
bf90f86502b2
many new lemmas about take & drop, incl the famous take-lemma
 paulson parents: 
6794diff
changeset | 1011 | by (subgoal_tac "Suc(m+nat) < n" 1); | 
| 
bf90f86502b2
many new lemmas about take & drop, incl the famous take-lemma
 paulson parents: 
6794diff
changeset | 1012 | by (arith_tac 2); | 
| 
bf90f86502b2
many new lemmas about take & drop, incl the famous take-lemma
 paulson parents: 
6794diff
changeset | 1013 | by (Asm_simp_tac 1); | 
| 
bf90f86502b2
many new lemmas about take & drop, incl the famous take-lemma
 paulson parents: 
6794diff
changeset | 1014 | by (Clarify_tac 1); | 
| 
bf90f86502b2
many new lemmas about take & drop, incl the famous take-lemma
 paulson parents: 
6794diff
changeset | 1015 | by (subgoal_tac "n = Suc(m+nat)" 1); | 
| 
bf90f86502b2
many new lemmas about take & drop, incl the famous take-lemma
 paulson parents: 
6794diff
changeset | 1016 | by (arith_tac 2); | 
| 
bf90f86502b2
many new lemmas about take & drop, incl the famous take-lemma
 paulson parents: 
6794diff
changeset | 1017 | by (Asm_simp_tac 1); | 
| 
bf90f86502b2
many new lemmas about take & drop, incl the famous take-lemma
 paulson parents: 
6794diff
changeset | 1018 | by (simp_tac (simpset() addsimps [nth_Cons] addsplits [nat.split]) 1); | 
| 
bf90f86502b2
many new lemmas about take & drop, incl the famous take-lemma
 paulson parents: 
6794diff
changeset | 1019 | by (arith_tac 1); | 
| 6433 | 1020 | qed_spec_mp "nth_map_upt"; | 
| 1021 | ||
| 6813 
bf90f86502b2
many new lemmas about take & drop, incl the famous take-lemma
 paulson parents: 
6794diff
changeset | 1022 | Goal "ALL xs ys. k <= length xs --> k <= length ys --> \ | 
| 
bf90f86502b2
many new lemmas about take & drop, incl the famous take-lemma
 paulson parents: 
6794diff
changeset | 1023 | \ (ALL i. i < k --> xs!i = ys!i) \ | 
| 
bf90f86502b2
many new lemmas about take & drop, incl the famous take-lemma
 paulson parents: 
6794diff
changeset | 1024 | \ --> take k xs = take k ys"; | 
| 
bf90f86502b2
many new lemmas about take & drop, incl the famous take-lemma
 paulson parents: 
6794diff
changeset | 1025 | by (induct_tac "k" 1); | 
| 
bf90f86502b2
many new lemmas about take & drop, incl the famous take-lemma
 paulson parents: 
6794diff
changeset | 1026 | by (ALLGOALS (asm_simp_tac (simpset() addsimps [less_Suc_eq_0_disj, | 
| 
bf90f86502b2
many new lemmas about take & drop, incl the famous take-lemma
 paulson parents: 
6794diff
changeset | 1027 | all_conj_distrib]))); | 
| 
bf90f86502b2
many new lemmas about take & drop, incl the famous take-lemma
 paulson parents: 
6794diff
changeset | 1028 | by (Clarify_tac 1); | 
| 
bf90f86502b2
many new lemmas about take & drop, incl the famous take-lemma
 paulson parents: 
6794diff
changeset | 1029 | (*Both lists must be non-empty*) | 
| 
bf90f86502b2
many new lemmas about take & drop, incl the famous take-lemma
 paulson parents: 
6794diff
changeset | 1030 | by (exhaust_tac "xs" 1); | 
| 
bf90f86502b2
many new lemmas about take & drop, incl the famous take-lemma
 paulson parents: 
6794diff
changeset | 1031 | by (exhaust_tac "ys" 2); | 
| 
bf90f86502b2
many new lemmas about take & drop, incl the famous take-lemma
 paulson parents: 
6794diff
changeset | 1032 | by (ALLGOALS Clarify_tac); | 
| 
bf90f86502b2
many new lemmas about take & drop, incl the famous take-lemma
 paulson parents: 
6794diff
changeset | 1033 | (*prenexing's needed, not miniscoping*) | 
| 
bf90f86502b2
many new lemmas about take & drop, incl the famous take-lemma
 paulson parents: 
6794diff
changeset | 1034 | by (ALLGOALS (full_simp_tac (simpset() addsimps (all_simps RL [sym]) | 
| 
bf90f86502b2
many new lemmas about take & drop, incl the famous take-lemma
 paulson parents: 
6794diff
changeset | 1035 | delsimps (all_simps)))); | 
| 
bf90f86502b2
many new lemmas about take & drop, incl the famous take-lemma
 paulson parents: 
6794diff
changeset | 1036 | by (Blast_tac 1); | 
| 
bf90f86502b2
many new lemmas about take & drop, incl the famous take-lemma
 paulson parents: 
6794diff
changeset | 1037 | qed_spec_mp "nth_take_lemma"; | 
| 
bf90f86502b2
many new lemmas about take & drop, incl the famous take-lemma
 paulson parents: 
6794diff
changeset | 1038 | |
| 
bf90f86502b2
many new lemmas about take & drop, incl the famous take-lemma
 paulson parents: 
6794diff
changeset | 1039 | Goal "[| length xs = length ys; \ | 
| 
bf90f86502b2
many new lemmas about take & drop, incl the famous take-lemma
 paulson parents: 
6794diff
changeset | 1040 | \ ALL i. i < length xs --> xs!i = ys!i |] \ | 
| 
bf90f86502b2
many new lemmas about take & drop, incl the famous take-lemma
 paulson parents: 
6794diff
changeset | 1041 | \ ==> xs = ys"; | 
| 
bf90f86502b2
many new lemmas about take & drop, incl the famous take-lemma
 paulson parents: 
6794diff
changeset | 1042 | by (forward_tac [[le_refl, eq_imp_le] MRS nth_take_lemma] 1); | 
| 
bf90f86502b2
many new lemmas about take & drop, incl the famous take-lemma
 paulson parents: 
6794diff
changeset | 1043 | by (ALLGOALS (asm_full_simp_tac (simpset() addsimps [take_all]))); | 
| 
bf90f86502b2
many new lemmas about take & drop, incl the famous take-lemma
 paulson parents: 
6794diff
changeset | 1044 | qed_spec_mp "nth_equalityI"; | 
| 
bf90f86502b2
many new lemmas about take & drop, incl the famous take-lemma
 paulson parents: 
6794diff
changeset | 1045 | |
| 
bf90f86502b2
many new lemmas about take & drop, incl the famous take-lemma
 paulson parents: 
6794diff
changeset | 1046 | (*The famous take-lemma*) | 
| 
bf90f86502b2
many new lemmas about take & drop, incl the famous take-lemma
 paulson parents: 
6794diff
changeset | 1047 | Goal "(ALL i. take i xs = take i ys) ==> xs = ys"; | 
| 
bf90f86502b2
many new lemmas about take & drop, incl the famous take-lemma
 paulson parents: 
6794diff
changeset | 1048 | by (dres_inst_tac [("x", "max (length xs) (length ys)")] spec 1);
 | 
| 
bf90f86502b2
many new lemmas about take & drop, incl the famous take-lemma
 paulson parents: 
6794diff
changeset | 1049 | by (full_simp_tac (simpset() addsimps [le_max_iff_disj, take_all]) 1); | 
| 
bf90f86502b2
many new lemmas about take & drop, incl the famous take-lemma
 paulson parents: 
6794diff
changeset | 1050 | qed_spec_mp "take_equalityI"; | 
| 
bf90f86502b2
many new lemmas about take & drop, incl the famous take-lemma
 paulson parents: 
6794diff
changeset | 1051 | |
| 5272 | 1052 | |
| 4605 | 1053 | (** nodups & remdups **) | 
| 1054 | section "nodups & remdups"; | |
| 1055 | ||
| 4935 | 1056 | Goal "set(remdups xs) = set xs"; | 
| 4605 | 1057 | by (induct_tac "xs" 1); | 
| 1058 | by (Simp_tac 1); | |
| 4686 | 1059 | by (asm_full_simp_tac (simpset() addsimps [insert_absorb]) 1); | 
| 4605 | 1060 | qed "set_remdups"; | 
| 1061 | Addsimps [set_remdups]; | |
| 1062 | ||
| 4935 | 1063 | Goal "nodups(remdups xs)"; | 
| 4605 | 1064 | by (induct_tac "xs" 1); | 
| 5316 | 1065 | by Auto_tac; | 
| 4605 | 1066 | qed "nodups_remdups"; | 
| 1067 | ||
| 4935 | 1068 | Goal "nodups xs --> nodups (filter P xs)"; | 
| 4605 | 1069 | by (induct_tac "xs" 1); | 
| 5316 | 1070 | by Auto_tac; | 
| 4605 | 1071 | qed_spec_mp "nodups_filter"; | 
| 1072 | ||
| 3589 
244daa75f890
Added function `replicate' and lemmas map_cong and set_replicate.
 nipkow parents: 
3586diff
changeset | 1073 | (** replicate **) | 
| 
244daa75f890
Added function `replicate' and lemmas map_cong and set_replicate.
 nipkow parents: 
3586diff
changeset | 1074 | section "replicate"; | 
| 
244daa75f890
Added function `replicate' and lemmas map_cong and set_replicate.
 nipkow parents: 
3586diff
changeset | 1075 | |
| 6794 | 1076 | Goal "length(replicate n x) = n"; | 
| 6813 
bf90f86502b2
many new lemmas about take & drop, incl the famous take-lemma
 paulson parents: 
6794diff
changeset | 1077 | by (induct_tac "n" 1); | 
| 
bf90f86502b2
many new lemmas about take & drop, incl the famous take-lemma
 paulson parents: 
6794diff
changeset | 1078 | by Auto_tac; | 
| 6794 | 1079 | qed "length_replicate"; | 
| 1080 | Addsimps [length_replicate]; | |
| 1081 | ||
| 1082 | Goal "map f (replicate n x) = replicate n (f x)"; | |
| 1083 | by (induct_tac "n" 1); | |
| 6813 
bf90f86502b2
many new lemmas about take & drop, incl the famous take-lemma
 paulson parents: 
6794diff
changeset | 1084 | by Auto_tac; | 
| 6794 | 1085 | qed "map_replicate"; | 
| 1086 | Addsimps [map_replicate]; | |
| 1087 | ||
| 1088 | Goal "(replicate n x) @ (x#xs) = x # replicate n x @ xs"; | |
| 1089 | by (induct_tac "n" 1); | |
| 6813 
bf90f86502b2
many new lemmas about take & drop, incl the famous take-lemma
 paulson parents: 
6794diff
changeset | 1090 | by Auto_tac; | 
| 6794 | 1091 | qed "replicate_app_Cons_same"; | 
| 1092 | ||
| 1093 | Goal "rev(replicate n x) = replicate n x"; | |
| 1094 | by (induct_tac "n" 1); | |
| 6813 
bf90f86502b2
many new lemmas about take & drop, incl the famous take-lemma
 paulson parents: 
6794diff
changeset | 1095 | by (Simp_tac 1); | 
| 6794 | 1096 | by (asm_simp_tac (simpset() addsimps [replicate_app_Cons_same]) 1); | 
| 1097 | qed "rev_replicate"; | |
| 1098 | Addsimps [rev_replicate]; | |
| 1099 | ||
| 1100 | Goal"n ~= 0 --> hd(replicate n x) = x"; | |
| 1101 | by (induct_tac "n" 1); | |
| 6813 
bf90f86502b2
many new lemmas about take & drop, incl the famous take-lemma
 paulson parents: 
6794diff
changeset | 1102 | by Auto_tac; | 
| 6794 | 1103 | qed_spec_mp "hd_replicate"; | 
| 1104 | Addsimps [hd_replicate]; | |
| 1105 | ||
| 1106 | Goal "n ~= 0 --> tl(replicate n x) = replicate (n-1) x"; | |
| 1107 | by (induct_tac "n" 1); | |
| 6813 
bf90f86502b2
many new lemmas about take & drop, incl the famous take-lemma
 paulson parents: 
6794diff
changeset | 1108 | by Auto_tac; | 
| 6794 | 1109 | qed_spec_mp "tl_replicate"; | 
| 1110 | Addsimps [tl_replicate]; | |
| 1111 | ||
| 1112 | Goal "n ~= 0 --> last(replicate n x) = x"; | |
| 1113 | by (induct_tac "n" 1); | |
| 6813 
bf90f86502b2
many new lemmas about take & drop, incl the famous take-lemma
 paulson parents: 
6794diff
changeset | 1114 | by Auto_tac; | 
| 6794 | 1115 | qed_spec_mp "last_replicate"; | 
| 1116 | Addsimps [last_replicate]; | |
| 1117 | ||
| 1118 | Goal "!i. i<n --> (replicate n x)!i = x"; | |
| 6813 
bf90f86502b2
many new lemmas about take & drop, incl the famous take-lemma
 paulson parents: 
6794diff
changeset | 1119 | by (induct_tac "n" 1); | 
| 
bf90f86502b2
many new lemmas about take & drop, incl the famous take-lemma
 paulson parents: 
6794diff
changeset | 1120 | by (Simp_tac 1); | 
| 
bf90f86502b2
many new lemmas about take & drop, incl the famous take-lemma
 paulson parents: 
6794diff
changeset | 1121 | by (asm_simp_tac (simpset() addsimps [nth_Cons] addsplits [nat.split]) 1); | 
| 6794 | 1122 | qed_spec_mp "nth_replicate"; | 
| 1123 | Addsimps [nth_replicate]; | |
| 1124 | ||
| 4935 | 1125 | Goal "set(replicate (Suc n) x) = {x}";
 | 
| 4423 | 1126 | by (induct_tac "n" 1); | 
| 5316 | 1127 | by Auto_tac; | 
| 3589 
244daa75f890
Added function `replicate' and lemmas map_cong and set_replicate.
 nipkow parents: 
3586diff
changeset | 1128 | val lemma = result(); | 
| 
244daa75f890
Added function `replicate' and lemmas map_cong and set_replicate.
 nipkow parents: 
3586diff
changeset | 1129 | |
| 5043 | 1130 | Goal "n ~= 0 ==> set(replicate n x) = {x}";
 | 
| 4423 | 1131 | by (fast_tac (claset() addSDs [not0_implies_Suc] addSIs [lemma]) 1); | 
| 3589 
244daa75f890
Added function `replicate' and lemmas map_cong and set_replicate.
 nipkow parents: 
3586diff
changeset | 1132 | qed "set_replicate"; | 
| 
244daa75f890
Added function `replicate' and lemmas map_cong and set_replicate.
 nipkow parents: 
3586diff
changeset | 1133 | Addsimps [set_replicate]; | 
| 5162 | 1134 | |
| 6794 | 1135 | Goal "replicate (n+m) x = replicate n x @ replicate m x"; | 
| 1136 | by (induct_tac "n" 1); | |
| 1137 | by Auto_tac; | |
| 1138 | qed "replicate_add"; | |
| 5162 | 1139 | |
| 5281 | 1140 | (*** Lexcicographic orderings on lists ***) | 
| 1141 | section"Lexcicographic orderings on lists"; | |
| 1142 | ||
| 1143 | Goal "wf r ==> wf(lexn r n)"; | |
| 5318 | 1144 | by (induct_tac "n" 1); | 
| 1145 | by (Simp_tac 1); | |
| 1146 | by (Simp_tac 1); | |
| 1147 | by (rtac wf_subset 1); | |
| 1148 | by (rtac Int_lower1 2); | |
| 1149 | by (rtac wf_prod_fun_image 1); | |
| 1150 | by (rtac injI 2); | |
| 6813 
bf90f86502b2
many new lemmas about take & drop, incl the famous take-lemma
 paulson parents: 
6794diff
changeset | 1151 | by Auto_tac; | 
| 5281 | 1152 | qed "wf_lexn"; | 
| 1153 | ||
| 1154 | Goal "!xs ys. (xs,ys) : lexn r n --> length xs = n & length ys = n"; | |
| 5318 | 1155 | by (induct_tac "n" 1); | 
| 6813 
bf90f86502b2
many new lemmas about take & drop, incl the famous take-lemma
 paulson parents: 
6794diff
changeset | 1156 | by Auto_tac; | 
| 5281 | 1157 | qed_spec_mp "lexn_length"; | 
| 1158 | ||
| 1159 | Goalw [lex_def] "wf r ==> wf(lex r)"; | |
| 5318 | 1160 | by (rtac wf_UN 1); | 
| 1161 | by (blast_tac (claset() addIs [wf_lexn]) 1); | |
| 1162 | by (Clarify_tac 1); | |
| 1163 | by (rename_tac "m n" 1); | |
| 1164 | by (subgoal_tac "m ~= n" 1); | |
| 1165 | by (Blast_tac 2); | |
| 1166 | by (blast_tac (claset() addDs [lexn_length,not_sym]) 1); | |
| 5281 | 1167 | qed "wf_lex"; | 
| 1168 | AddSIs [wf_lex]; | |
| 1169 | ||
| 1170 | Goal | |
| 1171 | "lexn r n = \ | |
| 1172 | \ {(xs,ys). length xs = n & length ys = n & \
 | |
| 1173 | \ (? xys x y xs' ys'. xs= xys @ x#xs' & ys= xys @ y#ys' & (x,y):r)}"; | |
| 5318 | 1174 | by (induct_tac "n" 1); | 
| 1175 | by (Simp_tac 1); | |
| 1176 | by (Blast_tac 1); | |
| 5641 | 1177 | by (asm_full_simp_tac (simpset() | 
| 5296 | 1178 | addsimps [lex_prod_def]) 1); | 
| 5641 | 1179 | by (auto_tac (claset(), simpset())); | 
| 5318 | 1180 | by (Blast_tac 1); | 
| 1181 | by (rename_tac "a xys x xs' y ys'" 1); | |
| 1182 |  by (res_inst_tac [("x","a#xys")] exI 1);
 | |
| 1183 | by (Simp_tac 1); | |
| 1184 | by (exhaust_tac "xys" 1); | |
| 5641 | 1185 | by (ALLGOALS (asm_full_simp_tac (simpset()))); | 
| 5318 | 1186 | by (Blast_tac 1); | 
| 5281 | 1187 | qed "lexn_conv"; | 
| 1188 | ||
| 1189 | Goalw [lex_def] | |
| 1190 | "lex r = \ | |
| 1191 | \ {(xs,ys). length xs = length ys & \
 | |
| 1192 | \ (? xys x y xs' ys'. xs= xys @ x#xs' & ys= xys @ y#ys' & (x,y):r)}"; | |
| 5641 | 1193 | by (force_tac (claset(), simpset() addsimps [lexn_conv]) 1); | 
| 5281 | 1194 | qed "lex_conv"; | 
| 1195 | ||
| 1196 | Goalw [lexico_def] "wf r ==> wf(lexico r)"; | |
| 5318 | 1197 | by (Blast_tac 1); | 
| 5281 | 1198 | qed "wf_lexico"; | 
| 1199 | AddSIs [wf_lexico]; | |
| 1200 | ||
| 1201 | Goalw | |
| 1202 | [lexico_def,diag_def,lex_prod_def,measure_def,inv_image_def] | |
| 1203 | "lexico r = {(xs,ys). length xs < length ys | \
 | |
| 1204 | \ length xs = length ys & (xs,ys) : lex r}"; | |
| 5318 | 1205 | by (Simp_tac 1); | 
| 5281 | 1206 | qed "lexico_conv"; | 
| 1207 | ||
| 5283 | 1208 | Goal "([],ys) ~: lex r"; | 
| 5318 | 1209 | by (simp_tac (simpset() addsimps [lex_conv]) 1); | 
| 5283 | 1210 | qed "Nil_notin_lex"; | 
| 1211 | ||
| 1212 | Goal "(xs,[]) ~: lex r"; | |
| 5318 | 1213 | by (simp_tac (simpset() addsimps [lex_conv]) 1); | 
| 5283 | 1214 | qed "Nil2_notin_lex"; | 
| 1215 | ||
| 1216 | AddIffs [Nil_notin_lex,Nil2_notin_lex]; | |
| 1217 | ||
| 1218 | Goal "((x#xs,y#ys) : lex r) = \ | |
| 1219 | \ ((x,y) : r & length xs = length ys | x=y & (xs,ys) : lex r)"; | |
| 5318 | 1220 | by (simp_tac (simpset() addsimps [lex_conv]) 1); | 
| 1221 | by (rtac iffI 1); | |
| 1222 | by (blast_tac (claset() addIs [Cons_eq_appendI]) 2); | |
| 1223 | by (REPEAT(eresolve_tac [conjE, exE] 1)); | |
| 1224 | by (exhaust_tac "xys" 1); | |
| 1225 | by (Asm_full_simp_tac 1); | |
| 1226 | by (Asm_full_simp_tac 1); | |
| 1227 | by (Blast_tac 1); | |
| 5283 | 1228 | qed "Cons_in_lex"; | 
| 1229 | AddIffs [Cons_in_lex]; |