# HG changeset patch # User traytel # Date 1360234662 -3600 # Node ID 550f265864e30626af83b11a6a353c7525006cce # Parent 78de6c7e8a583c65075f90a05086f5a62eff41c1 infix syntax for streams (reflecting the one for lists) diff -r 78de6c7e8a58 -r 550f265864e3 src/HOL/BNF/Examples/Stream.thy --- a/src/HOL/BNF/Examples/Stream.thy Wed Feb 06 17:57:21 2013 +0100 +++ b/src/HOL/BNF/Examples/Stream.thy Thu Feb 07 11:57:42 2013 +0100 @@ -12,7 +12,7 @@ imports "../BNF" begin -codata 'a stream = Stream (shd: 'a) (stl: "'a stream") +codata 'a stream = Stream (shd: 'a) (stl: "'a stream") (infixr "##" 65) (* TODO: Provide by the package*) theorem stream_set_induct: @@ -42,7 +42,7 @@ primrec shift :: "'a list \ 'a stream \ 'a stream" (infixr "@-" 65) where "shift [] s = s" -| "shift (x # xs) s = Stream x (shift xs s)" +| "shift (x # xs) s = x ## shift xs s" lemma shift_append[simp]: "(xs @ ys) @- s = xs @- ys @- s" by (induct xs) auto @@ -71,10 +71,10 @@ thus ?case using stream.unfold[of hd "\xs. tl xs @ [hd xs]" u] by (auto simp: cycle_def) qed auto -lemma cycle_Cons: "cycle (x # xs) = Stream x (cycle (xs @ [x]))" -proof (coinduct rule: stream.coinduct[of "\s1 s2. \x xs. s1 = cycle (x # xs) \ s2 = Stream x (cycle (xs @ [x]))"]) +lemma cycle_Cons: "cycle (x # xs) = x ## cycle (xs @ [x])" +proof (coinduct rule: stream.coinduct[of "\s1 s2. \x xs. s1 = cycle (x # xs) \ s2 = x ## cycle (xs @ [x])"]) case (2 s1 s2) - then obtain x xs where "s1 = cycle (x # xs) \ s2 = Stream x (cycle (xs @ [x]))" by blast + then obtain x xs where "s1 = cycle (x # xs) \ s2 = x ## cycle (xs @ [x])" by blast thus ?case by (auto simp: cycle_def intro!: exI[of _ "hd (xs @ [x])"] exI[of _ "tl (xs @ [x])"] stream.unfold) qed auto @@ -83,7 +83,7 @@ streams :: "'a set => 'a stream set" for A :: "'a set" where - Stream[intro!, simp, no_atp]: "\a \ A; s \ streams A\ \ Stream a s \ streams A" + Stream[intro!, simp, no_atp]: "\a \ A; s \ streams A\ \ a ## s \ streams A" lemma shift_streams: "\w \ lists A; s \ streams A\ \ w @- s \ streams A" by (induct w) auto @@ -91,13 +91,13 @@ lemma stream_set_streams: assumes "stream_set s \ A" shows "s \ streams A" -proof (coinduct rule: streams.coinduct[of "\s'. \a s. s' = Stream a s \ a \ A \ stream_set s \ A"]) +proof (coinduct rule: streams.coinduct[of "\s'. \a s. s' = a ## s \ a \ A \ stream_set s \ A"]) case streams from assms show ?case by (cases s) auto next - fix s' assume "\a s. s' = Stream a s \ a \ A \ stream_set s \ A" + fix s' assume "\a s. s' = a ## s \ a \ A \ stream_set s \ A" then guess a s by (elim exE) - with assms show "\a l. s' = Stream a l \ - a \ A \ ((\a s. l = Stream a s \ a \ A \ stream_set s \ A) \ l \ streams A)" + with assms show "\a l. s' = a ## l \ + a \ A \ ((\a s. l = a ## s \ a \ A \ stream_set s \ A) \ l \ streams A)" by (cases s) auto qed @@ -105,17 +105,17 @@ subsection {* flatten a stream of lists *} definition flat where - "flat \ stream_unfold (hd o shd) (\s. if tl (shd s) = [] then stl s else Stream (tl (shd s)) (stl s))" + "flat \ stream_unfold (hd o shd) (\s. if tl (shd s) = [] then stl s else tl (shd s) ## stl s)" lemma flat_simps[simp]: "shd (flat ws) = hd (shd ws)" - "stl (flat ws) = flat (if tl (shd ws) = [] then stl ws else Stream (tl (shd ws)) (stl ws))" + "stl (flat ws) = flat (if tl (shd ws) = [] then stl ws else tl (shd ws) ## stl ws)" unfolding flat_def by auto -lemma flat_Cons[simp]: "flat (Stream (x#xs) w) = Stream x (flat (if xs = [] then w else Stream xs w))" -unfolding flat_def using stream.unfold[of "hd o shd" _ "Stream (x#xs) w"] by auto +lemma flat_Cons[simp]: "flat ((x # xs) ## ws) = x ## flat (if xs = [] then ws else xs ## ws)" +unfolding flat_def using stream.unfold[of "hd o shd" _ "(x # xs) ## ws"] by auto -lemma flat_Stream[simp]: "xs \ [] \ flat (Stream xs ws) = xs @- flat ws" +lemma flat_Stream[simp]: "xs \ [] \ flat (xs ## ws) = xs @- flat ws" by (induct xs) auto lemma flat_unfold: "shd ws \ [] \ flat ws = shd ws @- flat (stl ws)" @@ -132,9 +132,9 @@ "sdrop 0 s = s" | "sdrop (Suc n) s = sdrop n (stl s)" -primrec snth :: "nat \ 'a stream \ 'a" where - "snth 0 s = shd s" -| "snth (Suc n) s = snth n (stl s)" +primrec snth :: "'a stream \ nat \ 'a" (infixl "!!" 100) where + "s !! 0 = shd s" +| "s !! Suc n = stl s !! n" lemma stake_sdrop: "stake n s @- sdrop n s = s" by (induct n arbitrary: s) auto