on master help sign in
Show that listen works as advertised
commitbf25f3421fa32c5bf1ece3d667cf9ae1c59550bb authorAndrzej Rybczak <andrzej@rybczak.net> dateTue 21 Dec 2021, 03:05 (5 years ago) parenta2681eb
2 files changed+25−7
effectful-core/src/Effectful/State/Local.hs+8−5 effectful-core/src/Effectful/Writer/Local.hs+17−2
effectful-core/src/Effectful/State/Local.hs+8−5
@@ -5,13 +5,12 @@
55--
66-- - very fast.
77--
8−-- /Note:/ unlike 'Control.Monad.Trans.State.StateT', the 'State' effect doesn't
9−-- lose state modifications in presence of runtime exceptions:
10−--
11−-- >>> import Control.Exception (ErrorCall)
12−-- >>> import Control.Monad.Catch
8+-- /Note:/ unlike the 'Control.Monad.Trans.State.StateT' monad transformer from
9+-- the @transformers@ library, the 'State' effect doesn't lose state
10+-- modifications when an exception is received:
1311--
1412-- >>> import qualified Control.Monad.Trans.State.Strict as S
13+--
1514-- >>> :{
1615-- (`S.execStateT` "Hi") . handle (\(_::ErrorCall) -> pure ()) $ do
1716-- S.modify (++ " there!")
@@ -126,3 +125,7 @@ modifyM
126125 => (s -> Eff es s) -- ^ The monadic function to modify the state.
127126 -> Eff es ()
128127modifyM f = stateM (\s -> ((), ) <$> f s)
128+
129+-- $setup
130+-- >>> import Control.Exception (ErrorCall)
131+-- >>> import Control.Monad.Catch
effectful-core/src/Effectful/Writer/Local.hs+17−2
@@ -24,7 +24,7 @@ module Effectful.Writer.Local
2424 , listens
2525 ) where
2626
27−import Control.Exception
27+import Control.Exception (onException, mask)
2828
2929import Effectful.Dispatch.Static
3030import Effectful.Monad
@@ -57,7 +57,18 @@ tell w = stateEffect $ \(IdA (Writer w0)) -> ((), IdA (Writer (w0 <> w)))
5757--
5858-- /Note:/ if a runtime exception is received while the action is executed, the
5959-- partial output of the action will still be appended to the overall output of
60−-- the 'Writer'.
60+-- the 'Writer':
61+--
62+-- >>> :{
63+-- runEff . execWriter @String $ do
64+-- tell "Hi"
65+-- handle (\(_::ErrorCall) -> pure ((), "")) $ do
66+-- tell " there"
67+-- listen $ do
68+-- tell "!"
69+-- error "oops"
70+-- :}
71+-- "Hi there!"
6172listen :: (Writer w :> es, Monoid w) => Eff es a -> Eff es (a, w)
6273listen m = unsafeEff $ \es -> mask $ \restore -> do
6374 w0 <- stateEnv es $ \(IdA (Writer w)) -> (w, IdA (Writer mempty))
@@ -77,3 +88,7 @@ listens :: (Writer w :> es, Monoid w) => (w -> b) -> Eff es a -> Eff es (a, b)
7788listens f m = do
7889 (a, w) <- listen m
7990 pure (a, f w)
91+
92+-- $setup
93+-- >>> import Control.Monad.Catch
94+-- >>> import Control.Exception (ErrorCall)