|
1 | 1 | module Examples where
|
2 |
| - import Debug.Trace(trace, Trace()) |
| 2 | + import Debug.Trace(Trace()) |
3 | 3 |
|
4 | 4 | import Data.Either(either)
|
5 | 5 |
|
6 | 6 | import Control.Monad.Aff
|
7 | 7 | import Control.Monad.Aff.AVar
|
8 | 8 | import Control.Monad.Aff.Par
|
| 9 | + import Control.Monad.Aff.Debug.Trace(trace) |
9 | 10 | import Control.Apply((*>))
|
10 | 11 | import Control.Alt(Alt, (<|>))
|
11 | 12 | import Control.Monad.Eff.Class(liftEff)
|
12 | 13 | import Control.Monad.Eff.Exception(error)
|
13 | 14 | import Control.Monad.Error.Class(throwError)
|
14 | 15 |
|
15 |
| - type Test = forall e. Aff (trace :: Trace | e) Unit |
16 |
| - type TestAVar = forall e. Aff (trace :: Trace, avar :: AVAR | e) Unit |
| 16 | + type Test a = forall e. Aff (trace :: Trace | e) a |
| 17 | + type TestAVar a = forall e. Aff (trace :: Trace, avar :: AVAR | e) a |
17 | 18 |
|
18 |
| - test_sequencing :: Number -> Test |
19 |
| - test_sequencing 0 = liftEff $ trace "Done" |
| 19 | + test_sequencing :: Number -> Test _ |
| 20 | + test_sequencing 0 = trace "Done" |
20 | 21 | test_sequencing n = do
|
21 |
| - later' 100 (liftEff $ trace (show (n / 10) ++ " seconds left")) |
| 22 | + later' 100 (trace (show (n / 10) ++ " seconds left")) |
22 | 23 | test_sequencing (n - 1)
|
23 | 24 |
|
24 |
| - test_pure :: Test |
| 25 | + test_pure :: Test _ |
25 | 26 | test_pure = do
|
26 | 27 | pure unit
|
27 | 28 | pure unit
|
28 | 29 | pure unit
|
29 |
| - liftEff $ trace "Success: Got all the way past 4 pures" |
| 30 | + trace "Success: Got all the way past 4 pures" |
30 | 31 |
|
31 |
| - test_attempt :: Test |
| 32 | + test_attempt :: Test _ |
32 | 33 | test_attempt = do
|
33 | 34 | e <- attempt (throwError (error "Oh noes!"))
|
34 |
| - liftEff $ either (const $ trace "Success: Exception caught") (const $ trace "Failure: Exception NOT caught!!!") e |
| 35 | + either (const $ trace "Success: Exception caught") (const $ trace "Failure: Exception NOT caught!!!") e |
35 | 36 |
|
36 |
| - test_apathize :: Test |
| 37 | + test_apathize :: Test _ |
37 | 38 | test_apathize = do
|
38 | 39 | apathize $ throwError (error "Oh noes!")
|
39 |
| - liftEff $ trace "Success: Exceptions don't stop the apathetic" |
| 40 | + trace "Success: Exceptions don't stop the apathetic" |
40 | 41 |
|
41 |
| - test_putTakeVar :: TestAVar |
| 42 | + test_putTakeVar :: TestAVar _ |
42 | 43 | test_putTakeVar = do
|
43 | 44 | v <- makeVar
|
44 | 45 | forkAff (later $ putVar v 1.0)
|
45 | 46 | a <- takeVar v
|
46 |
| - liftEff $ trace ("Success: Value " ++ show a) |
| 47 | + trace ("Success: Value " ++ show a) |
47 | 48 |
|
48 |
| - test_killFirstForked :: Test |
| 49 | + test_killFirstForked :: Test _ |
49 | 50 | test_killFirstForked = do
|
50 | 51 | c <- forkAff (later' 100 $ pure "Failure: This should have been killed!")
|
51 | 52 | b <- c `cancel` (error "Just die")
|
52 |
| - liftEff $ trace (if b then "Success: Killed first forked" else "Failure: Couldn't kill first forked") |
| 53 | + trace (if b then "Success: Killed first forked" else "Failure: Couldn't kill first forked") |
53 | 54 |
|
54 | 55 |
|
55 |
| - test_killVar :: TestAVar |
| 56 | + test_killVar :: TestAVar _ |
56 | 57 | test_killVar = do
|
57 | 58 | v <- makeVar
|
58 | 59 | killVar v (error "DOA")
|
59 | 60 | e <- attempt $ takeVar v
|
60 |
| - liftEff $ either (const $ trace "Success: Killed queue dead") (const $ trace "Failure: Oh noes, queue survived!") e |
| 61 | + either (const $ trace "Success: Killed queue dead") (const $ trace "Failure: Oh noes, queue survived!") e |
61 | 62 |
|
62 |
| - test_parRace :: TestAVar |
| 63 | + test_parRace :: TestAVar _ |
63 | 64 | test_parRace = do
|
64 | 65 | s <- runPar (Par (later' 100 $ pure "Success: Early bird got the worm") <|>
|
65 | 66 | Par (later' 200 $ pure "Failure: Late bird got the worm"))
|
66 |
| - liftEff $ trace s |
| 67 | + trace s |
67 | 68 |
|
68 |
| - test_parRaceKill1 :: TestAVar |
| 69 | + test_parRaceKill1 :: TestAVar _ |
69 | 70 | test_parRaceKill1 = do
|
70 | 71 | s <- runPar (Par (later' 100 $ throwError (error ("Oh noes!"))) <|>
|
71 | 72 | Par (later' 200 $ pure "Success: Early error was ignored in favor of late success"))
|
72 |
| - liftEff $ trace s |
| 73 | + trace s |
73 | 74 |
|
74 |
| - test_parRaceKill2 :: TestAVar |
| 75 | + test_parRaceKill2 :: TestAVar _ |
75 | 76 | test_parRaceKill2 = do
|
76 | 77 | e <- attempt $ runPar (Par (later' 100 $ throwError (error ("Oh noes!"))) <|>
|
77 | 78 | Par (later' 200 $ throwError (error ("Oh noes!"))))
|
78 |
| - liftEff $ either (const $ trace "Success: Killing both kills it dead") (const $ trace "Failure: It's alive!!!") e |
| 79 | + either (const $ trace "Success: Killing both kills it dead") (const $ trace "Failure: It's alive!!!") e |
79 | 80 |
|
80 |
| - test_semigroupCanceler :: Test |
| 81 | + test_semigroupCanceler :: Test _ |
81 | 82 | test_semigroupCanceler =
|
82 | 83 | let
|
83 | 84 | c = Canceler (const (pure true)) <> Canceler (const (pure true))
|
84 | 85 | in do
|
85 | 86 | v <- cancel c (error "CANCEL")
|
86 |
| - liftEff $ trace (if v then "Success: Canceled semigroup composite canceler" |
| 87 | + trace (if v then "Success: Canceled semigroup composite canceler" |
87 | 88 | else "Failure: Could not cancel semigroup composite canceler")
|
88 | 89 |
|
89 |
| - test_cancelLater :: TestAVar |
| 90 | + test_cancelLater :: TestAVar _ |
90 | 91 | test_cancelLater = do
|
91 | 92 | c <- forkAff $ (do pure "Binding"
|
92 |
| - _ <- later' 100 $ liftEff $ trace ("Failure: Later was not canceled!") |
| 93 | + _ <- later' 100 $ trace ("Failure: Later was not canceled!") |
93 | 94 | pure "Binding")
|
94 | 95 | v <- cancel c (error "Cause")
|
95 |
| - liftEff $ trace (if v then "Success: Canceled later" else "Failure: Did not cancel later") |
| 96 | + trace (if v then "Success: Canceled later" else "Failure: Did not cancel later") |
96 | 97 |
|
97 |
| - test_cancelPar :: TestAVar |
| 98 | + test_cancelPar :: TestAVar _ |
98 | 99 | test_cancelPar = do
|
99 |
| - c <- forkAff <<< runPar $ Par (later' 100 $ liftEff $ trace "Failure: #1 should not get through") <|> |
100 |
| - Par (later' 100 $ liftEff $ trace "Failure: #2 should not get through") |
| 100 | + c <- forkAff <<< runPar $ Par (later' 100 $ trace "Failure: #1 should not get through") <|> |
| 101 | + Par (later' 100 $ trace "Failure: #2 should not get through") |
101 | 102 | v <- c `cancel` (error "Must cancel")
|
102 |
| - liftEff $ trace (if v then "Success: Canceling composite of two Par succeeded" |
| 103 | + trace (if v then "Success: Canceling composite of two Par succeeded" |
103 | 104 | else "Failure: Canceling composite of two Par failed")
|
104 | 105 |
|
105 | 106 | main = launchAff $ do
|
106 |
| - liftEff $ trace "Testing sequencing" |
| 107 | + trace "Testing sequencing" |
107 | 108 | test_sequencing 3
|
108 | 109 |
|
109 |
| - liftEff $ trace "Testing pure" |
| 110 | + trace "Testing pure" |
110 | 111 | test_pure
|
111 | 112 |
|
112 |
| - liftEff $ trace "Testing attempt" |
| 113 | + trace "Testing attempt" |
113 | 114 | test_attempt
|
114 | 115 |
|
115 |
| - liftEff $ trace "Testing later" |
116 |
| - later $ liftEff $ trace "Success: It happened later" |
| 116 | + trace "Testing later" |
| 117 | + later $ trace "Success: It happened later" |
117 | 118 |
|
118 |
| - liftEff $ trace "Testing kill of later" |
| 119 | + trace "Testing kill of later" |
119 | 120 | test_cancelLater
|
120 | 121 |
|
121 |
| - liftEff $ trace "Testing kill of first forked" |
| 122 | + trace "Testing kill of first forked" |
122 | 123 | test_killFirstForked
|
123 | 124 |
|
124 |
| - liftEff $ trace "Testing apathize" |
| 125 | + trace "Testing apathize" |
125 | 126 | test_apathize
|
126 | 127 |
|
127 |
| - liftEff $ trace "Testing semigroup canceler" |
| 128 | + trace "Testing semigroup canceler" |
128 | 129 | test_semigroupCanceler
|
129 | 130 |
|
130 |
| - liftEff $ trace "Testing AVar - putVar, takeVar" |
| 131 | + trace "Testing AVar - putVar, takeVar" |
131 | 132 | test_putTakeVar
|
132 | 133 |
|
133 |
| - liftEff $ trace "Testing AVar killVar" |
| 134 | + trace "Testing AVar killVar" |
134 | 135 | test_killVar
|
135 | 136 |
|
136 |
| - liftEff $ trace "Testing Par (<|>)" |
| 137 | + trace "Testing Par (<|>)" |
137 | 138 | test_parRace
|
138 | 139 |
|
139 |
| - liftEff $ trace "Testing Par (<|>) - kill one" |
| 140 | + trace "Testing Par (<|>) - kill one" |
140 | 141 | test_parRaceKill1
|
141 | 142 |
|
142 |
| - liftEff $ trace "Testing Par (<|>) - kill two" |
| 143 | + trace "Testing Par (<|>) - kill two" |
143 | 144 | test_parRaceKill2
|
144 | 145 |
|
145 |
| - liftEff $ trace "Testing cancel of Par (<|>)" |
| 146 | + trace "Testing cancel of Par (<|>)" |
146 | 147 | test_cancelPar
|
147 | 148 |
|
148 |
| - liftEff $ trace "Done testing" |
| 149 | + trace "Done testing" |
0 commit comments