From eceeb6a33e33e3ad6b4a11c8ac6dc9e17f09f189 Mon Sep 17 00:00:00 2001 From: Lautaro Emanuel Date: Thu, 15 Dec 2022 13:25:22 -0300 Subject: [PATCH 1/2] Add 'liftMaybe' --- Control/Monad/Error/Class.hs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/Control/Monad/Error/Class.hs b/Control/Monad/Error/Class.hs index b2465e7..3c5cc0f 100644 --- a/Control/Monad/Error/Class.hs +++ b/Control/Monad/Error/Class.hs @@ -45,6 +45,7 @@ The Error monad (also called the Exception monad). module Control.Monad.Error.Class ( MonadError(..), liftEither, + liftMaybe, tryError, withError, handleError, @@ -74,7 +75,7 @@ import Control.Monad.Trans.Class (lift) import Control.Exception (IOException, catch, ioError) import Control.Monad (Monad) import Data.Monoid (Monoid) -import Prelude (Either (Left, Right), Maybe (Nothing), either, flip, (.), IO, pure, (<$>), (>>=)) +import Prelude (Either (Left, Right), Maybe (Nothing), either, maybe, flip, (.), IO, pure, (<$>), (>>=)) {- | The strategy of combining computations that can throw exceptions @@ -122,6 +123,16 @@ where @action1@ returns an 'Either' to represent errors. liftEither :: MonadError e m => Either e a -> m a liftEither = either throwError pure +{- | +Lifts a @'Maybe' a@ into any @'MonadError' e@, using @e@ as the error if the 'Maybe' is 'Nothing'. + +> do { val <- liftMaybe e =<< action1; action2 } + +where @action1@ returns an 'Maybe'. +-} +liftMaybe :: MonadError e m => e -> Maybe a -> m a +liftMaybe e = maybe (throwError e) pure + instance MonadError IOException IO where throwError = ioError catchError = catch From dbc850fa68b54cf3a5c97977c95edf38aad3f894 Mon Sep 17 00:00:00 2001 From: Lautaro Emanuel Date: Thu, 15 Dec 2022 13:28:23 -0300 Subject: [PATCH 2/2] Update docs --- Control/Monad/Error/Class.hs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Control/Monad/Error/Class.hs b/Control/Monad/Error/Class.hs index 3c5cc0f..263b551 100644 --- a/Control/Monad/Error/Class.hs +++ b/Control/Monad/Error/Class.hs @@ -124,11 +124,11 @@ liftEither :: MonadError e m => Either e a -> m a liftEither = either throwError pure {- | -Lifts a @'Maybe' a@ into any @'MonadError' e@, using @e@ as the error if the 'Maybe' is 'Nothing'. +Lifts a @'Maybe'@ into any @'MonadError' e@, using a supplied @e@ as the error if the 'Maybe' is 'Nothing'. > do { val <- liftMaybe e =<< action1; action2 } -where @action1@ returns an 'Maybe'. +where @action1@ returns a 'Maybe'. -} liftMaybe :: MonadError e m => e -> Maybe a -> m a liftMaybe e = maybe (throwError e) pure