New Foundations for PBT
Harry Goldstein
University at Buffalo · IFIP WG 2.11
Hi everyone. Today I’m going to be talking about some existing and some ongoing work,
focused around new foundations for property-based testing.
What’s the point of testing anymore?
And I want to start with a kind of uncomfortable question that I’ve been asking myself
recently, which is, what’s the point of testing anymore?
Lean Port of Minecraft
And to see why I might ask a question like that: this is an implementation of Minecraft.
It’s written in Lean, and it has proofs in a lot of the places where you might expect
there to be tests.
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
So, for example, there’s a proof that entities collide properly with blocks, that clicks
don’t get lost, and that faces are only drawn if they’re supposed to be visible.
This took me
Claude
2.5 hours
And this wasn’t some huge undertaking to build this. In fact, I built this in 2.5
hours. Well, Claude built this in 2.5 hours.
And so, there was a point at which I kind of put my head in my hands and I said, you
know, maybe all this work I’ve been doing on testing isn’t worth it. Do we actually
need tests anymore?
Game Logic
Lean
raylib
C
?
Graphics Model
Lean
tested
proved
But then I looked a little bit more closely at what Claude had done. And I found that
while the core game logic of the Minecraft game was written in Lean, the rendering
code and some of the low-level graphics stuff was in a C library called raylib, that
was just called from Lean. And to be clear, I don’t actually have a problem with this
design. I think raylib’s a perfectly good library. And I don’t see a lot of benefit to
writing a bunch of graphics drivers in Lean. But one does wonder, how could we
possibly write proofs in Lean that engage with this lower level API of the renderer?
And so, I had Claude build a model of the graphics. And then I tested that model
against raylib.
Once I was confident that the model actually did model raylib, then we wrote some
proofs that showed that the game logic interacted correctly with the model graphics
engine. And ultimately, then, we could get some end-to-end proofs about how the game
logic ended up influencing pixels on the screen.
Lean
Rocq
F*
Verus
Proofs within components.
Tests between languages.
And this made me feel quite a bit better about where testing belongs in this world. I
think in the sort of far-flung future, in the utopia of formal methods, there will be
proofs within most software components that show that the software does what the
software says it does.
But I think there will continue to be tests between different languages and between
different frameworks, where trying to bridge the semantics formally would just be too
difficult.
Model
raylib
Instructions
upload 3
beginScene
draw 3
unload 3
≈
Generators
Now. Okay, so if I believe this future, then we need to make sure that that testing is
actually good. So let’s look at how we did this testing in this Minecraft clone. So,
like I said, we had a model and we had raylib, which is being modeled.
And what we did is generate lots and lots of sequences of instructions to these two
graphics engines. And we checked that for each sequence of instructions, the model and
raylib agreed.
And in order to come up with these sequences of instructions, we needed a generator.
And generators are programs in the property-based testing literature that produce lots
and lots of random data which we can use for testing.
How do we build great generators?
And so what I’m going to talk about in the rest of this talk is, how do we build great
generators?
Palamedes
Synthesizing Generators
PLDI 2026
Basalt
Foundations for Generators
Feedback wanted!
And I’m going to do this in two parts. The first is about Palamedes. This was a paper
that was presented at PLDI 2026. And it’s about synthesizing PBT generators.
And then I’m going to talk about Basalt, which is about a kind of new foundation for
generators. And I’d love some feedback on that. In particular, we’re currently writing
up some of the results there.
Palamedes
Synthesizing Generators
Okay. So let’s start by talking about Palamedes. At its core, Palamedes engages with a
particular problem that we have with generators, which is that in PBT, our generators
are often constrained.
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)
some 513
some 90417
none
some 2466
some 0
some 7731802
some 148
some 30566
some 912
some 4400219
some 68201
some 37
some 5024
some 801133
some 65
some 0
some 1290
some 27718
some 366
some 9052471
some 4087
none
some 615220
some 73
some 18849
some 2203
some 540916
some 0
some 7140
some 12
So take this theorem that we might want to test. So this engages with a data structure
called a slot. And this is from the Minecraft clone, so the player has a bunch of
inventory slots, and those inventory slots can be in one of two states: either there’s
nothing in the slot, so the slot is none, or there’s something in the
slot and it’s a stack of items between one and 64 values. So this slotOK
predicate says what it means for a slot to be well formed. And then we have this
theorem, takeOne_ok, which says that if we have a slot that is well
formed, then taking one out of that slot still gives us a slot that’s well formed.
Now, what we might start doing is just generating slots at random. But if we do this,
we’re going to get a lot that aren’t valid. And this is just because the numbers end
up falling outside the valid range. So what we’d like to do is produce a generator
that can produce valid slots by construction.
∃ g : G α, ∀ a, a ∈ g.support ↔ P a
In Palamedes, we do this by proof search. So in particular, we construct the following
theorem. This theorem says that there exists some generator, such that the generator’s
support, the values that it can produce, is exactly P. And if we can prove
this theorem constructively, then we can lift the witness out, and that generator will
be exactly the generator we want for testing.
∨
oneOf
slot = none
return none
∃ n,
let n ←
(1 ≤ n ∧ n ≤ 64)
chooseNat 1 64
∧
;
slot = some n
return (some n)
[
,
do
]
apply synth_pick
apply synth_pure
apply synth_bind
apply synth_between
apply synth_pure
apply synth_pick
· apply synth_pure
· apply synth_bind
· apply synth_between
· intros
apply synth_pure
To give you a taste of what this looks like, I’m not going to be able to go into the
kind of gory details. But if you have this predicate, our slotOK
predicate, and you break it up into pieces, you can see how each of those pieces
corresponds to a different piece of a corresponding generator.
So, for example, a disjunction corresponds to a random choice. An equality constraint
corresponds to returning a particular value. An existential corresponds to a monadic
bind, a kind of sample, and then continue. A range predicate corresponds to a random
choice in a range. And when you put it all together, you get a generator which, by
construction, satisfies the predicate that we started with.
Now, what’s especially special about this in Palamedes is that we didn’t have to
implement a whole bunch of specialized code to implement this synthesis. Instead, what
happened is that this synthesis happens via tactics in Lean. So this is just
metaprogramming. Lean’s tactic system is built in Lean, so it’s Lean code that is
doing all this under the hood. And this string of tactics corresponds to breaking down
the predicate that we saw before, and building up the generator that we see now.
Taking this a step farther, we actually don’t have to write all of these tactics
manually. We can use proof search. And so the Aesop tactic in Lean uses proof search
to assemble a bunch of tactics into a proof. And in fact, we use Aesop under the hood
in Palamedes to actually do the core synthesis of the generators.
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)
]
So to see how this happens one more time: basically, the user writes down, I want a
generator for slots, and they write down generator_search slotOK. And it
just spits out the generator that we saw before.
/-- 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
Okay, so that’s the core idea behind Palamedes, but of course, I just showed you a
very simple example. And one other thing that predicates often have, which can be
difficult to deal with, is recursion. So now I just want to tell you quickly how we do
that.
I’m going to introduce a different predicate, framesOK, which just
constrains a list of frame durations for the graphics renderer. And the thing to focus
on is that it’s written with a fold.
fold
List Frame → Bool
true
unfold
Bool → G (List Frame)
And in Palamedes, we make generators from predicates that have folds via unfolds.
Basically, we observe that if a fold would take some data structure and give us true,
then an unfold could take true and give us a bunch of data structures for which that
fold would then have given us true. We can kind of round trip the fold and the unfold.
And this makes a lot of sense if you consider the fact that turning a predicate into a
generator is a lot like finding the inverse of that predicate. You’re just finding a
kind of nondeterministic inverse, because there are lots of values that the predicate
might map to true.
one step of the fold
true
one step of the unfold
fold
true
unfold
I don’t have time, again, to get into the really gory details of this process. But at
a high level, what we do is we show that if one step of the fold can be inverted by
one step of the unfold, then by construction, the unfold can invert the fold.
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
]
And this is how Palamedes can do all sorts of really interesting generators. So we can
do the framesOK generator. We can also do a version of the
framesOK generator that isn’t written using fold, by, under the hood,
observing that this recursion actually is a fold and turning it into a fold.
Palamedes can handle…
4
2
6
1
3
7
Red-Black Trees
λ x : Nat .
λ y : Bool .
x
STLC Terms
And we can also do fun things like red-black trees and STLC terms. Palamedes is
actually quite expressive in the range of generators that it can synthesize.
/-- 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 .
∨
oneOf
Automates synthesis with Lean’s tactic metaprogramming.
true
Handles recursive functions by matching folds with unfolds.
So, just to sort of recap what happened in this section: Palamedes can synthesize
generators that are correct by construction. It automates synthesis using Lean’s tactic
metaprogramming, and it handles recursive functions by matching folds with unfolds.
Basalt
Foundations for Generators
Okay, next, I’m gonna talk about Basalt, which is my new foundations for how PBT
generators should be expressed.
What does it mean for generators to be correct?
And the Basalt project started with this question: what does it mean for generators to
be correct? You might think that it’s the thing that I said just a few slides ago, which
is what Palamedes enforces, which is that the generator generates exactly the values
that satisfy the predicate.
def genFrames : G (List Frame) :=
oneOf [
return [],
do
let n ← chooseNat 0 200000
let ns ← genFrames
return n :: ns
]
[]
[16]
[]
[8840, 33]
[]
[]
[151907]
[]
[17, 60412, 250]
[]
[4]
[]
[]
[72655, 1001]
[33]
[2, 119]
But this can’t really be right, because actually, if you take the genFrames
generator that we produced with Palamedes, it produces empty lists half the time. That’s
not particularly good for testing.
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)
]
So then you might think, well, okay, maybe we just need to kind of mess with the
probabilities a bit. And maybe we can just have an LLM do that.
But then, if you look at this generator, which Claude produced as part of the
Minecraft project that I showed you before, it tried to do a bunch of fancy stuff to
kind of bias the generator to produce interesting values. But in doing so, it
accidentally broke completeness. This generator misses a range of values, which a
complete generator wouldn’t have missed. And this makes it so that we may not find
certain bugs if the frame duration happens to need a value between 451 and 899.
Prior work denotes generators as
sets (their support),
we want to talk about them as
distributions .
The fundamental problem here is that we have a bunch of competing concerns, right? We
need soundness and we need completeness, but we also need to be able to talk about the
distribution.
Basically, prior work, which includes the QuickChick paper, but also Palamedes, uses
sets as their denotation of generators. And what we really need is distributions. We
need to be able to prove things about generators at the level of distributions.
def SPMF (α : Type) : Type :=
{μ : α → ℝ≥0∞ // (∑' a, μ a) ≤ 1}
In order to do this, we need a new kind of semantic domain in which to interpret
generators. And I’m going to choose subprobability mass functions, or SPMFs. And a
subprobability mass function is two things. It’s a mass function: it assigns a real
number mass to each value of type α. And then we need to promise that the
total mass sums to at most one. And this is what makes it able to be interpreted as a
probability.
Now, for some of you who might be more familiar with probabilistic programming stuff,
you might be a little bit worried that I’m not doing some measure theory here. But
generators are always going to be generating in countable domains. And so we actually
don’t need to worry about measure theory here.
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]
…
Okay, once we have this SPMF interpretation of generators, we can interpret a generator
like genFrames as a distribution. And we can see that it produces empty
lists with probability one half, and one-element lists with probability one fourth, et
cetera. This is not a particularly good distribution, but now we can know that for sure.
And importantly here, the probability of divergence is actually going to be zero.
def loop : G Bool := loop
def genCoin : G Bool :=
oneOf [
return true,
return false,
loop
]
1/3
1/3
true
false
1/3
diverge
And I said we were using subprobability mass functions, not just probability mass
functions. So the mass doesn’t actually need to sum all the way to one. It can sum to
less than one.
And so that’s illustrated in this generator right here, which actually diverges with
probability one third. So with probability one third you get true, and with one third
you get false, but with one third you just diverge and don’t do anything. You might
think this is actually a problem, but I’ll show you later why this actually ends up
being really nice.
example : HasProbability genFrames [] ≤ 1 / 10 := by
rw [genFrames, SPMF.prob_oneOf]
simp [prob_cons_nil]
unsolved goals
⊢ 1 / 2 ≤ 1 / 10
Okay, now that we have SPMFs, we can rule out generators that we didn’t like before. So
for example, our genFrames generator: we can show that, yes, it has one
half probability on the empty list. So if, for example, we want to require that it
generates the empty list with less than one tenth probability, that proof would fail.
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
And similarly, for the generator that Claude wrote, which is incomplete, if we try to
prove that it is complete, it will tell us, no, you’re missing between 451 and 899,
exactly like I said. So now we have the semantic power to be able to rule out both of
the bad kinds of generators that I showed you before.
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?
?
It turns out there’s a bunch of things we can prove about generators. So, we can prove
that they’re sound and complete, just like before. We can prove the support. We can
also prove almost sure termination. This is really nice if we want to be able to write
a generator that doesn’t structurally terminate, but instead terminates with just high
probability. I’ll show you an example of one of those in a few minutes. We might want
to know the probability of a particular value, or some expected value. We might also
want to know the cost. We might want to know how many choices are made when generating
a particular value.
But one thing I’d love feedback on, either now or later, is what else might we want to
prove about generators? Now that we have all of this power, what do we want to do with
it? I think all these things on the slide right now are things that it’s pretty easy
to recommend proving about all kinds of generators. But I think it’s a little bit
trickier to be able to say, for a more specific context, what shapes of things might
people want to enforce about their generators?
How do we run an SPMF ?
Okay, there’s one more thing I want to talk about now that we’ve gotten the proof stuff
out of the way, which is, how do you actually run one of these generators? Because an
SPMF is actually not runnable. In Lean, it’s a classical object.
-- 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
And so the trick is that you don’t actually represent generators as SPMFs. You
represent generators using a tagless final style. So, actually, what generators are in
Basalt is universally quantified values, quantified over some generator type.
So, the way this genFrames actually ends up being typed is: for all
G, which are type constructors, assuming G satisfies a
generator type class, then the value is of type G of List
Frame. This allows us to interpret a generator at lots of different kinds of
types, which satisfy the Gen type class.
genFrames
[Gen G] : G (List Frame)
SPMF
for distributional proofs
SPMF.Cost
for proofs about cost
IO
for standard PBT
FuzzGen
for fuzzing
?
Now that we have this tagless final embedding, we can interpret generators in all
sorts of ways. So, for example, we can interpret them as SPMFs, or SPMFs that track
cost. This way we can prove things about the number of choices that a generator needs
to produce a given value.
We can run them both in IO for standard PBT and with a fuzzing back end
for fuzzing.
And maybe we can do other things. I’d love if you have ideas for kinds of things that
we might be able to interpret these generators as.
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”
Here’s the Gen type class for you, just so you can see what it looks
like. It has two main parts. The first is that generators have to be monads with
random choice. This is classic QuickCheck stuff.
But the second part is, I think, what’s a little bit more unique about Basalt.
Generators need to be CCPOs. This gives us Knaster–Tarski fixed points, and in some
recent code that has been implemented in Lean, and that is now supported by the
language, this allows us to write non-terminating definitions, but still prove
things about those non-terminating definitions, because the definition is well
formed as a Knaster–Tarski fixed point. If you don’t know what that means, don’t
worry.
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
]
The upshot is that we can write generators like I’ve been writing them in this talk,
that look like this, using this partial_fixpoint annotation. And we don’t
have to give them fuel.
And in QuickChick, the other formal representation of generators, you do have to give
them fuel like this. And this ends up being a real pain, both when running them and
for proofs.
Can Lean generators be fast ?
The last question I want to answer is, can generators in Lean be fast? Are people
actually gonna want to run these things? It’s nice that you can prove things and run the
same generator. But do you actually want to run them in Lean?
QuickCheck
Basalt
bst
1.2×
heap
1.7×
sorted
1.6×
nat
1.5×
And it turns out the answer is yes. And it’s because I implemented some tricks from a
paper by some collaborators and I.
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
⋮
So, the main reason one might not want to run these generators is that when you use
combinators like frequency, you end up allocating quite a bit of memory.
Because every time you evaluate this combinator, you actually have to allocate memory
for this list here.
frequency!
let n ← chooseNat 0 9
(2, a)
if n < 2 then a
(5, b)
else if n < 7 then b
(3, c)
else if n < 10 then c
[
,
,
]
do
else default
frequency [
(2, a),
(5, b),
(3, c)
]
=
Richey and Cutler et al., Fail Faster , OOPSLA 2026
So, what I do in Basalt is this trick from a paper by Richey and Cutler, which I’m on,
from OOPSLA just last week. And the idea is to replace these frequency
combinators with elaborator macros. And so this frequency will actually
elaborate to just an if chain. And then it’ll also elaborate to a proof that that if
chain is the same as the original frequency.
And so, when running the generator, we get to use this if chain, which is really fast
and doesn’t allocate. And when proving things about the generator, we get to use this
kind of nicer frequency form.
oneOf, frequency
oneOf!, frequency!
QuickCheck
bst
1.8×
heap
2.6×
sorted
3.0×
nat
2.6×
And it turns out that implementing this makes our generators quite a bit faster. So in
some examples, the generator gets over three times faster. And it turns out, actually,
that the generator is also faster than Haskell QuickCheck, and also OCaml’s Base
QuickCheck. So running these generators in Lean is actually faster than in many other
programming languages in head-to-head comparisons.
Palamedes
Synthesizes generators that are
correct by construction .
∨
oneOf
Automates synthesis with Lean’s tactic metaprogramming.
true
Handles recursive functions by matching folds with unfolds.
Basalt
A new foundational representation for
generators .
Represents distributions with sub-probability mass functions.
SPMF
IO
Uses tagless-final to make generators interpretable.
Gains significant efficiency using elaborator macros.
Okay, so I hope you had fun. That was Basalt, a new foundational representation for
generators. It represents distributions with SPMFs, uses tagless final to make
generators interpretable, and gains significant efficiency using elaborator macros.
And you also saw Palamedes, and I’m really excited about this stuff.
Thank you!
Collaborators
Cassia Torczon
Daniel Sainati
Thia Richey
Joe Cutler
Hila Peleg
Leo Lampropoulos
Benjamin Pierce
I’d love to talk more about any of this if you’re interested. But for now, thank you
very much.