Contents

Contract clauses are placed between the return type and the body. Each clause begins with a colon and is an inline pre, an inline post, or a named contract application.

Preconditions#

A precondition is : pre expression. The expression must be bool and is evaluated in the function's parameter scope plus module scope.

fn divide(numerator i32, denominator i32) i32
    : pre denominator != 0
=> numerator / denominator

Repeated preconditions are conjunctive.

fn narrow(n i32) i32
    : pre n >= 0
    : pre n <= 10
    : pre n != 5
=> n

A precondition may call a pure helper, which is evaluated at compile time when its arguments are known.

pure fn even(n i32) bool => n % 2 == 0

fn half(n i32) i32
    : pre even(n)
    : post r => r * 2 == n
=> n / 2

An impure helper makes the clause a runtime check.

fn check(n i32) bool {
    print(n)
    return n > 0
}

fn need_positive(n i32) i32
    : pre check(n)
=> n

Use an impure helper only when the check depends on runtime state.

Postconditions#

A postcondition is : post expression or : post name => expression. The optional name binds the returned value inside the clause.

fn absolute(n i32) i32
    : post r => r >= 0
    : post r => r == n || r == -n
{
    if n < 0 {
        return -n
    }
    return n
}

A postcondition without a result name is an ordinary predicate over parameters, globals, and functions.

fn require_positive(n i32) i32
    : pre n > 0
    : post n > 0
=> n

Every distinct result name is bound to the same returned value and is in scope in every postcondition of the function.

pure fn increment(n i32) i32
    : post r => r == n + 1
    : post next => next == r
=> n + 1

A result name may not duplicate a parameter name or another result name. Result names are not visible in the body or in preconditions.

Mutable Parameters#

There is no old expression. A postcondition sees parameter values as they are when the function returns.

fn clear(n i32) i32
    : post n == 0
{
    n = 0
    return n
}

To describe a transition, avoid mutating the parameter.

fn cleared(_n i32) i32
    : post r => r == 0
=> 0

Clause Blocks#

A block lists several conjunctive clauses without repeating the : pre prefix.

fn allow(n i32) i32
    : pre {
        n > 0
        n < 100
        n != 42
    }
=> n

This is equivalent to:

fn allow(n i32) i32
    : pre n > 0 && n < 100 && n != 42
=> n

The same rule applies to postconditions and to named contract declarations. See Named Contracts.

Boolean Expressions#

Every precondition and postcondition must have type bool.

fn invalid(n i32) i32
    : pre n + 1          // error: contract expression must be bool
=> n

Definition-Time Checks#

The compiler evaluates contracts even when a function is never called, as long as the contract does not depend on parameters. A contract that is always false is a compile error.

fn impossible() i32
    : pre 1 == 2
=> 1
pure fn also_impossible() i32
    : post r => 1 == 2
=> 1

A contract that depends on parameters is checked at each call or at runtime. The compiler does not reject a function merely because it cannot prove the postcondition for all inputs.

Call-Site Checks#

A call with constant arguments is checked against the callee's preconditions. A false precondition is a compile error pointing at the call and the clause.

fn checked(n i32) i32
    : pre n > 0
=> n

fn main() {
    print(checked(0))      // compile-time error
}

With non-constant arguments, the precondition remains in the callee and is checked at runtime.

Purity and Folding#

A pure function is evaluated during compile-time folding when its arguments are known. An impure function is only checked for its own preconditions.

The evaluator supports literals, variables, unary and binary operators, casts, short-circuit logic, if, block expressions, and calls to non-generic functions with known arguments. It does not evaluate field access, indexing, record literals, list literals, match, is, interpolation, or lambdas.

type Account {
    id      i32
    balance i64
}

pure fn solvent(account Account) bool => account.balance >= 0

fn deposit(account Account, amount i64) Account
    : pre amount > 0
    : post r => r.balance == account.balance + amount
=> Account { id = account.id, balance = account.balance + amount }

The postcondition above refers to fields and a record literal, so it is a runtime check even when every argument is constant. See Compile Time and Runtime.

Generic Functions#

A generic function may carry inline clauses. Clauses are checked per instantiation after substitution.

fn require_non_empty[T](values List[T]) List[T]
    : pre values.len > 0
    : post r => r.len >= 1
=> values

A contract declaration cannot declare type parameters. Write clauses inline or specialize the contract to a concrete type.

Trait Methods#

Trait method declarations may carry clauses. When an implementation supplies 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 }
}

Common Mistakes#

Mistake Consequence Remedy
Writing a block when one predicate is intended Each expression becomes a separate conjunct Use && inside one expression
Referring to old No such expression exists Describe the result or use a separate function
Mutating a parameter Postconditions see the mutated value Return a new value instead
Calling an impure helper Clause becomes a runtime check Use a pure predicate
Duplicating a type invariant in every precondition Noise and drift Let the type carry the property
Writing a postcondition that restates the return type Little value State a relationship between input and output
Using -release to hide a violated contract Undefined behaviour in optimized builds Fix the contract or the implementation