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.Functor.Alt

  • 2 types
  • 3 classes
  • 10 values
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, …
classclass Functor f => Alt (f :: Type -> Type) where
#

Laws:

<!> is associative:             (a <!> b) <!> c = a <!> (b <!> c)
<$> left-distributes over <!>:  f <$> (a <!> b) = (f <$> a) <!> (f <$> b)

If extended to an Alternative then <!> should equal <|>.

Ideally, an instance of Alt also satisfies the "left distribution" law of MonadPlus with respect to <.>:

<.> right-distributes over <!>: (a <!> b) <.> c = (a <.> c) <!> (b <.> c)

IO, Either a, ExceptT e m and GHC.Conc.STM instead satisfy the "left catch" law:

pure a <!> b = pure a

Maybe and Identity satisfy both "left distribution" and "left catch".

These variations cannot be stated purely in terms of the dependencies of Alt.

When and if MonadPlus is successfully refactored, this class should also be refactored to remove these instances.

The right distributive law should extend in the cases where the a Bind or Monad is provided to yield variations of the right distributive law:

(m <!> n) >>- f = (m >>- f) <!> (m >>- f)
(m <!> n) >>= f = (m >>= f) <!> (m >>= f)

Methods

Instances50Alt, …
  • Alt FirstDefined in semigroupoids-6.0.1 · Data.Functor.Alt
  • Alt LastDefined in semigroupoids-6.0.1 · Data.Functor.Alt
  • Alt IntMapDefined in semigroupoids-6.0.1 · Data.Functor.Alt
  • Alt SeqDefined in semigroupoids-6.0.1 · Data.Functor.Alt
  • Alt NonEmptyDefined in semigroupoids-6.0.1 · Data.Functor.Alt
  • Alt IdentityDefined in semigroupoids-6.0.1 · Data.Functor.Alt

    Choose the first option every time. While 'choose the last option' every time is also valid, this instance satisfies more laws.

  • Alt FirstDefined in semigroupoids-6.0.1 · Data.Functor.Alt
  • Alt LastDefined in semigroupoids-6.0.1 · Data.Functor.Alt
  • Alt MaybeDefined in semigroupoids-6.0.1 · Data.Functor.Alt
  • Alt IODefined in semigroupoids-6.0.1 · Data.Functor.Alt

    This instance does not actually satisfy the (<.>) right distributive law It instead satisfies the "left catch" law

  • Alt VectorDefined in vector-instances-3.4.2 · Data.Vector.Instances · orphan
  • Alt []Defined in semigroupoids-6.0.1 · Data.Functor.Alt
  • Alternative f => Alt (WrappedApplicative f)Defined in semigroupoids-6.0.1 · Data.Functor.Alt
  • MonadPlus m => Alt (WrappedMonad m)Defined in semigroupoids-6.0.1 · Data.Functor.Alt
  • Semigroup e => Alt (Validation e)Defined in either-5.0.3 · Data.Either.Validation

    For two errors, this instance reports both of them.

  • Ord k => Alt (Map k)Defined in semigroupoids-6.0.1 · Data.Functor.Alt
  • Alt ProxyDefined in semigroupoids-6.0.1 · Data.Functor.Alt
  • Alt U1Defined in semigroupoids-6.0.1 · Data.Functor.Alt
  • Alt V1Defined in semigroupoids-6.0.1 · Data.Functor.Alt
  • Alt (Alt f)Defined in free-5.2 · Control.Alternative.Free
  • Alt (Alt f)Defined in free-5.2 · Control.Alternative.Free.Final
  • Alt (Either a)Defined in semigroupoids-6.0.1 · Data.Functor.Alt
  • Alt f => Alt (Coyoneda f)Defined in kan-extensions-5.2.7 · Data.Functor.Coyoneda
  • Alt f => Alt (Yoneda f)Defined in kan-extensions-5.2.7 · Data.Functor.Yoneda
  • Alt f => Alt (Lift f)Defined in semigroupoids-6.0.1 · Data.Functor.Alt
  • (Functor f, Monad f) => Alt (MaybeT f)Defined in semigroupoids-6.0.1 · Data.Functor.Alt
  • (Hashable k, Eq k) => Alt (HashMap k)Defined in semigroupoids-6.0.1 · Data.Functor.Alt
  • ArrowPlus a => Alt (WrappedArrow a b)Defined in semigroupoids-6.0.1 · Data.Functor.Alt
  • Alt f => Alt (Rec1 f)Defined in semigroupoids-6.0.1 · Data.Functor.Alt
  • Alt f => Alt (Static f a)Defined in semigroupoids-6.0.1 · Data.Semigroupoid.Static
  • Alt f => Alt (Backwards f)Defined in semigroupoids-6.0.1 · Data.Functor.Alt
  • Alt f => Alt (IdentityT f)Defined in semigroupoids-6.0.1 · Data.Functor.Alt
  • Alt f => Alt (ReaderT e f)Defined in semigroupoids-6.0.1 · Data.Functor.Alt
  • Alt f => Alt (StateT e f)Defined in semigroupoids-6.0.1 · Data.Functor.Alt
  • Alt f => Alt (StateT e f)Defined in semigroupoids-6.0.1 · Data.Functor.Alt
  • Alt f => Alt (WriterT w f)Defined in semigroupoids-6.0.1 · Data.Functor.Alt
  • Alt f => Alt (WriterT w f)Defined in semigroupoids-6.0.1 · Data.Functor.Alt
  • Alt f => Alt (WriterT w f)Defined in semigroupoids-6.0.1 · Data.Functor.Alt
  • Alt f => Alt (Reverse f)Defined in semigroupoids-6.0.1 · Data.Functor.Alt
  • (Functor f, Monad f, Semigroup e) => Alt (ExceptT e f)Defined in semigroupoids-6.0.1 · Data.Functor.Alt
  • Semigroup c => Alt (K1 i c)Defined in semigroupoids-6.0.1 · Data.Functor.Alt
    since 5.3.8
  • (Alt f, Alt g) => Alt (Product f g)Defined in semigroupoids-6.0.1 · Data.Functor.Alt
  • (Alt f, Alt g) => Alt (f :*: g)Defined in semigroupoids-6.0.1 · Data.Functor.Alt
  • (v ~~ v', Alt v') => Alt (Codensity v)Defined in kan-extensions-5.2.7 · Control.Monad.Codensity
  • Alt f => Alt (M1 i c f)Defined in semigroupoids-6.0.1 · Data.Functor.Alt
  • Alt f => Alt (RWST r w s f)Defined in semigroupoids-6.0.1 · Data.Functor.Alt
  • Alt f => Alt (RWST r w s f)Defined in semigroupoids-6.0.1 · Data.Functor.Alt
  • Alt f => Alt (RWST r w s f)Defined in semigroupoids-6.0.1 · Data.Functor.Alt
  • (Alt f, Functor g) => Alt (Compose f g)Defined in semigroupoids-6.0.1 · Data.Functor.Alt
  • (Alt f, Functor g) => Alt (f :.: g)Defined in semigroupoids-6.0.1 · Data.Functor.Alt
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 $. Note the similarities between their types:

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

Whereas $ 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)
valuegalt :: (Generic1 f, Alt (Rep1 f)) => f a -> f a -> f a
#

Generic (<!>). Caveats:

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

  2. Any types where the a does not appear must have a Semigroup instance.

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

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 MaybeApply (f :: Type -> Type) a
#

Transform an Apply into an Applicative by adding a unit.

Constructors

Instances7Functor, Applicative, Comonad, Apply, Extend, Copointed, …
newtypenewtype WrappedApplicative (f :: Type -> Type) a
#

Wrap an Applicative to be used as a member of Apply

Instances8Functor, Applicative, Alternative, Alt, Apply, Plus, …