The following is a simple example of using delimited continuation (reset/shift):
import Control.Monad
import Control.Monad.Trans
import Control.Monad.Trans.Cont
test :: Integer
test = evalCont . reset $ do
r <- shift $ \k -> do
return $ k 10
return $ 1 + r
λ> test1
11
It works well.
However, I'd like to extract the continuation k
as a pure function for future use, instead of just calling it inside shift.
For example, I hope the test2
could return the k
:
test2 :: Integer -> Integer
test2 = evalCont . reset $ do
r <- shift $ \k -> do
return $ k
return $ 1 + r
but GHC complains:
? Couldn't match type 'Integer -> Integer' with 'Integer'
Expected type: Cont (Integer -> Integer) (Integer -> Integer)
Actual type: ContT
(Integer -> Integer)
Data.Functor.Identity.Identity
((Integer -> Integer) -> Integer -> Integer)
? In a stmt of a 'do' block: return $ k
In the expression: do return $ k
In the second argument of '($)', namely '\ k -> do return $ k'
|
88 | return $ k
| ^^^^^^^^^^
Anyone could help me to work around this problem?
Thanks.