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

Modulebifunctors-5.6.2Haskell2010

Data.Biapplicative

  • 2 classes
  • 6 values
  • Packagebifunctors-5.6.2
  • Exports8
  • LanguageHaskell2010
  • LicenceBSD-3-Clause
  • SourceBiapplicative.hs

Biapplicative bifunctors

8 declarations
classclass Bifunctor p => Biapplicative (p :: Type -> Type -> Type) where
#

Methods

Instances17Biapplicative, …
value(<<$>>) :: (a -> b) -> a -> b
#
valuebiliftA3
  1. :: Biapplicative w
  2. => a -> b -> c -> d
  3. -> e -> f -> g -> h
  4. -> w a e
  5. -> w b f
  6. -> w c g
  7. -> w d h
#

Lift ternary functions

valuetraverseBia
  1. :: (Traversable t, Biapplicative p)
  2. => a -> p b c
  3. -> t a
  4. -> p (t b) (t c)
#

Traverse a Traversable container in a Biapplicative.

traverseBia satisfies the following properties:

Pairing
traverseBia (,) t = (t, t)
Composition
traverseBia (Biff . bimap g h . f) = Biff . bimap (traverse g) (traverse h) . traverseBia f
traverseBia (Tannen . fmap f . g) = Tannen . fmap (traverseBia f) . traverse g
Naturality
 t . traverseBia f = traverseBia (t . f)

for every biapplicative transformation t.

A biapplicative transformation from a Biapplicative P to a Biapplicative Q is a function

t :: P a b -> Q a b

preserving the Biapplicative operations. That is,

Performance note

traverseBia is fairly efficient, and uses compiler rewrite rules to be even more efficient for a few important types like []. However, if performance is critical, you might consider writing a container-specific implementation.

classclass (forall a. Functor (p a)) => Bifunctor (p :: Type -> Type -> Type) where
#

A bifunctor is a type constructor that takes two type arguments and is a functor in both arguments. That is, unlike with Functor, a type constructor such as Either does not need to be partially applied for a Bifunctor instance, and the methods in this class permit mapping functions over the Left value or the Right value, or both at the same time.

Formally, the class Bifunctor represents a bifunctor from Hask -> Hask.

Intuitively it is a bifunctor where both the first and second arguments are covariant.

The class definition of a Bifunctor p uses the QuantifiedConstraints language extension to quantify over the first type argument a in its context. The context requires that p a must be a Functor for all a. In other words a partially applied Bifunctor must be a Functor. This makes Functor a superclass of Bifunctor such that a function with a Bifunctor constraint may use fmap in its implementation. Functor has been a quantified superclass of Bifunctor since base-4.18.0.0.

You can define a Bifunctor by either defining bimap or by defining both first and second. The second method must agree with fmap:

second ≡ fmap

From this it follows that:

second id ≡ id

If you supply bimap, you should ensure that:

bimap id id ≡ id

If you supply first and second, ensure:

first id ≡ id
second id ≡ id

If you supply both, you should also ensure:

bimap f g ≡ first f . second g

These ensure by parametricity:

bimap  (f . g) (h . i) ≡ bimap f h . bimap g i
first  (f . g) ≡ first  f . first  g
second (f . g) ≡ second f . second g

Methods

  • bimap :: (a -> b) -> (c -> d) -> p a c -> p b d

    Map over both arguments at the same time.

    bimap f g ≡ first f . second g
    Examples
    Example1 expression
    bimap toUpper (+1) ('j', 3)('J',4)
    Example1 expression
    bimap toUpper (+1) (Left 'j')Left 'J'
    Example1 expression
    bimap toUpper (+1) (Right 3)Right 4
  • first :: (a -> b) -> p a c -> p b c

    Map covariantly over the first argument.

    first f ≡ bimap f id
    Examples
    Example1 expression
    first toUpper ('j', 3)('J',3)
    Example1 expression
    first toUpper (Left 'j')Left 'J'
  • second :: (b -> c) -> p a b -> p a c

    Map covariantly over the second argument.

    second ≡ bimap id
    Examples
    Example1 expression
    second (+1) ('j', 3)('j',4)
    Example1 expression
    second (+1) (Right 3)Right 4
Instances21Bifunctor, …