on master help sign in
Remove Except as it's needed in pure code for handling Error
4537055Andrzej Rybczak
src/Effectful.hs−6
@@ -15,15 +15,9 @@ module Effectful
15 , runEff
15 , runEff
16
16
17 -- ** Operations with side effects
17 -- ** Operations with side effects
18−
19− -- *** Arbitrary I/O
20 , IOE
18 , IOE
21 , runIOE
19 , runIOE
22
20
23− -- *** Runtime exceptions
24− , Except
25− , runExcept
26−
27 -- * Building 'Effect' handlers
21 -- * Building 'Effect' handlers
28
22
29 -- ** Basic handlers
23 -- ** Basic handlers
src/Effectful/Error.hs+2−2
@@ -3,7 +3,7 @@
3The 'Error' effect provided by this module is a suitable replacement for the
3The 'Error' effect provided by this module is a suitable replacement for the
4'Control.Monad.Except.ExceptT' monad transformer found in the @mtl@ library. It
4'Control.Monad.Except.ExceptT' monad transformer found in the @mtl@ library. It
5is __not__ intended to be a general mechanism for catching errors, that's what
5is __not__ intended to be a general mechanism for catching errors, that's what
6−the 'Except' effect is for.
6+functions from the "Control.Monad.Catch" module are for.
7
7
8For example, if you want to catch an unchecked exception like
8For example, if you want to catch an unchecked exception like
9'Control.Exception.SomeException' or 'Control.Exception.ErrorCall' you could use
9'Control.Exception.SomeException' or 'Control.Exception.ErrorCall' you could use
@@ -14,7 +14,7 @@ For example, if you want to catch an unchecked exception like
14 let boom :: Eff es String
14 let boom :: Eff es String
15 boom = error "BOOM!"
15 boom = error "BOOM!"
16 in
16 in
17− runIOE . runExcept $ boom `E.catch` \(e :: ErrorCall) -> pure "caught some error"
17+ runIOE $ boom `E.catch` \(e :: ErrorCall) -> pure "caught some error"
18:}
18:}
19"caught some error"
19"caught some error"
20
20
src/Effectful/Internal/Monad.hs+3−16
@@ -25,10 +25,6 @@ module Effectful.Internal.Monad
25 , IOE
25 , IOE
26 , runIOE
26 , runIOE
27
27
28− -- * Except
29− , Except
30− , runExcept
31−
32 -- * Low-level helpers
28 -- * Low-level helpers
33
29
34 -- ** Running
30 -- ** Running
@@ -178,19 +174,10 @@ instance MonadFix (Eff es) where
178----------------------------------------
174----------------------------------------
179-- Exception
175-- Exception
180
176
181−-- | Handle runtime exceptions via 'E.MonadThrow', 'E.MonadCatch' and
182−-- 'E.MonadMask'.
183−data Except :: Effect where
184− Except :: Except m r
185−
186−-- | Introduce the ability to handle runtime exceptions.
187−runExcept :: IOE :> es => Eff (Except : es) a -> Eff es a
188−runExcept = evalEffect (IdE Except)
189−
190−instance Except :> es => E.MonadThrow (Eff es) where
177+instance E.MonadThrow (Eff es) where
191 throwM = unsafeEff_ . throwIO
178 throwM = unsafeEff_ . throwIO
192
179
193−instance Except :> es => E.MonadCatch (Eff es) where
180+instance E.MonadCatch (Eff es) where
194 catch m handler = unsafeEff $ \es -> do
181 catch m handler = unsafeEff $ \es -> do
195 size <- sizeEnv es
182 size <- sizeEnv es
196 unEff m es `catch` \e -> do
183 unEff m es `catch` \e -> do
@@ -198,7 +185,7 @@ instance Except :> es => E.MonadCatch (Eff es) where
198 unEff (handler e) es
185 unEff (handler e) es
199 {-# INLINABLE catch #-}
186 {-# INLINABLE catch #-}
200
187
201−instance Except :> es => E.MonadMask (Eff es) where
188+instance E.MonadMask (Eff es) where
202 mask k = unsafeEff $ \es -> mask $ \restore ->
189 mask k = unsafeEff $ \es -> mask $ \restore ->
203 unEff (k $ \m -> unsafeEff $ restore . unEff m) es
190 unEff (k $ \m -> unsafeEff $ restore . unEff m) es
204
191
src/Effectful/Monad.hs−4
@@ -10,10 +10,6 @@ module Effectful.Monad
10 -- * Arbitrary I/O
10 -- * Arbitrary I/O
11 , IOE
11 , IOE
12 , runIOE
12 , runIOE
13−
14− -- * Exception handling
15− , Except
16− , runExcept
17 ) where
13 ) where
18
14
19import Effectful.Internal.Effect
15import Effectful.Internal.Effect
tests/ErrorTests.hs+1−1
@@ -31,5 +31,5 @@ data NestedErr :: Effect where
31nestedErr :: (HasCallStack, NestedErr :> es) => Eff es ()
31nestedErr :: (HasCallStack, NestedErr :> es) => Eff es ()
32nestedErr = send NestedErr
32nestedErr = send NestedErr
33
33
34−runNestedErr :: Error String :> es => Eff (NestedErr : es) a -> Eff es a
34+runNestedErr :: Errors '[String] es => Eff (NestedErr : es) a -> Eff es a
35runNestedErr = interpret $ \NestedErr -> throwError "nested error"
35runNestedErr = interpret $ \NestedErr -> throwError "nested error"
tests/StateTests.hs+6−6
@@ -50,7 +50,7 @@ test_deepStack = runIOE $ do
50 U.assertEqual "n" 15 n
50 U.assertEqual "n" 15 n
51
51
52test_exceptions :: Assertion
52test_exceptions :: Assertion
53−test_exceptions = runIOE . runExcept $ do
53+test_exceptions = runIOE $ do
54 testTry "exceptions" E.try
54 testTry "exceptions" E.try
55 testCatch "exceptions" E.catch
55 testCatch "exceptions" E.catch
56 testTry "lifted-base" LE.try
56 testTry "lifted-base" LE.try
@@ -60,8 +60,8 @@ test_exceptions = runIOE . runExcept $ do
60 where
60 where
61 testTry
61 testTry
62 :: String
62 :: String
63− -> (forall a es. (IOE :> es, Except :> es) => Eff es a -> Eff es (Either U.Ex a))
63+ -> (forall a es. IOE :> es => Eff es a -> Eff es (Either U.Ex a))
64− -> Eff '[Except, IOE] ()
64+ -> Eff '[IOE] ()
65 testTry lib tryImpl = do
65 testTry lib tryImpl = do
66 e <- tryImpl $ runState (0::Int) action
66 e <- tryImpl $ runState (0::Int) action
67 U.assertEqual (lib ++ " - exception caught") e (Left U.Ex)
67 U.assertEqual (lib ++ " - exception caught") e (Left U.Ex)
@@ -70,15 +70,15 @@ test_exceptions = runIOE . runExcept $ do
70
70
71 testCatch
71 testCatch
72 :: String
72 :: String
73− -> (forall a es. (IOE :> es, Except :> es) => Eff es a -> (U.Ex -> Eff es a) -> Eff es a)
73+ -> (forall a es. IOE :> es => Eff es a -> (U.Ex -> Eff es a) -> Eff es a)
74− -> Eff '[Except, IOE] ()
74+ -> Eff '[IOE] ()
75 testCatch lib catchImpl = do
75 testCatch lib catchImpl = do
76 s <- execState (0::Int) $ do
76 s <- execState (0::Int) $ do
77 _ <- (evalState () action) `catchImpl` \U.Ex -> modify @Int (+4)
77 _ <- (evalState () action) `catchImpl` \U.Ex -> modify @Int (+4)
78 modify @Int (+8)
78 modify @Int (+8)
79 U.assertEqual (lib ++ " - state correctly updated") s 13
79 U.assertEqual (lib ++ " - state correctly updated") s 13
80
80
81− action :: (State Int :> es, Except :> es) => Eff es ()
81+ action :: State Int :> es => Eff es ()
82 action = do
82 action = do
83 modify @Int (+1)
83 modify @Int (+1)
84 _ <- E.throwM U.Ex
84 _ <- E.throwM U.Ex