Skip to content

Commit

Permalink
fromFree
Browse files Browse the repository at this point in the history
This is the `Free` analogue of `fromF`. I believe it's a valid
(but possibly inefficient) implementation of `toF`.
  • Loading branch information
treeowl committed Nov 15, 2018
1 parent fe35b81 commit a8b4ede
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
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

0 comments on commit a8b4ede

Please sign in to comment.