A Tour of Temporal Typing Code, Contracts & Types · 5/6
Code, Contracts & Types · Page 5 of 6

Function Contracts

Not every specification is a state machine. A module-level function whose return type carries Requires or Ensures is a function contract.

Its predicates bind the function's parameters by name (and result in Ensures), and a predicate may omit parameters it does not need. For every argument tuple in the model's domains where every Requires and parameter Where holds, the checker verifies that the body returns without error and satisfies every Ensures and return-type Where.

Notice implies(p, q) in clamp: it expresses logical implication (not p or q) cleanly in predicates.

Try it: Click Check to exhaustively verify both clamp (343 input tuples) and integer_sqrt (50 inputs). Then introduce an off-by-one bug in clamp — e.g., change return low to return low - 1 — and watch the checker print the exact failing arguments!
Click Check (or press Shift+Enter) to run the front-end and check this specification. Switch to TLA⁺ to view its TLA⁺ meaning.
---- MODULE Contracts ----
EXTENDS Integers

ClampSpec(value, low, high, result) ==
    (low <= high) =>
        (low <= result /\ result <= high
         /\ (low <= value /\ value <= high => result = value))

SqrtSpec(n, result) ==
    (n >= 0) => (result >= 0 /\ result * result <= n /\ n < (result + 1) * (result + 1))
====