Skip to content

feat(InformationTheory): linear codes over finite fields and minimum distance properties#38014

Open
cduenasnavarro wants to merge 11 commits intoleanprover-community:masterfrom
cduenasnavarro:my-feature-branch
Open

feat(InformationTheory): linear codes over finite fields and minimum distance properties#38014
cduenasnavarro wants to merge 11 commits intoleanprover-community:masterfrom
cduenasnavarro:my-feature-branch

Conversation

@cduenasnavarro
Copy link
Copy Markdown

@cduenasnavarro cduenasnavarro commented Apr 13, 2026

Define linear codes over a finite field F as finite-dimensional subspaces of Fin n → F,
together with their minimum Hamming distance.

Main definitions:

  • LinearCode
  • minDist
  • LinearCodeWithDist
  • hammingSphere

Main results:

  • minDist_eq_sInf_pairwiseDist: characterisation of the minimum distance via pairwise distances
  • disjoint_spheres: Hamming spheres of radius t around distinct codewords are disjoint
    if 2 * t < d

Pending:

  • Choosing an adequate book reference

Open in Gitpod

@github-actions github-actions bot added the new-contributor This PR was made by a contributor with at most 5 merged PRs. Welcome to the community! label Apr 13, 2026
@github-actions
Copy link
Copy Markdown

Welcome new contributor!

Thank you for contributing to Mathlib! If you haven't done so already, please review our contribution guidelines, as well as the style guide and naming conventions. In particular, we kindly remind contributors that we have guidelines regarding the use of AI when making pull requests.

