Copyright | (c) 2017-2019 Andrew Lelechenko |
---|---|
License | MIT |
Maintainer | Andrew Lelechenko <andrew.lelechenko@gmail.com> |
Safe Haskell | None |
Language | Haskell2010 |
Modular arithmetic, promoting moduli to the type level, with an emphasis on performance. Originally part of arithmoi package.
Documentation
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 modulo 10: …−17, −7, 3, 13, 23…
>>>
:set -XDataKinds
>>>
3 + 8 :: Mod 10
(1 `modulo` 10) -- because 3 + 8 = 11 ≡ 1 (mod 10)
Warning: division by residue, which is not
coprime
with the modulo, throws DivideByZero
.
Consider using invertMod
for non-prime moduli.
Instances
KnownNat m => Bounded (Mod m) Source # | |
KnownNat m => Enum (Mod m) Source # | |
Eq (Mod m) Source # | |
KnownNat m => Fractional (Mod m) Source # | See the warning about division above. |
KnownNat m => Num (Mod m) Source # | |
Ord (Mod m) Source # | |
KnownNat m => Show (Mod m) Source # | |
Generic (Mod m) Source # | |
NFData (Mod m) Source # | |
KnownNat m => GcdDomain (Mod m) Source # | See the warning about division above. |
KnownNat m => Euclidean (Mod m) Source # | See the warning about division above. |
KnownNat m => Field (Mod m) Source # | See the warning about division above. |
Defined in Data.Mod | |
KnownNat m => Semiring (Mod m) Source # | |
KnownNat m => Ring (Mod m) Source # | |
type Rep (Mod m) Source # | |
unMod :: Mod m -> Natural Source #
The canonical representative of the residue class, always between 0 and m - 1 inclusively.
(^%) :: (KnownNat m, Integral a) => Mod m -> a -> Mod m infixr 8 Source #
Drop-in replacement for ^
with much better performance.
Negative powers are allowed, but may throw DivideByZero
, if an argument
is not coprime with the modulo.
Building with -O
triggers a rewrite rule ^
= ^%
.
>>>
:set -XDataKinds
>>>
3 ^% 4 :: Mod 10
(1 `modulo` 10) -- because 3 ^ 4 = 81 ≡ 1 (mod 10)>>>
3 ^% (-1) :: Mod 10
(7 `modulo` 10) -- because 3 * 7 = 21 ≡ 1 (mod 10)>>>
4 ^% (-1) :: Mod 10
(*** Exception: divide by zero -- because 4 and 10 are not coprime