HORIZON HASKELLDocslts/ghc-9.10.xc74966e2026-09-27Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27

Modulenumeric-prelude-0.4.4Haskell98

Algebra.IntegralDomain

  • 1 class
  • 20 values

Class

4 declarations
classclass C a => C a where
#

IntegralDomain corresponds to a commutative ring, where a mod b picks a canonical element of the equivalence class of a in the ideal generated by b. div and mod satisfy the laws

                        a * b === b * a
(a `div` b) * b + (a `mod` b) === a
              (a+k*b) `mod` b === a `mod` b
                    0 `mod` b === 0

Typical examples of IntegralDomain include integers and polynomials over a field. Note that for a field, there is a canonical instance defined by the above rules; e.g.,

instance IntegralDomain.C Rational where
    divMod a b =
       if isZero b
         then (undefined,a)
         else (a\/b,0)

It shall be noted, that div, mod, divMod have a parameter order which is unfortunate for partial application. But it is adapted to mathematical conventions, where the operators are used in infix notation.

Minimal definition: divMod or (div and mod)

Instances19C, …
  • C IntegerDefined in numeric-prelude-0.4.4 · Algebra.IntegralDomain
  • C Int16Defined in numeric-prelude-0.4.4 · Algebra.IntegralDomain
  • C Int32Defined in numeric-prelude-0.4.4 · Algebra.IntegralDomain
  • C Int64Defined in numeric-prelude-0.4.4 · Algebra.IntegralDomain
  • C Int8Defined in numeric-prelude-0.4.4 · Algebra.IntegralDomain
  • C Word16Defined in numeric-prelude-0.4.4 · Algebra.IntegralDomain
  • C Word32Defined in numeric-prelude-0.4.4 · Algebra.IntegralDomain
  • C Word64Defined in numeric-prelude-0.4.4 · Algebra.IntegralDomain
  • C Word8Defined in numeric-prelude-0.4.4 · Algebra.IntegralDomain
  • C IntDefined in numeric-prelude-0.4.4 · Algebra.IntegralDomain
  • C WordDefined in numeric-prelude-0.4.4 · Algebra.IntegralDomain
  • C TDefined in numeric-prelude-0.4.4 · Number.Peano
  • Integral a => C (T a)Defined in numeric-prelude-0.4.4 · MathObj.Wrapper.Haskell98
  • C a => C (T a)Defined in numeric-prelude-0.4.4 · MathObj.Wrapper.NumericPrelude
  • C a => C (T a)Defined in numeric-prelude-0.4.4 · Number.Complex
  • (Ord a, C a) => C (T a)Defined in numeric-prelude-0.4.4 · Number.NonNegative · orphan
  • (Ord a, C a, C a) => C (T a)Defined in numeric-prelude-0.4.4 · Number.NonNegativeChunky

    divMod is implemented in terms of divModStrict. If it is needed we could also provide a function that accesses the divisor first in a lazy way and then uses a strict divisor for subsequent rounds of the subtraction loop. This way we can handle the cases "dividend smaller than divisor" and "dividend greater than divisor" in a lazy and efficient way. However changing the way of operation within one number is also not nice.

  • (C a, C a) => C (T a)Defined in numeric-prelude-0.4.4 · MathObj.Polynomial

    The C instance is intensionally built from the C structure of the polynomial coefficients. If we would use Integral.C a superclass, then the Euclidean algorithm could not determine the greatest common divisor of e.g. [1,1] and [2].

  • (C a, C a) => C (T a)Defined in numeric-prelude-0.4.4 · MathObj.PowerSeries
methoddiv :: a -> a -> a
#
methodmod :: a -> a -> a
#
methoddivMod :: a -> a -> (a, a)
#
Property
\n (QC.NonZero m) -> let (q,r) = divMod n m in n == (q*m+r :: Integer)

Derived functions

10 declarations
valuedivModZero :: (C a, C a) => a -> a -> (a, a)
#

Allows division by zero. If the divisor is zero, then the dividend is returned as remainder.

valuedivChecked :: (C a, C a) => a -> a -> a
#

Returns the result of the division, if divisible. Otherwise undefined.

valuesafeDiv :: (C a, C a) => a -> a -> a
#

Deprecated. use divChecked instead

Returns the result of the division, if divisible. Otherwise undefined.

valuedivUp :: C a => a -> a -> a
#

divUp n m is similar to div but it rounds up the quotient, such that divUp n m * m = roundUp n m.

valueroundDown :: C a => a -> a -> a
#

roundDown n m rounds n down to the next multiple of m. That is, roundDown n m is the greatest multiple of m that is at most n. The parameter order is consistent with div and friends, but maybe not useful for partial application.

Property
\n (QC.NonZero m) -> div n m * m == (roundDown n m :: Integer)
valueroundUp :: C a => a -> a -> a
#

roundUp n m rounds n up to the next multiple of m. That is, roundUp n m is the greatest multiple of m that is at most n.

Property
\n (QC.NonZero m) -> divUp n m * m == (roundUp n m :: Integer)
Property
\n (QC.Positive m) -> let x = roundDown n m in  n-m < x && x <= (n :: Integer)
Property
\n (QC.NonZero m) -> - roundDown n m == (roundUp (-n) m :: Integer)

Algorithms

2 declarations
valuedecomposeVarPositional :: (C a, C a) => [a] -> a -> [a]
#

decomposeVarPositional [b0,b1,b2,...] x decomposes x into a positional representation with mixed bases x0 + b0*(x1 + b1*(x2 + b2*x3)) E.g. decomposeVarPositional (repeat 10) 123 == [3,2,1]

Properties

8 declarations