Skip to content

Where clauses on recfun (#1298) - #1306

Open
jsiek wants to merge 6 commits into
mainfrom
issue-1298-recfun-where
Open

jsiek wants to merge 6 commits into
mainfrom
issue-1298-recfun-where

Conversation

@jsiek

@jsiek jsiek commented Nov 20, 2026

Copy link
Copy Markdown
Owner

Closes #1298 (recfun can't take a where clause)

A recfun (general recursion with a measure) may now have a where clause, as fun and recursive can since #1291 (where clauses on generic functions):

recfun mpow<T> where { Monoid<T> } (n : UInt, x : T) -> T
  measure n of UInt
{
  if n = 0 then zero
  else x + mpow(n ∸ 1, x)
}
terminates {
  arbitrary n:UInt, x:T
  uint_monus_one_less[n]
}
// mpow : fn <T> where { Monoid<T> } (UInt, T) -> T

assert mpow(3, true) = true                      // Monoid<bool>'s operations
assert map([0, 2], fun n:UInt { @mpow<bool>(n, true) }) = [false, true]

theorem mpow_one: all T:type where { Monoid<T> }. all x:T. mpow(1, x) = x
proof
  arbitrary T:type where { Monoid<T> }
  arbitrary x:T
  expand 2*mpow
  add_zero[x]
end

Before, both parsers rejected where after the type parameters.

The termination obligation

The obligation still quantifies over the function's own parameters only. Here it is all n:UInt, x:T. if not n = 0 then n ∸ 1 < n, so the terminates proof starts with arbitrary n:UInt, x:T as before. The proof is checked in the where clause's scope, the same as after arbitrary T:type where { Monoid<T> }:

  • the clause's operations are variables;
  • its theorems are givens, so have az: all y:T. y + zero = y by add_zero works inside terminates;
  • its models are in scope.

The obligation isn't stated as a where formula, all T:type where {...}. .... recfun's type parameters are already free in the obligation (declared as type variables) rather than quantified, and this keeps that shape. This is the main design point to review.

Changes

Following recursive ... where from #1291:

  • Grammar: Deduce.lark and parser.py (a general_recursive_function_where alternative), and rec_desc_parser.parse_gen_rec_function.
  • GenRecFun.where: uniquify binds the clause and adds its operations to vars, after the function's own parameters. The terminates proof is uniquified in the clause's scope. Printing (_header, to_string, so also .thm output) shows the clause and hides the operations (own_vars()).
  • Its type: where_fun_type in process_declarations, type_check_stmt, collect_env and type synthesis. The body and measure are checked with the clause's models (add_where_models).
  • Reduction: a call through a TermInst passes the operations (TermInst.dicts) after the arguments, as for RecFun.
  • Termination check: the obligation uses own_vars(), and the proof is checked under where_scope. That's a new helper in checker_concepts, shared with intro_where: it declares the operations and the theorems as givens and adds the models.
  • Reference.md: the where-clause Functions paragraph covers recfun and its terminates proof. Reference.md has no recfun grammar section to extend.

Tests

  • test/test-imports/ConceptMonoidSum.pf: mpow and the where theorem mpow_one. The .thm output reparses as recfun mpow<T> where { Monoid<T> } (n:UInt, x:T) -> T ....
  • test/should-validate/where_functions.pf, with both parsers:
    • mpow computing at bool;
    • @mpow<bool> inside a lambda passed to map;
    • mpow_one<bool>;
    • a recfun whose terminates proof uses the concept's theorem add_zero.
  • New should-error: test/should-error/where_recfun_no_model.pf ("no model of Monoid in scope").
  • Full test-deduce.py, make static, unit tests (748), and the LSP tests test_state_isolation, test_goal_at and test_library pass.

🤖 Generated with Claude Code

A `recfun` may now have a where clause, as `fun` and `recursive` can
since #1291:

  recfun mpow<T> where { Monoid<T> } (n : UInt, x : T) -> T
    measure n of UInt
  { if n = 0 then zero else x + mpow(n ∸ 1, x) }
  terminates { arbitrary n:UInt, x:T  uint_monus_one_less[n] }

- Both parsers, and GenRecFun's `where`: uniquify binds the clause and
  adds its operations to `vars` after the function's own parameters;
  printing shows the clause and hides them (`own_vars`).
- Its type is `where_fun_type`'s (process_declarations, type_check_stmt,
  collect_env, type synthesis); the body is checked with the clause's
  models; a call through a `TermInst` passes the operations after the
  arguments.
