# HG changeset patch # User traytel # Date 1366878670 -7200 # Node ID d2b265ebc1fa6b04c76cd736ef5abbeb2cb81fa7 # Parent e11b1ee200f58816baebfae8c2fe76bb06cce92d specify nicer names for map, set and rel in the stream library diff -r e11b1ee200f5 -r d2b265ebc1fa src/HOL/BNF/Examples/Koenig.thy --- a/src/HOL/BNF/Examples/Koenig.thy Thu Apr 25 09:25:50 2013 +0200 +++ b/src/HOL/BNF/Examples/Koenig.thy Thu Apr 25 10:31:10 2013 +0200 @@ -110,8 +110,8 @@ (* some more stream theorems *) -lemma stream_map[simp]: "stream_map f = stream_dtor_unfold (f o shd \ stl)" -unfolding stream_map_def pair_fun_def shd_def'[abs_def] stl_def'[abs_def] +lemma stream_map[simp]: "smap f = stream_dtor_unfold (f o shd \ stl)" +unfolding smap_def pair_fun_def shd_def'[abs_def] stl_def'[abs_def] map_pair_def o_def prod_case_beta by simp definition plus :: "nat stream \ nat stream \ nat stream" (infixr "\" 66) where @@ -119,7 +119,7 @@ stream_dtor_unfold ((%(xs, ys). shd xs + shd ys) \ (%(xs, ys). (stl xs, stl ys))) (xs, ys)" definition scalar :: "nat \ nat stream \ nat stream" (infixr "\" 68) where - [simp]: "scalar n = stream_map (\x. n * x)" + [simp]: "scalar n = smap (\x. n * x)" definition ones :: "nat stream" where [simp]: "ones = stream_dtor_unfold ((%x. 1) \ id) ()" definition twos :: "nat stream" where [simp]: "twos = stream_dtor_unfold ((%x. 2) \ id) ()" diff -r e11b1ee200f5 -r d2b265ebc1fa src/HOL/BNF/Examples/Stream.thy --- a/src/HOL/BNF/Examples/Stream.thy Thu Apr 25 09:25:50 2013 +0200 +++ b/src/HOL/BNF/Examples/Stream.thy Thu Apr 25 10:31:10 2013 +0200 @@ -12,7 +12,8 @@ imports "../BNF" begin -codata 'a stream = Stream (shd: 'a) (stl: "'a stream") (infixr "##" 65) +codata (sset: 'a) stream (map: smap rel: stream_all2) = + Stream (shd: 'a) (stl: "'a stream") (infixr "##" 65) declaration {* Nitpick_HOL.register_codatatype @@ -23,7 +24,7 @@ fixes x :: 'stream_element_type begin - lemma "stream_set s = {}" + lemma "sset s = {}" nitpick oops @@ -45,7 +46,7 @@ (*for code generation only*) definition smember :: "'a \ 'a stream \ bool" where - [code_abbrev]: "smember x s \ x \ stream_set s" + [code_abbrev]: "smember x s \ x \ sset s" lemma smember_code[code, simp]: "smember x (Stream y s) = (if x = y then True else smember x s)" unfolding smember_def by auto @@ -53,33 +54,33 @@ hide_const (open) smember (* TODO: Provide by the package*) -theorem stream_set_induct: - "\\s. P (shd s) s; \s y. \y \ stream_set (stl s); P y (stl s)\ \ P y s\ \ - \y \ stream_set s. P y s" +theorem sset_induct: + "\\s. P (shd s) s; \s y. \y \ sset (stl s); P y (stl s)\ \ P y s\ \ + \y \ sset s. P y s" by (rule stream.dtor_set_induct) (auto simp add: shd_def stl_def stream_case_def fsts_def snds_def split_beta) -lemma stream_map_simps[simp]: - "shd (stream_map f s) = f (shd s)" "stl (stream_map f s) = stream_map f (stl s)" - unfolding shd_def stl_def stream_case_def stream_map_def stream.dtor_unfold +lemma smap_simps[simp]: + "shd (smap f s) = f (shd s)" "stl (smap f s) = smap f (stl s)" + unfolding shd_def stl_def stream_case_def smap_def stream.dtor_unfold by (case_tac [!] s) (auto simp: Stream_def stream.dtor_ctor) declare stream.map[code] -theorem shd_stream_set: "shd s \ stream_set s" +theorem shd_sset: "shd s \ sset s" by (auto simp add: shd_def stl_def stream_case_def fsts_def snds_def split_beta) (metis UnCI fsts_def insertI1 stream.dtor_set) -theorem stl_stream_set: "y \ stream_set (stl s) \ y \ stream_set s" +theorem stl_sset: "y \ sset (stl s) \ y \ sset s" by (auto simp add: shd_def stl_def stream_case_def fsts_def snds_def split_beta) (metis insertI1 set_mp snds_def stream.dtor_set_set_incl) (* only for the non-mutual case: *) -theorem stream_set_induct1[consumes 1, case_names shd stl, induct set: "stream_set"]: - assumes "y \ stream_set s" and "\s. P (shd s) s" - and "\s y. \y \ stream_set (stl s); P y (stl s)\ \ P y s" +theorem sset_induct1[consumes 1, case_names shd stl, induct set: "sset"]: + assumes "y \ sset s" and "\s. P (shd s) s" + and "\s y. \y \ sset (stl s); P y (stl s)\ \ P y s" shows "P y s" - using assms stream_set_induct by blast + using assms sset_induct by blast (* end TODO *) @@ -89,7 +90,7 @@ "shift [] s = s" | "shift (x # xs) s = x ## shift xs s" -lemma stream_map_shift[simp]: "stream_map f (xs @- s) = map f xs @- stream_map f s" +lemma smap_shift[simp]: "smap f (xs @- s) = map f xs @- smap f s" by (induct xs) auto lemma shift_append[simp]: "(xs @ ys) @- s = xs @- ys @- s" @@ -100,7 +101,7 @@ "stl (xs @- s) = (if xs = [] then stl s else tl xs @- s)" by (induct xs) auto -lemma stream_set_shift[simp]: "stream_set (xs @- s) = set xs \ stream_set s" +lemma sset_shift[simp]: "sset (xs @- s) = set xs \ sset s" by (induct xs) auto lemma shift_left_inj[simp]: "xs @- s1 = xs @- s2 \ s1 = s2" @@ -118,16 +119,16 @@ lemma shift_streams: "\w \ lists A; s \ streams A\ \ w @- s \ streams A" by (induct w) auto -lemma stream_set_streams: - assumes "stream_set s \ A" +lemma sset_streams: + assumes "sset s \ A" shows "s \ streams A" -proof (coinduct rule: streams.coinduct[of "\s'. \a s. s' = a ## s \ a \ A \ stream_set s \ A"]) +proof (coinduct rule: streams.coinduct[of "\s'. \a s. s' = a ## s \ a \ A \ sset s \ A"]) case streams from assms show ?case by (cases s) auto next - fix s' assume "\a s. s' = a ## s \ a \ A \ stream_set s \ A" + fix s' assume "\a s. s' = a ## s \ a \ A \ sset s \ A" then guess a s by (elim exE) with assms show "\a l. s' = a ## l \ - a \ A \ ((\a s. l = a ## s \ a \ A \ stream_set s \ A) \ l \ streams A)" + a \ A \ ((\a s. l = a ## s \ a \ A \ sset s \ A) \ l \ streams A)" by (cases s) auto qed @@ -138,7 +139,7 @@ "s !! 0 = shd s" | "s !! Suc n = stl s !! n" -lemma snth_stream_map[simp]: "stream_map f s !! n = f (s !! n)" +lemma snth_smap[simp]: "smap f s !! n = f (s !! n)" by (induct n arbitrary: s) auto lemma shift_snth_less[simp]: "p < length xs \ (xs @- s) !! p = xs ! p" @@ -147,12 +148,12 @@ lemma shift_snth_ge[simp]: "p \ length xs \ (xs @- s) !! p = s !! (p - length xs)" by (induct p arbitrary: xs) (auto simp: Suc_diff_eq_diff_pred) -lemma snth_stream_set[simp]: "s !! n \ stream_set s" - by (induct n arbitrary: s) (auto intro: shd_stream_set stl_stream_set) +lemma snth_sset[simp]: "s !! n \ sset s" + by (induct n arbitrary: s) (auto intro: shd_sset stl_sset) -lemma stream_set_range: "stream_set s = range (snth s)" +lemma sset_range: "sset s = range (snth s)" proof (intro equalityI subsetI) - fix x assume "x \ stream_set s" + fix x assume "x \ sset s" thus "x \ range (snth s)" proof (induct s) case (stl s x) @@ -168,7 +169,7 @@ lemma length_stake[simp]: "length (stake n s) = n" by (induct n arbitrary: s) auto -lemma stake_stream_map[simp]: "stake n (stream_map f s) = map f (stake n s)" +lemma stake_smap[simp]: "stake n (smap f s) = map f (stake n s)" by (induct n arbitrary: s) auto primrec sdrop :: "nat \ 'a stream \ 'a stream" where @@ -179,7 +180,7 @@ "shd (sdrop n s) = s !! n" "stl (sdrop n s) = sdrop (Suc n) s" by (induct n arbitrary: s) auto -lemma sdrop_stream_map[simp]: "sdrop n (stream_map f s) = stream_map f (sdrop n s)" +lemma sdrop_smap[simp]: "sdrop n (smap f s) = smap f (sdrop n s)" by (induct n arbitrary: s) auto lemma sdrop_stl: "sdrop n (stl s) = stl (sdrop n s)" @@ -192,11 +193,11 @@ "s = stake i s @- s !! i ## sdrop (Suc i) s" by (subst stake_sdrop[symmetric, of _ i]) (metis sdrop_simps stream.collapse) -lemma stream_map_alt: "stream_map f s = s' \ (\n. f (s !! n) = s' !! n)" (is "?L = ?R") +lemma smap_alt: "smap f s = s' \ (\n. f (s !! n) = s' !! n)" (is "?L = ?R") proof assume ?R thus ?L - by (coinduct rule: stream.coinduct[of "\s1 s2. \n. s1 = stream_map f (sdrop n s) \ s2 = sdrop n s'"]) + by (coinduct rule: stream.coinduct[of "\s1 s2. \n. s1 = smap f (sdrop n s) \ s2 = sdrop n s'"]) (auto intro: exI[of _ 0] simp del: sdrop.simps(2)) qed auto @@ -243,8 +244,8 @@ definition "stream_all P s = (\p. P (s !! p))" -lemma stream_all_iff[iff]: "stream_all P s \ Ball (stream_set s) P" - unfolding stream_all_def stream_set_range by auto +lemma stream_all_iff[iff]: "stream_all P s \ Ball (sset s) P" + unfolding stream_all_def sset_range by auto lemma stream_all_shift[simp]: "stream_all P (xs @- s) = (list_all P xs \ stream_all P s)" unfolding stream_all_iff list_all_iff by auto @@ -359,16 +360,16 @@ lemma sdrop_fromN[simp]: "sdrop m (fromN n) = fromN (n + m)" unfolding fromN_def by (induct m arbitrary: n) auto -lemma stream_set_fromN[simp]: "stream_set (fromN n) = {n ..}" (is "?L = ?R") +lemma sset_fromN[simp]: "sset (fromN n) = {n ..}" (is "?L = ?R") proof safe fix m assume "m : ?L" moreover - { fix s assume "m \ stream_set s" "\n'\n. s = fromN n'" - hence "n \ m" by (induct arbitrary: n rule: stream_set_induct1) fastforce+ + { fix s assume "m \ sset s" "\n'\n. s = fromN n'" + hence "n \ m" by (induct arbitrary: n rule: sset_induct1) fastforce+ } ultimately show "n \ m" by blast next - fix m assume "n \ m" thus "m \ ?L" by (metis le_iff_add snth_fromN snth_stream_set) + fix m assume "n \ m" thus "m \ ?L" by (metis le_iff_add snth_fromN snth_sset) qed abbreviation "nats \ fromN 0" @@ -393,15 +394,15 @@ lemma flat_unfold: "shd ws \ [] \ flat ws = shd ws @- flat (stl ws)" by (cases ws) auto -lemma flat_snth: "\xs \ stream_set s. xs \ [] \ flat s !! n = (if n < length (shd s) then +lemma flat_snth: "\xs \ sset s. xs \ [] \ flat s !! n = (if n < length (shd s) then shd s ! n else flat (stl s) !! (n - length (shd s)))" - by (metis flat_unfold not_less shd_stream_set shift_snth_ge shift_snth_less) + by (metis flat_unfold not_less shd_sset shift_snth_ge shift_snth_less) -lemma stream_set_flat[simp]: "\xs \ stream_set s. xs \ [] \ - stream_set (flat s) = (\xs \ stream_set s. set xs)" (is "?P \ ?L = ?R") +lemma sset_flat[simp]: "\xs \ sset s. xs \ [] \ + sset (flat s) = (\xs \ sset s. set xs)" (is "?P \ ?L = ?R") proof safe fix x assume ?P "x : ?L" - then obtain m where "x = flat s !! m" by (metis image_iff stream_set_range) + then obtain m where "x = flat s !! m" by (metis image_iff sset_range) with `?P` obtain n m' where "x = s !! n ! m'" "m' < length (s !! n)" proof (atomize_elim, induct m arbitrary: s rule: less_induct) case (less y) @@ -416,60 +417,60 @@ moreover with False have "y > 0" by (cases y) simp_all ultimately have "y - length (shd s) < y" by simp } - moreover have "\xs \ stream_set (stl s). xs \ []" using less(2) by (cases s) auto + moreover have "\xs \ sset (stl s). xs \ []" using less(2) by (cases s) auto ultimately have "\n m'. x = stl s !! n ! m' \ m' < length (stl s !! n)" by (intro less(1)) auto thus ?thesis by (metis snth.simps(2)) qed qed - thus "x \ ?R" by (auto simp: stream_set_range dest!: nth_mem) + thus "x \ ?R" by (auto simp: sset_range dest!: nth_mem) next - fix x xs assume "xs \ stream_set s" ?P "x \ set xs" thus "x \ ?L" - by (induct rule: stream_set_induct1) - (metis UnI1 flat_unfold shift.simps(1) stream_set_shift, - metis UnI2 flat_unfold shd_stream_set stl_stream_set stream_set_shift) + fix x xs assume "xs \ sset s" ?P "x \ set xs" thus "x \ ?L" + by (induct rule: sset_induct1) + (metis UnI1 flat_unfold shift.simps(1) sset_shift, + metis UnI2 flat_unfold shd_sset stl_sset sset_shift) qed subsection {* merge a stream of streams *} definition smerge :: "'a stream stream \ 'a stream" where - "smerge ss = flat (stream_map (\n. map (\s. s !! n) (stake (Suc n) ss) @ stake n (ss !! n)) nats)" + "smerge ss = flat (smap (\n. map (\s. s !! n) (stake (Suc n) ss) @ stake n (ss !! n)) nats)" lemma stake_nth[simp]: "m < n \ stake n s ! m = s !! m" by (induct n arbitrary: s m) (auto simp: nth_Cons', metis Suc_pred snth.simps(2)) -lemma snth_stream_set_smerge: "ss !! n !! m \ stream_set (smerge ss)" +lemma snth_sset_smerge: "ss !! n !! m \ sset (smerge ss)" proof (cases "n \ m") case False thus ?thesis unfolding smerge_def - by (subst stream_set_flat) + by (subst sset_flat) (auto simp: stream.set_map' in_set_conv_nth simp del: stake.simps intro!: exI[of _ n, OF disjI2] exI[of _ m, OF mp]) next case True thus ?thesis unfolding smerge_def - by (subst stream_set_flat) + by (subst sset_flat) (auto simp: stream.set_map' in_set_conv_nth image_iff simp del: stake.simps snth.simps intro!: exI[of _ m, OF disjI1] bexI[of _ "ss !! n"] exI[of _ n, OF mp]) qed -lemma stream_set_smerge: "stream_set (smerge ss) = UNION (stream_set ss) stream_set" +lemma sset_smerge: "sset (smerge ss) = UNION (sset ss) sset" proof safe - fix x assume "x \ stream_set (smerge ss)" - thus "x \ UNION (stream_set ss) stream_set" - unfolding smerge_def by (subst (asm) stream_set_flat) - (auto simp: stream.set_map' in_set_conv_nth stream_set_range simp del: stake.simps, fast+) + fix x assume "x \ sset (smerge ss)" + thus "x \ UNION (sset ss) sset" + unfolding smerge_def by (subst (asm) sset_flat) + (auto simp: stream.set_map' in_set_conv_nth sset_range simp del: stake.simps, fast+) next - fix s x assume "s \ stream_set ss" "x \ stream_set s" - thus "x \ stream_set (smerge ss)" using snth_stream_set_smerge by (auto simp: stream_set_range) + fix s x assume "s \ sset ss" "x \ sset s" + thus "x \ sset (smerge ss)" using snth_sset_smerge by (auto simp: sset_range) qed subsection {* product of two streams *} definition sproduct :: "'a stream \ 'b stream \ ('a \ 'b) stream" where - "sproduct s1 s2 = smerge (stream_map (\x. stream_map (Pair x) s2) s1)" + "sproduct s1 s2 = smerge (smap (\x. smap (Pair x) s2) s1)" -lemma stream_set_sproduct: "stream_set (sproduct s1 s2) = stream_set s1 \ stream_set s2" - unfolding sproduct_def stream_set_smerge by (auto simp: stream.set_map') +lemma sset_sproduct: "sset (sproduct s1 s2) = sset s1 \ sset s2" + unfolding sproduct_def sset_smerge by (auto simp: stream.set_map') subsection {* interleave two streams *} @@ -492,24 +493,24 @@ by (induct n arbitrary: s1 s2) (auto dest: even_nat_Suc_div_2 odd_nat_plus_one_div_two[folded nat_2]) -lemma stream_set_sinterleave: "stream_set (sinterleave s1 s2) = stream_set s1 \ stream_set s2" +lemma sset_sinterleave: "sset (sinterleave s1 s2) = sset s1 \ sset s2" proof (intro equalityI subsetI) - fix x assume "x \ stream_set (sinterleave s1 s2)" - then obtain n where "x = sinterleave s1 s2 !! n" unfolding stream_set_range by blast - thus "x \ stream_set s1 \ stream_set s2" by (cases "even n") auto + fix x assume "x \ sset (sinterleave s1 s2)" + then obtain n where "x = sinterleave s1 s2 !! n" unfolding sset_range by blast + thus "x \ sset s1 \ sset s2" by (cases "even n") auto next - fix x assume "x \ stream_set s1 \ stream_set s2" - thus "x \ stream_set (sinterleave s1 s2)" + fix x assume "x \ sset s1 \ sset s2" + thus "x \ sset (sinterleave s1 s2)" proof - assume "x \ stream_set s1" - then obtain n where "x = s1 !! n" unfolding stream_set_range by blast + assume "x \ sset s1" + then obtain n where "x = s1 !! n" unfolding sset_range by blast hence "sinterleave s1 s2 !! (2 * n) = x" by simp - thus ?thesis unfolding stream_set_range by blast + thus ?thesis unfolding sset_range by blast next - assume "x \ stream_set s2" - then obtain n where "x = s2 !! n" unfolding stream_set_range by blast + assume "x \ sset s2" + then obtain n where "x = s2 !! n" unfolding sset_range by blast hence "sinterleave s1 s2 !! (2 * n + 1) = x" by simp - thus ?thesis unfolding stream_set_range by blast + thus ?thesis unfolding sset_range by blast qed qed @@ -532,22 +533,22 @@ subsection {* zip via function *} -definition "stream_map2 f s1 s2 = +definition "smap2 f s1 s2 = stream_unfold (\(s1,s2). f (shd s1) (shd s2)) (map_pair stl stl) (s1, s2)" -lemma stream_map2_simps[simp]: - "shd (stream_map2 f s1 s2) = f (shd s1) (shd s2)" - "stl (stream_map2 f s1 s2) = stream_map2 f (stl s1) (stl s2)" - unfolding stream_map2_def by auto +lemma smap2_simps[simp]: + "shd (smap2 f s1 s2) = f (shd s1) (shd s2)" + "stl (smap2 f s1 s2) = smap2 f (stl s1) (stl s2)" + unfolding smap2_def by auto -lemma stream_map2_unfold[code]: - "stream_map2 f (Stream a s1) (Stream b s2) = Stream (f a b) (stream_map2 f s1 s2)" - unfolding stream_map2_def by (subst stream.unfold) simp +lemma smap2_unfold[code]: + "smap2 f (Stream a s1) (Stream b s2) = Stream (f a b) (smap2 f s1 s2)" + unfolding smap2_def by (subst stream.unfold) simp -lemma stream_map2_szip: - "stream_map2 f s1 s2 = stream_map (split f) (szip s1 s2)" +lemma smap2_szip: + "smap2 f s1 s2 = smap (split f) (szip s1 s2)" by (coinduct rule: stream.coinduct[of - "\s1 s2. \s1' s2'. s1 = stream_map2 f s1' s2' \ s2 = stream_map (split f) (szip s1' s2')"]) + "\s1 s2. \s1' s2'. s1 = smap2 f s1' s2' \ s2 = smap (split f) (szip s1' s2')"]) fastforce+ @@ -574,7 +575,7 @@ lemma stake_siterate[simp]: "stake n (siterate f x) = map (\n. (f^^n) x) [0 ..< n]" by (induct n arbitrary: x) (auto simp del: stake.simps(2) simp: stake_Suc) -lemma stream_set_siterate: "stream_set (siterate f x) = {(f^^n) x | n. True}" - by (auto simp: stream_set_range) +lemma sset_siterate: "sset (siterate f x) = {(f^^n) x | n. True}" + by (auto simp: sset_range) end