| author | wenzelm | 
| Sat, 01 Jun 2024 16:26:35 +0200 | |
| changeset 80234 | cce5670be9f9 | 
| parent 66453 | cc19f7ca2ed6 | 
| permissions | -rw-r--r-- | 
| 62725 | 1 | (* Title: HOL/Corec_Examples/Tests/Stream_Friends.thy | 
| 2 | Author: Aymeric Bouzy, Ecole polytechnique | |
| 3 | Author: Jasmin Blanchette, Inria, LORIA, MPII | |
| 4 | Copyright 2015, 2016 | |
| 5 | ||
| 6 | Friendly functions on streams. | |
| 7 | *) | |
| 8 | ||
| 9 | section \<open>Friendly Functions on Streams\<close> | |
| 10 | ||
| 11 | theory Stream_Friends | |
| 66453 
cc19f7ca2ed6
session-qualified theory imports: isabelle imports -U -i -d '~~/src/Benchmarks' -a;
 wenzelm parents: 
62732diff
changeset | 12 | imports "HOL-Library.BNF_Corec" | 
| 62725 | 13 | begin | 
| 14 | ||
| 15 | codatatype 'a stream = SCons (shd: 'a) (stl: "'a stream") | |
| 16 | ||
| 17 | corec pls :: "nat stream \<Rightarrow> nat stream \<Rightarrow> nat stream" where | |
| 18 | "pls s s' = SCons (shd s + shd s') (pls (stl s) (stl s'))" | |
| 19 | ||
| 20 | friend_of_corec pls :: "nat stream \<Rightarrow> nat stream \<Rightarrow> nat stream" where | |
| 21 | "pls s s' = SCons (shd s + shd s') (pls (stl s) (stl s'))" | |
| 22 | sorry | |
| 23 | ||
| 24 | corec onetwo :: "nat stream" where | |
| 25 | "onetwo = SCons 1 (SCons 2 onetwo)" | |
| 26 | ||
| 27 | corec prd :: "nat stream \<Rightarrow> nat stream \<Rightarrow> nat stream" where | |
| 28 | "prd xs ys = SCons (shd xs * shd ys) (pls (prd xs (stl ys)) (prd (stl xs) ys))" | |
| 29 | ||
| 30 | friend_of_corec prd where | |
| 31 | "prd xs ys = SCons (shd xs * shd ys) (pls (prd xs (stl ys)) (prd (stl xs) ys))" | |
| 32 | sorry | |
| 33 | ||
| 34 | corec Exp :: "nat stream \<Rightarrow> nat stream" where | |
| 35 | "Exp xs = SCons (2 ^^ shd xs) (prd (stl xs) (Exp xs))" | |
| 36 | ||
| 37 | friend_of_corec Exp where | |
| 38 | "Exp xs = SCons (2 ^^ shd xs) (prd (stl xs) (Exp xs))" | |
| 39 | sorry | |
| 40 | ||
| 41 | corec prd2 :: "nat stream \<Rightarrow> nat stream \<Rightarrow> nat stream" where | |
| 42 | "prd2 xs ys = SCons (shd xs * shd ys) (pls (prd xs (stl ys)) (prd2 (stl xs) ys))" | |
| 43 | ||
| 44 | fun sthe_default :: "'a stream \<Rightarrow> nat \<Rightarrow> 'a stream option \<Rightarrow> 'a stream" where | |
| 45 | "sthe_default s _ None = s" | |
| 46 | | "sthe_default _ _ (Some t) = t" | |
| 47 | ||
| 62730 | 48 | (* FIXME: | 
| 62725 | 49 | friend_of_corec sthe_default where | 
| 50 | "sthe_default s n opt = SCons (shd (case opt of None \<Rightarrow> s | Some t \<Rightarrow> t)) (stl (case opt of None \<Rightarrow> s | Some t \<Rightarrow> t))" | |
| 51 | sorry | |
| 52 | ||
| 53 | corec funky0 :: "nat \<Rightarrow> nat stream" where | |
| 54 | "funky0 n = SCons 0 (sthe_default undefined n (map_option funky0 undefined))" | |
| 55 | ||
| 56 | consts funky0' :: "nat stream \<Rightarrow> nat stream" | |
| 57 | ||
| 58 | friend_of_corec funky0' :: "nat stream \<Rightarrow> nat stream" where | |
| 59 | "funky0' ns = SCons 0 (sthe_default undefined (shd ns) (map_option funky0' undefined))" | |
| 60 | sorry | |
| 61 | ||
| 62 | corec funky :: "nat \<Rightarrow> nat stream" where | |
| 63 | "funky n = SCons 0 (sthe_default (funky (n + 1)) n (map_option funky (Some (n + 2))))" | |
| 64 | ||
| 65 | corec funky' :: "nat \<Rightarrow> nat \<Rightarrow> nat stream" where | |
| 66 | "funky' m n = SCons 0 (sthe_default (funky' (m - 1) (n + 1)) m (map_option (%(x, y). funky' (Suc x) (Suc y)) (Some (m - 2, n + 2))))" | |
| 67 | ||
| 68 | corec funky'' :: "nat \<Rightarrow> nat stream" where | |
| 69 | "funky'' n = SCons 0 (sthe_default (funky'' (n + 1)) n (Some (funky'' (n + 2))))" | |
| 70 | ||
| 71 | corec phunky0 :: "nat \<Rightarrow> nat stream" where | |
| 72 | "phunky0 n = sthe_default undefined n undefined" | |
| 73 | ||
| 74 | fun lthe_default :: "'a stream \<Rightarrow> 'a stream option \<Rightarrow> 'a stream" where | |
| 75 | "lthe_default s None = s" | |
| 76 | | "lthe_default _ (Some t) = t" | |
| 77 | ||
| 78 | friend_of_corec lthe_default where | |
| 79 | "lthe_default s opt = SCons (shd (case opt of None \<Rightarrow> s | Some t \<Rightarrow> t)) (stl (case opt of None \<Rightarrow> s | Some t \<Rightarrow> t))" | |
| 80 | sorry | |
| 81 | ||
| 82 | corec phunky_debug :: "'a \<Rightarrow> 'a stream" where | |
| 83 | "phunky_debug n = lthe_default (SCons n (lthe_default undefined (map_option phunky_debug undefined))) undefined" | |
| 84 | ||
| 85 | corec phunky1 :: "nat \<Rightarrow> nat stream" where | |
| 86 | "phunky1 n = sthe_default (SCons 0 (sthe_default (phunky1 (n + 1)) n (map_option phunky1 (Some (n + 2))))) n undefined" | |
| 87 | ||
| 88 | corec phunky2 :: "nat \<Rightarrow> nat stream" where | |
| 89 | "phunky2 n = sthe_default (SCons 0 (sthe_default (phunky2 (n + 1)) n (map_option phunky2 (Some (n + 2))))) n | |
| 90 | (map_option (%m. SCons m (sthe_default (phunky2 (n + 1)) n (map_option phunky2 (Some (n + 2))))) (Some n))" | |
| 91 | ||
| 92 | corec phunky3 :: "nat \<Rightarrow> nat stream" where | |
| 93 | "phunky3 n = sthe_default (SCons 0 (sthe_default (phunky3 (n + 1)) n (map_option phunky3 (Some (n + 3))))) n | |
| 94 | (Some (SCons n (sthe_default (phunky3 (n + 1)) n (map_option phunky3 (Some (n + 3))))))" | |
| 62732 | 95 | *) | 
| 62725 | 96 | |
| 97 | end |