| author | wenzelm | 
| Tue, 10 Mar 2015 13:55:10 +0100 | |
| changeset 59666 | 0e9f303d1515 | 
| parent 59016 | be4a911aca71 | 
| child 60011 | 3eef7a43cd51 | 
| permissions | -rw-r--r-- | 
| 58607 
1f90ea1b4010
move Stream theory from Datatype_Examples to Library
 hoelzl parents: 
58309diff
changeset | 1 | (* Title: HOL/Library/Stream.thy | 
| 50518 | 2 | Author: Dmitriy Traytel, TU Muenchen | 
| 3 | Author: Andrei Popescu, TU Muenchen | |
| 51778 | 4 | Copyright 2012, 2013 | 
| 50518 | 5 | |
| 6 | Infinite streams. | |
| 7 | *) | |
| 8 | ||
| 58881 | 9 | section {* Infinite Streams *}
 | 
| 50518 | 10 | |
| 11 | theory Stream | |
| 55076 | 12 | imports "~~/src/HOL/Library/Nat_Bijection" | 
| 50518 | 13 | begin | 
| 14 | ||
| 57206 
d9be905d6283
changed syntax of map: and rel: arguments to BNF-based datatypes
 blanchet parents: 
57175diff
changeset | 15 | codatatype (sset: 'a) stream = | 
| 54720 
0a9920e46b3a
removed obsolete codegen setup; Stream -> SCons; tuned
 traytel parents: 
54498diff
changeset | 16 | SCons (shd: 'a) (stl: "'a stream") (infixr "##" 65) | 
| 57206 
d9be905d6283
changed syntax of map: and rel: arguments to BNF-based datatypes
 blanchet parents: 
57175diff
changeset | 17 | for | 
| 
d9be905d6283
changed syntax of map: and rel: arguments to BNF-based datatypes
 blanchet parents: 
57175diff
changeset | 18 | map: smap | 
| 
d9be905d6283
changed syntax of map: and rel: arguments to BNF-based datatypes
 blanchet parents: 
57175diff
changeset | 19 | rel: stream_all2 | 
| 51409 | 20 | |
| 51462 | 21 | (*for code generation only*) | 
| 22 | definition smember :: "'a \<Rightarrow> 'a stream \<Rightarrow> bool" where | |
| 51772 
d2b265ebc1fa
specify nicer names for map, set and rel in the stream library
 traytel parents: 
51766diff
changeset | 23 | [code_abbrev]: "smember x s \<longleftrightarrow> x \<in> sset s" | 
| 51462 | 24 | |
| 54720 
0a9920e46b3a
removed obsolete codegen setup; Stream -> SCons; tuned
 traytel parents: 
54498diff
changeset | 25 | lemma smember_code[code, simp]: "smember x (y ## s) = (if x = y then True else smember x s)" | 
| 51462 | 26 | unfolding smember_def by auto | 
| 27 | ||
| 28 | hide_const (open) smember | |
| 29 | ||
| 57983 
6edc3529bb4e
reordered some (co)datatype property names for more consistency
 blanchet parents: 
57206diff
changeset | 30 | lemmas smap_simps[simp] = stream.map_sel | 
| 
6edc3529bb4e
reordered some (co)datatype property names for more consistency
 blanchet parents: 
57206diff
changeset | 31 | lemmas shd_sset = stream.set_sel(1) | 
| 
6edc3529bb4e
reordered some (co)datatype property names for more consistency
 blanchet parents: 
57206diff
changeset | 32 | lemmas stl_sset = stream.set_sel(2) | 
| 50518 | 33 | |
| 57986 | 34 | theorem sset_induct[consumes 1, case_names shd stl, induct set: sset]: | 
| 35 | assumes "y \<in> sset s" and "\<And>s. P (shd s) s" and "\<And>s y. \<lbrakk>y \<in> sset (stl s); P y (stl s)\<rbrakk> \<Longrightarrow> P y s" | |
| 50518 | 36 | shows "P y s" | 
| 57986 | 37 | using assms by induct (metis stream.sel(1), auto) | 
| 50518 | 38 | |
| 59000 | 39 | lemma smap_ctr: "smap f s = x ## s' \<longleftrightarrow> f (shd s) = x \<and> smap f (stl s) = s'" | 
| 40 | by (cases s) simp | |
| 50518 | 41 | |
| 42 | subsection {* prepend list to stream *}
 | |
| 43 | ||
| 44 | primrec shift :: "'a list \<Rightarrow> 'a stream \<Rightarrow> 'a stream" (infixr "@-" 65) where | |
| 45 | "shift [] s = s" | |
| 51023 
550f265864e3
infix syntax for streams (reflecting the one for lists)
 traytel parents: 
50518diff
changeset | 46 | | "shift (x # xs) s = x ## shift xs s" | 
| 50518 | 47 | |
| 51772 
d2b265ebc1fa
specify nicer names for map, set and rel in the stream library
 traytel parents: 
51766diff
changeset | 48 | lemma smap_shift[simp]: "smap f (xs @- s) = map f xs @- smap f s" | 
| 51353 | 49 | by (induct xs) auto | 
| 50 | ||
| 50518 | 51 | lemma shift_append[simp]: "(xs @ ys) @- s = xs @- ys @- s" | 
| 51141 | 52 | by (induct xs) auto | 
| 50518 | 53 | |
| 54 | lemma shift_simps[simp]: | |
| 55 | "shd (xs @- s) = (if xs = [] then shd s else hd xs)" | |
| 56 | "stl (xs @- s) = (if xs = [] then stl s else tl xs @- s)" | |
| 51141 | 57 | by (induct xs) auto | 
| 50518 | 58 | |
| 51772 
d2b265ebc1fa
specify nicer names for map, set and rel in the stream library
 traytel parents: 
51766diff
changeset | 59 | lemma sset_shift[simp]: "sset (xs @- s) = set xs \<union> sset s" | 
| 51141 | 60 | by (induct xs) auto | 
| 50518 | 61 | |
| 51352 | 62 | lemma shift_left_inj[simp]: "xs @- s1 = xs @- s2 \<longleftrightarrow> s1 = s2" | 
| 63 | by (induct xs) auto | |
| 64 | ||
| 50518 | 65 | |
| 54469 | 66 | subsection {* set of streams with elements in some fixed set *}
 | 
| 50518 | 67 | |
| 68 | coinductive_set | |
| 54469 | 69 | streams :: "'a set \<Rightarrow> 'a stream set" | 
| 50518 | 70 | for A :: "'a set" | 
| 71 | where | |
| 51023 
550f265864e3
infix syntax for streams (reflecting the one for lists)
 traytel parents: 
50518diff
changeset | 72 | Stream[intro!, simp, no_atp]: "\<lbrakk>a \<in> A; s \<in> streams A\<rbrakk> \<Longrightarrow> a ## s \<in> streams A" | 
| 50518 | 73 | |
| 59000 | 74 | lemma in_streams: "stl s \<in> streams S \<Longrightarrow> shd s \<in> S \<Longrightarrow> s \<in> streams S" | 
| 75 | by (cases s) auto | |
| 76 | ||
| 77 | lemma streamsE: "s \<in> streams A \<Longrightarrow> (shd s \<in> A \<Longrightarrow> stl s \<in> streams A \<Longrightarrow> P) \<Longrightarrow> P" | |
| 78 | by (erule streams.cases) simp_all | |
| 79 | ||
| 80 | lemma Stream_image: "x ## y \<in> (op ## x') ` Y \<longleftrightarrow> x = x' \<and> y \<in> Y" | |
| 81 | by auto | |
| 82 | ||
| 50518 | 83 | lemma shift_streams: "\<lbrakk>w \<in> lists A; s \<in> streams A\<rbrakk> \<Longrightarrow> w @- s \<in> streams A" | 
| 51141 | 84 | by (induct w) auto | 
| 50518 | 85 | |
| 54469 | 86 | lemma streams_Stream: "x ## s \<in> streams A \<longleftrightarrow> x \<in> A \<and> s \<in> streams A" | 
| 87 | by (auto elim: streams.cases) | |
| 88 | ||
| 89 | lemma streams_stl: "s \<in> streams A \<Longrightarrow> stl s \<in> streams A" | |
| 90 | by (cases s) (auto simp: streams_Stream) | |
| 91 | ||
| 92 | lemma streams_shd: "s \<in> streams A \<Longrightarrow> shd s \<in> A" | |
| 93 | by (cases s) (auto simp: streams_Stream) | |
| 94 | ||
| 51772 
d2b265ebc1fa
specify nicer names for map, set and rel in the stream library
 traytel parents: 
51766diff
changeset | 95 | lemma sset_streams: | 
| 
d2b265ebc1fa
specify nicer names for map, set and rel in the stream library
 traytel parents: 
51766diff
changeset | 96 | assumes "sset s \<subseteq> A" | 
| 50518 | 97 | shows "s \<in> streams A" | 
| 54027 
e5853a648b59
use new coinduction method and primcorec in examples
 traytel parents: 
53808diff
changeset | 98 | using assms proof (coinduction arbitrary: s) | 
| 
e5853a648b59
use new coinduction method and primcorec in examples
 traytel parents: 
53808diff
changeset | 99 | case streams then show ?case by (cases s) simp | 
| 50518 | 100 | qed | 
| 101 | ||
| 54469 | 102 | lemma streams_sset: | 
| 103 | assumes "s \<in> streams A" | |
| 104 | shows "sset s \<subseteq> A" | |
| 105 | proof | |
| 106 | fix x assume "x \<in> sset s" from this `s \<in> streams A` show "x \<in> A" | |
| 107 | by (induct s) (auto intro: streams_shd streams_stl) | |
| 108 | qed | |
| 109 | ||
| 110 | lemma streams_iff_sset: "s \<in> streams A \<longleftrightarrow> sset s \<subseteq> A" | |
| 111 | by (metis sset_streams streams_sset) | |
| 112 | ||
| 113 | lemma streams_mono: "s \<in> streams A \<Longrightarrow> A \<subseteq> B \<Longrightarrow> s \<in> streams B" | |
| 114 | unfolding streams_iff_sset by auto | |
| 115 | ||
| 59000 | 116 | lemma streams_mono2: "S \<subseteq> T \<Longrightarrow> streams S \<subseteq> streams T" | 
| 117 | by (auto intro: streams_mono) | |
| 118 | ||
| 54469 | 119 | lemma smap_streams: "s \<in> streams A \<Longrightarrow> (\<And>x. x \<in> A \<Longrightarrow> f x \<in> B) \<Longrightarrow> smap f s \<in> streams B" | 
| 120 | unfolding streams_iff_sset stream.set_map by auto | |
| 121 | ||
| 122 | lemma streams_empty: "streams {} = {}"
 | |
| 123 | by (auto elim: streams.cases) | |
| 124 | ||
| 125 | lemma streams_UNIV[simp]: "streams UNIV = UNIV" | |
| 126 | by (auto simp: streams_iff_sset) | |
| 50518 | 127 | |
| 51141 | 128 | subsection {* nth, take, drop for streams *}
 | 
| 129 | ||
| 130 | primrec snth :: "'a stream \<Rightarrow> nat \<Rightarrow> 'a" (infixl "!!" 100) where | |
| 131 | "s !! 0 = shd s" | |
| 132 | | "s !! Suc n = stl s !! n" | |
| 133 | ||
| 59000 | 134 | lemma snth_Stream: "(x ## s) !! Suc i = s !! i" | 
| 135 | by simp | |
| 136 | ||
| 51772 
d2b265ebc1fa
specify nicer names for map, set and rel in the stream library
 traytel parents: 
51766diff
changeset | 137 | lemma snth_smap[simp]: "smap f s !! n = f (s !! n)" | 
| 51141 | 138 | by (induct n arbitrary: s) auto | 
| 139 | ||
| 140 | lemma shift_snth_less[simp]: "p < length xs \<Longrightarrow> (xs @- s) !! p = xs ! p" | |
| 141 | by (induct p arbitrary: xs) (auto simp: hd_conv_nth nth_tl) | |
| 142 | ||
| 143 | lemma shift_snth_ge[simp]: "p \<ge> length xs \<Longrightarrow> (xs @- s) !! p = s !! (p - length xs)" | |
| 144 | by (induct p arbitrary: xs) (auto simp: Suc_diff_eq_diff_pred) | |
| 145 | ||
| 57175 | 146 | lemma shift_snth: "(xs @- s) !! n = (if n < length xs then xs ! n else s !! (n - length xs))" | 
| 147 | by auto | |
| 148 | ||
| 51772 
d2b265ebc1fa
specify nicer names for map, set and rel in the stream library
 traytel parents: 
51766diff
changeset | 149 | lemma snth_sset[simp]: "s !! n \<in> sset s" | 
| 
d2b265ebc1fa
specify nicer names for map, set and rel in the stream library
 traytel parents: 
51766diff
changeset | 150 | by (induct n arbitrary: s) (auto intro: shd_sset stl_sset) | 
| 51141 | 151 | |
| 51772 
d2b265ebc1fa
specify nicer names for map, set and rel in the stream library
 traytel parents: 
51766diff
changeset | 152 | lemma sset_range: "sset s = range (snth s)" | 
| 51141 | 153 | proof (intro equalityI subsetI) | 
| 51772 
d2b265ebc1fa
specify nicer names for map, set and rel in the stream library
 traytel parents: 
51766diff
changeset | 154 | fix x assume "x \<in> sset s" | 
| 51141 | 155 | thus "x \<in> range (snth s)" | 
| 156 | proof (induct s) | |
| 157 | case (stl s x) | |
| 158 | then obtain n where "x = stl s !! n" by auto | |
| 159 | thus ?case by (auto intro: range_eqI[of _ _ "Suc n"]) | |
| 160 | qed (auto intro: range_eqI[of _ _ 0]) | |
| 161 | qed auto | |
| 50518 | 162 | |
| 59000 | 163 | lemma streams_iff_snth: "s \<in> streams X \<longleftrightarrow> (\<forall>n. s !! n \<in> X)" | 
| 164 | by (force simp: streams_iff_sset sset_range) | |
| 165 | ||
| 166 | lemma snth_in: "s \<in> streams X \<Longrightarrow> s !! n \<in> X" | |
| 167 | by (simp add: streams_iff_snth) | |
| 168 | ||
| 50518 | 169 | primrec stake :: "nat \<Rightarrow> 'a stream \<Rightarrow> 'a list" where | 
| 170 | "stake 0 s = []" | |
| 171 | | "stake (Suc n) s = shd s # stake n (stl s)" | |
| 172 | ||
| 51141 | 173 | lemma length_stake[simp]: "length (stake n s) = n" | 
| 174 | by (induct n arbitrary: s) auto | |
| 175 | ||
| 51772 
d2b265ebc1fa
specify nicer names for map, set and rel in the stream library
 traytel parents: 
51766diff
changeset | 176 | lemma stake_smap[simp]: "stake n (smap f s) = map f (stake n s)" | 
| 51141 | 177 | by (induct n arbitrary: s) auto | 
| 178 | ||
| 57175 | 179 | lemma take_stake: "take n (stake m s) = stake (min n m) s" | 
| 180 | proof (induct m arbitrary: s n) | |
| 181 | case (Suc m) thus ?case by (cases n) auto | |
| 182 | qed simp | |
| 183 | ||
| 50518 | 184 | primrec sdrop :: "nat \<Rightarrow> 'a stream \<Rightarrow> 'a stream" where | 
| 185 | "sdrop 0 s = s" | |
| 186 | | "sdrop (Suc n) s = sdrop n (stl s)" | |
| 187 | ||
| 51141 | 188 | lemma sdrop_simps[simp]: | 
| 189 | "shd (sdrop n s) = s !! n" "stl (sdrop n s) = sdrop (Suc n) s" | |
| 190 | by (induct n arbitrary: s) auto | |
| 191 | ||
| 51772 
d2b265ebc1fa
specify nicer names for map, set and rel in the stream library
 traytel parents: 
51766diff
changeset | 192 | lemma sdrop_smap[simp]: "sdrop n (smap f s) = smap f (sdrop n s)" | 
| 51141 | 193 | by (induct n arbitrary: s) auto | 
| 50518 | 194 | |
| 51352 | 195 | lemma sdrop_stl: "sdrop n (stl s) = stl (sdrop n s)" | 
| 196 | by (induct n) auto | |
| 197 | ||
| 57175 | 198 | lemma drop_stake: "drop n (stake m s) = stake (m - n) (sdrop n s)" | 
| 199 | proof (induct m arbitrary: s n) | |
| 200 | case (Suc m) thus ?case by (cases n) auto | |
| 201 | qed simp | |
| 202 | ||
| 50518 | 203 | lemma stake_sdrop: "stake n s @- sdrop n s = s" | 
| 51141 | 204 | by (induct n arbitrary: s) auto | 
| 205 | ||
| 206 | lemma id_stake_snth_sdrop: | |
| 207 | "s = stake i s @- s !! i ## sdrop (Suc i) s" | |
| 208 | by (subst stake_sdrop[symmetric, of _ i]) (metis sdrop_simps stream.collapse) | |
| 50518 | 209 | |
| 51772 
d2b265ebc1fa
specify nicer names for map, set and rel in the stream library
 traytel parents: 
51766diff
changeset | 210 | lemma smap_alt: "smap f s = s' \<longleftrightarrow> (\<forall>n. f (s !! n) = s' !! n)" (is "?L = ?R") | 
| 51141 | 211 | proof | 
| 212 | assume ?R | |
| 54027 
e5853a648b59
use new coinduction method and primcorec in examples
 traytel parents: 
53808diff
changeset | 213 | then have "\<And>n. smap f (sdrop n s) = sdrop n s'" | 
| 
e5853a648b59
use new coinduction method and primcorec in examples
 traytel parents: 
53808diff
changeset | 214 | by coinduction (auto intro: exI[of _ 0] simp del: sdrop.simps(2)) | 
| 
e5853a648b59
use new coinduction method and primcorec in examples
 traytel parents: 
53808diff
changeset | 215 | then show ?L using sdrop.simps(1) by metis | 
| 51141 | 216 | qed auto | 
| 217 | ||
| 218 | lemma stake_invert_Nil[iff]: "stake n s = [] \<longleftrightarrow> n = 0" | |
| 219 | by (induct n) auto | |
| 50518 | 220 | |
| 57175 | 221 | lemma sdrop_shift: "sdrop i (w @- s) = drop i w @- sdrop (i - length w) s" | 
| 222 | by (induct i arbitrary: w s) (auto simp: drop_tl drop_Suc neq_Nil_conv) | |
| 50518 | 223 | |
| 57175 | 224 | lemma stake_shift: "stake i (w @- s) = take i w @ stake (i - length w) s" | 
| 225 | by (induct i arbitrary: w s) (auto simp: neq_Nil_conv) | |
| 50518 | 226 | |
| 227 | lemma stake_add[simp]: "stake m s @ stake n (sdrop m s) = stake (m + n) s" | |
| 51141 | 228 | by (induct m arbitrary: s) auto | 
| 50518 | 229 | |
| 230 | lemma sdrop_add[simp]: "sdrop n (sdrop m s) = sdrop (m + n) s" | |
| 51141 | 231 | by (induct m arbitrary: s) auto | 
| 232 | ||
| 57175 | 233 | lemma sdrop_snth: "sdrop n s !! m = s !! (n + m)" | 
| 234 | by (induct n arbitrary: m s) auto | |
| 235 | ||
| 51430 | 236 | partial_function (tailrec) sdrop_while :: "('a \<Rightarrow> bool) \<Rightarrow> 'a stream \<Rightarrow> 'a stream" where 
 | 
| 237 | "sdrop_while P s = (if P (shd s) then sdrop_while P (stl s) else s)" | |
| 238 | ||
| 54720 
0a9920e46b3a
removed obsolete codegen setup; Stream -> SCons; tuned
 traytel parents: 
54498diff
changeset | 239 | lemma sdrop_while_SCons[code]: | 
| 
0a9920e46b3a
removed obsolete codegen setup; Stream -> SCons; tuned
 traytel parents: 
54498diff
changeset | 240 | "sdrop_while P (a ## s) = (if P a then sdrop_while P s else a ## s)" | 
| 51430 | 241 | by (subst sdrop_while.simps) simp | 
| 242 | ||
| 243 | lemma sdrop_while_sdrop_LEAST: | |
| 244 | assumes "\<exists>n. P (s !! n)" | |
| 245 | shows "sdrop_while (Not o P) s = sdrop (LEAST n. P (s !! n)) s" | |
| 246 | proof - | |
| 247 | from assms obtain m where "P (s !! m)" "\<And>n. P (s !! n) \<Longrightarrow> m \<le> n" | |
| 248 | and *: "(LEAST n. P (s !! n)) = m" by atomize_elim (auto intro: LeastI Least_le) | |
| 249 | thus ?thesis unfolding * | |
| 250 | proof (induct m arbitrary: s) | |
| 251 | case (Suc m) | |
| 252 | hence "sdrop_while (Not \<circ> P) (stl s) = sdrop m (stl s)" | |
| 253 | by (metis (full_types) not_less_eq_eq snth.simps(2)) | |
| 254 | moreover from Suc(3) have "\<not> (P (s !! 0))" by blast | |
| 255 | ultimately show ?case by (subst sdrop_while.simps) simp | |
| 256 | qed (metis comp_apply sdrop.simps(1) sdrop_while.simps snth.simps(1)) | |
| 257 | qed | |
| 258 | ||
| 54027 
e5853a648b59
use new coinduction method and primcorec in examples
 traytel parents: 
53808diff
changeset | 259 | primcorec sfilter where | 
| 
e5853a648b59
use new coinduction method and primcorec in examples
 traytel parents: 
53808diff
changeset | 260 | "shd (sfilter P s) = shd (sdrop_while (Not o P) s)" | 
| 
e5853a648b59
use new coinduction method and primcorec in examples
 traytel parents: 
53808diff
changeset | 261 | | "stl (sfilter P s) = sfilter P (stl (sdrop_while (Not o P) s))" | 
| 52905 | 262 | |
| 263 | lemma sfilter_Stream: "sfilter P (x ## s) = (if P x then x ## sfilter P s else sfilter P s)" | |
| 264 | proof (cases "P x") | |
| 54720 
0a9920e46b3a
removed obsolete codegen setup; Stream -> SCons; tuned
 traytel parents: 
54498diff
changeset | 265 | case True thus ?thesis by (subst sfilter.ctr) (simp add: sdrop_while_SCons) | 
| 52905 | 266 | next | 
| 54720 
0a9920e46b3a
removed obsolete codegen setup; Stream -> SCons; tuned
 traytel parents: 
54498diff
changeset | 267 | case False thus ?thesis by (subst (1 2) sfilter.ctr) (simp add: sdrop_while_SCons) | 
| 52905 | 268 | qed | 
| 269 | ||
| 51141 | 270 | |
| 271 | subsection {* unary predicates lifted to streams *}
 | |
| 272 | ||
| 273 | definition "stream_all P s = (\<forall>p. P (s !! p))" | |
| 274 | ||
| 51772 
d2b265ebc1fa
specify nicer names for map, set and rel in the stream library
 traytel parents: 
51766diff
changeset | 275 | lemma stream_all_iff[iff]: "stream_all P s \<longleftrightarrow> Ball (sset s) P" | 
| 
d2b265ebc1fa
specify nicer names for map, set and rel in the stream library
 traytel parents: 
51766diff
changeset | 276 | unfolding stream_all_def sset_range by auto | 
| 51141 | 277 | |
| 278 | lemma stream_all_shift[simp]: "stream_all P (xs @- s) = (list_all P xs \<and> stream_all P s)" | |
| 279 | unfolding stream_all_iff list_all_iff by auto | |
| 280 | ||
| 54469 | 281 | lemma stream_all_Stream: "stream_all P (x ## X) \<longleftrightarrow> P x \<and> stream_all P X" | 
| 282 | by simp | |
| 283 | ||
| 51141 | 284 | |
| 285 | subsection {* recurring stream out of a list *}
 | |
| 286 | ||
| 54027 
e5853a648b59
use new coinduction method and primcorec in examples
 traytel parents: 
53808diff
changeset | 287 | primcorec cycle :: "'a list \<Rightarrow> 'a stream" where | 
| 
e5853a648b59
use new coinduction method and primcorec in examples
 traytel parents: 
53808diff
changeset | 288 | "shd (cycle xs) = hd xs" | 
| 
e5853a648b59
use new coinduction method and primcorec in examples
 traytel parents: 
53808diff
changeset | 289 | | "stl (cycle xs) = cycle (tl xs @ [hd xs])" | 
| 54720 
0a9920e46b3a
removed obsolete codegen setup; Stream -> SCons; tuned
 traytel parents: 
54498diff
changeset | 290 | |
| 51141 | 291 | lemma cycle_decomp: "u \<noteq> [] \<Longrightarrow> cycle u = u @- cycle u" | 
| 54027 
e5853a648b59
use new coinduction method and primcorec in examples
 traytel parents: 
53808diff
changeset | 292 | proof (coinduction arbitrary: u) | 
| 
e5853a648b59
use new coinduction method and primcorec in examples
 traytel parents: 
53808diff
changeset | 293 | case Eq_stream then show ?case using stream.collapse[of "cycle u"] | 
| 
e5853a648b59
use new coinduction method and primcorec in examples
 traytel parents: 
53808diff
changeset | 294 | by (auto intro!: exI[of _ "tl u @ [hd u]"]) | 
| 
e5853a648b59
use new coinduction method and primcorec in examples
 traytel parents: 
53808diff
changeset | 295 | qed | 
| 51141 | 296 | |
| 51409 | 297 | lemma cycle_Cons[code]: "cycle (x # xs) = x ## cycle (xs @ [x])" | 
| 54027 
e5853a648b59
use new coinduction method and primcorec in examples
 traytel parents: 
53808diff
changeset | 298 | by (subst cycle.ctr) simp | 
| 50518 | 299 | |
| 300 | lemma cycle_rotated: "\<lbrakk>v \<noteq> []; cycle u = v @- s\<rbrakk> \<Longrightarrow> cycle (tl u @ [hd u]) = tl v @- s" | |
| 51141 | 301 | by (auto dest: arg_cong[of _ _ stl]) | 
| 50518 | 302 | |
| 303 | lemma stake_append: "stake n (u @- s) = take (min (length u) n) u @ stake (n - length u) s" | |
| 304 | proof (induct n arbitrary: u) | |
| 305 | case (Suc n) thus ?case by (cases u) auto | |
| 306 | qed auto | |
| 307 | ||
| 308 | lemma stake_cycle_le[simp]: | |
| 309 | assumes "u \<noteq> []" "n < length u" | |
| 310 | shows "stake n (cycle u) = take n u" | |
| 311 | using min_absorb2[OF less_imp_le_nat[OF assms(2)]] | |
| 51141 | 312 | by (subst cycle_decomp[OF assms(1)], subst stake_append) auto | 
| 50518 | 313 | |
| 314 | lemma stake_cycle_eq[simp]: "u \<noteq> [] \<Longrightarrow> stake (length u) (cycle u) = u" | |
| 57175 | 315 | by (subst cycle_decomp) (auto simp: stake_shift) | 
| 50518 | 316 | |
| 317 | lemma sdrop_cycle_eq[simp]: "u \<noteq> [] \<Longrightarrow> sdrop (length u) (cycle u) = cycle u" | |
| 57175 | 318 | by (subst cycle_decomp) (auto simp: sdrop_shift) | 
| 50518 | 319 | |
| 320 | lemma stake_cycle_eq_mod_0[simp]: "\<lbrakk>u \<noteq> []; n mod length u = 0\<rbrakk> \<Longrightarrow> | |
| 321 | stake n (cycle u) = concat (replicate (n div length u) u)" | |
| 51141 | 322 | by (induct "n div length u" arbitrary: n u) (auto simp: stake_add[symmetric]) | 
| 50518 | 323 | |
| 324 | lemma sdrop_cycle_eq_mod_0[simp]: "\<lbrakk>u \<noteq> []; n mod length u = 0\<rbrakk> \<Longrightarrow> | |
| 325 | sdrop n (cycle u) = cycle u" | |
| 51141 | 326 | by (induct "n div length u" arbitrary: n u) (auto simp: sdrop_add[symmetric]) | 
| 50518 | 327 | |
| 328 | lemma stake_cycle: "u \<noteq> [] \<Longrightarrow> | |
| 329 | stake n (cycle u) = concat (replicate (n div length u) u) @ take (n mod length u) u" | |
| 51141 | 330 | by (subst mod_div_equality[of n "length u", symmetric], unfold stake_add[symmetric]) auto | 
| 50518 | 331 | |
| 332 | lemma sdrop_cycle: "u \<noteq> [] \<Longrightarrow> sdrop n (cycle u) = cycle (rotate (n mod length u) u)" | |
| 51141 | 333 | by (induct n arbitrary: u) (auto simp: rotate1_rotate_swap rotate1_hd_tl rotate_conv_mod[symmetric]) | 
| 334 | ||
| 335 | ||
| 54497 
c76dec4df4d7
BNF/Examples/Stream: rename same to sconst; define same, fromN in terms of siterate
 hoelzl parents: 
54469diff
changeset | 336 | subsection {* iterated application of a function *}
 | 
| 
c76dec4df4d7
BNF/Examples/Stream: rename same to sconst; define same, fromN in terms of siterate
 hoelzl parents: 
54469diff
changeset | 337 | |
| 
c76dec4df4d7
BNF/Examples/Stream: rename same to sconst; define same, fromN in terms of siterate
 hoelzl parents: 
54469diff
changeset | 338 | primcorec siterate where | 
| 
c76dec4df4d7
BNF/Examples/Stream: rename same to sconst; define same, fromN in terms of siterate
 hoelzl parents: 
54469diff
changeset | 339 | "shd (siterate f x) = x" | 
| 
c76dec4df4d7
BNF/Examples/Stream: rename same to sconst; define same, fromN in terms of siterate
 hoelzl parents: 
54469diff
changeset | 340 | | "stl (siterate f x) = siterate f (f x)" | 
| 
c76dec4df4d7
BNF/Examples/Stream: rename same to sconst; define same, fromN in terms of siterate
 hoelzl parents: 
54469diff
changeset | 341 | |
| 
c76dec4df4d7
BNF/Examples/Stream: rename same to sconst; define same, fromN in terms of siterate
 hoelzl parents: 
54469diff
changeset | 342 | lemma stake_Suc: "stake (Suc n) s = stake n s @ [s !! n]" | 
| 
c76dec4df4d7
BNF/Examples/Stream: rename same to sconst; define same, fromN in terms of siterate
 hoelzl parents: 
54469diff
changeset | 343 | by (induct n arbitrary: s) auto | 
| 
c76dec4df4d7
BNF/Examples/Stream: rename same to sconst; define same, fromN in terms of siterate
 hoelzl parents: 
54469diff
changeset | 344 | |
| 
c76dec4df4d7
BNF/Examples/Stream: rename same to sconst; define same, fromN in terms of siterate
 hoelzl parents: 
54469diff
changeset | 345 | lemma snth_siterate[simp]: "siterate f x !! n = (f^^n) x" | 
| 
c76dec4df4d7
BNF/Examples/Stream: rename same to sconst; define same, fromN in terms of siterate
 hoelzl parents: 
54469diff
changeset | 346 | by (induct n arbitrary: x) (auto simp: funpow_swap1) | 
| 
c76dec4df4d7
BNF/Examples/Stream: rename same to sconst; define same, fromN in terms of siterate
 hoelzl parents: 
54469diff
changeset | 347 | |
| 
c76dec4df4d7
BNF/Examples/Stream: rename same to sconst; define same, fromN in terms of siterate
 hoelzl parents: 
54469diff
changeset | 348 | lemma sdrop_siterate[simp]: "sdrop n (siterate f x) = siterate f ((f^^n) x)" | 
| 
c76dec4df4d7
BNF/Examples/Stream: rename same to sconst; define same, fromN in terms of siterate
 hoelzl parents: 
54469diff
changeset | 349 | by (induct n arbitrary: x) (auto simp: funpow_swap1) | 
| 
c76dec4df4d7
BNF/Examples/Stream: rename same to sconst; define same, fromN in terms of siterate
 hoelzl parents: 
54469diff
changeset | 350 | |
| 
c76dec4df4d7
BNF/Examples/Stream: rename same to sconst; define same, fromN in terms of siterate
 hoelzl parents: 
54469diff
changeset | 351 | lemma stake_siterate[simp]: "stake n (siterate f x) = map (\<lambda>n. (f^^n) x) [0 ..< n]" | 
| 
c76dec4df4d7
BNF/Examples/Stream: rename same to sconst; define same, fromN in terms of siterate
 hoelzl parents: 
54469diff
changeset | 352 | by (induct n arbitrary: x) (auto simp del: stake.simps(2) simp: stake_Suc) | 
| 
c76dec4df4d7
BNF/Examples/Stream: rename same to sconst; define same, fromN in terms of siterate
 hoelzl parents: 
54469diff
changeset | 353 | |
| 
c76dec4df4d7
BNF/Examples/Stream: rename same to sconst; define same, fromN in terms of siterate
 hoelzl parents: 
54469diff
changeset | 354 | lemma sset_siterate: "sset (siterate f x) = {(f^^n) x | n. True}"
 | 
| 
c76dec4df4d7
BNF/Examples/Stream: rename same to sconst; define same, fromN in terms of siterate
 hoelzl parents: 
54469diff
changeset | 355 | by (auto simp: sset_range) | 
| 
c76dec4df4d7
BNF/Examples/Stream: rename same to sconst; define same, fromN in terms of siterate
 hoelzl parents: 
54469diff
changeset | 356 | |
| 
c76dec4df4d7
BNF/Examples/Stream: rename same to sconst; define same, fromN in terms of siterate
 hoelzl parents: 
54469diff
changeset | 357 | lemma smap_siterate: "smap f (siterate f x) = siterate f (f x)" | 
| 
c76dec4df4d7
BNF/Examples/Stream: rename same to sconst; define same, fromN in terms of siterate
 hoelzl parents: 
54469diff
changeset | 358 | by (coinduction arbitrary: x) auto | 
| 
c76dec4df4d7
BNF/Examples/Stream: rename same to sconst; define same, fromN in terms of siterate
 hoelzl parents: 
54469diff
changeset | 359 | |
| 
c76dec4df4d7
BNF/Examples/Stream: rename same to sconst; define same, fromN in terms of siterate
 hoelzl parents: 
54469diff
changeset | 360 | |
| 51141 | 361 | subsection {* stream repeating a single element *}
 | 
| 362 | ||
| 54497 
c76dec4df4d7
BNF/Examples/Stream: rename same to sconst; define same, fromN in terms of siterate
 hoelzl parents: 
54469diff
changeset | 363 | abbreviation "sconst \<equiv> siterate id" | 
| 51141 | 364 | |
| 54497 
c76dec4df4d7
BNF/Examples/Stream: rename same to sconst; define same, fromN in terms of siterate
 hoelzl parents: 
54469diff
changeset | 365 | lemma shift_replicate_sconst[simp]: "replicate n x @- sconst x = sconst x" | 
| 
c76dec4df4d7
BNF/Examples/Stream: rename same to sconst; define same, fromN in terms of siterate
 hoelzl parents: 
54469diff
changeset | 366 | by (subst (3) stake_sdrop[symmetric]) (simp add: map_replicate_trivial) | 
| 51141 | 367 | |
| 57175 | 368 | lemma sset_sconst[simp]: "sset (sconst x) = {x}"
 | 
| 54497 
c76dec4df4d7
BNF/Examples/Stream: rename same to sconst; define same, fromN in terms of siterate
 hoelzl parents: 
54469diff
changeset | 369 | by (simp add: sset_siterate) | 
| 51141 | 370 | |
| 57175 | 371 | lemma sconst_alt: "s = sconst x \<longleftrightarrow> sset s = {x}"
 | 
| 372 | proof | |
| 373 |   assume "sset s = {x}"
 | |
| 374 | then show "s = sconst x" | |
| 375 | proof (coinduction arbitrary: s) | |
| 376 | case Eq_stream | |
| 377 |     then have "shd s = x" "sset (stl s) \<subseteq> {x}" by (case_tac [!] s) auto
 | |
| 378 |     then have "sset (stl s) = {x}" by (cases "stl s") auto
 | |
| 379 | with `shd s = x` show ?case by auto | |
| 380 | qed | |
| 381 | qed simp | |
| 382 | ||
| 59016 | 383 | lemma sconst_cycle: "sconst x = cycle [x]" | 
| 54497 
c76dec4df4d7
BNF/Examples/Stream: rename same to sconst; define same, fromN in terms of siterate
 hoelzl parents: 
54469diff
changeset | 384 | by coinduction auto | 
| 51141 | 385 | |
| 54497 
c76dec4df4d7
BNF/Examples/Stream: rename same to sconst; define same, fromN in terms of siterate
 hoelzl parents: 
54469diff
changeset | 386 | lemma smap_sconst: "smap f (sconst x) = sconst (f x)" | 
| 
c76dec4df4d7
BNF/Examples/Stream: rename same to sconst; define same, fromN in terms of siterate
 hoelzl parents: 
54469diff
changeset | 387 | by coinduction auto | 
| 51141 | 388 | |
| 54497 
c76dec4df4d7
BNF/Examples/Stream: rename same to sconst; define same, fromN in terms of siterate
 hoelzl parents: 
54469diff
changeset | 389 | lemma sconst_streams: "x \<in> A \<Longrightarrow> sconst x \<in> streams A" | 
| 
c76dec4df4d7
BNF/Examples/Stream: rename same to sconst; define same, fromN in terms of siterate
 hoelzl parents: 
54469diff
changeset | 390 | by (simp add: streams_iff_sset) | 
| 51141 | 391 | |
| 392 | ||
| 393 | subsection {* stream of natural numbers *}
 | |
| 394 | ||
| 54497 
c76dec4df4d7
BNF/Examples/Stream: rename same to sconst; define same, fromN in terms of siterate
 hoelzl parents: 
54469diff
changeset | 395 | abbreviation "fromN \<equiv> siterate Suc" | 
| 54469 | 396 | |
| 51141 | 397 | abbreviation "nats \<equiv> fromN 0" | 
| 398 | ||
| 54497 
c76dec4df4d7
BNF/Examples/Stream: rename same to sconst; define same, fromN in terms of siterate
 hoelzl parents: 
54469diff
changeset | 399 | lemma sset_fromN[simp]: "sset (fromN n) = {n ..}"
 | 
| 54720 
0a9920e46b3a
removed obsolete codegen setup; Stream -> SCons; tuned
 traytel parents: 
54498diff
changeset | 400 | by (auto simp add: sset_siterate le_iff_add) | 
| 54497 
c76dec4df4d7
BNF/Examples/Stream: rename same to sconst; define same, fromN in terms of siterate
 hoelzl parents: 
54469diff
changeset | 401 | |
| 57175 | 402 | lemma stream_smap_fromN: "s = smap (\<lambda>j. let i = j - n in s !! i) (fromN n)" | 
| 403 | by (coinduction arbitrary: s n) | |
| 404 | (force simp: neq_Nil_conv Let_def snth.simps(2)[symmetric] Suc_diff_Suc | |
| 405 | intro: stream.map_cong split: if_splits simp del: snth.simps(2)) | |
| 406 | ||
| 407 | lemma stream_smap_nats: "s = smap (snth s) nats" | |
| 408 | using stream_smap_fromN[where n = 0] by simp | |
| 409 | ||
| 51141 | 410 | |
| 51462 | 411 | subsection {* flatten a stream of lists *}
 | 
| 412 | ||
| 54027 
e5853a648b59
use new coinduction method and primcorec in examples
 traytel parents: 
53808diff
changeset | 413 | primcorec flat where | 
| 51462 | 414 | "shd (flat ws) = hd (shd ws)" | 
| 54027 
e5853a648b59
use new coinduction method and primcorec in examples
 traytel parents: 
53808diff
changeset | 415 | | "stl (flat ws) = flat (if tl (shd ws) = [] then stl ws else tl (shd ws) ## stl ws)" | 
| 51462 | 416 | |
| 417 | lemma flat_Cons[simp, code]: "flat ((x # xs) ## ws) = x ## flat (if xs = [] then ws else xs ## ws)" | |
| 54027 
e5853a648b59
use new coinduction method and primcorec in examples
 traytel parents: 
53808diff
changeset | 418 | by (subst flat.ctr) simp | 
| 51462 | 419 | |
| 420 | lemma flat_Stream[simp]: "xs \<noteq> [] \<Longrightarrow> flat (xs ## ws) = xs @- flat ws" | |
| 421 | by (induct xs) auto | |
| 422 | ||
| 423 | lemma flat_unfold: "shd ws \<noteq> [] \<Longrightarrow> flat ws = shd ws @- flat (stl ws)" | |
| 424 | by (cases ws) auto | |
| 425 | ||
| 51772 
d2b265ebc1fa
specify nicer names for map, set and rel in the stream library
 traytel parents: 
51766diff
changeset | 426 | lemma flat_snth: "\<forall>xs \<in> sset s. xs \<noteq> [] \<Longrightarrow> flat s !! n = (if n < length (shd s) then | 
| 51462 | 427 | shd s ! n else flat (stl s) !! (n - length (shd s)))" | 
| 51772 
d2b265ebc1fa
specify nicer names for map, set and rel in the stream library
 traytel parents: 
51766diff
changeset | 428 | by (metis flat_unfold not_less shd_sset shift_snth_ge shift_snth_less) | 
| 51462 | 429 | |
| 51772 
d2b265ebc1fa
specify nicer names for map, set and rel in the stream library
 traytel parents: 
51766diff
changeset | 430 | lemma sset_flat[simp]: "\<forall>xs \<in> sset s. xs \<noteq> [] \<Longrightarrow> | 
| 
d2b265ebc1fa
specify nicer names for map, set and rel in the stream library
 traytel parents: 
51766diff
changeset | 431 | sset (flat s) = (\<Union>xs \<in> sset s. set xs)" (is "?P \<Longrightarrow> ?L = ?R") | 
| 51462 | 432 | proof safe | 
| 433 | fix x assume ?P "x : ?L" | |
| 51772 
d2b265ebc1fa
specify nicer names for map, set and rel in the stream library
 traytel parents: 
51766diff
changeset | 434 | then obtain m where "x = flat s !! m" by (metis image_iff sset_range) | 
| 51462 | 435 | with `?P` obtain n m' where "x = s !! n ! m'" "m' < length (s !! n)" | 
| 436 | proof (atomize_elim, induct m arbitrary: s rule: less_induct) | |
| 437 | case (less y) | |
| 438 | thus ?case | |
| 439 | proof (cases "y < length (shd s)") | |
| 440 | case True thus ?thesis by (metis flat_snth less(2,3) snth.simps(1)) | |
| 441 | next | |
| 442 | case False | |
| 443 | hence "x = flat (stl s) !! (y - length (shd s))" by (metis less(2,3) flat_snth) | |
| 444 | moreover | |
| 53374 
a14d2a854c02
tuned proofs -- clarified flow of facts wrt. calculation;
 wenzelm parents: 
53290diff
changeset | 445 |       { from less(2) have *: "length (shd s) > 0" by (cases s) simp_all
 | 
| 
a14d2a854c02
tuned proofs -- clarified flow of facts wrt. calculation;
 wenzelm parents: 
53290diff
changeset | 446 | with False have "y > 0" by (cases y) simp_all | 
| 
a14d2a854c02
tuned proofs -- clarified flow of facts wrt. calculation;
 wenzelm parents: 
53290diff
changeset | 447 | with * have "y - length (shd s) < y" by simp | 
| 51462 | 448 | } | 
| 51772 
d2b265ebc1fa
specify nicer names for map, set and rel in the stream library
 traytel parents: 
51766diff
changeset | 449 | moreover have "\<forall>xs \<in> sset (stl s). xs \<noteq> []" using less(2) by (cases s) auto | 
| 51462 | 450 | ultimately have "\<exists>n m'. x = stl s !! n ! m' \<and> m' < length (stl s !! n)" by (intro less(1)) auto | 
| 451 | thus ?thesis by (metis snth.simps(2)) | |
| 452 | qed | |
| 453 | qed | |
| 51772 
d2b265ebc1fa
specify nicer names for map, set and rel in the stream library
 traytel parents: 
51766diff
changeset | 454 | thus "x \<in> ?R" by (auto simp: sset_range dest!: nth_mem) | 
| 51462 | 455 | next | 
| 51772 
d2b265ebc1fa
specify nicer names for map, set and rel in the stream library
 traytel parents: 
51766diff
changeset | 456 | fix x xs assume "xs \<in> sset s" ?P "x \<in> set xs" thus "x \<in> ?L" | 
| 57986 | 457 | by (induct rule: sset_induct) | 
| 51772 
d2b265ebc1fa
specify nicer names for map, set and rel in the stream library
 traytel parents: 
51766diff
changeset | 458 | (metis UnI1 flat_unfold shift.simps(1) sset_shift, | 
| 
d2b265ebc1fa
specify nicer names for map, set and rel in the stream library
 traytel parents: 
51766diff
changeset | 459 | metis UnI2 flat_unfold shd_sset stl_sset sset_shift) | 
| 51462 | 460 | qed | 
| 461 | ||
| 462 | ||
| 463 | subsection {* merge a stream of streams *}
 | |
| 464 | ||
| 465 | definition smerge :: "'a stream stream \<Rightarrow> 'a stream" where | |
| 51772 
d2b265ebc1fa
specify nicer names for map, set and rel in the stream library
 traytel parents: 
51766diff
changeset | 466 | "smerge ss = flat (smap (\<lambda>n. map (\<lambda>s. s !! n) (stake (Suc n) ss) @ stake n (ss !! n)) nats)" | 
| 51462 | 467 | |
| 468 | lemma stake_nth[simp]: "m < n \<Longrightarrow> stake n s ! m = s !! m" | |
| 469 | by (induct n arbitrary: s m) (auto simp: nth_Cons', metis Suc_pred snth.simps(2)) | |
| 470 | ||
| 51772 
d2b265ebc1fa
specify nicer names for map, set and rel in the stream library
 traytel parents: 
51766diff
changeset | 471 | lemma snth_sset_smerge: "ss !! n !! m \<in> sset (smerge ss)" | 
| 51462 | 472 | proof (cases "n \<le> m") | 
| 473 | case False thus ?thesis unfolding smerge_def | |
| 51772 
d2b265ebc1fa
specify nicer names for map, set and rel in the stream library
 traytel parents: 
51766diff
changeset | 474 | by (subst sset_flat) | 
| 53290 | 475 | (auto simp: stream.set_map in_set_conv_nth simp del: stake.simps | 
| 51462 | 476 | intro!: exI[of _ n, OF disjI2] exI[of _ m, OF mp]) | 
| 477 | next | |
| 478 | case True thus ?thesis unfolding smerge_def | |
| 51772 
d2b265ebc1fa
specify nicer names for map, set and rel in the stream library
 traytel parents: 
51766diff
changeset | 479 | by (subst sset_flat) | 
| 53290 | 480 | (auto simp: stream.set_map in_set_conv_nth image_iff simp del: stake.simps snth.simps | 
| 51462 | 481 | intro!: exI[of _ m, OF disjI1] bexI[of _ "ss !! n"] exI[of _ n, OF mp]) | 
| 482 | qed | |
| 483 | ||
| 51772 
d2b265ebc1fa
specify nicer names for map, set and rel in the stream library
 traytel parents: 
51766diff
changeset | 484 | lemma sset_smerge: "sset (smerge ss) = UNION (sset ss) sset" | 
| 51462 | 485 | proof safe | 
| 51772 
d2b265ebc1fa
specify nicer names for map, set and rel in the stream library
 traytel parents: 
51766diff
changeset | 486 | fix x assume "x \<in> sset (smerge ss)" | 
| 
d2b265ebc1fa
specify nicer names for map, set and rel in the stream library
 traytel parents: 
51766diff
changeset | 487 | thus "x \<in> UNION (sset ss) sset" | 
| 
d2b265ebc1fa
specify nicer names for map, set and rel in the stream library
 traytel parents: 
51766diff
changeset | 488 | unfolding smerge_def by (subst (asm) sset_flat) | 
| 53290 | 489 | (auto simp: stream.set_map in_set_conv_nth sset_range simp del: stake.simps, fast+) | 
| 51462 | 490 | next | 
| 51772 
d2b265ebc1fa
specify nicer names for map, set and rel in the stream library
 traytel parents: 
51766diff
changeset | 491 | fix s x assume "s \<in> sset ss" "x \<in> sset s" | 
| 
d2b265ebc1fa
specify nicer names for map, set and rel in the stream library
 traytel parents: 
51766diff
changeset | 492 | thus "x \<in> sset (smerge ss)" using snth_sset_smerge by (auto simp: sset_range) | 
| 51462 | 493 | qed | 
| 494 | ||
| 495 | ||
| 496 | subsection {* product of two streams *}
 | |
| 497 | ||
| 498 | definition sproduct :: "'a stream \<Rightarrow> 'b stream \<Rightarrow> ('a \<times> 'b) stream" where
 | |
| 51772 
d2b265ebc1fa
specify nicer names for map, set and rel in the stream library
 traytel parents: 
51766diff
changeset | 499 | "sproduct s1 s2 = smerge (smap (\<lambda>x. smap (Pair x) s2) s1)" | 
| 51462 | 500 | |
| 51772 
d2b265ebc1fa
specify nicer names for map, set and rel in the stream library
 traytel parents: 
51766diff
changeset | 501 | lemma sset_sproduct: "sset (sproduct s1 s2) = sset s1 \<times> sset s2" | 
| 53290 | 502 | unfolding sproduct_def sset_smerge by (auto simp: stream.set_map) | 
| 51462 | 503 | |
| 504 | ||
| 505 | subsection {* interleave two streams *}
 | |
| 506 | ||
| 54027 
e5853a648b59
use new coinduction method and primcorec in examples
 traytel parents: 
53808diff
changeset | 507 | primcorec sinterleave where | 
| 
e5853a648b59
use new coinduction method and primcorec in examples
 traytel parents: 
53808diff
changeset | 508 | "shd (sinterleave s1 s2) = shd s1" | 
| 
e5853a648b59
use new coinduction method and primcorec in examples
 traytel parents: 
53808diff
changeset | 509 | | "stl (sinterleave s1 s2) = sinterleave s2 (stl s1)" | 
| 51462 | 510 | |
| 511 | lemma sinterleave_code[code]: | |
| 512 | "sinterleave (x ## s1) s2 = x ## sinterleave s2 s1" | |
| 54027 
e5853a648b59
use new coinduction method and primcorec in examples
 traytel parents: 
53808diff
changeset | 513 | by (subst sinterleave.ctr) simp | 
| 51462 | 514 | |
| 515 | lemma sinterleave_snth[simp]: | |
| 516 | "even n \<Longrightarrow> sinterleave s1 s2 !! n = s1 !! (n div 2)" | |
| 58710 
7216a10d69ba
augmented and tuned facts on even/odd and division
 haftmann parents: 
58607diff
changeset | 517 | "odd n \<Longrightarrow> sinterleave s1 s2 !! n = s2 !! (n div 2)" | 
| 
7216a10d69ba
augmented and tuned facts on even/odd and division
 haftmann parents: 
58607diff
changeset | 518 | by (induct n arbitrary: s1 s2) simp_all | 
| 51462 | 519 | |
| 51772 
d2b265ebc1fa
specify nicer names for map, set and rel in the stream library
 traytel parents: 
51766diff
changeset | 520 | lemma sset_sinterleave: "sset (sinterleave s1 s2) = sset s1 \<union> sset s2" | 
| 51462 | 521 | proof (intro equalityI subsetI) | 
| 51772 
d2b265ebc1fa
specify nicer names for map, set and rel in the stream library
 traytel parents: 
51766diff
changeset | 522 | fix x assume "x \<in> sset (sinterleave s1 s2)" | 
| 
d2b265ebc1fa
specify nicer names for map, set and rel in the stream library
 traytel parents: 
51766diff
changeset | 523 | then obtain n where "x = sinterleave s1 s2 !! n" unfolding sset_range by blast | 
| 
d2b265ebc1fa
specify nicer names for map, set and rel in the stream library
 traytel parents: 
51766diff
changeset | 524 | thus "x \<in> sset s1 \<union> sset s2" by (cases "even n") auto | 
| 51462 | 525 | next | 
| 51772 
d2b265ebc1fa
specify nicer names for map, set and rel in the stream library
 traytel parents: 
51766diff
changeset | 526 | fix x assume "x \<in> sset s1 \<union> sset s2" | 
| 
d2b265ebc1fa
specify nicer names for map, set and rel in the stream library
 traytel parents: 
51766diff
changeset | 527 | thus "x \<in> sset (sinterleave s1 s2)" | 
| 51462 | 528 | proof | 
| 51772 
d2b265ebc1fa
specify nicer names for map, set and rel in the stream library
 traytel parents: 
51766diff
changeset | 529 | assume "x \<in> sset s1" | 
| 
d2b265ebc1fa
specify nicer names for map, set and rel in the stream library
 traytel parents: 
51766diff
changeset | 530 | then obtain n where "x = s1 !! n" unfolding sset_range by blast | 
| 51462 | 531 | hence "sinterleave s1 s2 !! (2 * n) = x" by simp | 
| 51772 
d2b265ebc1fa
specify nicer names for map, set and rel in the stream library
 traytel parents: 
51766diff
changeset | 532 | thus ?thesis unfolding sset_range by blast | 
| 51462 | 533 | next | 
| 51772 
d2b265ebc1fa
specify nicer names for map, set and rel in the stream library
 traytel parents: 
51766diff
changeset | 534 | assume "x \<in> sset s2" | 
| 
d2b265ebc1fa
specify nicer names for map, set and rel in the stream library
 traytel parents: 
51766diff
changeset | 535 | then obtain n where "x = s2 !! n" unfolding sset_range by blast | 
| 51462 | 536 | hence "sinterleave s1 s2 !! (2 * n + 1) = x" by simp | 
| 51772 
d2b265ebc1fa
specify nicer names for map, set and rel in the stream library
 traytel parents: 
51766diff
changeset | 537 | thus ?thesis unfolding sset_range by blast | 
| 51462 | 538 | qed | 
| 539 | qed | |
| 540 | ||
| 541 | ||
| 51141 | 542 | subsection {* zip *}
 | 
| 543 | ||
| 54027 
e5853a648b59
use new coinduction method and primcorec in examples
 traytel parents: 
53808diff
changeset | 544 | primcorec szip where | 
| 
e5853a648b59
use new coinduction method and primcorec in examples
 traytel parents: 
53808diff
changeset | 545 | "shd (szip s1 s2) = (shd s1, shd s2)" | 
| 
e5853a648b59
use new coinduction method and primcorec in examples
 traytel parents: 
53808diff
changeset | 546 | | "stl (szip s1 s2) = szip (stl s1) (stl s2)" | 
| 51141 | 547 | |
| 54720 
0a9920e46b3a
removed obsolete codegen setup; Stream -> SCons; tuned
 traytel parents: 
54498diff
changeset | 548 | lemma szip_unfold[code]: "szip (a ## s1) (b ## s2) = (a, b) ## (szip s1 s2)" | 
| 54027 
e5853a648b59
use new coinduction method and primcorec in examples
 traytel parents: 
53808diff
changeset | 549 | by (subst szip.ctr) simp | 
| 51409 | 550 | |
| 51141 | 551 | lemma snth_szip[simp]: "szip s1 s2 !! n = (s1 !! n, s2 !! n)" | 
| 552 | by (induct n arbitrary: s1 s2) auto | |
| 553 | ||
| 57175 | 554 | lemma stake_szip[simp]: | 
| 555 | "stake n (szip s1 s2) = zip (stake n s1) (stake n s2)" | |
| 556 | by (induct n arbitrary: s1 s2) auto | |
| 557 | ||
| 558 | lemma sdrop_szip[simp]: "sdrop n (szip s1 s2) = szip (sdrop n s1) (sdrop n s2)" | |
| 559 | by (induct n arbitrary: s1 s2) auto | |
| 560 | ||
| 561 | lemma smap_szip_fst: | |
| 562 | "smap (\<lambda>x. f (fst x)) (szip s1 s2) = smap f s1" | |
| 563 | by (coinduction arbitrary: s1 s2) auto | |
| 564 | ||
| 565 | lemma smap_szip_snd: | |
| 566 | "smap (\<lambda>x. g (snd x)) (szip s1 s2) = smap g s2" | |
| 567 | by (coinduction arbitrary: s1 s2) auto | |
| 568 | ||
| 51141 | 569 | |
| 570 | subsection {* zip via function *}
 | |
| 571 | ||
| 54027 
e5853a648b59
use new coinduction method and primcorec in examples
 traytel parents: 
53808diff
changeset | 572 | primcorec smap2 where | 
| 51772 
d2b265ebc1fa
specify nicer names for map, set and rel in the stream library
 traytel parents: 
51766diff
changeset | 573 | "shd (smap2 f s1 s2) = f (shd s1) (shd s2)" | 
| 54027 
e5853a648b59
use new coinduction method and primcorec in examples
 traytel parents: 
53808diff
changeset | 574 | | "stl (smap2 f s1 s2) = smap2 f (stl s1) (stl s2)" | 
| 51141 | 575 | |
| 51772 
d2b265ebc1fa
specify nicer names for map, set and rel in the stream library
 traytel parents: 
51766diff
changeset | 576 | lemma smap2_unfold[code]: | 
| 54720 
0a9920e46b3a
removed obsolete codegen setup; Stream -> SCons; tuned
 traytel parents: 
54498diff
changeset | 577 | "smap2 f (a ## s1) (b ## s2) = f a b ## (smap2 f s1 s2)" | 
| 54027 
e5853a648b59
use new coinduction method and primcorec in examples
 traytel parents: 
53808diff
changeset | 578 | by (subst smap2.ctr) simp | 
| 51409 | 579 | |
| 51772 
d2b265ebc1fa
specify nicer names for map, set and rel in the stream library
 traytel parents: 
51766diff
changeset | 580 | lemma smap2_szip: | 
| 
d2b265ebc1fa
specify nicer names for map, set and rel in the stream library
 traytel parents: 
51766diff
changeset | 581 | "smap2 f s1 s2 = smap (split f) (szip s1 s2)" | 
| 54027 
e5853a648b59
use new coinduction method and primcorec in examples
 traytel parents: 
53808diff
changeset | 582 | by (coinduction arbitrary: s1 s2) auto | 
| 50518 | 583 | |
| 57175 | 584 | lemma smap_smap2[simp]: | 
| 585 | "smap f (smap2 g s1 s2) = smap2 (\<lambda>x y. f (g x y)) s1 s2" | |
| 586 | unfolding smap2_szip stream.map_comp o_def split_def .. | |
| 587 | ||
| 588 | lemma smap2_alt: | |
| 589 | "(smap2 f s1 s2 = s) = (\<forall>n. f (s1 !! n) (s2 !! n) = s !! n)" | |
| 590 | unfolding smap2_szip smap_alt by auto | |
| 591 | ||
| 592 | lemma snth_smap2[simp]: | |
| 593 | "smap2 f s1 s2 !! n = f (s1 !! n) (s2 !! n)" | |
| 594 | by (induct n arbitrary: s1 s2) auto | |
| 595 | ||
| 596 | lemma stake_smap2[simp]: | |
| 597 | "stake n (smap2 f s1 s2) = map (split f) (zip (stake n s1) (stake n s2))" | |
| 598 | by (induct n arbitrary: s1 s2) auto | |
| 599 | ||
| 600 | lemma sdrop_smap2[simp]: | |
| 601 | "sdrop n (smap2 f s1 s2) = smap2 f (sdrop n s1) (sdrop n s2)" | |
| 602 | by (induct n arbitrary: s1 s2) auto | |
| 603 | ||
| 50518 | 604 | end |