From a8b4ede608f52b7cca91e414f37aba4ed9e3d222 Mon Sep 17 00:00:00 2001 From: David Feuer Date: Thu, 25 Oct 2018 17:18:37 -0400 Subject: [PATCH] fromFree This is the `Free` analogue of `fromF`. I believe it's a valid (but possibly inefficient) implementation of `toF`. --- src/Control/Monad/Free.hs | 6 ++++++ src/Control/Monad/Free/Church.hs | 2 ++ 2 files changed, 8 insertions(+) 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