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

Raja rajasharan at gmail.com
Sat Jun 4 04:40:59 UTC 2016


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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20160604/aa3025ce/attachment.html>


More information about the Beginners mailing list