50518

1 
(* Title: HOL/BNF/Examples/Stream.thy


2 
Author: Dmitriy Traytel, TU Muenchen


3 
Author: Andrei Popescu, TU Muenchen


4 
Copyright 2012


5 


6 
Infinite streams.


7 
*)


8 


9 
header {* Infinite Streams *}


10 


11 
theory Stream


12 
imports "../BNF"


13 
begin


14 


15 
codata 'a stream = Stream (shd: 'a) (stl: "'a stream")


16 


17 
(* TODO: Provide by the package*)


18 
theorem stream_set_induct:


19 
"\<lbrakk>\<And>s. P (shd s) s; \<And>s y. \<lbrakk>y \<in> stream_set (stl s); P y (stl s)\<rbrakk> \<Longrightarrow> P y s\<rbrakk> \<Longrightarrow>


20 
\<forall>y \<in> stream_set s. P y s"


21 
by (rule stream.dtor_set_induct)


22 
(auto simp add: shd_def stl_def stream_case_def fsts_def snds_def split_beta)


23 


24 
theorem shd_stream_set: "shd s \<in> stream_set s"


25 
by (auto simp add: shd_def stl_def stream_case_def fsts_def snds_def split_beta)


26 
(metis UnCI fsts_def insertI1 stream.dtor_set)


27 


28 
theorem stl_stream_set: "y \<in> stream_set (stl s) \<Longrightarrow> y \<in> stream_set s"


29 
by (auto simp add: shd_def stl_def stream_case_def fsts_def snds_def split_beta)


30 
(metis insertI1 set_mp snds_def stream.dtor_set_set_incl)


31 


32 
(* only for the nonmutual case: *)


33 
theorem stream_set_induct1[consumes 1, case_names shd stl, induct set: "stream_set"]:


34 
assumes "y \<in> stream_set s" and "\<And>s. P (shd s) s"


35 
and "\<And>s y. \<lbrakk>y \<in> stream_set (stl s); P y (stl s)\<rbrakk> \<Longrightarrow> P y s"


36 
shows "P y s"


37 
using assms stream_set_induct by blast


38 
(* end TODO *)


39 


40 


41 
subsection {* prepend list to stream *}


42 


43 
primrec shift :: "'a list \<Rightarrow> 'a stream \<Rightarrow> 'a stream" (infixr "@" 65) where


44 
"shift [] s = s"


45 
 "shift (x # xs) s = Stream x (shift xs s)"


46 


47 
lemma shift_append[simp]: "(xs @ ys) @ s = xs @ ys @ s"


48 
by (induct xs) auto


49 


50 
lemma shift_simps[simp]:


51 
"shd (xs @ s) = (if xs = [] then shd s else hd xs)"


52 
"stl (xs @ s) = (if xs = [] then stl s else tl xs @ s)"


53 
by (induct xs) auto


54 


55 


56 
subsection {* recurring stream out of a list *}


57 


58 
definition cycle :: "'a list \<Rightarrow> 'a stream" where


59 
"cycle = stream_unfold hd (\<lambda>xs. tl xs @ [hd xs])"


60 


61 
lemma cycle_simps[simp]:


62 
"shd (cycle u) = hd u"


63 
"stl (cycle u) = cycle (tl u @ [hd u])"


64 
by (auto simp: cycle_def)


65 


66 


67 
lemma cycle_decomp: "u \<noteq> [] \<Longrightarrow> cycle u = u @ cycle u"


68 
proof (coinduct rule: stream.coinduct[of "\<lambda>s1 s2. \<exists>u. s1 = cycle u \<and> s2 = u @ cycle u \<and> u \<noteq> []"])


69 
case (2 s1 s2)


70 
then obtain u where "s1 = cycle u \<and> s2 = u @ cycle u \<and> u \<noteq> []" by blast


71 
thus ?case using stream.unfold[of hd "\<lambda>xs. tl xs @ [hd xs]" u] by (auto simp: cycle_def)


