Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Deduce.lark
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,7 @@ imp_block: "{" imp_stmt_list "}" -> imp_block
| visibility "fun" identifier type_params_opt "(" variable_list ")" "{" term "}" -> fun
| visibility "fun" identifier "<" identifier_list ">" "where" "{" concept_ref_list "}" "(" variable_list ")" "{" term "}" -> fun_where
| visibility "recfun" identifier type_params_opt "(" variable_list ")" "->" type "measure" term "of" type "{" term "}" "terminates" proof -> general_recursive_function
| visibility "recfun" identifier "<" identifier_list ">" "where" "{" concept_ref_list "}" "(" variable_list ")" "->" type "measure" term "of" type "{" term "}" "terminates" proof -> general_recursive_function_where
| visibility "proc" identifier type_params_opt "(" proc_param_list ")" proc_return_opt proc_spec_list imp_block proc_proof_opt -> proc_decl
| visibility "observer" identifier type_params_opt "(" proc_param_list ")" "->" type observer_reads_list observer_body_opt -> observer_decl
| visibility "resource" identifier type_params_opt "(" proc_param_list ")" resource_body_opt -> resource_decl
Expand Down
53 changes: 40 additions & 13 deletions abstract_syntax/declarations.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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 '') \
Expand Down Expand Up @@ -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)
Expand All @@ -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)

Expand All @@ -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

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 +1426 to +1427

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>


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

