Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Combinatorics/Colex): binary expansions give the colexicographic order on Finset Nat #12346

Open
wants to merge 10 commits into
base: master
Choose a base branch
from

Conversation

apnelson1
Copy link
Collaborator

@apnelson1 apnelson1 commented Apr 22, 2024

This PR fills in a todo in Combinatorics/Colex by providing the natural order isomorphism between Nat and Colex Nat that maps each n to the set of indices of ones in the binary expansion of n.

We define bitSet n to be this 'finset of bits' corresponding to n, and prove a few API lemmas for it, before defining the equivalence. The definition bitSet could be argued to fit in Nat/Bits or Nat/Digits, but both are quite far back in the import hierarchy compared to Finset, and the proof of bijectivity uses the machinery in Colex.


Open in Gitpod

@apnelson1 apnelson1 added the awaiting-review The author would like community review of the PR label Apr 22, 2024
Copy link
Collaborator

@YaelDillies YaelDillies left a comment

Choose a reason for hiding this comment

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

The definition bitSet could be argued to fit in Nat/Bits or Nat/Digits, but both are quite far back in the import hierarchy compared to Finset, and the proof of bijectivity uses the machinery in Colex.

According to #11757, they shouldn't be, so I believe you could put that material there, or at least in a separate file since it really sticks out from the rest of Combinatorics.Colex.

Mathlib/Combinatorics/Colex.lean Outdated Show resolved Hide resolved

/-- The function which maps the natural number `∑ i ∈ s, 2^i` to the Finset `s`.
This could also be defined using `Nat.bits`, but it seems easier to avoid the `List` api. -/
def Nat.bitSet (n : ℕ) : Finset ℕ := by
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
def Nat.bitSet (n : ℕ) : Finset ℕ := by
def Nat.bitsFinset (n : ℕ) : Finset ℕ := by

is more accurate maybe?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Possibly, but it isn't so nice-looking mathematically, and I think there is little danger of ambiguity. I'll let others weigh in.

Copy link
Collaborator

Choose a reason for hiding this comment

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

@apnelson1
Copy link
Collaborator Author

The definition bitSet could be argued to fit in Nat/Bits or Nat/Digits, but both are quite far back in the import hierarchy compared to Finset, and the proof of bijectivity uses the machinery in Colex.

According to #11757, they shouldn't be, so I believe you could put that material there, or at least in a separate file since it really sticks out from the rest of Combinatorics.Colex.

You're right that it sticks out. I've moved it to a separate file - importing Colex in Nat.Bits causes circular dependencies, and unwinding them before #11757 is likely wasted effort.

@YaelDillies YaelDillies added awaiting-zulip and removed awaiting-review The author would like community review of the PR labels Apr 23, 2024
induction' n using binaryRec with b _ s
· exact ∅
cases b
· exact image (· + 1) s
Copy link
Member

Choose a reason for hiding this comment

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

Can you use map here instead? It will be faster and have nicer defeqs. Same for cons instead of insert below.

Copy link
Collaborator Author

@apnelson1 apnelson1 Apr 24, 2024

Choose a reason for hiding this comment

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

edit : never mind

variable {a n : ℕ} {s : Finset ℕ}

/-- The function which maps the natural number `∑ i in s, 2^i` to the Finset `s`. -/
def bitIndices (n : ℕ) : Finset ℕ := by
Copy link
Member

Choose a reason for hiding this comment

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

Should this be a (sorted) list instead of a finset? Nothing about the name suggests that it should be a finset. See Nat.factors for a similar case of using a list instead.

Copy link
Collaborator

Choose a reason for hiding this comment

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

In the context of colex, it should be a finset. But that doesn't mean we can't construct it as a list originally.

@grunweg
Copy link
Collaborator

grunweg commented May 24, 2024

Coming here from PR triage: is this PR still blocked on a zulip decision?

Copy link
Collaborator

@YaelDillies YaelDillies left a comment

Choose a reason for hiding this comment

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

Released under Apache 2.0 license as described in the file LICENSE.
Authors: Peter Nelson
-/
import Mathlib.Combinatorics.Colex
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think Combinatorics.Colex should import Data.Nat.BitIndices, not the other way around. Can you turn the imports around?

@YaelDillies YaelDillies added awaiting-author A reviewer has asked the author a question or requested changes t-combinatorics Combinatorics and removed awaiting-zulip labels May 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting-author A reviewer has asked the author a question or requested changes t-combinatorics Combinatorics
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants