While trying to build some intuition for the ContT monad transformer I (perhaps unsurprisingly) found myself confused. The issue lies with the shiftT operation which doesn't seem to do anything useful.
First a simplistic example of how one might use it
shiftT $ \famr -> lift $ do
a <- calculateAFromEnvironment
famr a
famr a
could be some more complex expression as long as it returns some m r
. Now an attempt to explain my intuition that shiftT is doesn't add anything:
-- inline shiftT
ContT (\f2 -> evalContT ((\f1 -> lift (do
a <- calculateAFromEnvironment
f1 a)) f2))
-- beta reduction
ContT (\f2 -> evalContT (lift (do
a <- calculateAFromEnvironment
f2 a)))
-- inline evalConT
ContT (\f2 -> runContT (lift (do
a <- calculateAFromEnvironment
f2 a)) return)
-- inline lift
ContT (\f2 -> runContT (ContT (\f3 -> (do
a <- calculateAFromEnvironment
f2 a) >>= f3)) return)
-- apply runConT
ContT (\f2 -> (\f3 -> (do
a <- calculateAFromEnvironment
f2 a) >>= f3) return)
-- beta reduce
ContT (\f2 -> (do
a <- calculateAFromEnvironment
f2 a) >>= return)
-- (>>= return) is identity
ContT $ \f2 -> do
a <- calculateAFromEnvironment
f2 a
Turns out we could have just build the ContT directly.
Question time: Is there a situation where shift/shiftT add anything over cont/ContT? Or are they just used to make the code more readable?
After searching github by Gurkenglas's advice I've discovered this very nice explanation of shiftT
and resetT
with examples of usages, motivation and semantic!
Those functions are very simple. Their definition in transformers
library is straightforward:
resetT :: (Monad m) => ContT r m r -> ContT r' m r
resetT = lift . evalContT
shiftT :: (Monad m) => ((a -> m r) -> ContT r m r) -> ContT r m a
shiftT f = ContT (evalContT . f)
But philosophy and meaning far behind some intuitive understanding. So I recommend you to read explanation from the link above. Sometimes it happens that things that are easy to define actually can do something complex.
Adapted documentation from the explanation in pugs linked above:
shiftT
shiftT
is likecallCC
, except that when you activate the continuation provided byshiftT
, it will run to the end of the nearest enclosingresetT
, then jump back to just after the point at which you activated the continuation. Note that because control eventually returns to the point after the subcontinuation is activated, you can activate it multiple times in the same block. This is unlikecallCC
's continuations, which discard the current execution path when activated.See
resetT
for an example of how these delimited subcontinuations actually work.
resetT
Create a scope that
shiftT
's subcontinuations are guaranteed to eventually exit out the end of. Consider this example:resetT $ do alfa bravo x <- shiftT $ \esc -> do -- note: esc :: m Int, not a ContT charlie lift $ esc 1 delta lift $ esc 2 return 0 zulu x
This will:
Perform
alfa
Perform
bravo
Perform
charlie
Bind
x
to 1, and thus performzulu 1
Fall off the end of
resetT
, and jump back to just afteresc 1
Perform
delta
Bind
x
to 2, and thus performzulu 2
Fall off the end of
resetT
, and jump back to just afteresc 2
Escape from the
resetT
, causing it to yield 0Thus, unlike
callCC
's continuations, these subcontinuations will eventually return to the point after they are activated, after falling off the end of the nearestresetT
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With