Code, Contracts & Types · Page 3 of 6
Refined Types (Where)
Annotated[T, Where(predicate, "name")] defines a refined type — the set
comprehension {x ∈ T : predicate(x)}. Define it once, and its meaning adapts
to wherever you use it:
| Position | Meaning |
|---|---|
c: Constant[Positive] | An assumption: ASSUME p(c) |
v: State[NonNegative] | An invariant: □p(v) |
def f(self, x: Positive) | A parameter guard: only matching inputs are explored |
-> Annotated[Positive, ...] | A return obligation on result |
Look at Wallet on the right: @spec() doesn't even need an explicit
Invariant clause for balance >= 0 — the type
State[NonNegative] carries that invariant automatically!
Try it: Remove
Requires(lambda s, amount: amount <= s.balance),
from spend and click Check. Even with an empty @spec(),
State[NonNegative] catches the negative balance!