-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathExamples.hs
329 lines (299 loc) · 8.93 KB
/
Examples.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE MonoLocalBinds #-}
{-# LANGUAGE DataKinds #-}
module Examples where
import Control.Monad.Except (MonadError, ExceptT, runExceptT)
import Control.Monad.Oops ( CouldBe, Variant )
import Data.Text (Text)
import Text.Read (readMaybe)
import Data.Functor.Identity (Identity)
import Control.Monad.Trans (MonadIO)
import Data.Function ((&))
import qualified Control.Monad.Oops as OO
import qualified System.IO as IO
import Control.Monad.IO.Class (MonadIO(..))
import Control.Exception (IOException)
import Control.Monad.Catch (MonadCatch)
-- | A simple function that throws an error.
--
-- The type is the one that is inferred by GHC.
readIntV1 :: ()
=> MonadError (Variant e) m
=> OO.CouldBeF e Text
=> String
-> m Int
readIntV1 s = case readMaybe @Int s of
Just i -> return i
Nothing -> OO.throw @Text "Not an integer"
-- | A simple function that throws an error.
--
-- This is the same as before, but we can rewrite constraint on 'e' differently.
readIntV2 :: ()
=> MonadError (Variant e) m
=> e `CouldBe` Text
=> String
-> m Int
readIntV2 s = case readMaybe @Int s of
Just i -> return i
Nothing -> OO.throw @Text "Not an integer"
-- | A simple function that throws an error.
--
-- We can also use 'ExceptT'
readIntV3 :: ()
=> e `CouldBe` Text
=> String
-> ExceptT (Variant e) Identity Int
readIntV3 s = case readMaybe @Int s of
Just i -> return i
Nothing -> OO.throw @Text "Not an integer"
-- | A simple IO function that throws an error.
--
-- We can also use 'ExceptT' of 'IO'.
readIntV4 :: ()
=> e `CouldBe` Text
=> String
-> ExceptT (Variant e) IO Int
readIntV4 s = case readMaybe @Int s of
Just i -> return i
Nothing -> OO.throw @Text "Not an integer"
-- | A simple function that throws an error.
--
-- Or use MonadIO instead of IO directly.
readIntV5 :: ()
=> MonadError (Variant e) m
=> MonadIO m
=> e `CouldBe` Text
=> String
-> m Int
readIntV5 s = case readMaybe @Int s of
Just i -> return i
Nothing -> OO.throw @Text "Not an integer"
-- | A simple function that throws an error.
--
-- Or use 'MonadIO' with 'ExceptT'.
readIntV6 :: ()
=> MonadIO m
=> e `CouldBe` Text
=> String
-> ExceptT (Variant e) m Int
readIntV6 s = case readMaybe @Int s of
Just i -> return i
Nothing -> OO.throw @Text "Not an integer"
-- We can represent each error as a separate type.
data NotAnInteger = NotAnInteger
data NotPositive = NotPositive
-- | A simple function can throw two errors
readPositiveInt1 :: ()
=> MonadIO m
=> e `CouldBe` NotAnInteger
=> e `CouldBe` NotPositive
=> String
-> ExceptT (Variant e) m Int
readPositiveInt1 s =
case readMaybe @Int s of
Just i ->
if i > 0
then return i
else OO.throw NotPositive
Nothing -> OO.throw NotAnInteger
-- We can call a function that throws an error of type 'Text' and allow the
-- error to propagate by declaring we also throw an error of type 'Text'.
example1 :: ()
=> e `CouldBe` Text
=> String
-> ExceptT (Variant e) IO Int
example1 s = do
i <- readInt s
liftIO $ IO.print i
return i
where
readInt :: ()
=> e `CouldBe` Text
=> String
-> ExceptT (Variant e) IO Int
readInt = error "unimplemented"
-- Or alternatively, we can catch the error of type 'Text' and handle it.
-- in which case the error doesn't propagate. Notice the 'e CouldBe Text'
-- constraint is not needed in this case.
example2 :: ()
=> String
-> ExceptT (Variant e) IO Int
example2 s = do
i <- readInt s
& OO.catch @Text (\_ -> pure 0)
liftIO $ IO.print i
return i
where
readInt :: ()
=> e `CouldBe` Text
=> String
-> ExceptT (Variant e) IO Int
readInt = error "unimplemented"
-- When we don't throw any errors, we can use the 'runOops' function to
-- convert the 'ExceptT' to an 'IO' action.
example3 :: ()
=> String
-> IO Int
example3 s = OO.runOops $ do
i <- readInt s
& OO.catch @Text (\_ -> pure 0)
liftIO $ IO.print i
return i
where
readInt :: ()
=> e `CouldBe` Text
=> String
-> ExceptT (Variant e) IO Int
readInt = error "unimplemented"
data FileNotFound = FileNotFound
data FileNotReadable = FileNotReadable
data Errors1
= Errors1NotPositive NotPositive
| Errors1NotAnInteger NotAnInteger
-- We can call a function that throws multiple errors into a function
-- that only throws one by just catching and rethrowing the one
-- error.
example4 :: ()
=> MonadIO m
=> e `CouldBe` Errors1
=> String
-> ExceptT (Variant e) m Int
example4 s = do
i <- readPositiveInt1 s
& OO.catch @NotPositive (OO.throw . Errors1NotPositive)
& OO.catch @NotAnInteger (OO.throw . Errors1NotAnInteger)
liftIO $ IO.print i
return i
--------------------------------------------------------------------------------
-- Embedding 'Oops' into vanilla 'ExceptT' and 'Either' code.
-- We we have a function that only throws one error, we can use 'runOopsInExceptT'
-- to remove the 'Variant' wrapper leaving only the 'ExceptT'.
example5 :: ()
=> MonadIO m
=> String
-> ExceptT Errors1 m Int
example5 s = OO.runOopsInExceptT $ do
i <- readPositiveInt1 s
& OO.catch @NotPositive (OO.throw . Errors1NotPositive)
& OO.catch @NotAnInteger (OO.throw . Errors1NotAnInteger)
liftIO $ IO.print i
return i
-- We we have a function that only throws one error, we can use 'runOopsInEither'
-- to remove the 'Variant' wrapper and the 'Except' leaving only the 'Either'.
example6 :: ()
=> MonadIO m
=> String
-> m (Either Errors1 Int)
example6 s = OO.runOopsInEither $ do
i <- readPositiveInt1 s
& OO.catch @NotPositive (OO.throw . Errors1NotPositive)
& OO.catch @NotAnInteger (OO.throw . Errors1NotAnInteger)
liftIO $ IO.print i
return i
--------------------------------------------------------------------------------
-- Embedding vanilla 'ExceptT' and 'Either' code into 'Oops' code.
-- We can call a function that throws multiple errors into a function
-- that only throws one by just catching and rethrowing the one
-- error.
-- We can call a function that throws vanilla 'ExceptT' errors and use 'onLeft'
-- to catch and rethrow the errors as oops errors.
example7 :: ()
=> MonadIO m
=> e `CouldBe` NotPositive
=> e `CouldBe` NotAnInteger
=> String
-> ExceptT (Variant e) m Int
example7 s = do
i <- readInt s
& OO.onLeft OO.throw
pos <- requirePositive i
& OO.onLeft OO.throw
liftIO $ IO.print pos
return pos
where
readInt :: MonadIO m => String -> m (Either NotAnInteger Int)
readInt = error "unimplemented"
requirePositive :: MonadIO m => Int -> m (Either NotPositive Int)
requirePositive = error "unimplemented"
-- 'onLeftThrow' is shorthand for 'onLeft throw'.
example8 :: ()
=> MonadIO m
=> e `CouldBe` NotPositive
=> e `CouldBe` NotAnInteger
=> String
-> ExceptT (Variant e) m Int
example8 s = do
i <- readInt s
& OO.onLeftThrow
pos <- requirePositive i
& OO.onLeftThrow
liftIO $ IO.print pos
return pos
where
readInt :: MonadIO m => String -> m (Either NotAnInteger Int)
readInt = error "unimplemented"
requirePositive :: MonadIO m => Int -> m (Either NotPositive Int)
requirePositive = error "unimplemented"
-- We can similarly call a function that throws via vanilla 'ExceptT'
example9 :: ()
=> MonadIO m
=> e `CouldBe` NotPositive
=> e `CouldBe` NotAnInteger
=> String
-> ExceptT (Variant e) m Int
example9 s = do
i <- liftIO (runExceptT (readInt s))
& OO.onLeftThrow
pos <- liftIO (runExceptT (requirePositive i))
& OO.onLeftThrow
liftIO $ IO.print pos
return pos
where
readInt :: String -> ExceptT NotAnInteger IO Int
readInt = error "unimplemented"
requirePositive :: Int -> ExceptT NotPositive IO Int
requirePositive = error "unimplemented"
-- We can similarly call a function that throws via vanilla 'ExceptT'
example10 :: ()
=> MonadIO m
=> e `CouldBe` NotPositive
=> e `CouldBe` NotAnInteger
=> String
-> ExceptT (Variant e) m Int
example10 s = do
i <- liftIO (runExceptT (readInt s))
& OO.onLeftThrow
pos <- liftIO (runExceptT (requirePositive i))
& OO.onLeftThrow
liftIO $ IO.print pos
return pos
where
readInt :: String -> ExceptT NotAnInteger IO Int
readInt = error "unimplemented"
requirePositive :: Int -> ExceptT NotPositive IO Int
requirePositive = error "unimplemented"
-- We can also catch runtime exceptions and rethrow them as checked exceptions.
example11 :: ()
=> MonadIO m
=> MonadCatch m
=> e `CouldBe` IOException
=> ExceptT (Variant e) m String
example11 = do
i <- liftIO (IO.readFile "moo")
& OO.onException @IOException OO.throw
liftIO $ IO.print i
return i
-- The 'onExceptionThrow' is shorthand for 'onException throw'.
example12 :: ()
=> MonadIO m
=> MonadCatch m
=> e `CouldBe` IOException
=> ExceptT (Variant e) m String
example12 = do
i <- liftIO (IO.readFile "moo")
& OO.onExceptionThrow @IOException
liftIO $ IO.print i
return i