Lean / Reader
Lean reader

PaletteCross.lean

70 lines10 declarationsno sorryimports PaletteCore

Lean 4 Web has Mathlib, so it can check files that import only Mathlib. This one also imports PaletteCore, which it does not have: you can read it there, but it will not check. Build the project itself to check it.

  1. import PaletteCore
  2. namespace ParityPalettes
  3. abbrev CrossRow := Nat × Nat
  4. def crossRows (n : Nat) : List CrossRow :=
  5. (List.range n).flatMap fun a =>
  6. ((List.range ((n-1-a)/3)).map (·+1)).filterMap fun d =>
  7. if d%2=1 then some (a,d) else none
  8. theorem crossBounds {r : CrossRow} {n : Nat} (h : r ∈ crossRows n) :
  9. 0<r.2 ∧ r.2%2=1 ∧ r.1+3*r.2<n := by
  10. obtain ⟨a, ha, hr⟩ := List.mem_flatMap.mp h
  11. obtain ⟨d,hd,hs⟩ := List.mem_filterMap.mp hr
  12. obtain ⟨k,hk,hkd⟩ := List.mem_map.mp hd
  13. simp only [List.mem_range] at ha hk
  14. subst d
  15. by_cases odd : (k+1)%2=1
  16. · simp [odd] at hs
  17. subst r
  18. exact ⟨by omega,odd,by simp; omega⟩
  19. · simp [odd] at hs
  20. def parPos (r : CrossRow) (side : Bool) : Nat :=
  21. if r.1%2 = side.toNat then r.1 else r.1+r.2
  22. def endDigit (length word index : Nat) : Nat := digit word (length-1-index)
  23. def sameAt (n w : Nat) (side : Bool) (r : CrossRow) : Bool :=
  24. let length := (n+1-side.toNat)/2
  25. let a := parPos r side / 2
  26. endDigit length w a == endDigit length w (a+r.2)
  27. def sig {α : Type} (flag : α → Bool) : List α → Nat
  28. | [] => 0
  29. | x::xs => Nat.bit (flag x) (sig flag xs)
  30. theorem bit_zero (b : Bool) (n : Nat) : (Nat.bit b n).testBit 0 = b := by
  31. cases b <;> simp [Nat.bit_val, Nat.testBit_zero, Nat.add_mod]
  32. theorem and_zero_of_avoids {α : Type} (f g : α → Bool) (xs : List α)
  33. (h : ∀ x ∈ xs, f x = true → g x = false) : sig f xs &&& sig g xs = 0 := by
  34. induction xs with
  35. | nil => simp [sig]
  36. | cons x xs ih =>
  37. have tail := ih (fun y hy => h y (by simp [hy]))
  38. apply Nat.eq_of_testBit_eq
  39. intro i
  40. cases i with
  41. | zero =>
  42. simp only [sig,Nat.testBit_and,bit_zero]
  43. cases hfx : f x with
  44. | false => simp
  45. | true => simp [h x (by simp) hfx]
  46. | succ i =>
  47. simpa only [sig,Nat.testBit_and,Nat.testBit_bit_succ,Nat.zero_testBit] using
  48. congrArg (fun t : Nat => t.testBit i) tail
  49. theorem same_rename (p : Fin 4 → Fin 4) (hp : Function.Injective p)
  50. (n w : Nat) (side : Bool) (r : CrossRow) (hr : r ∈ crossRows n) :
  51. sameAt n (rename p ((n+1-side.toNat)/2) w) side r = sameAt n w side r := by
  52. obtain ⟨_,ho,hb⟩ := crossBounds hr
  53. have hidx : (parPos r side)/2+r.2 < (n+1-side.toNat)/2 := by
  54. cases side <;> simp [parPos] <;> split <;> omega
  55. simp only [sameAt,endDigit]
  56. rw [rename_digit,rename_digit,perm_eq _ hp _ _ (digit_lt _ _) (digit_lt _ _)] <;> omega
  57. end ParityPalettes