@@ -7,11 +7,12 @@ module Control.Applicative.Free
77 , analyzeFreeAp
88 ) where
99
10- import Prelude ( class Applicative , class Apply , class Functor , type (~>), Unit , (<<<), apply , flip , id , map , pure , unit )
10+ import Prelude hiding ( ap )
1111
12- import Data.Const (Const (Const), getConst )
12+ import Data.Const (Const (..) )
1313import Data.Exists (Exists , mkExists , runExists )
1414import Data.Monoid (class Monoid )
15+ import Data.Newtype (unwrap )
1516
1617-- | The free applicative functor for a type constructor `f`.
1718data FreeAp f a = Pure a | Ap (Exists (ApF f a ))
@@ -28,13 +29,13 @@ liftFreeAp a = ap (\_ -> a) (\_ -> Pure id)
2829
2930-- | Run a free applicative functor using the applicative instance for
3031-- | the type constructor `f`.
31- retractFreeAp :: forall f a . ( Applicative f ) => FreeAp f a -> f a
32+ retractFreeAp :: forall f a . Applicative f => FreeAp f a -> f a
3233retractFreeAp (Pure a) = pure a
3334retractFreeAp (Ap x) = runExists (\(ApF v k') -> apply (retractFreeAp (k' unit)) (v unit)) x
3435
3536-- | Run a free applicative functor with a natural transformation from
3637-- | the type constructor `f` to the applicative functor `g`.
37- foldFreeAp :: forall f g a . ( Applicative g ) => (f ~> g ) -> FreeAp f a -> g a
38+ foldFreeAp :: forall f g a . Applicative g => (f ~> g ) -> FreeAp f a -> g a
3839foldFreeAp k (Pure a) = pure a
3940foldFreeAp k (Ap x) = runExists (\(ApF v k') -> apply (map (flip id) (k (v unit))) (foldFreeAp k (k' unit))) x
4041
@@ -45,8 +46,8 @@ hoistFreeAp k (Pure a) = Pure a
4546hoistFreeAp k (Ap x) = runExists (\(ApF v k') -> ap (\_ -> k (v unit)) (\_ -> hoistFreeAp k (k' unit))) x
4647
4748-- | Perform monoidal analysis over the free applicative functor `f`.
48- analyzeFreeAp :: forall f m a . ( Monoid m ) => (forall b . f b -> m ) -> FreeAp f a -> m
49- analyzeFreeAp k = getConst <<< foldFreeAp (Const <<< k)
49+ analyzeFreeAp :: forall f m a . Monoid m => (forall b . f b -> m ) -> FreeAp f a -> m
50+ analyzeFreeAp k = unwrap <<< foldFreeAp (Const <<< k)
5051
5152instance functorFreeAp :: Functor (FreeAp f ) where
5253 map k (Pure a) = Pure (k a)
0 commit comments