Expand All @@ -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:
Expand Down
7 changes: 5 additions & 2 deletions abstract_syntax/terms.py
Original file line number Diff line number Diff line change
Expand Up @@ -1424,8 +1424,11 @@ def reduce(self, env: Env) -> Term:
typarams = gen_fun.type_params
params = gen_fun.vars
body = gen_fun.body
subst2: dict[str, Term | Type] = {k: v for ((k,t),v) in zip(params, args)}
ret = do_function_call(loc, name, typarams, type_args, [x for (x,t) in params], args,
# The operations for a where clause come after the arguments
# (#1298), as for `RecFun`.
all_args = args + fun.dicts
subst2: dict[str, Term | Type] = {k: v for ((k,t),v) in zip(params, all_args)}
ret = do_function_call(loc, name, typarams, type_args, [x for (x,t) in params], all_args,
body, subst2, env, None,
display_args=args)

Expand Down
74 changes: 69 additions & 5 deletions checker_concepts.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@
from typing import Callable, cast

from abstract_syntax import (
All, AllElimTypes, And, BoolType, ConceptBinding, ConceptDecl,
All, AllElimTypes, And, BoolType, Call, ConceptBinding, ConceptDecl,
ConceptMember, ConceptMemberProof, ConceptRef, Env,
Formula, IfThen, ModelBinding, ModelDecl, Proof, ResolvedVar, Term, Type,
FunctionType, TypeInst, TypeType, VarRef, WhereClause, WhereMember, base_name, concept_closure,
where_body,
)
from checker_types import check_formula, check_type, type_check_term
from error import user_error
from error import UserError, user_error, wrap_user_error
from lark.tree import Meta

# The term that stands for a member of a concept instance: a where
Expand Down Expand Up @@ -201,10 +201,23 @@ def intro_where(loc: Meta, goal: All, x: str, where: WhereClause,
frm = goal.body.substitute({goal.var[0]: ResolvedVar(loc, None, x)})
frm = _strip_operations(loc, frm, goal_where, lookup)
frm = _discharge(loc, frm, _premise(body_env, where, lookup, loc))
return frm, _assume_where(loc, where, body_env, lookup)


def where_scope(loc: Meta, where: WhereClause, env: Env) -> Env:
"""The scope of the (checked) where clause ``where``, as after
`arbitrary T:type where {...}`: its operations, its theorems as
givens, and its models."""
env = declare_where_operations(where, env, loc)
return _assume_where(loc, where, env, _binder_lookup(env, where, loc))


def _assume_where(loc: Meta, where: WhereClause, env: Env,
lookup: MemberLookup) -> Env:
for wm in where.theorems():
body_env = body_env.declare_local_proof_var(
loc, wm.binder, axiom(body_env, wm.instance, wm.member, lookup, loc))
return frm, add_where_models(loc, where, body_env)
env = env.declare_local_proof_var(
loc, wm.binder, axiom(env, wm.instance, wm.member, lookup, loc))
return add_where_models(loc, where, env)


def add_where_models(loc: Meta, where: WhereClause, env: Env) -> Env:
Expand Down Expand Up @@ -398,6 +411,57 @@ def check_concept_member(term: ConceptMember, env: Env) -> Term:
return _unfold(var, [model], env)


def well_founded_less(less: ConceptMember, env: Env) -> Term:
"""The `<` that a recfun's measure decreases along (#1308): the
operation of the model of ``WellFounded<τ>`` in scope. The concept must
be the concept of well-founded orders: one operation `<` and the
theorem that `<` admits strong induction, which every model proves."""
loc = less.location
decl = concept_of(env, less.ref.concept, loc)
if not _is_well_founded(decl):
user_error(loc, 'the concept ' + base_name(decl.name) + ' in scope is not'
+ ' the concept of well-founded orders (lib/WellFounded.pf):'
+ ' it needs one operation `<` and the theorem\n\t'
+ '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)')
try:
return check_concept_member(less, env)
except UserError as e:
raise wrap_user_error(e, '\nA recfun\'s measure decreases along the `<`'
+ ' of a model of WellFounded at its type, which'
+ ' proves that `<` is well-founded.')


def _is_well_founded(decl: ConceptDecl) -> bool:
if len(decl.type_params) != 1 or len(decl.funs) != 1:
return False
(lt, lt_ty) = decl.funs[0]
if base_name(lt) != '<' or not isinstance(lt_ty, FunctionType) \
or lt_ty.type_params or len(lt_ty.param_types) != 2 \
or not isinstance(lt_ty.return_type, BoolType):
return False
t = lt_ty.param_types[0]
if not (isinstance(t, VarRef) and t.get_name() == decl.type_params[0]
and lt_ty.param_types[1] == t):
return False
loc = decl.location
bool_ty = BoolType(loc)
pred_ty = FunctionType(loc, [], [t], bool_ty)
def var(x: str, ty: Type) -> Term:
return ResolvedVar(loc, ty, x)
def call(f: Term, args: list[Term]) -> Formula:
return Call(loc, bool_ty, f, args)
p, n, j, i = var('P', pred_ty), var('n', t), var('j', t), var('i', t)
smaller = All(loc, bool_ty, ('i', t), (0, 1),
IfThen(loc, bool_ty, call(var(lt, lt_ty), [i, j]), call(p, [i])))
step = All(loc, bool_ty, ('j', t), (0, 1),
IfThen(loc, bool_ty, smaller, call(p, [j])))
strong = All(loc, bool_ty, ('P', pred_ty), (1, 2),
All(loc, bool_ty, ('n', t), (0, 2),
IfThen(loc, bool_ty, step, call(p, [n]))))
return any(frm == strong for (_, frm) in decl.theorems)


def concept_member_formula(proof: ConceptMemberProof, env: Env) -> Formula:
"""`@C<τ>.thm` outside a where clause binding ``C<τ>``: the concept's
theorem at the operations of the model in scope (which proved it)."""
Expand Down
42 changes: 29 additions & 13 deletions checker_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

from checker_concepts import (
add_where_models, check_where, collect_model, model_obligations,
well_founded_less, where_scope,
process_concept, process_model,
)
from abstract_syntax import (
Expand Down Expand Up @@ -1009,12 +1010,10 @@ def process_declaration_visibility(decl: Declaration, env: Env,
user_error(loc, 'Add type annotations to the parameters.')
checked_param_types: List[Type] = [cast(Type, t) for t in param_types]

fun_type = FunctionType(loc, typarams, checked_param_types, checked_returns)
# print('process declaration:')
# print(decl.pretty_print(4))
check_type(measure_ty, env)
# return? GenRecFun(loc, name, typarams, params, returns, measure, measure_ty, body, terminates)
# changed to decl
where = check_where(decl.where, body_env) if decl.where else None
fun_type = where_fun_type(loc, typarams, checked_param_types,
checked_returns, where)
check_type(measure_ty, body_env)
return (decl, env.declare_term_var(loc, name, fun_type,
visibility=decl.visibility))

Expand Down Expand Up @@ -1742,16 +1741,19 @@ def _type_check_stmt(stmt: Statement, env: Env,
for (x, p) in param_pairs]
checked_returns = check_type(returns, body_env)
checked_measure_ty = check_type(measure_ty, body_env)
where = check_where(stmt.where, body_env) if stmt.where else None

fun_type = _alpha_renamed_function_type(
loc, typarams, [t for (_, t) in checked_param_pairs],
checked_returns)
checked_returns, where)

env = env.define_term_var(loc, name, fun_type, stmt.reduce(env),
stmt.visibility)

body_env = env.declare_type_vars(loc, typarams)
body_env = body_env.declare_term_vars(loc, checked_param_pairs)
if where is not None:
body_env = add_where_models(loc, where, body_env)
new_measure = type_check_term(measure, checked_measure_ty, body_env, None, [])

new_body = type_check_term(body, checked_returns, body_env, None, [])
Expand All @@ -1760,7 +1762,8 @@ def _type_check_stmt(stmt: Statement, env: Env,
checked_returns, new_measure, checked_measure_ty,
new_body, terminates,
stmt.trusted_terminates,
visibility=stmt.visibility, less=stmt.less)
visibility=stmt.visibility, less=stmt.less,
where=where)
# print('type check stmt:')
# print(new_recfun.pretty_print(4))
return new_recfun
Expand Down Expand Up @@ -1865,7 +1868,8 @@ def collect_env(stmt: Statement, env: Env) -> Env:

case GenRecFun(loc, name, typarams, params, returns, _, _,
body, _):
fun_type = FunctionType(loc, typarams, [t for (x,t) in params], returns)
fun_type = where_fun_type(loc, typarams, [t for (x,t) in params],
returns, stmt.where)
return env.define_term_var(loc, name, fun_type, stmt,
stmt.visibility)

Expand Down Expand Up @@ -2186,7 +2190,17 @@ def _check_proofs(stmt: Statement, env: Env) -> None:
if stmt.trusted_terminates:
return
body_env = env.declare_type_vars(loc, typarams)

# The obligation quantifies over the function's own parameters; the
# where clause's operations and theorems are in scope (#1298).
params = stmt.own_vars()
if stmt.where is not None:
body_env = where_scope(loc, stmt.where, body_env)

# The measure decreases along the `<` of the model of WellFounded at
# its type (#1308).
assert stmt.less is not None
less = well_founded_less(stmt.less, body_env)

# find recursive calls in the body
calls = find_rec_calls(name, body, body_env)
formulas: list[Formula] = []
Expand All @@ -2195,9 +2209,8 @@ def _check_proofs(stmt: Statement, env: Env) -> None:
for call in calls:
lhs = cast(Term, measure.substitute({x: arg for ((x,t),arg) in zip(params,call.args)}))
rhs = measure.copy()
less = OverloadedVar(loc, None, stmt.less)
# `Call` is a Term in the class hierarchy but acts as a Formula
# when its return type is Bool (here: `<` overloads).
# when its return type is Bool.
less_frm = cast(Formula, Call(loc, None, less, [lhs,rhs]))
condition = And(loc, None, call.conditions) \
if len(call.conditions) > 0 else None
Expand All @@ -2218,8 +2231,11 @@ def _check_proofs(stmt: Statement, env: Env) -> None:
formula = formulas[0]
else:
user_error(loc, 'There were no recursive calls in the body of this recfun')
# Quantify over the parameters the obligation mentions.
occurring = formula.free_vars()
for (x,t) in reversed(params):
formula = All(loc, None, (x,t), (0,1), formula)
if x in occurring:
formula = All(loc, None, (x,t), (0,1), formula)
formula = check_formula(formula, body_env)

# check that the terminates proof proves the above formula
Expand Down
2 changes: 1 addition & 1 deletion checker_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -1255,7 +1255,7 @@ def type_synth_term(
case GenRecFun(loc, name, typarams, params, returns, _, _,
body, _):
param_types = [t for (p,t) in params]
fun_type = FunctionType(loc, typarams, param_types, returns)
fun_type = where_fun_type(loc, typarams, param_types, returns, term.where)
ret = term
term.typeof = fun_type

Expand Down
11 changes: 11 additions & 0 deletions compiler/lower.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,11 @@ def lower_stmt(self, s: ast.Statement, module: str) -> Optional[ir.TopLevel]:
or isinstance(s, ast.Export) or isinstance(s, ast.Associative) \
or isinstance(s, ast.Trace):
return None
if isinstance(s, (ast.ConceptDecl, ast.ModelDecl)):
# No code of their own: the standard library's models of
# WellFounded only order recfun measures, in proofs (#1308).
# Passing a model's operations at run time is #1292.
return None
if isinstance(s, ast.Import):
# Imports are lowered to a flat statement list upstream
# (the proof checker walks them via `Import.ast`); the
Expand Down Expand Up @@ -433,6 +438,12 @@ def lower_term(self, t: ast.Term) -> ir.Term:
if isinstance(t, ast.Mark):
return self.lower_term(t.subject)
if isinstance(t, ast.TermInst):
if t.dicts:
raise CompileError(
t.location,
"compiler does not yet pass the operations of a where "
f"clause, in the use of {t.subject} (issue #1292)",
)
return self.lower_term(t.subject)
if isinstance(t, ast.Generic):
return self.lower_term(t.body)
Expand Down
2 changes: 1 addition & 1 deletion examples/replicate.pf
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ recfun replicate<T>(n : UInt, x : T) -> List<T>
else node(x, replicate(n ∸ 1, x))
}
terminates {
arbitrary n:UInt, x:T
arbitrary n:UInt
uint_monus_one_less[n]
}

Expand Down
Loading