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