3.
About this talk
Slides are available here:
http://es.slideshare.net/JosLuisGarcaHernndez/applicative-style-programming
Code is available here:
https://github.com/pepegar/Control.Applicative.Talk
jlgarhdez HaskellMADpepegar

4.
What are we learning today?
● What are applicatives?
● When do we use them?
● Who is using them?
● How to use them?
jlgarhdez HaskellMADpepegar

5.
What are applicatives?
Applicatives are an abstraction between functions and monads. And they open the
doors for the so called applicative style!
jlgarhdez HaskellMADpepegar

7.
What are applicatives?
Let’s understand the typeclass:
It exposes a `pure` function, that puts a value inside the applicative context
And a `<*>` function, that applies a function inside an applicative context to the
content of another value with the same applicative context
jlgarhdez HaskellMADpepegar

15.
A small example. Recap
So, what do we learn from our small example?
Monads are not a silver bullet, and can be replaced easily by Applicative Functors
when ordering is not important.
jlgarhdez HaskellMADpepegar

16.
When to use applicatives?
When we need more than a functor, and less than a monad…
Sounds appealing, but WTF is this!?
jlgarhdez HaskellMADpepegar

22.
Who uses Applicative?
Applicative is being used all over the place nowadays. Some interesting examples
of its use are:
● HAXL. Efficient data access
● CmdTheLine. Command line argument parsing
● Validation. Data validation library
● Attoparsec. ByteString parsing library
jlgarhdez HaskellMADpepegar

23.
How to use Applicatives?
@jlgarhdez
Applicatives are really easy to use. You just need to provide an instance of the
typeclass for your data type.
data Maybe a = Just a
| Nothing
instance Applicative Maybe where
pure = Just
Just f <*> m = fmap f m
Nothing <*> _m = Nothing
jlgarhdez HaskellMADpepegar

39.
Abstract Syntax Trees
As you know, the most important part of functional programming is referential
transparency. This means creating values, not executing effects.
That’s what our free applicatives does, they create little programs, or Abstract
Syntax Trees
jlgarhdez HaskellMADpepegar

40.
Applicatives AST
As you imagine from our
implementation, this Blog does
nothing, just create values. It does
not go to the DB to fetch
users/posts/whatever.
It just creates what is called an
Abstract Syntax Tree
jlgarhdez HaskellMADpepegar

41.
Monadic AST
And, if we compare this AST with the
one created by its monadic
counterpart, implemented in terms of
monadic bind, we can see something
interesting.
We need to evaluate getPost 1 in
order to evaluate getAuthor 1
jlgarhdez HaskellMADpepegar

42.
Applicative AST. Static Analysis
@jlgarhdez
Applicative functors’ ASTs allow you to explore them from the top down without
evaluating a single line of code! This technique is called static analysis, and is
awesome for:
● Optimizing performance (deduplicate requests, in a series of HTTP requests)
● Calculate dependencies automatically
● Lint your code
jlgarhdez HaskellMADpepegar

43.
Static Analysis
Analysis of a program that does not evaluate the program.
It is possible with our Applicatives!
jlgarhdez HaskellMADpepegar

50.
Composing Applicative
Functors
Unlike Monads, our Applicatives are closed on composition. This means that you
can provide an instance of Applicative for the product of any other two
applicatives!
jlgarhdez HaskellMADpepegar

52.
Applicative Do Notation
Created at Facebook, ApplicativeDo extension allows you to write Applicative
Code in a more familiar fashion, if you come from imperative programming.
jlgarhdez HaskellMADpepegar