concept Applicative in category haskell

appears as: Applicative, n Applicative
Haskell in Depth

This is an excerpt from Manning's book Haskell in Depth.

  • Applicative enables injecting value into a context and applying a function in a context to a value in a context.
  • The Alternative type class extents the Applicative type class. There is also the MonadPlus type class with the same idea for Monad.

    newtype MyApp logEntry state a = MyApp {
          runApp :: ReaderT AppEnv
                      (WriterT [logEntry]
                          (StateT state
                                  IO)) a
        } deriving (Functor, Applicative, Monad,
                    MonadIO,
                    MonadReader AppEnv,
                    MonadWriter [logEntry],
                    MonadState state)
    Get Programming with Haskell

    This is an excerpt from Manning's book Get Programming with Haskell.

    Figure 14.2 shows the type classes that are defined in Haskell’s standard library. Arrows from one class to another indicate a superclass relationship. This unit has covered most of the basic type classes. In unit 3, you’ll start exploring the more abstract type classes Semigroup and Monoid, and you’ll start to see how different type classes can be from interfaces. In unit 5, you’ll look at a family of type classes—Functor, Applicative, and Monad—that provide a way to model the context of a computation. Although this last group is particularly challenging to learn, it also allows for some of Haskell’s most powerful abstractions.

    You’ve just finished learning about two important type classes, Functor and Applicative. Each has allowed you to perform increasingly powerful computations within a context such as Maybe or IO. Functor allows you to change individual values in a context:

    GHCi> (+ 2) <$> Just 3
    Just 5

    Applicative increases your power by enabling you to use partial application in a context. This, in turn, allows you to use multiple arguments in a context:

    Applicative and Functor have taught you to think more abstractly about solving problems in types such as Maybe. The general form of the combining function you want is as follows:

    Applicative f => f a -> (a -> f b) -> f b

    You’ll assume the Applicative constraint rather than Functor only because Applicative is more powerful. If you can’t solve your problem with Applicative, you can’t solve it with Functor either. Now let’s take a look at the tools you get from Applicative and Functor:

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

    Unfortunately, for all the power you’ve gained with Applicative, it doesn’t look like any combination of these tools will solve this rather straightforward problem of wanting to chain together two functions. You can solve this problem by writing a wrapper for lookup-Credits to be a function of Maybe UserName -> Maybe PlayerCredits.

    Listing 30.5. altLookupCredits, a solution without using Functor or Applicative
    altLookupCredits :: Maybe UserName -> Maybe PlayerCredits altLookupCredits Nothing = Nothing altLookupCredits (Just username) = lookupCredits username

    30.1. The limitations of Applicative and Functor

    Remembering back to our visual representation of Functor, Applicative, and Monad, you’ve solved three of the four possible mismatches. The Functor’s fmap provides an adapter when you have a value in a context and a regular function, and want your result in a context, as shown in figure 30.1.

    Figure 30.1. A visualization of the mismatch between function and context that Functor solves

    Applicative’s <*> allows you to connect a function in a context with values in a context, as shown in figure 30.2.

    Figure 30.2. Applicative's <*> solves the problem of functions themselves being in a context.

    And finally, Applicative’s pure method allows you to handle the case of your final result not being in a context, as shown in figure 30.3.

    Figure 30.3. Applicative’s pure method means you can always put a result into a context.