Skip to content

recfun measures decrease along a model of WellFounded (#1308) - #1310

Merged
jsiek merged 2 commits into
issue-1298-recfun-wherefrom
issue-1308-wellfounded
Nov 20, 2026
Merged

jsiek merged 2 commits into
issue-1298-recfun-wherefrom
issue-1308-wellfounded

Conversation

@jsiek

@jsiek jsiek commented Nov 20, 2026

Copy link
Copy Markdown
Owner

Closes #1308 (soundness: recfun accepts a measure whose < is not well-founded)

Stacked on #1306 (where clauses on recfun); GitHub retargets it to main when #1306 merges.

The bug

union Tick { tick }
fun operator < (a : Tick, b : Tick) { true }

recfun loop(n : UInt) -> UInt
  measure tick of Tick
{ 1 + loop(n) }
terminates { arbitrary n:UInt  evaluate }

theorem oops: false
proof
  have eq: # loop(0) # = 1 + loop(0) by expand loop.
  have bad: loop(0) < # loop(0) # by replace eq.
  apply uint_less_irreflexive[loop(0)] to bad
end
  • Before: the file is valid, including theorem oops: false.
  • After:
    no model of WellFounded<Tick> in scope
    A recfun's measure decreases along the `<` of a model of WellFounded at its type, which proves that `<` is well-founded.
    

A measure of type Int gets the same error (no model of WellFounded<Int> in scope).

The fix

A measure of type τ decreases along the < of the model of WellFounded<τ> in scope. The concept is in lib/WellFounded.pf:

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)
}
  • Models in the library:
    • WellFounded<Nat> in NatDiv.pf, from strong_induction. That theorem and its helper move above recfun /, which now needs the model; they don't depend on /.
    • WellFounded<UInt> in UIntAdd.pf, from uint_strong_induction.
  • A new measure type: prove strong induction for its < and give a model. test/should-validate/recfun_well_founded.pf does this for union Steps { steps(UInt) }, ordered by the count.
  • A generic measure: recfun down<T> where { WellFounded<T> } (x : T, y : T) -> UInt measure x of T gets its order from the where clause, and its terminates proof sees strong_induction as a given. down(3, 1) computes at UInt.
  • Look-alike concepts: the checker resolves the name WellFounded, then checks that the concept has one operation < and a theorem alpha-equivalent to strong induction for that <. So a concept WellFounded<T> { fun operator < : ... } without the theorem can't stand in. That case is covered by recfun_fake_well_founded.pf.

Code

  • GenRecFun: uniquify records @WellFounded<τ>.< (a ConceptMember, with the models in scope) in less, replacing the list of the names of <.
  • The termination check builds the obligation with well_founded_less (checker_concepts.py), which is the model's < after the shape check. A where { WellFounded<T> } clause's model is found first, as for @C<T>.m.
  • process_declarations checks the measure type with the type parameters in scope. Before, measure x of T failed with "undefined type variable T".
  • Two changes so existing paths keep working:
    • Compiler (compiler/lower.py): it skips ConceptDecl and ModelDecl, which have no code of their own. Without this, programs importing Nat or UInt failed 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 bounds check (imperative_verifier.py): it gathers < by base name over the environment. It now skips models' operation definitions, because WellFounded<UInt>'s < at UInt made i < length(a) ambiguous.
  • Lowering fixture test/compile/lower/genrecfun.pf: it runs without the standard library, so it declares the concept and a model of WellFounded<MyNat> from a postulated strong induction. The lowering doesn't use the model. Its expected IR changed only in uniquify counters.

Docs

  • Reference.md's Recfun section describes the WellFounded requirement, the library's models, how to add one, and the where-clause case.
  • StandardLib.md lists WellFounded.

Tests

🤖 Generated with Claude Code

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
jsiek marked this pull request as ready for review November 20, 2026 15:39
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-20T15:43:14.092151Z 6a6a226 Draft marked ready
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment thread abstract_syntax/declarations.py Resolved
@jsiek
jsiek merged commit 194238c into issue-1298-recfun-where Nov 20, 2026
@jsiek
jsiek deleted the issue-1308-wellfounded branch November 20, 2026 15:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant