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

Modulerebase-1.21.2Haskell2010

Rebase.Data.Functor.Apply

  • 2 types
  • 2 classes
  • 8 values
  • Packagerebase-1.21.2
  • Exports12
  • LanguageHaskell2010
  • LicenceMIT
  • SourceClass.hs
classclass Functor f => Apply (f :: Type -> Type) where
#

A strong lax semi-monoidal endofunctor. This is equivalent to an Applicative without pure.

Laws:

(.) <$> u <.> v <.> w = u <.> (v <.> w)
x <.> (f <$> y) = (. f) <$> x <.> y
f <$> (x <.> y) = (f .) <$> x <.> y

The laws imply that .> and <. really ignore their left and right results, respectively, and really return their right and left results, respectively. Specifically,

(mf <$> m) .> (nf <$> n) = nf <$> (m .> n)
(mf <$> m) <. (nf <$> n) = mf <$> (m <. n)

Methods

  • (<.>) :: f (a -> b) -> f a -> f binfixl 4
  • (.>) :: f a -> f b -> f binfixl 4
     a .> b = const id <$> a <.> b
  • (<.) :: f a -> f b -> f ainfixl 4
     a <. b = const <$> a <.> b
  • liftF2 :: (a -> b -> c) -> f a -> f b -> f c

    Lift a binary function into a comonad with zipping

Instances96Apply, …
classclass Functor (f :: Type -> Type) where
#

A type f is a Functor if it provides a function fmap which, given any types a and b lets you apply any function from (a -> b) to turn an f a into an f b, preserving the structure of f. Furthermore f needs to adhere to the following:

Identity

fmap id == id

Composition

fmap (f . g) == fmap f . fmap g

Note, that the second law follows from the free theorem of the type fmap and the first law, so you need only check that the former condition holds. See these articles by School of Haskell or David Luposchainsky for an explanation.

Methods

  • fmap :: (a -> b) -> f a -> f b

    fmap is used to apply a function of type (a -> b) to a value of type f a, where f is a functor, to produce a value of type f b. Note that for any type constructor with more than one parameter (e.g., Either), only the last type parameter can be modified with fmap (e.g., b in `Either a b`).

    Some type constructors with two parameters or more have a Data.Bifunctor instance that allows both the last and the penultimate parameters to be mapped over.

    Examples

    Convert from a Maybe Int to a Maybe String using show:

    Example2 expressions
    fmap show NothingNothingfmap show (Just 3)Just "3"

    Convert from an Either Int Int to an Either Int String using show:

    Example2 expressions
    fmap show (Left 17)Left 17fmap show (Right 17)Right "17"

    Double each element of a list:

    Example1 expression
    fmap (*2) [1,2,3][2,4,6]

    Apply even to the second element of a pair:

    Example1 expression
    fmap even (2,2)(2,True)

    It may seem surprising that the function is only applied to the last element of the tuple compared to the list example above which applies it to every element in the list. To understand, remember that tuples are type constructors with multiple type parameters: a tuple of 3 elements (a,b,c) can also be written (,,) a b c and its Functor instance is defined for Functor ((,,) a b) (i.e., only the third parameter is free to be mapped over with fmap).

    It explains why fmap can be used with tuples containing values of different types as in the following example:

    Example1 expression
    fmap even ("hello", 1.0, 4)("hello",1.0,True)
  • (<$) :: a -> f b -> f ainfixl 4

    Replace all locations in the input with the same value. The default definition is fmap . const, but this may be overridden with a more efficient version.

    Examples

    Perform a computation with Maybe and replace the result with a constant value if it is Just:

    Example2 expressions
    'a' <$ Just 2Just 'a''a' <$ NothingNothing
Instances241Functor, …
value($>) :: Functor f => f a -> b -> f b
#

Flipped version of <$.

Examples

Replace the contents of a Maybe Int with a constant String:

Example1 expression
Nothing $> "foo"Nothing
Example1 expression
Just 90210 $> "foo"Just "foo"

Replace the contents of an Either Int Int with a constant String, resulting in an Either Int String:

Example1 expression
Left 8675309 $> "foo"Left 8675309
Example1 expression
Right 8675309 $> "foo"Right "foo"

Replace each element of a list with a constant String:

Example1 expression
[1,2,3] $> "foo"["foo","foo","foo"]

Replace the second element of a pair with a constant String:

Example1 expression
(1,2) $> "foo"(1,"foo")
value(<$>) :: Functor f => (a -> b) -> f a -> f b
#

An infix synonym for fmap.

The name of this operator is an allusion to Prelude.$. Note the similarities between their types:

 ($)  ::              (a -> b) ->   a ->   b
(<$>) :: Functor f => (a -> b) -> f a -> f b

Whereas Prelude.$ is function application, <$> is function application lifted over a Functor.

Examples

Convert from a Maybe Int to a Maybe String using show:

Example1 expression
show <$> NothingNothing
Example1 expression
show <$> Just 3Just "3"

Convert from an Either Int Int to an Either Int String using show:

Example1 expression
show <$> Left 17Left 17
Example1 expression
show <$> Right 17Right "17"

Double each element of a list:

Example1 expression
(*2) <$> [1,2,3][2,4,6]

Apply even to the second element of a pair:

Example1 expression
even <$> (2,2)(2,True)
value(<*.>) :: Apply f => MaybeApply f (a -> b) -> f a -> f b
#

Apply a possibly-empty-with-unit container of functions to a non-empty container of values.

value(<.*>) :: Apply f => f (a -> b) -> MaybeApply f a -> f b
#

Apply a non-empty container of functions to a possibly-empty-with-unit container of values.

newtypenewtype WrappedApplicative (f :: Type -> Type) a
#

Wrap an Applicative to be used as a member of Apply

Instances8Functor, Applicative, Alternative, Alt, Apply, Plus, …
newtypenewtype MaybeApply (f :: Type -> Type) a
#

Transform an Apply into an Applicative by adding a unit.

Constructors

Instances7Functor, Applicative, Comonad, Apply, Extend, Copointed, …
value(<..>) :: Apply w => w a -> w (a -> b) -> w b
#

A variant of <.> with the arguments reversed.

valuegliftF2
  1. :: (Generic1 w, Apply (Rep1 w))
  2. => a -> b -> c
  3. -> w a
  4. -> w b
  5. -> w c
#

Generic liftF2. Caveats:

  1. Will not compile if w is a sum type.

  2. Types in w that do not mention the type variable must be instances of Semigroup.

valueliftF3 :: Apply w => (a -> b -> c -> d) -> w a -> w b -> w c -> w d
#

Lift a ternary function into a comonad with zipping