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

Modulemod-0.2.0.1Haskell2010

Data.Mod.Word

Modular arithmetic, promoting moduli to the type level, with an emphasis on performance. Originally part of the arithmoi package.

This module supports only moduli, which fit into Word. Use the (slower) Data.Mod module for handling arbitrary-sized moduli.

  • 1 type
  • 3 values
  • Packagemod-0.2.0.1
  • Exports4
  • LanguageHaskell2010
  • LicenceMIT
  • SourceWord.hs
newtypenewtype Mod (m :: Nat)
#

This data type represents integers modulo m, equipped with useful instances.

For example, 3 :: Mod 10 stands for the class of integers congruent to 3 \bmod 10 \colon \ldots {−17}, −7, 3, 13, 23 \ldots

Example2 expressions
:set -XDataKinds3 + 8 :: Mod 10 -- 3 + 8 = 11 ≡ 1 (mod 10)1

Note: Mod 0 has no inhabitants, eventhough \mathbb{Z}/0\mathbb{Z} is technically isomorphic to \mathbb{Z} .

Instances24Bounded, Enum, Eq, Fractional, Num, Ord, …
valueunMod :: Mod m -> Word
#

The canonical representative of the residue class, always between 0 and m - 1 (inclusively).

Example2 expressions
:set -XDataKinds-1 :: Mod 109
valueinvertMod :: KnownNat m => Mod m -> Maybe (Mod m)
#

If an argument is coprime with the modulus, return its modular inverse. Otherwise return Nothing.

Example3 expressions
:set -XDataKindsinvertMod 3 :: Mod 10 -- 3 * 7 = 21 ≡ 1 (mod 10)Just 7invertMod 4 :: Mod 10 -- 4 and 10 are not coprimeNothing
value(^%) :: (KnownNat m, Integral a) => Mod m -> a -> Mod m
#

Drop-in replacement for ^ with a bit better performance. Negative powers are allowed, but may throw DivideByZero, if an argument is not coprime with the modulus.

Example4 expressions
:set -XDataKinds3 ^% 4 :: Mod 10    -- 3 ^ 4 = 81 ≡ 1 (mod 10)13 ^% (-1) :: Mod 10 -- 3 * 7 = 21 ≡ 1 (mod 10)74 ^% (-1) :: Mod 10 -- 4 and 10 are not coprime(*** Exception: divide by zero