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

fromFree #181

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/Control/Monad/Free.hs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ module Control.Monad.Free
, iterM
, hoistFree
, foldFree
, fromFree
, toFreeT
, cutoff
, unfold
Expand Down Expand Up @@ -365,6 +366,11 @@ foldFree :: Monad m => (forall x . f x -> m x) -> Free f a -> m a
foldFree _ (Pure a) = return a
foldFree f (Free as) = f as >>= foldFree f

-- | Convert a 'Free' monad to an arbitrary 'MonadFree' instance.
fromFree :: (Functor f, MonadFree f m) => Free f a -> m a
fromFree (Pure x) = return x
fromFree (Free ff) = wrap (fromFree <$> ff)

-- | Convert a 'Free' monad from "Control.Monad.Free" to a 'FreeT.FreeT' monad
-- from "Control.Monad.Trans.Free".
toFreeT :: (Functor f, Monad m) => Free f a -> FreeT.FreeT f m a
Expand Down
2 changes: 2 additions & 0 deletions src/Control/Monad/Free/Church.hs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,8 @@ fromF (F m) = m return wrap
{-# INLINE fromF #-}

-- | Generate a Church-encoded free monad from a 'Free' monad.
--
-- @toF = fromFree@
toF :: Functor f => Free f a -> F f a
toF xs = F (\kp kf -> go kp kf xs) where
go kp _ (Pure a) = kp a
Expand Down