Break `abs` into two aspects

Dannyu NDos ndospark320 at
Tue Jan 28 10:42:41 UTC 2020

This two-step proposal suggests to alter the official Haskell 2010 standard.

1. Introduction:

The `Num` typeclass effectively represents a ring. Yet it also has `abs`
and `signum`. `abs` represents a norm, but its type is wrong:

    abs :: a -> a

Mathematically, the return value must be a nonnegative real number. Yet
there is no guarantee that the type `a` can have that value. This led us to
have too strong restriction to `instance Num Complex`:

    instance RealFloat a => Num (Complex a) where ...

I found a way for `abs` (and `signum`) to have much more mathematical place.

2. Step 1:

First, I suggest to add the following variants of `abs` and `signum`:

    realAbs :: Real a => a -> a
    realAbs x = if x >= 0 then x else negate x
    realSignum :: Real a => a -> a
    realSignum x = case compare 0 x of
         LT -> -1
         EQ -> 0
         _  -> 1

These can replace `abs` and `signum` instances for integral and rational

3. Step 2:

Second, I suggest to move `abs` and `signum` from `Num` to `Floating`:

    class Floating a where
         abs :: a -> a
         signum :: a -> a

This exploits the fact that `Floating` represents a field with
characteristic 0, regarding exponential and trigonometric functions are
defined as Taylor series. The definition of convergence of series is
defined using norms.

4. Conclusion:

This enables us to implement rings (Num) and fields (Fractional) without
concerning about norms. For example, Gaussian integers.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the Libraries mailing list