Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

For different possible Monad instances of a type, is the implied Functor instance always the same?

According to the Typeclassopedia and this link a type can only have a single Functor instance (there's a proof in the link). But it is my understanding that it is possible for a given type to have multiple possible Monad instances, is this right? But for a given Monad instance there is a free Functor instance with

fmap f xs  =  xs >>= return . f

From this, I conclude that if I stumble upon a type in which I can define multiple Monad instances in different ways, then the fmap function derived as above must equal for all of them, in other words, if I have two pairs of functions:

bind_1 :: m a -> (a -> m b) -> m b
unit_1 :: a -> m a

bind_2 :: m a -> (a -> m b) -> m b
unit_2 :: a -> m a

for the same type constructor m, than, necessarily:

xs `bind_1` (unit_1 . f) == xs `bind_2` (unit_2 . f)

for all xs :: a and f :: a -> b.

Is this true? Does this hold as a theorem?

like image 737
Rafael S. Calsaverini Avatar asked Feb 13 '14 17:02

Rafael S. Calsaverini


People also ask

Is every monad a functor?

As I understand, every monad is a functor but not every functor is a monad. A functor takes a pure function (and a functorial value) whereas a monad takes a Kleisli arrow, i.e. a function that returns a monad (and a monadic value).

Are all monads Applicatives?

Monads are not a replacement for applicative functors Instead, every monad is an applicative functor (as well as a functor). It is considered good practice not to use >>= if all you need is <*>, or even fmap.

What is a functor in Haskell?

Functor in Haskell is a kind of functional representation of different Types which can be mapped over. It is a high level concept of implementing polymorphism. According to Haskell developers, all the Types such as List, Map, Tree, etc. are the instance of the Haskell Functor.

Is maybe a functor Haskell?

Another simple example of a functor is the Maybe type. This object can contain a value of a particular type as Just , or it is Nothing (like a null value).


1 Answers

Yes. In fact we can make the stronger statement that all function with the type

fmap :: (a -> b) -> (F a -> F b)

such that fmap id = id are equivalent. This actually just falls out of the type of fmap with something called parametricity.

In your case, if >>= and return satisfy the monad laws, then

 mFmap f a  = a >>= return . f
 mFmap id a = a >>= return . id
 mFmap id a = a >>= return
 mFmap id a = a
 mFmap id = id

By the monad law that a >>= return is just a. Using this result, we can appeal to the free theorem we get from parametricity and we have our proof.

like image 150
Daniel Gratzer Avatar answered Oct 25 '22 02:10

Daniel Gratzer