pith. machine review for the scientific record. sign in
def definition def or abbrev high

grayToNat

show as:
view Lean formalization →

The grayToNat definition supplies the standard inverse map for the binary-reflected Gray code, recovering the original natural number from its Gray-encoded form via successive XOR with right shifts. Combinatorialists studying hypercube Hamiltonians cite this as the recovery step that inverts the single-bit transition encoding. The body implements the recovery as a tail-recursive accumulation of XORs bounded by 64 iterations to ensure termination for machine integers.

claimThe function $grayToNat(g)$ returns the natural number $n$ obtained by the cumulative XOR $n = g ⊕ (g ≫ 1) ⊕ (g ≫ 2) ⊕ ⋯$ until the shifted argument vanishes.

background

The binary-reflected Gray code generates a Hamiltonian cycle on the d-dimensional hypercube Q_d in which consecutive codes differ by exactly one bit and the first and last entries also differ by one bit. This module supplies the forward and inverse maps for the encoding; the inverse converts a Gray code integer back to its binary representation. The local setting is the Patterns layer, which treats Gray codes as combinatorial primitives for traversals and encodings.

proof idea

The definition introduces a recursive auxiliary function aux that begins with shift 0 and accumulator 0, then repeatedly right-shifts the input g, XORs the nonzero result into the accumulator, increments the shift, and decrements a fuel counter of 64. Termination occurs either when the shifted value is zero or fuel is exhausted.

why it matters in Recognition Science

This definition is invoked by the two inversion theorems grayToNat_inverts_natToGray and natToGray_inverts_grayToNat in GrayCodeAxioms, which establish that the maps are mutual inverses below 2^64. It supplies the concrete implementation of the Gray-code bijection used in hypercube traversals, feeding the combinatorial structures that appear in the Recognition Science treatment of patterns and self-similar encodings.

scope and limits

Lean usage

theorem gray_inverse_correct (g : ℕ) (h : g < 2^64) : grayToNat (natToGray g) = g := GrayCodeAxioms.natToGray_inverts_grayToNat g h

formal statement (Lean)

  38def grayToNat (g : ℕ) : ℕ :=

proof body

Definition body.

  39  -- Inverse Gray code: repeatedly XOR with shifted versions
  40  -- g XOR (g >> 1) XOR (g >> 2) XOR ...
  41  -- For bounded values, this terminates
  42  let rec aux (shift : ℕ) (acc : ℕ) (fuel : ℕ) : ℕ :=
  43    match fuel with
  44    | 0 => acc
  45    | fuel' + 1 =>
  46      let shifted := g >>> shift
  47      if shifted = 0 then acc
  48      else aux (shift + 1) (acc ^^^ shifted) fuel'
  49  aux 0 0 64  -- 64 shifts is enough for any practical number
  50
  51-- Properties and classical results are provided via
  52-- `IndisputableMonolith.Patterns.GrayCodeAxioms.GrayCodeFacts`.
  53-- This module remains axiom-free and parametric over those facts.
  54
  55end Patterns
  56end IndisputableMonolith

used by (2)

From the project-wide theorem graph. These declarations reference this one in their body.

depends on (8)

Lean names referenced from this declaration's body.