monadic-recursion-schemes 0.1.0.0 → 0.1.1.0
raw patch · 2 files changed
+62/−21 lines, 2 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
+ Data.Functor.Foldable.Monadic: futuM' :: (Monad m, Traversable (Base t), Corecursive t) => (a -> m (Base t (Free (Base t) a))) -> a -> m t
+ Data.Functor.Foldable.Monadic: histoM' :: (Monad m, Traversable (Base t), Recursive t) => (Base t (Cofree (Base t) a) -> m a) -> t -> m a
Files
monadic-recursion-schemes.cabal view
@@ -4,7 +4,7 @@ -- http://haskell.org/cabal/users-guide/ name: monadic-recursion-schemes-version: 0.1.0.0+version: 0.1.1.0 synopsis: Recursion Schemes for Monadic version. description: Yet another recursion schemes for monadic style, depends on recursion-schemes. homepage: https://github.com/cutsea110/monadic-recursion-schemes.git
src/Data/Functor/Foldable/Monadic.hs view
@@ -7,62 +7,103 @@ ( cataM, anaM , paraM, apoM , histoM, futuM+ , histoM', futuM' , zygoM, cozygoM , hyloM ) where -import Control.Comonad (Comonad (..))-import Control.Comonad.Cofree (Cofree (..))-import Control.Monad ((<=<), liftM2)-import Control.Monad.Free (Free (..))-import Control.Monad.Trans.Class (lift)-import Control.Monad.Trans.Reader (ReaderT, ask, runReaderT)-import Data.Functor.Foldable (Recursive (..), Corecursive (..), Base, Fix (..))+import Control.Comonad (Comonad (..))+import Control.Comonad.Cofree (Cofree (..))+import qualified Control.Comonad.Trans.Cofree as Cf (CofreeF (..))+import Control.Monad ((<=<), liftM2)+import Control.Monad.Free (Free (..))+import qualified Control.Monad.Trans.Free as Fr (FreeF (..))+import Control.Monad.Trans.Class (lift)+import Control.Monad.Trans.Reader (ReaderT, ask, runReaderT)+import Data.Functor.Foldable (Recursive (..), Corecursive (..), Base, Fix (..)) +-- | catamorphism cataM :: (Monad m, Traversable (Base t), Recursive t)- => (Base t a -> m a) -> t -> m a+ => (Base t a -> m a) -- ^ algebra+ -> t -> m a cataM phi = h where h = phi <=< mapM h . project +-- | anamorphism anaM :: (Monad m, Traversable (Base t), Corecursive t)- => (a -> m (Base t a)) -> a -> m t+ => (a -> m (Base t a)) -- ^ coalgebra+ -> a -> m t anaM psi = h where h = (return . embed) <=< mapM h <=< psi +-- | paramorphism paraM :: (Monad m, Traversable (Base t), Recursive t)- => (Base t (t, a) -> m a) -> t -> m a+ => (Base t (t, a) -> m a) -- ^ algebra+ -> t -> m a paraM phi = h where h = phi <=< mapM (liftM2 (,) <$> return <*> h) . project +-- | apomorphism apoM :: (Monad m, Traversable (Base t), Corecursive t)- => (a -> m (Base t (Either t a))) -> a -> m t+ => (a -> m (Base t (Either t a))) -- ^ coalgebra+ -> a -> m t apoM psi = h where h = (return . embed) <=< mapM (either return h) <=< psi +-- | histomorphism on recursion variant histoM :: (Monad m, Traversable (Base t), Recursive t)- => (Base t (Cofree (Base t) a) -> m a) -> t -> m a-histoM phi = return . extract <=< cataM f- where f = return . uncurry (:<) <=< (liftM2 (,) <$> phi <*> return)+ => (Base t (Cofree (Base t) a) -> m a) -- ^ algebra+ -> t -> m a+histoM phi = h+ where h = phi <=< mapM f . project+ f = anaM (liftM2 (Cf.:<) <$> h <*> (return . project)) +-- | histomorphism on catamorphism variant+histoM' :: (Monad m, Traversable (Base t), Recursive t)+ => (Base t (Cofree (Base t) a) -> m a)+ -> t -> m a+histoM' phi = return . extract <=< cataM f+ where f = return . uncurry (:<) <=< (liftM2 (,) <$> phi <*> return)++-- | futumorphism on recursion variant futuM :: (Monad m, Traversable (Base t), Corecursive t)- => (a -> m (Base t (Free (Base t) a))) -> a -> m t-futuM psi = anaM f . Pure+ => (a -> m (Base t (Free (Base t) a))) -- ^ coalgebra+ -> a -> m t+futuM psi = h+ where h = (return . embed) <=< mapM f <=< psi+ f = cataM $ \case+ Fr.Pure a -> h a+ Fr.Free fb -> return (embed fb)++-- | futumorphism on anamorphism variant+futuM' :: (Monad m, Traversable (Base t), Corecursive t)+ => (a -> m (Base t (Free (Base t) a))) -- ^ coalgebra+ -> a -> m t+futuM' psi = anaM f . Pure where f (Pure a) = psi a f (Free fb) = return fb +-- | zygomorphism zygoM :: (Monad m, Traversable (Base t), Recursive t)- => (Base t a -> m a) -> (Base t (a, b) -> m b) -> t -> m b+ => (Base t a -> m a) -- ^ algebra for fst+ -> (Base t (a, b) -> m b) -- ^ algebra for snd from product+ -> t -> m b zygoM f phi = return . snd <=< cataM g where g = liftM2 (,) <$> (f <=< return . fmap fst) <*> phi +-- | cozygomorphism cozygoM :: (Monad m, Traversable (Base t), Corecursive t)- => (a -> m (Base t a)) -> (b -> m (Base t (Either a b))) -> b -> m t+ => (a -> m (Base t a)) -- ^ coalgebra for fst+ -> (b -> m (Base t (Either a b))) -- ^ coalgebra for snd to coproduct+ -> b -> m t cozygoM f psi = anaM g . Right where g = either (return . fmap Left <=< f) psi +-- | hylomorphism hyloM :: (Monad m, Traversable t)- => (t b -> m b) -> (a -> m (t a)) -> a -> m b+ => (t b -> m b) -- ^ algebra+ -> (a -> m (t a)) -- ^ coalgebra+ -> a -> m b hyloM phi psi = h where h = phi <=< mapM h <=< psi-