Contents

A named contract packages clauses under a name. The name can be applied to several functions and composed with other contracts.

Declaring a Contract#

A contract declaration has a name, a parameter list, and a body of pre and post clauses. A block after pre or post is a sequence of conjunctive clauses.

contract FactorialCond(n i32) {
    pre {
        n >= 0
        n <= 12
    }
    post r => {
        r >= 1
    }
}

This is equivalent to two preconditions, n >= 0 and n <= 12, plus one postcondition, r >= 1.

Contract parameters are ordinary typed parameters and cannot have type void. Contract declarations do not accept type parameters.

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 their use. All contract declarations are registered before checking.

Applying a Contract#

Apply a contract with : Name(arguments). Applications 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

Arguments are type-checked against the contract's parameters, and their number must match.

fn bad(x i32) i32
    : NonNegative(x, x)      // error: contract expects one argument
=> x

Clauses are expanded with the supplied arguments substituted for contract parameters. Expansion is capture-safe.

contract Below(limit i32, value i32) {
    pre value < limit
}

fn small(n i32) i32
    : Below(100, n)
=> n

Below(100, n) expands to n < 100.

Composition#

A contract may extend base contracts. Base clauses are collected recursively and placed before the derived contract's own clauses.

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. Recursive composition is rejected.

Base contract arguments are checked at declaration time, even if the derived contract is never used.

Expansion#

Applying a contract is clause expansion with substitution. There is no separate conformance proof. 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. A contract is not a trait or type class and cannot select an implementation.

Declaration Checking#

Every contract declaration is checked, whether used or not. Names must resolve to contract parameters, module constants, functions, constructors, or types. 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 that name in scope. The name is bound to the function's return type when the contract is expanded.

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
Visibility pub exports the contract
Forward references Allowed

Trait Methods#

Trait methods may declare contracts. When an implementation provides its own method, the trait's clauses are copied onto it with parameter names mapped. An implementation may add clauses.

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. GreaterThanZero is a type invariant hiding in a contract; move it to a refinement type.

Package clauses that travel together. A contract used once and encoding a local detail is clearer inline.

Summary#

Operation Syntax Effect
Declare contract Name(params) { ... } Defines a 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