{-# LANGUAGE BlockArguments #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TupleSections #-}
module Polysemy.Writer
(
Writer (..)
, tell
, listen
, censor
, runWriter
, runOutputAsWriter
) where
import Polysemy
import Polysemy.Output
import Polysemy.State
data Writer o m a where
Tell :: o -> Writer o m ()
Listen :: ∀ o m a. m a -> Writer o m (o, a)
Censor :: (o -> o) -> m a -> Writer o m a
makeSem ''Writer
runOutputAsWriter :: Member (Writer o) r => Sem (Output o ': r) a -> Sem r a
runOutputAsWriter = interpret \case
Output o -> tell o
{-# INLINE runOutputAsWriter #-}
runWriter
:: Monoid o
=> Sem (Writer o ': r) a
-> Sem r (o, a)
runWriter = runState mempty . reinterpretH \case
Tell o -> do
modify (<> o) >>= pureT
Listen m -> do
mm <- runT m
(o, fa) <- raise $ runWriter mm
pure $ fmap (o, ) fa
Censor f m -> do
mm <- runT m
~(o, a) <- raise $ runWriter mm
modify (<> f o)
pure a