merge
authorblanchet
Fri, 15 Feb 2013 11:36:34 +0100
changeset 5228743502299c935
parent 52286 4f0147ed8bcb
parent 52278 cc7423ce6774
child 52288 65b7ccb1d96a
merge
     1.1 --- a/src/HOL/BNF/Examples/Stream.thy	Fri Feb 15 11:27:15 2013 +0100
     1.2 +++ b/src/HOL/BNF/Examples/Stream.thy	Fri Feb 15 11:36:34 2013 +0100
     1.3 @@ -16,25 +16,33 @@
     1.4  
     1.5  (* TODO: Provide by the package*)
     1.6  theorem stream_set_induct:
     1.7 -   "\<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>
     1.8 -   \<forall>y \<in> stream_set s. P y s"
     1.9 -by (rule stream.dtor_set_induct)
    1.10 -   (auto simp add:  shd_def stl_def stream_case_def fsts_def snds_def split_beta)
    1.11 +  "\<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>
    1.12 +    \<forall>y \<in> stream_set s. P y s"
    1.13 +  by (rule stream.dtor_set_induct)
    1.14 +    (auto simp add:  shd_def stl_def stream_case_def fsts_def snds_def split_beta)
    1.15 +
    1.16 +lemma stream_map_simps[simp]:
    1.17 +  "shd (stream_map f s) = f (shd s)" "stl (stream_map f s) = stream_map f (stl s)"
    1.18 +  unfolding shd_def stl_def stream_case_def stream_map_def stream.dtor_unfold
    1.19 +  by (case_tac [!] s) (auto simp: Stream_def stream.dtor_ctor)
    1.20 +
    1.21 +lemma stream_map_Stream[simp]: "stream_map f (x ## s) = f x ## stream_map f s"
    1.22 +  by (metis stream.exhaust stream.sels stream_map_simps)
    1.23  
    1.24  theorem shd_stream_set: "shd s \<in> stream_set s"
    1.25 -by (auto simp add: shd_def stl_def stream_case_def fsts_def snds_def split_beta)
    1.26 -   (metis UnCI fsts_def insertI1 stream.dtor_set)
    1.27 +  by (auto simp add: shd_def stl_def stream_case_def fsts_def snds_def split_beta)
    1.28 +    (metis UnCI fsts_def insertI1 stream.dtor_set)
    1.29  
    1.30  theorem stl_stream_set: "y \<in> stream_set (stl s) \<Longrightarrow> y \<in> stream_set s"
    1.31 -by (auto simp add: shd_def stl_def stream_case_def fsts_def snds_def split_beta)
    1.32 -   (metis insertI1 set_mp snds_def stream.dtor_set_set_incl)
    1.33 +  by (auto simp add: shd_def stl_def stream_case_def fsts_def snds_def split_beta)
    1.34 +    (metis insertI1 set_mp snds_def stream.dtor_set_set_incl)
    1.35  
    1.36  (* only for the non-mutual case: *)
    1.37  theorem stream_set_induct1[consumes 1, case_names shd stl, induct set: "stream_set"]:
    1.38    assumes "y \<in> stream_set s" and "\<And>s. P (shd s) s"
    1.39    and "\<And>s y. \<lbrakk>y \<in> stream_set (stl s); P y (stl s)\<rbrakk> \<Longrightarrow> P y s"
    1.40    shows "P y s"
    1.41 -using assms stream_set_induct by blast
    1.42 +  using assms stream_set_induct by blast
    1.43  (* end TODO *)
    1.44  
    1.45  
    1.46 @@ -45,12 +53,152 @@
    1.47  | "shift (x # xs) s = x ## shift xs s"
    1.48  
    1.49  lemma shift_append[simp]: "(xs @ ys) @- s = xs @- ys @- s"
    1.50 -by (induct xs) auto
    1.51 +  by (induct xs) auto
    1.52  
    1.53  lemma shift_simps[simp]:
    1.54     "shd (xs @- s) = (if xs = [] then shd s else hd xs)"
    1.55     "stl (xs @- s) = (if xs = [] then stl s else tl xs @- s)"
    1.56 -by (induct xs) auto
    1.57 +  by (induct xs) auto
    1.58 +
    1.59 +lemma stream_set_shift[simp]: "stream_set (xs @- s) = set xs \<union> stream_set s"
    1.60 +  by (induct xs) auto
    1.61 +
    1.62 +
    1.63 +subsection {* set of streams with elements in some fixes set *}
    1.64 +
    1.65 +coinductive_set
    1.66 +  streams :: "'a set => 'a stream set"
    1.67 +  for A :: "'a set"
    1.68 +where
    1.69 +  Stream[intro!, simp, no_atp]: "\<lbrakk>a \<in> A; s \<in> streams A\<rbrakk> \<Longrightarrow> a ## s \<in> streams A"
    1.70 +
    1.71 +lemma shift_streams: "\<lbrakk>w \<in> lists A; s \<in> streams A\<rbrakk> \<Longrightarrow> w @- s \<in> streams A"
    1.72 +  by (induct w) auto
    1.73 +
    1.74 +lemma stream_set_streams:
    1.75 +  assumes "stream_set s \<subseteq> A"
    1.76 +  shows "s \<in> streams A"
    1.77 +proof (coinduct rule: streams.coinduct[of "\<lambda>s'. \<exists>a s. s' = a ## s \<and> a \<in> A \<and> stream_set s \<subseteq> A"])
    1.78 +  case streams from assms show ?case by (cases s) auto
    1.79 +next
    1.80 +  fix s' assume "\<exists>a s. s' = a ## s \<and> a \<in> A \<and> stream_set s \<subseteq> A"
    1.81 +  then guess a s by (elim exE)
    1.82 +  with assms show "\<exists>a l. s' = a ## l \<and>
    1.83 +    a \<in> A \<and> ((\<exists>a s. l = a ## s \<and> a \<in> A \<and> stream_set s \<subseteq> A) \<or> l \<in> streams A)"
    1.84 +    by (cases s) auto
    1.85 +qed
    1.86 +
    1.87 +
    1.88 +subsection {* flatten a stream of lists *}
    1.89 +
    1.90 +definition flat where
    1.91 +  "flat \<equiv> stream_unfold (hd o shd) (\<lambda>s. if tl (shd s) = [] then stl s else tl (shd s) ## stl s)"
    1.92 +
    1.93 +lemma flat_simps[simp]:
    1.94 +  "shd (flat ws) = hd (shd ws)"
    1.95 +  "stl (flat ws) = flat (if tl (shd ws) = [] then stl ws else tl (shd ws) ## stl ws)"
    1.96 +  unfolding flat_def by auto
    1.97 +
    1.98 +lemma flat_Cons[simp]: "flat ((x # xs) ## ws) = x ## flat (if xs = [] then ws else xs ## ws)"
    1.99 +  unfolding flat_def using stream.unfold[of "hd o shd" _ "(x # xs) ## ws"] by auto
   1.100 +
   1.101 +lemma flat_Stream[simp]: "xs \<noteq> [] \<Longrightarrow> flat (xs ## ws) = xs @- flat ws"
   1.102 +  by (induct xs) auto
   1.103 +
   1.104 +lemma flat_unfold: "shd ws \<noteq> [] \<Longrightarrow> flat ws = shd ws @- flat (stl ws)"
   1.105 +  by (cases ws) auto
   1.106 +
   1.107 +
   1.108 +subsection {* nth, take, drop for streams *}
   1.109 +
   1.110 +primrec snth :: "'a stream \<Rightarrow> nat \<Rightarrow> 'a" (infixl "!!" 100) where
   1.111 +  "s !! 0 = shd s"
   1.112 +| "s !! Suc n = stl s !! n"
   1.113 +
   1.114 +lemma snth_stream_map[simp]: "stream_map f s !! n = f (s !! n)"
   1.115 +  by (induct n arbitrary: s) auto
   1.116 +
   1.117 +lemma shift_snth_less[simp]: "p < length xs \<Longrightarrow> (xs @- s) !! p = xs ! p"
   1.118 +  by (induct p arbitrary: xs) (auto simp: hd_conv_nth nth_tl)
   1.119 +
   1.120 +lemma shift_snth_ge[simp]: "p \<ge> length xs \<Longrightarrow> (xs @- s) !! p = s !! (p - length xs)"
   1.121 +  by (induct p arbitrary: xs) (auto simp: Suc_diff_eq_diff_pred)
   1.122 +
   1.123 +lemma snth_stream_set[simp]: "s !! n \<in> stream_set s"
   1.124 +  by (induct n arbitrary: s) (auto intro: shd_stream_set stl_stream_set)
   1.125 +
   1.126 +lemma stream_set_range: "stream_set s = range (snth s)"
   1.127 +proof (intro equalityI subsetI)
   1.128 +  fix x assume "x \<in> stream_set s"
   1.129 +  thus "x \<in> range (snth s)"
   1.130 +  proof (induct s)
   1.131 +    case (stl s x)
   1.132 +    then obtain n where "x = stl s !! n" by auto
   1.133 +    thus ?case by (auto intro: range_eqI[of _ _ "Suc n"])
   1.134 +  qed (auto intro: range_eqI[of _ _ 0])
   1.135 +qed auto
   1.136 +
   1.137 +primrec stake :: "nat \<Rightarrow> 'a stream \<Rightarrow> 'a list" where
   1.138 +  "stake 0 s = []"
   1.139 +| "stake (Suc n) s = shd s # stake n (stl s)"
   1.140 +
   1.141 +lemma length_stake[simp]: "length (stake n s) = n"
   1.142 +  by (induct n arbitrary: s) auto
   1.143 +
   1.144 +lemma stake_stream_map[simp]: "stake n (stream_map f s) = map f (stake n s)"
   1.145 +  by (induct n arbitrary: s) auto
   1.146 +
   1.147 +primrec sdrop :: "nat \<Rightarrow> 'a stream \<Rightarrow> 'a stream" where
   1.148 +  "sdrop 0 s = s"
   1.149 +| "sdrop (Suc n) s = sdrop n (stl s)"
   1.150 +
   1.151 +lemma sdrop_simps[simp]:
   1.152 +  "shd (sdrop n s) = s !! n" "stl (sdrop n s) = sdrop (Suc n) s"
   1.153 +  by (induct n arbitrary: s)  auto
   1.154 +
   1.155 +lemma sdrop_stream_map[simp]: "sdrop n (stream_map f s) = stream_map f (sdrop n s)"
   1.156 +  by (induct n arbitrary: s) auto
   1.157 +
   1.158 +lemma stake_sdrop: "stake n s @- sdrop n s = s"
   1.159 +  by (induct n arbitrary: s) auto
   1.160 +
   1.161 +lemma id_stake_snth_sdrop:
   1.162 +  "s = stake i s @- s !! i ## sdrop (Suc i) s"
   1.163 +  by (subst stake_sdrop[symmetric, of _ i]) (metis sdrop_simps stream.collapse)
   1.164 +
   1.165 +lemma stream_map_alt: "stream_map f s = s' \<longleftrightarrow> (\<forall>n. f (s !! n) = s' !! n)" (is "?L = ?R")
   1.166 +proof
   1.167 +  assume ?R
   1.168 +  thus ?L 
   1.169 +    by (coinduct rule: stream.coinduct[of "\<lambda>s1 s2. \<exists>n. s1 = stream_map f (sdrop n s) \<and> s2 = sdrop n s'"])
   1.170 +      (auto intro: exI[of _ 0] simp del: sdrop.simps(2))
   1.171 +qed auto
   1.172 +
   1.173 +lemma stake_invert_Nil[iff]: "stake n s = [] \<longleftrightarrow> n = 0"
   1.174 +  by (induct n) auto
   1.175 +
   1.176 +lemma sdrop_shift: "\<lbrakk>s = w @- s'; length w = n\<rbrakk> \<Longrightarrow> sdrop n s = s'"
   1.177 +  by (induct n arbitrary: w s) auto
   1.178 +
   1.179 +lemma stake_shift: "\<lbrakk>s = w @- s'; length w = n\<rbrakk> \<Longrightarrow> stake n s = w"
   1.180 +  by (induct n arbitrary: w s) auto
   1.181 +
   1.182 +lemma stake_add[simp]: "stake m s @ stake n (sdrop m s) = stake (m + n) s"
   1.183 +  by (induct m arbitrary: s) auto
   1.184 +
   1.185 +lemma sdrop_add[simp]: "sdrop n (sdrop m s) = sdrop (m + n) s"
   1.186 +  by (induct m arbitrary: s) auto
   1.187 +
   1.188 +
   1.189 +subsection {* unary predicates lifted to streams *}
   1.190 +
   1.191 +definition "stream_all P s = (\<forall>p. P (s !! p))"
   1.192 +
   1.193 +lemma stream_all_iff[iff]: "stream_all P s \<longleftrightarrow> Ball (stream_set s) P"
   1.194 +  unfolding stream_all_def stream_set_range by auto
   1.195 +
   1.196 +lemma stream_all_shift[simp]: "stream_all P (xs @- s) = (list_all P xs \<and> stream_all P s)"
   1.197 +  unfolding stream_all_iff list_all_iff by auto
   1.198  
   1.199  
   1.200  subsection {* recurring stream out of a list *}
   1.201 @@ -61,8 +209,7 @@
   1.202  lemma cycle_simps[simp]:
   1.203    "shd (cycle u) = hd u"
   1.204    "stl (cycle u) = cycle (tl u @ [hd u])"
   1.205 -by (auto simp: cycle_def)
   1.206 -
   1.207 +  by (auto simp: cycle_def)
   1.208  
   1.209  lemma cycle_decomp: "u \<noteq> [] \<Longrightarrow> cycle u = u @- cycle u"
   1.210  proof (coinduct rule: stream.coinduct[of "\<lambda>s1 s2. \<exists>u. s1 = cycle u \<and> s2 = u @- cycle u \<and> u \<noteq> []"])
   1.211 @@ -79,83 +226,8 @@
   1.212      by (auto simp: cycle_def intro!: exI[of _ "hd (xs @ [x])"] exI[of _ "tl (xs @ [x])"] stream.unfold)
   1.213  qed auto
   1.214  
   1.215 -coinductive_set
   1.216 -  streams :: "'a set => 'a stream set"
   1.217 -  for A :: "'a set"
   1.218 -where
   1.219 -  Stream[intro!, simp, no_atp]: "\<lbrakk>a \<in> A; s \<in> streams A\<rbrakk> \<Longrightarrow> a ## s \<in> streams A"
   1.220 -
   1.221 -lemma shift_streams: "\<lbrakk>w \<in> lists A; s \<in> streams A\<rbrakk> \<Longrightarrow> w @- s \<in> streams A"
   1.222 -by (induct w) auto
   1.223 -
   1.224 -lemma stream_set_streams:
   1.225 -  assumes "stream_set s \<subseteq> A"
   1.226 -  shows "s \<in> streams A"
   1.227 -proof (coinduct rule: streams.coinduct[of "\<lambda>s'. \<exists>a s. s' = a ## s \<and> a \<in> A \<and> stream_set s \<subseteq> A"])
   1.228 -  case streams from assms show ?case by (cases s) auto
   1.229 -next
   1.230 -  fix s' assume "\<exists>a s. s' = a ## s \<and> a \<in> A \<and> stream_set s \<subseteq> A"
   1.231 -  then guess a s by (elim exE)
   1.232 -  with assms show "\<exists>a l. s' = a ## l \<and>
   1.233 -    a \<in> A \<and> ((\<exists>a s. l = a ## s \<and> a \<in> A \<and> stream_set s \<subseteq> A) \<or> l \<in> streams A)"
   1.234 -    by (cases s) auto
   1.235 -qed
   1.236 -
   1.237 -
   1.238 -subsection {* flatten a stream of lists *}
   1.239 -
   1.240 -definition flat where
   1.241 -  "flat \<equiv> stream_unfold (hd o shd) (\<lambda>s. if tl (shd s) = [] then stl s else tl (shd s) ## stl s)"
   1.242 -
   1.243 -lemma flat_simps[simp]:
   1.244 -  "shd (flat ws) = hd (shd ws)"
   1.245 -  "stl (flat ws) = flat (if tl (shd ws) = [] then stl ws else tl (shd ws) ## stl ws)"
   1.246 -unfolding flat_def by auto
   1.247 -
   1.248 -lemma flat_Cons[simp]: "flat ((x # xs) ## ws) = x ## flat (if xs = [] then ws else xs ## ws)"
   1.249 -unfolding flat_def using stream.unfold[of "hd o shd" _ "(x # xs) ## ws"] by auto
   1.250 -
   1.251 -lemma flat_Stream[simp]: "xs \<noteq> [] \<Longrightarrow> flat (xs ## ws) = xs @- flat ws"
   1.252 -by (induct xs) auto
   1.253 -
   1.254 -lemma flat_unfold: "shd ws \<noteq> [] \<Longrightarrow> flat ws = shd ws @- flat (stl ws)"
   1.255 -by (cases ws) auto
   1.256 -
   1.257 -
   1.258 -subsection {* take, drop, nth for streams *}
   1.259 -
   1.260 -primrec stake :: "nat \<Rightarrow> 'a stream \<Rightarrow> 'a list" where
   1.261 -  "stake 0 s = []"
   1.262 -| "stake (Suc n) s = shd s # stake n (stl s)"
   1.263 -
   1.264 -primrec sdrop :: "nat \<Rightarrow> 'a stream \<Rightarrow> 'a stream" where
   1.265 -  "sdrop 0 s = s"
   1.266 -| "sdrop (Suc n) s = sdrop n (stl s)"
   1.267 -
   1.268 -primrec snth :: "'a stream \<Rightarrow> nat \<Rightarrow> 'a" (infixl "!!" 100) where
   1.269 -  "s !! 0 = shd s"
   1.270 -| "s !! Suc n = stl s !! n"
   1.271 -
   1.272 -lemma stake_sdrop: "stake n s @- sdrop n s = s"
   1.273 -by (induct n arbitrary: s) auto
   1.274 -
   1.275 -lemma stake_empty: "stake n s = [] \<longleftrightarrow> n = 0"
   1.276 -by (cases n) auto
   1.277 -
   1.278 -lemma sdrop_shift: "\<lbrakk>s = w @- s'; length w = n\<rbrakk> \<Longrightarrow> sdrop n s = s'"
   1.279 -by (induct n arbitrary: w s) auto
   1.280 -
   1.281 -lemma stake_shift: "\<lbrakk>s = w @- s'; length w = n\<rbrakk> \<Longrightarrow> stake n s = w"
   1.282 -by (induct n arbitrary: w s) auto
   1.283 -
   1.284 -lemma stake_add[simp]: "stake m s @ stake n (sdrop m s) = stake (m + n) s"
   1.285 -by (induct m arbitrary: s) auto
   1.286 -
   1.287 -lemma sdrop_add[simp]: "sdrop n (sdrop m s) = sdrop (m + n) s"
   1.288 -by (induct m arbitrary: s) auto
   1.289 -
   1.290  lemma cycle_rotated: "\<lbrakk>v \<noteq> []; cycle u = v @- s\<rbrakk> \<Longrightarrow> cycle (tl u @ [hd u]) = tl v @- s"
   1.291 -by (auto dest: arg_cong[of _ _ stl])
   1.292 +  by (auto dest: arg_cong[of _ _ stl])
   1.293  
   1.294  lemma stake_append: "stake n (u @- s) = take (min (length u) n) u @ stake (n - length u) s"
   1.295  proof (induct n arbitrary: u)
   1.296 @@ -166,27 +238,105 @@
   1.297    assumes "u \<noteq> []" "n < length u"
   1.298    shows "stake n (cycle u) = take n u"
   1.299  using min_absorb2[OF less_imp_le_nat[OF assms(2)]]
   1.300 -by (subst cycle_decomp[OF assms(1)], subst stake_append) auto
   1.301 +  by (subst cycle_decomp[OF assms(1)], subst stake_append) auto
   1.302  
   1.303  lemma stake_cycle_eq[simp]: "u \<noteq> [] \<Longrightarrow> stake (length u) (cycle u) = u"
   1.304 -by (metis cycle_decomp stake_shift)
   1.305 +  by (metis cycle_decomp stake_shift)
   1.306  
   1.307  lemma sdrop_cycle_eq[simp]: "u \<noteq> [] \<Longrightarrow> sdrop (length u) (cycle u) = cycle u"
   1.308 -by (metis cycle_decomp sdrop_shift)
   1.309 +  by (metis cycle_decomp sdrop_shift)
   1.310  
   1.311  lemma stake_cycle_eq_mod_0[simp]: "\<lbrakk>u \<noteq> []; n mod length u = 0\<rbrakk> \<Longrightarrow>
   1.312     stake n (cycle u) = concat (replicate (n div length u) u)"
   1.313 -by (induct "n div length u" arbitrary: n u) (auto simp: stake_add[symmetric])
   1.314 +  by (induct "n div length u" arbitrary: n u) (auto simp: stake_add[symmetric])
   1.315  
   1.316  lemma sdrop_cycle_eq_mod_0[simp]: "\<lbrakk>u \<noteq> []; n mod length u = 0\<rbrakk> \<Longrightarrow>
   1.317     sdrop n (cycle u) = cycle u"
   1.318 -by (induct "n div length u" arbitrary: n u) (auto simp: sdrop_add[symmetric])
   1.319 +  by (induct "n div length u" arbitrary: n u) (auto simp: sdrop_add[symmetric])
   1.320  
   1.321  lemma stake_cycle: "u \<noteq> [] \<Longrightarrow>
   1.322     stake n (cycle u) = concat (replicate (n div length u) u) @ take (n mod length u) u"
   1.323 -by (subst mod_div_equality[of n "length u", symmetric], unfold stake_add[symmetric]) auto
   1.324 +  by (subst mod_div_equality[of n "length u", symmetric], unfold stake_add[symmetric]) auto
   1.325  
   1.326  lemma sdrop_cycle: "u \<noteq> [] \<Longrightarrow> sdrop n (cycle u) = cycle (rotate (n mod length u) u)"
   1.327 -by (induct n arbitrary: u) (auto simp: rotate1_rotate_swap rotate1_hd_tl rotate_conv_mod[symmetric])
   1.328 +  by (induct n arbitrary: u) (auto simp: rotate1_rotate_swap rotate1_hd_tl rotate_conv_mod[symmetric])
   1.329 +
   1.330 +
   1.331 +subsection {* stream repeating a single element *}
   1.332 +
   1.333 +definition "same x = stream_unfold (\<lambda>_. x) id ()"
   1.334 +
   1.335 +lemma same_simps[simp]: "shd (same x) = x" "stl (same x) = same x"
   1.336 +  unfolding same_def by auto
   1.337 +
   1.338 +lemma same_unfold: "same x = Stream x (same x)"
   1.339 +  by (metis same_simps stream.collapse)
   1.340 +
   1.341 +lemma snth_same[simp]: "same x !! n = x"
   1.342 +  unfolding same_def by (induct n) auto
   1.343 +
   1.344 +lemma stake_same[simp]: "stake n (same x) = replicate n x"
   1.345 +  unfolding same_def by (induct n) (auto simp: upt_rec)
   1.346 +
   1.347 +lemma sdrop_same[simp]: "sdrop n (same x) = same x"
   1.348 +  unfolding same_def by (induct n) auto
   1.349 +
   1.350 +lemma shift_replicate_same[simp]: "replicate n x @- same x = same x"
   1.351 +  by (metis sdrop_same stake_same stake_sdrop)
   1.352 +
   1.353 +lemma stream_all_same[simp]: "stream_all P (same x) \<longleftrightarrow> P x"
   1.354 +  unfolding stream_all_def by auto
   1.355 +
   1.356 +lemma same_cycle: "same x = cycle [x]"
   1.357 +  by (coinduct rule: stream.coinduct[of "\<lambda>s1 s2. s1 = same x \<and> s2 = cycle [x]"]) auto
   1.358 +
   1.359 +
   1.360 +subsection {* stream of natural numbers *}
   1.361 +
   1.362 +definition "fromN n = stream_unfold id Suc n"
   1.363 +
   1.364 +lemma fromN_simps[simp]: "shd (fromN n) = n" "stl (fromN n) = fromN (Suc n)"
   1.365 +  unfolding fromN_def by auto
   1.366 +
   1.367 +lemma snth_fromN[simp]: "fromN n !! m = n + m"
   1.368 +  unfolding fromN_def by (induct m arbitrary: n) auto
   1.369 +
   1.370 +lemma stake_fromN[simp]: "stake m (fromN n) = [n ..< m + n]"
   1.371 +  unfolding fromN_def by (induct m arbitrary: n) (auto simp: upt_rec)
   1.372 +
   1.373 +lemma sdrop_fromN[simp]: "sdrop m (fromN n) = fromN (n + m)"
   1.374 +  unfolding fromN_def by (induct m arbitrary: n) auto
   1.375 +
   1.376 +abbreviation "nats \<equiv> fromN 0"
   1.377 +
   1.378 +
   1.379 +subsection {* zip *}
   1.380 +
   1.381 +definition "szip s1 s2 =
   1.382 +  stream_unfold (map_pair shd shd) (map_pair stl stl) (s1, s2)"
   1.383 +
   1.384 +lemma szip_simps[simp]:
   1.385 +  "shd (szip s1 s2) = (shd s1, shd s2)" "stl (szip s1 s2) = szip (stl s1) (stl s2)"
   1.386 +  unfolding szip_def by auto
   1.387 +
   1.388 +lemma snth_szip[simp]: "szip s1 s2 !! n = (s1 !! n, s2 !! n)"
   1.389 +  by (induct n arbitrary: s1 s2) auto
   1.390 +
   1.391 +
   1.392 +subsection {* zip via function *}
   1.393 +
   1.394 +definition "stream_map2 f s1 s2 =
   1.395 +  stream_unfold (\<lambda>(s1,s2). f (shd s1) (shd s2)) (map_pair stl stl) (s1, s2)"
   1.396 +
   1.397 +lemma stream_map2_simps[simp]:
   1.398 + "shd (stream_map2 f s1 s2) = f (shd s1) (shd s2)"
   1.399 + "stl (stream_map2 f s1 s2) = stream_map2 f (stl s1) (stl s2)"
   1.400 +  unfolding stream_map2_def by auto
   1.401 +
   1.402 +lemma stream_map2_szip:
   1.403 +  "stream_map2 f s1 s2 = stream_map (split f) (szip s1 s2)"
   1.404 +  by (coinduct rule: stream.coinduct[of
   1.405 +    "\<lambda>s1 s2. \<exists>s1' s2'. s1 = stream_map2 f s1' s2' \<and> s2 = stream_map (split f) (szip s1' s2')"])
   1.406 +    fastforce+
   1.407  
   1.408  end
     2.1 --- a/src/HOL/Codegenerator_Test/RBT_Set_Test.thy	Fri Feb 15 11:27:15 2013 +0100
     2.2 +++ b/src/HOL/Codegenerator_Test/RBT_Set_Test.thy	Fri Feb 15 11:36:34 2013 +0100
     2.3 @@ -25,9 +25,17 @@
     2.4  lemma [code, code del]:
     2.5    "acc = acc" ..
     2.6  
     2.7 -lemmas [code del] =
     2.8 -  finite_set_code finite_coset_code 
     2.9 -  equal_set_code
    2.10 +lemma [code, code del]:
    2.11 +  "Cardinality.card' = Cardinality.card'" ..
    2.12 +
    2.13 +lemma [code, code del]:
    2.14 +  "Cardinality.finite' = Cardinality.finite'" ..
    2.15 +
    2.16 +lemma [code, code del]:
    2.17 +  "Cardinality.subset' = Cardinality.subset'" ..
    2.18 +
    2.19 +lemma [code, code del]:
    2.20 +  "Cardinality.eq_set = Cardinality.eq_set" ..
    2.21  
    2.22  (*
    2.23    If the code generation ends with an exception with the following message:
     3.1 --- a/src/HOL/Library/Cardinality.thy	Fri Feb 15 11:27:15 2013 +0100
     3.2 +++ b/src/HOL/Library/Cardinality.thy	Fri Feb 15 11:36:34 2013 +0100
     3.3 @@ -388,65 +388,133 @@
     3.4  subsection {* Code setup for sets *}
     3.5  
     3.6  text {*
     3.7 -  Implement operations @{term "finite"}, @{term "card"}, @{term "op \<subseteq>"}, and @{term "op ="} 
     3.8 -  for sets using @{term "finite_UNIV"} and @{term "card_UNIV"}.
     3.9 +  Implement @{term "CARD('a)"} via @{term card_UNIV} and provide
    3.10 +  implementations for @{term "finite"}, @{term "card"}, @{term "op \<subseteq>"}, 
    3.11 +  and @{term "op ="}if the calling context already provides @{class finite_UNIV}
    3.12 +  and @{class card_UNIV} instances. If we implemented the latter
    3.13 +  always via @{term card_UNIV}, we would require instances of essentially all 
    3.14 +  element types, i.e., a lot of instantiation proofs and -- at run time --
    3.15 +  possibly slow dictionary constructions.
    3.16  *}
    3.17  
    3.18 +definition card_UNIV' :: "'a card_UNIV"
    3.19 +where [code del]: "card_UNIV' = Phantom('a) CARD('a)"
    3.20 +
    3.21 +lemma CARD_code [code_unfold]:
    3.22 +  "CARD('a) = of_phantom (card_UNIV' :: 'a card_UNIV)"
    3.23 +by(simp add: card_UNIV'_def)
    3.24 +
    3.25 +lemma card_UNIV'_code [code]:
    3.26 +  "card_UNIV' = card_UNIV"
    3.27 +by(simp add: card_UNIV card_UNIV'_def)
    3.28 +
    3.29 +hide_const (open) card_UNIV'
    3.30 +
    3.31  lemma card_Compl:
    3.32    "finite A \<Longrightarrow> card (- A) = card (UNIV :: 'a set) - card (A :: 'a set)"
    3.33  by (metis Compl_eq_Diff_UNIV card_Diff_subset top_greatest)
    3.34  
    3.35 -lemma card_coset_code [code]:
    3.36 -  fixes xs :: "'a :: card_UNIV list" 
    3.37 -  shows "card (List.coset xs) = of_phantom (card_UNIV :: 'a card_UNIV) - length (remdups xs)"
    3.38 -by(simp add: List.card_set card_Compl card_UNIV)
    3.39 +context fixes xs :: "'a :: finite_UNIV list"
    3.40 +begin
    3.41  
    3.42 -lemma [code, code del]: "finite = finite" ..
    3.43 +definition finite' :: "'a set \<Rightarrow> bool"
    3.44 +where [simp, code del, code_abbrev]: "finite' = finite"
    3.45  
    3.46 -lemma [code]:
    3.47 -  fixes xs :: "'a :: card_UNIV list" 
    3.48 -  shows finite_set_code:
    3.49 -  "finite (set xs) = True" 
    3.50 -  and finite_coset_code:
    3.51 -  "finite (List.coset xs) \<longleftrightarrow> of_phantom (finite_UNIV :: 'a finite_UNIV)"
    3.52 +lemma finite'_code [code]:
    3.53 +  "finite' (set xs) \<longleftrightarrow> True"
    3.54 +  "finite' (List.coset xs) \<longleftrightarrow> of_phantom (finite_UNIV :: 'a finite_UNIV)"
    3.55  by(simp_all add: card_gt_0_iff finite_UNIV)
    3.56  
    3.57 -lemma coset_subset_code [code]:
    3.58 -  fixes xs :: "'a list" shows
    3.59 -  "List.coset xs \<subseteq> set ys \<longleftrightarrow> (let n = CARD('a) in n > 0 \<and> card (set (xs @ ys)) = n)"
    3.60 +end
    3.61 +
    3.62 +context fixes xs :: "'a :: card_UNIV list"
    3.63 +begin
    3.64 +
    3.65 +definition card' :: "'a set \<Rightarrow> nat" 
    3.66 +where [simp, code del, code_abbrev]: "card' = card"
    3.67 + 
    3.68 +lemma card'_code [code]:
    3.69 +  "card' (set xs) = length (remdups xs)"
    3.70 +  "card' (List.coset xs) = of_phantom (card_UNIV :: 'a card_UNIV) - length (remdups xs)"
    3.71 +by(simp_all add: List.card_set card_Compl card_UNIV)
    3.72 +
    3.73 +
    3.74 +definition subset' :: "'a set \<Rightarrow> 'a set \<Rightarrow> bool"
    3.75 +where [simp, code del, code_abbrev]: "subset' = op \<subseteq>"
    3.76 +
    3.77 +lemma subset'_code [code]:
    3.78 +  "subset' A (List.coset ys) \<longleftrightarrow> (\<forall>y \<in> set ys. y \<notin> A)"
    3.79 +  "subset' (set ys) B \<longleftrightarrow> (\<forall>y \<in> set ys. y \<in> B)"
    3.80 +  "subset' (List.coset xs) (set ys) \<longleftrightarrow> (let n = CARD('a) in n > 0 \<and> card(set (xs @ ys)) = n)"
    3.81  by(auto simp add: Let_def card_gt_0_iff dest: card_eq_UNIV_imp_eq_UNIV intro: arg_cong[where f=card])
    3.82    (metis finite_compl finite_set rev_finite_subset)
    3.83  
    3.84 -lemma equal_set_code [code]:
    3.85 -  fixes xs ys :: "'a :: equal list"
    3.86 +definition eq_set :: "'a set \<Rightarrow> 'a set \<Rightarrow> bool"
    3.87 +where [simp, code del, code_abbrev]: "eq_set = op ="
    3.88 +
    3.89 +lemma eq_set_code [code]:
    3.90 +  fixes ys
    3.91    defines "rhs \<equiv> 
    3.92    let n = CARD('a)
    3.93    in if n = 0 then False else 
    3.94          let xs' = remdups xs; ys' = remdups ys 
    3.95          in length xs' + length ys' = n \<and> (\<forall>x \<in> set xs'. x \<notin> set ys') \<and> (\<forall>y \<in> set ys'. y \<notin> set xs')"
    3.96 -  shows "equal_class.equal (List.coset xs) (set ys) \<longleftrightarrow> rhs" (is ?thesis1)
    3.97 -  and "equal_class.equal (set ys) (List.coset xs) \<longleftrightarrow> rhs" (is ?thesis2)
    3.98 -  and "equal_class.equal (set xs) (set ys) \<longleftrightarrow> (\<forall>x \<in> set xs. x \<in> set ys) \<and> (\<forall>y \<in> set ys. y \<in> set xs)" (is ?thesis3)
    3.99 -  and "equal_class.equal (List.coset xs) (List.coset ys) \<longleftrightarrow> (\<forall>x \<in> set xs. x \<in> set ys) \<and> (\<forall>y \<in> set ys. y \<in> set xs)" (is ?thesis4)
   3.100 +  shows "eq_set (List.coset xs) (set ys) \<longleftrightarrow> rhs" (is ?thesis1)
   3.101 +  and "eq_set (set ys) (List.coset xs) \<longleftrightarrow> rhs" (is ?thesis2)
   3.102 +  and "eq_set (set xs) (set ys) \<longleftrightarrow> (\<forall>x \<in> set xs. x \<in> set ys) \<and> (\<forall>y \<in> set ys. y \<in> set xs)" (is ?thesis3)
   3.103 +  and "eq_set (List.coset xs) (List.coset ys) \<longleftrightarrow> (\<forall>x \<in> set xs. x \<in> set ys) \<and> (\<forall>y \<in> set ys. y \<in> set xs)" (is ?thesis4)
   3.104  proof -
   3.105    show ?thesis1 (is "?lhs \<longleftrightarrow> ?rhs")
   3.106    proof
   3.107      assume ?lhs thus ?rhs
   3.108 -      by(auto simp add: equal_eq rhs_def Let_def List.card_set[symmetric] card_Un_Int[where A="set xs" and B="- set xs"] card_UNIV Compl_partition card_gt_0_iff dest: sym)(metis finite_compl finite_set)
   3.109 +      by(auto simp add: rhs_def Let_def List.card_set[symmetric] card_Un_Int[where A="set xs" and B="- set xs"] card_UNIV Compl_partition card_gt_0_iff dest: sym)(metis finite_compl finite_set)
   3.110    next
   3.111      assume ?rhs
   3.112      moreover have "\<lbrakk> \<forall>y\<in>set xs. y \<notin> set ys; \<forall>x\<in>set ys. x \<notin> set xs \<rbrakk> \<Longrightarrow> set xs \<inter> set ys = {}" by blast
   3.113      ultimately show ?lhs
   3.114 -      by(auto simp add: equal_eq rhs_def Let_def List.card_set[symmetric] card_UNIV card_gt_0_iff card_Un_Int[where A="set xs" and B="set ys"] dest: card_eq_UNIV_imp_eq_UNIV split: split_if_asm)
   3.115 +      by(auto simp add: rhs_def Let_def List.card_set[symmetric] card_UNIV card_gt_0_iff card_Un_Int[where A="set xs" and B="set ys"] dest: card_eq_UNIV_imp_eq_UNIV split: split_if_asm)
   3.116    qed
   3.117 -  thus ?thesis2 unfolding equal_eq by blast
   3.118 -  show ?thesis3 ?thesis4 unfolding equal_eq List.coset_def by blast+
   3.119 +  thus ?thesis2 unfolding eq_set_def by blast
   3.120 +  show ?thesis3 ?thesis4 unfolding eq_set_def List.coset_def by blast+
   3.121  qed
   3.122  
   3.123 -notepad begin (* test code setup *)
   3.124 -have "List.coset [True] = set [False] \<and> List.coset [] \<subseteq> List.set [True, False] \<and> finite (List.coset [True])"
   3.125 +end
   3.126 +
   3.127 +text {* 
   3.128 +  Provide more informative exceptions than Match for non-rewritten cases.
   3.129 +  If generated code raises one these exceptions, then a code equation calls
   3.130 +  the mentioned operator for an element type that is not an instance of
   3.131 +  @{class card_UNIV} and is therefore not implemented via @{term card_UNIV}.
   3.132 +  Constrain the element type with sort @{class card_UNIV} to change this.
   3.133 +*}
   3.134 +
   3.135 +definition card_coset_requires_card_UNIV :: "'a list \<Rightarrow> nat"
   3.136 +where [code del, simp]: "card_coset_requires_card_UNIV xs = card (List.coset xs)"
   3.137 +
   3.138 +code_abort card_coset_requires_card_UNIV
   3.139 +
   3.140 +lemma card_coset_error [code]:
   3.141 +  "card (List.coset xs) = card_coset_requires_card_UNIV xs"
   3.142 +by(simp)
   3.143 +
   3.144 +definition coset_subseteq_set_requires_card_UNIV :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool"
   3.145 +where [code del, simp]: "coset_subseteq_set_requires_card_UNIV xs ys \<longleftrightarrow> List.coset xs \<subseteq> set ys"
   3.146 +
   3.147 +code_abort coset_subseteq_set_requires_card_UNIV
   3.148 +
   3.149 +lemma coset_subseteq_set_code [code]:
   3.150 +  "List.coset xs \<subseteq> set ys \<longleftrightarrow> 
   3.151 +  (if xs = [] \<and> ys = [] then False else coset_subseteq_set_requires_card_UNIV xs ys)"
   3.152 +by simp
   3.153 +
   3.154 +notepad begin -- "test code setup"
   3.155 +have "List.coset [True] = set [False] \<and> 
   3.156 +      List.coset [] \<subseteq> List.set [True, False] \<and> 
   3.157 +      finite (List.coset [True])"
   3.158    by eval
   3.159  end
   3.160  
   3.161 +hide_const (open) card' finite' subset' eq_set
   3.162 +
   3.163  end
   3.164