HORIZON HASKELLDocslts/ghc-9.10.x248f8f02026-10-05Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · 248f8f0 · 2026-10-05

Modulererebase-1.21.2Haskell2010

Data.Bifunctor.Apply

  • 2 classes
  • 4 values
  • Packagererebase-1.21.2
  • Exports6
  • LanguageHaskell2010
  • LicenceMIT
  • SourceBifunctor.hs
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
Instances25Bifunctor, …
value(<<$>>) :: (a -> b) -> a -> b
#
value(<<..>>) :: Biapply p => p a c -> p (a -> b) (c -> d) -> p b d
#
valuebilift2
  1. :: Biapply w
  2. => a -> b -> c
  3. -> d -> e -> f
  4. -> w a d
  5. -> w b e
  6. -> w c f
#

Lift binary functions

valuebilift3
  1. :: Biapply 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

classclass Bifunctor p => Biapply (p :: Type -> Type -> Type) where
#

Methods

  • (<<.>>) :: p (a -> b) (c -> d) -> p a c -> p b dinfixl 4
  • (.>>) :: p a b -> p c d -> p c dinfixl 4
    a .> b ≡ const id <$> a <.> b
    
  • (<<.) :: p a b -> p c d -> p a binfixl 4
    a <. b ≡ const <$> a <.> b
    
Instances14Biapply, …