[Haskell] PROPOSAL: class aliases

Paul Govereau govereau at eecs.harvard.edu
Thu Oct 13 13:41:14 EDT 2005


On Oct 12, John Meacham wrote:
>
> [...]
>
> > class Num a where
> >     (+), (*)    :: a -> a -> a
> >     (-)         :: a -> a -> a
> >     negate      :: a -> a
> >     fromInteger :: Integer -> a
> 
> ideally we would want to split it up like so (but with more mathematically
> precise names):
> 
> > class Additive a where
> >   (+) :: a -> a -> a
> >   zero :: a
> >
> > class Additive a => AdditiveNegation where
> >     (-)         :: a -> a -> a
> >     negate      :: a -> a
> >     x - y  = x + negate y
> >
> > class Multiplicative a where
> >   (*) :: a -> a -> a
> >   one :: a
> >
> > class FromInteger a where
> >     fromInteger      :: Integer -> a
> 
> [...]
> 
> > class alias (Addititive a, AdditiveNegation a,
> >              Multiplicative a, FromInteger a) => Num a where
> >    one = fromInteger 1
> >    zero = fromInteger 0
> >    negate x = zero - x

This class alias isn't 100% backwards compatible, because the original
Num class doesn't have a zero method. For instance, if I had written
this function in my program:

  zero :: Num a => a
  zero = fromInteger 0

Then, after swapping in the new alias, Num, the compiler would
probably complain that I have multiple definitions for zero.

Perhaps there could be a mechanism for hiding class methods as well?
e.g.

class alias (Addititive a without zero,  -- remove zero
             AdditiveNegation a,
             Multiplicative a,
             FromInteger a) => Num a where ...

I am not sure this could still be done with a source-to-source
translation, but perhaps it is worth considering. Of course, if we
allow union and subtraction, then why not addition, intersection,
complement (ok, maybe not complement).

Paul


More information about the Haskell mailing list