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