Skip to content

Commit f2c0e3c

Browse files
committed
Use lazy
1 parent 25b2087 commit f2c0e3c

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

src/Control/Comonad/Cofree.purs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ module Control.Comonad.Cofree
1010

1111
import Prelude
1212

13-
import Control.Comonad (class Comonad)
1413
import Control.Alternative (class Alternative, (<|>), empty)
14+
import Control.Comonad (class Comonad)
1515
import Control.Extend (class Extend)
16+
1617
import Data.Foldable (class Foldable, foldr, foldl, foldMap)
18+
import Data.Lazy (Lazy, force, defer)
1719
import Data.Traversable (class Traversable, traverse)
1820

1921
-- | The `Cofree` `Comonad` for a functor.
@@ -23,12 +25,12 @@ import Data.Traversable (class Traversable, traverse)
2325
-- |
2426
-- | The `Comonad` instance supports _redecoration_, recomputing
2527
-- | 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)))
2729

2830
-- | Create a value of type `Cofree f a` from a label and a
2931
-- | functor-full of "subtrees".
3032
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)
3234

3335
infixr 5 mkCofree as :<
3436

@@ -38,12 +40,12 @@ head (Cofree h _) = h
3840

3941
-- | Returns the "subtrees" of a tree.
4042
tail :: forall f a. Cofree f a -> f (Cofree f a)
41-
tail (Cofree _ t) = t unit
43+
tail (Cofree _ t) = force t
4244

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))
4446
_tail (Cofree _ t) = t
4547

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)
4749
_lift = map <<< map
4850

4951
hoistCofree :: forall f g. Functor f => (f ~> g) -> Cofree f ~> Cofree g
@@ -56,7 +58,8 @@ unfoldCofree
5658
-> (s -> a)
5759
-> (s -> f s)
5860
-> 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))
6063

6164
instance functorCofree :: Functor f => Functor (Cofree f) where
6265
map f = loop where

0 commit comments

Comments
 (0)