Lean / Reader
Lean reader

PaletteCore.lean

140 lines23 declarationsno sorryimports Mathlib.Tactic
  1. import Mathlib.Tactic
  2. namespace ParityPalettes
  3. def differentThree (a b c d : Nat) : Bool :=
  4. !((a == b && (c == d || a == c || a == d)) ||
  5. (a == c && (b == d || a == d)) || (a == d && b == c) || (b == c && b == d))
  6. def digit (w pos : Nat) : Nat := w / 4^pos % 4
  7. def endOK (w length : Nat) : Bool :=
  8. (List.range ((length-1)/3)).all fun i =>
  9. let d := i+1
  10. differentThree (digit w 0) (digit w d) (digit w (2*d)) (digit w (3*d))
  11. def legal : Nat → Nat → Bool
  12. | 0, _ => true
  13. | n+1, w => endOK w (n+1) && legal n (w/4)
  14. def seed : List Nat := [6, 18, 22, 24, 25, 26, 27]
  15. def extend (n : Nat) (ws : List Nat) : List Nat :=
  16. ws.flatMap fun w => ([0,1,2,3].map fun c => 4*w+c).filter fun z => endOK z n
  17. def layer : Nat → List Nat
  18. | 0 => seed
  19. | k+1 => extend (k+5) (layer k)
  20. theorem cover (k w : Nat) (hstart : w/4^k ∈ seed) (h : legal (k+4) w = true) :
  21. w ∈ layer k := by
  22. induction k generalizing w with
  23. | zero => simpa [layer] using hstart
  24. | succ k ih =>
  25. have hs : (w/4)/4^k ∈ seed := by
  26. simpa [Nat.div_div_eq_div_mul, pow_succ, Nat.mul_comm] using hstart
  27. change (endOK w (k+5) && legal (k+4) (w/4)) = true at h
  28. have both := Bool.and_eq_true_iff.mp h
  29. apply List.mem_flatMap.mpr
  30. refine ⟨w/4, ih (w/4) hs both.2, List.mem_filter.mpr ⟨?_, both.1⟩⟩
  31. apply List.mem_map.mpr
  32. refine ⟨w%4, ?_, by omega⟩
  33. have hm := Nat.mod_lt w (by decide : 0<4)
  34. simp
  35. omega
  36. theorem digit_succ (w i : Nat) : digit w (i+1) = digit (w/4) i := by
  37. simp [digit, pow_succ, Nat.div_div_eq_div_mul, Nat.mul_comm]
  38. def rename (p : Fin 4 → Fin 4) : Nat → Nat → Nat
  39. | 0, _ => 0
  40. | n+1, w => 4 * rename p n (w/4) + (p ⟨w%4, Nat.mod_lt _ (by decide)⟩).val
  41. def permDigit (p : Fin 4 → Fin 4) (x : Nat) : Nat := (p ⟨x%4, Nat.mod_lt _ (by decide)⟩).val
  42. theorem rename_shift (p : Fin 4 → Fin 4) (n w : Nat) :
  43. rename p (n+1) w / 4 = rename p n (w/4) := by
  44. simp only [rename]
  45. have h := (p ⟨w%4, Nat.mod_lt _ (by decide)⟩).isLt
  46. omega
  47. theorem rename_digit (p : Fin 4 → Fin 4) (n w i : Nat) (hi : i<n) :
  48. digit (rename p n w) i = permDigit p (digit w i) := by
  49. induction n generalizing w i with
  50. | zero => omega
  51. | succ n ih =>
  52. cases i with
  53. | zero =>
  54. simp [rename, digit, permDigit, Nat.add_mod, Nat.mod_eq_of_lt (p _).isLt]
  55. | succ i =>
  56. rw [digit_succ, rename_shift, digit_succ w, ih (w/4) i (by omega)]
  57. theorem perm_eq (p : Fin 4 → Fin 4) (hp : Function.Injective p) (a b : Nat)
  58. (ha : a<4) (hb : b<4) : (permDigit p a == permDigit p b) = (a==b) := by
  59. apply Bool.eq_iff_iff.mpr
  60. simp [permDigit, Nat.mod_eq_of_lt ha, Nat.mod_eq_of_lt hb, hp.eq_iff, Fin.val_inj]
  61. theorem digit_lt (w i : Nat) : digit w i < 4 := Nat.mod_lt _ (by decide)
  62. theorem rename_legal (p : Fin 4 → Fin 4) (hp : Function.Injective p) (n w : Nat) :
  63. legal n (rename p n w) = legal n w := by
  64. induction n generalizing w with
  65. | zero => rfl
  66. | succ n ih =>
  67. simp only [legal, rename_shift, ih]
  68. congr 1
  69. apply Bool.eq_iff_iff.mpr
  70. simp only [endOK, List.all_eq_true, List.mem_range]
  71. constructor <;> intro H j hj
  72. all_goals
  73. have h := H j hj
  74. have h0 : 0<n+1 := by omega
  75. have h1 : j+1<n+1 := by omega
  76. have h2 : 2*(j+1)<n+1 := by omega
  77. have h3 : 3*(j+1)<n+1 := by omega
  78. simpa only [rename_digit _ _ _ _ h0, rename_digit _ _ _ _ h1,
  79. rename_digit _ _ _ _ h2, rename_digit _ _ _ _ h3, differentThree,
  80. perm_eq p hp _ _ (digit_lt _ _) (digit_lt _ _)] using h
  81. theorem rename_prefix (p : Fin 4 → Fin 4) (k w : Nat) :
  82. rename p (k+4) w / 4^k = rename p 4 (w / 4^k) := by
  83. induction k generalizing w with
  84. | zero => simp
  85. | succ k ih =>
  86. rw [pow_succ', ← Nat.div_div_eq_div_mul, ← Nat.div_div_eq_div_mul]
  87. rw [show k+1+4 = (k+4)+1 by omega, rename_shift, ih]
  88. def permCode (code : Nat) (c : Fin 4) : Fin 4 :=
  89. ⟨digit code c.val, digit_lt _ _⟩
  90. def permutationCodes : List Nat :=
  91. [27,30,39,45,54,57,75,78,99,108,114,120,135,141,147,156,177,180,198,201,210,216,225,228]
  92. def chosenPerm (i : Fin 24) : Fin 4 → Fin 4 := permCode (permutationCodes[i.val]!)
  93. set_option maxRecDepth 100000 in
  94. set_option maxHeartbeats 0 in
  95. theorem small_normalization : ∀ w : Fin 256, legal 4 w.val = true →
  96. ∃ i : Fin 24, Function.Injective (chosenPerm i) ∧ rename (chosenPerm i) 4 w.val ∈ seed := by
  97. decide +kernel
  98. theorem legal_prefix (k w : Nat) (h : legal (k+4) w = true) :
  99. legal 4 (w / 4^k) = true := by
  100. induction k generalizing w with
  101. | zero => simpa using h
  102. | succ k ih =>
  103. rw [pow_succ', ← Nat.div_div_eq_div_mul]
  104. exact ih (w/4) (Bool.and_eq_true_iff.mp h).2
  105. theorem normalize (k w : Nat) (h : legal (k+4) w = true) (width : w < 4^(k+4)) :
  106. ∃ p : Fin 4 → Fin 4, Function.Injective p ∧ rename p (k+4) w ∈ layer k := by
  107. have hw : w / 4^k < 256 := by
  108. apply (Nat.div_lt_iff_lt_mul (by positivity : 0<4^k)).mpr
  109. simpa [pow_add, Nat.mul_comm] using width
  110. obtain ⟨c, hi, hs⟩ := small_normalization ⟨_, hw⟩ (legal_prefix k w h)
  111. refine ⟨chosenPerm c, hi, cover k _ ?_ ?_⟩
  112. · rwa [rename_prefix]
  113. · rwa [rename_legal _ hi]
  114. end ParityPalettes