... | ... | @@ -76,32 +76,32 @@ Proposed new classes: |
|
|
|
|
|
```wiki
|
|
|
class AbelianGroup a where -- could also factor out Monoid
|
|
|
zero :: a
|
|
|
(+), (-) :: a -> a -> a
|
|
|
negate :: a -> a
|
|
|
zero :: a
|
|
|
(+), (-) :: a -> a -> a
|
|
|
negate :: a -> a
|
|
|
|
|
|
-- Minimal complete definition:
|
|
|
-- zero, (+) and (negate or (-))
|
|
|
negate x = zero - x
|
|
|
x - y = x + negate y
|
|
|
-- Minimal complete definition:
|
|
|
-- zero, (+) and (negate or (-))
|
|
|
negate x = zero - x
|
|
|
x - y = x + negate y
|
|
|
|
|
|
class AbelianGroup a => Ring a where
|
|
|
(*) :: a -> a -> a
|
|
|
one :: a
|
|
|
fromInteger :: Integer -> a
|
|
|
|
|
|
-- Minimal complete definition:
|
|
|
-- (*) and (one or fromInteger)
|
|
|
one = fromInteger 1
|
|
|
fromInteger n
|
|
|
| n < 0 = negate (fi (negate n))
|
|
|
| otherwise = fi n
|
|
|
where fi 0 = zero
|
|
|
fi 1 = one
|
|
|
fi n
|
|
|
| even n = fin + fin
|
|
|
| otherwise = fin + fin + one
|
|
|
where fin = fi (n `div` 2)
|
|
|
(*) :: a -> a -> a
|
|
|
one :: a
|
|
|
fromInteger :: Integer -> a
|
|
|
|
|
|
-- Minimal complete definition:
|
|
|
-- (*) and (one or fromInteger)
|
|
|
one = fromInteger 1
|
|
|
fromInteger n
|
|
|
| n < 0 = negate (fi (negate n))
|
|
|
| otherwise = fi n
|
|
|
where fi 0 = zero
|
|
|
fi 1 = one
|
|
|
fi n
|
|
|
| even n = fin + fin
|
|
|
| otherwise = fin + fin + one
|
|
|
where fin = fi (n `div` 2)
|
|
|
```
|
|
|
|
|
|
|
... | ... | @@ -109,7 +109,7 @@ Haskell 98 compatibility class: |
|
|
|
|
|
```wiki
|
|
|
class (Eq a, Show a, Ring a) => Num a where
|
|
|
abs, signum :: a -> a
|
|
|
abs, signum :: a -> a
|
|
|
```
|
|
|
|
|
|
## The Fractional class
|
... | ... | @@ -137,16 +137,16 @@ Proposed new classes: |
|
|
|
|
|
```wiki
|
|
|
class Ring a => DivisionRing a where
|
|
|
(/) :: a -> a -> a
|
|
|
recip :: a -> a
|
|
|
fromRational :: Rational -> a
|
|
|
(/) :: a -> a -> a
|
|
|
recip :: a -> a
|
|
|
fromRational :: Rational -> a
|
|
|
|
|
|
-- Minimal complete definition:
|
|
|
-- recip or (/)
|
|
|
recip x = one / x
|
|
|
x / y = x * recip y
|
|
|
fromRational x = fromInteger (numerator x) /
|
|
|
fromInteger (denominator x)
|
|
|
-- Minimal complete definition:
|
|
|
-- recip or (/)
|
|
|
recip x = one / x
|
|
|
x / y = x * recip y
|
|
|
fromRational x = fromInteger (numerator x) /
|
|
|
fromInteger (denominator x)
|
|
|
|
|
|
class DivisionRing a => Field a
|
|
|
```
|
... | ... | @@ -196,19 +196,19 @@ Proposed new class: |
|
|
|
|
|
```wiki
|
|
|
class Ring a => EuclideanDomain a where
|
|
|
div, mod :: a -> a -> a
|
|
|
divMod :: a -> a -> (a,a)
|
|
|
|
|
|
stdAssociate :: a -> a
|
|
|
stdUnit :: a -> a
|
|
|
|
|
|
-- Minimal complete definition:
|
|
|
-- (divMod or (div and mod)) and stdUnit
|
|
|
n `divMod` d = (n `div` d, n `mod` d)
|
|
|
n `div` d = q where (q,r) = divMod n d
|
|
|
n `mod` d = r where (q,r) = divMod n d
|
|
|
|
|
|
stdAssociate x = x `div` stdUnit x
|
|
|
div, mod :: a -> a -> a
|
|
|
divMod :: a -> a -> (a,a)
|
|
|
|
|
|
stdAssociate :: a -> a
|
|
|
stdUnit :: a -> a
|
|
|
|
|
|
-- Minimal complete definition:
|
|
|
-- (divMod or (div and mod)) and stdUnit
|
|
|
n `divMod` d = (n `div` d, n `mod` d)
|
|
|
n `div` d = q where (q,r) = divMod n d
|
|
|
n `mod` d = r where (q,r) = divMod n d
|
|
|
|
|
|
stdAssociate x = x `div` stdUnit x
|
|
|
```
|
|
|
|
|
|
|
... | ... | @@ -216,16 +216,16 @@ Haskell 98 compatibility class: |
|
|
|
|
|
```wiki
|
|
|
class (Real a, Enum a, EuclideanDomain a) => Integral a where
|
|
|
quot, rem :: a -> a -> a
|
|
|
quotRem :: a -> a -> (a,a)
|
|
|
toInteger :: a -> Integer
|
|
|
|
|
|
-- Minimal complete definition:
|
|
|
-- toInteger
|
|
|
n `quot` d = q where (q,r) = quotRem n d
|
|
|
n `rem` d = r where (q,r) = quotRem n d
|
|
|
quotRem n d = if signum r == - signum d then (q+one, r-d) else qr
|
|
|
where qr@(q,r) = divMod n d
|
|
|
quot, rem :: a -> a -> a
|
|
|
quotRem :: a -> a -> (a,a)
|
|
|
toInteger :: a -> Integer
|
|
|
|
|
|
-- Minimal complete definition:
|
|
|
-- toInteger
|
|
|
n `quot` d = q where (q,r) = quotRem n d
|
|
|
n `rem` d = r where (q,r) = quotRem n d
|
|
|
quotRem n d = if signum r == - signum d then (q+one, r-d) else qr
|
|
|
where qr@(q,r) = divMod n d
|
|
|
```
|
|
|
|
|
|
## The RealFloat class
|
... | ... | |