72 
qed auto


73 


74 
lemma cycle_Cons: "cycle (x # xs) = Stream x (cycle (xs @ [x]))"


75 
proof (coinduct rule: stream.coinduct[of "\<lambda>s1 s2. \<exists>x xs. s1 = cycle (x # xs) \<and> s2 = Stream x (cycle (xs @ [x]))"])


76 
case (2 s1 s2)


77 
then obtain x xs where "s1 = cycle (x # xs) \<and> s2 = Stream x (cycle (xs @ [x]))" by blast


78 
thus ?case


79 
by (auto simp: cycle_def intro!: exI[of _ "hd (xs @ [x])"] exI[of _ "tl (xs @ [x])"] stream.unfold)


80 
qed auto


81 


82 
coinductive_set


83 
streams :: "'a set => 'a stream set"


84 
for A :: "'a set"


85 
where


86 
Stream[intro!, simp, no_atp]: "\<lbrakk>a \<in> A; s \<in> streams A\<rbrakk> \<Longrightarrow> Stream a s \<in> streams A"


87 


88 
lemma shift_streams: "\<lbrakk>w \<in> lists A; s \<in> streams A\<rbrakk> \<Longrightarrow> w @ s \<in> streams A"


89 
by (induct w) auto


90 


91 
lemma stream_set_streams:


92 
assumes "stream_set s \<subseteq> A"


93 
shows "s \<in> streams A"


94 
proof (coinduct rule: streams.coinduct[of "\<lambda>s'. \<exists>a s. s' = Stream a s \<and> a \<in> A \<and> stream_set s \<subseteq> A"])


95 
case streams from assms show ?case by (cases s) auto


96 
next


97 
fix s' assume "\<exists>a s. s' = Stream a s \<and> a \<in> A \<and> stream_set s \<subseteq> A"


98 
then guess a s by (elim exE)


99 
with assms show "\<exists>a l. s' = Stream a l \<and>


100 
a \<in> A \<and> ((\<exists>a s. l = Stream a s \<and> a \<in> A \<and> stream_set s \<subseteq> A) \<or> l \<in> streams A)"


101 
by (cases s) auto


102 
qed


103 


104 


105 
subsection {* flatten a stream of lists *}


106 


107 
definition flat where


108 
"flat \<equiv> stream_unfold (hd o shd) (\<lambda>s. if tl (shd s) = [] then stl s else Stream (tl (shd s)) (stl s))"


109 


110 
lemma flat_simps[simp]:


111 
"shd (flat ws) = hd (shd ws)"


112 
"stl (flat ws) = flat (if tl (shd ws) = [] then stl ws else Stream (tl (shd ws)) (stl ws))"


113 
unfolding flat_def by auto


114 


115 
lemma flat_Cons[simp]: "flat (Stream (x#xs) w) = Stream x (flat (if xs = [] then w else Stream xs w))"


116 
unfolding flat_def using stream.unfold[of "hd o shd" _ "Stream (x#xs) w"] by auto


117 


118 
lemma flat_Stream[simp]: "xs \<noteq> [] \<Longrightarrow> flat (Stream xs ws) = xs @ flat ws"


119 
by (induct xs) auto


120 


121 
lemma flat_unfold: "shd ws \<noteq> [] \<Longrightarrow> flat ws = shd ws @ flat (stl ws)"


122 
by (cases ws) auto


123 


124 


125 
subsection {* take, drop, nth for streams *}


126 


127 
primrec stake :: "nat \<Rightarrow> 'a stream \<Rightarrow> 'a list" where


128 
"stake 0 s = []"


129 
 "stake (Suc n) s = shd s # stake n (stl s)"


130 


131 
primrec sdrop :: "nat \<Rightarrow> 'a stream \<Rightarrow> 'a stream" where


132 
"sdrop 0 s = s"


133 
 "sdrop (Suc n) s = sdrop n (stl s)"


