What is the mathematical term for Haskell Num typeclass?

250 Views Asked by At

The definition of Haskell Num typeclass:

class  Num a  where
    {-# MINIMAL (+), (*), abs, signum, fromInteger, (negate | (-)) #-}

    (+), (-), (*)       :: a -> a -> a
    -- | Unary negation.
    negate              :: a -> a
    -- | Absolute value.
    abs                 :: a -> a
    -- | Sign of a number.
    -- The functions 'abs' and 'signum' should satisfy the law:
    --
    -- > abs x * signum x == x
    --
    -- For real numbers, the 'signum' is either @-1@ (negative), @0@ (zero)
    -- or @1@ (positive).
    signum              :: a -> a
    -- | Conversion from an 'Integer'.
    -- An integer literal represents the application of the function
    -- 'fromInteger' to the appropriate value of type 'Integer',
    -- so such literals have type @('Num' a) => a@.
    fromInteger         :: Integer -> a

    {-# INLINE (-) #-}
    {-# INLINE negate #-}
    x - y               = x + negate y
    negate x            = 0 - x

Though Haskell standard defines no laws, there are some conventions.

The presence of $(+)$, $(-)$, $\text{negate}$, and $(×)$ suggests this is a ring. Let's denote the ring $R$.

The $\text{fromInteger}$ is a homomorphism from $ℤ$ to $R$. Also, it must satisfy that $\text{fromInteger }0 = 0$ and $\text{fromInteger }1 = 1$. So the ring must have unity.

$\text{abs}$ is mathematically troublesome to define. If this denoted norm, it would have type $\text{abs}: R \rightarrow ℝ$. But actually, it has type $\text{abs}: R \rightarrow R$! So let's define norm $R \rightarrow ℝ$ and a homomorphism $ℝ \rightarrow R$, and compose them.

$\text{signum}$ is defined as $\text{signum }x = x ÷ \text{abs }x$, if $x ≠ 0$ and the ring is a division ring.

Is there a mathematical term that summerizes all these ideas?

1

There are 1 best solutions below

0
On BEST ANSWER

After a conversation in Haskell Libraries Mailing List, I can answer this question myself.

There are at least 3 ways to define abs:

Absolute Value

For an ordered ring, values can be classified to positive values, zero, or negative value. As a consequence, I can give a top-level function for absolute value:

realAbs :: Real a => a -> a
realAbs x = if 0 <= x
    then x
    else negate x

Then I can let abs = realAbs.

Norm

If norm of every element is also an element, abs can be defined as the norm.

Associates of Action of Group of Units

Let $R$ be the ring and take group of units $U(R)$. Then $U(R)$ acts on $R$, then I can pick representives from each associates. For example, Gaussian integers $\mathbb{Z}^2$ have group of units $\{1,i,-1,-i\}$, and thus abs of Gaussian integers can be defined like:

instance Integral a => Num (Complex a) where
    abs 0 = 0
    abs (x :+ y) = if 0 < x && 0 <= y
        then x :+ y
        else if x <= 0 && 0 < y
            then y :+ negate x
            else if x < 0 && y <= 0
                then negate x :+ negate y
                else negate y :+ x