mod alternatives and similar packages
Based on the "Math" category.
Alternatively, view mod alternatives based on common mentions on social networks and blogs.

vector
An efficient implementation of Intindexed arrays (both mutable and immutable), with a powerful loop optimisation framework . 
statistics
A fast, high quality library for computing with statistics in Haskell. 
HerbiePlugin
GHC plugin that improves Haskell code's numerical stability 
computationalalgebra
GeneralPurpose Computer Algebra System as an EDSL in Haskell 
mwcrandom
A very fast Haskell library for generating high quality pseudorandom numbers. 
dimensional
Dimensional library variant built on Data Kinds, Closed Type Families, TypeNats (GHC 7.8+). 
numhask
A haskell numeric prelude, providing a clean structure for numbers and operations that combine them. 
poly
Fast polynomial arithmetic in Haskell (dense and sparse, univariate and multivariate, usual and Laurent) 
safedecimal
Safe and very efficient arithmetic operations on fixed decimal point numbers 
eigen
Haskel binding for Eigen library. Eigen is a C++ template library for linear algebra: matrices, vectors, numerical solvers, and related algorithms. 
diagramssolve
Miscellaneous solver code for diagrams (lowdegree polynomials, tridiagonal matrices) 
vectorbinaryinstances
Instances for the Haskell Binary class, for the types defined in the popular vector package.
* Code Quality Rankings and insights are calculated and provided by Lumnify.
They vary from L1 to L5 with "L5" being the highest.
Do you think we are missing an alternative of mod or a related project?
README
mod
Modular arithmetic, promoting moduli to the type level, with an emphasis on performance. Originally a part of arithmoi package.
> :set XDataKinds
> 4 + 5 :: Mod 7
(2 `modulo` 7)
> 4  5 :: Mod 7
(6 `modulo` 7)
> 4 * 5 :: Mod 7
(6 `modulo` 7)
> 4 / 5 :: Mod 7
(5 `modulo` 7)
> 4 ^ 5 :: Mod 7
(2 `modulo` 7)
Competitors
There are other Haskell packages, employing the very same idea of moduli on the type level,
namely modular
, modulararithmetic
and finitefield
. One can also use finitetypelits
,
which covers some elementary modular arithmetic as well.
Unfortunately, all of them fall behind
in terms of performance. Here is a brief comparison:
Discipline  mod 
modular 
modulararithmetic 
finitetypelits 
finitefield 

Addition  Fast  Slow  Slow  Slow  Slow 
Small (*) 
Fast  Slow  Slow  Slow  Slow 
Inversion  Fast  N/A  Slow  N/A  Slow 
Power  Fast  Slow  Slow  Slow  Slow 
Overflows  Safe  Safe  Unsafe  Safe  Safe 
Addition. All competing implementations of the modular addition involve divisions, while
mod
completely avoids this costly operation. It makes difference even for small numbers; e. g.,sum [1..10^7]
becomes 5x faster. For larger integers the speed up is even more significant, because the computational complexity of division is not linear.Small
(*)
. When a modulo fits a machine word (which is quite a common case on 64bit architectures),mod
implements the modular multiplication as a couple of CPU instructions and neither allocates intermediate arbitraryprecision values, nor callslibgmp
at all. For computations likeproduct [1..10^7]
this gives a 3x boost to performance in comparison to other libraries.Inversion. This package relies on
libgmp
for modular inversions. Even for small arguments it is about 5x faster than the native implementation of modular inversion inmodulararithmetic
.Power. This package relies on
libgmp
for modular exponentiation. Even for small arguments it is about 2x faster than competitors.Overflows. At first glance
modulararithmetic
is more flexible thanmod
, because it allows to specify the underlying representation of a modular residue, e. g.,Mod Integer 100
,Mod Int 100
,Mod Word8 100
. We argue that this is a dangerous freedom, vulnerable to overflows. For instance,20 ^ 2 :: Mod Word8 100
returns44
instead of expected0
. Even less expected is that50 :: Mod Word8 300
appears to be6
(remember that typelevel numbers are alwaysNatural
).
What is the difference between mod
and finitetypelits
?
mod
is specifically designed to represent modular residues
for mathematical applications (wrappingaround finite numbers) and
provides modular inversion and exponentiation.
The main focus of finitetypelits
is on nonwrappingaround finite numbers,
like indices of arrays in vectorsized
.
It features a Num
instance only for the sake of overloading numeric literals.
There is no lawful way to define Num
except modular arithmetic,
but from finitetypelits
viewpoint this is a byproduct.
Citius, altius, fortius!
If you are looking for an ultimate performance
and your moduli fit into Word
,
try Data.Mod.Word
,
which is a dropin replacement of Data.Mod
,
but offers almost twice faster addition and multiplication, and much less allocations.
Benchmarks
Here are some relative benchmarks (less is better),
which can be reproduced by running cabal bench
.
Discipline  Data.Mod.Word 
Data.Mod 
modular 
modulararithmetic 
finitetypelits 
finitefield 

Sum  0.4x  1x  4.5x  6.1x  3.3x  5.0x 
Product  0.6x  1x  3.6x  5.4x  3.1x  4.5x 
Inversion  0.8x  1x  N/A  6.1x  N/A  4.1x 
Power  0.9x  1x  6.0x  1.8x  1.9x  2.1x 
What's next?
This package was cut out of arithmoi
to provide a modular arithmetic
with a light dependency footprint. This goal certainly limits the scope of API
to the bare minimum. If you need more advanced tools
(the Chinese remainder theorem, cyclic groups, modular equations, etc.)
please refer to Math.NumberTheory.Moduli.