-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCont.hs
36 lines (29 loc) · 1008 Bytes
/
Cont.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
-- | Examples of the continuation effect.
module Example.Cont where
-- hspec
import Test.Hspec (Spec, it, shouldBe)
-- effet
import Control.Effect.Cont
import Control.Effect.Identity
import Example.Logger
--- Example Programs -----------------------------------------------------------
fiveTimesCont :: (Cont m, Logger m) => m ()
fiveTimesCont = do
logStr "x"
(k, num) <- callCC $ \k -> let f x = k (f, x)
in pure (f, 0::Int)
logStr "y"
logStr "z"
if num < 5
then k (num + 1) >> pure ()
else logStr $ show num
--- Test Cases -----------------------------------------------------------------
spec :: Spec
spec = do
it "evaluates fiveTimesCont" $
( runIdentity -- result: ((), [String])
. runCollectLogger -- result: Monad m => m ((), [String])
. evalCont -- effects: Logger
$ fiveTimesCont -- effects: Cont, Logger
) `shouldBe`
((), ["5","z","y","z","y","z","y","z","y","z","y","z","y","x"])