134 


135 
primrec snth :: "nat \<Rightarrow> 'a stream \<Rightarrow> 'a" where


136 
"snth 0 s = shd s"


137 
 "snth (Suc n) s = snth n (stl s)"


138 


139 
lemma stake_sdrop: "stake n s @ sdrop n s = s"


140 
by (induct n arbitrary: s) auto


141 


142 
lemma stake_empty: "stake n s = [] \<longleftrightarrow> n = 0"


143 
by (cases n) auto


144 


145 
lemma sdrop_shift: "\<lbrakk>s = w @ s'; length w = n\<rbrakk> \<Longrightarrow> sdrop n s = s'"


146 
by (induct n arbitrary: w s) auto


147 


148 
lemma stake_shift: "\<lbrakk>s = w @ s'; length w = n\<rbrakk> \<Longrightarrow> stake n s = w"


149 
by (induct n arbitrary: w s) auto


150 


151 
lemma stake_add[simp]: "stake m s @ stake n (sdrop m s) = stake (m + n) s"


152 
by (induct m arbitrary: s) auto


153 


154 
lemma sdrop_add[simp]: "sdrop n (sdrop m s) = sdrop (m + n) s"


155 
by (induct m arbitrary: s) auto


156 


157 
lemma cycle_rotated: "\<lbrakk>v \<noteq> []; cycle u = v @ s\<rbrakk> \<Longrightarrow> cycle (tl u @ [hd u]) = tl v @ s"


158 
by (auto dest: arg_cong[of _ _ stl])


159 


160 
lemma stake_append: "stake n (u @ s) = take (min (length u) n) u @ stake (n  length u) s"


161 
proof (induct n arbitrary: u)


162 
case (Suc n) thus ?case by (cases u) auto


163 
qed auto


164 


165 
lemma stake_cycle_le[simp]:


166 
assumes "u \<noteq> []" "n < length u"


167 
shows "stake n (cycle u) = take n u"


168 
using min_absorb2[OF less_imp_le_nat[OF assms(2)]]


169 
by (subst cycle_decomp[OF assms(1)], subst stake_append) auto


170 


171 
lemma stake_cycle_eq[simp]: "u \<noteq> [] \<Longrightarrow> stake (length u) (cycle u) = u"


172 
by (metis cycle_decomp stake_shift)


173 


174 
lemma sdrop_cycle_eq[simp]: "u \<noteq> [] \<Longrightarrow> sdrop (length u) (cycle u) = cycle u"


175 
by (metis cycle_decomp sdrop_shift)


176 


177 
lemma stake_cycle_eq_mod_0[simp]: "\<lbrakk>u \<noteq> []; n mod length u = 0\<rbrakk> \<Longrightarrow>


178 
stake n (cycle u) = concat (replicate (n div length u) u)"


179 
by (induct "n div length u" arbitrary: n u) (auto simp: stake_add[symmetric])


180 


181 
lemma sdrop_cycle_eq_mod_0[simp]: "\<lbrakk>u \<noteq> []; n mod length u = 0\<rbrakk> \<Longrightarrow>


182 
sdrop n (cycle u) = cycle u"


183 
by (induct "n div length u" arbitrary: n u) (auto simp: sdrop_add[symmetric])


184 


185 
lemma stake_cycle: "u \<noteq> [] \<Longrightarrow>


186 
stake n (cycle u) = concat (replicate (n div length u) u) @ take (n mod length u) u"


187 
by (subst mod_div_equality[of n "length u", symmetric], unfold stake_add[symmetric]) auto


188 


189 
lemma sdrop_cycle: "u \<noteq> [] \<Longrightarrow> sdrop n (cycle u) = cycle (rotate (n mod length u) u)"


190 
by (induct n arbitrary: u) (auto simp: rotate1_rotate_swap rotate1_hd_tl rotate_conv_mod[symmetric])


191 


192 
end
