{-# OPTIONS_HADDOCK not-home #-}
{-# LANGUAGE UndecidableInstances #-}
module Control.Monad.Validate.Internal where
import Control.Monad ((<=<))
import Control.Monad.IO.Class
import Control.Monad.Base
import Control.Monad.Catch
import Control.Monad.Except
import Control.Monad.Fix
import Control.Monad.Reader.Class
import Control.Monad.State.Strict
import Control.Monad.Trans.Control
import Control.Monad.Writer.Class
import Data.Functor
import Data.Functor.Identity
import Data.Tuple (swap)
import Control.Monad.Validate.Class
newtype ValidateT e m a = ValidateT
{ forall e (m :: * -> *) a.
ValidateT e m a
-> forall (s :: MonoMaybeS). StateT (MonoMaybe s e) (ExceptT e m) a
getValidateT :: forall s. StateT (MonoMaybe s e) (ExceptT e m) a }
deriving instance (Functor m) => Functor (ValidateT e m)
validateT
:: forall e m a. (Functor m)
=> (forall s. MonoMaybe s e -> m (Either e (MonoMaybe s e, a)))
-> ValidateT e m a
validateT :: forall e (m :: * -> *) a.
Functor m =>
(forall (s :: MonoMaybeS).
MonoMaybe s e -> m (Either e (MonoMaybe s e, a)))
-> ValidateT e m a
validateT forall (s :: MonoMaybeS).
MonoMaybe s e -> m (Either e (MonoMaybe s e, a))
f = forall e (m :: * -> *) a.
(forall (s :: MonoMaybeS). StateT (MonoMaybe s e) (ExceptT e m) a)
-> ValidateT e m a
ValidateT (forall s (m :: * -> *) a. (s -> m (a, s)) -> StateT s m a
StateT (forall e (m :: * -> *) a. m (Either e a) -> ExceptT e m a
ExceptT forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a b. (a, b) -> (b, a)
swap) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (s :: MonoMaybeS).
MonoMaybe s e -> m (Either e (MonoMaybe s e, a))
f)))
{-# INLINE validateT #-}
unValidateT
:: forall s e m a. (Functor m)
=> MonoMaybe s e -> ValidateT e m a -> m (Either e (MonoMaybe s e, a))
unValidateT :: forall (s :: MonoMaybeS) e (m :: * -> *) a.
Functor m =>
MonoMaybe s e -> ValidateT e m a -> m (Either e (MonoMaybe s e, a))
unValidateT MonoMaybe s e
e (ValidateT forall (s :: MonoMaybeS). StateT (MonoMaybe s e) (ExceptT e m) a
m) = forall e (m :: * -> *) a. ExceptT e m a -> m (Either e a)
runExceptT (forall a b. (a, b) -> (b, a)
swap forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall s (m :: * -> *) a. StateT s m a -> s -> m (a, s)
runStateT forall (s :: MonoMaybeS). StateT (MonoMaybe s e) (ExceptT e m) a
m MonoMaybe s e
e)
{-# INLINE unValidateT #-}
instance (Monad m) => Applicative (ValidateT e m) where
pure :: forall a. a -> ValidateT e m a
pure a
v = forall e (m :: * -> *) a.
(forall (s :: MonoMaybeS). StateT (MonoMaybe s e) (ExceptT e m) a)
-> ValidateT e m a
ValidateT (forall (f :: * -> *) a. Applicative f => a -> f a
pure a
v)
{-# INLINE pure #-}
ValidateT e m (a -> b)
m1 <*> :: forall a b.
ValidateT e m (a -> b) -> ValidateT e m a -> ValidateT e m b
<*> ValidateT e m a
m2 = forall e (m :: * -> *) a.
Functor m =>
(forall (s :: MonoMaybeS).
MonoMaybe s e -> m (Either e (MonoMaybe s e, a)))
-> ValidateT e m a
validateT forall a b. (a -> b) -> a -> b
$ \MonoMaybe s e
e0 ->
forall (s :: MonoMaybeS) e (m :: * -> *) a.
Functor m =>
MonoMaybe s e -> ValidateT e m a -> m (Either e (MonoMaybe s e, a))
unValidateT MonoMaybe s e
e0 ValidateT e m (a -> b)
m1 forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
Left e
e1 -> forall (s :: MonoMaybeS) e (m :: * -> *) a.
Functor m =>
MonoMaybe s e -> ValidateT e m a -> m (Either e (MonoMaybe s e, a))
unValidateT (forall (s :: MonoMaybeS) a. a -> MonoMaybe s a
MJust @'SJust e
e1) ValidateT e m a
m2 forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \case
Left e
e2 -> forall a b. a -> Either a b
Left e
e2
Right (MJust e
e2, a
_) -> forall a b. a -> Either a b
Left e
e2
Right (MonoMaybe s e
e1, a -> b
v1) -> forall (s :: MonoMaybeS) e (m :: * -> *) a.
Functor m =>
MonoMaybe s e -> ValidateT e m a -> m (Either e (MonoMaybe s e, a))
unValidateT MonoMaybe s e
e1 ValidateT e m a
m2 forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \case
Left e
e2 -> forall a b. a -> Either a b
Left e
e2
Right (MonoMaybe s e
e2, a
v2) -> forall a b. b -> Either a b
Right (MonoMaybe s e
e2, a -> b
v1 a
v2)
{-# INLINABLE (<*>) #-}
instance (Monad m) => Monad (ValidateT e m) where
ValidateT forall (s :: MonoMaybeS). StateT (MonoMaybe s e) (ExceptT e m) a
m >>= :: forall a b.
ValidateT e m a -> (a -> ValidateT e m b) -> ValidateT e m b
>>= a -> ValidateT e m b
f = forall e (m :: * -> *) a.
(forall (s :: MonoMaybeS). StateT (MonoMaybe s e) (ExceptT e m) a)
-> ValidateT e m a
ValidateT (forall (s :: MonoMaybeS). StateT (MonoMaybe s e) (ExceptT e m) a
m forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \a
x -> forall e (m :: * -> *) a.
ValidateT e m a
-> forall (s :: MonoMaybeS). StateT (MonoMaybe s e) (ExceptT e m) a
getValidateT (a -> ValidateT e m b
f a
x))
{-# INLINE (>>=) #-}
instance MonadTrans (ValidateT e) where
lift :: forall (m :: * -> *) a. Monad m => m a -> ValidateT e m a
lift m a
m = forall e (m :: * -> *) a.
(forall (s :: MonoMaybeS). StateT (MonoMaybe s e) (ExceptT e m) a)
-> ValidateT e m a
ValidateT (forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> b
$ forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m a
m)
{-# INLINE lift #-}
instance (MonadFix m) => MonadFix (ValidateT e m) where
mfix :: forall a. (a -> ValidateT e m a) -> ValidateT e m a
mfix a -> ValidateT e m a
f = forall e (m :: * -> *) a.
(forall (s :: MonoMaybeS). StateT (MonoMaybe s e) (ExceptT e m) a)
-> ValidateT e m a
ValidateT forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadFix m => (a -> m a) -> m a
mfix (\a
x -> forall e (m :: * -> *) a.
ValidateT e m a
-> forall (s :: MonoMaybeS). StateT (MonoMaybe s e) (ExceptT e m) a
getValidateT (a -> ValidateT e m a
f a
x))
{-# INLINE mfix #-}
instance (MonadIO m) => MonadIO (ValidateT e m) where
liftIO :: forall a. IO a -> ValidateT e m a
liftIO = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO
{-# INLINE liftIO #-}
instance (MonadBase b m) => MonadBase b (ValidateT e m) where
liftBase :: forall α. b α -> ValidateT e m α
liftBase = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (b :: * -> *) (m :: * -> *) α. MonadBase b m => b α -> m α
liftBase
{-# INLINE liftBase #-}
data ValidateTState e a = forall s. ValidateTState
{ ()
getValidateTState :: Either e (MonoMaybe s e, a) }
deriving instance (Show e, Show a) => Show (ValidateTState e a)
deriving instance Functor (ValidateTState e)
instance MonadTransControl (ValidateT e) where
type StT (ValidateT e) a = ValidateTState e a
liftWith :: forall (m :: * -> *) a.
Monad m =>
(Run (ValidateT e) -> m a) -> ValidateT e m a
liftWith Run (ValidateT e) -> m a
f = forall e (m :: * -> *) a.
Functor m =>
(forall (s :: MonoMaybeS).
MonoMaybe s e -> m (Either e (MonoMaybe s e, a)))
-> ValidateT e m a
validateT forall a b. (a -> b) -> a -> b
$ \MonoMaybe s e
e ->
forall a b. b -> Either a b
Right forall b c a. (b -> c) -> (a -> b) -> a -> c
. (MonoMaybe s e
e,) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Run (ValidateT e) -> m a
f (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall e a (s :: MonoMaybeS).
Either e (MonoMaybe s e, a) -> ValidateTState e a
ValidateTState forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (s :: MonoMaybeS) e (m :: * -> *) a.
Functor m =>
MonoMaybe s e -> ValidateT e m a -> m (Either e (MonoMaybe s e, a))
unValidateT MonoMaybe s e
e)
{-# INLINABLE liftWith #-}
restoreT :: forall (m :: * -> *) a.
Monad m =>
m (StT (ValidateT e) a) -> ValidateT e m a
restoreT m (StT (ValidateT e) a)
m = forall e (m :: * -> *) a.
Functor m =>
(forall (s :: MonoMaybeS).
MonoMaybe s e -> m (Either e (MonoMaybe s e, a)))
-> ValidateT e m a
validateT forall a b. (a -> b) -> a -> b
$ \MonoMaybe s e
e1 -> do
ValidateTState Either e (MonoMaybe s e, a)
r <- m (StT (ValidateT e) a)
m
case MonoMaybe s e
e1 of
MonoMaybe s e
MNothing -> case Either e (MonoMaybe s e, a)
r of
Left e
e2 -> forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ forall a b. a -> Either a b
Left e
e2
Right (MJust e
e2, a
v) -> forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ forall a b. b -> Either a b
Right (forall (s :: MonoMaybeS) a. a -> MonoMaybe s a
MJust e
e2, a
v)
Right (MonoMaybe s e
MNothing, a
v) -> forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ forall a b. b -> Either a b
Right (forall a. MonoMaybe 'SMaybe a
MNothing, a
v)
MJust e
_ -> case Either e (MonoMaybe s e, a)
r of
Left e
e2 -> forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ forall a b. a -> Either a b
Left e
e2
Right (MJust e
e2, a
v) -> forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ forall a b. b -> Either a b
Right (forall (s :: MonoMaybeS) a. a -> MonoMaybe s a
MJust e
e2, a
v)
Right (MonoMaybe s e
MNothing, a
_) -> forall a. a
invalidRestoreError
{-# INLINABLE restoreT #-}
invalidRestoreError :: a
invalidRestoreError :: forall a. a
invalidRestoreError = forall a. HasCallStack => String -> a
error
String
"Control.Monad.Validate.ValidateT#restoreT: panic!\n\
\ An attempt was made to restore from a state captured before any validation\n\
\ errors occurred into a context with validation errors. This is probably the\n\
\ result of an incorrect use of MonadBaseControl (as validation errors should\n\
\ strictly increase). Ensure that all state is restored immediately upon\n\
\ returning from the base monad (or is not restored at all).\n\
\\n\
\ If you believe your use of MonadBaseControl is not in error, and this is a\n\
\ bug in ValidateT, please submit a bug report."
instance (MonadBaseControl b m) => MonadBaseControl b (ValidateT e m) where
type StM (ValidateT e m) a = ComposeSt (ValidateT e) m a
liftBaseWith :: forall a. (RunInBase (ValidateT e m) b -> b a) -> ValidateT e m a
liftBaseWith = forall (t :: (* -> *) -> * -> *) (b :: * -> *) (m :: * -> *) a.
(MonadTransControl t, MonadBaseControl b m) =>
(RunInBaseDefault t m b -> b a) -> t m a
defaultLiftBaseWith
restoreM :: forall a. StM (ValidateT e m) a -> ValidateT e m a
restoreM = forall (t :: (* -> *) -> * -> *) (b :: * -> *) (m :: * -> *) a.
(MonadTransControl t, MonadBaseControl b m) =>
ComposeSt t m a -> t m a
defaultRestoreM
{-# INLINE liftBaseWith #-}
{-# INLINE restoreM #-}
liftCatch
:: (Functor m)
=> (forall b. m b -> (e -> m b) -> m b)
-> ValidateT d m a -> (e -> ValidateT d m a) -> ValidateT d m a
liftCatch :: forall (m :: * -> *) e d a.
Functor m =>
(forall b. m b -> (e -> m b) -> m b)
-> ValidateT d m a -> (e -> ValidateT d m a) -> ValidateT d m a
liftCatch forall b. m b -> (e -> m b) -> m b
catchE ValidateT d m a
m e -> ValidateT d m a
f = forall e (m :: * -> *) a.
Functor m =>
(forall (s :: MonoMaybeS).
MonoMaybe s e -> m (Either e (MonoMaybe s e, a)))
-> ValidateT e m a
validateT forall a b. (a -> b) -> a -> b
$ \MonoMaybe s d
e ->
forall b. m b -> (e -> m b) -> m b
catchE (forall (s :: MonoMaybeS) e (m :: * -> *) a.
Functor m =>
MonoMaybe s e -> ValidateT e m a -> m (Either e (MonoMaybe s e, a))
unValidateT MonoMaybe s d
e ValidateT d m a
m) (forall (s :: MonoMaybeS) e (m :: * -> *) a.
Functor m =>
MonoMaybe s e -> ValidateT e m a -> m (Either e (MonoMaybe s e, a))
unValidateT MonoMaybe s d
e forall b c a. (b -> c) -> (a -> b) -> a -> c
. e -> ValidateT d m a
f)
{-# INLINE liftCatch #-}
instance (MonadError e m) => MonadError e (ValidateT a m) where
throwError :: forall a. e -> ValidateT a m a
throwError = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError
catchError :: forall a.
ValidateT a m a -> (e -> ValidateT a m a) -> ValidateT a m a
catchError = forall (m :: * -> *) e d a.
Functor m =>
(forall b. m b -> (e -> m b) -> m b)
-> ValidateT d m a -> (e -> ValidateT d m a) -> ValidateT d m a
liftCatch forall e (m :: * -> *) a.
MonadError e m =>
m a -> (e -> m a) -> m a
catchError
{-# INLINE throwError #-}
{-# INLINE catchError #-}
instance (MonadReader r m) => MonadReader r (ValidateT e m) where
ask :: ValidateT e m r
ask = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall r (m :: * -> *). MonadReader r m => m r
ask
local :: forall a. (r -> r) -> ValidateT e m a -> ValidateT e m a
local r -> r
f (ValidateT forall (s :: MonoMaybeS). StateT (MonoMaybe s e) (ExceptT e m) a
m) = forall e (m :: * -> *) a.
(forall (s :: MonoMaybeS). StateT (MonoMaybe s e) (ExceptT e m) a)
-> ValidateT e m a
ValidateT (forall r (m :: * -> *) a. MonadReader r m => (r -> r) -> m a -> m a
local r -> r
f forall (s :: MonoMaybeS). StateT (MonoMaybe s e) (ExceptT e m) a
m)
reader :: forall a. (r -> a) -> ValidateT e m a
reader = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
reader
{-# INLINE ask #-}
{-# INLINE local #-}
{-# INLINE reader #-}
instance (MonadState s m) => MonadState s (ValidateT e m) where
get :: ValidateT e m s
get = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall s (m :: * -> *). MonadState s m => m s
get
put :: s -> ValidateT e m ()
put = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s (m :: * -> *). MonadState s m => s -> m ()
put
state :: forall a. (s -> (a, s)) -> ValidateT e m a
state = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s (m :: * -> *) a. MonadState s m => (s -> (a, s)) -> m a
state
{-# INLINE get #-}
{-# INLINE put #-}
{-# INLINE state #-}
instance (MonadWriter w m) => MonadWriter w (ValidateT e m) where
writer :: forall a. (a, w) -> ValidateT e m a
writer = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall w (m :: * -> *) a. MonadWriter w m => (a, w) -> m a
writer
tell :: w -> ValidateT e m ()
tell = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell
listen :: forall a. ValidateT e m a -> ValidateT e m (a, w)
listen (ValidateT forall (s :: MonoMaybeS). StateT (MonoMaybe s e) (ExceptT e m) a
m) = forall e (m :: * -> *) a.
(forall (s :: MonoMaybeS). StateT (MonoMaybe s e) (ExceptT e m) a)
-> ValidateT e m a
ValidateT (forall w (m :: * -> *) a. MonadWriter w m => m a -> m (a, w)
listen forall (s :: MonoMaybeS). StateT (MonoMaybe s e) (ExceptT e m) a
m)
pass :: forall a. ValidateT e m (a, w -> w) -> ValidateT e m a
pass (ValidateT forall (s :: MonoMaybeS).
StateT (MonoMaybe s e) (ExceptT e m) (a, w -> w)
m) = forall e (m :: * -> *) a.
(forall (s :: MonoMaybeS). StateT (MonoMaybe s e) (ExceptT e m) a)
-> ValidateT e m a
ValidateT (forall w (m :: * -> *) a. MonadWriter w m => m (a, w -> w) -> m a
pass forall (s :: MonoMaybeS).
StateT (MonoMaybe s e) (ExceptT e m) (a, w -> w)
m)
{-# INLINE writer #-}
{-# INLINE tell #-}
{-# INLINE listen #-}
{-# INLINE pass #-}
instance (MonadThrow m) => MonadThrow (ValidateT e m) where
throwM :: forall e a. Exception e => e -> ValidateT e m a
throwM = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) e a. (MonadThrow m, Exception e) => e -> m a
throwM
{-# INLINE throwM #-}
instance (MonadCatch m) => MonadCatch (ValidateT e m) where
catch :: forall e a.
Exception e =>
ValidateT e m a -> (e -> ValidateT e m a) -> ValidateT e m a
catch = forall (m :: * -> *) e d a.
Functor m =>
(forall b. m b -> (e -> m b) -> m b)
-> ValidateT d m a -> (e -> ValidateT d m a) -> ValidateT d m a
liftCatch forall (m :: * -> *) e a.
(MonadCatch m, Exception e) =>
m a -> (e -> m a) -> m a
catch
{-# INLINE catch #-}
liftMask
:: (Functor m)
=> (forall c. ((forall a. m a -> m a) -> m c) -> m c)
-> ((forall a. ValidateT e m a -> ValidateT e m a) -> ValidateT e m b) -> ValidateT e m b
liftMask :: forall (m :: * -> *) e b.
Functor m =>
(forall c. ((forall a. m a -> m a) -> m c) -> m c)
-> ((forall a. ValidateT e m a -> ValidateT e m a)
-> ValidateT e m b)
-> ValidateT e m b
liftMask forall c. ((forall a. m a -> m a) -> m c) -> m c
maskE (forall a. ValidateT e m a -> ValidateT e m a) -> ValidateT e m b
f = forall e (m :: * -> *) a.
Functor m =>
(forall (s :: MonoMaybeS).
MonoMaybe s e -> m (Either e (MonoMaybe s e, a)))
-> ValidateT e m a
validateT forall a b. (a -> b) -> a -> b
$ \MonoMaybe s e
e1 ->
forall c. ((forall a. m a -> m a) -> m c) -> m c
maskE forall a b. (a -> b) -> a -> b
$ \forall a. m a -> m a
unmask ->
forall (s :: MonoMaybeS) e (m :: * -> *) a.
Functor m =>
MonoMaybe s e -> ValidateT e m a -> m (Either e (MonoMaybe s e, a))
unValidateT MonoMaybe s e
e1 forall a b. (a -> b) -> a -> b
$ (forall a. ValidateT e m a -> ValidateT e m a) -> ValidateT e m b
f forall a b. (a -> b) -> a -> b
$ \ValidateT e m a
m ->
forall e (m :: * -> *) a.
Functor m =>
(forall (s :: MonoMaybeS).
MonoMaybe s e -> m (Either e (MonoMaybe s e, a)))
-> ValidateT e m a
validateT forall a b. (a -> b) -> a -> b
$ \MonoMaybe s e
e2 ->
forall a. m a -> m a
unmask forall a b. (a -> b) -> a -> b
$ forall (s :: MonoMaybeS) e (m :: * -> *) a.
Functor m =>
MonoMaybe s e -> ValidateT e m a -> m (Either e (MonoMaybe s e, a))
unValidateT MonoMaybe s e
e2 ValidateT e m a
m
{-# INLINE liftMask #-}
instance (MonadMask m) => MonadMask (ValidateT e m) where
mask :: forall b.
((forall a. ValidateT e m a -> ValidateT e m a) -> ValidateT e m b)
-> ValidateT e m b
mask = forall (m :: * -> *) e b.
Functor m =>
(forall c. ((forall a. m a -> m a) -> m c) -> m c)
-> ((forall a. ValidateT e m a -> ValidateT e m a)
-> ValidateT e m b)
-> ValidateT e m b
liftMask forall (m :: * -> *) b.
MonadMask m =>
((forall a. m a -> m a) -> m b) -> m b
mask
uninterruptibleMask :: forall b.
((forall a. ValidateT e m a -> ValidateT e m a) -> ValidateT e m b)
-> ValidateT e m b
uninterruptibleMask = forall (m :: * -> *) e b.
Functor m =>
(forall c. ((forall a. m a -> m a) -> m c) -> m c)
-> ((forall a. ValidateT e m a -> ValidateT e m a)
-> ValidateT e m b)
-> ValidateT e m b
liftMask forall (m :: * -> *) b.
MonadMask m =>
((forall a. m a -> m a) -> m b) -> m b
uninterruptibleMask
generalBracket :: forall a b c.
ValidateT e m a
-> (a -> ExitCase b -> ValidateT e m c)
-> (a -> ValidateT e m b)
-> ValidateT e m (b, c)
generalBracket ValidateT e m a
m a -> ExitCase b -> ValidateT e m c
f a -> ValidateT e m b
g = forall e (m :: * -> *) a.
(forall (s :: MonoMaybeS). StateT (MonoMaybe s e) (ExceptT e m) a)
-> ValidateT e m a
ValidateT forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a b c.
MonadMask m =>
m a -> (a -> ExitCase b -> m c) -> (a -> m b) -> m (b, c)
generalBracket
(forall e (m :: * -> *) a.
ValidateT e m a
-> forall (s :: MonoMaybeS). StateT (MonoMaybe s e) (ExceptT e m) a
getValidateT ValidateT e m a
m)
(\a
a ExitCase b
b -> forall e (m :: * -> *) a.
ValidateT e m a
-> forall (s :: MonoMaybeS). StateT (MonoMaybe s e) (ExceptT e m) a
getValidateT forall a b. (a -> b) -> a -> b
$ a -> ExitCase b -> ValidateT e m c
f a
a ExitCase b
b)
(\a
a -> forall e (m :: * -> *) a.
ValidateT e m a
-> forall (s :: MonoMaybeS). StateT (MonoMaybe s e) (ExceptT e m) a
getValidateT forall a b. (a -> b) -> a -> b
$ a -> ValidateT e m b
g a
a)
{-# INLINE mask #-}
{-# INLINE uninterruptibleMask #-}
{-# INLINE generalBracket #-}
instance (Monad m, Semigroup e) => MonadValidate e (ValidateT e m) where
refute :: forall a. e -> ValidateT e m a
refute e
e2 = forall e (m :: * -> *) a.
Functor m =>
(forall (s :: MonoMaybeS).
MonoMaybe s e -> m (Either e (MonoMaybe s e, a)))
-> ValidateT e m a
validateT forall a b. (a -> b) -> a -> b
$ \MonoMaybe s e
e1 ->
let !e3 :: e
e3 = forall (s :: MonoMaybeS) b a.
((s ~ 'SMaybe) => b) -> (a -> b) -> MonoMaybe s a -> b
monoMaybe e
e2 (forall a. Semigroup a => a -> a -> a
<> e
e2) MonoMaybe s e
e1 in forall (f :: * -> *) a. Applicative f => a -> f a
pure (forall a b. a -> Either a b
Left e
e3)
dispute :: e -> ValidateT e m ()
dispute e
e2 = forall e (m :: * -> *) a.
Functor m =>
(forall (s :: MonoMaybeS).
MonoMaybe s e -> m (Either e (MonoMaybe s e, a)))
-> ValidateT e m a
validateT forall a b. (a -> b) -> a -> b
$ \MonoMaybe s e
e1 ->
let !e3 :: e
e3 = forall (s :: MonoMaybeS) b a.
((s ~ 'SMaybe) => b) -> (a -> b) -> MonoMaybe s a -> b
monoMaybe e
e2 (forall a. Semigroup a => a -> a -> a
<> e
e2) MonoMaybe s e
e1 in forall (f :: * -> *) a. Applicative f => a -> f a
pure (forall a b. b -> Either a b
Right (forall (s :: MonoMaybeS) a. a -> MonoMaybe s a
MJust e
e3, ()))
tolerate :: forall a. ValidateT e m a -> ValidateT e m (Maybe a)
tolerate ValidateT e m a
m = forall e (m :: * -> *) a.
Functor m =>
(forall (s :: MonoMaybeS).
MonoMaybe s e -> m (Either e (MonoMaybe s e, a)))
-> ValidateT e m a
validateT forall a b. (a -> b) -> a -> b
$ \MonoMaybe s e
e1 ->
forall a b. b -> Either a b
Right forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (\e
e2 -> (forall (s :: MonoMaybeS) a. a -> MonoMaybe s a
MJust e
e2, forall a. Maybe a
Nothing)) (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. a -> Maybe a
Just) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (s :: MonoMaybeS) e (m :: * -> *) a.
Functor m =>
MonoMaybe s e -> ValidateT e m a -> m (Either e (MonoMaybe s e, a))
unValidateT MonoMaybe s e
e1 ValidateT e m a
m
{-# INLINABLE refute #-}
{-# INLINABLE dispute #-}
{-# INLINABLE tolerate #-}
runValidateT :: forall e m a. (Functor m) => ValidateT e m a -> m (Either e a)
runValidateT :: forall e (m :: * -> *) a.
Functor m =>
ValidateT e m a -> m (Either e a)
runValidateT ValidateT e m a
m = forall (s :: MonoMaybeS) e (m :: * -> *) a.
Functor m =>
MonoMaybe s e -> ValidateT e m a -> m (Either e (MonoMaybe s e, a))
unValidateT forall a. MonoMaybe 'SMaybe a
MNothing ValidateT e m a
m forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \case
Left e
e -> forall a b. a -> Either a b
Left e
e
Right (MJust e
e, a
_) -> forall a b. a -> Either a b
Left e
e
Right (MonoMaybe 'SMaybe e
MNothing, a
v) -> forall a b. b -> Either a b
Right a
v
execValidateT :: forall e m a. (Monoid e, Functor m) => ValidateT e m a -> m e
execValidateT :: forall e (m :: * -> *) a.
(Monoid e, Functor m) =>
ValidateT e m a -> m e
execValidateT = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either forall a. a -> a
id forall a. Monoid a => a
mempty) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall e (m :: * -> *) a.
Functor m =>
ValidateT e m a -> m (Either e a)
runValidateT
embedValidateT :: forall e m a. (MonadValidate e m) => ValidateT e m a -> m a
embedValidateT :: forall e (m :: * -> *) a.
MonadValidate e m =>
ValidateT e m a -> m a
embedValidateT ValidateT e m a
m = forall (s :: MonoMaybeS) e (m :: * -> *) a.
Functor m =>
MonoMaybe s e -> ValidateT e m a -> m (Either e (MonoMaybe s e, a))
unValidateT forall a. MonoMaybe 'SMaybe a
MNothing ValidateT e m a
m forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
Left e
e -> forall e (m :: * -> *) a. MonadValidate e m => e -> m a
refute e
e
Right (MJust e
e, a
v) -> forall e (m :: * -> *). MonadValidate e m => e -> m ()
dispute e
e forall (f :: * -> *) a b. Functor f => f a -> b -> f b
$> a
v
Right (MonoMaybe 'SMaybe e
MNothing, a
v) -> forall (f :: * -> *) a. Applicative f => a -> f a
pure a
v
mapErrors
:: forall e1 e2 m a. (Monad m, Semigroup e2)
=> (e1 -> e2) -> ValidateT e1 m a -> ValidateT e2 m a
mapErrors :: forall e1 e2 (m :: * -> *) a.
(Monad m, Semigroup e2) =>
(e1 -> e2) -> ValidateT e1 m a -> ValidateT e2 m a
mapErrors e1 -> e2
f ValidateT e1 m a
m = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (forall (s :: MonoMaybeS) e (m :: * -> *) a.
Functor m =>
MonoMaybe s e -> ValidateT e m a -> m (Either e (MonoMaybe s e, a))
unValidateT forall a. MonoMaybe 'SMaybe a
MNothing ValidateT e1 m a
m) forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
Left e1
e -> forall e (m :: * -> *) a. MonadValidate e m => e -> m a
refute (e1 -> e2
f e1
e)
Right (MJust e1
e, a
v) -> forall e (m :: * -> *). MonadValidate e m => e -> m ()
dispute (e1 -> e2
f e1
e) forall (f :: * -> *) a b. Functor f => f a -> b -> f b
$> a
v
Right (MonoMaybe 'SMaybe e1
MNothing, a
v) -> forall (f :: * -> *) a. Applicative f => a -> f a
pure a
v
validateToError :: forall e m a. (MonadError e m) => ValidateT e m a -> m a
validateToError :: forall e (m :: * -> *) a. MonadError e m => ValidateT e m a -> m a
validateToError = forall e1 e2 (m :: * -> *) a.
MonadError e2 m =>
(e1 -> e2) -> ValidateT e1 m a -> m a
validateToErrorWith forall a. a -> a
id
{-# INLINE validateToError #-}
validateToErrorWith :: forall e1 e2 m a. (MonadError e2 m) => (e1 -> e2) -> ValidateT e1 m a -> m a
validateToErrorWith :: forall e1 e2 (m :: * -> *) a.
MonadError e2 m =>
(e1 -> e2) -> ValidateT e1 m a -> m a
validateToErrorWith e1 -> e2
f = 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
. e1 -> e2
f) forall (f :: * -> *) a. Applicative f => a -> f a
pure forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< forall e (m :: * -> *) a.
Functor m =>
ValidateT e m a -> m (Either e a)
runValidateT
{-# INLINE validateToErrorWith #-}
type Validate e = ValidateT e Identity
runValidate :: forall e a. Validate e a -> Either e a
runValidate :: forall e a. Validate e a -> Either e a
runValidate = forall a. Identity a -> a
runIdentity forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall e (m :: * -> *) a.
Functor m =>
ValidateT e m a -> m (Either e a)
runValidateT
{-# INLINE runValidate #-}
execValidate :: forall e a. (Monoid e) => Validate e a -> e
execValidate :: forall e a. Monoid e => Validate e a -> e
execValidate = forall a. Identity a -> a
runIdentity forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall e (m :: * -> *) a.
(Monoid e, Functor m) =>
ValidateT e m a -> m e
execValidateT
{-# INLINE execValidate #-}
data MonoMaybe s a where
MNothing :: MonoMaybe 'SMaybe a
MJust :: forall s a. !a -> MonoMaybe s a
deriving instance (Show a) => Show (MonoMaybe s a)
deriving instance (Eq a) => Eq (MonoMaybe s a)
deriving instance (Ord a) => Ord (MonoMaybe s a)
deriving instance Functor (MonoMaybe s)
data MonoMaybeS = SMaybe | SJust
monoMaybe :: (s ~ 'SMaybe => b) -> (a -> b) -> MonoMaybe s a -> b
monoMaybe :: forall (s :: MonoMaybeS) b a.
((s ~ 'SMaybe) => b) -> (a -> b) -> MonoMaybe s a -> b
monoMaybe (s ~ 'SMaybe) => b
v a -> b
f = \case
MonoMaybe s a
MNothing -> (s ~ 'SMaybe) => b
v
MJust a
x -> a -> b
f a
x
{-# INLINE monoMaybe #-}