Created
February 14, 2017 13:41
-
-
Save romac/5cb7020fb73c6a88a6d4c36f1da094b7 to your computer and use it in GitHub Desktop.
Data.Functor.Foldable.Monadic
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE RankNTypes #-} | |
{-# LANGUAGE FlexibleContexts #-} | |
module Data.Functor.Foldable.Monadic | |
( cataM | |
, anaM | |
, futuM | |
) where | |
import Protolude | |
import Data.Functor.Foldable (Base, Recursive, Corecursive, project, embed, ana) | |
import Control.Monad (liftM, join, (<=<)) | |
import Control.Monad.Free (Free(..)) | |
cataM :: (Recursive t, Monad m, Traversable (Base t)) | |
=> (Base t a -> m a) | |
-> t | |
-> m a | |
cataM alg t = alg =<< traverse (cataM alg) (project t) | |
anaM :: (Corecursive t, Traversable (Base t), Monad m) | |
=> (a -> m (Base t a)) | |
-> a | |
-> m t | |
anaM coalg = go | |
where go a = fmap embed (coalg a >>= mapM go) | |
futuM :: (Corecursive t, Traversable (Base t), Monad m) | |
=> (a -> m (Base t (Free (Base t) a))) | |
-> a | |
-> m t | |
futuM coalg = anaM go . Pure | |
where | |
go (Pure a) = coalg a | |
go (Free fa) = return fa |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment