Skip to content
Merged
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
23 changes: 16 additions & 7 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 @@ -1373,10 +1373,11 @@ 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`
Expand Down Expand Up @@ -1407,7 +1408,15 @@ 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')
Comment thread
jsiek marked this conversation as resolved.
Resolved
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:
Expand All @@ -1417,7 +1426,7 @@ def uniquify(self, env: object, ctx: object) -> GenRecFun:
new_vars, new_returns, new_measure,
new_measure_ty, new_body, new_terminates,
self.trusted_terminates, visibility=self.visibility,
less=list(env_map.get('<', [])), where=where)
less=less, where=where)

_exports_overload = True

Expand Down
55 changes: 53 additions & 2 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 @@ -411,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
16 changes: 8 additions & 8 deletions checker_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

from checker_concepts import (
add_where_models, check_where, collect_model, model_obligations,
where_scope,
well_founded_less, where_scope,
process_concept, process_model,
)
from abstract_syntax import (
Expand Down Expand Up @@ -1013,11 +1013,7 @@ def process_declaration_visibility(decl: Declaration, env: Env,
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)
# 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
check_type(measure_ty, body_env)
return (decl, env.declare_term_var(loc, name, fun_type,
visibility=decl.visibility))

Expand Down Expand Up @@ -2200,6 +2196,11 @@ def _check_proofs(stmt: Statement, env: Env) -> None:
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 @@ -2208,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 Down
5 changes: 5 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
25 changes: 22 additions & 3 deletions gh_pages/doc/Reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -2655,9 +2655,28 @@ assert replicate(3, 7) = [7, 7, 7]
* The parameters are written with their types, as for `fun`, followed
by `->` and the return type.
* `measure m of τ` gives the measure `m`, a term over the parameters,
and its type `τ`. The measure decreases by `<` on `τ`, which must be
well-founded, so use `UInt` or `Nat`. Deduce doesn't check this yet
(issue #1308).
and its type `τ`. The measure decreases along the `<` of the
[model](#model-statement) of `WellFounded<τ>` in scope, the concept
of well-founded orders (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)
}
```

Its theorem says that `<` admits strong induction, so a measure can't
decrease forever, and every call of the `recfun` terminates. The
standard library has models for `Nat` and `UInt`. For another type,
prove strong induction for its `<` and give a model, e.g.
`model WellFounded<Steps> { theorem strong_induction = steps_strong_induction }`.
It is an error if there is no model of `WellFounded<τ>` in scope; for
example, `Int` has none, because `<` on `Int` has no least element. A
generic `recfun` can be measured by a type parameter `T` under
`where { WellFounded<T> }`.
* The body is a term, which can use `if`-`then`-`else` and `switch` to
decide when to recurse.
* The proof after `terminates` proves that the measure decreases. For
Expand Down
5 changes: 5 additions & 0 deletions gh_pages/doc/StandardLib.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,8 @@ Only the top-level module files (such as `UInt.pf`) are listed below. Each modul
axioms a file depends on.
- ([`Set.thm`](../lib/Set.thm), [`Set.pf`](../lib/Set.pf)): Sets
- ([`UInt.thm`](../lib/UInt.thm), [`UInt.pf`](../lib/UInt.pf)): Unsigned integers
- ([`WellFounded.thm`](../lib/WellFounded.thm), [`WellFounded.pf`](../lib/WellFounded.pf)):
The concept `WellFounded` of well-founded orders, whose `<` admits
strong induction. A [`recfun`](./Reference.md#recfun-statement)'s
measure decreases along the `<` of a model of it; `Nat` and `UInt`
have models.
10 changes: 7 additions & 3 deletions imperative_verifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@

import style
from abstract_syntax import (
And, ArrayGet, Bool, Call, Env, Formula, OverloadedVar, Proof, Term,
base_name, is_true,
And, ArrayGet, Bool, Call, Env, Formula, ModelBinding, OverloadedVar,
Proof, Term, base_name, is_true,
)


Expand Down Expand Up @@ -174,8 +174,12 @@ def index_in_bounds_goal(loc: Meta, handle: Term, index: Term,
writes in the symbolic state)."""
from checker_types import type_check_formula
from error import user_error
# A model's operations are reached through the model, not by name: the
# model of WellFounded<UInt> defines a `<` at UInt too (#1308).
model_ops = {define for b in env.dict.values() if isinstance(b, ModelBinding)
for (_, define, _) in b.model.funs}
length_names = [n for n in env.dict if base_name(n) == 'length']
lt_names = [n for n in env.dict if base_name(n) == '<']
lt_names = [n for n in env.dict if base_name(n) == '<' and n not in model_ops]
if not length_names or not lt_names:
user_error(loc, 'a mutable-array bounds check needs `length` and `<` in '
'scope; add `import List` and `import UInt`')
Expand Down
73 changes: 40 additions & 33 deletions lib/NatDiv.pf
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import NatMonus
import NatMult
import NatLess
import NatEvenOdd
public import WellFounded

/*
Theorems about Nat division, modulo, and divisibility.
Expand Down Expand Up @@ -236,39 +237,6 @@ proof
div2_aux_mono
end

/*
Properties of Division and Modulo
*/

// Division by iterated subtraction.
recfun operator /(n : Nat, m : Nat) -> Nat
measure n of Nat
{
if n < m then zero
else if m = zero then zero
else suc(zero) + (n ∸ m) / m
}
terminates {
arbitrary n:Nat, m:Nat
assume cond: not (n < m) and not (m = zero)
suffices m + (n ∸ m) < m + n by add_both_sides_of_less[m,n∸m,n]
suffices n < m + n by {
have m_n: m ≤ n by apply not_less_implies_less_equal to conjunct 0 of cond
replace apply monus_add_identity[n,m] to m_n.
}
obtain m' where m_sm: m = suc(m') from apply not_zero_suc to conjunct 1 of cond
suffices n < suc(m') + n by replace m_sm.
suffices n ≤ m' + n by expand operator+ | operator< | operator≤.
replace add_commute
show n ≤ n + m'
less_equal_add
}

// Modulo: the remainder left after dividing n by m.
fun operator % (n:Nat, m:Nat) {
n ∸ (n / m) * m
}

// Helper for strong_induction: P holds for every k below an arbitrary bound j.
lemma strong_induction_aux: all P: fn Nat -> bool.
if (all n:Nat. if (all k:Nat. if k < n then P(k)) then P(n))
Expand Down Expand Up @@ -324,6 +292,45 @@ proof
conclude P(n) by apply X[suc(n),n] to recall n < suc(n)
end

// `<` on Nat is well-founded, so it can order a recfun's measure (#1308).
model WellFounded<Nat> {
theorem strong_induction = strong_induction
}

/*
Properties of Division and Modulo
*/

// Division by iterated subtraction.
recfun operator /(n : Nat, m : Nat) -> Nat
measure n of Nat
{
if n < m then zero
else if m = zero then zero
else suc(zero) + (n ∸ m) / m
}
terminates {
arbitrary n:Nat, m:Nat
assume cond: not (n < m) and not (m = zero)
suffices m + (n ∸ m) < m + n by add_both_sides_of_less[m,n∸m,n]
suffices n < m + n by {
have m_n: m ≤ n by apply not_less_implies_less_equal to conjunct 0 of cond
replace apply monus_add_identity[n,m] to m_n.
}
obtain m' where m_sm: m = suc(m') from apply not_zero_suc to conjunct 1 of cond
suffices n < suc(m') + n by replace m_sm.
suffices n ≤ m' + n by expand operator+ | operator< | operator≤.
replace add_commute
show n ≤ n + m'
less_equal_add
}

// Modulo: the remainder left after dividing n by m.
fun operator % (n:Nat, m:Nat) {
n ∸ (n / m) * m
}


// Internal predicate used to prove `division` by strong induction on the dividend.
private fun DivPred(n:Nat) {
all m:Nat. if zero < m then some r:Nat. (n / m) * m + r = n and r < m
Expand Down
6 changes: 6 additions & 0 deletions lib/UIntAdd.pf
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import Nat
import UIntDefs
import UIntToFrom
import UIntLess
public import WellFounded

/*
Theorems about UInt addition.
Expand Down Expand Up @@ -773,3 +774,8 @@ proof
have Pn: P(fromNat(toNat(n))) by expand Q in Qn
conclude P(n) by replace uint_fromNat_toNat in Pn
end

// `<` on UInt is well-founded, so it can order a recfun's measure (#1308).
model WellFounded<UInt> {
theorem strong_induction = uint_strong_induction
}
14 changes: 14 additions & 0 deletions lib/WellFounded.pf
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module WellFounded

// Well-founded orders (#1308). The measure of a `recfun` must have a
// type with a model of `WellFounded`: its `<` is the order the measure
// decreases along, and `strong_induction` proves that the order has no
// infinite descending chain, so the recursion terminates. The standard
// library has models for `Nat` and `UInt`.

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)
}
4 changes: 2 additions & 2 deletions test/compile/lower/genrecfun.cir
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ union MyNat.s0_0 {zero.s0_1/0, suc.s0_2/1}
fn iszero.s1_2(n.s1_0) = match n.s1_0 { | zero.s0_1 -> true | suc.s0_2(n'.s1_1) -> false }
fn pred.s2_2(n.s2_0) = match n.s2_0 { | zero.s0_1 -> zero.s0_1 | suc.s0_2(n'.s2_1) -> n'.s2_1 }
fn <.s3_0($scr0, m.s3_1) = match $scr0 { | zero.s0_1 -> match m.s3_1 { | zero.s0_1 -> false | suc.s0_2(m'.s3_2) -> true } | suc.s0_2(n'.s3_3) -> match m.s3_1 { | zero.s0_1 -> false | suc.s0_2(m'.s3_5) -> <.s3_0(n'.s3_3, m'.s3_5) } }
fn count_down.s6_0(n.s6_1) = if iszero.s1_2(n.s6_1) then zero.s0_1 else count_down.s6_0(pred.s2_2(n.s6_1))
print count_down.s6_0(suc.s0_2(suc.s0_2(suc.s0_2(zero.s0_1))))
fn count_down.s9_0(n.s9_1) = if iszero.s1_2(n.s9_1) then zero.s0_1 else count_down.s9_0(pred.s2_2(n.s9_1))
print count_down.s9_0(suc.s0_2(suc.s0_2(suc.s0_2(zero.s0_1))))
4 changes: 2 additions & 2 deletions test/compile/lower/genrecfun.ir
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ union MyNat.s0_0 {zero.s0_1/0, suc.s0_2/1}
fn iszero.s1_2(n.s1_0) = match n.s1_0 { | zero.s0_1 -> true | suc.s0_2(n'.s1_1) -> false }
fn pred.s2_2(n.s2_0) = match n.s2_0 { | zero.s0_1 -> zero.s0_1 | suc.s0_2(n'.s2_1) -> n'.s2_1 }
fn <.s3_0($scr0, m.s3_1) = match $scr0 { | zero.s0_1 -> match m.s3_1 { | zero.s0_1 -> false | suc.s0_2(m'.s3_2) -> true } | suc.s0_2(n'.s3_3) -> match m.s3_1 { | zero.s0_1 -> false | suc.s0_2(m'.s3_5) -> <.s3_0(n'.s3_3, m'.s3_5) } }
fn count_down.s6_0(n.s6_1) = if iszero.s1_2(n.s6_1) then zero.s0_1 else count_down.s6_0(pred.s2_2(n.s6_1))
print count_down.s6_0(suc.s0_2(suc.s0_2(suc.s0_2(zero.s0_1))))
fn count_down.s9_0(n.s9_1) = if iszero.s1_2(n.s9_1) then zero.s0_1 else count_down.s9_0(pred.s2_2(n.s9_1))
print count_down.s9_0(suc.s0_2(suc.s0_2(suc.s0_2(zero.s0_1))))
Loading