haccepted-0.1.0.0: Data structures and algorithms
Safe HaskellNone
LanguageHaskell2010

Mod

Description

Modular arithmetic

Mod m i is an integer of type i modulo m. m will usually be a compile-time constant. If m is not known at compile time, Mod can still be used via GHC.TypeNats.SomeNat. m must be >= 2 and fit in the type i. Integer operations on i for values in [0..m-1] must not overflow. Examples to avoid are using (-) with a word type or using (*) with a large enough mod that (m-1)^2 oveflows.

This is a very general type, for something simpler see MInt.hs.

Instances of Eq, Num, Fractional exist for Mod m i. All the usual operations take O(1) time, except for recip which takes O(log n) time. This assumes Integral i methods take O(1) time. An instance of Enum exists for MInt. The enum is cyclic, it wraps to 0 after m-1. Unboxed array support is available via Unbox.

Synopsis

Documentation

newtype Mod (m :: Nat) i Source #

Type for modular arithmetic modulo m with underlying type i.

Constructors

Mod 

Fields

Instances

Instances details
(KnownNat m, Integral i) => Enum (Mod m i) Source # 
Instance details

Defined in Mod

Methods

succ :: Mod m i -> Mod m i #

pred :: Mod m i -> Mod m i #

toEnum :: Int -> Mod m i #

fromEnum :: Mod m i -> Int #

enumFrom :: Mod m i -> [Mod m i] #

enumFromThen :: Mod m i -> Mod m i -> [Mod m i] #

enumFromTo :: Mod m i -> Mod m i -> [Mod m i] #

enumFromThenTo :: Mod m i -> Mod m i -> Mod m i -> [Mod m i] #

Eq i => Eq (Mod m i) Source # 
Instance details

Defined in Mod

Methods

(==) :: Mod m i -> Mod m i -> Bool #

(/=) :: Mod m i -> Mod m i -> Bool #

(KnownNat m, Integral i) => Fractional (Mod m i) Source # 
Instance details

Defined in Mod

Methods

(/) :: Mod m i -> Mod m i -> Mod m i #

recip :: Mod m i -> Mod m i #

fromRational :: Rational -> Mod m i #

(KnownNat m, Integral i) => Num (Mod m i) Source # 
Instance details

Defined in Mod

Methods

(+) :: Mod m i -> Mod m i -> Mod m i #

(-) :: Mod m i -> Mod m i -> Mod m i #

(*) :: Mod m i -> Mod m i -> Mod m i #

negate :: Mod m i -> Mod m i #

abs :: Mod m i -> Mod m i #

signum :: Mod m i -> Mod m i #

fromInteger :: Integer -> Mod m i #

Ord i => Ord (Mod m i) Source # 
Instance details

Defined in Mod

Methods

compare :: Mod m i -> Mod m i -> Ordering #

(<) :: Mod m i -> Mod m i -> Bool #

(<=) :: Mod m i -> Mod m i -> Bool #

(>) :: Mod m i -> Mod m i -> Bool #

(>=) :: Mod m i -> Mod m i -> Bool #

max :: Mod m i -> Mod m i -> Mod m i #

min :: Mod m i -> Mod m i -> Mod m i #

Show i => Show (Mod m i) Source # 
Instance details

Defined in Mod

Methods

showsPrec :: Int -> Mod m i -> ShowS #

show :: Mod m i -> String #

showList :: [Mod m i] -> ShowS #

NFData i => NFData (Mod m i) Source # 
Instance details

Defined in Mod

Methods

rnf :: Mod m i -> () #

Unbox (Mod m i) Source # 
Instance details

Defined in Mod

Associated Types

type Unboxed (Mod m i) Source #

Methods

toU :: Mod m i -> Unboxed (Mod m i) Source #

frU :: Unboxed (Mod m i) -> Mod m i Source #

type Unboxed (Mod m i) Source # 
Instance details

Defined in Mod

type Unboxed (Mod m i) = i

type M7 = Mod 1000000007 Int Source #

Commonly used modulus.

type M3 = Mod 998244353 Int Source #

Commonly used modulus.

invMaybe :: forall m i. (KnownNat m, Integral i) => Mod m i -> Maybe (Mod m i) Source #

The multiplicative inverse modulo m. It exists if and only if the number is coprime to m. O(log n).