We are currently working on new rules for what content should and shouldn't be allowed on this website, and are looking for feedback! See Esolang:2026 topicality proposal to view and give feedback on the current draft.
Kolakoski sequence/Lean
Implementations of the Kolakoski sequence in Lean. All programs are formally verified and proved to generate the same sequence as the Lean program from the Kolakoski sequence page.
Incremental
Proof of correctness [1]. This program generates the sequence by creating longer and longer prefixes.
import Mathlib def f₁ (xs : List ℕ) : List ℕ := xs.zip (.range xs.length) |>.flatMap λ (x, n) => .replicate x $ if Even n then 1 else 2 def f₂ (xs : List ℕ) (n k : ℕ) : List ℕ := if k = 0 ∨ n ≤ xs.length then xs else f₂ (f₁ xs) n (k - 1) def f₃ (n : ℕ) : List ℕ := f₂ [1, 2] n n |>.take n def kolakoski (i : ℕ) : ℕ := (f₃ $ i + 1)[i]!
Queue-based 1
Proof of correctness [2]. This program uses a queue and passes the state recursively.
import Mathlib def kolakoski' (n : ℕ) : List ℕ := let rec go (fuel : ℕ) (seq : List ℕ) (i : ℕ) (cur : ℕ) : List ℕ := match fuel with | 0 => seq | fuel + 1 => let runLen := (seq[i]?).getD 1 go fuel (seq ++ List.replicate runLen cur) (i + 1) (3 - cur) List.take n (go n [1, 2, 2] 2 1) def kolakoski (n : ℕ) : ℕ := ((kolakoski' (n + 1))[n]?).getD 0
Queue-based 2
Proof of correctness [3]. Unlike the previous algorithm, this one does not initialize the queue with [1, 2, 2], and instead uses the current element when the queue is empty, which turns out to be enough.
import Mathlib def kolakoski' (q : List ℕ) (c : ℕ) (n : ℕ) : List ℕ := match n with | 0 => [] | n + 1 => let k := q.head?.getD c k :: kolakoski' (q ++ List.replicate k c).tail (3 - c) n def kolakoski (n : ℕ) : ℕ := kolakoski' [] 1 (n + 1) |>.getLast!
Lazy list
Proof of correctness [4]. This is a port of the following Haskell program (written by John Tromp) to Lean:
a = 1 : 2 : drop 2 (concat . zipWith replicate a . cycle $ [1, 2])
Mathlib does not have lazy lists, so we need to implement them from scratch. Here we use structure Word to denote lazy lists.
import Mathlib variable {α β γ : Type*} noncomputable def choose? (p : α → Prop) : Option α := haveI := Classical.propDecidable if h : ∃ x, p x then some h.choose else none def mkList (n : ℕ) (f : ℕ → α) : List α := List.range n |>.map f open Classical in noncomputable def Nat.find? (p : ℕ → Prop) : Option ℕ := if h : ∃ n, p n then some $ Nat.find h else none @[ext] structure Word (α : Type*) where get? : ℕ → Option α wf : ∀ i, get? i = none → ∀ n, i < n → get? n = none namespace Word def cons (x : α) (xs : Word α) : Word α where get? i := if i = 0 then x else xs.get? (i - 1) wf := by grind [xs.wf] def drop (n : ℕ) (xs : Word α) : Word α where get? i := xs.get? (i + n) wf := by grind [xs.wf] def ofGet?' (f : ℕ → Option α) (i : ℕ) : Option α := if ∀ j < i, f j ≠ none then f i else none theorem wf_ofGet?' {f : ℕ → Option α} : ∀ i, ofGet?' f i = none → ∀ (n : ℕ), i < n → ofGet?' f n = none := by simp only [ofGet?', ne_eq, ite_eq_right_iff]; grind def ofGet? (f : ℕ → Option α) : Word α where get? := ofGet?' f wf := wf_ofGet?' def take (n : ℕ) (xs : Word α) : List α := mkList n (xs.get? · |>.toList) |>.flatten def append' (f g : ℕ → Option α) : ℕ → Option α | 0 => f 0 |>.or $ g 0 | i + 1 => match f 0 with | none => g (i + 1) | some _ => append' (f $ · + 1) g i noncomputable def append (xs ys : Word α) : Word α := ofGet? $ append' xs.get? ys.get? noncomputable def ofList (xs : List α) : Word α := ofGet? λ i => xs[i]? noncomputable def nil : Word α := ofList [] noncomputable def flatten (L : Word (Word α)) : Word α := ofGet? λ i => choose? λ x => ∃ n, (L.take n |>.foldr append nil).get? i = some x noncomputable def zipWith (f : α → β → γ) (xs : Word α) (ys : Word β) : Word γ := ofGet? λ i => match xs.get? i, ys.get? i with | some x, some y => f x y | _, _ => none noncomputable def length? (xs : Word α) : Option ℕ := Nat.find? (xs.get? · = none) noncomputable def toList? (xs : Word α) : Option (List α) := xs.length? |>.map xs.take noncomputable def ofSeq (a : ℕ → α) : Word α := ofGet? λ i => a i noncomputable def cycleList : List α → Word α | [] => nil | (x :: xs) => ofSeq λ i => (x :: xs)[i % (xs.length + 1)]'( by rw [List.length_cons]; apply Nat.mod_lt; simp) noncomputable def cycle (xs : Word α) : Word α := match xs.toList? with | some xs => cycleList xs | none => xs noncomputable instance : Coe (List α) (Word α) := ⟨ofList⟩ noncomputable instance : Coe (ℕ → α) (Word α) := ⟨ofSeq⟩ end Word open Word noncomputable def kolakoski' (a : Word ℕ) : Word ℕ := cons 1 $ cons 2 $ drop 2 $ flatten ∘ zipWith (ofList $ List.replicate · ·) a ∘ cycle $ [1, 2] open Classical in noncomputable def kolakoski : ℕ → ℕ := epsilon λ a => a = kolakoski' a
Bitwise
Proof of correctness [5]. This is a highly efficient iterative algorithm based on bitwise operations. This program is ported from the following Python program (written by David Eppstein) to Lean:
def Kolakoski():
x = y = -1
while True:
yield [2, 1][x&1]
f = y &~ (y+1)
x ^= f
y = (y+1) | (f & (x>>1))
It uses self-similar bitwise operations to generate a sequence that is equal to its own run lengths, and the unique sequence with that property is the Kolakoski sequence.
import Mathlib instance : HOr ℤ ℤ ℤ := ⟨Int.lor⟩ instance : HAnd ℤ ℤ ℤ := ⟨Int.land⟩ instance : HXor ℤ ℤ ℤ := ⟨Int.xor⟩ def kolakoski' (n : ℕ) : List ℕ := Id.run do let mut xs : List ℕ := [] let mut x : ℤ := -1 let mut y : ℤ := -1 for _ in [:n] do xs := (if Odd x then 1 else 2) :: xs let m := y &&& (y + 1).lnot x := x ^^^ m y := y + 1 ||| m &&& (x >>> 1) return xs.reverse def kolakoski (n : ℕ) : ℕ := (kolakoski' $ n + 1)[n]!