Open idontgetoutmuch opened 3 years ago
import Control.Monad.Free.Church
import Control.Monad.Prompt
fK :: Functor p => p r -> F p r
fK p = F (\f c -> c (fmap f p))
bToE :: Functor p => Prompt p a -> F p a
bToE p = runPromptM fK p
eToD :: F p a -> Prompt p a
eToD f = foldF prompt f
Even better I can replace fK
by liftF
import Control.Monad.Free.Church
import Control.Monad.Prompt
import qualified System.IO as IO
bToE :: Functor p => Prompt p a -> F p a
bToE p = runPromptM liftF p
eToB :: F p a -> Prompt p a
eToB f = foldF prompt f
-- Example
data Terminal a
= GetLine (String -> a)
| PrintLine String a
instance Functor Terminal where
fmap f (GetLine g) = GetLine (f . g)
fmap f (PrintLine s x) = PrintLine s (f x)
myProgramE :: F Terminal ()
myProgramE = do
a <- liftF (GetLine id)
b <- liftF (GetLine id)
liftF (PrintLine (a ++ b) ())
myProgramB :: Prompt Terminal ()
myProgramB = do
a <- prompt (GetLine id)
b <- prompt (GetLine id)
prompt (PrintLine (a ++ b) ())
f :: Terminal a -> IO a
f (GetLine next) = next <$> IO.getLine
f (PrintLine str next) = next <$ putStrLn str
interpretE :: F Terminal a -> IO a
interpretE = foldF f
interpretB :: Prompt Terminal a -> IO a
interpretB = runPromptM f
@int-e do you think it's possible to replace PromptMonad by Free? I've played around a bit and have
and
Perhaps it's just not possible to do this without thinking harder.