Repository navigation
Conversation
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>
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>
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: 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".
| if where is not None: | ||
| new_vars += where_params(where) |
There was a problem hiding this comment.
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 👍 / 👎.
There was a problem hiding this comment.
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.
| if where is not None: | ||
| new_vars += where_params(where) |
There was a problem hiding this comment.
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 👍 / 👎.
There was a problem hiding this comment.
Thanks, fixed in 5037094. recursive ... where from #1291 had the same problem, through RecFun.params.
RecFungetsown_params(), matchingGenRecFun.own_vars(), and its_headeruses it._symbol_info_forrenders 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>
Closes #1298 (recfun can't take a where clause)
A
recfun(general recursion with a measure) may now have a where clause, asfunandrecursivecan since #1291 (where clauses on generic functions):Before, both parsers rejected
whereafter 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 theterminatesproof starts witharbitrary n:UInt, x:Tas before. The proof is checked in the where clause's scope, the same as afterarbitrary T:type where { Monoid<T> }:have az: all y:T. y + zero = y by add_zeroworks insideterminates;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 ... wherefrom #1291:Deduce.larkandparser.py(ageneral_recursive_function_wherealternative), andrec_desc_parser.parse_gen_rec_function.GenRecFun.where: uniquify binds the clause and adds its operations tovars, after the function's own parameters. Theterminatesproof is uniquified in the clause's scope. Printing (_header,to_string, so also.thmoutput) shows the clause and hides the operations (own_vars()).where_fun_typein process_declarations, type_check_stmt, collect_env and type synthesis. The body and measure are checked with the clause's models (add_where_models).TermInstpasses the operations (TermInst.dicts) after the arguments, as forRecFun.own_vars(), and the proof is checked underwhere_scope. That's a new helper in checker_concepts, shared withintro_where: it declares the operations and the theorems as givens and adds the models.recfunand itsterminatesproof. Reference.md has norecfungrammar section to extend.Tests
test/test-imports/ConceptMonoidSum.pf:mpowand the where theoremmpow_one. The.thmoutput reparses asrecfun mpow<T> where { Monoid<T> } (n:UInt, x:T) -> T ....test/should-validate/where_functions.pf, with both parsers:mpowcomputing atbool;@mpow<bool>inside a lambda passed tomap;mpow_one<bool>;recfunwhoseterminatesproof uses the concept's theoremadd_zero.test/should-error/where_recfun_no_model.pf("no model of Monoid in scope").test-deduce.py,make static, unit tests (748), and the LSP teststest_state_isolation,test_goal_atandtest_librarypass.🤖 Generated with Claude Code