- The termination obligation quantifies over the function's own
  parameters, and the `terminates` proof is checked in the clause's
  scope (`where_scope`, shared with `intro_where`): its operations, its
  theorems as givens, and its models.
- Tests: `mpow` in test/test-imports/ConceptMonoidSum.pf with a where
  theorem; uses in where_functions.pf (computing, `@mpow<bool>` in a
  lambda passed to `map`, the where theorem at bool, and a `terminates`
  proof using `add_zero`); should-error where_recfun_no_model.pf.
- Reference.md: where clauses on `recfun`.

Closes #1298

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
jsiek and others added 2 commits November 20, 2026 09:03
Reference.md had no section on `recfun`. The new one gives its grammar
(with and without a where clause), the `replicate` example, what the
measure and the `terminates` obligation are (with the obligation for
`replicate`), and that a where clause is in scope in the body and the
proof. It notes that the measure's `<` must be well-founded, which
Deduce doesn't check yet (#1308). The Recursive Function section points
to it.

Refs #1298

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The obligation quantified over every parameter of the recfun, so
replicate's was `all n:UInt. all x:T. if n ≠ 0 then n ∸ 1 < n` although
`x` doesn't occur. It now quantifies over only the parameters that
occur: `all n:UInt. if n ≠ 0 then n ∸ 1 < n`. The `terminates` proofs
that bound the unused `x` drop it; the library's recfuns use all their
parameters, so it is unchanged. Reference.md's Recfun section says so.

Refs #1298

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Refs #1298

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jsiek
jsiek marked this pull request as ready for review November 20, 2026 15:45
@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:48:26.513746Z cbf5f05 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: cbf5f0512e

ℹ️ 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 on lines +1413 to +1414
if where is not None:
new_vars += where_params(where)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Pass where dictionaries when lowering recfun calls

When a recfun ... where is compiled, these appended operation parameters become real function parameters because compiler/lower.py::lower_genrecfun lowers every entry in r.vars. However, lower_call strips the TermInst and emits only Call.args, discarding its dicts, so both external and recursive calls pass fewer arguments than the generated function expects and concept operations read missing runtime arguments. Preserve and append the TermInst.dicts during call lowering, or otherwise erase/substitute these dictionary parameters consistently.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Thanks, confirmed. lower_term peels a TermInst to its subject and drops dicts. For

fun twice<T> where { Monoid<T> } (x:T) { x + x }
print twice(true)

--no-stdlib --compile failed while emitting C, with arity mismatch calling twice.s4_4: declared 3, called with 1. This applies to every where function from #1291, not only recfun.

Passing the operations needs more than appending dicts, though: lowering doesn't emit models' operations yet either. Both are the work of #1292 (the C back end for concepts and where clauses). I corrected #1292's plan, which said nothing extra was needed for where functions: #1292 (comment)

For now, 5037094 makes lowering stop at a TermInst with dictionaries, with

compiler does not yet pass the operations of a where clause, in the use of twice (issue #1292)

test/unit/test_compile_where.py pins this for a fun and a recfun.

Comment on lines +1413 to +1414
if where is not None:
new_vars += where_params(where)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Hide dictionary parameters from recfun signatures

For a checked recfun with a where clause, this makes stmt.vars include fresh internal operation binders, but lsp/query.py::_symbol_info_for still renders every entry in stmt.vars. Consequently, editor outlines and the MCP list_symbols result expose generated names and extra parameters that are absent from the source signature. Render stmt.own_vars() there, as the declaration's own printers now do.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Thanks, fixed in 5037094. recursive ... where from #1291 had the same problem, through RecFun.params.

  • RecFun gets own_params(), matching GenRecFun.own_vars(), and its _header uses it.
  • _symbol_info_for renders only the source's parameters, with their base names, and shows the where clause.

test/lsp/test_symbols.py::test_list_symbols_hides_where_operations pins these signatures:

function msum<T> where { Monoid<T> } (List<T>) -> T
function replicate<T> where { Monoid<T> } (n: UInt, x: T) -> List<T>

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>
)

From Codex's review of #1306:

- An outline/list_symbols signature for a where function listed the
  operations that uniquify appends as parameters. RecFun gets
  own_params() (GenRecFun already has own_vars()), and both signatures
  show the where clause and only the source's parameters, e.g.
  function msum<T> where { Monoid<T> } (List<T>) -> T.
- Lowering dropped TermInst.dicts, so a call to a where function passed
  fewer arguments than the function takes, and emitting C failed with
  an arity mismatch. Lowering now stops at a TermInst with dictionaries
  with an error naming #1292, which tracks passing them.

Refs #1298, #1292

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
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.

recfun can't take a where clause

1 participant