-
Notifications
You must be signed in to change notification settings - Fork 48
/
Exit.hs
42 lines (38 loc) · 1011 Bytes
/
Exit.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
module Hackage.Security.Util.Exit where
import Prelude
import Control.Monad (liftM)
import Control.Monad.Except (ExceptT, runExceptT, throwError)
{-------------------------------------------------------------------------------
Auxiliary: multiple exit points
-------------------------------------------------------------------------------}
-- | Multiple exit points
--
-- We can simulate the imperative code
--
-- > if (cond1)
-- > return exp1;
-- > if (cond2)
-- > return exp2;
-- > if (cond3)
-- > return exp3;
-- > return exp4;
--
-- as
--
-- > multipleExitPoints $ do
-- > when (cond1) $
-- > exit exp1
-- > when (cond2) $
-- > exit exp2
-- > when (cond3) $
-- > exit exp3
-- > return exp4
multipleExitPoints :: Monad m => ExceptT a m a -> m a
multipleExitPoints = liftM aux . runExceptT
where
aux :: Either a a -> a
aux (Left a) = a
aux (Right a) = a
-- | Function exit point (see 'multipleExitPoints')
exit :: Monad m => e -> ExceptT e m a
exit = throwError