#if __GLASGOW_HASKELL__ >= 702
#endif
module Data.Validation
(
AccValidation(..)
, validate
, validationNel
, fromEither
, liftError
, validation
, toEither
, orElse
, valueOr
, ensure
, codiagonal
, validationed
, bindValidation
, _Failure
, _Success
, Validate(..)
, revalidate
) where
import Control.Applicative(Applicative((<*>), pure), (<$>))
import Control.DeepSeq (NFData (rnf))
import Control.Lens (over, under)
import Control.Lens.Getter((^.))
import Control.Lens.Iso(Swapped(..), Iso, iso, from)
import Control.Lens.Prism(Prism, prism)
import Control.Lens.Review(( # ))
import Data.Bifoldable(Bifoldable(bifoldr))
import Data.Bifunctor(Bifunctor(bimap))
import Data.Bitraversable(Bitraversable(bitraverse))
import Data.Bool (Bool)
import Data.Data(Data)
import Data.Either(Either(Left, Right), either)
import Data.Eq(Eq)
import Data.Foldable(Foldable(foldr))
import Data.Function((.), ($), id)
import Data.Functor(Functor(fmap))
import Data.Functor.Alt(Alt((<!>)))
import Data.Functor.Apply(Apply((<.>)))
import Data.List.NonEmpty (NonEmpty)
import Data.Monoid(Monoid(mappend, mempty))
import Data.Ord(Ord)
import Data.Semigroup(Semigroup((<>)))
import Data.Traversable(Traversable(traverse))
import Data.Typeable(Typeable)
#if __GLASGOW_HASKELL__ >= 702
import GHC.Generics (Generic)
#endif
import Prelude(Show)
data AccValidation err a =
AccFailure err
| AccSuccess a
deriving (
Eq, Ord, Show, Data, Typeable
#if __GLASGOW_HASKELL__ >= 702
, Generic
#endif
)
instance Functor (AccValidation err) where
fmap _ (AccFailure e) =
AccFailure e
fmap f (AccSuccess a) =
AccSuccess (f a)
instance Semigroup err => Apply (AccValidation err) where
AccFailure e1 <.> b = AccFailure $ case b of
AccFailure e2 -> e1 <> e2
AccSuccess _ -> e1
AccSuccess _ <.> AccFailure e2 =
AccFailure e2
AccSuccess f <.> AccSuccess a =
AccSuccess (f a)
instance Semigroup err => Applicative (AccValidation err) where
pure =
AccSuccess
(<*>) =
(<.>)
instance Alt (AccValidation err) where
AccFailure _ <!> x =
x
AccSuccess a <!> _ =
AccSuccess a
instance Foldable (AccValidation err) where
foldr f x (AccSuccess a) =
f a x
foldr _ x (AccFailure _) =
x
instance Traversable (AccValidation err) where
traverse f (AccSuccess a) =
AccSuccess <$> f a
traverse _ (AccFailure e) =
pure (AccFailure e)
instance Bifunctor AccValidation where
bimap f _ (AccFailure e) =
AccFailure (f e)
bimap _ g (AccSuccess a) =
AccSuccess (g a)
instance Bifoldable AccValidation where
bifoldr _ g x (AccSuccess a) =
g a x
bifoldr f _ x (AccFailure e) =
f e x
instance Bitraversable AccValidation where
bitraverse _ g (AccSuccess a) =
AccSuccess <$> g a
bitraverse f _ (AccFailure e) =
AccFailure <$> f e
appAccValidation ::
(err -> err -> err)
-> AccValidation err a
-> AccValidation err a
-> AccValidation err a
appAccValidation m (AccFailure e1) (AccFailure e2) =
AccFailure (e1 `m` e2)
appAccValidation _ (AccFailure _) (AccSuccess a2) =
AccSuccess a2
appAccValidation _ (AccSuccess a1) (AccFailure _) =
AccSuccess a1
appAccValidation _ (AccSuccess a1) (AccSuccess _) =
AccSuccess a1
instance Semigroup e => Semigroup (AccValidation e a) where
(<>) =
appAccValidation (<>)
instance Monoid e => Monoid (AccValidation e a) where
mappend =
appAccValidation mappend
mempty =
AccFailure mempty
instance Swapped AccValidation where
swapped =
iso
(\v -> case v of
AccFailure e -> AccSuccess e
AccSuccess a -> AccFailure a)
(\v -> case v of
AccFailure a -> AccSuccess a
AccSuccess e -> AccFailure e)
instance (NFData e, NFData a) => NFData (AccValidation e a) where
rnf v =
case v of
AccFailure e -> rnf e
AccSuccess a -> rnf a
validate :: Validate v => e -> (a -> Bool) -> a -> v e a
validate e p a =
if p a then _Success # a else _Failure # e
validationNel :: Either e a -> AccValidation (NonEmpty e) a
validationNel = liftError pure
fromEither :: Either e a -> AccValidation e a
fromEither = liftError id
liftError :: (b -> e) -> Either b a -> AccValidation e a
liftError f = either (AccFailure . f) AccSuccess
validation :: (e -> c) -> (a -> c) -> AccValidation e a -> c
validation ec ac v = case v of
AccFailure e -> ec e
AccSuccess a -> ac a
toEither :: AccValidation e a -> Either e a
toEither = validation Left Right
orElse :: Validate v => v e a -> a -> a
orElse v a = case v ^. _AccValidation of
AccFailure _ -> a
AccSuccess x -> x
valueOr :: Validate v => (e -> a) -> v e a -> a
valueOr ea v = case v ^. _AccValidation of
AccFailure e -> ea e
AccSuccess a -> a
codiagonal :: AccValidation a a -> a
codiagonal = valueOr id
ensure :: Validate v => e -> (a -> Bool) -> v e a -> v e a
ensure e p =
over _AccValidation $ \v -> case v of
AccFailure x -> AccFailure x
AccSuccess a -> validate e p a
validationed :: Validate v => (v e a -> v e' a') -> AccValidation e a -> AccValidation e' a'
validationed f = under _AccValidation f
bindValidation :: AccValidation e a -> (a -> AccValidation e b) -> AccValidation e b
bindValidation v f = case v of
AccFailure e -> AccFailure e
AccSuccess a -> f a
class Validate f where
_AccValidation ::
Iso (f e a) (f g b) (AccValidation e a) (AccValidation g b)
_Either ::
Iso (f e a) (f g b) (Either e a) (Either g b)
_Either =
iso
(\x -> case x ^. _AccValidation of
AccFailure e -> Left e
AccSuccess a -> Right a)
(\x -> _AccValidation # case x of
Left e -> AccFailure e
Right a -> AccSuccess a)
instance Validate AccValidation where
_AccValidation =
id
_Either =
iso
(\x -> case x of
AccFailure e -> Left e
AccSuccess a -> Right a)
(\x -> case x of
Left e -> AccFailure e
Right a -> AccSuccess a)
instance Validate Either where
_AccValidation =
iso
fromEither
toEither
_Either =
id
_Failure ::
Validate f =>
Prism (f e1 a) (f e2 a) e1 e2
_Failure =
prism
(\x -> _Either # Left x)
(\x -> case x ^. _Either of
Left e -> Right e
Right a -> Left (_Either # Right a))
_Success ::
Validate f =>
Prism (f e a) (f e b) a b
_Success =
prism
(\x -> _Either # Right x)
(\x -> case x ^. _Either of
Left e -> Left (_Either # Left e)
Right a -> Right a)
revalidate :: (Validate f, Validate g) => Iso (f e1 s) (f e2 t) (g e1 s) (g e2 t)
revalidate = _AccValidation . from _AccValidation