11 theory Stream |
11 theory Stream |
12 imports "../BNF" |
12 imports "../BNF" |
13 begin |
13 begin |
14 |
14 |
15 codata 'a stream = Stream (shd: 'a) (stl: "'a stream") (infixr "##" 65) |
15 codata 'a stream = Stream (shd: 'a) (stl: "'a stream") (infixr "##" 65) |
|
16 |
|
17 declaration {* |
|
18 Nitpick_HOL.register_codatatype |
|
19 @{typ "'stream_element_type stream"} @{const_name stream_case} [dest_Const @{term Stream}] |
|
20 (*FIXME: long type variable name required to reduce the probability of |
|
21 a name clash of Nitpick in context. E.g.: |
|
22 context |
|
23 fixes x :: 'stream_element_type |
|
24 begin |
|
25 |
|
26 lemma "stream_set s = {}" |
|
27 nitpick |
|
28 oops |
|
29 |
|
30 end |
|
31 *) |
|
32 *} |
|
33 |
|
34 code_datatype Stream |
|
35 lemmas [code] = stream.sels stream.sets stream.case |
|
36 |
|
37 lemma stream_case_cert: |
|
38 assumes "CASE \<equiv> stream_case c" |
|
39 shows "CASE (a ## s) \<equiv> c a s" |
|
40 using assms by simp_all |
|
41 |
|
42 setup {* |
|
43 Code.add_case @{thm stream_case_cert} |
|
44 *} |
16 |
45 |
17 (* TODO: Provide by the package*) |
46 (* TODO: Provide by the package*) |
18 theorem stream_set_induct: |
47 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> |
48 "\<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" |
49 \<forall>y \<in> stream_set s. P y s" |
24 lemma stream_map_simps[simp]: |
53 lemma stream_map_simps[simp]: |
25 "shd (stream_map f s) = f (shd s)" "stl (stream_map f s) = stream_map f (stl s)" |
54 "shd (stream_map f s) = f (shd s)" "stl (stream_map f s) = stream_map f (stl s)" |
26 unfolding shd_def stl_def stream_case_def stream_map_def stream.dtor_unfold |
55 unfolding shd_def stl_def stream_case_def stream_map_def stream.dtor_unfold |
27 by (case_tac [!] s) (auto simp: Stream_def stream.dtor_ctor) |
56 by (case_tac [!] s) (auto simp: Stream_def stream.dtor_ctor) |
28 |
57 |
29 lemma stream_map_Stream[simp]: "stream_map f (x ## s) = f x ## stream_map f s" |
58 lemma stream_map_Stream[simp, code]: "stream_map f (x ## s) = f x ## stream_map f s" |
30 by (metis stream.exhaust stream.sels stream_map_simps) |
59 by (metis stream.exhaust stream.sels stream_map_simps) |
31 |
60 |
32 theorem shd_stream_set: "shd s \<in> stream_set s" |
61 theorem shd_stream_set: "shd s \<in> stream_set s" |
33 by (auto simp add: shd_def stl_def stream_case_def fsts_def snds_def split_beta) |
62 by (auto simp add: shd_def stl_def stream_case_def fsts_def snds_def split_beta) |
34 (metis UnCI fsts_def insertI1 stream.dtor_set) |
63 (metis UnCI fsts_def insertI1 stream.dtor_set) |
198 lemma flat_simps[simp]: |
227 lemma flat_simps[simp]: |
199 "shd (flat ws) = hd (shd ws)" |
228 "shd (flat ws) = hd (shd ws)" |
200 "stl (flat ws) = flat (if tl (shd ws) = [] then stl ws else tl (shd ws) ## stl ws)" |
229 "stl (flat ws) = flat (if tl (shd ws) = [] then stl ws else tl (shd ws) ## stl ws)" |
201 unfolding flat_def by auto |
230 unfolding flat_def by auto |
202 |
231 |
203 lemma flat_Cons[simp]: "flat ((x # xs) ## ws) = x ## flat (if xs = [] then ws else xs ## ws)" |
232 lemma flat_Cons[simp, code]: "flat ((x # xs) ## ws) = x ## flat (if xs = [] then ws else xs ## ws)" |
204 unfolding flat_def using stream.unfold[of "hd o shd" _ "(x # xs) ## ws"] by auto |
233 unfolding flat_def using stream.unfold[of "hd o shd" _ "(x # xs) ## ws"] by auto |
205 |
234 |
206 lemma flat_Stream[simp]: "xs \<noteq> [] \<Longrightarrow> flat (xs ## ws) = xs @- flat ws" |
235 lemma flat_Stream[simp]: "xs \<noteq> [] \<Longrightarrow> flat (xs ## ws) = xs @- flat ws" |
207 by (induct xs) auto |
236 by (induct xs) auto |
208 |
237 |
261 case (2 s1 s2) |
290 case (2 s1 s2) |
262 then obtain u where "s1 = cycle u \<and> s2 = u @- cycle u \<and> u \<noteq> []" by blast |
291 then obtain u where "s1 = cycle u \<and> s2 = u @- cycle u \<and> u \<noteq> []" by blast |
263 thus ?case using stream.unfold[of hd "\<lambda>xs. tl xs @ [hd xs]" u] by (auto simp: cycle_def) |
292 thus ?case using stream.unfold[of hd "\<lambda>xs. tl xs @ [hd xs]" u] by (auto simp: cycle_def) |
264 qed auto |
293 qed auto |
265 |
294 |
266 lemma cycle_Cons: "cycle (x # xs) = x ## cycle (xs @ [x])" |
295 lemma cycle_Cons[code]: "cycle (x # xs) = x ## cycle (xs @ [x])" |
267 proof (coinduct rule: stream.coinduct[of "\<lambda>s1 s2. \<exists>x xs. s1 = cycle (x # xs) \<and> s2 = x ## cycle (xs @ [x])"]) |
296 proof (coinduct rule: stream.coinduct[of "\<lambda>s1 s2. \<exists>x xs. s1 = cycle (x # xs) \<and> s2 = x ## cycle (xs @ [x])"]) |
268 case (2 s1 s2) |
297 case (2 s1 s2) |
269 then obtain x xs where "s1 = cycle (x # xs) \<and> s2 = x ## cycle (xs @ [x])" by blast |
298 then obtain x xs where "s1 = cycle (x # xs) \<and> s2 = x ## cycle (xs @ [x])" by blast |
270 thus ?case |
299 thus ?case |
271 by (auto simp: cycle_def intro!: exI[of _ "hd (xs @ [x])"] exI[of _ "tl (xs @ [x])"] stream.unfold) |
300 by (auto simp: cycle_def intro!: exI[of _ "hd (xs @ [x])"] exI[of _ "tl (xs @ [x])"] stream.unfold) |
312 definition "same x = stream_unfold (\<lambda>_. x) id ()" |
341 definition "same x = stream_unfold (\<lambda>_. x) id ()" |
313 |
342 |
314 lemma same_simps[simp]: "shd (same x) = x" "stl (same x) = same x" |
343 lemma same_simps[simp]: "shd (same x) = x" "stl (same x) = same x" |
315 unfolding same_def by auto |
344 unfolding same_def by auto |
316 |
345 |
317 lemma same_unfold: "same x = Stream x (same x)" |
346 lemma same_unfold[code]: "same x = x ## same x" |
318 by (metis same_simps stream.collapse) |
347 by (metis same_simps stream.collapse) |
319 |
348 |
320 lemma snth_same[simp]: "same x !! n = x" |
349 lemma snth_same[simp]: "same x !! n = x" |
321 unfolding same_def by (induct n) auto |
350 unfolding same_def by (induct n) auto |
322 |
351 |
340 |
369 |
341 definition "fromN n = stream_unfold id Suc n" |
370 definition "fromN n = stream_unfold id Suc n" |
342 |
371 |
343 lemma fromN_simps[simp]: "shd (fromN n) = n" "stl (fromN n) = fromN (Suc n)" |
372 lemma fromN_simps[simp]: "shd (fromN n) = n" "stl (fromN n) = fromN (Suc n)" |
344 unfolding fromN_def by auto |
373 unfolding fromN_def by auto |
|
374 |
|
375 lemma fromN_unfold[code]: "fromN n = n ## fromN (Suc n)" |
|
376 unfolding fromN_def by (metis id_def stream.unfold) |
345 |
377 |
346 lemma snth_fromN[simp]: "fromN n !! m = n + m" |
378 lemma snth_fromN[simp]: "fromN n !! m = n + m" |
347 unfolding fromN_def by (induct m arbitrary: n) auto |
379 unfolding fromN_def by (induct m arbitrary: n) auto |
348 |
380 |
349 lemma stake_fromN[simp]: "stake m (fromN n) = [n ..< m + n]" |
381 lemma stake_fromN[simp]: "stake m (fromN n) = [n ..< m + n]" |
374 |
406 |
375 lemma szip_simps[simp]: |
407 lemma szip_simps[simp]: |
376 "shd (szip s1 s2) = (shd s1, shd s2)" "stl (szip s1 s2) = szip (stl s1) (stl s2)" |
408 "shd (szip s1 s2) = (shd s1, shd s2)" "stl (szip s1 s2) = szip (stl s1) (stl s2)" |
377 unfolding szip_def by auto |
409 unfolding szip_def by auto |
378 |
410 |
|
411 lemma szip_unfold[code]: "szip (Stream a s1) (Stream b s2) = Stream (a, b) (szip s1 s2)" |
|
412 unfolding szip_def by (subst stream.unfold) simp |
|
413 |
379 lemma snth_szip[simp]: "szip s1 s2 !! n = (s1 !! n, s2 !! n)" |
414 lemma snth_szip[simp]: "szip s1 s2 !! n = (s1 !! n, s2 !! n)" |
380 by (induct n arbitrary: s1 s2) auto |
415 by (induct n arbitrary: s1 s2) auto |
381 |
416 |
382 |
417 |
383 subsection {* zip via function *} |
418 subsection {* zip via function *} |
384 |
419 |
385 definition "stream_map2 f s1 s2 = |
420 definition "stream_map2 f s1 s2 = |
386 stream_unfold (\<lambda>(s1,s2). f (shd s1) (shd s2)) (map_pair stl stl) (s1, s2)" |
421 stream_unfold (\<lambda>(s1,s2). f (shd s1) (shd s2)) (map_pair stl stl) (s1, s2)" |
387 |
422 |
388 lemma stream_map2_simps[simp]: |
423 lemma stream_map2_simps[simp]: |
389 "shd (stream_map2 f s1 s2) = f (shd s1) (shd s2)" |
424 "shd (stream_map2 f s1 s2) = f (shd s1) (shd s2)" |
390 "stl (stream_map2 f s1 s2) = stream_map2 f (stl s1) (stl s2)" |
425 "stl (stream_map2 f s1 s2) = stream_map2 f (stl s1) (stl s2)" |
391 unfolding stream_map2_def by auto |
426 unfolding stream_map2_def by auto |
|
427 |
|
428 lemma stream_map2_unfold[code]: |
|
429 "stream_map2 f (Stream a s1) (Stream b s2) = Stream (f a b) (stream_map2 f s1 s2)" |
|
430 unfolding stream_map2_def by (subst stream.unfold) simp |
392 |
431 |
393 lemma stream_map2_szip: |
432 lemma stream_map2_szip: |
394 "stream_map2 f s1 s2 = stream_map (split f) (szip s1 s2)" |
433 "stream_map2 f s1 s2 = stream_map (split f) (szip s1 s2)" |
395 by (coinduct rule: stream.coinduct[of |
434 by (coinduct rule: stream.coinduct[of |
396 "\<lambda>s1 s2. \<exists>s1' s2'. s1 = stream_map2 f s1' s2' \<and> s2 = stream_map (split f) (szip s1' s2')"]) |
435 "\<lambda>s1 s2. \<exists>s1' s2'. s1 = stream_map2 f s1' s2' \<and> s2 = stream_map (split f) (szip s1' s2')"]) |