[Haskell-cafe] class default method proposal

apfelmus apfelmus at quantentunnel.de
Tue Dec 11 13:29:11 EST 2007


Simon Peyton-Jones wrote:
> 
> b) having instance declarations silently spring into existence
> 
> Concerning (b) here's a suggestion.  As now, require that every instance
> requires an instance declaration.  So, in the main example of
> http://haskell.org/haskellwiki/Class_system_extension_proposal, for a new
> data type T you'd write
>
>         instance Monad T where
>           return = ...
>           (>>=)  = ...
> 
>         instance Functor T
>         instance Applicative T

Without the automatic search, this is already possible

     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

        -- aka liftM
     fmapDefault :: Monad m => (a -> b) -> m a -> m b
     fmapDefault f m = m >>= (return . f)

     instance Monad [] where
         return x = [x]
         (>>=)    = flip concatMap

     instance Functor [] where
         fmap = fmapDefault

  fmap  is already written for you, the instance declaration is only 
boilerplate. I first saw this in  Data.Traversable .


Regards,
apfelmus



More information about the Glasgow-haskell-users mailing list