In opposition of Functor as super-class of Monad

Alexey Khudyakov alexey.skladnoy at gmail.com
Tue Jan 4 14:32:55 CET 2011


On 04.01.2011 13:24, oleg at okmij.org wrote:
>
> I'd like to argue in opposition of making Functor a super-class of
> Monad. I would argue that superclass constraints are not the right
> tool for expressing mathematical relationship such that all monads are
> functors and applicatives.
>
> Then argument is practical. It seems that making Functor a superclass
> of Monad makes defining new monad instances more of a chore, leading
> to code duplication. To me, code duplication is a sign that an
> abstraction is missing or misused.
>


I think I understood your point. But it looks like that it's possible to 
use subclass's function in superclass instance. At very least GHC is 
able to do it.

Following example works just fine without any language extensions in 
GHC6.12.3


import Prelude hiding (Monad(..), Functor(..))

class Functor f where
   fmap :: (a -> b) -> f a -> f b

class Functor m => Monad m where
   return :: a -> m a
   (>>=) :: m a -> (a -> m b) -> m b

instance Functor Maybe where
   fmap f m = m >>= (return . f)
instance Monad Maybe where
   return = Just
   Nothing >>= _ = Nothing
   Just x  >>= f = f x



More information about the Haskell-prime mailing list