| Safe Haskell | Safe |
|---|---|
| Language | Haskell98 |
Data.Modular
Contents
Description
Types for working with integers modulo some constant.
- data Mod i n
- unMod :: (i `Mod` n) -> i
- toMod :: forall n i. (Integral i, KnownNat n) => i -> i `Mod` n
- toMod' :: forall n i j. (Integral i, Integral j, KnownNat n) => i -> j `Mod` n
- inv :: forall n i. (KnownNat n, Integral i) => Mod i n -> Mod i n
- type (/) = Mod
- type ℤ = Integer
- modVal :: forall i proxy n. (Integral i, KnownNat n) => i -> proxy n -> Mod i n
- data SomeMod i
- someModVal :: Integral i => i -> Integer -> Maybe (SomeMod i)
Documentation
and its synonym Mod/ let you wrap arbitrary numeric types
in a modulus. To work with integers (mod 7) backed by ,
you could use one of the following equivalent types:Integer
Mod Integer 7 Integer `Mod` 7 Integer/7 ℤ/7
('ℤ' is a synonym for provided by this library. In
Emacs, you can use the TeX input mode to type it with Integer\Bbb{Z}.)
The usual numeric typeclasses are defined for these types. You can
always extract the underlying value with .unMod
Here is a quick example:
>>>10 * 11 :: ℤ/75
It also works correctly with negative numeric literals:
>>>(-10) * 11 :: ℤ/72
Modular division is an inverse of modular multiplication. It is defined when divisor is coprime to modulus:
>>>7 `div` 3 :: ℤ/1613>>>3 * 13 :: ℤ/167
To use type level numeric literals you need to enable the
DataKinds extension and to use infix syntax for Mod or the /
synonym, you need TypeOperators.
Preliminaries
To use type level numeric literals you need to enable
the DataKinds extension:
>>>:set -XDataKinds
To use infix syntax for or the Mod/ synonym,
enable TypeOperators:
>>>:set -XTypeOperators
Modular arithmetic
Wraps an underlying Integeral type i in a newtype annotated
with the bound n.
Instances
| (Integral i, KnownNat n) => Bounded (Mod i n) Source # | |
| (Integral i, KnownNat n) => Enum (Mod i n) Source # | |
| Eq i => Eq (Mod i n) Source # | |
| (Integral i, KnownNat n) => Integral (Mod i n) Source # | Integer division uses modular inverse |
| (Integral i, KnownNat n) => Num (Mod i n) Source # | |
| Ord i => Ord (Mod i n) Source # | |
| (Read i, Integral i, KnownNat n) => Read (Mod i n) Source # | |
| (Integral i, KnownNat n) => Real (Mod i n) Source # | |
| Show i => Show (Mod i n) Source # | |
toMod :: forall n i. (Integral i, KnownNat n) => i -> i `Mod` n Source #
Injects a value of the underlying type into the modulus type, wrapping as appropriate.
toMod' :: forall n i j. (Integral i, Integral j, KnownNat n) => i -> j `Mod` n Source #
Wraps an integral number, converting between integral types.
inv :: forall n i. (KnownNat n, Integral i) => Mod i n -> Mod i n Source #
The modular inverse.
>>>inv 3 :: ℤ/75>>>3 * 5 :: ℤ/71
Note that only numbers coprime to n have an inverse modulo n:
inv 6 :: ℤ/15
- ** Exception: divide by 6 (mod 15), non-coprime to modulus
modVal :: forall i proxy n. (Integral i, KnownNat n) => i -> proxy n -> Mod i n Source #
Convert an integral number i into a value given modular
bound Modn at type level.
A modular number with an unknown bound.