Named Contracts
Reusable clauses, composition, application, substitution, and visibility.
Contents
A named contract packages a set of clauses under a name. The name can be applied to several functions, composed with other contracts, and exported from a module. Named contracts are the main tool for keeping obligations consistent across a codebase.
Declaring a Contract#
A contract declaration gives the contract a name, a parameter list, and a body of pre and post clauses. Inside the body, a block after pre or post is a sequence of separate conjunctive clauses.
contract FactorialCond(n i32) {
pre {
n >= 0
n <= 12
}
post r => {
r >= 1
}
}
The example above is equivalent to two preconditions, n >= 0 and n <= 12, plus one postcondition, r >= 1. Each expression in the block is a separate clause. Function-level contract blocks use the same conjunctive rule, so the two forms compose without a surprise.
A contract parameter is an ordinary typed parameter. A parameter cannot have type void. Contract declarations do not accept generic type parameters; a reusable contract over a parameterized type must use a concrete type or inline clauses.
contract InRange(value i32, low i32, high i32) {
pre low <= high
pre value >= low
pre value <= high
}
Contract names are private by default and may be marked pub. They may be declared before or after the functions that use them. All contract declarations are registered before checking, so a forward reference is valid.
Applying a Contract#
Apply a contract in a function signature with : Name(arguments). The application may be mixed with inline clauses.
contract NonNegative(x i32) {
pre x >= 0
}
fn take(x i32) i32
: NonNegative(x)
: post r => r >= x
=> x
The arguments are type-checked against the contract's parameters, and their number must match. A mismatch is reported at the application.
fn bad(x i32) i32
: NonNegative(x, x) // error: contract expects one argument
=> x
The contract's clauses are expanded with the supplied arguments substituted for the contract parameters. The expansion is capture-safe: a parameter name in the contract is replaced by the argument expression without capturing a local of the same name in the function.
contract Below(limit i32, value i32) {
pre value < limit
}
fn small(n i32) i32
: Below(100, n)
=> n
The expansion of Below(100, n) is the clause n < 100. The function's parameter n is substituted for value, and the literal 100 is substituted for limit.
Composition#
A contract may extend one or more base contracts. The base clauses are collected recursively and placed before the derived contract's own clauses. This lets a shared obligation be stated once and extended by a more specific contract.
contract NonNegative(x i32) {
pre x >= 0
}
contract Positive(x i32) : NonNegative(x) {
pre x > 0
}
contract SmallPositive(x i32) : Positive(x) {
pre x < 10
}
fn take(x i32) i32 : SmallPositive(x) => x
A call to take must satisfy x >= 0, x > 0, and x < 10. The redundant x >= 0 is harmless and documents the lineage. A recursive composition, where a contract eventually includes itself, is rejected with a clear error.
Base contract arguments are checked at declaration time, even if the derived contract is never used. This catches a mismatched base before it becomes a confusing expansion error at a call site.
Expansion, Not Conformance Proof#
Applying a contract is best understood as clause expansion with substitution. The compiler does not create a separate proof obligation that the function "implements" the named contract beyond checking the expanded clauses. In practice this gives the same benefit: the function's callers see the clauses, and a violation is reported by the ordinary compile-time or runtime checks.
The distinction matters in two places. First, an argument expression is substituted into every clause, so if the expression has an effect it may be evaluated more than once. Keep contract arguments pure and cheap. Second, a contract is a statement about a function's interface, not a trait or a type class; it cannot be used to select an implementation or to dispatch at runtime.
Declaration Checking#
Every contract declaration is checked, whether or not it is used. Names in clauses must resolve to contract parameters, module constants, functions, constructors, or types. A free variable or an unknown name is an error. pre clauses and post clauses without a result name must be bool.
contract Broken(n i32) {
pre missing(n) // error: undefined name 'missing'
}
contract NotBoolean(n i32) {
pre n + 1 // error: contract expression must be bool
}
A post clause with a result name has the result name in scope. The name is bound to the function's return type when the contract is expanded into a function. If the contract is never expanded, the result name is still validated for uniqueness and scope, but its type is not checked until the application.
Contract Composition Rules#
| Rule | Behaviour |
|---|---|
| Multiple bases | Comma-separated after the first colon |
| Base arguments | Type-checked at declaration |
| Base order | Bases come before derived clauses |
| Recursive composition | Rejected |
| Duplicate clauses | Allowed; each is checked independently |
| Generic parameters | Not allowed in a contract declaration |
| Visibility | pub exports the contract to other modules |
| Forward references | Allowed |
Contracts on Trait Methods#
Trait methods may declare contracts, and default method bodies carry those contracts when the default is instantiated for an implementation. When an implementation provides its own method, the trait's clauses are copied onto that method automatically, with the trait parameter names mapped to the implementation's parameter names. An implementation may add its own clauses as well; all of them are checked together.
trait Number[T] {
pub fn add(x, y T) T
}
type Pos {
value i64
}
impl Pos of Number {
pub fn add(x, y Self) Self
: pre x.value >= 0
: post r => r.value >= x.value
=> Self { value = x.value + y.value }
}
Naming and Reuse#
A named contract should name a domain rule, not a single clause. Funded, Solvent, and InRange describe an obligation a caller can reason about. GreaterThanZero is a type invariant hiding in a contract; move it to a refinement type instead.
Package clauses that travel together. A transfer obligation naturally includes a positive amount, a sufficient source balance, and a destination with room. A caller that can establish all three should be able to apply one name rather than repeat three clauses at every call site. A contract that is used once and encodes a local detail is usually clearer inline.
Summary#
| Operation | Syntax | Effect |
|---|---|---|
| Declare | contract Name(params) { ... } |
Defines a reusable clause bundle |
| Extend | contract Name(...) : Base(...) { ... } |
Includes base clauses first |
| Apply | fn f(...) : Name(args) |
Expands clauses with substitution |
| Export | pub contract Name(...) |
Makes the contract visible to other modules |
| Declare a result name | post r => expr |
Binds the result inside the clause |
| Group declaration clauses | pre { expr expr } |
Each expression is a separate conjunct |
| Group function clauses | pre { expr expr } |
Each expression is a separate conjunct |