# Applicative functor

### From HaskellWiki

m (→How to switch from monads: Improved layout) |
(Extended part about applicative transformers) |
||

Line 109: | Line 109: | ||

From the [[Monad Transformer Library]] we are used to have two flavours of every monad: a base monad like <hask>State</hask> and a transformer variant <hask>StateT</hask>. In the [http://hackage.haskell.org/package/transformers/ transformers] package we even have only monad transformers except the <hask>Identity</hask> monad. | From the [[Monad Transformer Library]] we are used to have two flavours of every monad: a base monad like <hask>State</hask> and a transformer variant <hask>StateT</hask>. In the [http://hackage.haskell.org/package/transformers/ transformers] package we even have only monad transformers except the <hask>Identity</hask> monad. | ||

− | So where are applicative transformers? The answer is, that we do not need special transformers for applicative functors since they can be combined in a generic way. | + | So where are applicative transformers? The answer is, that in most situations, we do not need special transformers for applicative functors since they can be combined in a generic way. |

<haskell> | <haskell> | ||

h :: f (g (a -> b)) | h :: f (g (a -> b)) | ||

Line 121: | Line 121: | ||

It can be useful to use the applicative composition even when you have a monad transformer at hand. In the example above <hask>f</hask> might be <hask>Writer (Sum Int)</hask> that is used for counting the number of involved applicative actions. Since in an applicative functor the number of run actions is independent from interim results, the writer can count the actions at compile time. | It can be useful to use the applicative composition even when you have a monad transformer at hand. In the example above <hask>f</hask> might be <hask>Writer (Sum Int)</hask> that is used for counting the number of involved applicative actions. Since in an applicative functor the number of run actions is independent from interim results, the writer can count the actions at compile time. | ||

+ | |||

+ | It is not true that transformers are unnecessary for applicatives, though. Consider <hask>State s (IO a) == s -> (s, IO a)</hask>, which behaves like an applicative with the above trick, but it is different from <hask>StateT s IO a == s -> IO (s, a)</hask>. The latter is more useful in some situations, and it is not a composition of any two applicatives. | ||

== How to switch from monads == | == How to switch from monads == |

## Latest revision as of 17:09, 7 May 2018

An applicative functor has more structure than a functor but less than a monad. See the Haddock docs for Control.Applicative.

## Contents |

## [edit] 1 Example

It has turned out that many applications do not require monad functionality but only those of applicative functors. Monads allow you to run actions depending on the outcomes of earlier actions.

do text <- getLine if null text then putStrLn "You refuse to enter something?" else putStrLn ("You entered " ++ text)

This is obviously necessary in some cases, but in other cases it is disadvantageous.

Consider an extended IO monad which handles automated closing of allocated resources. This is possible with a monad.

openDialog, openWindow :: String -> CleanIO () liftToCleanup :: IO a -> CleanIO a runAndCleanup :: CleanIO a -> IO a runAndCleanup $ do text <- liftToCleanup getLine if null text then openDialog "You refuse to enter something?" else openWindow ("You entered " ++ text)

I.e. if the dialog was opened, the dialog must be closed, but not the window. That is, the cleanup procedure depends on the outcomes of earlier actions.

Now consider the slightly different task, where functions shall register *initialization* routines
that shall be run before the actual action takes place.
(See the original discussion started by Michael T. Richter in Haskell-Cafe:
Practical Haskell Question)
This is impossible in the monadic framework.

If you eliminate this dependency, you end up in an applicative functor and there you can do the initialization trick. You could write

initializeAndRun $ liftA2 (liftToInit getLine) (writeToWindow "You requested to open a window")

## [edit] 2 Usage

If you have the variables

f :: a -> b -> c a :: f a b :: f b

- pure f <*> a <*> b
- liftA2 f a b

Consider the non-functorial expression:

x :: x g :: x -> y h :: y -> y -> z let y = g x in h y y

Very simple. Now we like to generalize this to

fx :: f x fg :: f (x -> y) fh :: f (y -> y -> z)

However, we note that

let fy = fg <*> fx in fh <*> fy <*> fy

liftA3 (\x g h -> let y = g x in h y y) fx fg fh

## [edit] 3 Some advantages of applicative functors

- Code that uses only the interface is more general than code that uses theApplicativeinterface, because there are more applicative functors than monads. TheMonadis an applicative functor on lists, whereZipListis implemented byliftA2. It is a typical example of an applicative functor that is not a monad.zipWith
- Programming with has a more applicative/functional feel. Especially for newbies, it may encourage functional style even when programming with effects. Monad programming with do notation encourages a more sequential & imperative style.Applicative

## [edit] 4 Applicative transformers

From the Monad Transformer Library we are used to have two flavours of every monad: a base monad likeSo where are applicative transformers? The answer is, that in most situations, we do not need special transformers for applicative functors since they can be combined in a generic way.

h :: f (g (a -> b)) a :: f (g a) liftA2 (<*>) h a :: f (g b)

## [edit] 5 How to switch from monads

- Start using ,liftM, etc orliftM2where you can, in place ofap/do. You will often encounter code like(>>=)

do x <- fx y <- fy return (g x y)

- It can be rewritten to . In general, whenever the choice or construction of monadic actions does not depend on the outcomes of previous monadic actions, then it should be possible to rewrite everything withliftM2 g fx fy.liftM

- When you notice you're
*only*using those monad methods, then importand replaceControl.Applicativewithreturn,purewithliftM(or(<$>)orfmap),liftAwithliftM2, etc, andliftA2withap. If your function signature was(<*>), change toMonad m => ...(and maybe renameApplicative m => ...tomor whatever).f

## [edit] 6 Alternative terms

Applicative functors were introduced by several people under different names:

- Ross Paterson called them Sequence
- Conor McBride called them Idiom
- The same kind of structure is used in the UU Parsing-Combinators.