IndisputableMonolith.Patterns.GrayCodeAxioms
IndisputableMonolith/Patterns/GrayCodeAxioms.lean · 155 lines · 7 declarations
show as:
view math explainer →
1import Mathlib
2import IndisputableMonolith.Patterns
3
4/-!
5# Gray Code Classical Results
6
7This module declares well-known Gray code properties as axioms pending
8full bitwise formalization.
9
10## Background
11
12The binary-reflected Gray code (BRGC) is a well-studied combinatorial object:
13- Invented by Frank Gray (1953), US Patent 2,632,058
14- Standard construction: gray(n) = n XOR (n >> 1)
15- Inverse: binary(g) = g XOR (g>>1) XOR (g>>2) XOR ...
16
17## Properties
18
19All properties declared here have:
201. **Multiple published proofs** in discrete mathematics literature
212. **Efficient algorithms** with O(log n) complexity
223. **Extensive use** in digital systems, error correction, computer graphics
234. **Numerical verification** to arbitrary bit depths
24
25## References
26
271. Savage, C. D. (1997). "A survey of combinatorial Gray codes." *SIAM Review*, 39(4):605–629.
282. Knuth, D. E. (2011). *The Art of Computer Programming, Vol 4A: Combinatorial Algorithms*. Section 7.2.1.1.
293. Gray, F. (1953). "Pulse code communication." US Patent 2,632,058.
30
31-/
32
33namespace IndisputableMonolith
34namespace Patterns
35namespace GrayCodeAxioms
36
37/-- Inverse Gray code: convert Gray code back to natural number via cumulative XOR. -/
38def grayInverse (g : ℕ) : ℕ :=
39 let rec loop (shift : ℕ) (acc : ℕ) (fuel : ℕ) : ℕ :=
40 match fuel with
41 | 0 => acc
42 | fuel' + 1 =>
43 let shifted := g >>> shift
44 if shifted = 0 then acc
45 else loop (shift + 1) (acc ^^^ shifted) fuel'
46 loop 0 0 64
47
48/-- Hypothesis envelope for Gray code classical properties. -/
49class GrayCodeFacts where
50 grayToNat_inverts_natToGray :
51 ∀ n : ℕ, n < 2^64 → grayInverse (n ^^^ (n >>> 1)) = n
52 natToGray_inverts_grayToNat :
53 ∀ g : ℕ, g < 2^64 →
54 let n := grayInverse g
55 n ^^^ (n >>> 1) = g
56 grayToNat_preserves_bound :
57 ∀ g d : ℕ, g < 2^d → d ≤ 64 → grayInverse g < 2^d
58 pattern_to_nat_bound :
59 ∀ (d : ℕ) (p : Pattern d),
60 (∑ k : Fin d, if p k then 2^(k.val) else 0) < 2^d
61 gray_code_one_bit_property :
62 ∀ (d n : ℕ), n + 1 < 2^d →
63 ∃! k : ℕ, k < d ∧
64 ((n ^^^ (n >>> 1)).testBit k ≠ ((n+1) ^^^ ((n+1) >>> 1)).testBit k)
65
66variable [GrayCodeFacts]
67
68/-- **Classical Result**: Gray code inverse is a left inverse.
69
70The inverse Gray code operation (cumulative XOR) correctly inverts the forward
71Gray code transformation.
72
73**Proof**: Induction on bit positions with XOR algebra
74
75**References**:
76- Knuth (2011), Exercise 7.2.1.1.4
77- Savage (1997), Section 2.1
78
79**Formalization Blocker**: Requires bitwise induction infrastructure for Nat
80
81**Status**: Standard result in discrete mathematics
82-/
83theorem grayToNat_inverts_natToGray :
84 ∀ n : ℕ, n < 2^64 → grayInverse (n ^^^ (n >>> 1)) = n :=
85 GrayCodeFacts.grayToNat_inverts_natToGray
86
87/-- **Classical Result**: natToGray is a left inverse of grayToNat.
88
89The forward Gray code transformation inverts the inverse operation.
90
91**Proof**: Follows from bijectivity of Gray code map
92
93**References**: Same as above
94
95**Status**: Consequence of inverse correctness
96-/
97theorem natToGray_inverts_grayToNat :
98 ∀ g : ℕ, g < 2^64 →
99 let n := grayInverse g
100 n ^^^ (n >>> 1) = g :=
101 GrayCodeFacts.natToGray_inverts_grayToNat
102
103/-- **Classical Result**: Gray code preserves bounds.
104
105If g < 2^d, then grayToNat(g) < 2^d.
106
107**Proof**: XOR operations preserve bit width
108
109**References**: Elementary bit manipulation
110
111**Status**: Simple bitwise reasoning
112-/
113theorem grayToNat_preserves_bound :
114 ∀ g d : ℕ, g < 2^d → d ≤ 64 → grayInverse g < 2^d :=
115 GrayCodeFacts.grayToNat_preserves_bound
116
117/-- **Classical Result**: Pattern to number conversion bound.
118
119Converting a d-bit pattern to a number gives a value < 2^d.
120
121**Proof**: Sum of 2^i for i < d equals 2^d - 1 < 2^d
122
123**References**: Elementary combinatorics
124
125**Status**: Straightforward calculation
126-/
127theorem pattern_to_nat_bound :
128 ∀ (d : ℕ) (p : Pattern d),
129 (∑ k : Fin d, if p k then 2^(k.val) else 0) < 2^d :=
130 GrayCodeFacts.pattern_to_nat_bound
131
132/-- **Classical Result**: Consecutive Gray codes differ in one bit.
133
134For any n < 2^d - 1, gray(n) and gray(n+1) differ in exactly one bit position.
135
136**Proof**:
137- gray(n) XOR gray(n+1) = [n XOR (n>>1)] XOR [(n+1) XOR ((n+1)>>1)]
138- This simplifies to a single power of 2 (bit at position of least significant 0 in n)
139
140**References**:
141- Savage (1997), Theorem 2.1
142- Knuth (2011), Theorem 7.2.1.1.A
143
144**Status**: Defining property of Gray codes
145-/
146theorem gray_code_one_bit_property :
147 ∀ (d n : ℕ), n + 1 < 2^d →
148 ∃! k : ℕ, k < d ∧
149 (n ^^^ (n >>> 1)).testBit k ≠ ((n+1) ^^^ ((n+1) >>> 1)).testBit k :=
150 GrayCodeFacts.gray_code_one_bit_property
151
152end GrayCodeAxioms
153end Patterns
154end IndisputableMonolith
155