HORIZON HASKELLDocslts/ghc-9.10.xc74966e2026-09-27Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27

Modulevalidation-1.1.3Haskell2010

Data.Validation

A data type similar to Data.Either that accumulates failures.

  • 1 type
  • 1 class
  • 15 values
  • Packagevalidation-1.1.3
  • Exports17
  • LanguageHaskell2010
  • LicenceBSD-3-Clause
  • SourceValidation.hs

Data type

1 declaration
datadata Validation err a
#

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 Bind or 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

Instances20Bifoldable, Bifunctor, Bitraversable, Swap, Validate, Functor, …

Constructing validations

4 declarations
valuevalidate :: Validate v => e -> (a -> Maybe b) -> a -> v e b
#

validates an a producing an updated optional value, returning e in the empty case.

This can be thought of as having the less general type:

validate :: e -> (a -> Maybe b) -> a -> Validation e b

Functions on validations

8 declarations
valueorElse :: Validate v => v e a -> a -> a
#

v orElse a returns a when v is Failure, and the a in Success a.

This can be thought of as having the less general type:

orElse :: Validation e a -> a -> a
valuevalueOr :: Validate v => (e -> a) -> v e a -> a
#

Return the a or run the given function over the e.

This can be thought of as having the less general type:

valueOr :: (e -> a) -> Validation e a -> a
valueensure :: Validate v => e -> (a -> Maybe b) -> v e a -> v e b
#

ensure ensures that a validation remains unchanged upon failure, updating a successful validation with an optional value that could fail with e otherwise.

This can be thought of as having the less general type:

ensure :: e -> (a -> Maybe b) -> Validation e a -> Validation e b
valuevalidationed
  1. :: Validate v
  2. => v e a -> v e' a'
  3. -> Validation e a
  4. -> Validation e' a'
#

Run a function on anything with a Validate instance (usually Either) as if it were a function on Validation

This can be thought of as having the type

(Either e a -> Either e' a') -> Validation e a -> Validation e' a'
valuebindValidation :: Validation e a -> (a -> Validation e b) -> Validation e b
#

bindValidation binds through a 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.

Prisms

2 declarations

These prisms are useful for writing code which is polymorphic in its choice of Either or Validation. This choice can then be made later by a user, depending on their needs.

An example of this style of usage can be found here

Isomorphisms

2 declarations