Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
some freer manipulations
{-#LANGUAGE GADTs, TypeApplications, DataKinds, TypeOperators #-}
import Control.Monad.Freer
import qualified Control.Monad.Freer as F
import Control.Monad.Trans.Writer
import Control.Monad.IO.Class
type Loc = Int
type LogSource = Int
type LogLevel = Int
type LogStr = String
data Logger v where
Log :: Loc -> LogSource -> LogLevel -> LogStr -> Logger ()
f1 :: (Member (WriterT [String] IO) r) => String -> Eff r ()
f1 str = send (tell [str] :: WriterT [String] IO ())
f2 :: (Member (WriterT [String] IO) r) => String -> Eff r ()
f2 str = send (tell @IO [str])
g :: (Member Logger r) => String -> Eff r ()
g str = send (Log 0 0 0 str)
h1 :: (Member (WriterT [String] IO) r, Member Logger r) => Eff r ()
h1 = do
g "hi"
f2 "hi"
f1 "hi"
main1 = do
ls <- execWriterT $ runM $ writerLogger h1 :: IO [String]
mapM_ putStrLn ls
h2 :: (Member (WriterT [String] IO) r, Member Logger r) => Eff r ()
h2 = do
send $ (liftIO $ putStrLn "interpolated IO!" :: WriterT [String] IO ())
h1
send $ (liftIO $ putStrLn "more IO!" :: WriterT [String] IO ())
main2 = do
ls <- execWriterT $ runM $ writerLogger h2 :: IO [String]
mapM_ putStrLn ls
writerLogger :: (Member (WriterT [String] IO) r) => Eff (Logger:r) a -> Eff r a
writerLogger = handleRelay return (\(Log _ _ _ str) f -> f1 str >>= f)
program :: Member Logger r => Eff r ()
program = g "hi" >> g "ho"
main3 = do
ls <- execWriterT $ runM $ writerLogger program :: IO [String]
mapM_ putStrLn ls
program_with_io :: (Member Logger r, Member IO r) => Eff r ()
program_with_io = do
send (putStrLn "interpolated IO" :: IO ())
program
main4 = do
ls <- execWriterT $ runM
$ runNat (liftIO @(WriterT [String] IO))
$ writerLogger program_with_io :: IO [String]
mapM_ putStrLn ls
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.