Repository navigation
recfun measures decrease along a model of WellFounded (#1308) - #1310
Merged
Merged
Conversation
A recfun's termination obligation compared the measure with whatever
`<` was in scope, at any measure type, so a `<` that isn't well-founded
(a user's `<` that is always true, or `<` on Int) let a non-terminating
recfun through, and its equation proved false.
Now the measure of type τ decreases along the `<` of the model of
`WellFounded<τ>` in scope:
concept WellFounded<T> {
fun operator < : fn (T, T) -> bool
theorem strong_induction: all P: fn T -> bool, n:T.
if (all j:T. if (all i:T. if i < j then P(i)) then P(j)) then P(n)
}
- lib/WellFounded.pf declares the concept. NatDiv.pf has the model for
Nat (strong_induction moves above `/`, which needs it) and UIntAdd.pf
the model for UInt (uint_strong_induction).
- GenRecFun records `@WellFounded<τ>.<` with the models in scope at
uniquify, in place of the names of `<`. The termination check takes
that model's `<` (`well_founded_less`), after checking that the
concept named WellFounded in scope has one operation `<` and the
strong induction theorem for it, so a look-alike concept can't stand
in. A where clause `where { WellFounded<T> }` provides the order for
a generic measure of type T.
- process_declarations checks the measure type with the type
parameters in scope, so a measure can have type T.
- The compiler skips concept and model declarations (they have no code
of their own; running where functions is #1292), so programs that
import Nat or UInt still compile.
- The imperative bounds check looks up `<` by name; it skips models'
operation definitions, which now include a `<` at UInt.
Tests: should-error for the #1308 Tick repro, an Int measure, and a
look-alike concept; should-validate for a user type with its own model
and a generic recfun measured by T. The lowering fixture genrecfun.pf
declares the concept and a model (it runs without the stdlib).
Reference.md's Recfun section and StandardLib.md describe WellFounded.
Closes #1308
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
jsiek
marked this pull request as ready for review
November 20, 2026 15:39
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 6a6a2266f3
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #1308 (soundness: recfun accepts a measure whose < is not well-founded)
Stacked on #1306 (where clauses on recfun); GitHub retargets it to
mainwhen #1306 merges.The bug
theorem oops: false.A measure of type
Intgets the same error (no model of WellFounded<Int> in scope).The fix
A measure of type
τdecreases along the<of the model ofWellFounded<τ>in scope. The concept is inlib/WellFounded.pf:WellFounded<Nat>inNatDiv.pf, fromstrong_induction. That theorem and its helper move aboverecfun /, which now needs the model; they don't depend on/.WellFounded<UInt>inUIntAdd.pf, fromuint_strong_induction.<and give a model.test/should-validate/recfun_well_founded.pfdoes this forunion Steps { steps(UInt) }, ordered by the count.recfun down<T> where { WellFounded<T> } (x : T, y : T) -> UInt measure x of Tgets its order from the where clause, and itsterminatesproof seesstrong_inductionas a given.down(3, 1)computes atUInt.WellFounded, then checks that the concept has one operation<and a theorem alpha-equivalent to strong induction for that<. So aconcept WellFounded<T> { fun operator < : ... }without the theorem can't stand in. That case is covered byrecfun_fake_well_founded.pf.Code
GenRecFun: uniquify records@WellFounded<τ>.<(aConceptMember, with the models in scope) inless, replacing the list of the names of<.well_founded_less(checker_concepts.py), which is the model's<after the shape check. Awhere { WellFounded<T> }clause's model is found first, as for@C<T>.m.process_declarationschecks the measure type with the type parameters in scope. Before,measure x of Tfailed with "undefined type variable T".compiler/lower.py): it skipsConceptDeclandModelDecl, which have no code of their own. Without this, programs importingNatorUIntfailed with "compiler does not yet support top-level: ConceptDecl". Running where functions is still C back end: support concepts, models, and where clauses #1292.imperative_verifier.py): it gathers<by base name over the environment. It now skips models' operation definitions, becauseWellFounded<UInt>'s<atUIntmadei < length(a)ambiguous.test/compile/lower/genrecfun.pf: it runs without the standard library, so it declares the concept and a model ofWellFounded<MyNat>from a postulated strong induction. The lowering doesn't use the model. Its expected IR changed only in uniquify counters.Docs
WellFoundedrequirement, the library's models, how to add one, and the where-clause case.WellFounded.Tests
recfun_measure_not_well_founded.pf(the Soundness: recfun accepts a measure whose < is not well-founded, proving false #1308 repro),recfun_measure_int.pf,recfun_fake_well_founded.pf.recfun_well_founded.pf, with both parsers.test-deduce.py,make static, unit tests (697), and the LSP state-isolation, library and goal-at tests pass.test/compile/run_lower.pypasses exceptsource_map.pf.run_lower'ssource_map.pf: its out-of-boundsprintis now rejected by the checker, filed as Compile fixture source_map.pf fails: checker rejects its out-of-bounds print #1309.run_e2e's programs that use the standard library: the lowering doesn't supportViewDecl, Compiling with the stdlib prelude fails: lowering does not support ViewDecl (lib/UIntDefs.pf) #1211.🤖 Generated with Claude Code