{-# OPTIONS_HADDOCK not-home #-}
{-# LANGUAGE UndecidableInstances #-}
module Control.Monad.Validate.Class
( MonadValidate(..)
, exceptToValidate
, exceptToValidateWith
, WrappedMonadTrans(..)
) where
import qualified Control.Monad.Trans.RWS.CPS as CPS
import qualified Control.Monad.Trans.RWS.Lazy as Lazy
import qualified Control.Monad.Trans.RWS.Strict as Strict
import qualified Control.Monad.Trans.State.Lazy as Lazy
import qualified Control.Monad.Trans.State.Strict as Strict
import qualified Control.Monad.Trans.Writer.CPS as CPS
import qualified Control.Monad.Trans.Writer.Lazy as Lazy
import qualified Control.Monad.Trans.Writer.Strict as Strict
import Control.Monad
import Control.Monad.Trans.Class
import Control.Monad.Trans.Control
import Control.Monad.Trans.Except
import Control.Monad.Trans.Identity
import Control.Monad.Trans.Maybe
import Control.Monad.Trans.Reader
import Data.Functor
import Data.Kind (Type)
class (Monad m, Semigroup e) => MonadValidate e m | m -> e where
refute :: e -> m a
dispute :: e -> m ()
dispute = forall (f :: * -> *) a. Functor f => f a -> f ()
void forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall e (m :: * -> *) a. MonadValidate e m => m a -> m (Maybe a)
tolerate forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall e (m :: * -> *) a. MonadValidate e m => e -> m a
refute
{-# INLINE dispute #-}
tolerate :: m a -> m (Maybe a)
exceptToValidate :: forall e m a. (MonadValidate e m) => ExceptT e m a -> m a
exceptToValidate :: forall e (m :: * -> *) a. MonadValidate e m => ExceptT e m a -> m a
exceptToValidate = forall e1 e2 (m :: * -> *) a.
MonadValidate e2 m =>
(e1 -> e2) -> ExceptT e1 m a -> m a
exceptToValidateWith forall a. a -> a
id
{-# INLINE exceptToValidate #-}
exceptToValidateWith :: forall e1 e2 m a. (MonadValidate e2 m) => (e1 -> e2) -> ExceptT e1 m a -> m a
exceptToValidateWith :: forall e1 e2 (m :: * -> *) a.
MonadValidate e2 m =>
(e1 -> e2) -> ExceptT e1 m a -> m a
exceptToValidateWith e1 -> e2
f = forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (forall e (m :: * -> *) a. MonadValidate e m => e -> m a
refute 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. ExceptT e m a -> m (Either e a)
runExceptT
{-# INLINE exceptToValidateWith #-}
newtype WrappedMonadTrans (t :: (Type -> Type) -> Type -> Type) (m :: Type -> Type) (a :: Type)
= WrapMonadTrans { forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
WrappedMonadTrans t m a -> t m a
unwrapMonadTrans :: t m a }
deriving (forall a b. a -> WrappedMonadTrans t m b -> WrappedMonadTrans t m a
forall a b.
(a -> b) -> WrappedMonadTrans t m a -> WrappedMonadTrans t m b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a b.
Functor (t m) =>
a -> WrappedMonadTrans t m b -> WrappedMonadTrans t m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a b.
Functor (t m) =>
(a -> b) -> WrappedMonadTrans t m a -> WrappedMonadTrans t m b
<$ :: forall a b. a -> WrappedMonadTrans t m b -> WrappedMonadTrans t m a
$c<$ :: forall (t :: (* -> *) -> * -> *) (m :: * -> *) a b.
Functor (t m) =>
a -> WrappedMonadTrans t m b -> WrappedMonadTrans t m a
fmap :: forall a b.
(a -> b) -> WrappedMonadTrans t m a -> WrappedMonadTrans t m b
$cfmap :: forall (t :: (* -> *) -> * -> *) (m :: * -> *) a b.
Functor (t m) =>
(a -> b) -> WrappedMonadTrans t m a -> WrappedMonadTrans t m b
Functor, forall a. a -> WrappedMonadTrans t m a
forall a b.
WrappedMonadTrans t m a
-> WrappedMonadTrans t m b -> WrappedMonadTrans t m a
forall a b.
WrappedMonadTrans t m a
-> WrappedMonadTrans t m b -> WrappedMonadTrans t m b
forall a b.
WrappedMonadTrans t m (a -> b)
-> WrappedMonadTrans t m a -> WrappedMonadTrans t m b
forall a b c.
(a -> b -> c)
-> WrappedMonadTrans t m a
-> WrappedMonadTrans t m b
-> WrappedMonadTrans t 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 {t :: (* -> *) -> * -> *} {m :: * -> *}.
Applicative (t m) =>
Functor (WrappedMonadTrans t m)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
Applicative (t m) =>
a -> WrappedMonadTrans t m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a b.
Applicative (t m) =>
WrappedMonadTrans t m a
-> WrappedMonadTrans t m b -> WrappedMonadTrans t m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a b.
Applicative (t m) =>
WrappedMonadTrans t m a
-> WrappedMonadTrans t m b -> WrappedMonadTrans t m b
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a b.
Applicative (t m) =>
WrappedMonadTrans t m (a -> b)
-> WrappedMonadTrans t m a -> WrappedMonadTrans t m b
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a b c.
Applicative (t m) =>
(a -> b -> c)
-> WrappedMonadTrans t m a
-> WrappedMonadTrans t m b
-> WrappedMonadTrans t m c
<* :: forall a b.
WrappedMonadTrans t m a
-> WrappedMonadTrans t m b -> WrappedMonadTrans t m a
$c<* :: forall (t :: (* -> *) -> * -> *) (m :: * -> *) a b.
Applicative (t m) =>
WrappedMonadTrans t m a
-> WrappedMonadTrans t m b -> WrappedMonadTrans t m a
*> :: forall a b.
WrappedMonadTrans t m a
-> WrappedMonadTrans t m b -> WrappedMonadTrans t m b
$c*> :: forall (t :: (* -> *) -> * -> *) (m :: * -> *) a b.
Applicative (t m) =>
WrappedMonadTrans t m a
-> WrappedMonadTrans t m b -> WrappedMonadTrans t m b
liftA2 :: forall a b c.
(a -> b -> c)
-> WrappedMonadTrans t m a
-> WrappedMonadTrans t m b
-> WrappedMonadTrans t m c
$cliftA2 :: forall (t :: (* -> *) -> * -> *) (m :: * -> *) a b c.
Applicative (t m) =>
(a -> b -> c)
-> WrappedMonadTrans t m a
-> WrappedMonadTrans t m b
-> WrappedMonadTrans t m c
<*> :: forall a b.
WrappedMonadTrans t m (a -> b)
-> WrappedMonadTrans t m a -> WrappedMonadTrans t m b
$c<*> :: forall (t :: (* -> *) -> * -> *) (m :: * -> *) a b.
Applicative (t m) =>
WrappedMonadTrans t m (a -> b)
-> WrappedMonadTrans t m a -> WrappedMonadTrans t m b
pure :: forall a. a -> WrappedMonadTrans t m a
$cpure :: forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
Applicative (t m) =>
a -> WrappedMonadTrans t m a
Applicative, forall a. a -> WrappedMonadTrans t m a
forall a b.
WrappedMonadTrans t m a
-> WrappedMonadTrans t m b -> WrappedMonadTrans t m b
forall a b.
WrappedMonadTrans t m a
-> (a -> WrappedMonadTrans t m b) -> WrappedMonadTrans t 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
forall {t :: (* -> *) -> * -> *} {m :: * -> *}.
Monad (t m) =>
Applicative (WrappedMonadTrans t m)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
Monad (t m) =>
a -> WrappedMonadTrans t m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a b.
Monad (t m) =>
WrappedMonadTrans t m a
-> WrappedMonadTrans t m b -> WrappedMonadTrans t m b
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a b.
Monad (t m) =>
WrappedMonadTrans t m a
-> (a -> WrappedMonadTrans t m b) -> WrappedMonadTrans t m b
return :: forall a. a -> WrappedMonadTrans t m a
$creturn :: forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
Monad (t m) =>
a -> WrappedMonadTrans t m a
>> :: forall a b.
WrappedMonadTrans t m a
-> WrappedMonadTrans t m b -> WrappedMonadTrans t m b
$c>> :: forall (t :: (* -> *) -> * -> *) (m :: * -> *) a b.
Monad (t m) =>
WrappedMonadTrans t m a
-> WrappedMonadTrans t m b -> WrappedMonadTrans t m b
>>= :: forall a b.
WrappedMonadTrans t m a
-> (a -> WrappedMonadTrans t m b) -> WrappedMonadTrans t m b
$c>>= :: forall (t :: (* -> *) -> * -> *) (m :: * -> *) a b.
Monad (t m) =>
WrappedMonadTrans t m a
-> (a -> WrappedMonadTrans t m b) -> WrappedMonadTrans t m b
Monad, forall (m :: * -> *) a. Monad m => m a -> WrappedMonadTrans t m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> WrappedMonadTrans t m a
forall (t :: (* -> *) -> * -> *).
(forall (m :: * -> *) a. Monad m => m a -> t m a) -> MonadTrans t
lift :: forall (m :: * -> *) a. Monad m => m a -> WrappedMonadTrans t m a
$clift :: forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> WrappedMonadTrans t m a
MonadTrans, forall (m :: * -> *) a.
Monad m =>
m (StT (WrappedMonadTrans t) a) -> WrappedMonadTrans t m a
forall (m :: * -> *) a.
Monad m =>
(Run (WrappedMonadTrans t) -> m a) -> WrappedMonadTrans t m a
forall (t :: (* -> *) -> * -> *).
MonadTrans t
-> (forall (m :: * -> *) a. Monad m => (Run t -> m a) -> t m a)
-> (forall (m :: * -> *) a. Monad m => m (StT t a) -> t m a)
-> MonadTransControl t
forall {t :: (* -> *) -> * -> *}.
MonadTransControl t =>
MonadTrans (WrappedMonadTrans t)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTransControl t, Monad m) =>
m (StT (WrappedMonadTrans t) a) -> WrappedMonadTrans t m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTransControl t, Monad m) =>
(Run (WrappedMonadTrans t) -> m a) -> WrappedMonadTrans t m a
restoreT :: forall (m :: * -> *) a.
Monad m =>
m (StT (WrappedMonadTrans t) a) -> WrappedMonadTrans t m a
$crestoreT :: forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTransControl t, Monad m) =>
m (StT (WrappedMonadTrans t) a) -> WrappedMonadTrans t m a
liftWith :: forall (m :: * -> *) a.
Monad m =>
(Run (WrappedMonadTrans t) -> m a) -> WrappedMonadTrans t m a
$cliftWith :: forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTransControl t, Monad m) =>
(Run (WrappedMonadTrans t) -> m a) -> WrappedMonadTrans t m a
MonadTransControl)
instance (MonadTransControl t, Monad (t m), MonadValidate e m)
=> MonadValidate e (WrappedMonadTrans t m) where
refute :: forall a. e -> WrappedMonadTrans t m a
refute = 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. MonadValidate e m => e -> m a
refute
dispute :: e -> WrappedMonadTrans t m ()
dispute = 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 :: * -> *). MonadValidate e m => e -> m ()
dispute
tolerate :: forall a.
WrappedMonadTrans t m a -> WrappedMonadTrans t m (Maybe a)
tolerate WrappedMonadTrans t m a
m = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTransControl t, Monad m) =>
(Run t -> m a) -> t m a
liftWith (\Run (WrappedMonadTrans t)
run -> forall e (m :: * -> *) a. MonadValidate e m => m a -> m (Maybe a)
tolerate (Run (WrappedMonadTrans t)
run WrappedMonadTrans t m a
m)) forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (forall (f :: * -> *) a. Applicative f => a -> f a
pure 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 b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTransControl t, Monad m) =>
m (StT t a) -> t m a
restoreT forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a. Applicative f => a -> f a
pure)
{-# INLINE refute #-}
{-# INLINE dispute #-}
{-# INLINE tolerate #-}
deriving via (WrappedMonadTrans IdentityT m) instance (MonadValidate e m) => MonadValidate e (IdentityT m)
deriving via (WrappedMonadTrans (ExceptT a) m) instance (MonadValidate e m) => MonadValidate e (ExceptT a m)
deriving via (WrappedMonadTrans MaybeT m) instance (MonadValidate e m) => MonadValidate e (MaybeT m)
deriving via (WrappedMonadTrans (ReaderT r) m) instance (MonadValidate e m) => MonadValidate e (ReaderT r m)
deriving via (WrappedMonadTrans (Lazy.RWST r w s) m) instance (MonadValidate e m, Monoid w) => MonadValidate e (Lazy.RWST r w s m)
deriving via (WrappedMonadTrans (Strict.RWST r w s) m) instance (MonadValidate e m, Monoid w) => MonadValidate e (Strict.RWST r w s m)
deriving via (WrappedMonadTrans (Lazy.StateT s) m) instance (MonadValidate e m) => MonadValidate e (Lazy.StateT s m)
deriving via (WrappedMonadTrans (Strict.StateT s) m) instance (MonadValidate e m) => MonadValidate e (Strict.StateT s m)
deriving via (WrappedMonadTrans (Lazy.WriterT w) m) instance (MonadValidate e m, Monoid w) => MonadValidate e (Lazy.WriterT w m)
deriving via (WrappedMonadTrans (Strict.WriterT w) m) instance (MonadValidate e m, Monoid w) => MonadValidate e (Strict.WriterT w m)
instance (MonadValidate e m, Monoid w) => MonadValidate e (CPS.WriterT w m) where
refute :: forall a. e -> WriterT w m a
refute = 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. MonadValidate e m => e -> m a
refute
dispute :: e -> WriterT w m ()
dispute = 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 :: * -> *). MonadValidate e m => e -> m ()
dispute
tolerate :: forall a. WriterT w m a -> WriterT w m (Maybe a)
tolerate WriterT w m a
m = forall (m :: * -> *) w a.
(Functor m, Monoid w) =>
m (a, w) -> WriterT w m a
CPS.writerT forall a b. (a -> b) -> a -> b
$ forall e (m :: * -> *) a. MonadValidate e m => m a -> m (Maybe a)
tolerate (forall w (m :: * -> *) a. Monoid w => WriterT w m a -> m (a, w)
CPS.runWriterT WriterT w m a
m) forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&>
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (forall a. Maybe a
Nothing, forall a. Monoid a => a
mempty) (\(a
v, w
w) -> (forall a. a -> Maybe a
Just a
v, w
w))
{-# INLINE refute #-}
{-# INLINE dispute #-}
{-# INLINE tolerate #-}
instance (MonadValidate e m, Monoid w) => MonadValidate e (CPS.RWST r w s m) where
refute :: forall a. e -> RWST r w s m a
refute = 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. MonadValidate e m => e -> m a
refute
dispute :: e -> RWST r w s m ()
dispute = 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 :: * -> *). MonadValidate e m => e -> m ()
dispute
tolerate :: forall a. RWST r w s m a -> RWST r w s m (Maybe a)
tolerate RWST r w s m a
m = forall (m :: * -> *) w r s a.
(Functor m, Monoid w) =>
(r -> s -> m (a, s, w)) -> RWST r w s m a
CPS.rwsT forall a b. (a -> b) -> a -> b
$ \r
r s
s1 -> forall e (m :: * -> *) a. MonadValidate e m => m a -> m (Maybe a)
tolerate (forall w r s (m :: * -> *) a.
Monoid w =>
RWST r w s m a -> r -> s -> m (a, s, w)
CPS.runRWST RWST r w s m a
m r
r s
s1) forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&>
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (forall a. Maybe a
Nothing, s
s1, forall a. Monoid a => a
mempty) (\(a
v, s
s2, w
w) -> (forall a. a -> Maybe a
Just a
v, s
s2, w
w))
{-# INLINE refute #-}
{-# INLINE dispute #-}
{-# INLINE tolerate #-}