We use a review queue to manage reviews. If your PR does not appear there, it is probably because it is not successfully building (i.e., it doesn't have a green checkmark), has the awaiting-author tag, or another reason described in the Lifecycle of a PR. The review dashboard has a dedicated webpage which shows whether your PR is on the review queue, and (if not), why.

If you haven't already done so, please come to https://leanprover.zulipchat.com/, introduce yourself, and mention your new PR.

Thank you again for joining our community.

@github-actions
Copy link
Copy Markdown

github-actions bot commented Apr 13, 2026

PR summary 2a556ee342

Import changes for modified files

No significant changes to the import graph

Import changes for all files
Files Import difference
Mathlib.InformationTheory.Coding.LinearCode (new file) 1532

Declarations diff

+ LinearCode
+ LinearCodeWithDist
+ Word
+ disjoint_spheres
+ exists_pair_hammingDist_eq_minDist
+ hammingSphere
+ minDist
+ minDist_eq_sInf_pairwiseDist
+ minDist_le_hammingDist

You can run this locally as follows
## summary with just the declaration names:
./scripts/pr_summary/declarations_diff.sh <optional_commit>

## more verbose report:
./scripts/pr_summary/declarations_diff.sh long <optional_commit>

The doc-module for scripts/pr_summary/declarations_diff.sh contains some details about this script.


No changes to technical debt.

You can run this locally as

./scripts/reporting/technical-debt-metrics.sh pr_summary
  • The relative value is the weighted sum of the differences with weight given by the inverse of the current value of the statistic.
  • The absolute value is the relative value divided by the total sum of the inverses of the current values (i.e. the weighted average of the differences).

@github-actions github-actions bot added the t-measure-probability Measure theory / Probability theory label Apr 13, 2026
@github-actions
Copy link
Copy Markdown

github-actions bot commented Apr 13, 2026

✅ PR Title Formatted Correctly

The title of this PR has been updated to match our commit style conventions.
Thank you!

@cduenasnavarro cduenasnavarro changed the title Creation of InformationTheory/Coding/LinearCode.lean feat(InformationTheory): linear codes over finite fields and minimum distance properties Apr 13, 2026
Copy link
Copy Markdown
Collaborator

@vihdzp vihdzp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursory remarks, not all too familiar with the maths.


namespace InformationTheory

variable (F : Type*) [Field F] [Fintype F] [DecidableEq F]
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why Fintype instead of Finite? And why DecidableEq?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi! Thank you for reviewing. Feel free to correct me if I'm missing anything; I'm quite new to lean.

  1. I used Fintype because it seems to be what is used for finite fields:
    https://leanprover-community.github.io/mathlib4_docs/Mathlib/FieldTheory/Finite/Basic.html#FiniteField
    Also, Mathlib's Fintype.finite states that Fintypes are Finite:
theorem Fintype.finite{α : Type u_4} (_inst : Fintype α) :
Finite α
  1. I added DecidableEq because minDist wouldn't compile otherwise due to the requirements of hammingNorm, showing this error:
    failed to synthesize instance of type class Fin n → DecidableEq F

/-- An **$[n, k]_q$-linear code** is a $k$-dimensional subspace of $\mathbb{F}_q^n$. -/
structure LinearCode (n k : ℕ) where
/-- The underlying $k$-dimensional subspace of $\mathbb{F}_q^n$, encoded as `Fin n → F`. -/
space : Subspace F (Fin n → F)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe we might generally use carrier for something like this?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This reminds me of Module.Grassmannian, which is defined by restricting the rank on the quotient, but there is a TODO to recover the alternative definition that restricts the rank on the space itself. Perhaps this is too far but I would like to see a unified definition of "collection of submodules of the same rank"

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vihdzp Changing space to carrier
@wwylele Thanks for reviewing! Are there any other changes I should make then for consistency, apart from carrier?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have concrete suggestion as the unification with Module.Grassmannian might be controversial and beyond the scope. I think it is fine to just change it to carrier for now

/-- The **minimum distance** $d(C)$ of a code $C$ is the infimum of the Hamming weights of its
non-zero elements. -/
noncomputable def minDist {n k : ℕ} (C : LinearCode F n k) : ℕ :=
sInf {w | ∃ x : Fin n → F, x ∈ C.space ∧ x ≠ 0 ∧ w = hammingNorm x}
Copy link
Copy Markdown
Collaborator

@vihdzp vihdzp Apr 13, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
sInf {w | ∃ x : Fin n → F, x ∈ C.space ∧ x ≠ 0 ∧ w = hammingNorm x}
⨅ {x : Fin n → F // x ∈ C.space ∧ x ≠ 0}, hammingNorm x.1

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The right syntax would be ⨅ x : {x : Fin n → F // x ∈ C.space ∧ x ≠ 0}, hammingNorm x.1, right? But when I change it to that, I can't use ext in the lemma.


/-- An **$[n, k, d]$-linear code** is an $[n, k]$-linear code with minimum distance at least
`d`. -/
structure LinearCodeWithDist (n k d : ℕ) where
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it make sense to extend LinearCode F n k?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I've managed to make that work. It does look cleaner!

omit [Fintype F] in
/-- The minimum distance of a code coincides with the infimum of pairwise Hamming distances
between distinct codewords. -/
lemma minDist_eq_sInf_pairwiseDist {n k : ℕ} (C : LinearCode F n k) :
Copy link
Copy Markdown
Collaborator

@vihdzp vihdzp Apr 13, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of a sInf theorem, I think it might be more reasonable to split this into two: a theorem minDist F C ≤ hammingDist x y, and a theorem ∃ x ∈ C.space, ∃ y ∈ C.space, x ≠ y ∧ hammingDist x y = minDist F C.

Copy link
Copy Markdown
Author

@cduenasnavarro cduenasnavarro Apr 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this theorem is useful for comparing the pairwise minimum (it's a minimum since the set is finite) and minDist. Anyhow, I'm adding these other two results, in case they are needed.

rw [hammingDist_eq_hammingNorm x y]
exact hammingDist_zero_right (-x + y)

/-- The **Hamming sphere** $S_t(c)$ is the set of all vectors in $\mathbb{F}_q^n$ at Hamming
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems suggestive of some kind of metric space... would it make sense to define a type alias for Fin n → F with the Hamming distance?

Copy link
Copy Markdown
Author

@cduenasnavarro cduenasnavarro Apr 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So would adding this

/-- A **word** of length `n` over the field `F`: an arbitrary vector in $\mathbb{F}_q^n$,
equipped with the Hamming distance. -/
abbrev Word (n : ℕ) : Type _ := Hamming (fun _ : Fin n ↦ F)

for future usage be enough?

Copy link
Copy Markdown
Collaborator

@wwylele wwylele Apr 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should just be Hamming, right?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you need to specify the vector space, just Hamming doesn't compile, if that's what you mean

@rkirov
Copy link
Copy Markdown
Contributor

rkirov commented Apr 14, 2026

Happy to provide second opinion on the theory of error-correcting codes if needed (also feel free to ignore me as I am not a mathlib contributor).

In terms of the theory these are the right first steps. Only questions to consider:

I assume you are working up to hamming bound, which is also the first result in the theory usually.

@rkirov
Copy link
Copy Markdown
Contributor

rkirov commented Apr 14, 2026

oh also, c₁ ∈ C.code.space could be c₁ ∈ C.code or even c₁ ∈ C if you add the right typeclass instances, leaving to the mathlib experts to decide what is more ideomatic, but it would read better.


## Main definitions

* `LinearCode`: An $[n, k]_q$-linear code, i.e., a $k$-dimensional subspace of $\mathbb{F}_q^n$.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* `LinearCode`: An $[n, k]_q$-linear code, i.e., a $k$-dimensional subspace of $\mathbb{F}_q^n$.
* `LinearCode`: An $[n, k]_q$-linear code, i.e., a $k$-dimensional subspace of $\mathbb{F}_{q^n}$.

Copy link
Copy Markdown
Author

@cduenasnavarro cduenasnavarro Apr 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've always seen it written as (F_q)^n, it's the vector space of n elements, with the elements belonging to F_q

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh sorry, I misread this part. I think you are right. Please ignore this

namespace InformationTheory

variable (F : Type*) [Field F] [Fintype F] [DecidableEq F]
variable {q : ℕ} (hq : Fintype.card F = q)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't look like q and hq are used

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They are not used (for now); I added it in case it is needed in the future. In the notation I've seen, it's a explicit parameter in the definition of linear code, so I thought I would explicitly define it

/-- An **$[n, k]_q$-linear code** is a $k$-dimensional subspace of $\mathbb{F}_q^n$. -/
structure LinearCode (n k : ℕ) where
/-- The underlying $k$-dimensional subspace of $\mathbb{F}_q^n$, encoded as `Fin n → F`. -/
space : Subspace F (Fin n → F)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This reminds me of Module.Grassmannian, which is defined by restricting the rank on the quotient, but there is a TODO to recover the alternative definition that restricts the rank on the space itself. Perhaps this is too far but I would like to see a unified definition of "collection of submodules of the same rank"

@cduenasnavarro
Copy link
Copy Markdown
Author

@rkirov

Happy to provide second opinion on the theory of error-correcting codes if needed (also feel free to ignore me as I am not a mathlib contributor).

In terms of the theory these are the right first steps. Only questions to consider:

I assume you are working up to hamming bound, which is also the first result in the theory usually.

  1. We should probably at least define them, but I personally have not studied general codes and I don't know which results apply to the general case.
    I also don't know whether they would go in the same file. If so, the file might look a bit odd if we define general codes but define and prove most things for linear code. Although I'm not very knowledgeable of mathlib's structure.
  2. Good idea, thanks! That simplifies the theorem a bit.
  3. That's a good goal and I'll try to continue contributing to coding theory whenever I have the time (this is quite fun), but feel free to contribute too if you want :) I'll notify in the zulip thread before I start working on anything so we don't overlap.

oh also, c₁ ∈ C.code.space could be c₁ ∈ C.code or even c₁ ∈ C if you add the right typeclass instances, leaving to the mathlib experts to decide what is more ideomatic, but it would read better.

It now looks a bit better (C.carrier) thanks to extension as @vihdzp suggested

Copy link
Copy Markdown
Author

@cduenasnavarro cduenasnavarro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly changes from the comments I've received so far

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

new-contributor This PR was made by a contributor with at most 5 merged PRs. Welcome to the community! t-measure-probability Measure theory / Probability theory

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants