11module Control.Applicative.Free
2- ( FreeAp ()
3- , NaturalTransformation ()
2+ ( FreeAp
43 , liftFreeAp
54 , retractFreeAp
65 , foldFreeAp
76 , hoistFreeAp
87 , analyzeFreeAp
98 ) where
109
11- import Prelude (Applicative , Apply , Functor , Unit () , (<<<), apply , flip , id , map , pure , unit )
10+ import Prelude (class Applicative , class Apply , class Functor , type (~>), Unit , (<<<), apply , flip , id , map , pure , unit )
1211
13- import Data.Const (Const (.. ), getConst )
14- import Data.Exists (Exists () , mkExists , runExists )
15- import Data.Monoid (Monoid )
12+ import Data.Const (Const (Const ), getConst )
13+ import Data.Exists (Exists , mkExists , runExists )
14+ import Data.Monoid (class Monoid )
1615
1716-- | The free applicative functor for a type constructor `f`.
1817data FreeAp f a = Pure a | Ap (Exists (ApF f a ))
1918
2019data ApF f a i = ApF (Unit -> f i ) (Unit -> FreeAp f (i -> a ))
2120
22- type NaturalTransformation f g = forall a . f a -> g a
23-
2421ap :: forall f a i . (Unit -> f i ) -> (Unit -> FreeAp f (i -> a )) -> FreeAp f a
2522ap v k = Ap (mkExists (ApF v k))
2623
@@ -37,13 +34,13 @@ retractFreeAp (Ap x) = runExists (\(ApF v k') -> apply (retractFreeAp (k' unit))
3734
3835-- | Run a free applicative functor with a natural transformation from
3936-- | the type constructor `f` to the applicative functor `g`.
40- foldFreeAp :: forall f g a . (Applicative g ) => NaturalTransformation f g -> FreeAp f a -> g a
37+ foldFreeAp :: forall f g a . (Applicative g ) => ( f ~> g ) -> FreeAp f a -> g a
4138foldFreeAp k (Pure a) = pure a
4239foldFreeAp k (Ap x) = runExists (\(ApF v k') -> apply (map (flip id) (k (v unit))) (foldFreeAp k (k' unit))) x
4340
4441-- | Natural transformation from `FreeAp f a` to `FreeAp g a` given a
4542-- | natural transformation from `f` to `g`.
46- hoistFreeAp :: forall f g a . NaturalTransformation f g -> FreeAp f a -> FreeAp g a
43+ hoistFreeAp :: forall f g a . ( f ~> g ) -> FreeAp f a -> FreeAp g a
4744hoistFreeAp k (Pure a) = Pure a
4845hoistFreeAp k (Ap x) = runExists (\(ApF v k') -> ap (\_ -> k (v unit)) (\_ -> hoistFreeAp k (k' unit))) x
4946
0 commit comments