[Haskell-cafe] class default method proposal
ajb at spamcop.net
ajb at spamcop.net
Tue Dec 11 20:52:10 EST 2007
G'day all.
Quoting David Menendez <dave at zednenem.com>:
> 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
More information about the Haskell-Cafe
mailing list