New
Foundations
for PBT

What’s the point
of testing
anymore?

Lean Port of Minecraft

The game: a fox on a grassy hillside.

State.step_entities_sound Entities collide properly with blocks

Session.play_dig_iff Clicks don’t get lost

mem_chunkFaces Faces are only drawn if visible

214 theorems

This took me Claude

2.5 hours

Game Logic Lean raylib C ? Graphics Model Lean tested proved
Lean Rocq F* Verus Proofs within components. Tests between languages.
Model raylib Instructions upload 3 beginScene draw 3 unload 3 ≈ Generators

How do we build great generators?

Palamedes Synthesizing Generators PLDI 2026 Basalt Foundations for Generators Feedback wanted!

Palamedes

Synthesizing Generators


def slotOK (slot : Option Nat) : Prop :=
  slot = none ∨
  ∃ n, (1 ≤ n ∧ n ≤ 64) ∧ slot = some n
            

theorem takeOne_ok (slot : Option Nat) :
    slotOK slot →
    slotOK (takeOne slot)
            

∃ g : G α, ∀ a, a ∈ g.support ↔ P a
          

def genSlot : G (Option Nat) := by
  generator_search slotOK
            

def genSlot : G (Option Nat) :=
  oneOf [
    return none,
    do
      let n ← chooseNat 1 64
      return (some n)
  ]
              

/-- Duration of one frame. -/
abbrev Frame := Nat
            

def framesOK (fs : List Frame) : Bool :=
  List.fold
    (fun
      | .nilF => true
      | .consF n ok => n ≤ 200000 && ok)
    fs
            
fold List Frame → Bool true unfold Bool → G (List Frame)
one step of the fold true one step of the unfold fold true unfold

theorem fold_unfold_inverse
    (alg  : ListF α β → β)
    (step : β → G (ListF α β)) :
    (∀ b, (step b).support =
      fun l => alg l = b) →
    (∀ b, (List.unfold step b).support =
      fun xs => List.fold alg xs = b)
            

def framesOK (fs : List Frame) : Bool :=
  List.fold
    (fun
      | .nilF => true
      | .consF n ok => n ≤ 200000 && ok)
    fs
              

def framesOK : List Frame → Bool
  | [] => true
  | n :: fs => n ≤ 200000 && framesOK fs
                

def genFrames :=
  List.unfold
    (fun () => oneOf [
      return .nilF,
      do
        let n ← chooseNat 0 200000
        return (.consF n ())
    ])
    ()
              

def genFrames : G (List Frame) :=
  oneOf [
    return [],
    do
      let n ← chooseNat 0 200000
      let ns ← genFrames
      return n :: ns
  ]
                
Palamedes can handle… 4 2 6 1 3 7 Red-Black Trees λ x : Nat. λ y : Bool. x STLC Terms

/-- Red-black trees. -/
def isRBT (t : Tree (Color × Nat))
    (h lo hi : Nat) : Bool :=
  isRR t && isBST t (lo, hi) && isBH t h
                

def genRBT (h lo hi : Nat) :
    G (Option (Tree (Color × Nat))) := by
  generator_search
    (fun t => isRBT t h lo hi)
                

/-- Well-typed lambda terms. -/
def isWellTyped (Γ : List Ty)
    (t : Term) : Prop :=
  ∃ τ, getType t Γ = some τ
                

def genWellTyped (Γ : List Ty) :
    G Term := by
  generator_search
    (fun t => isWellTyped Γ t)
                

Palamedes

Synthesizes generators that are correct by construction.

Automates synthesis with Lean’s tactic metaprogramming.

Handles recursive functions by matching folds with unfolds.

Basalt

Foundations for Generators

What does it mean for generators to be correct?


def genFrames : G (List Frame) :=
  oneOf [
    return [],
    do
      let n ← chooseNat 0 200000
      let ns ← genFrames
      return n :: ns
  ]
          

def elapsedOK n := n ≤ 200000

def genElapsed : G Nat :=
  frequency [
    (2, return 0),
    (5, chooseNat 1 450),
    (3, chooseNat 900 1100),
    (1, return 1000),
    (2, chooseNat 1001 16999),
    (1, chooseNat 14000 200000)
  ]
          

Prior work denotes generators as

sets (their support),

