[Haskell-cafe] class default method proposal
David Menendez
dave at zednenem.com
Tue Dec 11 15:16:39 EST 2007
On Dec 11, 2007 1:29 PM, apfelmus <apfelmus at quantentunnel.de> wrote:
> 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 .
>
This is pretty much how I define Functor and Applicative instances for my
monads. It is admittedly irritating to have to write out the boilerplate,
but it doesn't seem irritating enough to require a language extension to
eliminate.
--
Dave Menendez <dave at zednenem.com>
<http://www.eyrie.org/~zednenem/>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/glasgow-haskell-users/attachments/20071211/086426c7/attachment.htm
More information about the Glasgow-haskell-users
mailing list