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