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