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