{-# 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
class (Monad m, Semigroup e) => MonadValidate e m | m -> e where
refute :: e -> m a
dispute :: e -> m ()
dispute = void . tolerate . refute
{-# INLINE dispute #-}
tolerate :: m a -> m (Maybe a)
exceptToValidate :: forall e m a. (MonadValidate e m) => ExceptT e m a -> m a
exceptToValidate = exceptToValidateWith id
{-# INLINE exceptToValidate #-}
exceptToValidateWith :: forall e1 e2 m a. (MonadValidate e2 m) => (e1 -> e2) -> ExceptT e1 m a -> m a
exceptToValidateWith f = either (refute . f) pure <=< runExceptT
{-# INLINE exceptToValidateWith #-}
newtype WrappedMonadTrans (t :: (* -> *) -> * -> *) (m :: * -> *) (a :: *)
= WrapMonadTrans { unwrapMonadTrans :: t m a }
deriving (Functor, Applicative, Monad, MonadTrans, MonadTransControl)
instance (MonadTransControl t, Monad (t m), MonadValidate e m)
=> MonadValidate e (WrappedMonadTrans t m) where
refute = lift . refute
dispute = lift . dispute
tolerate m = liftWith (\run -> tolerate (run m)) >>=
maybe (pure Nothing) (fmap Just . restoreT . 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 = lift . refute
dispute = lift . dispute
tolerate m = CPS.writerT $ tolerate (CPS.runWriterT m) <&>
maybe (Nothing, mempty) (\(v, w) -> (Just v, w))
{-# INLINE refute #-}
{-# INLINE dispute #-}
{-# INLINE tolerate #-}
instance (MonadValidate e m, Monoid w) => MonadValidate e (CPS.RWST r w s m) where
refute = lift . refute
dispute = lift . dispute
tolerate m = CPS.rwsT $ \r s1 -> tolerate (CPS.runRWST m r s1) <&>
maybe (Nothing, s1, mempty) (\(v, s2, w) -> (Just v, s2, w))
{-# INLINE refute #-}
{-# INLINE dispute #-}
{-# INLINE tolerate #-}