mod: Fast type-safe modular arithmetic

[ library, math, mit, number-theory ] [ Propose Tags ]

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


[Skip to Readme]

Flags

Automatic Flags
NameDescriptionDefault
semirings

Derive semiring instances

Enabled

Use -f <flag> to enable a flag, or -f -<flag> to disable that flag. More info

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

  • No Candidates
Versions [RSS] 0.0.0.0, 0.1.0.0, 0.1.1.0, 0.1.2.0, 0.1.2.1, 0.1.2.2, 0.2.0.0, 0.2.0.1
Change log changelog.md
Dependencies base (>=4.10 && <5), deepseq, integer-gmp (<1.1), semirings (>=0.5) [details]
License MIT
Copyright 2019 Andrew Lelechenko
Author Andrew Lelechenko <andrew.lelechenko@gmail.com>
Maintainer Andrew Lelechenko <andrew.lelechenko@gmail.com>
Category Math, Number Theory
Home page https://github.com/Bodigrim/mod
Bug tracker https://github.com/Bodigrim/mod/issues
Source repo head: git clone https://github.com/Bodigrim/mod
Uploaded by Bodigrim at 2020-01-28T19:07:37Z
Distributions Arch:0.2.0.1, LTSHaskell:0.2.0.1, NixOS:0.2.0.1, Stackage:0.2.0.1
Reverse Dependencies 7 direct, 7747 indirect [details]
Downloads 7099 total (181 in the last 30 days)
Rating 2.0 (votes: 1) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs available [build log]
Last success reported on 2020-01-28 [all 1 reports]

Readme for mod-0.1.1.0

[back to package description]

mod Build Status Hackage Hackage CI Stackage LTS Stackage Nightly

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 and modular-arithmetic. Unfortunately, both of them fall behind in terms of performance. Here is a brief comparison:

Discipline mod modular modular-arithmetic
Addition Fast Slow Slow
Small (*) Fast Slow Slow
Inversion Fast N/A Slow
Power Fast Slow Slow
Overflows Safe Safe Unsafe
  • Addition. It appears that modular and modular-arithmetic 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 64-bit architectures), mod implements the modular multiplication as a couple of CPU instructions and neither allocates intermediate arbitrary-precision values, nor calls libgmp at all. For computations like product [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 in modular-arithmetic.

  • Power. This package relies on libgmp for modular exponentiation. Even for small arguments it is about 2x faster than competitors.

  • Overflows. At first glance modular-arithmetic is more flexible than mod, 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 returns 44 instead of expected 0. Even less expected is that 50 :: Mod Word8 300 appears to be 6 (remember that type-level numbers are always Natural).

Citius, altius, fortius!

If you are looking for an ultimate performance and your moduli fit into Word, try Data.Mod.Word, which is a drop-in replacement of Data.Mod, but offers 3x faster addition, 2x faster multiplication and much less allocations.

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.