@@ -11,11 +11,12 @@ module Control.Comonad.Cofree
11
11
) where
12
12
13
13
import Prelude
14
- import Control.Monad.Free (Free , runFreeM )
15
14
import Control.Alternative (class Alternative , (<|>), empty )
16
15
import Control.Comonad (class Comonad , extract )
17
16
import Control.Extend (class Extend )
18
- import Control.Monad.State (State , runState , state )
17
+ import Control.Monad.Free (Free , runFreeM )
18
+ import Control.Monad.Rec.Class (class MonadRec )
19
+ import Control.Monad.State (State , StateT (..), runState , runStateT , state )
19
20
import Data.Foldable (class Foldable , foldr , foldl , foldMap )
20
21
import Data.Lazy (Lazy , force , defer )
21
22
import Data.Traversable (class Traversable , traverse )
@@ -83,6 +84,22 @@ explore pair m w =
83
84
step :: f (Free f (a -> b )) -> State (Cofree g a ) (Free f (a -> b ))
84
85
step ff = state \cof -> pair (map Tuple ff) (tail cof)
85
86
87
+ exploreM
88
+ :: forall f g a b m
89
+ . (Functor f , Functor g , MonadRec m )
90
+ => (forall x y . f (x -> y ) -> g x -> m y )
91
+ -> Free f (a -> b )
92
+ -> Cofree g a
93
+ -> m b
94
+ exploreM pair m w =
95
+ eval <$> runStateT (runFreeM step m) w
96
+ where
97
+ step :: f (Free f (a -> b )) -> StateT (Cofree g a ) m (Free f (a -> b ))
98
+ step ff = StateT \cof -> pair (map Tuple ff) (tail cof)
99
+
100
+ eval :: forall x y . Tuple (x -> y ) (Cofree g x ) -> y
101
+ eval (Tuple f cof) = f (extract cof)
102
+
86
103
instance eqCofree :: (Eq (f (Cofree f a )), Eq a ) => Eq (Cofree f a ) where
87
104
eq x y = head x == head y && tail x == tail y
88
105
0 commit comments