Skip to content

Add unfoldCofree #57

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions src/Control/Comonad/Cofree.purs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import Prelude
import Control.Comonad (class Comonad)
import Control.Alternative (class Alternative, (<|>), empty)
import Control.Extend (class Extend)
import Control.Monad.Trampoline (Trampoline, runTrampoline)

import Data.Foldable (class Foldable, foldr, foldl, foldMap)
import Data.Traversable (class Traversable, traverse)

Expand All @@ -25,12 +23,12 @@ import Data.Traversable (class Traversable, traverse)
-- |
-- | The `Comonad` instance supports _redecoration_, recomputing
-- | labels from the local context.
data Cofree f a = Cofree a (Trampoline (f (Cofree f a)))
data Cofree f a = Cofree a (Unit -> f (Cofree f a))

-- | Create a value of type `Cofree f a` from a label and a
-- | functor-full of "subtrees".
mkCofree :: forall f a. a -> (f (Cofree f a)) -> Cofree f a
mkCofree a t = Cofree a (pure t)
mkCofree :: forall f a. a -> f (Cofree f a) -> Cofree f a
mkCofree a t = Cofree a \_ -> t

infixr 5 mkCofree as :<

Expand All @@ -40,17 +38,26 @@ head (Cofree h _) = h

-- | Returns the "subtrees" of a tree.
tail :: forall f a. Cofree f a -> f (Cofree f a)
tail (Cofree _ t) = runTrampoline t
tail (Cofree _ t) = t unit

_tail :: forall f a. Cofree f a -> Trampoline (f (Cofree f a))
_tail :: forall f a. Cofree f a -> Unit -> f (Cofree f a)
_tail (Cofree _ t) = t

_lift :: forall f a b. Functor f => (a -> b) -> Trampoline (f a) -> Trampoline (f b)
_lift :: forall f a b. Functor f => (a -> b) -> (Unit -> f a) -> Unit -> f b
_lift = map <<< map

hoistCofree :: forall f g. Functor f => (f ~> g) -> Cofree f ~> Cofree g
hoistCofree nat cf = head cf :< nat (hoistCofree nat <$> tail cf)

unfoldCofree
:: forall f s a
. Functor f
=> s
-> (s -> a)
-> (s -> f s)
-> Cofree f a
unfoldCofree s e n = Cofree (e s) \u -> map (\s1 -> unfoldCofree s1 e n) (n s)

instance functorCofree :: Functor f => Functor (Cofree f) where
map f = loop where
loop fa = Cofree (f (head fa)) (_lift loop (_tail fa))
Expand Down