{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
module Validation
(
Validation (..)
, isFailure
, isSuccess
, validation
, failures
, successes
, partitionValidations
, fromFailure
, fromSuccess
, failure
, failureIf
, failureUnless
, validationToEither
, eitherToValidation
, validateAll
, whenSuccess
, whenFailure
, whenSuccess_
, whenFailure_
, whenSuccessM
, whenFailureM
, whenSuccessM_
, whenFailureM_
, failureToMaybe
, successToMaybe
, maybeToFailure
, maybeToSuccess
) where
import Control.Applicative (Alternative (..), Applicative (..))
import Control.DeepSeq (NFData, NFData1, NFData2 (..))
import Control.Selective (Selective (..))
import Data.Bifoldable (Bifoldable (..))
import Data.Bifunctor (Bifunctor (..))
import Data.Bitraversable (Bitraversable (..))
import Data.Data (Data)
import Data.Foldable (Foldable (..))
import Data.Kind (Constraint)
import Data.List.NonEmpty (NonEmpty (..))
import GHC.Generics (Generic, Generic1)
import GHC.TypeLits (ErrorMessage (..), TypeError)
import Validation.Combinators
data Validation e a
= Failure e
| Success a
deriving stock (Validation e a -> Validation e a -> Bool
(Validation e a -> Validation e a -> Bool)
-> (Validation e a -> Validation e a -> Bool)
-> Eq (Validation e a)
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall e a.
(Eq e, Eq a) =>
Validation e a -> Validation e a -> Bool
/= :: Validation e a -> Validation e a -> Bool
$c/= :: forall e a.
(Eq e, Eq a) =>
Validation e a -> Validation e a -> Bool
== :: Validation e a -> Validation e a -> Bool
$c== :: forall e a.
(Eq e, Eq a) =>
Validation e a -> Validation e a -> Bool
Eq, Eq (Validation e a)
Eq (Validation e a)
-> (Validation e a -> Validation e a -> Ordering)
-> (Validation e a -> Validation e a -> Bool)
-> (Validation e a -> Validation e a -> Bool)
-> (Validation e a -> Validation e a -> Bool)
-> (Validation e a -> Validation e a -> Bool)
-> (Validation e a -> Validation e a -> Validation e a)
-> (Validation e a -> Validation e a -> Validation e a)
-> Ord (Validation e a)
Validation e a -> Validation e a -> Bool
Validation e a -> Validation e a -> Ordering
Validation e a -> Validation e a -> Validation e a
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall e a. (Ord e, Ord a) => Eq (Validation e a)
forall e a.
(Ord e, Ord a) =>
Validation e a -> Validation e a -> Bool
forall e a.
(Ord e, Ord a) =>
Validation e a -> Validation e a -> Ordering
forall e a.
(Ord e, Ord a) =>
Validation e a -> Validation e a -> Validation e a
min :: Validation e a -> Validation e a -> Validation e a
$cmin :: forall e a.
(Ord e, Ord a) =>
Validation e a -> Validation e a -> Validation e a
max :: Validation e a -> Validation e a -> Validation e a
$cmax :: forall e a.
(Ord e, Ord a) =>
Validation e a -> Validation e a -> Validation e a
>= :: Validation e a -> Validation e a -> Bool
$c>= :: forall e a.
(Ord e, Ord a) =>
Validation e a -> Validation e a -> Bool
> :: Validation e a -> Validation e a -> Bool
$c> :: forall e a.
(Ord e, Ord a) =>
Validation e a -> Validation e a -> Bool
<= :: Validation e a -> Validation e a -> Bool
$c<= :: forall e a.
(Ord e, Ord a) =>
Validation e a -> Validation e a -> Bool
< :: Validation e a -> Validation e a -> Bool
$c< :: forall e a.
(Ord e, Ord a) =>
Validation e a -> Validation e a -> Bool
compare :: Validation e a -> Validation e a -> Ordering
$ccompare :: forall e a.
(Ord e, Ord a) =>
Validation e a -> Validation e a -> Ordering
$cp1Ord :: forall e a. (Ord e, Ord a) => Eq (Validation e a)
Ord, Int -> Validation e a -> ShowS
[Validation e a] -> ShowS
Validation e a -> String
(Int -> Validation e a -> ShowS)
-> (Validation e a -> String)
-> ([Validation e a] -> ShowS)
-> Show (Validation e a)
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall e a. (Show e, Show a) => Int -> Validation e a -> ShowS
forall e a. (Show e, Show a) => [Validation e a] -> ShowS
forall e a. (Show e, Show a) => Validation e a -> String
showList :: [Validation e a] -> ShowS
$cshowList :: forall e a. (Show e, Show a) => [Validation e a] -> ShowS
show :: Validation e a -> String
$cshow :: forall e a. (Show e, Show a) => Validation e a -> String
showsPrec :: Int -> Validation e a -> ShowS
$cshowsPrec :: forall e a. (Show e, Show a) => Int -> Validation e a -> ShowS
Show, (forall x. Validation e a -> Rep (Validation e a) x)
-> (forall x. Rep (Validation e a) x -> Validation e a)
-> Generic (Validation e a)
forall x. Rep (Validation e a) x -> Validation e a
forall x. Validation e a -> Rep (Validation e a) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall e a x. Rep (Validation e a) x -> Validation e a
forall e a x. Validation e a -> Rep (Validation e a) x
$cto :: forall e a x. Rep (Validation e a) x -> Validation e a
$cfrom :: forall e a x. Validation e a -> Rep (Validation e a) x
Generic, (forall a. Validation e a -> Rep1 (Validation e) a)
-> (forall a. Rep1 (Validation e) a -> Validation e a)
-> Generic1 (Validation e)
forall a. Rep1 (Validation e) a -> Validation e a
forall a. Validation e a -> Rep1 (Validation e) a
forall e a. Rep1 (Validation e) a -> Validation e a
forall e a. Validation e a -> Rep1 (Validation e) a
forall k (f :: k -> *).
(forall (a :: k). f a -> Rep1 f a)
-> (forall (a :: k). Rep1 f a -> f a) -> Generic1 f
$cto1 :: forall e a. Rep1 (Validation e) a -> Validation e a
$cfrom1 :: forall e a. Validation e a -> Rep1 (Validation e) a
Generic1, Typeable (Validation e a)
DataType
Constr
Typeable (Validation e a)
-> (forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Validation e a -> c (Validation e a))
-> (forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Validation e a))
-> (Validation e a -> Constr)
-> (Validation e a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c (Validation e a)))
-> (forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (Validation e a)))
-> ((forall b. Data b => b -> b)
-> Validation e a -> Validation e a)
-> (forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Validation e a -> r)
-> (forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Validation e a -> r)
-> (forall u.
(forall d. Data d => d -> u) -> Validation e a -> [u])
-> (forall u.
Int -> (forall d. Data d => d -> u) -> Validation e a -> u)
-> (forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d)
-> Validation e a -> m (Validation e a))
-> (forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> Validation e a -> m (Validation e a))
-> (forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> Validation e a -> m (Validation e a))
-> Data (Validation e a)
Validation e a -> DataType
Validation e a -> Constr
(forall b. Data b => b -> b) -> Validation e a -> Validation e a
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Validation e a -> c (Validation e a)
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Validation e a)
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (Validation e a))
forall a.
Typeable a
-> (forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
(r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
(r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall u.
Int -> (forall d. Data d => d -> u) -> Validation e a -> u
forall u. (forall d. Data d => d -> u) -> Validation e a -> [u]
forall e a. (Data e, Data a) => Typeable (Validation e a)
forall e a. (Data e, Data a) => Validation e a -> DataType
forall e a. (Data e, Data a) => Validation e a -> Constr
forall e a.
(Data e, Data a) =>
(forall b. Data b => b -> b) -> Validation e a -> Validation e a
forall e a u.
(Data e, Data a) =>
Int -> (forall d. Data d => d -> u) -> Validation e a -> u
forall e a u.
(Data e, Data a) =>
(forall d. Data d => d -> u) -> Validation e a -> [u]
forall e a r r'.
(Data e, Data a) =>
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Validation e a -> r
forall e a r r'.
(Data e, Data a) =>
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Validation e a -> r
forall e a (m :: * -> *).
(Data e, Data a, Monad m) =>
(forall d. Data d => d -> m d)
-> Validation e a -> m (Validation e a)
forall e a (m :: * -> *).
(Data e, Data a, MonadPlus m) =>
(forall d. Data d => d -> m d)
-> Validation e a -> m (Validation e a)
forall e a (c :: * -> *).
(Data e, Data a) =>
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Validation e a)
forall e a (c :: * -> *).
(Data e, Data a) =>
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Validation e a -> c (Validation e a)
forall e a (t :: * -> *) (c :: * -> *).
(Data e, Data a, Typeable t) =>
(forall d. Data d => c (t d)) -> Maybe (c (Validation e a))
forall e a (t :: * -> * -> *) (c :: * -> *).
(Data e, Data a, Typeable t) =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (Validation e a))
forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Validation e a -> r
forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Validation e a -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d)
-> Validation e a -> m (Validation e a)
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> Validation e a -> m (Validation e a)
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Validation e a)
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Validation e a -> c (Validation e a)
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c (Validation e a))
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (Validation e a))
$cSuccess :: Constr
$cFailure :: Constr
$tValidation :: DataType
gmapMo :: (forall d. Data d => d -> m d)
-> Validation e a -> m (Validation e a)
$cgmapMo :: forall e a (m :: * -> *).
(Data e, Data a, MonadPlus m) =>
(forall d. Data d => d -> m d)
-> Validation e a -> m (Validation e a)
gmapMp :: (forall d. Data d => d -> m d)
-> Validation e a -> m (Validation e a)
$cgmapMp :: forall e a (m :: * -> *).
(Data e, Data a, MonadPlus m) =>
(forall d. Data d => d -> m d)
-> Validation e a -> m (Validation e a)
gmapM :: (forall d. Data d => d -> m d)
-> Validation e a -> m (Validation e a)
$cgmapM :: forall e a (m :: * -> *).
(Data e, Data a, Monad m) =>
(forall d. Data d => d -> m d)
-> Validation e a -> m (Validation e a)
gmapQi :: Int -> (forall d. Data d => d -> u) -> Validation e a -> u
$cgmapQi :: forall e a u.
(Data e, Data a) =>
Int -> (forall d. Data d => d -> u) -> Validation e a -> u
gmapQ :: (forall d. Data d => d -> u) -> Validation e a -> [u]
$cgmapQ :: forall e a u.
(Data e, Data a) =>
(forall d. Data d => d -> u) -> Validation e a -> [u]
gmapQr :: (r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Validation e a -> r
$cgmapQr :: forall e a r r'.
(Data e, Data a) =>
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Validation e a -> r
gmapQl :: (r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Validation e a -> r
$cgmapQl :: forall e a r r'.
(Data e, Data a) =>
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Validation e a -> r
gmapT :: (forall b. Data b => b -> b) -> Validation e a -> Validation e a
$cgmapT :: forall e a.
(Data e, Data a) =>
(forall b. Data b => b -> b) -> Validation e a -> Validation e a
dataCast2 :: (forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (Validation e a))
$cdataCast2 :: forall e a (t :: * -> * -> *) (c :: * -> *).
(Data e, Data a, Typeable t) =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (Validation e a))
dataCast1 :: (forall d. Data d => c (t d)) -> Maybe (c (Validation e a))
$cdataCast1 :: forall e a (t :: * -> *) (c :: * -> *).
(Data e, Data a, Typeable t) =>
(forall d. Data d => c (t d)) -> Maybe (c (Validation e a))
dataTypeOf :: Validation e a -> DataType
$cdataTypeOf :: forall e a. (Data e, Data a) => Validation e a -> DataType
toConstr :: Validation e a -> Constr
$ctoConstr :: forall e a. (Data e, Data a) => Validation e a -> Constr
gunfold :: (forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Validation e a)
$cgunfold :: forall e a (c :: * -> *).
(Data e, Data a) =>
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Validation e a)
gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Validation e a -> c (Validation e a)
$cgfoldl :: forall e a (c :: * -> *).
(Data e, Data a) =>
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Validation e a -> c (Validation e a)
$cp1Data :: forall e a. (Data e, Data a) => Typeable (Validation e a)
Data)
deriving anyclass (Validation e a -> ()
(Validation e a -> ()) -> NFData (Validation e a)
forall a. (a -> ()) -> NFData a
forall e a. (NFData e, NFData a) => Validation e a -> ()
rnf :: Validation e a -> ()
$crnf :: forall e a. (NFData e, NFData a) => Validation e a -> ()
NFData, (forall a. (a -> ()) -> Validation e a -> ())
-> NFData1 (Validation e)
forall e a. NFData e => (a -> ()) -> Validation e a -> ()
forall a. (a -> ()) -> Validation e a -> ()
forall (f :: * -> *).
(forall a. (a -> ()) -> f a -> ()) -> NFData1 f
liftRnf :: (a -> ()) -> Validation e a -> ()
$cliftRnf :: forall e a. NFData e => (a -> ()) -> Validation e a -> ()
NFData1)
instance Functor (Validation e) where
fmap :: (a -> b) -> Validation e a -> Validation e b
fmap :: (a -> b) -> Validation e a -> Validation e b
fmap a -> b
_ (Failure e
e) = e -> Validation e b
forall e a. e -> Validation e a
Failure e
e
fmap a -> b
f (Success a
a) = b -> Validation e b
forall e a. a -> Validation e a
Success (a -> b
f a
a)
{-# INLINE fmap #-}
(<$) :: a -> Validation e b -> Validation e a
a
x <$ :: a -> Validation e b -> Validation e a
<$ Success b
_ = a -> Validation e a
forall e a. a -> Validation e a
Success a
x
a
_ <$ Failure e
e = e -> Validation e a
forall e a. e -> Validation e a
Failure e
e
{-# INLINE (<$) #-}
instance (Semigroup e, Semigroup a) => Semigroup (Validation e a) where
(<>) :: Validation e a -> Validation e a -> Validation e a
<> :: Validation e a -> Validation e a -> Validation e a
(<>) = (a -> a -> a) -> Validation e a -> Validation e a -> Validation e a
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 a -> a -> a
forall a. Semigroup a => a -> a -> a
(<>)
{-# INLINE (<>) #-}
instance (Semigroup e, Semigroup a, Monoid a) => Monoid (Validation e a) where
mempty :: Validation e a
mempty :: Validation e a
mempty = a -> Validation e a
forall e a. a -> Validation e a
Success a
forall a. Monoid a => a
mempty
{-# INLINE mempty #-}
mappend :: Validation e a -> Validation e a -> Validation e a
mappend :: Validation e a -> Validation e a -> Validation e a
mappend = Validation e a -> Validation e a -> Validation e a
forall a. Semigroup a => a -> a -> a
(<>)
{-# INLINE mappend #-}
instance Semigroup e => Applicative (Validation e) where
pure :: a -> Validation e a
pure :: a -> Validation e a
pure = a -> Validation e a
forall e a. a -> Validation e a
Success
{-# INLINE pure #-}
(<*>) :: Validation e (a -> b) -> Validation e a -> Validation e b
Failure e
e1 <*> :: Validation e (a -> b) -> Validation e a -> Validation e b
<*> Validation e a
b = e -> Validation e b
forall e a. e -> Validation e a
Failure (e -> Validation e b) -> e -> Validation e b
forall a b. (a -> b) -> a -> b
$ case Validation e a
b of
Failure e
e2 -> e
e1 e -> e -> e
forall a. Semigroup a => a -> a -> a
<> e
e2
Success a
_ -> e
e1
Success a -> b
_ <*> Failure e
e = e -> Validation e b
forall e a. e -> Validation e a
Failure e
e
Success a -> b
f <*> Success a
a = b -> Validation e b
forall e a. a -> Validation e a
Success (a -> b
f a
a)
{-# INLINE (<*>) #-}
(*>) :: Validation e a -> Validation e b -> Validation e b
Failure e
e1 *> :: Validation e a -> Validation e b -> Validation e b
*> Validation e b
b = e -> Validation e b
forall e a. e -> Validation e a
Failure (e -> Validation e b) -> e -> Validation e b
forall a b. (a -> b) -> a -> b
$ case Validation e b
b of
Failure e
e2 -> e
e1 e -> e -> e
forall a. Semigroup a => a -> a -> a
<> e
e2
Success b
_ -> e
e1
Success a
_ *> Failure e
e = e -> Validation e b
forall e a. e -> Validation e a
Failure e
e
Success a
_ *> Success b
b = b -> Validation e b
forall e a. a -> Validation e a
Success b
b
{-# INLINE (*>) #-}
(<*) :: Validation e a -> Validation e b -> Validation e a
Failure e
e1 <* :: Validation e a -> Validation e b -> Validation e a
<* Validation e b
b = e -> Validation e a
forall e a. e -> Validation e a
Failure (e -> Validation e a) -> e -> Validation e a
forall a b. (a -> b) -> a -> b
$ case Validation e b
b of
Failure e
e2 -> e
e1 e -> e -> e
forall a. Semigroup a => a -> a -> a
<> e
e2
Success b
_ -> e
e1
Success a
_ <* Failure e
e = e -> Validation e a
forall e a. e -> Validation e a
Failure e
e
Success a
a <* Success b
_ = a -> Validation e a
forall e a. a -> Validation e a
Success a
a
{-# INLINE (<*) #-}
liftA2 :: (a -> b -> c) -> Validation e a -> Validation e b -> Validation e c
liftA2 :: (a -> b -> c) -> Validation e a -> Validation e b -> Validation e c
liftA2 a -> b -> c
_ (Failure e
e1) Validation e b
b = e -> Validation e c
forall e a. e -> Validation e a
Failure (e -> Validation e c) -> e -> Validation e c
forall a b. (a -> b) -> a -> b
$ case Validation e b
b of
Failure e
e2 -> e
e1 e -> e -> e
forall a. Semigroup a => a -> a -> a
<> e
e2
Success b
_ -> e
e1
liftA2 a -> b -> c
_ (Success a
_) (Failure e
e) = e -> Validation e c
forall e a. e -> Validation e a
Failure e
e
liftA2 a -> b -> c
f (Success a
a) (Success b
b) = c -> Validation e c
forall e a. a -> Validation e a
Success (a -> b -> c
f a
a b
b)
{-# INLINE liftA2 #-}
instance Semigroup e => Selective (Validation e) where
select :: Validation e (Either a b) -> Validation e (a -> b) -> Validation e b
select :: Validation e (Either a b)
-> Validation e (a -> b) -> Validation e b
select (Failure e
e) Validation e (a -> b)
_ = e -> Validation e b
forall e a. e -> Validation e a
Failure e
e
select (Success Either a b
eab) Validation e (a -> b)
f = case Either a b
eab of
Left a
a -> ((a -> b) -> a -> b
forall a b. (a -> b) -> a -> b
$ a
a) ((a -> b) -> b) -> Validation e (a -> b) -> Validation e b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Validation e (a -> b)
f
Right b
b -> b -> Validation e b
forall e a. a -> Validation e a
Success b
b
{-# INLINE select #-}
instance (Semigroup e, Monoid e) => Alternative (Validation e) where
empty :: Validation e a
empty :: Validation e a
empty = e -> Validation e a
forall e a. e -> Validation e a
Failure e
forall a. Monoid a => a
mempty
{-# INLINE empty #-}
(<|>) :: Validation e a -> Validation e a -> Validation e a
s :: Validation e a
s@Success{} <|> :: Validation e a -> Validation e a -> Validation e a
<|> Validation e a
_ = Validation e a
s
Validation e a
_ <|> s :: Validation e a
s@Success{} = Validation e a
s
Failure e
e <|> Failure e
e' = e -> Validation e a
forall e a. e -> Validation e a
Failure (e
e e -> e -> e
forall a. Semigroup a => a -> a -> a
<> e
e')
{-# INLINE (<|>) #-}
instance Foldable (Validation e) where
fold :: Monoid m => Validation e m -> m
fold :: Validation e m -> m
fold = \case
Failure e
_ -> m
forall a. Monoid a => a
mempty
Success m
a -> m
a
{-# INLINE fold #-}
foldMap :: Monoid m => (a -> m) -> Validation e a -> m
foldMap :: (a -> m) -> Validation e a -> m
foldMap a -> m
f = \case
Failure e
_ -> m
forall a. Monoid a => a
mempty
Success a
a -> a -> m
f a
a
{-# INLINE foldMap #-}
foldr :: (a -> b -> b) -> b -> Validation e a -> b
foldr :: (a -> b -> b) -> b -> Validation e a -> b
foldr a -> b -> b
f b
x = \case
Failure e
_ -> b
x
Success a
a -> a -> b -> b
f a
a b
x
{-# INLINE foldr #-}
foldr' :: (a -> b -> b) -> b -> Validation e a -> b
foldr' :: (a -> b -> b) -> b -> Validation e a -> b
foldr' = (a -> b -> b) -> b -> Validation e a -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr
{-# INLINE foldr' #-}
foldl :: (b -> a -> b) -> b -> Validation e a -> b
foldl :: (b -> a -> b) -> b -> Validation e a -> b
foldl b -> a -> b
f b
x = \case
Failure e
_ -> b
x
Success a
a -> b -> a -> b
f b
x a
a
{-# INLINE foldl #-}
foldl' :: (b -> a -> b) -> b -> Validation e a -> b
foldl' :: (b -> a -> b) -> b -> Validation e a -> b
foldl' = (b -> a -> b) -> b -> Validation e a -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl
{-# INLINE foldl' #-}
toList :: Validation e a -> [a]
toList :: Validation e a -> [a]
toList = \case
Failure e
_ -> []
Success a
a -> [a
a]
{-# INLINE toList #-}
null :: Validation e a -> Bool
null :: Validation e a -> Bool
null = \case
Failure e
_ -> Bool
True
Success a
_ -> Bool
False
{-# INLINE null #-}
length :: Validation e a -> Int
length :: Validation e a -> Int
length = \case
Failure e
_ -> Int
0
Success a
_ -> Int
1
{-# INLINE length #-}
elem :: Eq a => a -> Validation e a -> Bool
elem :: a -> Validation e a -> Bool
elem a
x = \case
Failure e
_ -> Bool
False
Success a
a -> a
x a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
a
{-# INLINE elem #-}
sum :: Num a => Validation e a -> a
sum :: Validation e a -> a
sum = \case
Failure e
_ -> a
0
Success a
a -> a
a
{-# INLINE sum #-}
product :: Num a => Validation e a -> a
product :: Validation e a -> a
product = \case
Failure e
_ -> a
1
Success a
a -> a
a
{-# INLINE product #-}
instance Traversable (Validation e) where
traverse :: Applicative f => (a -> f b) -> Validation e a -> f (Validation e b)
traverse :: (a -> f b) -> Validation e a -> f (Validation e b)
traverse a -> f b
f (Success a
a) = b -> Validation e b
forall e a. a -> Validation e a
Success (b -> Validation e b) -> f b -> f (Validation e b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> f b
f a
a
traverse a -> f b
_ (Failure e
e) = Validation e b -> f (Validation e b)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (e -> Validation e b
forall e a. e -> Validation e a
Failure e
e)
{-# INLINE traverse #-}
sequenceA :: Applicative f => Validation e (f a) -> f (Validation e a)
sequenceA :: Validation e (f a) -> f (Validation e a)
sequenceA = \case
Failure e
e -> Validation e a -> f (Validation e a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (e -> Validation e a
forall e a. e -> Validation e a
Failure e
e)
Success f a
f -> a -> Validation e a
forall e a. a -> Validation e a
Success (a -> Validation e a) -> f a -> f (Validation e a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> f a
f
{-# INLINE sequenceA #-}
instance Bifunctor Validation where
bimap :: (e -> d) -> (a -> b) -> Validation e a -> Validation d b
bimap :: (e -> d) -> (a -> b) -> Validation e a -> Validation d b
bimap e -> d
f a -> b
_ (Failure e
e) = d -> Validation d b
forall e a. e -> Validation e a
Failure (e -> d
f e
e)
bimap e -> d
_ a -> b
g (Success a
a) = b -> Validation d b
forall e a. a -> Validation e a
Success (a -> b
g a
a)
{-# INLINE bimap #-}
first :: (e -> d) -> Validation e a -> Validation d a
first :: (e -> d) -> Validation e a -> Validation d a
first e -> d
f (Failure e
e) = d -> Validation d a
forall e a. e -> Validation e a
Failure (e -> d
f e
e)
first e -> d
_ (Success a
a) = a -> Validation d a
forall e a. a -> Validation e a
Success a
a
{-# INLINE first #-}
second :: (a -> b) -> Validation e a -> Validation e b
second :: (a -> b) -> Validation e a -> Validation e b
second a -> b
_ (Failure e
e) = e -> Validation e b
forall e a. e -> Validation e a
Failure e
e
second a -> b
g (Success a
a) = b -> Validation e b
forall e a. a -> Validation e a
Success (a -> b
g a
a)
{-# INLINE second #-}
instance Bifoldable Validation where
bifoldMap :: (a -> m) -> (b -> m) -> Validation a b -> m
bifoldMap a -> m
f b -> m
_ (Failure a
e) = a -> m
f a
e
bifoldMap a -> m
_ b -> m
g (Success b
a) = b -> m
g b
a
{-# INLINE bifoldMap #-}
instance Bitraversable Validation where
bitraverse
:: Applicative f
=> (e -> f d)
-> (a -> f b)
-> Validation e a
-> f (Validation d b)
bitraverse :: (e -> f d) -> (a -> f b) -> Validation e a -> f (Validation d b)
bitraverse e -> f d
f a -> f b
_ (Failure e
e) = d -> Validation d b
forall e a. e -> Validation e a
Failure (d -> Validation d b) -> f d -> f (Validation d b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> e -> f d
f e
e
bitraverse e -> f d
_ a -> f b
g (Success a
a) = b -> Validation d b
forall e a. a -> Validation e a
Success (b -> Validation d b) -> f b -> f (Validation d b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> f b
g a
a
{-# INLINE bitraverse #-}
instance NFData2 Validation where
liftRnf2 :: (e -> ()) -> (a -> ()) -> Validation e a -> ()
liftRnf2 :: (e -> ()) -> (a -> ()) -> Validation e a -> ()
liftRnf2 e -> ()
f a -> ()
_s (Failure e
x) = e -> ()
f e
x
liftRnf2 e -> ()
_f a -> ()
s (Success a
y) = a -> ()
s a
y
instance (NoValidationMonadError, Semigroup e) => Monad (Validation e) where
return :: a -> Validation e a
return = String -> a -> Validation e a
forall a. HasCallStack => String -> a
error String
"Unreachable Validation instance of Monad"
>>= :: Validation e a -> (a -> Validation e b) -> Validation e b
(>>=) = String -> Validation e a -> (a -> Validation e b) -> Validation e b
forall a. HasCallStack => String -> a
error String
"Unreachable Validation instance of Monad"
type family NoValidationMonadError :: Constraint where
NoValidationMonadError = TypeError
( 'Text "Type 'Validation' doesn't have lawful 'Monad' instance"
':$$: 'Text "which means that you can't use 'Monad' methods with 'Validation'."
)
validationToEither :: Validation e a -> Either e a
validationToEither :: Validation e a -> Either e a
validationToEither = \case
Failure e
e -> e -> Either e a
forall a b. a -> Either a b
Left e
e
Success a
a -> a -> Either e a
forall a b. b -> Either a b
Right a
a
{-# INLINE validationToEither #-}
eitherToValidation :: Either e a -> Validation e a
eitherToValidation :: Either e a -> Validation e a
eitherToValidation = \case
Left e
e -> e -> Validation e a
forall e a. e -> Validation e a
Failure e
e
Right a
a -> a -> Validation e a
forall e a. a -> Validation e a
Success a
a
{-# INLINE eitherToValidation #-}
isFailure :: Validation e a -> Bool
isFailure :: Validation e a -> Bool
isFailure = \case
Failure e
_ -> Bool
True
Success a
_ -> Bool
False
isSuccess :: Validation e a -> Bool
isSuccess :: Validation e a -> Bool
isSuccess = \case
Success a
_ -> Bool
True
Failure e
_ -> Bool
False
validation :: (e -> x) -> (a -> x) -> Validation e a -> x
validation :: (e -> x) -> (a -> x) -> Validation e a -> x
validation e -> x
fe a -> x
fa = \case
Success a
a -> a -> x
fa a
a
Failure e
e -> e -> x
fe e
e
failures :: [Validation e a] -> [e]
failures :: [Validation e a] -> [e]
failures [Validation e a]
v = [e
e | Failure e
e <- [Validation e a]
v]
{-# INLINE failures #-}
successes :: [Validation e a] -> [a]
successes :: [Validation e a] -> [a]
successes [Validation e a]
v = [a
a | Success a
a <- [Validation e a]
v]
{-# INLINE successes #-}
partitionValidations :: [Validation e a] -> ([e], [a])
partitionValidations :: [Validation e a] -> ([e], [a])
partitionValidations = [Validation e a] -> ([e], [a])
forall e a. [Validation e a] -> ([e], [a])
go
where
go :: [Validation e a] -> ([e], [a])
go :: [Validation e a] -> ([e], [a])
go [] = ([], [])
go (Failure e
e:[Validation e a]
rest) = ([e] -> [e]) -> ([e], [a]) -> ([e], [a])
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first (e
ee -> [e] -> [e]
forall a. a -> [a] -> [a]
:) (([e], [a]) -> ([e], [a])) -> ([e], [a]) -> ([e], [a])
forall a b. (a -> b) -> a -> b
$ [Validation e a] -> ([e], [a])
forall e a. [Validation e a] -> ([e], [a])
go [Validation e a]
rest
go (Success a
a:[Validation e a]
rest) = ([a] -> [a]) -> ([e], [a]) -> ([e], [a])
forall (p :: * -> * -> *) b c a.
Bifunctor p =>
(b -> c) -> p a b -> p a c
second (a
aa -> [a] -> [a]
forall a. a -> [a] -> [a]
:) (([e], [a]) -> ([e], [a])) -> ([e], [a]) -> ([e], [a])
forall a b. (a -> b) -> a -> b
$ [Validation e a] -> ([e], [a])
forall e a. [Validation e a] -> ([e], [a])
go [Validation e a]
rest
fromFailure :: e -> Validation e a -> e
fromFailure :: e -> Validation e a -> e
fromFailure e
_ (Failure e
e) = e
e
fromFailure e
e Validation e a
_ = e
e
fromSuccess :: a -> Validation e a -> a
fromSuccess :: a -> Validation e a -> a
fromSuccess a
_ (Success a
a) = a
a
fromSuccess a
a Validation e a
_ = a
a
failure :: e -> Validation (NonEmpty e) a
failure :: e -> Validation (NonEmpty e) a
failure e
e = NonEmpty e -> Validation (NonEmpty e) a
forall e a. e -> Validation e a
Failure (e
e e -> [e] -> NonEmpty e
forall a. a -> [a] -> NonEmpty a
:| [])
{-# INLINE failure #-}
failureIf :: Bool -> e -> Validation (NonEmpty e) ()
failureIf :: Bool -> e -> Validation (NonEmpty e) ()
failureIf Bool
p e
e
| Bool
p = e -> Validation (NonEmpty e) ()
forall e a. e -> Validation (NonEmpty e) a
failure e
e
| Bool
otherwise = () -> Validation (NonEmpty e) ()
forall e a. a -> Validation e a
Success ()
{-# INLINE failureIf #-}
failureUnless :: Bool -> e -> Validation (NonEmpty e) ()
failureUnless :: Bool -> e -> Validation (NonEmpty e) ()
failureUnless Bool
p e
e
| Bool
p = () -> Validation (NonEmpty e) ()
forall e a. a -> Validation e a
Success ()
| Bool
otherwise = e -> Validation (NonEmpty e) ()
forall e a. e -> Validation (NonEmpty e) a
failure e
e
{-# INLINE failureUnless #-}