liveBook · Manning

Applicative functor

As explained in Learn Haskell by Example

Applicative is a Haskell type class for applying functions in a computational context to values in that context and sequencing the resulting computations.

An applicative functor combines computations that share a context. In Haskell, `Applicative` extends `Functor`: where `fmap` applies an ordinary unary function to a wrapped value, applicative application lets a wrapped function consume wrapped arguments. This makes a fixed sequence of parsers read several pieces of input and assemble one result. section 12.1.3 section 12.3

class Functor f => Applicative f where
  pure :: a -> f a
  (<*>) :: f (a -> b) -> f a -> f b
  liftA2 :: (a -> b -> c) -> f a -> f b -> f c
  (*>) :: f a -> f b -> f b
  (<*) :: f a -> f b -> f a
  {-# MINIMAL pure, ((<*>) | liftA2) #-}

The core API puts a value into a context with `pure` and applies a contextual function with `(<*>)`; `liftA2`, `(*>)`, and `(<*)` support common combinations. section 12.1.3

Use the operators according to which results a sequence should retain:

  • Start a construction with `(<$>)` when mapping its first wrapped argument, then add further arguments with `(<*>)`. section 12.1.3
  • Use `(*>)` to run a computation and discard its result, or `(<*)` to retain the left result while still running the right computation. section 12.1.3
  • Use `pure` to lift a plain value or function into the same context; use `liftA2` for a binary function. section 12.1.3

Build a PNM header parser

A header has a fixed layout: a `P1` marker, spaces, width, more spaces, and height. Map `Header` over the first integer, sequence punctuation with `*>`, and apply the resulting wrapped constructor to the second integer. Each parser consumes input in turn; a failed parser returns its error instead of a header. `Header <$> (string "P1" *> spaces *> integer) <*> (spaces *> integer)` section 12.1.3

Applicative composition is appropriate when the parser’s structure is known before parsing begins. It cannot choose later parsing behavior from an earlier result. For a PNM format whose magic number determines whether a maximum value must be read, use `Monad` and bind instead. section 12.1.5

See failure propagation with `Maybe`

`Just (+1) <*> Just 1` produces `Just 2`, while applying the same function to `Nothing` produces `Nothing`. Chaining `pure add3 <*> Just 1 <*> Just 2 <*> Just 3` builds `Just 6`; any missing argument makes the combined result `Nothing`. section 12.1.3

How Learn Haskell by Example covers it

How deep it goes
Defines `pure`, `(<*>)`, and helper operators, then implements an Applicative instance for a custom Parser.
Examples
Demonstrates Maybe arithmetic, Hello World parsing, and PNM Header parsing.
What it assumes
Builds on Functor and introduces applicative composition before deferring data-dependent choices to Monad.
What you can do after
Build it from scratch, implement and use an Applicative Parser for fixed-format input.
How it connects
Connects Functor, Maybe, Parser, Monad, Alternative, and effectful code.

Also explained in

Compare how 4 books explain applicative functor →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Haskell by Example. The section links point to the parts of the book it draws on.