Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing `Monad ((->) e)`

Tags:

haskell

monads

Typeclassopedia presents this exercise:

Implement a Monad instance for ((->) e).

Here's the MyMonad class.

class (MyMonad m) where
    ret     :: a   -> m a
    flatMap :: m a -> (a -> m b) -> m b

Then, I begin to attempt to implement the Monad instance for ((->) e).

instance (MyMonad (-> e)) where
    ret x       = ...
    flatMap m f = ...

But, I don't have intuition for what ((->) e) means.

Please help me understand it, as well as give me a hint towards solving for ret.

like image 757
Kevin Meredith Avatar asked Dec 20 '22 08:12

Kevin Meredith


1 Answers

There's something of a syntax trick going on here. It might be easier to think of (->) e as (e ->) or, even more clearly, if we write

type Arr a b = a -> b

then (->) e is about the same as Arr e.

So what is the type of ret? It ends up as

ret :: a -> (e -> a)

which should be more solvable now.

like image 200
J. Abrahamson Avatar answered Jan 12 '23 11:01

J. Abrahamson