ArticleslgStudy

mathematics

Applicative functor

Applicative functor is a mathematics topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Applicative functor rather than just read about it. In short: In functional programming, an applicative functor, or an applicative for short, is an intermediate structure between functors and monads. Applicative functors allow for functorial computations to be sequenced (unlike plain functors), but don't allow using results from prior computations in the definition of subsequent ones (unlike monads).

Key takeaways

  • Applicative functor belongs to mathematics; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Applicative functor to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Applicative functor from memory before moving on to harder problems.

Reference excerpt

In functional programming, an applicative functor, or an applicative for short, is an intermediate structure between functors and monads. Applicative functors allow for functorial computations to be sequenced (unlike plain functors), but don't allow using results from prior computations in the definition of subsequent ones (unlike monads). In terms of category theory, applicative functors may be defined as lax monoidal functors with tensorial strength. This may not be the most obvious categorification of the standard definition of applicative functor given below, but they are equivalent in the category of Haskell types. Note that the observation that all monads are applicative functors is specific to the category of Haskell types, and is not true in general with this categorical definition of applicative functor; monads in an arbitrary category need not preserve any monoidal product. Applicative functors were introduced in 2008 by Conor McBride and Ross Paterson in their paper Applicative programming with effects. Applicative functors first appeared as a library feature in Haskell, but have since spread to other languages such as Idris, Agda, OCaml, Scala, and F#. Glasgow Haskell, Idris, and F# offer language features designed to ease programming with applicative functors. In Haskell, applicative functors are implemented in the Applicative type class. While in languages like Haskell monads are applicative functors, this is not always the case in general settings of category theory—examples of monads which are not strong can be found on Math Overflow.

Definition In Haskell, an applicative is a parameterized type that can be thought of as being a container for data of the parameter type with two additional methods: pure and <*>. The pure method for an applicative of parameterized type f has type

and can be thought of as bringing values into the applicative. The <*> method for an applicative of type f has type

and can be thought of as the equivalent of function application inside the applicative. Alternatively, instead of providing <*>, one may provide a function called liftA2. These two functions may be defined in terms of each other; therefore only one is needed for a minimally complete definition. Applicatives are also required to satisfy four equational laws:

Identity: pure id <*> v = v Composition: pure (.) <*> u <*> v <*> w = u <*> (v <*> w) Homomorphism: pure f <*> pure x = pure (f x) Interchange: u <*> pure y = pure ($ y) <*> u Every applicative is a functor. To be explicit, given the methods pure and <*>, fmap can be implemented as

The commonly used notation g <$> x is equivalent to pure g <*> x.

Examples In Haskell, the Maybe type can be made an instance of the type class Applicative using the following definition:

As stated in the Definition section, pure turns an a into a Maybe a, and <*> applies a Maybe function to a Maybe value. Using the Maybe applicative for type a allows one to operate on values of type a with the error being handled automatically by the applicative machinery. For example, to add m :: Maybe Int and n :: Maybe Int, one needs only write

For the non-error case, adding m=Just i and n=Just j gives Just(i+j). If either of m or n is Nothing, then the result will be Nothing also. This example also demonstrates how applicatives allow a sort of generalized function application.

See also Functor Monad

References

External links Description of the Applicative typeclass in the Haskell docs Definition of the Applicative typeclass in the Glasgow Haskell Prelude

Worked examples

Example 1 — a first encounter with Applicative functor

Start with the simplest possible case. Write down what Applicative functor claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In mathematics, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Applicative functor before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Applicative functor ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Applicative functor

In research
Applicative functor appears in mathematics research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Applicative functor in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Applicative functor is common in secondary-school and first-year university syllabi. It links to neighbouring topics Functional programming, Programming idioms, Software design patterns, so understanding it makes those chapters shorter.
In everyday life
Look for Applicative functor outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Applicative functor” →

How to study Applicative functor in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Applicative functor means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Applicative functor out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Applicative functor in simple terms?

In functional programming, an applicative functor, or an applicative for short, is an intermediate structure between functors and monads. Applicative functors allow for functorial computations to be sequenced (unlike plain functors), but don't allow using results from prior computations in the defi…

Why does Applicative functor matter?

Because it connects several mathematics ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Applicative functor?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Applicative functor.

Tags

  • Functional programming
  • Programming idioms
  • Software design patterns

Keep exploring