|
| 1 | +# Contracts & Safety |
| 2 | + |
| 3 | +Milo has built-in support for **design-by-contract** annotations and **safety profile checking**. Contracts let you state what a function expects and guarantees. The compiler type-checks them, and can export them as formal verification conditions for SMT solvers like Z3. Safety profiles enforce domain-specific coding standards (DO-178C, ISO 26262, NASA, IEC 61508, IEC 62304) at compile time. |
| 4 | + |
| 5 | +This is compile-time only — no runtime assertions, no overhead. The compiler proves your code is correct or rejects it. |
| 6 | + |
| 7 | +## Contracts |
| 8 | + |
| 9 | +Three keywords: `requires` (precondition), `ensures` (postcondition), and `invariant` (loop invariant). Each takes a boolean expression. |
| 10 | + |
| 11 | +### Preconditions — `requires` |
| 12 | + |
| 13 | +State what must be true when a function is called: |
| 14 | + |
| 15 | +```milo |
| 16 | +fn clamp(value: i64, lo: i64, hi: i64): i64 |
| 17 | + requires lo <= hi |
| 18 | +{ |
| 19 | + if value < lo { return lo } |
| 20 | + if value > hi { return hi } |
| 21 | + return value |
| 22 | +} |
| 23 | +``` |
| 24 | + |
| 25 | +Multiple `requires` clauses are allowed — all must hold: |
| 26 | + |
| 27 | +```milo |
| 28 | +fn divide(a: i64, b: i64): i64 |
| 29 | + requires b != 0 |
| 30 | + requires a >= 0 |
| 31 | +{ |
| 32 | + return a / b |
| 33 | +} |
| 34 | +``` |
| 35 | + |
| 36 | +### Postconditions — `ensures` |
| 37 | + |
| 38 | +State what the function guarantees about its return value. The special variable `result` refers to the return value: |
| 39 | + |
| 40 | +```milo |
| 41 | +fn clamp(value: i64, lo: i64, hi: i64): i64 |
| 42 | + requires lo <= hi |
| 43 | + ensures result >= lo && result <= hi |
| 44 | +{ |
| 45 | + if value < lo { return lo } |
| 46 | + if value > hi { return hi } |
| 47 | + return value |
| 48 | +} |
| 49 | +``` |
| 50 | + |
| 51 | +### Loop invariants — `invariant` |
| 52 | + |
| 53 | +State what remains true across every iteration of a loop: |
| 54 | + |
| 55 | +```milo |
| 56 | +fn sumTo(n: i64): i64 |
| 57 | + requires n >= 0 |
| 58 | + ensures result >= 0 |
| 59 | +{ |
| 60 | + var total: i64 = 0 |
| 61 | + var i: i64 = 1 |
| 62 | + while i <= n |
| 63 | + invariant total >= 0 |
| 64 | + invariant i >= 1 |
| 65 | + { |
| 66 | + total = total + i |
| 67 | + i = i + 1 |
| 68 | + } |
| 69 | + return total |
| 70 | +} |
| 71 | +``` |
| 72 | + |
| 73 | +### What the compiler checks |
| 74 | + |
| 75 | +Contracts are type-checked — every `requires`, `ensures`, and `invariant` expression must be `bool`. The compiler rejects non-boolean contract expressions at compile time: |
| 76 | + |
| 77 | +``` |
| 78 | +error: requires clause must be bool, got i64 |
| 79 | + --> example.milo:2:12 |
| 80 | + | |
| 81 | +2 | requires x + 1 |
| 82 | + | ^^^^^ |
| 83 | +``` |
| 84 | + |
| 85 | +## Formal verification — `milo verify` |
| 86 | + |
| 87 | +The `verify` command translates contracts into [SMT-LIB2](https://smtlib.cs.uiowa.edu/) format — the standard input language for theorem provers like [Z3](https://github.com/Z3Prover/z3) and [CVC5](https://cvc5.github.io/). |
| 88 | + |
| 89 | +```bash |
| 90 | +milo verify flight_controller.milo |
| 91 | +``` |
| 92 | + |
| 93 | +This outputs verification conditions that you can pipe to Z3: |
| 94 | + |
| 95 | +``` |
| 96 | +── precondition ── clamp ── |
| 97 | +precondition of clamp: (<= lo hi) |
| 98 | +(set-logic QF_LIA) |
| 99 | +(declare-const value Int) |
| 100 | +(declare-const lo Int) |
| 101 | +(declare-const hi Int) |
| 102 | +(assert (not (<= lo hi))) |
| 103 | +(check-sat) |
| 104 | +; sat = precondition can be violated, unsat = always holds |
| 105 | +``` |
| 106 | + |
| 107 | +If Z3 returns `unsat`, the condition always holds — your contract is mathematically proven. If it returns `sat`, there exists a counterexample where the contract can be violated. |
| 108 | + |
| 109 | +This is the same approach used in SPARK/Ada (the only other systems language with built-in formal verification) and Dafny. The difference: Milo doesn't require a separate toolchain or annotation language. Contracts are part of the language. |
| 110 | + |
| 111 | +## Safety profiles — `milo safety` |
| 112 | + |
| 113 | +Safety-critical domains have coding standards that restrict what language features are allowed. Milo can check your code against these standards at compile time. |
| 114 | + |
| 115 | +```bash |
| 116 | +milo safety flight_controller.milo --safety=do178c-a |
| 117 | +``` |
| 118 | + |
| 119 | +### Available profiles |
| 120 | + |
| 121 | +```bash |
| 122 | +milo safety --list |
| 123 | +``` |
| 124 | + |
| 125 | +| Domain | Standard | Profiles | Governs | |
| 126 | +|--------|----------|----------|---------| |
| 127 | +| Avionics | DO-178C | `do178c-a`, `do178c-b`, `do178c-c` | Airborne software (DAL A–C) | |
| 128 | +| Automotive | ISO 26262 | `iso26262-a` through `iso26262-d` | Vehicle ECUs, ADAS (ASIL A–D) | |
| 129 | +| Spacecraft | NASA-STD-8739.8 | `nasa-a`, `nasa-b` | Flight software (Class A–B) | |
| 130 | +| Industrial | IEC 61508 | `iec61508-3`, `iec61508-4` | Nuclear, rail signaling (SIL 3–4) | |
| 131 | +| Medical | IEC 62304 | `iec62304-a`, `iec62304-b`, `iec62304-c` | Device software (Class A–C) | |
| 132 | + |
| 133 | +### What gets checked |
| 134 | + |
| 135 | +Each profile is a combination of constraints, tuned to the standard's requirements: |
| 136 | + |
| 137 | +| Constraint | Description | Strictest at | |
| 138 | +|------------|-------------|-------------| |
| 139 | +| No recursion | Direct self-calls banned | DO-178C A, IEC 61508 SIL 4 | |
| 140 | +| Bounded loops | `while` loops must have `invariant` clauses | DO-178C A, NASA A | |
| 141 | +| No dynamic allocation | No Vec, String, HashMap construction | IEC 61508 SIL 4 | |
| 142 | +| Require contracts | All functions need `requires`/`ensures` | DO-178C A, NASA A | |
| 143 | +| No floating point | Integer-only arithmetic | IEC 61508 SIL 4 | |
| 144 | +| Complexity limit | Cyclomatic complexity cap per function | IEC 61508 SIL 4 (max 15) | |
| 145 | +| No unsafe blocks | `unsafe { }` banned entirely | All profiles | |
| 146 | +| Full match coverage | All `match` arms required | Most profiles | |
| 147 | + |
| 148 | +Example output when violations are found: |
| 149 | + |
| 150 | +``` |
| 151 | +safety check failed: do178c-a — 3 violation(s) |
| 152 | +
|
| 153 | + error: [do178c-a] function 'processInput' must have requires/ensures contracts |
| 154 | + error: [do178c-a] function 'processInput' contains recursion (banned at this safety level) |
| 155 | + error: [do178c-a] while loop in 'processInput' must have an invariant clause for bounded execution |
| 156 | +``` |
| 157 | + |
| 158 | +### Integrating with CI |
| 159 | + |
| 160 | +Add safety checking to your build pipeline: |
| 161 | + |
| 162 | +```bash |
| 163 | +milo safety src/controller.milo --safety=do178c-a || exit 1 |
| 164 | +``` |
| 165 | + |
| 166 | +The command exits with code 1 if any errors are found, making it suitable for CI gates. |
| 167 | + |
| 168 | +## Why this matters |
| 169 | + |
| 170 | +Most languages bolt safety analysis on after the fact with expensive third-party tools (LDRA, Polyspace, Coverity). In Milo, it's part of the compiler: |
| 171 | + |
| 172 | +- **Contracts are code, not comments.** They're type-checked, versioned, and can't drift from the implementation. |
| 173 | +- **Formal verification without a separate toolchain.** One command generates SMT-LIB2 from the same source file. |
| 174 | +- **Standards compliance as a compiler flag.** No separate static analysis pass, no proprietary tool licenses. |
| 175 | +- **Zero runtime cost.** Everything is checked at compile time. Your binary is just as fast with contracts as without. |
| 176 | + |
| 177 | +Combined with Milo's existing ownership system (no use-after-free, no data races, no null pointer dereferences), contracts close the gap on *logic errors* — the class of bugs that memory safety alone can't catch. |
0 commit comments