@@ -10,10 +10,12 @@ module Control.Comonad.Cofree
10
10
11
11
import Prelude
12
12
13
- import Control.Comonad (class Comonad )
14
13
import Control.Alternative (class Alternative , (<|>), empty )
14
+ import Control.Comonad (class Comonad )
15
15
import Control.Extend (class Extend )
16
+
16
17
import Data.Foldable (class Foldable , foldr , foldl , foldMap )
18
+ import Data.Lazy (Lazy , force , defer )
17
19
import Data.Traversable (class Traversable , traverse )
18
20
19
21
-- | The `Cofree` `Comonad` for a functor.
@@ -23,12 +25,12 @@ import Data.Traversable (class Traversable, traverse)
23
25
-- |
24
26
-- | The `Comonad` instance supports _redecoration_, recomputing
25
27
-- | labels from the local context.
26
- data Cofree f a = Cofree a (Unit -> f (Cofree f a ))
28
+ data Cofree f a = Cofree a (Lazy ( f (Cofree f a ) ))
27
29
28
30
-- | Create a value of type `Cofree f a` from a label and a
29
31
-- | functor-full of "subtrees".
30
32
mkCofree :: forall f a . a -> f (Cofree f a ) -> Cofree f a
31
- mkCofree a t = Cofree a \_ -> t
33
+ mkCofree a t = Cofree a (defer \_ -> t)
32
34
33
35
infixr 5 mkCofree as :<
34
36
@@ -38,12 +40,12 @@ head (Cofree h _) = h
38
40
39
41
-- | Returns the "subtrees" of a tree.
40
42
tail :: forall f a . Cofree f a -> f (Cofree f a )
41
- tail (Cofree _ t) = t unit
43
+ tail (Cofree _ t) = force t
42
44
43
- _tail :: forall f a . Cofree f a -> Unit -> f (Cofree f a )
45
+ _tail :: forall f a . Cofree f a -> Lazy ( f (Cofree f a ) )
44
46
_tail (Cofree _ t) = t
45
47
46
- _lift :: forall f a b . Functor f => (a -> b ) -> ( Unit -> f a ) -> Unit -> f b
48
+ _lift :: forall f a b . Functor f => (a -> b ) -> Lazy ( f a ) -> Lazy ( f b )
47
49
_lift = map <<< map
48
50
49
51
hoistCofree :: forall f g . Functor f => (f ~> g ) -> Cofree f ~> Cofree g
@@ -56,7 +58,8 @@ unfoldCofree
56
58
-> (s -> a )
57
59
-> (s -> f s )
58
60
-> Cofree f a
59
- unfoldCofree s e n = Cofree (e s) \u -> map (\s1 -> unfoldCofree s1 e n) (n s)
61
+ unfoldCofree s e n =
62
+ Cofree (e s) (defer \u -> map (\s1 -> unfoldCofree s1 e n) (n s))
60
63
61
64
instance functorCofree :: Functor f => Functor (Cofree f ) where
62
65
map f = loop where
0 commit comments