Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Church-encoded free monad transformer + recursion schemes #14

Closed
roshanshariff opened this issue Mar 31, 2013 · 2 comments
Closed

Church-encoded free monad transformer + recursion schemes #14

roshanshariff opened this issue Mar 31, 2013 · 2 comments

Comments

@roshanshariff
Copy link
Collaborator

Code here

This is a first attempt at a Church-encoded free monad transformer, represented by the type:

newtype FT f m a = FT { runFT :: forall r. (a -> m r) -> (f (m r) -> m r) -> m r }

The module includes equivalents of hoistFreeT and transFreeT from Control.Monad.Trans.Free.

There are also three recursion schemes:

cataFT :: (a -> m b) -> (f (m b) -> m b) -> FT f m a -> m b
cataFT kp kf ft = runFT ft kp kf

foldFT :: (Traversable f, Monad m) => (a -> m b) -> (f b -> m b) -> FT f m a -> m b
foldFT kp kf = cataFT kp $ kf <=< sequence

sequenceFT :: (Traversable f, Monad m, MonadFree f n) => FT f m a -> m (n a)
sequenceFT = foldFT (return . return) (return . wrap)

The code also adds analogues of these recursion schemes for the existing FreeT class. The isomorphism of FreeT and FT is witnessed by the functions toFreeT and fromFreeT.

If the cataFT and cataFreeT definitions can be moved to a type class for free monad transformers (the transformer version of MonadFree), the recursion schemes can be written generically. I'm not sure if this is a good design choice.

There are some more functions in the control-monad-free package not yet included in free. I think it would be worth porting over those that are likely to be useful.

(Note: This code is a first draft. There are only the bare minimum of type class instances, no documentation, and not very good names for the new types and functions.)

@ekmett
Copy link
Owner

ekmett commented Apr 1, 2013

Sold. Now it is just a matter of getting all the instances written.

@ekmett
Copy link
Owner

ekmett commented Mar 5, 2014

Closing this as we merged it in ~4 months ago.

@ekmett ekmett closed this as completed Mar 5, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants