
On Dec 11, 2007 1:29 PM, apfelmus
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

G'day all.
Quoting David Menendez
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.
In the case of Functor and Applicative, that's true. Cases where it's likely to be more useful involve absurdly fine-grained class hierarchies, like the numeric prelude. Getting fanciful for a moment, and not claiming that this is a good structure: class Plus a b c | a b -> c where (+) :: a -> b -> c class Minus a b c | a b -> c where (-) :: a -> b -> c class Times a b c | a b -> c where (*) :: a -< b -> c class Zero a where zero :: a class One a where one :: a class (Zero a, Plus a a a, Minus a a a) => Additive a where negate :: a -> a negate a = zero - a a - b = a + negate b class (Mul a a a, One a) => Multiplicative a where (^) :: a -> Integer -> a class (Multiplicative a, Additive a) => Ring a where fromInteger :: Integer -> a zero = fromInteger 0 one = fromInteger 1 class (Ring a, Ord a) => OrderedRing a where abs :: a -> a abs x = x `max` negate x signum :: a -> a -- etc class (OrderedRing a, Show a) => Num a You can imagine how unwieldy this would now get if you just wanted to declare an instance for Num a. Cheers, Andrew Bromage
participants (2)
-
ajb@spamcop.net
-
David Menendez