Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is this declaration not allowed in Haskell?

Tags:

haskell

I know that Functor and Applicative should be superclasses of Monad, but aren't for historical reasons. However, why isn't is possible to declare Monad an instance of Functor? This would have roughly the same effect, but without having to modify existing code. If you're trying to do this, GHC complains:

instance Functor Monad where
   fmap = liftM

Class `Monad' used as a type
In the instance declaration for `Functor Monad'

Why is that? There's probably a good reason for it.

like image 553
bseibold Avatar asked Sep 29 '11 09:09

bseibold


1 Answers

Your syntax is wrong. Monad is a typeclass, not a data type. What you could write is

instance Monad a => Functor a where fmap = liftM

However, this will only work with the extensions FlexibleInstances (permits instances that are not of the form T a1 a2 ... an where a1, a2, ... an are type variables and there is no context) and UndecidableInstances (which permits this specific instance [I don't know why this is needed]).

like image 192
fuz Avatar answered Oct 14 '22 23:10

fuz