{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MultiParamTypeClasses #-}

module Control.Monad.Except.IO
    ( ExceptIOT (..)
    , runExceptIOT
    , module Control.Monad.Error.Class
    , modifyError
    ) where

import Control.Applicative
import Control.Monad.Catch hiding (catch, try)
import Control.Monad.Error.Class hiding (modifyError)
import qualified Control.Monad.Except as Except
import Control.Monad.Reader
import Control.Monad.State
import Control.Monad.Writer
import UnliftIO

-- | This type is useful for providing a 'MonadError' constraint to an 'IO'
-- action for a given type. It can replace 'ExceptT'.
--
-- Note that 'catchError' will use the behavior from "UnliftIO" - so catch won't
-- catch an asynchronous exception.
--
-- @since 0.1.0.0
newtype ExceptIOT e m a = ExceptIOT { forall e (m :: * -> *) a. ExceptIOT e m a -> m a
unsafeRunExceptIOT :: m a }
    deriving newtype
        ( forall a b. a -> ExceptIOT e m b -> ExceptIOT e m a
forall a b. (a -> b) -> ExceptIOT e m a -> ExceptIOT e m b
forall e (m :: * -> *) a b.
Functor m =>
a -> ExceptIOT e m b -> ExceptIOT e m a
forall e (m :: * -> *) a b.
Functor m =>
(a -> b) -> ExceptIOT e m a -> ExceptIOT e m b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: forall a b. a -> ExceptIOT e m b -> ExceptIOT e m a
$c<$ :: forall e (m :: * -> *) a b.
Functor m =>
a -> ExceptIOT e m b -> ExceptIOT e m a
fmap :: forall a b. (a -> b) -> ExceptIOT e m a -> ExceptIOT e m b
$cfmap :: forall e (m :: * -> *) a b.
Functor m =>
(a -> b) -> ExceptIOT e m a -> ExceptIOT e m b
Functor, forall a. a -> ExceptIOT e m a
forall a b. ExceptIOT e m a -> ExceptIOT e m b -> ExceptIOT e m a
forall a b. ExceptIOT e m a -> ExceptIOT e m b -> ExceptIOT e m b
forall a b.
ExceptIOT e m (a -> b) -> ExceptIOT e m a -> ExceptIOT e m b
forall a b c.
(a -> b -> c)
-> ExceptIOT e m a -> ExceptIOT e m b -> ExceptIOT e m c
forall {e} {m :: * -> *}. Applicative m => Functor (ExceptIOT e m)
forall e (m :: * -> *) a. Applicative m => a -> ExceptIOT e m a
forall e (m :: * -> *) a b.
Applicative m =>
ExceptIOT e m a -> ExceptIOT e m b -> ExceptIOT e m a
forall e (m :: * -> *) a b.
Applicative m =>
ExceptIOT e m a -> ExceptIOT e m b -> ExceptIOT e m b
forall e (m :: * -> *) a b.
Applicative m =>
ExceptIOT e m (a -> b) -> ExceptIOT e m a -> ExceptIOT e m b
forall e (m :: * -> *) a b c.
Applicative m =>
(a -> b -> c)
-> ExceptIOT e m a -> ExceptIOT e m b -> ExceptIOT e m c
forall (f :: * -> *).
Functor f
-> (forall a. a -> f a)
-> (forall a b. f (a -> b) -> f a -> f b)
-> (forall a b c. (a -> b -> c) -> f a -> f b -> f c)
-> (forall a b. f a -> f b -> f b)
-> (forall a b. f a -> f b -> f a)
-> Applicative f
<* :: forall a b. ExceptIOT e m a -> ExceptIOT e m b -> ExceptIOT e m a
$c<* :: forall e (m :: * -> *) a b.
Applicative m =>
ExceptIOT e m a -> ExceptIOT e m b -> ExceptIOT e m a
*> :: forall a b. ExceptIOT e m a -> ExceptIOT e m b -> ExceptIOT e m b
$c*> :: forall e (m :: * -> *) a b.
Applicative m =>
ExceptIOT e m a -> ExceptIOT e m b -> ExceptIOT e m b
liftA2 :: forall a b c.
(a -> b -> c)
-> ExceptIOT e m a -> ExceptIOT e m b -> ExceptIOT e m c
$cliftA2 :: forall e (m :: * -> *) a b c.
Applicative m =>
(a -> b -> c)
-> ExceptIOT e m a -> ExceptIOT e m b -> ExceptIOT e m c
<*> :: forall a b.
ExceptIOT e m (a -> b) -> ExceptIOT e m a -> ExceptIOT e m b
$c<*> :: forall e (m :: * -> *) a b.
Applicative m =>
ExceptIOT e m (a -> b) -> ExceptIOT e m a -> ExceptIOT e m b
pure :: forall a. a -> ExceptIOT e m a
$cpure :: forall e (m :: * -> *) a. Applicative m => a -> ExceptIOT e m a
Applicative, forall a. a -> ExceptIOT e m a
forall a b. ExceptIOT e m a -> ExceptIOT e m b -> ExceptIOT e m b
forall a b.
ExceptIOT e m a -> (a -> ExceptIOT e m b) -> ExceptIOT e m b
forall {e} {m :: * -> *}. Monad m => Applicative (ExceptIOT e m)
forall e (m :: * -> *) a. Monad m => a -> ExceptIOT e m a
forall e (m :: * -> *) a b.
Monad m =>
ExceptIOT e m a -> ExceptIOT e m b -> ExceptIOT e m b
forall e (m :: * -> *) a b.
Monad m =>
ExceptIOT e m a -> (a -> ExceptIOT e m b) -> ExceptIOT e m b
forall (m :: * -> *).
Applicative m
-> (forall a b. m a -> (a -> m b) -> m b)
-> (forall a b. m a -> m b -> m b)
-> (forall a. a -> m a)
-> Monad m
return :: forall a. a -> ExceptIOT e m a
$creturn :: forall e (m :: * -> *) a. Monad m => a -> ExceptIOT e m a
>> :: forall a b. ExceptIOT e m a -> ExceptIOT e m b -> ExceptIOT e m b
$c>> :: forall e (m :: * -> *) a b.
Monad m =>
ExceptIOT e m a -> ExceptIOT e m b -> ExceptIOT e m b
>>= :: forall a b.
ExceptIOT e m a -> (a -> ExceptIOT e m b) -> ExceptIOT e m b
$c>>= :: forall e (m :: * -> *) a b.
Monad m =>
ExceptIOT e m a -> (a -> ExceptIOT e m b) -> ExceptIOT e m b
Monad, forall a. IO a -> ExceptIOT e m a
forall {e} {m :: * -> *}. MonadIO m => Monad (ExceptIOT e m)
forall e (m :: * -> *) a. MonadIO m => IO a -> ExceptIOT e m a
forall (m :: * -> *).
Monad m -> (forall a. IO a -> m a) -> MonadIO m
liftIO :: forall a. IO a -> ExceptIOT e m a
$cliftIO :: forall e (m :: * -> *) a. MonadIO m => IO a -> ExceptIOT e m a
MonadIO, forall a. ExceptIOT e m a
forall a. ExceptIOT e m a -> ExceptIOT e m a -> ExceptIOT e m a
forall {e} {m :: * -> *}. MonadPlus m => Monad (ExceptIOT e m)
forall {e} {m :: * -> *}.
MonadPlus m =>
Alternative (ExceptIOT e m)
forall e (m :: * -> *) a. MonadPlus m => ExceptIOT e m a
forall e (m :: * -> *) a.
MonadPlus m =>
ExceptIOT e m a -> ExceptIOT e m a -> ExceptIOT e m a
forall (m :: * -> *).
Alternative m
-> Monad m
-> (forall a. m a)
-> (forall a. m a -> m a -> m a)
-> MonadPlus m
mplus :: forall a. ExceptIOT e m a -> ExceptIOT e m a -> ExceptIOT e m a
$cmplus :: forall e (m :: * -> *) a.
MonadPlus m =>
ExceptIOT e m a -> ExceptIOT e m a -> ExceptIOT e m a
mzero :: forall a. ExceptIOT e m a
$cmzero :: forall e (m :: * -> *) a. MonadPlus m => ExceptIOT e m a
MonadPlus, forall a. ExceptIOT e m a
forall a. ExceptIOT e m a -> ExceptIOT e m [a]
forall a. ExceptIOT e m a -> ExceptIOT e m a -> ExceptIOT e m a
forall {e} {m :: * -> *}.
Alternative m =>
Applicative (ExceptIOT e m)
forall e (m :: * -> *) a. Alternative m => ExceptIOT e m a
forall e (m :: * -> *) a.
Alternative m =>
ExceptIOT e m a -> ExceptIOT e m [a]
forall e (m :: * -> *) a.
Alternative m =>
ExceptIOT e m a -> ExceptIOT e m a -> ExceptIOT e m a
forall (f :: * -> *).
Applicative f
-> (forall a. f a)
-> (forall a. f a -> f a -> f a)
-> (forall a. f a -> f [a])
-> (forall a. f a -> f [a])
-> Alternative f
many :: forall a. ExceptIOT e m a -> ExceptIOT e m [a]
$cmany :: forall e (m :: * -> *) a.
Alternative m =>
ExceptIOT e m a -> ExceptIOT e m [a]
some :: forall a. ExceptIOT e m a -> ExceptIOT e m [a]
$csome :: forall e (m :: * -> *) a.
Alternative m =>
ExceptIOT e m a -> ExceptIOT e m [a]
<|> :: forall a. ExceptIOT e m a -> ExceptIOT e m a -> ExceptIOT e m a
$c<|> :: forall e (m :: * -> *) a.
Alternative m =>
ExceptIOT e m a -> ExceptIOT e m a -> ExceptIOT e m a
empty :: forall a. ExceptIOT e m a
$cempty :: forall e (m :: * -> *) a. Alternative m => ExceptIOT e m a
Alternative
        , NonEmpty (ExceptIOT e m a) -> ExceptIOT e m a
ExceptIOT e m a -> ExceptIOT e m a -> ExceptIOT e m a
forall b. Integral b => b -> ExceptIOT e m a -> ExceptIOT e m a
forall a.
(a -> a -> a)
-> (NonEmpty a -> a)
-> (forall b. Integral b => b -> a -> a)
-> Semigroup a
forall e (m :: * -> *) a.
Semigroup (m a) =>
NonEmpty (ExceptIOT e m a) -> ExceptIOT e m a
forall e (m :: * -> *) a.
Semigroup (m a) =>
ExceptIOT e m a -> ExceptIOT e m a -> ExceptIOT e m a
forall e (m :: * -> *) a b.
(Semigroup (m a), Integral b) =>
b -> ExceptIOT e m a -> ExceptIOT e m a
stimes :: forall b. Integral b => b -> ExceptIOT e m a -> ExceptIOT e m a
$cstimes :: forall e (m :: * -> *) a b.
(Semigroup (m a), Integral b) =>
b -> ExceptIOT e m a -> ExceptIOT e m a
sconcat :: NonEmpty (ExceptIOT e m a) -> ExceptIOT e m a
$csconcat :: forall e (m :: * -> *) a.
Semigroup (m a) =>
NonEmpty (ExceptIOT e m a) -> ExceptIOT e m a
<> :: ExceptIOT e m a -> ExceptIOT e m a -> ExceptIOT e m a
$c<> :: forall e (m :: * -> *) a.
Semigroup (m a) =>
ExceptIOT e m a -> ExceptIOT e m a -> ExceptIOT e m a
Semigroup, ExceptIOT e m a
[ExceptIOT e m a] -> ExceptIOT e m a
ExceptIOT e m a -> ExceptIOT e m a -> ExceptIOT e m a
forall a.
Semigroup a -> a -> (a -> a -> a) -> ([a] -> a) -> Monoid a
forall {e} {m :: * -> *} {a}.
Monoid (m a) =>
Semigroup (ExceptIOT e m a)
forall e (m :: * -> *) a. Monoid (m a) => ExceptIOT e m a
forall e (m :: * -> *) a.
Monoid (m a) =>
[ExceptIOT e m a] -> ExceptIOT e m a
forall e (m :: * -> *) a.
Monoid (m a) =>
ExceptIOT e m a -> ExceptIOT e m a -> ExceptIOT e m a
mconcat :: [ExceptIOT e m a] -> ExceptIOT e m a
$cmconcat :: forall e (m :: * -> *) a.
Monoid (m a) =>
[ExceptIOT e m a] -> ExceptIOT e m a
mappend :: ExceptIOT e m a -> ExceptIOT e m a -> ExceptIOT e m a
$cmappend :: forall e (m :: * -> *) a.
Monoid (m a) =>
ExceptIOT e m a -> ExceptIOT e m a -> ExceptIOT e m a
mempty :: ExceptIOT e m a
$cmempty :: forall e (m :: * -> *) a. Monoid (m a) => ExceptIOT e m a
Monoid, forall b.
((forall a. ExceptIOT e m a -> IO a) -> IO b) -> ExceptIOT e m b
forall {e} {m :: * -> *}.
MonadUnliftIO m =>
MonadIO (ExceptIOT e m)
forall e (m :: * -> *) b.
MonadUnliftIO m =>
((forall a. ExceptIOT e m a -> IO a) -> IO b) -> ExceptIOT e m b
forall (m :: * -> *).
MonadIO m
-> (forall b. ((forall a. m a -> IO a) -> IO b) -> m b)
-> MonadUnliftIO m
withRunInIO :: forall b.
((forall a. ExceptIOT e m a -> IO a) -> IO b) -> ExceptIOT e m b
$cwithRunInIO :: forall e (m :: * -> *) b.
MonadUnliftIO m =>
((forall a. ExceptIOT e m a -> IO a) -> IO b) -> ExceptIOT e m b
MonadUnliftIO, MonadReader r, MonadState s
        , MonadWriter w, forall a. (a -> ExceptIOT e m a) -> ExceptIOT e m a
forall {e} {m :: * -> *}. MonadFix m => Monad (ExceptIOT e m)
forall e (m :: * -> *) a.
MonadFix m =>
(a -> ExceptIOT e m a) -> ExceptIOT e m a
forall (m :: * -> *).
Monad m -> (forall a. (a -> m a) -> m a) -> MonadFix m
mfix :: forall a. (a -> ExceptIOT e m a) -> ExceptIOT e m a
$cmfix :: forall e (m :: * -> *) a.
MonadFix m =>
(a -> ExceptIOT e m a) -> ExceptIOT e m a
MonadFix, forall a. String -> ExceptIOT e m a
forall {e} {m :: * -> *}. MonadFail m => Monad (ExceptIOT e m)
forall e (m :: * -> *) a. MonadFail m => String -> ExceptIOT e m a
forall (m :: * -> *).
Monad m -> (forall a. String -> m a) -> MonadFail m
fail :: forall a. String -> ExceptIOT e m a
$cfail :: forall e (m :: * -> *) a. MonadFail m => String -> ExceptIOT e m a
MonadFail, forall e a. Exception e => e -> ExceptIOT e m a
forall {e} {m :: * -> *}. MonadThrow m => Monad (ExceptIOT e m)
forall e (m :: * -> *) e a.
(MonadThrow m, Exception e) =>
e -> ExceptIOT e m a
forall (m :: * -> *).
Monad m -> (forall e a. Exception e => e -> m a) -> MonadThrow m
throwM :: forall e a. Exception e => e -> ExceptIOT e m a
$cthrowM :: forall e (m :: * -> *) e a.
(MonadThrow m, Exception e) =>
e -> ExceptIOT e m a
MonadThrow, forall e a.
Exception e =>
ExceptIOT e m a -> (e -> ExceptIOT e m a) -> ExceptIOT e m a
forall {e} {m :: * -> *}.
MonadCatch m =>
MonadThrow (ExceptIOT e m)
forall e (m :: * -> *) e a.
(MonadCatch m, Exception e) =>
ExceptIOT e m a -> (e -> ExceptIOT e m a) -> ExceptIOT e m a
forall (m :: * -> *).
MonadThrow m
-> (forall e a. Exception e => m a -> (e -> m a) -> m a)
-> MonadCatch m
catch :: forall e a.
Exception e =>
ExceptIOT e m a -> (e -> ExceptIOT e m a) -> ExceptIOT e m a
$ccatch :: forall e (m :: * -> *) e a.
(MonadCatch m, Exception e) =>
ExceptIOT e m a -> (e -> ExceptIOT e m a) -> ExceptIOT e m a
MonadCatch, forall b.
((forall a. ExceptIOT e m a -> ExceptIOT e m a) -> ExceptIOT e m b)
-> ExceptIOT e m b
forall a b c.
ExceptIOT e m a
-> (a -> ExitCase b -> ExceptIOT e m c)
-> (a -> ExceptIOT e m b)
-> ExceptIOT e m (b, c)
forall {e} {m :: * -> *}. MonadMask m => MonadCatch (ExceptIOT e m)
forall e (m :: * -> *) b.
MonadMask m =>
((forall a. ExceptIOT e m a -> ExceptIOT e m a) -> ExceptIOT e m b)
-> ExceptIOT e m b
forall e (m :: * -> *) a b c.
MonadMask m =>
ExceptIOT e m a
-> (a -> ExitCase b -> ExceptIOT e m c)
-> (a -> ExceptIOT e m b)
-> ExceptIOT e m (b, c)
forall (m :: * -> *).
MonadCatch m
-> (forall b. ((forall a. m a -> m a) -> m b) -> m b)
-> (forall b. ((forall a. m a -> m a) -> m b) -> m b)
-> (forall a b c.
    m a -> (a -> ExitCase b -> m c) -> (a -> m b) -> m (b, c))
-> MonadMask m
generalBracket :: forall a b c.
ExceptIOT e m a
-> (a -> ExitCase b -> ExceptIOT e m c)
-> (a -> ExceptIOT e m b)
-> ExceptIOT e m (b, c)
$cgeneralBracket :: forall e (m :: * -> *) a b c.
MonadMask m =>
ExceptIOT e m a
-> (a -> ExitCase b -> ExceptIOT e m c)
-> (a -> ExceptIOT e m b)
-> ExceptIOT e m (b, c)
uninterruptibleMask :: forall b.
((forall a. ExceptIOT e m a -> ExceptIOT e m a) -> ExceptIOT e m b)
-> ExceptIOT e m b
$cuninterruptibleMask :: forall e (m :: * -> *) b.
MonadMask m =>
((forall a. ExceptIOT e m a -> ExceptIOT e m a) -> ExceptIOT e m b)
-> ExceptIOT e m b
mask :: forall b.
((forall a. ExceptIOT e m a -> ExceptIOT e m a) -> ExceptIOT e m b)
-> ExceptIOT e m b
$cmask :: forall e (m :: * -> *) b.
MonadMask m =>
((forall a. ExceptIOT e m a -> ExceptIOT e m a) -> ExceptIOT e m b)
-> ExceptIOT e m b
MonadMask
        )

-- |
--
-- @since 0.1.0.0
instance (MonadUnliftIO m, Exception e) => MonadError e (ExceptIOT e m) where
    throwError :: forall a. e -> ExceptIOT e m a
throwError = forall (m :: * -> *) e a. (MonadIO m, Exception e) => e -> m a
throwIO
    catchError :: forall a.
ExceptIOT e m a -> (e -> ExceptIOT e m a) -> ExceptIOT e m a
catchError = forall (m :: * -> *) e a.
(MonadUnliftIO m, Exception e) =>
m a -> (e -> m a) -> m a
catch

-- | Run an 'ExceptIOT' action. This catches the thrown exception, but only if
-- it is the @e@ that the type mentions. All other exceptions will remain
-- uncaught.
--
-- @since 0.1.0.0
runExceptIOT :: (Exception e, MonadUnliftIO m) => ExceptIOT e m a -> m (Either e a)
runExceptIOT :: forall e (m :: * -> *) a.
(Exception e, MonadUnliftIO m) =>
ExceptIOT e m a -> m (Either e a)
runExceptIOT = forall (m :: * -> *) e a.
(MonadUnliftIO m, Exception e) =>
m a -> m (Either e a)
try forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall e (m :: * -> *) a. ExceptIOT e m a -> m a
unsafeRunExceptIOT

-- | Like 'Except.modifyError', but it selects the 'ExceptIOT' instance for 'IO'
-- exceptions instead of the 'ExceptT' instance with an 'Either' error.
--
-- @since 0.1.0.0
modifyError
    :: (Exception e, MonadUnliftIO m, MonadError e' m)
    => (e -> e') -> ExceptIOT e m a -> m a
modifyError :: forall e (m :: * -> *) e' a.
(Exception e, MonadUnliftIO m, MonadError e' m) =>
(e -> e') -> ExceptIOT e m a -> m a
modifyError e -> e'
f ExceptIOT e m a
action =
    forall e (m :: * -> *) a.
(Exception e, MonadUnliftIO m) =>
ExceptIOT e m a -> m (Either e a)
runExceptIOT ExceptIOT e m a
action forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError forall b c a. (b -> c) -> (a -> b) -> a -> c
. e -> e'
f) forall (f :: * -> *) a. Applicative f => a -> f a
pure