Module Preface_stdlib.Validation

Implementation for Validation.t.

Validation is very similar to Result, the main difference between the two lies in the (delayed) implementation of Preface_specs.APPLICATIVE which allows, unlike Result, the accumulation of errors.

Type

type ('a, 'errors) t =
  1. | Valid of 'a
  2. | Invalid of 'errors

Implementation

Bifunctor

module Bifunctor : Preface_specs.BIFUNCTOR with type ('a, 'b) t = ('a, 'b) t

Delayed implementation

By setting the error type of Validation.t it is possible to get implementations for abstractions on constructors of type with an arity of 1.

Functor

module Functor (T : Preface_specs.Types.T0) : Preface_specs.FUNCTOR with type 'a t = ('a, T.t) t

Alt

module Alt (Errors : Preface_specs.SEMIGROUP) : Preface_specs.ALT with type 'a t = ('a, Errors.t) t

Applicative

Validation.t implements Preface_specs.APPLICATIVE and introduces an interface to define Preface_specs.TRAVERSABLE using Validation as an iterable structure.

As you can see, it is in the definition of the Preface_specs.APPLICATIVE that Validation differs from Result. The 'errors part must be a Preface_specs.SEMIGROUP to allow for the accumulation of errors.

Selective

Monad

Validation.t implements Preface_specs.MONAD and introduces an interface to define Preface_specs.TRAVERSABLE using Validation as an iterable structure.

Foldable

module Foldable (T : Preface_specs.Types.T0) : Preface_specs.FOLDABLE with type 'a t = ('a, T.t) t

Invariant

Additional functions

Additional functions to facilitate practical work with Validation.t.

val valid : 'a -> ('a, 'b) t

Wrap a value into Valid.

val invalid : 'b -> ('a, 'b) t

Wrap an error value Invalid.

val pure : 'a -> ('a, 'b) t

Alias for valid.

val case : ('a -> 'c) -> ('b -> 'c) -> ('a, 'b) t -> 'c

case f g x apply f if x is Valid, g if x is Invalid.

val equal : ('a -> 'a -> bool) -> ('b -> 'b -> bool) -> ('a, 'b) t -> ('a, 'b) t -> bool

Equality between Validation.t.

val pp : (Stdlib.Format.formatter -> 'a -> unit) -> (Stdlib.Format.formatter -> 'b -> unit) -> Stdlib.Format.formatter -> ('a, 'b) t -> unit

Formatter for pretty-printing for Validation.t.