Refactoring Semigroup/Monoid (was: instance Applicative Data.Map)
Michael Sloan
mgsloan at gmail.com
Tue Nov 20 00:07:43 CET 2012
I think we'd rather like to avoid UndecidableInstances in the prelude!
This does bring up an interesting idea, though, as far as using a flat
hierarchy for these classes. Default method signatures could be used to
provide similar behavior.
http://www.haskell.org/ghc/docs/7.4.2/html/users_guide/type-class-extensions.html#class-default-signatures
I'm not saying that this is a good idea (I think that a good idea would be
closer to my https://github.com/mgsloan/instance-templates proposal - which
I seriously need to rework to be more appealing), but I wonder if this
would work:
class Applicative f where
applicative_map :: (a -> b) -> f a -> f b
default applicative_map :: Monad f => (a -> b) -> f a -> f b
applicative_map = monad_map
-- ...
class Monad m where
monad_map :: (a -> b) -> m a -> m b
default monad_map :: Applicative m => (a -> b) -> m a -> m b
monad_map = applicative_map
-- ...
I haven't seen mutually recursive defaultings like this (not sure if even
works) - but could be a nifty trick for some applications. The defaults in
Applicative are more important, so these could be removed in favor of using
the usual standard defaults in Monad.
-Michael
On Mon, Nov 19, 2012 at 11:00 AM, Tyson Whitehead <twhitehead at gmail.com>wrote:
> On November 19, 2012 12:25:57 Tyson Whitehead wrote:
> > GHC 7.0.4 accepts this with FlexibleInstances and UndecidableInstances,
> but
> > it seems to still have some issues (features?) as it overrides signatures
> >
> > *Main> :t pure
> > pure :: Monad f => a -> f a
> >
> > unless you add other instances that are only at the lowel levels. For
> > example, adding Maybe at the Applicative level to the above [] instance
> >
> > instance Applicative Maybe where
> > applicative_apply (Just f) (Just x) = Just (f x)
> > applicative_apply _ _ = Nothing
> > applicative_map f (Just x) = Just (f x)
> > applicative_map _ _ = Nothing
> > applicative_pure x = Just x
> >
> > gives
> >
> > *Main> :t pure
> > pure :: Monad f => a -> f a
> > *Main> :t Main.fmap
> > Main.fmap :: Applicative f => (a -> b) -> f a -> f b
>
> Cut and paste mistake there. That last bit should have been
>
> *Main> :t pure
> pure :: Applicative f => a -> f a
> *Main> :t Main.fmap
> Main.fmap :: Applicative f => (a -> b) -> f a -> f b
>
> Adding a Functor only instance will make fmap resolve correctly.
>
> Cheers! -Tyson
>
> _______________________________________________
> Libraries mailing list
> Libraries at haskell.org
> http://www.haskell.org/mailman/listinfo/libraries
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/libraries/attachments/20121119/6b7a1442/attachment-0001.htm>
More information about the Libraries
mailing list