Repository navigation
Where clauses on recfun (#1298) #1306
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
8eb0b9b
98d70ea
c53adc0
cbf5f05
194238c
5037094
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -39,7 +39,7 @@ | |
| if TYPE_CHECKING: | ||
| from .env import Env | ||
| from .ops import uniquify_deduce | ||
| from .concepts import WhereClause, where_params | ||
| from .concepts import ConceptMember, ConceptRef, WhereClause, where_params | ||
|
|
||
| @dataclass | ||
| class Declaration(Statement): | ||
|
|
@@ -1307,8 +1307,12 @@ def _hidden(self) -> int: | |
| (added by uniquify).""" | ||
| return len(self.where.operations()) if self.where else 0 | ||
|
|
||
| def own_params(self) -> List[Type]: | ||
| """``params`` without the operations of the where clause.""" | ||
| return self.params[:len(self.params) - self._hidden()] | ||
|
|
||
| def _header(self) -> str: | ||
| params = self.params[:len(self.params) - self._hidden()] | ||
| params = self.own_params() | ||
| return complete_name(self.name) \ | ||
| + ('<' + ','.join([name2str(t) for t in self.type_params]) + '>' \ | ||
| if len(self.type_params) > 0 else '') \ | ||
|
|
@@ -1373,10 +1377,16 @@ class GenRecFun(Declaration): | |
| body: Term | ||
| terminates: Proof | ||
| trusted_terminates: bool = False | ||
| # The unique names of the `<` visible where this function is defined, | ||
| # recorded by ``uniquify``: the termination conditions compare the | ||
| # measure with it (#1250). | ||
| less: List[str] = field(default_factory=list, kw_only=True) | ||
| # `@WellFounded<measure_ty>.<`, recorded by ``uniquify`` with the | ||
| # models in scope: the termination conditions compare the measure with | ||
| # the `<` of the model of WellFounded at the measure's type, whose | ||
| # theorem proves that `<` is well-founded (#1308). | ||
| less: Optional[ConceptMember] = field(default=None, kw_only=True) | ||
| # A where clause on the type parameters (#1298), as for `RecFun` | ||
| # (#1282): uniquify adds the operations of its refinement closure to | ||
| # ``vars``, after the function's own parameters. The `terminates` | ||
| # proof is in the clause's scope. | ||
| where: Optional[WhereClause] = field(default=None, kw_only=True) | ||
|
|
||
| def uniquify(self, env: object, ctx: object) -> GenRecFun: | ||
| env_map = cast(UniquifyEnv, env) | ||
|
|
@@ -1386,9 +1396,8 @@ def uniquify(self, env: object, ctx: object) -> GenRecFun: | |
|
|
||
| body_env, new_type_params = uniquify_type_params( | ||
| env_map, self.type_params, uniq_ctx, self.location, extend) | ||
| terminates_env = copy_dict(env_map) | ||
| for (old, new) in zip(self.type_params, new_type_params): | ||
| extend(terminates_env, old, new, self.location) | ||
| where = self.where.bind(body_env, uniq_ctx) if self.where else None | ||
| terminates_env = copy_dict(body_env) | ||
|
|
||
| new_returns = self.returns.uniquify(body_env, uniq_ctx) | ||
|
|
||
|
|
@@ -1403,15 +1412,25 @@ def uniquify(self, env: object, ctx: object) -> GenRecFun: | |
| overwrite(body_env, old, new, self.location) | ||
|
|
||
| new_measure = self.measure.uniquify(body_env, uniq_ctx) | ||
| new_measure_ty = self.measure_ty.uniquify(env_map, uniq_ctx) | ||
| new_measure_ty = self.measure_ty.uniquify(body_env, uniq_ctx) | ||
| if 'WellFounded' not in body_env: | ||
| static_error(self.location, 'a recfun needs the concept WellFounded' | ||
| + ' (lib/WellFounded.pf) in scope: its measure decreases' | ||
| + ' along the `<` of a model of WellFounded') | ||
| less = ConceptMember(self.location, None, | ||
| ConceptRef(self.location, 'WellFounded', | ||
| [self.measure_ty]).uniquify(body_env, uniq_ctx), | ||
| '<', models=list(body_env.get(MODELS_KEY, []))) | ||
| new_terminates = self.terminates.uniquify(terminates_env, uniq_ctx) | ||
| new_body = self.body.uniquify(body_env, uniq_ctx) | ||
| if where is not None: | ||
| new_vars += where_params(where) | ||
|
Comment on lines
+1426
to
+1427
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
For a checked Useful? React with 👍 / 👎.
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks, fixed in 5037094.
|
||
|
|
||
| return GenRecFun(self.location, new_name, new_type_params, | ||
| new_vars, new_returns, new_measure, | ||
| new_measure_ty, new_body, new_terminates, | ||
| self.trusted_terminates, visibility=self.visibility, | ||
| less=list(env_map.get('<', []))) | ||
| less=less, where=where) | ||
|
|
||
| _exports_overload = True | ||
|
|
||
|
|
@@ -1423,18 +1442,26 @@ def __str__(self) -> str: | |
|
|
||
| def to_string(self) -> str: | ||
| return 'recfun ' + self.name + '<' + ','.join(self.type_params) + '>' \ | ||
| + (' ' + str(self.where) + ' ' if self.where else '') \ | ||
| + '(' + ', '.join([x + ':' + str(t) if t else x\ | ||
| for (x,t) in self.vars]) + ')' \ | ||
| for (x,t) in self.own_vars()]) + ')' \ | ||
| + ' -> ' + str(self.returns) + '\n' \ | ||
| + '\tmeasure ' + str(self.measure) \ | ||
| + ' {\n' + str(self.body) + '\n}\n' \ | ||
| + 'terminates {\n' + str(self.terminates) + '\n}\n' | ||
|
|
||
| def own_vars(self) -> List[Tuple[str, Type]]: | ||
| """``vars`` without the operations of the where clause (added by | ||
| uniquify).""" | ||
| k = len(self.where.operations()) if self.where else 0 | ||
| return self.vars[:len(self.vars) - k] | ||
|
|
||
| def _header(self) -> str: | ||
| return complete_name(self.name) \ | ||
| + ('<' + ','.join([name2str(t) for t in self.type_params]) + '>' \ | ||
| if len(self.type_params) > 0 else '') \ | ||
| + '(' + ', '.join([base_name(x) + ':' + str(t) if t else x for (x,t) in self.vars])\ | ||
| + (' ' + str(self.where) + ' ' if self.where else '') \ | ||
| + '(' + ', '.join([base_name(x) + ':' + str(t) if t else x for (x,t) in self.own_vars()])\ | ||
| + ') -> ' + str(self.returns) | ||
|
|
||
| def opaque_signature(self) -> str | None: | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When a
recfun ... whereis compiled, these appended operation parameters become real function parameters becausecompiler/lower.py::lower_genrecfunlowers every entry inr.vars. However,lower_callstrips theTermInstand emits onlyCall.args, discarding itsdicts, so both external and recursive calls pass fewer arguments than the generated function expects and concept operations read missing runtime arguments. Preserve and append theTermInst.dictsduring call lowering, or otherwise erase/substitute these dictionary parameters consistently.Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, confirmed.
lower_termpeels aTermInstto its subject and dropsdicts. For--no-stdlib --compilefailed while emitting C, witharity mismatch calling twice.s4_4: declared 3, called with 1. This applies to every where function from #1291, not onlyrecfun.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
TermInstwith dictionaries, withtest/unit/test_compile_where.pypins this for afunand arecfun.