Used by 1 package in nightly-2026-10-03(full list with versions):

either-n

System F Logo

Data types like Either but with more constructors, and optics for the constructors of any sum type.

Download from hackage.

Either3

Either3 a b c is a value of one of three types:

data Either3 a b c
  = First3 a
  | Second3 b
  | Third3 c

The Functor, Applicative and Monad instances act on the third type parameter, so First3 and Second3 short-circuit, in the same way Left does for Either. The Bifunctor instances act on the second and third type parameters.

Either3T

Either3T f a b c is an Either3 inside a type constructor f, in the same way that ExceptT e m a is m (Either e a):

newtype Either3T f a b c = Either3T (f (Either3 a b c))

Use _Wrapped to convert between Either3T f a b c and f (Either3 a b c). The Monad instance requires Monad f, and MonadIO, MonadFail, MonadState, MonadReader, MonadWriter, MonadError, MonadRWS, MonadCont and MonadZip are lifted from f. either3Identity is the isomorphism between Either3 a b c and Either3T Identity a b c.

The type parameters are in the order f a b c so that Either3T f a is a Bifunctor. As a consequence, Either3T has no MonadTrans instance.

Injections

Data.Lens.Injection provides the classes Injection1 to Injection19, the sum-type duals of Field1 to Field19 from lens. Where _1 is a lens to the first field of a product, _I1 is a prism to the first constructor of a sum:

>>> (Left 1 :: Either Int String) ^? _I1
Just 1

>>> _I2 # True :: Either3 Int Bool String
Second3 True

Instances are provided for Either, Maybe, Bool, Ordering, lists, Identity, (), Data.Functor.Sum, GHC.Generics.:+:, Either3 and Either3T Identity. Each class has a default implementation for any type with a Generic instance, so an instance needs no body:

data T a = T1 a | T2 | T3 Int Bool
  deriving Generic

instance Injection1 (T a) (T b) a b
instance Injection2 (T a) (T a) () ()
instance Injection3 (T a) (T a) (Int, Bool) (Int, Bool)

A constructor with no fields has the focus (), a constructor with one field has that field as its focus, and a constructor with more than one field has any type with a single constructor with the same fields, such as a tuple, as its focus.

Changes

Changelog

0.1.0.0

  • Add Data.Either3, re-exporting Data.Either3.Either3 and Data.Either3.Either3T
  • Add Either3 a b c, with constructors First3, Second3 and Third3, and instances for Eq, Eq1, Eq2, Ord, Ord1, Ord2, Show, Show1, Show2, Generic, Generic1, Data, NFData, Semigroup, Functor, Apply, Applicative, Bind, Monad, Alt, Extend, Selective, Foldable, Traversable, Bifunctor, Bifoldable, Bitraversable, Swap, FunctorWithIndex, FoldableWithIndex, TraversableWithIndex, MonadZip, Each, Injection1, Injection2 and Injection3
  • Add Either3T f a b c, a newtype for f (Either3 a b c), as ExceptT e m a is for m (Either e a), with Wrapped and Rewrapped instances for converting to and from f (Either3 a b c). Instances for Generic, Generic1, Data, NFData, Eq, Eq1, Eq2, Ord, Ord1, Ord2, Show, Show1, Show2, Semigroup, Functor, Apply, Applicative, Bind, Monad, Alt, Extend, Selective, Foldable, Traversable, Bifunctor, Bifoldable, Bitraversable, Swap, FunctorWithIndex, FoldableWithIndex, TraversableWithIndex and Each, each with the corresponding constraint on f; instances lifted from f, as for MaybeT, for MonadIO, MonadFail, MonadState, MonadReader, MonadWriter, MonadError, MonadRWS, MonadCont and MonadZip; and instances for f = Identity for Injection1, Injection2, Injection3, GetEither3, HasEither3 and AsEither3
  • Add either3Identity, the isomorphism between Either3 a b c and Either3TIdentity a b c
  • Add isomorphisms reordering the type parameters of Either3 (either3ACB, either3BAC, either3BCA, either3CAB, either3CBA) and of Either3T (either3TACB, either3TBAC, either3TBCA, either3TCAB, either3TCBA)
  • Add classy optics GetEither3, HasEither3, ReviewEither3, AsEither3 and GetEither3T, HasEither3T, ReviewEither3T, AsEither3T. Instances of HasX define setX, and instances of AsX define matchX; the lens and prism follow from them
  • Add rewrite rules fusing compositions of fmap, bimap, foldMap, foldr, traverse, bifoldMap and bitraverse for Either3 and Either3T
  • Add hedgehog property tests for the laws of every instance, using hedgehog-fn to generate functions
  • Add Injection1 .. Injection19 classes (Data.Lens.Injection.InjectionN), the sum-type duals of Field1 .. Field19 from lens, with prisms _I1 .. _I19, and a Data.Lens.Injection module re-exporting them
  • Add Injection instances for Either, Maybe, Bool, Ordering, [], Identity, (), Data.Functor.Sum and GHC.Generics (:+:). The second injection of [] focuses on NonEmpty
  • Add a GHC.Generics default for each InjectionN class, so that an instance for a type with a Generic instance needs no body
  • Add Data.Lens.Injection.Generic, with injection, a prism to the nth constructor of any type with a Generic instance, where n is given by a proxy, such as Proxy @0. The generic machinery behind it is in Data.Lens.Injection.Generic.Internal, which is not part of the stable API