Contracts
The design-by-contract model, clause kinds, and compiler guarantees.
Contents
A contract is a boolean expression stating an obligation or guarantee, written next to the code it describes. Contracts are evaluated at compile time when the compiler has enough information, at runtime otherwise, and are removed by -release.
Contract Forms#
| Form | Where it appears | What it states |
|---|---|---|
| Precondition | : pre expr on a function or trait method |
What the caller must establish before the call |
| Postcondition | : post r => expr on a function or trait method |
What the callee guarantees about the result and final state |
| Named contract | contract Name(...) { ... }, applied with : Name(args) |
A reusable bundle of preconditions and postconditions |
| Type invariant | : invariant expr on a refinement or record |
Which values of the type are valid |
| Loop invariant | : invariant expr on a for loop |
What remains true at the loop header |
assert is not a contract form; it is a test-only statement. See Testing Contracts.
Responsibility#
A precondition belongs to the caller. It states that the function is not required to handle inputs outside the stated domain. A postcondition belongs to the callee. It states that every accepted call produces a result satisfying the stated property. A type invariant belongs to the value. It states that a constructed value is valid and that an invalid value cannot exist.
type Account {
id i32
balance i64
}
contract Funded(account Account, amount i64) {
pre amount > 0
pre amount <= account.balance
}
fn withdraw(account Account, amount i64) Account
: Funded(account, amount)
: post r => r.id == account.id
: post r => r.balance == account.balance - amount
=> Account { id = account.id, balance = account.balance - amount }
Compile Time and Runtime#
The compiler contains a constant evaluator that runs after type checking. It folds module constants, evaluates pure functions with constant arguments, and checks every contract it can decide. A contract that evaluates to false is an error. One that evaluates to true is discharged. One that cannot be decided becomes a runtime check.
fn half(n i32) i32
: pre n % 2 == 0
: post r => r * 2 == n
=> n / 2
fn main() {
print(half(10)) // checked at compile time
print(half(7)) // compile-time error: precondition fails
}
fn show(n i32) {
print(half(n)) // runtime check
}
The evaluator does not perform range analysis, relational reasoning, or symbolic algebra. It evaluates constants, pure function calls, arithmetic, comparisons, short-circuit logic, and a few control-flow expressions. Anything else remains a runtime check. See Compile Time and Runtime.
Failure#
A failed runtime precondition, postcondition, or loop invariant aborts the program. A contract violation is a programming error and is not modelled as a value.
Expected failure is modelled with Result. Checked construction T { ... }? returns an error value when an invariant does not hold.
| Situation | Mechanism | Failure behaviour |
|---|---|---|
| Input outside the function's domain | Precondition | Abort (caller bug) |
| Callee breaks its guarantee | Postcondition | Abort (implementation bug) |
| Loop does not maintain its invariant | Loop invariant | Abort (implementation bug) |
| Expected operation failure | Result[T, E] |
Returned as Error |
| Expected absence | Option[T] |
Returned as None |
| Runtime value may violate a type invariant | T { ... }? |
Returned as Error |
Scope#
A precondition or postcondition may refer to parameters, module constants, types, constructors, and functions. A postcondition may refer to result names introduced after post. Result names are bound to the returned value and are visible in every postcondition of the function.
pure fn next(n i32) i32
: post r => r == n + 1
: post s => s == r
=> n + 1
Both r and s are bound to the result. A result name may not duplicate a parameter name.
A type invariant uses self for a refinement and field names for a record. A loop invariant uses the loop variable and names visible at the loop header. There is no implicit result variable and no old expression. A postcondition sees final parameter values, not pre-state.
Named Contracts#
: FactorialCond(n) applies a named contract. The compiler expands its clauses with the supplied arguments and checks them as if written inline. Expansion is capture-safe; recursive composition is rejected.
There is no separate conformance proof. The function receives the clauses, and the ordinary compile-time and runtime checks decide whether they hold.
Contracts and Types#
A type invariant makes an invalid value unrepresentable. A precondition states an obligation that depends on the call context. A postcondition states a relationship between input and output.
Prefer types where they suffice. A positive integer belongs in a refinement type. A sorted list belongs in a postcondition. A valid transfer belongs in a combination of record invariants, preconditions, and postconditions.