In hoc signo vinces (Was: Revamping the numeric classes)
Joe Fasel
jhf@lanl.gov
Mon, 12 Feb 2001 14:13:38 -0700 (MST)
On 09-Feb-2001 William Lee Irwin III wrote:
| Matrix rings actually manage to expose the inappropriateness of signum
| and abs' definitions and relationships to Num very well:
|
| class (Eq a, Show a) => Num a where
| (+), (-), (*) :: a -> a -> a
| negate :: a -> a
| abs, signum :: a -> a
| fromInteger :: Integer -> a
| fromInt :: Int -> a -- partain: Glasgow extension
|
| Pure arithmetic ((+), (-), (*), negate) works just fine.
|
| But there are no good injections to use for fromInteger or fromInt,
| the type of abs is wrong if it's going to be a norm, and it's not
| clear that signum makes much sense.
For fromInteger, fromInt, and abs, the result should be a scalar matrix.
For the two coercions, I don't think there would be much controversy about this.
I agree that it would be nice if abs could return a scalar, but this requires
multiparameter classes, so we have to make do with a scalar matrix.
We already have this problem with complex numbers: It might be nice
if the result of abs were real.
signum does make sense. You want abs and signum to obey these laws:
x == abs x * signum x
abs (signum x) == (if abs x == 0 then 0 else 1)
Thus, having fixed an appropriate matrix norm, signum is a normalization
function, just as with reals and complexes.
If we make the leap to multiparameter classes, I think this is
the signature we want:
class (Eq a, Show a) => Num a b | a --> b where
(+), (-), (*) :: a -> a -> a
negate :: a -> a
abs :: a -> b
signum :: a -> a
scale :: b -> a -> a
fromInteger :: Integer -> a
fromInt :: Int -> a
Here, b is the type of norms of a. Instead of the first law above, we have
x == scale (abs x) (signum x)
All this, of course, is independent of whether we want a more proper
algebraic class hierarchy, with (+) introduced by Monoid, negate and (-)
by Group, etc.
Cheers,
--Joe
Joseph H. Fasel, Ph.D. email: jhf@lanl.gov
Technology Modeling and Analysis phone: +1 505 667 7158
University of California fax: +1 505 667 2960
Los Alamos National Laboratory post: TSA-7 MS F609; Los Alamos, NM 87545