| Safe Haskell | None |
|---|---|
| Language | Haskell2010 |
Data.Validation.Unpacked
Description
- data Validation err a where
- Validation (#err | a#)
- pattern Failure :: err -> Validation err a
- pattern Success :: a -> Validation err a
- failure :: err -> Validation err a
- success :: a -> Validation err a
- validate :: err -> (a -> Bool) -> a -> Validation err a
- fromEither :: Either e a -> Validation e a
- liftError :: (b -> e) -> Either b a -> Validation e a
- validation :: (e -> c) -> (a -> c) -> Validation e a -> c
- toEither :: Validation e a -> Either e a
- orElse :: Validation e a -> a -> a
- valueOr :: (e -> a) -> Validation e a -> a
- ensure :: e -> (a -> Bool) -> Validation e a -> Validation e a
- codiagonal :: Validation a a -> a
- bindValidation :: Validation e a -> (a -> Validation e b) -> Validation e b
- fromBaseValidation :: Validation a b -> Validation a b
- toBaseValidation :: Validation a b -> Validation a b
Data type
data Validation err a Source #
A Validation is either a value of the type err or a, similar to Either. However,
the Applicative instance for Validation accumulates errors using a Semigroup on err.
In contrast, the Applicative for Either returns only the first error.
A consequence of this is that Validation has no Monad instance. This is because
such an instance would violate the law that a Monad's ap must equal the
Applicative's <*>
An example of typical usage can be found here.
Constructors
| Validation (#err | a#) |
Bundled Patterns
| pattern Failure :: err -> Validation err a | |
| pattern Success :: a -> Validation err a |
Instances
| Bitraversable Validation Source # | |
| Bifoldable Validation Source # | |
| Bifunctor Validation Source # | |
| Functor (Validation err) Source # | |
| Semigroup err => Applicative (Validation err) Source # | |
| Foldable (Validation err) Source # | |
| Traversable (Validation err) Source # | |
| (Eq a, Eq b) => Eq (Validation a b) Source # | |
| (Ord a, Ord b) => Ord (Validation a b) Source # | |
| (Read a, Read b) => Read (Validation a b) Source # | |
| (Show b, Show a) => Show (Validation a b) Source # | |
| Semigroup e => Semigroup (Validation e a) Source # | |
| Monoid e => Monoid (Validation e a) Source # | |
| (NFData e, NFData a) => NFData (Validation e a) Source # | |
Construction
failure :: err -> Validation err a Source #
This is the same as Failure.
success :: a -> Validation err a Source #
This is the same as Success.
validate :: err -> (a -> Bool) -> a -> Validation err a Source #
validates the a with the given predicate, returning e if the predicate does not hold.
fromEither :: Either e a -> Validation e a Source #
Converts from Either to Validation.
liftError :: (b -> e) -> Either b a -> Validation e a Source #
liftError is useful for converting an Either to an Validation
when the Left of the Either needs to be lifted into a Semigroup.
Functions on validations
validation :: (e -> c) -> (a -> c) -> Validation e a -> c Source #
Case analysis on Validation.
toEither :: Validation e a -> Either e a Source #
Converts from Validation to Either.
orElse :: Validation e a -> a -> a Source #
v returns orElse aa when v is Failure, and the a in Success a.
valueOr :: (e -> a) -> Validation e a -> a Source #
Return the a or run the given function over the e.
ensure :: e -> (a -> Bool) -> Validation e a -> Validation e a Source #
ensure leaves the validation unchanged when the predicate holds, or
fails with e otherwise.
codiagonal :: Validation a a -> a Source #
codiagonal gets the value out of either side.
bindValidation :: Validation e a -> (a -> Validation e b) -> Validation e b Source #
bindValidation binds through an Validation, which is useful for
composing Validations sequentially. Note that despite having a bind
function of the correct type, Validation is not a monad.
The reason is, this bind does not accumulate errors, so it does not
agree with the Applicative instance.
There is nothing wrong with using this function, it just does not make a
valid Monad instance.
Conversion
fromBaseValidation :: Validation a b -> Validation a b Source #
Convert validation's Validation to a Validation.
toBaseValidation :: Validation a b -> Validation a b Source #
Convert a Validation to validation's Validation.