{-# OPTIONS_HADDOCK not-home #-}
{-# LANGUAGE UndecidableInstances #-}

module Control.Monad.Validate.Class
  ( MonadValidate(..)
  , exceptToValidate
  , exceptToValidateWith

  -- * Deriving @MonadValidate@ instances with @DerivingVia@
  , 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)

{-| The class of validation monads, intended to be used to validate data structures while collecting
errors along the way. In a sense, 'MonadValidate' is like a combination of
'Control.Monad.Error.Class.MonadError' and 'Control.Monad.Writer.Class.MonadWriter', but it isn’t
entirely like either. The two essential differences are:

  1. Unlike 'Control.Monad.Error.Class.throwError', raising an error using 'refute' does not always
     abort the entire computation—it may only abort a local part of it.

  2. Unlike 'Control.Monad.Writer.Class.tell', raising an error using 'dispute' still causes the
     computation to globally fail, it just doesn’t affect local execution.

Instances must obey the following law:

@
'dispute' ≡ 'void' '.' 'tolerate' '.' 'refute'
@

For a more thorough explanation, with examples, see the documentation for
'Control.Monad.Validate.ValidateT'. -}
class (Monad m, Semigroup e) => MonadValidate e m | m -> e where
  -- | Raises a fatal validation error. Aborts the current branch of the validation (i.e. does not
  -- return).
  --
  -- @
  -- >>> 'Control.Monad.Validate.runValidate' ('refute' ["boom"] '>>' 'refute' ["bang"])
  -- 'Left' ["boom"]
  -- @
  refute :: e -> m a

  -- | Raises a non-fatal validation error. The overall validation fails, and the error is recorded,
  -- but validation continues in an attempt to try and discover more errors.
  --
  -- @
  -- >>> 'Control.Monad.Validate.runValidate' ('dispute' ["boom"] '>>' 'dispute' ["bang"])
  -- 'Left' ["boom", "bang"]
  -- @
  --
  -- If not explicitly implemented, the default implementation is @'void' '.' 'tolerate' '.'
  -- 'refute'@ (which must behave equivalently by law), but it is sometimes possible to provide a
  -- more efficient implementation.
  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@ behaves like @m@, except that any fatal errors raised by 'refute' are altered
  -- to non-fatal errors that return 'Nothing'. This allows @m@’s result to be used for further
  -- validation if it succeeds without preventing further validation from occurring upon failure.
  --
  -- @
  -- >>> 'Control.Monad.Validate.runValidate' ('tolerate' ('refute' ["boom"]) '>>' 'refute' ["bang"])
  -- 'Left' ["boom", "bang"]
  -- @
  --
  -- @since 1.1.0.0
  tolerate :: m a -> m (Maybe a)

{-| Runs an 'ExceptT' computation, and if it raised an error, re-raises it using 'refute'. This
effectively converts a computation that uses 'ExceptT' (or 'Control.Monad.Except.MonadError') into
one that uses 'MonadValidate'.

@
>>> 'Control.Monad.Validate.runValidate' '$' 'exceptToValidate' ('pure' 42)
'Right' 42
>>> 'Control.Monad.Validate.runValidate' '$' 'exceptToValidate' ('Control.Monad.Except.throwError' ["boom"])
'Left' "boom"
@

@since 1.2.0.0 -}
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 #-}

{-| Like 'exceptToValidate', but additionally accepts a function, which is applied to the error
raised by 'ExceptT' before passing it to 'refute'. This can be useful if the original error type is
not a 'Semigroup'.

@
>>> 'Control.Monad.Validate.runValidate' '$' 'exceptToValidateWith' (:[]) ('pure' 42)
'Right' 42
>>> 'Control.Monad.Validate.runValidate' '$' 'exceptToValidateWith' (:[]) ('Control.Monad.Except.throwError' "boom")
'Left' ["boom"]
@

@since 1.2.0.0 -}
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 #-}

{-| If you have a monad transformer that implements the 'MonadTransControl' class, this newtype
wrapper can be used to automatically derive instances of 'MonadValidate' using the @DerivingVia@
GHC extension.

Example:

@
{\-\# LANGUAGE DerivingVia \#-\}

newtype CustomT c m a = CustomT { runCustomT :: ... }
  deriving ('MonadValidate' e) via ('WrappedMonadTrans' (CustomT c) m)
@

@since 1.2.0.0 -}
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 #-}