I have a quarrel with the class Num declaration in PreludeCore.

I am a relative newcomer to the Haskell mailing list and so these remarks
may possibly have already been covered. In any case, I come to Haskell
from Gofer, which supports type predicates with more than one argument.
Perhaps these remarks can be taken as lending support for this facility.


A major point about overloading is the ability to mimic standard
mathematical practice in notation. Frequently one wants "additive" types
that support (+),(-),zero but not (*); e.g. vectors, functions into
additive types, etc. What you do want (*) for is scalar multiplication.
My personalised prelude in Gofer has "unbundled" Num into classes Add, Act
and Unit, as follows:

class Unit a where unit :: a

class Act a b where (*) :: a -> b -> b   -- not possible in Haskell

class Add a where
  (+),(-) :: a -> a -> a
  negate :: a -> a
  zero :: a

instance (Act a b, Act a c) => Act a (b,c) where
  x * (y,z) = (x*y,x*z)

-- ditto for triples etc

instance Act a b => Act a (c->b) where
  x * f = \z -> (x*(f z))

instance (Add a, Add b) => Add (a,b) where
   (x,y) + (x',y') = (x+x',y+y')
   (x,y) - (x',y') = (x-x',y-y')
   negate (x,y) = (negate x, negate y)
   zero = (zero,zero)

--- ditto for triples etc

instance Add a => Add (b->a) where
  f + g = \z -> (f z)+(g z)
  f - g = \z -> (f z)-(g z)
  negate f = \z -> negate (f z)
  zero = \z -> zero

I have found this a convenient basis to work with. I would be grateful for
comments.

-- Gavin Wraith

Reply via email to