Compile Time and Runtime
What the evaluator proves, what becomes a runtime check, and how release mode changes the answer.
Contents
Haze has two contract-checking engines. The compile-time evaluator runs after type checking and folds what it can. The generated program contains runtime checks for everything the evaluator could not decide. Understanding the boundary is the key to writing contracts that catch bugs early without turning every call into a runtime branch.
The Pipeline#
The compiler parses the program, resolves modules, checks types, and then folds the checked program. Folding evaluates module constants to a fixed point, checks definition-time contracts, and evaluates pure function bodies where possible. A contract that evaluates to false during folding is a compile error. A contract that evaluates to true is discharged. A contract that cannot be evaluated is kept in the function's clause list and emitted as a runtime check.
const LIMIT i32 = 10
fn below(n i32) i32
: pre n < LIMIT
=> n
fn main() {
print(below(3)) // folded and accepted
print(below(12)) // compile-time error
}
The constant LIMIT is folded first, so below(3) and below(12) are both evaluated at compile time. The same contract on a variable argument would remain a runtime check.
What the Evaluator Supports#
| Construct | Evaluated at compile time | Note |
|---|---|---|
| Integer, float, boolean, string literals | Yes | Fixed-width arithmetic |
| Variables in the constant environment | Yes | Parameters are unknown unless constant |
Unary -, ~, ! |
Yes | Type-aware |
| Binary arithmetic, comparison, bitwise, shifts | Yes | Wrapping at the operand width |
&&, || |
Yes | Short-circuit |
if |
Yes | Only the selected branch |
| Block expression | Yes | Final expression is the value |
cast, trunc |
Yes | Fixed-width conversion |
| Calls to non-generic functions with known arguments | Preconditions checked; pure body evaluated | Impure body not evaluated |
| Field access, indexing | No | Remains a runtime check |
| Record literal, list literal | No | Remains a runtime check |
match, is |
No | Remains a runtime check |
| Interpolation, lambda | No | Remains a runtime check |
| Generic function body | Checked per instantiation | Only bounded bodies are checked at declaration |
The evaluator has a recursion depth limit of 512 and a loop iteration limit of 100,000. A computation beyond those limits is not folded; the contract remains a runtime check. This is a safety valve, not an error.
The Decision Matrix#
| Contract | Compile-time result | What happens |
|---|---|---|
| Precondition at a call site | All arguments known and true | Accepted; the call may still have a runtime check if the clause survives |
| Precondition at a call site | All arguments known and false | Compile error at the call site |
| Precondition at a call site | Not evaluable | Runtime check before the body |
| Precondition at definition | Constant false independent of parameters | Compile error |
| Precondition at definition | Constant true independent of parameters | Clause removed |
| Postcondition on a pure function | Body and result known and false | Compile error |
| Postcondition on a pure function | Not evaluable | Runtime check at each return |
| Postcondition on an impure function | Never body-evaluated | Runtime check |
| Postcondition | Constant false independent of the result | Compile error |
| Normal construction | Constants and invariant false | Compile error |
| Normal construction | Constants and invariant true | Accepted, no runtime check |
| Normal construction | Runtime value | Compile error; use ? |
| Checked construction | Provable | Warning: redundant check |
| Checked construction | Known false | Compile error |
| Checked construction | Runtime value | Runtime check; error on failure |
| Loop invariant | Constant false | Compile error |
| Loop invariant | Constant true | Clause removed |
| Loop invariant | Not decidable | Runtime check at the loop header |
A contract that is true at compile time does not necessarily disappear from the function. If the clause depends on parameters and survives folding, a runtime call may still re-check it. The check is cheap for a simple predicate and protects the function when it is called with a value the compiler did not see.
Runtime Failures#
A failed runtime precondition, postcondition, or loop invariant panics. It does not return an error value. This is the correct semantics for a contract: the caller has violated the function's domain, or the implementation has violated its guarantee. Such a failure is a bug, not an expected outcome.
Expected failure belongs in Result. A function that may fail should return a Result and let the caller handle the error. A function that returns a plain value has promised that it cannot fail for inputs in its stated domain.
Release Mode#
The -release flag removes runtime preconditions, postconditions, loop invariants, and checked-construction invariant checks. It does not disable compile-time folding, and it does not remove index bounds checks. A constant-false contract is still a compile error, a constant-false invariant is still a compile error, and a normal construction that cannot be verified is still rejected.
| Check | Debug build | -release |
|---|---|---|
| Compile-time contract folding | Yes | Yes |
| Compile-time definition checks | Yes | Yes |
| Runtime preconditions | Yes | No |
| Runtime postconditions | Yes | No |
| Runtime loop invariants | Yes | No |
| Runtime checked-construction invariants | Yes | No |
| Index bounds checks | Yes | Yes |
assert in tests |
Yes | Yes |
Release mode is a performance decision. It is safe only when the program has been verified by testing and by reasoning outside the compiler. It is not a way to make a failing contract compile.
Why the Evaluator Is Concrete#
The evaluator does not perform range analysis or symbolic reasoning. A contract such as r >= 0 && r <= n is checked by evaluating r and n when both are known. It is not used to prove that a multiplication cannot overflow or that a loop counter stays in range. This is deliberate: a more clever evaluator makes the language's behaviour depend on the cleverness of the compiler, and a future optimization could change which programs compile.
The practical consequence is that a contract should state a property the evaluator can decide when the inputs are constants, and it should remain meaningful as a runtime check otherwise. Avoid contracts that require a theorem prover to establish; prefer a helper function that computes the property and returns bool.
Contract Placement#
Place a precondition where the obligation belongs to the caller, and a postcondition where the guarantee belongs to the callee. A precondition that is really a type invariant should be moved to a refinement type. A postcondition that is really a property of a value should be moved to a record invariant. A contract that restates the return type adds noise; a contract that states a relationship between input and output earns its place.
Debugging a Contract#
When a contract fails at compile time, the diagnostic points at the clause and, when the failure comes from a call site, at the call. The primary message names the failing comparison and the values that made it false. When a contract fails at runtime, the panic message names the kind of check. The usual causes are an argument outside the documented domain, a parameter mutated after the precondition was checked, or a postcondition that refers to a field the implementation did not set as expected.
The first step is to determine which side of the contract is wrong. If the caller can legitimately pass the value, the precondition is too strong. If the callee can legitimately return the value, the postcondition is too strong. If neither is true, the implementation is wrong. Contracts are a design tool, not an oracle; a failing contract is a question about the interface, not just a bug report.