The Mathlib Handbook

The volumes of the handbook are not written yet. This site shows the map of Mathlib that they will follow, at Mathlib 4472534b60.

The areas / V02. Numbers

Mathlib.Data.ZMod.Defs

17 declarations. Mathlib docs · Source

Definition of ZMod n + basic results.

This file provides the basic details of ZMod n, including its commutative ring structure.

Implementation notes

This used to be inlined into Data.ZMod.Basic. This file imports CharP.Lemmas, which is an issue; all CharP instances create an Algebra (ZMod p) R instance; however, this instance may not be definitionally equal to other Algebra instances (for example, GaloisField also has an Algebra instance as it is defined as a SplittingField). The way to fix this is to use the forgetful inheritance pattern, and make CharP carry the data of what the smul should be (so for example, the smul on the GaloisField CharP instance should be equal to the smul from its SplittingField structure); there is only one possible ZMod p algebra for any p, so this is not an issue mathematically. For this to be possible, however, we need CharP.Lemmas to be able to import some part of ZMod.

Ring structure on Fin n

We define a commutative ring structure on Fin n. Afterwards, when we define ZMod n in terms of Fin n, we use these definitions to register the ring structure on ZMod n as type class instance.

theorem Fin.val_intCast E0 #

∀ {n : ℕ} [inst : NeZero n] (x : ℤ), ↑↑x = (x % ↑n).toNat

instance Fin.instCommSemigroup E0 #

(n : ℕ) → CommSemigroup (Fin n)

Multiplicative commutative semigroup structure on Fin n.

instance Fin.instDistrib E0 #

(n : ℕ) → Distrib (Fin n)

Distributive structure on Fin n.

instance Fin.instNonUnitalCommRing E0 #

(n : ℕ) → [NeZero n] → NonUnitalCommRing (Fin n)

instance Fin.instCommMonoid E0 #

(n : ℕ) → [NeZero n] → CommMonoid (Fin n)

instance Fin.instHasDistribNeg E0 #

(n : ℕ) → HasDistribNeg (Fin n)

Note this is more general than Fin.instCommRing as it applies (vacuously) to Fin 0 too.

def Fin.instCommRing E1 #

(n : ℕ) → [NeZero n] → CommRing (Fin n)

Commutative ring structure on Fin n.

This is not a global instance, but can introduced locally using open Fin.CommRing in ....

This is not an instance because the binop% elaborator assumes that there are no non-trivial coercion loops, but this instance would introduce a coercion from Nat to Fin n and back. Non-trivial loops lead to undesirable and counterintuitive elaboration behavior.

For example, for x : Fin k and n : Nat, it causes x < n to be elaborated as x < ↑n rather than ↑x < n, silently introducing wraparound arithmetic.

instance Fin.instNeZeroHAddNatOfNat_mathlib_1 E0 #

∀ (n : ℕ) [NeZero n], NeZero 1

def ZMod E3 #

ℕ → Type

The integers modulo n : ℕ.

instance ZMod.decidableEq E0 #

(n : ℕ) → DecidableEq (ZMod n)

instance ZMod.repr E0 #

(n : ℕ) → Repr (ZMod n)

instance ZMod.instUnique E0 #

Unique (ZMod 1)

instance ZMod.fintype E0 #

(n : ℕ) → [NeZero n] → Fintype (ZMod n)

instance ZMod.infinite E0 #

Infinite (ZMod 0)

theorem ZMod.card E0 #

∀ (n : ℕ) [inst : Fintype (ZMod n)], Fintype.card (ZMod n) = n

instance ZMod.commRing E0 #

(n : ℕ) → CommRing (ZMod n)

instance ZMod.inhabited E0 #

(n : ℕ) → Inhabited (ZMod n)