src/HOL/Library/Stream.thy
author nipkow
Tue Sep 22 14:31:22 2015 +0200 (2015-09-22)
changeset 61225 1a690dce8cfc
parent 60500 903bb1495239
child 61424 c3658c18b7bc
permissions -rw-r--r--
tuned references
     1 (*  Title:      HOL/Library/Stream.thy
     2     Author:     Dmitriy Traytel, TU Muenchen
     3     Author:     Andrei Popescu, TU Muenchen
     4     Copyright   2012, 2013
     5 
     6 Infinite streams.
     7 *)
     8 
     9 section \<open>Infinite Streams\<close>
    10 
    11 theory Stream
    12 imports "~~/src/HOL/Library/Nat_Bijection"
    13 begin
    14 
    15 codatatype (sset: 'a) stream =
    16   SCons (shd: 'a) (stl: "'a stream") (infixr "##" 65)
    17 for
    18   map: smap
    19   rel: stream_all2
    20 
    21 context
    22 begin
    23 
    24 (*for code generation only*)
    25 qualified definition smember :: "'a \<Rightarrow> 'a stream \<Rightarrow> bool" where
    26   [code_abbrev]: "smember x s \<longleftrightarrow> x \<in> sset s"
    27 
    28 lemma smember_code[code, simp]: "smember x (y ## s) = (if x = y then True else smember x s)"
    29   unfolding smember_def by auto
    30 
    31 end
    32 
    33 lemmas smap_simps[simp] = stream.map_sel
    34 lemmas shd_sset = stream.set_sel(1)
    35 lemmas stl_sset = stream.set_sel(2)
    36 
    37 theorem sset_induct[consumes 1, case_names shd stl, induct set: sset]:
    38   assumes "y \<in> sset s" and "\<And>s. P (shd s) s" and "\<And>s y. \<lbrakk>y \<in> sset (stl s); P y (stl s)\<rbrakk> \<Longrightarrow> P y s"
    39   shows "P y s"
    40 using assms by induct (metis stream.sel(1), auto)
    41 
    42 lemma smap_ctr: "smap f s = x ## s' \<longleftrightarrow> f (shd s) = x \<and> smap f (stl s) = s'"
    43   by (cases s) simp
    44 
    45 subsection \<open>prepend list to stream\<close>
    46 
    47 primrec shift :: "'a list \<Rightarrow> 'a stream \<Rightarrow> 'a stream" (infixr "@-" 65) where
    48   "shift [] s = s"
    49 | "shift (x # xs) s = x ## shift xs s"
    50 
    51 lemma smap_shift[simp]: "smap f (xs @- s) = map f xs @- smap f s"
    52   by (induct xs) auto
    53 
    54 lemma shift_append[simp]: "(xs @ ys) @- s = xs @- ys @- s"
    55   by (induct xs) auto
    56 
    57 lemma shift_simps[simp]:
    58    "shd (xs @- s) = (if xs = [] then shd s else hd xs)"
    59    "stl (xs @- s) = (if xs = [] then stl s else tl xs @- s)"
    60   by (induct xs) auto
    61 
    62 lemma sset_shift[simp]: "sset (xs @- s) = set xs \<union> sset s"
    63   by (induct xs) auto
    64 
    65 lemma shift_left_inj[simp]: "xs @- s1 = xs @- s2 \<longleftrightarrow> s1 = s2"
    66   by (induct xs) auto
    67 
    68 
    69 subsection \<open>set of streams with elements in some fixed set\<close>
    70 
    71 coinductive_set
    72   streams :: "'a set \<Rightarrow> 'a stream set"
    73   for A :: "'a set"
    74 where
    75   Stream[intro!, simp, no_atp]: "\<lbrakk>a \<in> A; s \<in> streams A\<rbrakk> \<Longrightarrow> a ## s \<in> streams A"
    76 
    77 lemma in_streams: "stl s \<in> streams S \<Longrightarrow> shd s \<in> S \<Longrightarrow> s \<in> streams S"
    78   by (cases s) auto
    79 
    80 lemma streamsE: "s \<in> streams A \<Longrightarrow> (shd s \<in> A \<Longrightarrow> stl s \<in> streams A \<Longrightarrow> P) \<Longrightarrow> P"
    81   by (erule streams.cases) simp_all
    82 
    83 lemma Stream_image: "x ## y \<in> (op ## x') ` Y \<longleftrightarrow> x = x' \<and> y \<in> Y"
    84   by auto
    85 
    86 lemma shift_streams: "\<lbrakk>w \<in> lists A; s \<in> streams A\<rbrakk> \<Longrightarrow> w @- s \<in> streams A"
    87   by (induct w) auto
    88 
    89 lemma streams_Stream: "x ## s \<in> streams A \<longleftrightarrow> x \<in> A \<and> s \<in> streams A"
    90   by (auto elim: streams.cases)
    91 
    92 lemma streams_stl: "s \<in> streams A \<Longrightarrow> stl s \<in> streams A"
    93   by (cases s) (auto simp: streams_Stream)
    94 
    95 lemma streams_shd: "s \<in> streams A \<Longrightarrow> shd s \<in> A"
    96   by (cases s) (auto simp: streams_Stream)
    97 
    98 lemma sset_streams:
    99   assumes "sset s \<subseteq> A"
   100   shows "s \<in> streams A"
   101 using assms proof (coinduction arbitrary: s)
   102   case streams then show ?case by (cases s) simp
   103 qed
   104 
   105 lemma streams_sset:
   106   assumes "s \<in> streams A"
   107   shows "sset s \<subseteq> A"
   108 proof
   109   fix x assume "x \<in> sset s" from this \<open>s \<in> streams A\<close> show "x \<in> A"
   110     by (induct s) (auto intro: streams_shd streams_stl)
   111 qed
   112 
   113 lemma streams_iff_sset: "s \<in> streams A \<longleftrightarrow> sset s \<subseteq> A"
   114   by (metis sset_streams streams_sset)
   115 
   116 lemma streams_mono:  "s \<in> streams A \<Longrightarrow> A \<subseteq> B \<Longrightarrow> s \<in> streams B"
   117   unfolding streams_iff_sset by auto
   118 
   119 lemma streams_mono2: "S \<subseteq> T \<Longrightarrow> streams S \<subseteq> streams T"
   120   by (auto intro: streams_mono)
   121 
   122 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"
   123   unfolding streams_iff_sset stream.set_map by auto
   124 
   125 lemma streams_empty: "streams {} = {}"
   126   by (auto elim: streams.cases)
   127 
   128 lemma streams_UNIV[simp]: "streams UNIV = UNIV"
   129   by (auto simp: streams_iff_sset)
   130 
   131 subsection \<open>nth, take, drop for streams\<close>
   132 
   133 primrec snth :: "'a stream \<Rightarrow> nat \<Rightarrow> 'a" (infixl "!!" 100) where
   134   "s !! 0 = shd s"
   135 | "s !! Suc n = stl s !! n"
   136 
   137 lemma snth_Stream: "(x ## s) !! Suc i = s !! i"
   138   by simp
   139 
   140 lemma snth_smap[simp]: "smap f s !! n = f (s !! n)"
   141   by (induct n arbitrary: s) auto
   142 
   143 lemma shift_snth_less[simp]: "p < length xs \<Longrightarrow> (xs @- s) !! p = xs ! p"
   144   by (induct p arbitrary: xs) (auto simp: hd_conv_nth nth_tl)
   145 
   146 lemma shift_snth_ge[simp]: "p \<ge> length xs \<Longrightarrow> (xs @- s) !! p = s !! (p - length xs)"
   147   by (induct p arbitrary: xs) (auto simp: Suc_diff_eq_diff_pred)
   148 
   149 lemma shift_snth: "(xs @- s) !! n = (if n < length xs then xs ! n else s !! (n - length xs))"
   150   by auto
   151 
   152 lemma snth_sset[simp]: "s !! n \<in> sset s"
   153   by (induct n arbitrary: s) (auto intro: shd_sset stl_sset)
   154 
   155 lemma sset_range: "sset s = range (snth s)"
   156 proof (intro equalityI subsetI)
   157   fix x assume "x \<in> sset s"
   158   thus "x \<in> range (snth s)"
   159   proof (induct s)
   160     case (stl s x)
   161     then obtain n where "x = stl s !! n" by auto
   162     thus ?case by (auto intro: range_eqI[of _ _ "Suc n"])
   163   qed (auto intro: range_eqI[of _ _ 0])
   164 qed auto
   165 
   166 lemma streams_iff_snth: "s \<in> streams X \<longleftrightarrow> (\<forall>n. s !! n \<in> X)"
   167   by (force simp: streams_iff_sset sset_range)
   168 
   169 lemma snth_in: "s \<in> streams X \<Longrightarrow> s !! n \<in> X"
   170   by (simp add: streams_iff_snth)
   171 
   172 primrec stake :: "nat \<Rightarrow> 'a stream \<Rightarrow> 'a list" where
   173   "stake 0 s = []"
   174 | "stake (Suc n) s = shd s # stake n (stl s)"
   175 
   176 lemma length_stake[simp]: "length (stake n s) = n"
   177   by (induct n arbitrary: s) auto
   178 
   179 lemma stake_smap[simp]: "stake n (smap f s) = map f (stake n s)"
   180   by (induct n arbitrary: s) auto
   181 
   182 lemma take_stake: "take n (stake m s) = stake (min n m) s"
   183 proof (induct m arbitrary: s n)
   184   case (Suc m) thus ?case by (cases n) auto
   185 qed simp
   186 
   187 primrec sdrop :: "nat \<Rightarrow> 'a stream \<Rightarrow> 'a stream" where
   188   "sdrop 0 s = s"
   189 | "sdrop (Suc n) s = sdrop n (stl s)"
   190 
   191 lemma sdrop_simps[simp]:
   192   "shd (sdrop n s) = s !! n" "stl (sdrop n s) = sdrop (Suc n) s"
   193   by (induct n arbitrary: s)  auto
   194 
   195 lemma sdrop_smap[simp]: "sdrop n (smap f s) = smap f (sdrop n s)"
   196   by (induct n arbitrary: s) auto
   197 
   198 lemma sdrop_stl: "sdrop n (stl s) = stl (sdrop n s)"
   199   by (induct n) auto
   200 
   201 lemma drop_stake: "drop n (stake m s) = stake (m - n) (sdrop n s)"
   202 proof (induct m arbitrary: s n)
   203   case (Suc m) thus ?case by (cases n) auto
   204 qed simp
   205 
   206 lemma stake_sdrop: "stake n s @- sdrop n s = s"
   207   by (induct n arbitrary: s) auto
   208 
   209 lemma id_stake_snth_sdrop:
   210   "s = stake i s @- s !! i ## sdrop (Suc i) s"
   211   by (subst stake_sdrop[symmetric, of _ i]) (metis sdrop_simps stream.collapse)
   212 
   213 lemma smap_alt: "smap f s = s' \<longleftrightarrow> (\<forall>n. f (s !! n) = s' !! n)" (is "?L = ?R")
   214 proof
   215   assume ?R
   216   then have "\<And>n. smap f (sdrop n s) = sdrop n s'"
   217     by coinduction (auto intro: exI[of _ 0] simp del: sdrop.simps(2))
   218   then show ?L using sdrop.simps(1) by metis
   219 qed auto
   220 
   221 lemma stake_invert_Nil[iff]: "stake n s = [] \<longleftrightarrow> n = 0"
   222   by (induct n) auto
   223 
   224 lemma sdrop_shift: "sdrop i (w @- s) = drop i w @- sdrop (i - length w) s"
   225   by (induct i arbitrary: w s) (auto simp: drop_tl drop_Suc neq_Nil_conv)
   226 
   227 lemma stake_shift: "stake i (w @- s) = take i w @ stake (i - length w) s"
   228   by (induct i arbitrary: w s) (auto simp: neq_Nil_conv)
   229 
   230 lemma stake_add[simp]: "stake m s @ stake n (sdrop m s) = stake (m + n) s"
   231   by (induct m arbitrary: s) auto
   232 
   233 lemma sdrop_add[simp]: "sdrop n (sdrop m s) = sdrop (m + n) s"
   234   by (induct m arbitrary: s) auto
   235 
   236 lemma sdrop_snth: "sdrop n s !! m = s !! (n + m)"
   237   by (induct n arbitrary: m s) auto
   238 
   239 partial_function (tailrec) sdrop_while :: "('a \<Rightarrow> bool) \<Rightarrow> 'a stream \<Rightarrow> 'a stream" where 
   240   "sdrop_while P s = (if P (shd s) then sdrop_while P (stl s) else s)"
   241 
   242 lemma sdrop_while_SCons[code]:
   243   "sdrop_while P (a ## s) = (if P a then sdrop_while P s else a ## s)"
   244   by (subst sdrop_while.simps) simp
   245 
   246 lemma sdrop_while_sdrop_LEAST:
   247   assumes "\<exists>n. P (s !! n)"
   248   shows "sdrop_while (Not o P) s = sdrop (LEAST n. P (s !! n)) s"
   249 proof -
   250   from assms obtain m where "P (s !! m)" "\<And>n. P (s !! n) \<Longrightarrow> m \<le> n"
   251     and *: "(LEAST n. P (s !! n)) = m" by atomize_elim (auto intro: LeastI Least_le)
   252   thus ?thesis unfolding *
   253   proof (induct m arbitrary: s)
   254     case (Suc m)
   255     hence "sdrop_while (Not \<circ> P) (stl s) = sdrop m (stl s)"
   256       by (metis (full_types) not_less_eq_eq snth.simps(2))
   257     moreover from Suc(3) have "\<not> (P (s !! 0))" by blast
   258     ultimately show ?case by (subst sdrop_while.simps) simp
   259   qed (metis comp_apply sdrop.simps(1) sdrop_while.simps snth.simps(1))
   260 qed
   261 
   262 primcorec sfilter where
   263   "shd (sfilter P s) = shd (sdrop_while (Not o P) s)"
   264 | "stl (sfilter P s) = sfilter P (stl (sdrop_while (Not o P) s))"
   265 
   266 lemma sfilter_Stream: "sfilter P (x ## s) = (if P x then x ## sfilter P s else sfilter P s)"
   267 proof (cases "P x")
   268   case True thus ?thesis by (subst sfilter.ctr) (simp add: sdrop_while_SCons)
   269 next
   270   case False thus ?thesis by (subst (1 2) sfilter.ctr) (simp add: sdrop_while_SCons)
   271 qed
   272 
   273 
   274 subsection \<open>unary predicates lifted to streams\<close>
   275 
   276 definition "stream_all P s = (\<forall>p. P (s !! p))"
   277 
   278 lemma stream_all_iff[iff]: "stream_all P s \<longleftrightarrow> Ball (sset s) P"
   279   unfolding stream_all_def sset_range by auto
   280 
   281 lemma stream_all_shift[simp]: "stream_all P (xs @- s) = (list_all P xs \<and> stream_all P s)"
   282   unfolding stream_all_iff list_all_iff by auto
   283 
   284 lemma stream_all_Stream: "stream_all P (x ## X) \<longleftrightarrow> P x \<and> stream_all P X"
   285   by simp
   286 
   287 
   288 subsection \<open>recurring stream out of a list\<close>
   289 
   290 primcorec cycle :: "'a list \<Rightarrow> 'a stream" where
   291   "shd (cycle xs) = hd xs"
   292 | "stl (cycle xs) = cycle (tl xs @ [hd xs])"
   293 
   294 lemma cycle_decomp: "u \<noteq> [] \<Longrightarrow> cycle u = u @- cycle u"
   295 proof (coinduction arbitrary: u)
   296   case Eq_stream then show ?case using stream.collapse[of "cycle u"]
   297     by (auto intro!: exI[of _ "tl u @ [hd u]"])
   298 qed
   299 
   300 lemma cycle_Cons[code]: "cycle (x # xs) = x ## cycle (xs @ [x])"
   301   by (subst cycle.ctr) simp
   302 
   303 lemma cycle_rotated: "\<lbrakk>v \<noteq> []; cycle u = v @- s\<rbrakk> \<Longrightarrow> cycle (tl u @ [hd u]) = tl v @- s"
   304   by (auto dest: arg_cong[of _ _ stl])
   305 
   306 lemma stake_append: "stake n (u @- s) = take (min (length u) n) u @ stake (n - length u) s"
   307 proof (induct n arbitrary: u)
   308   case (Suc n) thus ?case by (cases u) auto
   309 qed auto
   310 
   311 lemma stake_cycle_le[simp]:
   312   assumes "u \<noteq> []" "n < length u"
   313   shows "stake n (cycle u) = take n u"
   314 using min_absorb2[OF less_imp_le_nat[OF assms(2)]]
   315   by (subst cycle_decomp[OF assms(1)], subst stake_append) auto
   316 
   317 lemma stake_cycle_eq[simp]: "u \<noteq> [] \<Longrightarrow> stake (length u) (cycle u) = u"
   318   by (subst cycle_decomp) (auto simp: stake_shift)
   319 
   320 lemma sdrop_cycle_eq[simp]: "u \<noteq> [] \<Longrightarrow> sdrop (length u) (cycle u) = cycle u"
   321   by (subst cycle_decomp) (auto simp: sdrop_shift)
   322 
   323 lemma stake_cycle_eq_mod_0[simp]: "\<lbrakk>u \<noteq> []; n mod length u = 0\<rbrakk> \<Longrightarrow>
   324    stake n (cycle u) = concat (replicate (n div length u) u)"
   325   by (induct "n div length u" arbitrary: n u) (auto simp: stake_add[symmetric])
   326 
   327 lemma sdrop_cycle_eq_mod_0[simp]: "\<lbrakk>u \<noteq> []; n mod length u = 0\<rbrakk> \<Longrightarrow>
   328    sdrop n (cycle u) = cycle u"
   329   by (induct "n div length u" arbitrary: n u) (auto simp: sdrop_add[symmetric])
   330 
   331 lemma stake_cycle: "u \<noteq> [] \<Longrightarrow>
   332    stake n (cycle u) = concat (replicate (n div length u) u) @ take (n mod length u) u"
   333   by (subst mod_div_equality[of n "length u", symmetric], unfold stake_add[symmetric]) auto
   334 
   335 lemma sdrop_cycle: "u \<noteq> [] \<Longrightarrow> sdrop n (cycle u) = cycle (rotate (n mod length u) u)"
   336   by (induct n arbitrary: u) (auto simp: rotate1_rotate_swap rotate1_hd_tl rotate_conv_mod[symmetric])
   337 
   338 
   339 subsection \<open>iterated application of a function\<close>
   340 
   341 primcorec siterate where
   342   "shd (siterate f x) = x"
   343 | "stl (siterate f x) = siterate f (f x)"
   344 
   345 lemma stake_Suc: "stake (Suc n) s = stake n s @ [s !! n]"
   346   by (induct n arbitrary: s) auto
   347 
   348 lemma snth_siterate[simp]: "siterate f x !! n = (f^^n) x"
   349   by (induct n arbitrary: x) (auto simp: funpow_swap1)
   350 
   351 lemma sdrop_siterate[simp]: "sdrop n (siterate f x) = siterate f ((f^^n) x)"
   352   by (induct n arbitrary: x) (auto simp: funpow_swap1)
   353 
   354 lemma stake_siterate[simp]: "stake n (siterate f x) = map (\<lambda>n. (f^^n) x) [0 ..< n]"
   355   by (induct n arbitrary: x) (auto simp del: stake.simps(2) simp: stake_Suc)
   356 
   357 lemma sset_siterate: "sset (siterate f x) = {(f^^n) x | n. True}"
   358   by (auto simp: sset_range)
   359 
   360 lemma smap_siterate: "smap f (siterate f x) = siterate f (f x)"
   361   by (coinduction arbitrary: x) auto
   362 
   363 
   364 subsection \<open>stream repeating a single element\<close>
   365 
   366 abbreviation "sconst \<equiv> siterate id"
   367 
   368 lemma shift_replicate_sconst[simp]: "replicate n x @- sconst x = sconst x"
   369   by (subst (3) stake_sdrop[symmetric]) (simp add: map_replicate_trivial)
   370 
   371 lemma sset_sconst[simp]: "sset (sconst x) = {x}"
   372   by (simp add: sset_siterate)
   373 
   374 lemma sconst_alt: "s = sconst x \<longleftrightarrow> sset s = {x}"
   375 proof
   376   assume "sset s = {x}"
   377   then show "s = sconst x"
   378   proof (coinduction arbitrary: s)
   379     case Eq_stream
   380     then have "shd s = x" "sset (stl s) \<subseteq> {x}" by (case_tac [!] s) auto
   381     then have "sset (stl s) = {x}" by (cases "stl s") auto
   382     with \<open>shd s = x\<close> show ?case by auto
   383   qed
   384 qed simp
   385 
   386 lemma sconst_cycle: "sconst x = cycle [x]"
   387   by coinduction auto
   388 
   389 lemma smap_sconst: "smap f (sconst x) = sconst (f x)"
   390   by coinduction auto
   391 
   392 lemma sconst_streams: "x \<in> A \<Longrightarrow> sconst x \<in> streams A"
   393   by (simp add: streams_iff_sset)
   394 
   395 
   396 subsection \<open>stream of natural numbers\<close>
   397 
   398 abbreviation "fromN \<equiv> siterate Suc"
   399 
   400 abbreviation "nats \<equiv> fromN 0"
   401 
   402 lemma sset_fromN[simp]: "sset (fromN n) = {n ..}"
   403   by (auto simp add: sset_siterate le_iff_add)
   404 
   405 lemma stream_smap_fromN: "s = smap (\<lambda>j. let i = j - n in s !! i) (fromN n)"
   406   by (coinduction arbitrary: s n)
   407     (force simp: neq_Nil_conv Let_def snth.simps(2)[symmetric] Suc_diff_Suc
   408       intro: stream.map_cong split: if_splits simp del: snth.simps(2))
   409 
   410 lemma stream_smap_nats: "s = smap (snth s) nats"
   411   using stream_smap_fromN[where n = 0] by simp
   412 
   413 
   414 subsection \<open>flatten a stream of lists\<close>
   415 
   416 primcorec flat where
   417   "shd (flat ws) = hd (shd ws)"
   418 | "stl (flat ws) = flat (if tl (shd ws) = [] then stl ws else tl (shd ws) ## stl ws)"
   419 
   420 lemma flat_Cons[simp, code]: "flat ((x # xs) ## ws) = x ## flat (if xs = [] then ws else xs ## ws)"
   421   by (subst flat.ctr) simp
   422 
   423 lemma flat_Stream[simp]: "xs \<noteq> [] \<Longrightarrow> flat (xs ## ws) = xs @- flat ws"
   424   by (induct xs) auto
   425 
   426 lemma flat_unfold: "shd ws \<noteq> [] \<Longrightarrow> flat ws = shd ws @- flat (stl ws)"
   427   by (cases ws) auto
   428 
   429 lemma flat_snth: "\<forall>xs \<in> sset s. xs \<noteq> [] \<Longrightarrow> flat s !! n = (if n < length (shd s) then 
   430   shd s ! n else flat (stl s) !! (n - length (shd s)))"
   431   by (metis flat_unfold not_less shd_sset shift_snth_ge shift_snth_less)
   432 
   433 lemma sset_flat[simp]: "\<forall>xs \<in> sset s. xs \<noteq> [] \<Longrightarrow> 
   434   sset (flat s) = (\<Union>xs \<in> sset s. set xs)" (is "?P \<Longrightarrow> ?L = ?R")
   435 proof safe
   436   fix x assume ?P "x : ?L"
   437   then obtain m where "x = flat s !! m" by (metis image_iff sset_range)
   438   with \<open>?P\<close> obtain n m' where "x = s !! n ! m'" "m' < length (s !! n)"
   439   proof (atomize_elim, induct m arbitrary: s rule: less_induct)
   440     case (less y)
   441     thus ?case
   442     proof (cases "y < length (shd s)")
   443       case True thus ?thesis by (metis flat_snth less(2,3) snth.simps(1))
   444     next
   445       case False
   446       hence "x = flat (stl s) !! (y - length (shd s))" by (metis less(2,3) flat_snth)
   447       moreover
   448       { from less(2) have *: "length (shd s) > 0" by (cases s) simp_all
   449         with False have "y > 0" by (cases y) simp_all
   450         with * have "y - length (shd s) < y" by simp
   451       }
   452       moreover have "\<forall>xs \<in> sset (stl s). xs \<noteq> []" using less(2) by (cases s) auto
   453       ultimately have "\<exists>n m'. x = stl s !! n ! m' \<and> m' < length (stl s !! n)" by (intro less(1)) auto
   454       thus ?thesis by (metis snth.simps(2))
   455     qed
   456   qed
   457   thus "x \<in> ?R" by (auto simp: sset_range dest!: nth_mem)
   458 next
   459   fix x xs assume "xs \<in> sset s" ?P "x \<in> set xs" thus "x \<in> ?L"
   460     by (induct rule: sset_induct)
   461       (metis UnI1 flat_unfold shift.simps(1) sset_shift,
   462        metis UnI2 flat_unfold shd_sset stl_sset sset_shift)
   463 qed
   464 
   465 
   466 subsection \<open>merge a stream of streams\<close>
   467 
   468 definition smerge :: "'a stream stream \<Rightarrow> 'a stream" where
   469   "smerge ss = flat (smap (\<lambda>n. map (\<lambda>s. s !! n) (stake (Suc n) ss) @ stake n (ss !! n)) nats)"
   470 
   471 lemma stake_nth[simp]: "m < n \<Longrightarrow> stake n s ! m = s !! m"
   472   by (induct n arbitrary: s m) (auto simp: nth_Cons', metis Suc_pred snth.simps(2))
   473 
   474 lemma snth_sset_smerge: "ss !! n !! m \<in> sset (smerge ss)"
   475 proof (cases "n \<le> m")
   476   case False thus ?thesis unfolding smerge_def
   477     by (subst sset_flat)
   478       (auto simp: stream.set_map in_set_conv_nth simp del: stake.simps
   479         intro!: exI[of _ n, OF disjI2] exI[of _ m, OF mp])
   480 next
   481   case True thus ?thesis unfolding smerge_def
   482     by (subst sset_flat)
   483       (auto simp: stream.set_map in_set_conv_nth image_iff simp del: stake.simps snth.simps
   484         intro!: exI[of _ m, OF disjI1] bexI[of _ "ss !! n"] exI[of _ n, OF mp])
   485 qed
   486 
   487 lemma sset_smerge: "sset (smerge ss) = UNION (sset ss) sset"
   488 proof safe
   489   fix x assume "x \<in> sset (smerge ss)"
   490   thus "x \<in> UNION (sset ss) sset"
   491     unfolding smerge_def by (subst (asm) sset_flat)
   492       (auto simp: stream.set_map in_set_conv_nth sset_range simp del: stake.simps, fast+)
   493 next
   494   fix s x assume "s \<in> sset ss" "x \<in> sset s"
   495   thus "x \<in> sset (smerge ss)" using snth_sset_smerge by (auto simp: sset_range)
   496 qed
   497 
   498 
   499 subsection \<open>product of two streams\<close>
   500 
   501 definition sproduct :: "'a stream \<Rightarrow> 'b stream \<Rightarrow> ('a \<times> 'b) stream" where
   502   "sproduct s1 s2 = smerge (smap (\<lambda>x. smap (Pair x) s2) s1)"
   503 
   504 lemma sset_sproduct: "sset (sproduct s1 s2) = sset s1 \<times> sset s2"
   505   unfolding sproduct_def sset_smerge by (auto simp: stream.set_map)
   506 
   507 
   508 subsection \<open>interleave two streams\<close>
   509 
   510 primcorec sinterleave where
   511   "shd (sinterleave s1 s2) = shd s1"
   512 | "stl (sinterleave s1 s2) = sinterleave s2 (stl s1)"
   513 
   514 lemma sinterleave_code[code]:
   515   "sinterleave (x ## s1) s2 = x ## sinterleave s2 s1"
   516   by (subst sinterleave.ctr) simp
   517 
   518 lemma sinterleave_snth[simp]:
   519   "even n \<Longrightarrow> sinterleave s1 s2 !! n = s1 !! (n div 2)"
   520   "odd n \<Longrightarrow> sinterleave s1 s2 !! n = s2 !! (n div 2)"
   521   by (induct n arbitrary: s1 s2) simp_all
   522 
   523 lemma sset_sinterleave: "sset (sinterleave s1 s2) = sset s1 \<union> sset s2"
   524 proof (intro equalityI subsetI)
   525   fix x assume "x \<in> sset (sinterleave s1 s2)"
   526   then obtain n where "x = sinterleave s1 s2 !! n" unfolding sset_range by blast
   527   thus "x \<in> sset s1 \<union> sset s2" by (cases "even n") auto
   528 next
   529   fix x assume "x \<in> sset s1 \<union> sset s2"
   530   thus "x \<in> sset (sinterleave s1 s2)"
   531   proof
   532     assume "x \<in> sset s1"
   533     then obtain n where "x = s1 !! n" unfolding sset_range by blast
   534     hence "sinterleave s1 s2 !! (2 * n) = x" by simp
   535     thus ?thesis unfolding sset_range by blast
   536   next
   537     assume "x \<in> sset s2"
   538     then obtain n where "x = s2 !! n" unfolding sset_range by blast
   539     hence "sinterleave s1 s2 !! (2 * n + 1) = x" by simp
   540     thus ?thesis unfolding sset_range by blast
   541   qed
   542 qed
   543 
   544 
   545 subsection \<open>zip\<close>
   546 
   547 primcorec szip where
   548   "shd (szip s1 s2) = (shd s1, shd s2)"
   549 | "stl (szip s1 s2) = szip (stl s1) (stl s2)"
   550 
   551 lemma szip_unfold[code]: "szip (a ## s1) (b ## s2) = (a, b) ## (szip s1 s2)"
   552   by (subst szip.ctr) simp
   553 
   554 lemma snth_szip[simp]: "szip s1 s2 !! n = (s1 !! n, s2 !! n)"
   555   by (induct n arbitrary: s1 s2) auto
   556 
   557 lemma stake_szip[simp]:
   558   "stake n (szip s1 s2) = zip (stake n s1) (stake n s2)"
   559   by (induct n arbitrary: s1 s2) auto
   560 
   561 lemma sdrop_szip[simp]: "sdrop n (szip s1 s2) = szip (sdrop n s1) (sdrop n s2)"
   562   by (induct n arbitrary: s1 s2) auto
   563 
   564 lemma smap_szip_fst:
   565   "smap (\<lambda>x. f (fst x)) (szip s1 s2) = smap f s1"
   566   by (coinduction arbitrary: s1 s2) auto
   567 
   568 lemma smap_szip_snd:
   569   "smap (\<lambda>x. g (snd x)) (szip s1 s2) = smap g s2"
   570   by (coinduction arbitrary: s1 s2) auto
   571 
   572 
   573 subsection \<open>zip via function\<close>
   574 
   575 primcorec smap2 where
   576   "shd (smap2 f s1 s2) = f (shd s1) (shd s2)"
   577 | "stl (smap2 f s1 s2) = smap2 f (stl s1) (stl s2)"
   578 
   579 lemma smap2_unfold[code]:
   580   "smap2 f (a ## s1) (b ## s2) = f a b ## (smap2 f s1 s2)"
   581   by (subst smap2.ctr) simp
   582 
   583 lemma smap2_szip:
   584   "smap2 f s1 s2 = smap (split f) (szip s1 s2)"
   585   by (coinduction arbitrary: s1 s2) auto
   586 
   587 lemma smap_smap2[simp]:
   588   "smap f (smap2 g s1 s2) = smap2 (\<lambda>x y. f (g x y)) s1 s2"
   589   unfolding smap2_szip stream.map_comp o_def split_def ..
   590 
   591 lemma smap2_alt:
   592   "(smap2 f s1 s2 = s) = (\<forall>n. f (s1 !! n) (s2 !! n) = s !! n)"
   593   unfolding smap2_szip smap_alt by auto
   594 
   595 lemma snth_smap2[simp]:
   596   "smap2 f s1 s2 !! n = f (s1 !! n) (s2 !! n)"
   597   by (induct n arbitrary: s1 s2) auto
   598 
   599 lemma stake_smap2[simp]:
   600   "stake n (smap2 f s1 s2) = map (split f) (zip (stake n s1) (stake n s2))"
   601   by (induct n arbitrary: s1 s2) auto
   602 
   603 lemma sdrop_smap2[simp]:
   604   "sdrop n (smap2 f s1 s2) = smap2 f (sdrop n s1) (sdrop n s2)"
   605   by (induct n arbitrary: s1 s2) auto
   606 
   607 end