[Haskell-beginners] Functions as containers; implications on being a Monad ((->) r)

Alex Belanger i.caught.air at gmail.com
Sat Jun 4 14:27:05 UTC 2016


Does it help if I bring your:

(\x -> x+7) <$> (+3) => (+10)

It doesn't magically become `(+10)`. The previous step unseen is `(+7) .
(+3)`. fmap for ((->) r) is defined as function  composition `.`.

fmap f g = f . g

You can think of functors as containers or contexts; both analogies work
well up to some degree (with the latter maybe more abstract and thus
flexible).

Another small terminology tip that could help you with your thinkering
might be to call ((->) r) a partially applied function. Suddently, it
narrows how you can potentially use/combine them.

Last tip, Monad ((->) r) is commonly refered to as the Reader monad.
Example of how it'd be used:

doThings = do
    a <- (*2)
    b <- (+10)
    return (a + b)

Notice how we can work with partially applied functions to which something
have yet to be applied and create a new partial function whose argument is
going to fill all the open holes.

This isn't easy to explain without looking at the types of everything, so I
encourage you to write it down and work out the types (:

Alex
On Jun 4, 2016 12:41 AM, "Raja" <rajasharan at gmail.com> wrote:


Everyone agrees ((->) r) is a Functor, an Applicative and a Monad but I've
never seen any good writeup going into details of explaining this.

So I was trying to brainstorm with my brother and went pretty far into the
concept for quite a few hours, but still got stuck when it came to Monads.

Before I showcase the question/problem I wanted to share our thinking
process.

Lets stick with common types like Maybe a, [a], simple function (a -> b)

**Everything is a Container**

 Just 4 => this is a container wrapping some value
[1,2,3] => this is a container wrapping bunch of values
(+3) => this is a container wrapping domains & ranges (infinite dictionary)

**When is a Container a Functor**

If we can peek inside a container and apply a simple function (a->b) to
each of its values and wrap the result back inside the container, then it
becomes a Functor.

Let's use (\x -> x+7) as simple function along with above three Containers

(\x -> x+7) <$> Just 4 => Just 11
(\x -> x+7) <$> [1,2,3] => [8,9,10]
(\x -> x+7) <$> (+3) => (+10)  -- well there is no Show defined but you get
the idea

**When is a Container an Applicative**

The simple function from above is also now wrapped inside a container and
we should be able to peek to use it just like functor. Also lets simplify
(\x -> x+7) to (+7).

Just (+7) <*> Just 4 => Just 11
[(+7)] <*> [1,2,3] => [8,9,10]
(\x -> (+7)) <*> (+3) => (+10) -- again no Show defined but works when pass
a number
-- but (+7) still needs to be wrapped in a Container though

**When is a Container a Monad**

This time we don't have a simple function (a->b) instead we have a
non-simple function (a -> Container). But rest stays almost the same.

We have to first peek inside a container and apply non-simple function to
each of its values. Since its a non-simple function we need to collect all
the returned Containers, unwrap them and wrap them all back in a Container
again.
(it almost feels like unwrap and wrapping them back is going to complicate
things)

Also Non-simple function cannot be reused as is for all three Containers
like in Functors & Applicatives.

Just 4 >>= (\x -> Just (x+7)) => Just 11
[1,2,3] >>= (\x -> [x+7]) => [8,9,10]
(+3) >>= (\x -> (+7)) => (+7)

Wait a minute ... the last line doesn't look right. Or should I say it
doesn't feel right to discard the `x' altogether.

OK let's jump to do this:
(+3) >>= (\x -> (+x)) => ??? -- apparently this solves for the equation:
f(x) = 2x + 3

(is 2x + 3 obvious for anyone??? it took us way longer to derive it)
(Does it have anything to do with Monad laws by any chance?)

This is where it feels like "Functions as containers" concept starts to
breakdown; its not the right analogy for Monads.

What does it even mean to unwrap a bunch of functions and wrap them back
again?

Hope this intrigues some of you as it did to us. Any thoughts and comments
greatly appreciated.

Thanks,
Raja

_______________________________________________
Beginners mailing list
Beginners at haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20160604/63791f50/attachment.html>


More information about the Beginners mailing list