diff --git a/src/Control/Monad/Free.hs b/src/Control/Monad/Free.hs index 33b2f42..06fd088 100644 --- a/src/Control/Monad/Free.hs +++ b/src/Control/Monad/Free.hs @@ -31,6 +31,7 @@ module Control.Monad.Free , iterM , hoistFree , foldFree + , fromFree , toFreeT , cutoff , unfold @@ -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 diff --git a/src/Control/Monad/Free/Church.hs b/src/Control/Monad/Free/Church.hs index caa1dc0..d73866c 100644 --- a/src/Control/Monad/Free/Church.hs +++ b/src/Control/Monad/Free/Church.hs @@ -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