concept Applicative in category functional programming

appears as: The Applicative, Applicative, Applicative, n Applicative
Functional Programming in Scala

This is an excerpt from Manning's book Functional Programming in Scala.

The Applicative interface is important for a few reasons:

  • In general, it’s preferable to implement combinators like traverse using as few assumptions as possible. It’s better to assume that a data type can provide map2 than flatMap. Otherwise we’d have to write a new traverse every time we encountered a type that’s Applicative but not a Monad! We’ll look at examples of such types next.
  • Because Applicative is “weaker” than Monad, this gives the interpreter of applicative effects more flexibility. To take just one example, consider parsing. If we describe a parser without resorting to flatMap, this implies that the structure of our grammar is determined before we begin parsing. Therefore, our interpreter or runner of parsers has more information about what it’ll be doing up front and is free to make additional assumptions and possibly use a more efficient implementation strategy for running the parser, based on this known structure. Adding flatMap is powerful, but it means we’re generating our parsers dynamically, so the interpreter may be more limited in what it can do. Power comes at a cost. See the chapter notes for more discussion of this issue.
  • 12.2. The Applicative trait

    Applicative functors can be captured by a new interface, Applicative, in which map2 and unit are primitives.

    Listing 12.1. Creating the Applicative interface
    trait Applicative[F[_]] extends Functor[F] { // primitive combinators def map2[A,B,C](fa: F[A], fb: F[B])(f: (A, B) => C): F[C] def unit[A](a: => A): F[A] // derived combinators def map[B](fa: F[A])(f: A => B): F[B] = #1 map2(fa, unit(()))((a, _) => f(a)) #2 def traverse[A,B](as: List[A])(f: A => F[B]): F[List[B]] #3 as.foldRight(unit(List[B]()))((a, fbs) => map2(f(a), fbs)(_ :: _)) } #1: We can implement map in terms of unit and map2. #2: Recall () is the sole value of type Unit, so unit(()) is calling unit with the dummy value (). #3: Definition of traverse is identical.

    This establishes that all applicatives are functors. We implement map in terms of map2 and unit, as we’ve done before for particular data types. The implementation is suggestive of laws for Applicative that we’ll examine later, since we expect this implementation of map to preserve structure as dictated by the Functor laws.

    Note that the implementation of traverse is unchanged. We can similarly move other combinators into Applicative that don’t depend directly on flatMap or join.

    Furthermore, we can now make Monad[F] a subtype of Applicative[F] by providing the default implementation of map2 in terms of flatMap. This tells us that all monads are applicative functors, and we don’t need to provide separate Applicative instances for all our data types that are already monads.

    Listing 12.2. Making Monad a subtype of Applicative
    trait Monad[F[_]] extends Applicative[F] { def flatMap[A,B](fa: F[A])(f: A => F[B]): F[B] = join(map(fa)(f)) #1 def join[A](ffa: F[F[A]]): F[A] = flatMap(ffa)(fa => fa) def compose[A,B,C](f: A => F[B], g: B => F[C]): A => F[C] = a => flatMap(f(a))(g) def map[B](fa: F[A])(f: A => B): F[B] = flatMap(fa)((a: A) => unit(f(a))) def map2[A,B,C](fa: F[A], fb: F[B])(f: (A, B) => C): F[C] = flatMap(fa)(a => map(fb)(b => f(a,b))) } #1: A minimal implementation of Monad must implement unit and override either flatMap or join and map.

    So far, we’ve been just rearranging the functions of our API and following the type signatures. Let’s take a step back to understand the difference in expressiveness between Monad and Applicative and what it all means.