we want to talk about them as

distributions.


def SPMF (α : Type) : Type :=
  {μ : α → ℝ≥0∞ // (∑' a, μ a) ≤ 1}
          

def genFrames : G (List Frame) :=
  oneOf [
    return [],
    do
      let n ← chooseNat 0 200000
      let ns ← genFrames
      return n :: ns
  ]
            
1/2 1/4 1/8 [] [n] [n, n] …

def loop : G Bool := loop

def genCoin : G Bool :=
  oneOf [
    return true,
    return false,
    loop
  ]
            
1/3 1/3 true false 1/3 diverge

example : HasProbability genFrames [] ≤ 1 / 10 := by
  rw [genFrames, SPMF.prob_oneOf]
  simp [prob_cons_nil]
            

unsolved goals
⊢ 1 / 2 ≤ 1 / 10
            

example : IsCompleteFor genElapsed (· ≤ 200000) := by
  intro n hn
  rw [genElapsed, SPMF.mem_support_iff_may]
  walk
  omega
            

omega could not prove the goal:
a possible counterexample may satisfy the constraints
  451 ≤ n ≤ 899
            

IsSoundAndComplete What is the support?

IsAlmostSurelyTerminating Does it terminate?

HasProbability How likely is a value?

HasExpectedValue How big is a value on average?

IsCostBounded How many choices are made?

?

How do we
run an SPMF?


-- genFrames : ∀ {G : Type → Type}, [Gen G] → G (List Frame)
            

def genFrames [Gen G] : G (List Frame) :=
  oneOf [
    return [],
    do
      let n ← chooseNat 0 200000
      let ns ← genFrames
      return n :: ns
  ]
            

Carette et al., Finally Tagless, Partially Evaluated, JFP 2009

genFrames [Gen G] : G (List Frame) SPMF for distributional proofs SPMF.Cost for proofs about cost IO for standard PBT FuzzGen for fuzzing ?

class Gen (G : Type → Type) where
  instMonad        : Monad G
  instRandomChoice : RandomChoice G
  instCCPO         : ∀ α, CCPO (G α)
            

Claessen and Hughes, QuickCheck, ICFP 2000

Knaster–Tarski fixpoint theorem (Knaster 1928, Tarski 1955)
The Lean Language Reference, “Recursive Definitions”


class Gen (g : Type u → Type v) where
  instInhabited    : ∀ α, Inhabited (g α)
  instMonad        : Monad g
  instRandomChoice : RandomChoice g
  instCCPO         : ∀ α, CCPO (g α)
  instMonoBind     : MonoBind g
            

def genFrames [Gen G] : G (List Frame) :=
  oneOf [
    return [],
    do
      let n ← chooseNat 0 200000
      let ns ← genFrames
      return n :: ns
  ]
partial_fixpoint
            

def genFrames fuel :=
  match fuel with
  | 0 => return []
  | fuel + 1 =>
    oneOf [
      return [],
      do
        let n ← chooseNat 0 200000
        let ns ← genFrames fuel
        return n :: ns
    ]
            

Can Lean
generators
be fast?

QuickCheck Basalt bst 1.2× heap 1.7× sorted 1.6× nat 1.5×

def genFrames [Gen G] :
    G (List Frame) :=
  frequency [
    (1, return []),
    (9, do
      let n ← chooseNat 0 200000
      let ns ← genFrames
      return n :: ns)
  ]
partial_fixpoint
            
genFrames genFrames genFrames genFrames ⋮

Richey and Cutler et al., Fail Faster, OOPSLA 2026

oneOf, frequency oneOf!, frequency! QuickCheck bst 1.8× heap 2.6× sorted 3.0× nat 2.6×

Palamedes

Synthesizes generators that are correct by construction.

Automates synthesis with Lean’s tactic metaprogramming.

Handles recursive functions by matching folds with unfolds.

Basalt

A new foundational representation for generators.

Represents distributions with sub-probability mass functions.

Uses tagless-final to make generators interpretable.

Gains significant efficiency using elaborator macros.

Thank you!

Collaborators

  • Cassia Torczon
  • Daniel Sainati
  • Thia Richey
  • Joe Cutler
  • Hila Peleg
  • Leo Lampropoulos
  • Benjamin Pierce

Thank you!

lean-mc