Zulip Chat Archive

Stream: mathlib4

Topic: Gray code


Daniel Weber (May 07 2024 at 06:19):

I have defined reflected binary gray code and proved some theorems about it in Lean. Is this something which could be added to Mathlib? Where?

I have also proved a few simple lemmas about bitwise operators - would it be OK to add them as simp lemmas? There is also a problem that simp uses Nat.bit_false and Nat.bit_true, which change stuff to bit0 and bit1, but they are deprecated. What's going on with that?

  • bit_testBit_zero: (Nat.bit b n).testBit 0 = b
  • bit_div2: (Nat.bit b n).div2 = n
  • succ_bit_true: Nat.bit true n + 1 = Nat.bit false (n+1)
  • succ_bit_false: Nat.bit false n + 1 = Nat.bit true n
  • succ_testBit_zero: (n + 1).testBit 0 = !n.testBit 0
  • bit_testBit_nonzero (h : i ≠ 0): (Nat.bit b n).testBit i = n.testBit (i-1)
  • div2_testBit: n.div2.testBit i = n.testBit (i+1)
  • xor_div2: (n ^^^ m).div2 = n.div2 ^^^ m.div2

Kim Morrison (May 07 2024 at 06:24):

Yes, it would be good to remove @[simp] from bit_false and bit_true.

Kim Morrison (May 07 2024 at 06:25):

I'm skeptical that bit_testBit_nonzero is useful (introduces a subtraction), but I guess the others are probably okay.

Yaël Dillies (May 07 2024 at 08:10):

Probably bit_testBit_succ : (Nat.bit b n).testBit (i + 1) = n.testBit i is better?

Daniel Weber (May 07 2024 at 09:07):

Yaël Dillies said:

Probably bit_testBit_succ : (Nat.bit b n).testBit (i + 1) = n.testBit i is better?

Yeah, seems so. Then I can replace by_cases i = 0 in my proof with cases i and everything is nicer

Daniel Weber (May 07 2024 at 09:12):

Ah, it exists already, docs#Nat.testBit_bit_succ

Daniel Weber (May 07 2024 at 09:12):

as does docs#Nat.testBit_bit_zero

Daniel Weber (May 07 2024 at 09:14):

and docs#Nat.div2_bit

Daniel Weber (May 07 2024 at 09:17):

It's a bit annoying that everything Init.Data.Nat.Bitwise.Lemmas is spelled with n / 2 while in Mathlib.Data.Nat.Bits everything is spelled with div2, but they aren't defeq

Yuyang Zhao (May 07 2024 at 13:57):

lean4#3756 and #12419 will make them defeq and provide some lemmas.

Daniel Weber (May 07 2024 at 16:47):

What would be a good location for this file? Mathlib.Data.Nat.GrayCode?

Eric Wieser (May 07 2024 at 16:47):

I'd argue that gray codes should be about BitVector not Nat

Daniel Weber (May 07 2024 at 16:49):

I first state everything as a permutation of Nat and then look at prefixes of it for BitVec

Daniel Weber (May 07 2024 at 17:12):

It slightly annoys me that it depends on choice even though there's no reason for it to. Any idea why Lean.Omega.normalize_sat depends on Classical.em? It seems to be due to the split, but its condition should be decidable

Yaël Dillies (May 07 2024 at 17:17):

... you opened a big can of worms right there

Yaël Dillies (May 07 2024 at 17:17):

lean4#2414

Ruben Van de Velde (May 07 2024 at 17:18):

If you want people to keep paying attention to your thread, I recommend not going much further in that direction :)

Kim Morrison (May 07 2024 at 23:04):

@Yuyang Zhao, there is an old request for motivation for lean4#3756 that hasn't been answered.

Daniel Weber (May 08 2024 at 04:15):

#12750 made a pull request

Kim Morrison (May 08 2024 at 04:21):

Could you move the non-gray code material into a separate PR? It will make reviewing easier.

Daniel Weber (May 08 2024 at 04:37):

Sorry, I don't have much experience with git–how do I do that?

Kim Morrison (May 08 2024 at 04:48):

To keep it simple, you can just:

git checkout master
git checkout -b pre_gray_code
git checkout CommandMaster_GrayCode <filenames containing the earlier material>
git commit -am "...."
git push

and make a new PR from that.

Daniel Weber (May 08 2024 at 06:25):

:thumbs_up: #12751

Daniel Weber (May 13 2024 at 16:35):

Daniel Weber said:

:thumbs_up: #12751

Is anyone available to take a look at this?

Yaël Dillies (May 13 2024 at 16:59):

In a month, after my exams... :grimacing:

Rida Hamadani (May 16 2024 at 06:16):

Daniel Weber said:

:thumbs_up: #12751

LGTM

Rida Hamadani (May 16 2024 at 06:17):

I wonder if size_eq_iff_testBit can be simplified more though ...


Last updated: May 02 2025 at 03:31 UTC