Lean / Reader
Lean readerPaletteCross.lean
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.
import PaletteCorenamespace ParityPalettesabbrev CrossRow := Nat × Natdef crossRows (n : Nat) : List CrossRow :=(List.range n).flatMap fun a =>((List.range ((n-1-a)/3)).map (·+1)).filterMap fun d =>if d%2=1 then some (a,d) else nonetheorem crossBounds {r : CrossRow} {n : Nat} (h : r ∈ crossRows n) :0<r.2 ∧ r.2%2=1 ∧ r.1+3*r.2<n := byobtain ⟨a, ha, hr⟩ := List.mem_flatMap.mp hobtain ⟨d,hd,hs⟩ := List.mem_filterMap.mp hrobtain ⟨k,hk,hkd⟩ := List.mem_map.mp hdsimp only [List.mem_range] at ha hksubst dby_cases odd : (k+1)%2=1· simp [odd] at hssubst rexact ⟨by omega,odd,by simp; omega⟩· simp [odd] at hsdef parPos (r : CrossRow) (side : Bool) : Nat :=if r.1%2 = side.toNat then r.1 else r.1+r.2def endDigit (length word index : Nat) : Nat := digit word (length-1-index)def sameAt (n w : Nat) (side : Bool) (r : CrossRow) : Bool :=let length := (n+1-side.toNat)/2let a := parPos r side / 2endDigit length w a == endDigit length w (a+r.2)def sig {α : Type} (flag : α → Bool) : List α → Nat| [] => 0| x::xs => Nat.bit (flag x) (sig flag xs)theorem bit_zero (b : Bool) (n : Nat) : (Nat.bit b n).testBit 0 = b := bycases b <;> simp [Nat.bit_val, Nat.testBit_zero, Nat.add_mod]theorem and_zero_of_avoids {α : Type} (f g : α → Bool) (xs : List α)(h : ∀ x ∈ xs, f x = true → g x = false) : sig f xs &&& sig g xs = 0 := byinduction xs with| nil => simp [sig]| cons x xs ih =>have tail := ih (fun y hy => h y (by simp [hy]))apply Nat.eq_of_testBit_eqintro icases i with| zero =>simp only [sig,Nat.testBit_and,bit_zero]cases hfx : f x with| false => simp| true => simp [h x (by simp) hfx]| succ i =>simpa only [sig,Nat.testBit_and,Nat.testBit_bit_succ,Nat.zero_testBit] usingcongrArg (fun t : Nat => t.testBit i) tailtheorem same_rename (p : Fin 4 → Fin 4) (hp : Function.Injective p)(n w : Nat) (side : Bool) (r : CrossRow) (hr : r ∈ crossRows n) :sameAt n (rename p ((n+1-side.toNat)/2) w) side r = sameAt n w side r := byobtain ⟨_,ho,hb⟩ := crossBounds hrhave hidx : (parPos r side)/2+r.2 < (n+1-side.toNat)/2 := bycases side <;> simp [parPos] <;> split <;> omegasimp only [sameAt,endDigit]rw [rename_digit,rename_digit,perm_eq _ hp _ _ (digit_lt _ _) (digit_lt _ _)] <;> omegaend ParityPalettes