-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathManaged.hs
78 lines (67 loc) · 2.33 KB
/
Managed.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
-- | Examples of the managed effect.
module Example.Managed where
-- base
import Prelude hiding (print)
-- hspec
import Test.Hspec (Spec, it)
-- effet
import Control.Effect.Error
import Control.Effect.Managed
import Hspec (print, shouldPrint)
--- Example Programs -----------------------------------------------------------
-- | Type used here as virtual handle.
newtype Handle = Handle { nameOf :: String }
-- | Gets a managed virtual handle specified by a name.
getHandle :: Managed m => String -> m Handle
getHandle name = manage
( print ("Alloc " ++ name) >> pure (Handle name) )
( \handle -> print $ "Free " ++ nameOf handle )
-- | Simple program that makes use of two handles. Handles are destroyed
-- automatically at the end of the program.
useHandles :: Managed m => m ()
useHandles = do
a <- getHandle "A"
b <- getHandle "B"
print $ "Use " ++ nameOf a
print $ "Use " ++ nameOf b
-- | Allocates some handlers, then throws an error.
throwCheck :: (Error String m, Managed m) => m ()
throwCheck = do
a <- getHandle "A"
b <- getHandle "B"
print $ "Use " ++ nameOf a
_ <- throwError "Some error"
print $ "Use " ++ nameOf b
--- Test Cases -----------------------------------------------------------------
spec :: Spec
spec = do
it "manages a handle" $
( runManaged -- result: (MonadBaseControl IO m, MonadIO m) => m (),
-- unified with IO ()
$ getHandle "X" ) -- effects: Managed
`shouldPrint`
"\"Alloc X\"\n\"Free X\"\n"
it "manages multiple handles" $
( runManaged -- result: (MonadBaseControl IO m, MonadIO m) => m (),
-- unified with IO ()
$ useHandles ) -- effects: Managed
`shouldPrint`
( "\"Alloc A\"\n"
++ "\"Alloc B\"\n"
++ "\"Use A\"\n"
++ "\"Use B\"\n"
++ "\"Free B\"\n"
++ "\"Free A\"\n"
)
it "manages multiple handles with an error" $
( runManaged -- result: (MonadBaseControl IO m, MonadIO m) => m (Either String ()),
-- unified with IO (Either String ())
. runError -- effects: Managed
$ throwCheck ) -- effects: Error String, Managed
`shouldPrint`
( "\"Alloc A\"\n"
++ "\"Alloc B\"\n"
++ "\"Use A\"\n"
++ "\"Free B\"\n"
++ "\"Free A\"\n"
)