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

Daniel Bergey bergey at alum.mit.edu
Sat Jun 4 16:56:02 UTC 2016


I think "a functor is a container" is not so helpful.  It works OK for
Maybe and List, but it doesn't seem helpful in understanding Either,
Reader, Writer, State, Continuation, promises.

For the instance ((->) r), it's important to keep track of which argument is the
r.  I think it helps to write the lambdas explicitly, rather than the
point-free (+3).  Keeping the type variables straight is also a reminder
that all these need to work for any types r, a, b, not only when all
three types are Int: (r ~ a ~ b ~ Int).

So we have:
fmap :: (a -> b) -> (r -> a) -> (r -> b)
fmap f g = \r -> f (g r)

fmap (\x -> x + 7) (\r -> r + 3) = \r -> (r + 3) + 7

(<*>) :: (r -> a -> b) -> (r -> a) -> (r -> b)
f <*> g = \r -> f r (g r)

Since r, a, b may all be different types, this is the only possible definition.

(\r -> (\x -> x + 7)) <*> (\r -> r + 3) = \r -> (r + 3) + 7

Note that the function on the left of <*> ignores its r parameter, so
this is exactly the same as the fmap example.  This follows the law (for
any Applicative) that

fmap f === pure f <*>

For an example that doesn't ignore the r:

(\r -> (\x -> r + x)) <*> (\r -> r + 3) = \r -> r + (r + 3)

Note also that the function on the left takes the argument of type r
first, and the argument of type a second.  The second argument to >>=,
on the other hand, takes an a, then an r:

(>>=) :: (r -> a) -> (a -> (r -> b)) -> (r -> b)
f >>= g = \r -> g (f r)

(\r -> r + 3) >>= (\x -> (\r -> r + 7)) = \r -> r + 7

Here the function on the right of >>= ignores its argument, so this
isn't very interesting.

(\r -> r + 3) >>= (\x -> (\r -> r + x)) = \r -> r + (r + 3)

is a bit more interesting. It uses both functions, but it's exactly the
same as the second Applicative example.

I *think* it's the case that for (r ->), there isn't anything we can do
with the Monad instance that we can't do with Applicative.  If someone
can confirm or refute that, I'd appreciate it.  That's of course not
true in general for other monads.

Hope this helps.

bergey


On 2016-06-04 at 00:40, 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


More information about the Beginners mailing list