author | traytel |
Wed, 24 Apr 2013 12:15:06 +0200 | |
changeset 51753 | 199ce8cf604c |
parent 51695 | 876281e7642f |
child 51766 | f19a4d0ab1bf |
permissions | -rw-r--r-- |
50518 | 1 |
(* Title: HOL/BNF/Examples/Stream.thy |
2 |
Author: Dmitriy Traytel, TU Muenchen |
|
3 |
Author: Andrei Popescu, TU Muenchen |
|
4 |
Copyright 2012 |
|
5 |
||
6 |
Infinite streams. |
|
7 |
*) |
|
8 |
||
9 |
header {* Infinite Streams *} |
|
10 |
||
11 |
theory Stream |
|
12 |
imports "../BNF" |
|
13 |
begin |
|
14 |
||
51023
550f265864e3
infix syntax for streams (reflecting the one for lists)
traytel
parents:
50518
diff
changeset
|
15 |
codata 'a stream = Stream (shd: 'a) (stl: "'a stream") (infixr "##" 65) |
50518 | 16 |
|
51409 | 17 |
declaration {* |
18 |
Nitpick_HOL.register_codatatype |
|
19 |
@{typ "'stream_element_type stream"} @{const_name stream_case} [dest_Const @{term Stream}] |
|
20 |
(*FIXME: long type variable name required to reduce the probability of |
|
21 |
a name clash of Nitpick in context. E.g.: |
|
22 |
context |
|
23 |
fixes x :: 'stream_element_type |
|
24 |
begin |
|
25 |
||
26 |
lemma "stream_set s = {}" |
|
27 |
nitpick |
|
28 |
oops |
|
29 |
||
30 |
end |
|
31 |
*) |
|
32 |
*} |
|
33 |
||
34 |
code_datatype Stream |
|
35 |
lemmas [code] = stream.sels stream.sets stream.case |
|
36 |
||
37 |
lemma stream_case_cert: |
|
38 |
assumes "CASE \<equiv> stream_case c" |
|
39 |
shows "CASE (a ## s) \<equiv> c a s" |
|
40 |
using assms by simp_all |
|
41 |
||
42 |
setup {* |
|
43 |
Code.add_case @{thm stream_case_cert} |
|
44 |
*} |
|
45 |
||
51462 | 46 |
(*for code generation only*) |
47 |
definition smember :: "'a \<Rightarrow> 'a stream \<Rightarrow> bool" where |
|
48 |
[code_abbrev]: "smember x s \<longleftrightarrow> x \<in> stream_set s" |
|
49 |
||
50 |
lemma smember_code[code, simp]: "smember x (Stream y s) = (if x = y then True else smember x s)" |
|
51 |
unfolding smember_def by auto |
|
52 |
||
53 |
hide_const (open) smember |
|
54 |
||
50518 | 55 |
(* TODO: Provide by the package*) |
56 |
theorem stream_set_induct: |
|
51141 | 57 |
"\<lbrakk>\<And>s. P (shd s) s; \<And>s y. \<lbrakk>y \<in> stream_set (stl s); P y (stl s)\<rbrakk> \<Longrightarrow> P y s\<rbrakk> \<Longrightarrow> |
58 |
\<forall>y \<in> stream_set s. P y s" |
|
59 |
by (rule stream.dtor_set_induct) |
|
60 |
(auto simp add: shd_def stl_def stream_case_def fsts_def snds_def split_beta) |
|
61 |
||
62 |
lemma stream_map_simps[simp]: |
|
63 |
"shd (stream_map f s) = f (shd s)" "stl (stream_map f s) = stream_map f (stl s)" |
|
64 |
unfolding shd_def stl_def stream_case_def stream_map_def stream.dtor_unfold |
|
65 |
by (case_tac [!] s) (auto simp: Stream_def stream.dtor_ctor) |
|
66 |
||
51753
199ce8cf604c
took out workaround for bug fixed in 5af40820948b
traytel
parents:
51695
diff
changeset
|
67 |
declare stream.map[code] |
50518 | 68 |
|
69 |
theorem shd_stream_set: "shd s \<in> stream_set s" |
|
51141 | 70 |
by (auto simp add: shd_def stl_def stream_case_def fsts_def snds_def split_beta) |
71 |
(metis UnCI fsts_def insertI1 stream.dtor_set) |
|
50518 | 72 |
|
73 |
theorem stl_stream_set: "y \<in> stream_set (stl s) \<Longrightarrow> y \<in> stream_set s" |
|
51141 | 74 |
by (auto simp add: shd_def stl_def stream_case_def fsts_def snds_def split_beta) |
75 |
(metis insertI1 set_mp snds_def stream.dtor_set_set_incl) |
|
50518 | 76 |
|
77 |
(* only for the non-mutual case: *) |
|
78 |
theorem stream_set_induct1[consumes 1, case_names shd stl, induct set: "stream_set"]: |
|
79 |
assumes "y \<in> stream_set s" and "\<And>s. P (shd s) s" |
|
80 |
and "\<And>s y. \<lbrakk>y \<in> stream_set (stl s); P y (stl s)\<rbrakk> \<Longrightarrow> P y s" |
|
81 |
shows "P y s" |
|
51141 | 82 |
using assms stream_set_induct by blast |
50518 | 83 |
(* end TODO *) |
84 |
||
85 |
||
86 |
subsection {* prepend list to stream *} |
|
87 |
||
88 |
primrec shift :: "'a list \<Rightarrow> 'a stream \<Rightarrow> 'a stream" (infixr "@-" 65) where |
|
89 |
"shift [] s = s" |
|
51023
550f265864e3
infix syntax for streams (reflecting the one for lists)
traytel
parents:
50518
diff
changeset
|
90 |
| "shift (x # xs) s = x ## shift xs s" |
50518 | 91 |
|
51353 | 92 |
lemma stream_map_shift[simp]: "stream_map f (xs @- s) = map f xs @- stream_map f s" |
93 |
by (induct xs) auto |
|
94 |
||
50518 | 95 |
lemma shift_append[simp]: "(xs @ ys) @- s = xs @- ys @- s" |
51141 | 96 |
by (induct xs) auto |
50518 | 97 |
|
98 |
lemma shift_simps[simp]: |
|
99 |
"shd (xs @- s) = (if xs = [] then shd s else hd xs)" |
|
100 |
"stl (xs @- s) = (if xs = [] then stl s else tl xs @- s)" |
|
51141 | 101 |
by (induct xs) auto |
50518 | 102 |
|
51141 | 103 |
lemma stream_set_shift[simp]: "stream_set (xs @- s) = set xs \<union> stream_set s" |
104 |
by (induct xs) auto |
|
50518 | 105 |
|
51352 | 106 |
lemma shift_left_inj[simp]: "xs @- s1 = xs @- s2 \<longleftrightarrow> s1 = s2" |
107 |
by (induct xs) auto |
|
108 |
||
50518 | 109 |
|
51141 | 110 |
subsection {* set of streams with elements in some fixes set *} |
50518 | 111 |
|
112 |
coinductive_set |
|
113 |
streams :: "'a set => 'a stream set" |
|
114 |
for A :: "'a set" |
|
115 |
where |
|
51023
550f265864e3
infix syntax for streams (reflecting the one for lists)
traytel
parents:
50518
diff
changeset
|
116 |
Stream[intro!, simp, no_atp]: "\<lbrakk>a \<in> A; s \<in> streams A\<rbrakk> \<Longrightarrow> a ## s \<in> streams A" |
50518 | 117 |
|
118 |
lemma shift_streams: "\<lbrakk>w \<in> lists A; s \<in> streams A\<rbrakk> \<Longrightarrow> w @- s \<in> streams A" |
|
51141 | 119 |
by (induct w) auto |
50518 | 120 |
|
121 |
lemma stream_set_streams: |
|
122 |
assumes "stream_set s \<subseteq> A" |
|
123 |
shows "s \<in> streams A" |
|
51023
550f265864e3
infix syntax for streams (reflecting the one for lists)
traytel
parents:
50518
diff
changeset
|
124 |
proof (coinduct rule: streams.coinduct[of "\<lambda>s'. \<exists>a s. s' = a ## s \<and> a \<in> A \<and> stream_set s \<subseteq> A"]) |
50518 | 125 |
case streams from assms show ?case by (cases s) auto |
126 |
next |
|
51023
550f265864e3
infix syntax for streams (reflecting the one for lists)
traytel
parents:
50518
diff
changeset
|
127 |
fix s' assume "\<exists>a s. s' = a ## s \<and> a \<in> A \<and> stream_set s \<subseteq> A" |
50518 | 128 |
then guess a s by (elim exE) |
51023
550f265864e3
infix syntax for streams (reflecting the one for lists)
traytel
parents:
50518
diff
changeset
|
129 |
with assms show "\<exists>a l. s' = a ## l \<and> |
550f265864e3
infix syntax for streams (reflecting the one for lists)
traytel
parents:
50518
diff
changeset
|
130 |
a \<in> A \<and> ((\<exists>a s. l = a ## s \<and> a \<in> A \<and> stream_set s \<subseteq> A) \<or> l \<in> streams A)" |
50518 | 131 |
by (cases s) auto |
132 |
qed |
|
133 |
||
134 |
||
51141 | 135 |
subsection {* nth, take, drop for streams *} |
136 |
||
137 |
primrec snth :: "'a stream \<Rightarrow> nat \<Rightarrow> 'a" (infixl "!!" 100) where |
|
138 |
"s !! 0 = shd s" |
|
139 |
| "s !! Suc n = stl s !! n" |
|
140 |
||
141 |
lemma snth_stream_map[simp]: "stream_map f s !! n = f (s !! n)" |
|
142 |
by (induct n arbitrary: s) auto |
|
143 |
||
144 |
lemma shift_snth_less[simp]: "p < length xs \<Longrightarrow> (xs @- s) !! p = xs ! p" |
|
145 |
by (induct p arbitrary: xs) (auto simp: hd_conv_nth nth_tl) |
|
146 |
||
147 |
lemma shift_snth_ge[simp]: "p \<ge> length xs \<Longrightarrow> (xs @- s) !! p = s !! (p - length xs)" |
|
148 |
by (induct p arbitrary: xs) (auto simp: Suc_diff_eq_diff_pred) |
|
149 |
||
150 |
lemma snth_stream_set[simp]: "s !! n \<in> stream_set s" |
|
151 |
by (induct n arbitrary: s) (auto intro: shd_stream_set stl_stream_set) |
|
152 |
||
153 |
lemma stream_set_range: "stream_set s = range (snth s)" |
|
154 |
proof (intro equalityI subsetI) |
|
155 |
fix x assume "x \<in> stream_set s" |
|
156 |
thus "x \<in> range (snth s)" |
|
157 |
proof (induct s) |
|
158 |
case (stl s x) |
|
159 |
then obtain n where "x = stl s !! n" by auto |
|
160 |
thus ?case by (auto intro: range_eqI[of _ _ "Suc n"]) |
|
161 |
qed (auto intro: range_eqI[of _ _ 0]) |
|
162 |
qed auto |
|
50518 | 163 |
|
164 |
primrec stake :: "nat \<Rightarrow> 'a stream \<Rightarrow> 'a list" where |
|
165 |
"stake 0 s = []" |
|
166 |
| "stake (Suc n) s = shd s # stake n (stl s)" |
|
167 |
||
51141 | 168 |
lemma length_stake[simp]: "length (stake n s) = n" |
169 |
by (induct n arbitrary: s) auto |
|
170 |
||
171 |
lemma stake_stream_map[simp]: "stake n (stream_map f s) = map f (stake n s)" |
|
172 |
by (induct n arbitrary: s) auto |
|
173 |
||
50518 | 174 |
primrec sdrop :: "nat \<Rightarrow> 'a stream \<Rightarrow> 'a stream" where |
175 |
"sdrop 0 s = s" |
|
176 |
| "sdrop (Suc n) s = sdrop n (stl s)" |
|
177 |
||
51141 | 178 |
lemma sdrop_simps[simp]: |
179 |
"shd (sdrop n s) = s !! n" "stl (sdrop n s) = sdrop (Suc n) s" |
|
180 |
by (induct n arbitrary: s) auto |
|
181 |
||
182 |
lemma sdrop_stream_map[simp]: "sdrop n (stream_map f s) = stream_map f (sdrop n s)" |
|
183 |
by (induct n arbitrary: s) auto |
|
50518 | 184 |
|
51352 | 185 |
lemma sdrop_stl: "sdrop n (stl s) = stl (sdrop n s)" |
186 |
by (induct n) auto |
|
187 |
||
50518 | 188 |
lemma stake_sdrop: "stake n s @- sdrop n s = s" |
51141 | 189 |
by (induct n arbitrary: s) auto |
190 |
||
191 |
lemma id_stake_snth_sdrop: |
|
192 |
"s = stake i s @- s !! i ## sdrop (Suc i) s" |
|
193 |
by (subst stake_sdrop[symmetric, of _ i]) (metis sdrop_simps stream.collapse) |
|
50518 | 194 |
|
51141 | 195 |
lemma stream_map_alt: "stream_map f s = s' \<longleftrightarrow> (\<forall>n. f (s !! n) = s' !! n)" (is "?L = ?R") |
196 |
proof |
|
197 |
assume ?R |
|
198 |
thus ?L |
|
199 |
by (coinduct rule: stream.coinduct[of "\<lambda>s1 s2. \<exists>n. s1 = stream_map f (sdrop n s) \<and> s2 = sdrop n s'"]) |
|
200 |
(auto intro: exI[of _ 0] simp del: sdrop.simps(2)) |
|
201 |
qed auto |
|
202 |
||
203 |
lemma stake_invert_Nil[iff]: "stake n s = [] \<longleftrightarrow> n = 0" |
|
204 |
by (induct n) auto |
|
50518 | 205 |
|
206 |
lemma sdrop_shift: "\<lbrakk>s = w @- s'; length w = n\<rbrakk> \<Longrightarrow> sdrop n s = s'" |
|
51141 | 207 |
by (induct n arbitrary: w s) auto |
50518 | 208 |
|
209 |
lemma stake_shift: "\<lbrakk>s = w @- s'; length w = n\<rbrakk> \<Longrightarrow> stake n s = w" |
|
51141 | 210 |
by (induct n arbitrary: w s) auto |
50518 | 211 |
|
212 |
lemma stake_add[simp]: "stake m s @ stake n (sdrop m s) = stake (m + n) s" |
|
51141 | 213 |
by (induct m arbitrary: s) auto |
50518 | 214 |
|
215 |
lemma sdrop_add[simp]: "sdrop n (sdrop m s) = sdrop (m + n) s" |
|
51141 | 216 |
by (induct m arbitrary: s) auto |
217 |
||
51430 | 218 |
partial_function (tailrec) sdrop_while :: "('a \<Rightarrow> bool) \<Rightarrow> 'a stream \<Rightarrow> 'a stream" where |
219 |
"sdrop_while P s = (if P (shd s) then sdrop_while P (stl s) else s)" |
|
220 |
||
221 |
lemma sdrop_while_Stream[code]: |
|
222 |
"sdrop_while P (Stream a s) = (if P a then sdrop_while P s else Stream a s)" |
|
223 |
by (subst sdrop_while.simps) simp |
|
224 |
||
225 |
lemma sdrop_while_sdrop_LEAST: |
|
226 |
assumes "\<exists>n. P (s !! n)" |
|
227 |
shows "sdrop_while (Not o P) s = sdrop (LEAST n. P (s !! n)) s" |
|
228 |
proof - |
|
229 |
from assms obtain m where "P (s !! m)" "\<And>n. P (s !! n) \<Longrightarrow> m \<le> n" |
|
230 |
and *: "(LEAST n. P (s !! n)) = m" by atomize_elim (auto intro: LeastI Least_le) |
|
231 |
thus ?thesis unfolding * |
|
232 |
proof (induct m arbitrary: s) |
|
233 |
case (Suc m) |
|
234 |
hence "sdrop_while (Not \<circ> P) (stl s) = sdrop m (stl s)" |
|
235 |
by (metis (full_types) not_less_eq_eq snth.simps(2)) |
|
236 |
moreover from Suc(3) have "\<not> (P (s !! 0))" by blast |
|
237 |
ultimately show ?case by (subst sdrop_while.simps) simp |
|
238 |
qed (metis comp_apply sdrop.simps(1) sdrop_while.simps snth.simps(1)) |
|
239 |
qed |
|
240 |
||
51141 | 241 |
|
242 |
subsection {* unary predicates lifted to streams *} |
|
243 |
||
244 |
definition "stream_all P s = (\<forall>p. P (s !! p))" |
|
245 |
||
246 |
lemma stream_all_iff[iff]: "stream_all P s \<longleftrightarrow> Ball (stream_set s) P" |
|
247 |
unfolding stream_all_def stream_set_range by auto |
|
248 |
||
249 |
lemma stream_all_shift[simp]: "stream_all P (xs @- s) = (list_all P xs \<and> stream_all P s)" |
|
250 |
unfolding stream_all_iff list_all_iff by auto |
|
251 |
||
252 |
||
253 |
subsection {* recurring stream out of a list *} |
|
254 |
||
255 |
definition cycle :: "'a list \<Rightarrow> 'a stream" where |
|
256 |
"cycle = stream_unfold hd (\<lambda>xs. tl xs @ [hd xs])" |
|
257 |
||
258 |
lemma cycle_simps[simp]: |
|
259 |
"shd (cycle u) = hd u" |
|
260 |
"stl (cycle u) = cycle (tl u @ [hd u])" |
|
261 |
by (auto simp: cycle_def) |
|
262 |
||
263 |
lemma cycle_decomp: "u \<noteq> [] \<Longrightarrow> cycle u = u @- cycle u" |
|
264 |
proof (coinduct rule: stream.coinduct[of "\<lambda>s1 s2. \<exists>u. s1 = cycle u \<and> s2 = u @- cycle u \<and> u \<noteq> []"]) |
|
265 |
case (2 s1 s2) |
|
266 |
then obtain u where "s1 = cycle u \<and> s2 = u @- cycle u \<and> u \<noteq> []" by blast |
|
267 |
thus ?case using stream.unfold[of hd "\<lambda>xs. tl xs @ [hd xs]" u] by (auto simp: cycle_def) |
|
268 |
qed auto |
|
269 |
||
51409 | 270 |
lemma cycle_Cons[code]: "cycle (x # xs) = x ## cycle (xs @ [x])" |
51141 | 271 |
proof (coinduct rule: stream.coinduct[of "\<lambda>s1 s2. \<exists>x xs. s1 = cycle (x # xs) \<and> s2 = x ## cycle (xs @ [x])"]) |
272 |
case (2 s1 s2) |
|
273 |
then obtain x xs where "s1 = cycle (x # xs) \<and> s2 = x ## cycle (xs @ [x])" by blast |
|
274 |
thus ?case |
|
275 |
by (auto simp: cycle_def intro!: exI[of _ "hd (xs @ [x])"] exI[of _ "tl (xs @ [x])"] stream.unfold) |
|
276 |
qed auto |
|
50518 | 277 |
|
278 |
lemma cycle_rotated: "\<lbrakk>v \<noteq> []; cycle u = v @- s\<rbrakk> \<Longrightarrow> cycle (tl u @ [hd u]) = tl v @- s" |
|
51141 | 279 |
by (auto dest: arg_cong[of _ _ stl]) |
50518 | 280 |
|
281 |
lemma stake_append: "stake n (u @- s) = take (min (length u) n) u @ stake (n - length u) s" |
|
282 |
proof (induct n arbitrary: u) |
|
283 |
case (Suc n) thus ?case by (cases u) auto |
|
284 |
qed auto |
|
285 |
||
286 |
lemma stake_cycle_le[simp]: |
|
287 |
assumes "u \<noteq> []" "n < length u" |
|
288 |
shows "stake n (cycle u) = take n u" |
|
289 |
using min_absorb2[OF less_imp_le_nat[OF assms(2)]] |
|
51141 | 290 |
by (subst cycle_decomp[OF assms(1)], subst stake_append) auto |
50518 | 291 |
|
292 |
lemma stake_cycle_eq[simp]: "u \<noteq> [] \<Longrightarrow> stake (length u) (cycle u) = u" |
|
51141 | 293 |
by (metis cycle_decomp stake_shift) |
50518 | 294 |
|
295 |
lemma sdrop_cycle_eq[simp]: "u \<noteq> [] \<Longrightarrow> sdrop (length u) (cycle u) = cycle u" |
|
51141 | 296 |
by (metis cycle_decomp sdrop_shift) |
50518 | 297 |
|
298 |
lemma stake_cycle_eq_mod_0[simp]: "\<lbrakk>u \<noteq> []; n mod length u = 0\<rbrakk> \<Longrightarrow> |
|
299 |
stake n (cycle u) = concat (replicate (n div length u) u)" |
|
51141 | 300 |
by (induct "n div length u" arbitrary: n u) (auto simp: stake_add[symmetric]) |
50518 | 301 |
|
302 |
lemma sdrop_cycle_eq_mod_0[simp]: "\<lbrakk>u \<noteq> []; n mod length u = 0\<rbrakk> \<Longrightarrow> |
|
303 |
sdrop n (cycle u) = cycle u" |
|
51141 | 304 |
by (induct "n div length u" arbitrary: n u) (auto simp: sdrop_add[symmetric]) |
50518 | 305 |
|
306 |
lemma stake_cycle: "u \<noteq> [] \<Longrightarrow> |
|
307 |
stake n (cycle u) = concat (replicate (n div length u) u) @ take (n mod length u) u" |
|
51141 | 308 |
by (subst mod_div_equality[of n "length u", symmetric], unfold stake_add[symmetric]) auto |
50518 | 309 |
|
310 |
lemma sdrop_cycle: "u \<noteq> [] \<Longrightarrow> sdrop n (cycle u) = cycle (rotate (n mod length u) u)" |
|
51141 | 311 |
by (induct n arbitrary: u) (auto simp: rotate1_rotate_swap rotate1_hd_tl rotate_conv_mod[symmetric]) |
312 |
||
313 |
||
314 |
subsection {* stream repeating a single element *} |
|
315 |
||
316 |
definition "same x = stream_unfold (\<lambda>_. x) id ()" |
|
317 |
||
318 |
lemma same_simps[simp]: "shd (same x) = x" "stl (same x) = same x" |
|
319 |
unfolding same_def by auto |
|
320 |
||
51409 | 321 |
lemma same_unfold[code]: "same x = x ## same x" |
51141 | 322 |
by (metis same_simps stream.collapse) |
323 |
||
324 |
lemma snth_same[simp]: "same x !! n = x" |
|
325 |
unfolding same_def by (induct n) auto |
|
326 |
||
327 |
lemma stake_same[simp]: "stake n (same x) = replicate n x" |
|
328 |
unfolding same_def by (induct n) (auto simp: upt_rec) |
|
329 |
||
330 |
lemma sdrop_same[simp]: "sdrop n (same x) = same x" |
|
331 |
unfolding same_def by (induct n) auto |
|
332 |
||
333 |
lemma shift_replicate_same[simp]: "replicate n x @- same x = same x" |
|
334 |
by (metis sdrop_same stake_same stake_sdrop) |
|
335 |
||
336 |
lemma stream_all_same[simp]: "stream_all P (same x) \<longleftrightarrow> P x" |
|
337 |
unfolding stream_all_def by auto |
|
338 |
||
339 |
lemma same_cycle: "same x = cycle [x]" |
|
340 |
by (coinduct rule: stream.coinduct[of "\<lambda>s1 s2. s1 = same x \<and> s2 = cycle [x]"]) auto |
|
341 |
||
342 |
||
343 |
subsection {* stream of natural numbers *} |
|
344 |
||
345 |
definition "fromN n = stream_unfold id Suc n" |
|
346 |
||
347 |
lemma fromN_simps[simp]: "shd (fromN n) = n" "stl (fromN n) = fromN (Suc n)" |
|
348 |
unfolding fromN_def by auto |
|
349 |
||
51409 | 350 |
lemma fromN_unfold[code]: "fromN n = n ## fromN (Suc n)" |
351 |
unfolding fromN_def by (metis id_def stream.unfold) |
|
352 |
||
51141 | 353 |
lemma snth_fromN[simp]: "fromN n !! m = n + m" |
354 |
unfolding fromN_def by (induct m arbitrary: n) auto |
|
355 |
||
356 |
lemma stake_fromN[simp]: "stake m (fromN n) = [n ..< m + n]" |
|
357 |
unfolding fromN_def by (induct m arbitrary: n) (auto simp: upt_rec) |
|
358 |
||
359 |
lemma sdrop_fromN[simp]: "sdrop m (fromN n) = fromN (n + m)" |
|
360 |
unfolding fromN_def by (induct m arbitrary: n) auto |
|
361 |
||
51352 | 362 |
lemma stream_set_fromN[simp]: "stream_set (fromN n) = {n ..}" (is "?L = ?R") |
363 |
proof safe |
|
364 |
fix m assume "m : ?L" |
|
365 |
moreover |
|
366 |
{ fix s assume "m \<in> stream_set s" "\<exists>n'\<ge>n. s = fromN n'" |
|
367 |
hence "n \<le> m" by (induct arbitrary: n rule: stream_set_induct1) fastforce+ |
|
368 |
} |
|
369 |
ultimately show "n \<le> m" by blast |
|
370 |
next |
|
371 |
fix m assume "n \<le> m" thus "m \<in> ?L" by (metis le_iff_add snth_fromN snth_stream_set) |
|
372 |
qed |
|
373 |
||
51141 | 374 |
abbreviation "nats \<equiv> fromN 0" |
375 |
||
376 |
||
51462 | 377 |
subsection {* flatten a stream of lists *} |
378 |
||
379 |
definition flat where |
|
380 |
"flat \<equiv> stream_unfold (hd o shd) (\<lambda>s. if tl (shd s) = [] then stl s else tl (shd s) ## stl s)" |
|
381 |
||
382 |
lemma flat_simps[simp]: |
|
383 |
"shd (flat ws) = hd (shd ws)" |
|
384 |
"stl (flat ws) = flat (if tl (shd ws) = [] then stl ws else tl (shd ws) ## stl ws)" |
|
385 |
unfolding flat_def by auto |
|
386 |
||
387 |
lemma flat_Cons[simp, code]: "flat ((x # xs) ## ws) = x ## flat (if xs = [] then ws else xs ## ws)" |
|
388 |
unfolding flat_def using stream.unfold[of "hd o shd" _ "(x # xs) ## ws"] by auto |
|
389 |
||
390 |
lemma flat_Stream[simp]: "xs \<noteq> [] \<Longrightarrow> flat (xs ## ws) = xs @- flat ws" |
|
391 |
by (induct xs) auto |
|
392 |
||
393 |
lemma flat_unfold: "shd ws \<noteq> [] \<Longrightarrow> flat ws = shd ws @- flat (stl ws)" |
|
394 |
by (cases ws) auto |
|
395 |
||
396 |
lemma flat_snth: "\<forall>xs \<in> stream_set s. xs \<noteq> [] \<Longrightarrow> flat s !! n = (if n < length (shd s) then |
|
397 |
shd s ! n else flat (stl s) !! (n - length (shd s)))" |
|
398 |
by (metis flat_unfold not_less shd_stream_set shift_snth_ge shift_snth_less) |
|
399 |
||
400 |
lemma stream_set_flat[simp]: "\<forall>xs \<in> stream_set s. xs \<noteq> [] \<Longrightarrow> |
|
401 |
stream_set (flat s) = (\<Union>xs \<in> stream_set s. set xs)" (is "?P \<Longrightarrow> ?L = ?R") |
|
402 |
proof safe |
|
403 |
fix x assume ?P "x : ?L" |
|
404 |
then obtain m where "x = flat s !! m" by (metis image_iff stream_set_range) |
|
405 |
with `?P` obtain n m' where "x = s !! n ! m'" "m' < length (s !! n)" |
|
406 |
proof (atomize_elim, induct m arbitrary: s rule: less_induct) |
|
407 |
case (less y) |
|
408 |
thus ?case |
|
409 |
proof (cases "y < length (shd s)") |
|
410 |
case True thus ?thesis by (metis flat_snth less(2,3) snth.simps(1)) |
|
411 |
next |
|
412 |
case False |
|
413 |
hence "x = flat (stl s) !! (y - length (shd s))" by (metis less(2,3) flat_snth) |
|
414 |
moreover |
|
415 |
{ from less(2) have "length (shd s) > 0" by (cases s) simp_all |
|
416 |
moreover with False have "y > 0" by (cases y) simp_all |
|
417 |
ultimately have "y - length (shd s) < y" by simp |
|
418 |
} |
|
419 |
moreover have "\<forall>xs \<in> stream_set (stl s). xs \<noteq> []" using less(2) by (cases s) auto |
|
420 |
ultimately have "\<exists>n m'. x = stl s !! n ! m' \<and> m' < length (stl s !! n)" by (intro less(1)) auto |
|
421 |
thus ?thesis by (metis snth.simps(2)) |
|
422 |
qed |
|
423 |
qed |
|
424 |
thus "x \<in> ?R" by (auto simp: stream_set_range dest!: nth_mem) |
|
425 |
next |
|
426 |
fix x xs assume "xs \<in> stream_set s" ?P "x \<in> set xs" thus "x \<in> ?L" |
|
427 |
by (induct rule: stream_set_induct1) |
|
428 |
(metis UnI1 flat_unfold shift.simps(1) stream_set_shift, |
|
429 |
metis UnI2 flat_unfold shd_stream_set stl_stream_set stream_set_shift) |
|
430 |
qed |
|
431 |
||
432 |
||
433 |
subsection {* merge a stream of streams *} |
|
434 |
||
435 |
definition smerge :: "'a stream stream \<Rightarrow> 'a stream" where |
|
436 |
"smerge ss = flat (stream_map (\<lambda>n. map (\<lambda>s. s !! n) (stake (Suc n) ss) @ stake n (ss !! n)) nats)" |
|
437 |
||
438 |
lemma stake_nth[simp]: "m < n \<Longrightarrow> stake n s ! m = s !! m" |
|
439 |
by (induct n arbitrary: s m) (auto simp: nth_Cons', metis Suc_pred snth.simps(2)) |
|
440 |
||
441 |
lemma snth_stream_set_smerge: "ss !! n !! m \<in> stream_set (smerge ss)" |
|
442 |
proof (cases "n \<le> m") |
|
443 |
case False thus ?thesis unfolding smerge_def |
|
444 |
by (subst stream_set_flat) |
|
445 |
(auto simp: stream.set_natural' in_set_conv_nth simp del: stake.simps |
|
446 |
intro!: exI[of _ n, OF disjI2] exI[of _ m, OF mp]) |
|
447 |
next |
|
448 |
case True thus ?thesis unfolding smerge_def |
|
449 |
by (subst stream_set_flat) |
|
450 |
(auto simp: stream.set_natural' in_set_conv_nth image_iff simp del: stake.simps snth.simps |
|
451 |
intro!: exI[of _ m, OF disjI1] bexI[of _ "ss !! n"] exI[of _ n, OF mp]) |
|
452 |
qed |
|
453 |
||
454 |
lemma stream_set_smerge: "stream_set (smerge ss) = UNION (stream_set ss) stream_set" |
|
455 |
proof safe |
|
456 |
fix x assume "x \<in> stream_set (smerge ss)" |
|
457 |
thus "x \<in> UNION (stream_set ss) stream_set" |
|
458 |
unfolding smerge_def by (subst (asm) stream_set_flat) |
|
459 |
(auto simp: stream.set_natural' in_set_conv_nth stream_set_range simp del: stake.simps, fast+) |
|
460 |
next |
|
461 |
fix s x assume "s \<in> stream_set ss" "x \<in> stream_set s" |
|
462 |
thus "x \<in> stream_set (smerge ss)" using snth_stream_set_smerge by (auto simp: stream_set_range) |
|
463 |
qed |
|
464 |
||
465 |
||
466 |
subsection {* product of two streams *} |
|
467 |
||
468 |
definition sproduct :: "'a stream \<Rightarrow> 'b stream \<Rightarrow> ('a \<times> 'b) stream" where |
|
469 |
"sproduct s1 s2 = smerge (stream_map (\<lambda>x. stream_map (Pair x) s2) s1)" |
|
470 |
||
471 |
lemma stream_set_sproduct: "stream_set (sproduct s1 s2) = stream_set s1 \<times> stream_set s2" |
|
472 |
unfolding sproduct_def stream_set_smerge by (auto simp: stream.set_natural') |
|
473 |
||
474 |
||
475 |
subsection {* interleave two streams *} |
|
476 |
||
477 |
definition sinterleave :: "'a stream \<Rightarrow> 'a stream \<Rightarrow> 'a stream" where |
|
478 |
[code del]: "sinterleave s1 s2 = |
|
479 |
stream_unfold (\<lambda>(s1, s2). shd s1) (\<lambda>(s1, s2). (s2, stl s1)) (s1, s2)" |
|
480 |
||
481 |
lemma sinterleave_simps[simp]: |
|
482 |
"shd (sinterleave s1 s2) = shd s1" "stl (sinterleave s1 s2) = sinterleave s2 (stl s1)" |
|
483 |
unfolding sinterleave_def by auto |
|
484 |
||
485 |
lemma sinterleave_code[code]: |
|
486 |
"sinterleave (x ## s1) s2 = x ## sinterleave s2 s1" |
|
487 |
by (metis sinterleave_simps stream.exhaust stream.sels) |
|
488 |
||
489 |
lemma sinterleave_snth[simp]: |
|
490 |
"even n \<Longrightarrow> sinterleave s1 s2 !! n = s1 !! (n div 2)" |
|
491 |
"odd n \<Longrightarrow> sinterleave s1 s2 !! n = s2 !! (n div 2)" |
|
492 |
by (induct n arbitrary: s1 s2) |
|
493 |
(auto dest: even_nat_Suc_div_2 odd_nat_plus_one_div_two[folded nat_2]) |
|
494 |
||
495 |
lemma stream_set_sinterleave: "stream_set (sinterleave s1 s2) = stream_set s1 \<union> stream_set s2" |
|
496 |
proof (intro equalityI subsetI) |
|
497 |
fix x assume "x \<in> stream_set (sinterleave s1 s2)" |
|
498 |
then obtain n where "x = sinterleave s1 s2 !! n" unfolding stream_set_range by blast |
|
499 |
thus "x \<in> stream_set s1 \<union> stream_set s2" by (cases "even n") auto |
|
500 |
next |
|
501 |
fix x assume "x \<in> stream_set s1 \<union> stream_set s2" |
|
502 |
thus "x \<in> stream_set (sinterleave s1 s2)" |
|
503 |
proof |
|
504 |
assume "x \<in> stream_set s1" |
|
505 |
then obtain n where "x = s1 !! n" unfolding stream_set_range by blast |
|
506 |
hence "sinterleave s1 s2 !! (2 * n) = x" by simp |
|
507 |
thus ?thesis unfolding stream_set_range by blast |
|
508 |
next |
|
509 |
assume "x \<in> stream_set s2" |
|
510 |
then obtain n where "x = s2 !! n" unfolding stream_set_range by blast |
|
511 |
hence "sinterleave s1 s2 !! (2 * n + 1) = x" by simp |
|
512 |
thus ?thesis unfolding stream_set_range by blast |
|
513 |
qed |
|
514 |
qed |
|
515 |
||
516 |
||
51141 | 517 |
subsection {* zip *} |
518 |
||
519 |
definition "szip s1 s2 = |
|
520 |
stream_unfold (map_pair shd shd) (map_pair stl stl) (s1, s2)" |
|
521 |
||
522 |
lemma szip_simps[simp]: |
|
523 |
"shd (szip s1 s2) = (shd s1, shd s2)" "stl (szip s1 s2) = szip (stl s1) (stl s2)" |
|
524 |
unfolding szip_def by auto |
|
525 |
||
51409 | 526 |
lemma szip_unfold[code]: "szip (Stream a s1) (Stream b s2) = Stream (a, b) (szip s1 s2)" |
527 |
unfolding szip_def by (subst stream.unfold) simp |
|
528 |
||
51141 | 529 |
lemma snth_szip[simp]: "szip s1 s2 !! n = (s1 !! n, s2 !! n)" |
530 |
by (induct n arbitrary: s1 s2) auto |
|
531 |
||
532 |
||
533 |
subsection {* zip via function *} |
|
534 |
||
535 |
definition "stream_map2 f s1 s2 = |
|
536 |
stream_unfold (\<lambda>(s1,s2). f (shd s1) (shd s2)) (map_pair stl stl) (s1, s2)" |
|
537 |
||
538 |
lemma stream_map2_simps[simp]: |
|
51409 | 539 |
"shd (stream_map2 f s1 s2) = f (shd s1) (shd s2)" |
540 |
"stl (stream_map2 f s1 s2) = stream_map2 f (stl s1) (stl s2)" |
|
51141 | 541 |
unfolding stream_map2_def by auto |
542 |
||
51409 | 543 |
lemma stream_map2_unfold[code]: |
544 |
"stream_map2 f (Stream a s1) (Stream b s2) = Stream (f a b) (stream_map2 f s1 s2)" |
|
545 |
unfolding stream_map2_def by (subst stream.unfold) simp |
|
546 |
||
51141 | 547 |
lemma stream_map2_szip: |
548 |
"stream_map2 f s1 s2 = stream_map (split f) (szip s1 s2)" |
|
549 |
by (coinduct rule: stream.coinduct[of |
|
550 |
"\<lambda>s1 s2. \<exists>s1' s2'. s1 = stream_map2 f s1' s2' \<and> s2 = stream_map (split f) (szip s1' s2')"]) |
|
551 |
fastforce+ |
|
50518 | 552 |
|
51462 | 553 |
|
554 |
subsection {* iterated application of a function *} |
|
555 |
||
556 |
definition siterate :: "('a \<Rightarrow> 'a) \<Rightarrow> 'a \<Rightarrow> 'a stream" where |
|
557 |
"siterate f x = x ## stream_unfold f f x" |
|
558 |
||
559 |
lemma siterate_simps[simp]: "shd (siterate f x) = x" "stl (siterate f x) = siterate f (f x)" |
|
560 |
unfolding siterate_def by (auto intro: stream.unfold) |
|
561 |
||
562 |
lemma siterate_code[code]: "siterate f x = x ## siterate f (f x)" |
|
563 |
by (metis siterate_def stream.unfold) |
|
564 |
||
565 |
lemma stake_Suc: "stake (Suc n) s = stake n s @ [s !! n]" |
|
566 |
by (induct n arbitrary: s) auto |
|
567 |
||
568 |
lemma snth_siterate[simp]: "siterate f x !! n = (f^^n) x" |
|
569 |
by (induct n arbitrary: x) (auto simp: funpow_swap1) |
|
570 |
||
571 |
lemma sdrop_siterate[simp]: "sdrop n (siterate f x) = siterate f ((f^^n) x)" |
|
572 |
by (induct n arbitrary: x) (auto simp: funpow_swap1) |
|
573 |
||
574 |
lemma stake_siterate[simp]: "stake n (siterate f x) = map (\<lambda>n. (f^^n) x) [0 ..< n]" |
|
575 |
by (induct n arbitrary: x) (auto simp del: stake.simps(2) simp: stake_Suc) |
|
576 |
||
577 |
lemma stream_set_siterate: "stream_set (siterate f x) = {(f^^n) x | n. True}" |
|
578 |
by (auto simp: stream_set_range) |
|
579 |
||
50518 | 580 |
end |