A Tour of Temporal Typing Code, Contracts & Types · 3/6
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:

PositionMeaning
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!
Click Check (or press Shift+Enter) to run the front-end and check this specification. Switch to TLA⁺ to view its TLA⁺ meaning.
---- MODULE Wallet ----
EXTENDS Integers
CONSTANT max_balance
VARIABLE balance

ASSUME max_balance > 0
Init == balance = 0
Deposit(amount) == amount > 0 /\ balance + amount <= max_balance /\ balance' = balance + amount
Spend(amount)   == amount > 0 /\ amount <= balance /\ balance' = balance - amount
Next == (\E a \in 1..3 : Deposit(a) \/ Spend(a))

BalanceNonNegative == balance >= 0
====