Example4 expressions>>> import Control.Monad.IO.Class>>> import Data.Map.Strict qualified as M>>> import Effectful.Dispatch.Dynamic>>> import Effectful.State.Static.Local
Given an effect:
Example1 expression>>> :{ data Write :: Effect where Write :: String -> Write m () type instance DispatchOf Write = Dynamic:}Example1 expression>>> :{ write :: Write :> es => String -> Eff es () write = send . Write:}its handler can be turned into an effectful operation with the Provider
effect:
Example1 expression>>> :{ action :: Provider_ Write FilePath :> es => Eff es () action = do provideWith_ @Write "in.txt" $ do write "hi" write "there" provideWith_ @Write "out.txt" $ do write "good" write "bye":}Then, given multiple interpreters:
Example1 expression>>> :{ runWriteIO :: IOE :> es => FilePath -> Eff (Write : es) a -> Eff es a runWriteIO fp = interpret_ $ \case Write msg -> liftIO . putStrLn $ fp ++ ": " ++ msg:}Example1 expression>>> :{ runWritePure :: State (M.Map FilePath [String]) :> es => FilePath -> Eff (Write : es) a -> Eff es a runWritePure fp = interpret_ $ \case Write msg -> modify $ M.insertWith (++) fp [msg]:}action can be supplied with either of them for the appropriate behavior:
Example1 expression>>> :{ runEff . runProvider_ runWriteIO $ action:}in.txt: hiin.txt: thereout.txt: goodout.txt: byeExample1 expression>>> :{ runPureEff . fmap (fmap reverse) . execState @(M.Map FilePath [String]) M.empty . runProvider_ runWritePure $ action:}fromList [("in.txt",["hi","there"]),("out.txt",["good","bye"])]Moreover, operations of the Provider effect can be intercepted with
interpose, e.g. to adjust the input of the effect handler:
Example1 expression>>> :{ adjustPaths :: Provider_ Write FilePath :> es => Eff es a -> Eff es a adjustPaths = interpose @(Provider_ Write FilePath) $ \env -> \case ProvideWith fp action -> do passthrough env $ ProvideWith ("logs/" ++ fp) action:}Example1 expression>>> :{ runEff . runProvider_ runWriteIO . adjustPaths $ action:}logs/in.txt: hilogs/in.txt: therelogs/out.txt: goodlogs/out.txt: bye