Repository navigation
Verification Scope
This document pins down what Curlee proves (and what it does not prove) in the first verification milestone.
Curlee’s long-term goal (per GEMINI_CONVERSATION.md) is “verification-first” development: code should not run unless it satisfies its declared contracts.
The MVP is intentionally conservative: we only support a small, decidable logic fragment and clear rules for when verification succeeds/fails.
MVP verification should:
- Reject obvious unsafe/invalid calls (e.g.
divide(10, 0)whendividerequiresdenominator != 0). - Support function contracts (
requires/ensures) and refinements (where) as machine-checkable facts (see Glossary). - Produce actionable diagnostics (point to contract, show model / counterexample when available).
- Be deterministic and fast enough to run on every edit.
- Never guess: if a contract/refinement cannot be interpreted within scope, compilation fails with a clear diagnostic.
MVP does not attempt:
- Whole-program termination proofs.
- Full memory safety proofs.
- Linear/ownership types.
- Temporal logic constraints (e.g.,
eventually(...),never(...)). - Quantifiers (
forall,exists), induction, or higher-order reasoning. - Rich string theory (beyond “opaque String type” and equality, if even that).
- Producing a portable cryptographic proof certificate (Proof-Carrying Code) that is independent of the verifier.
(We can evolve toward these later, but they dramatically increase complexity.)
In MVP, “proven” means:
- Curlee translates contracts/refinements into Z3 constraints.
- Curlee uses Z3 to decide whether a call/return obligation holds.
- If Z3 says the obligation can be violated, Curlee rejects the program.
This is a trust-the-solver model (common in practice). For multi-agent sovereignty we will later define how to ship and verify bundles; MVP focuses on the local correctness gate.
Contracts attach to functions:
fn divide(numerator: Int, denominator: Int) -> Int
[ requires denominator != 0;
ensures result * denominator == numerator; ]
{
return numerator / denominator;
}
Rules:
-
requires <pred>: 0+ clauses allowed. -
ensures <pred>: 0+ clauses allowed. -
resultis a reserved identifier inensuresand refers to the returned value.
Refinements can appear on:
- variable bindings
- function parameters
Examples:
let x: Int where x > 0 = 5;
fn takes_pos(x: Int where x > 0) -> Int {
return x;
}
MVP supports quantifier-free linear integer arithmetic + booleans (roughly: QF_LIA) plus equality.
-
Intmaps to Z3 Int. -
Boolmaps to Z3 Bool. -
Stringis not part of the logic initially (either unsupported in contracts or only equality). -
Vecis treated as a non-scalar/uninterpreted signature type in MVP; predicate reasoning over vector contents/indices is out of scope. - Fixed-size array bindings
[T; N](freestanding locals, issue #278) are modeled in the solver with Z3's array theory: element reads lower toselect, element writes tostore, so read-over-write coherence is exact. Every element access carries a per-access index-bounds obligation (0 <= i < N) discharged against the current facts — constant out-of-bounds (including negative) indices are rejected by the type checker first; symbolic indices must be provably in bounds (the loop-invariant machinery discharges this for bounded ring indices), and an unprovable index fails verification with a model note. Predicates themselves cannot reference array elements (out of scope; see §4.5).
The verifier supports in-scope struct field references (for example p.x) in expression-level obligations (call-site checks, return/ensures checks), with these limits:
- Only fields with scalar logical types (
Int,Bool) are lowered. - The base value must be an in-scope struct binding (parameter or local
let). - Non-scalar fields (for example nested non-
Int/Boolpayloads) are out of scope for expression lowering and are rejected with a hard error.
For locals initialized by a struct literal, scalar field initializers are reflected into verification facts for that binding (e.g., let p: Point = Point{ x: 1 } contributes p.x == 1 in scope).
Allowed in requires / ensures / where:
- boolean connectives:
&&,||,! - comparisons:
==,!=,<,<=,>,>= - integer ops:
+,-,*(restricted),/,%, and numeric literals - bitwise:
&,|,^,~,<<,>>(bit-precise 64-bit two's-complement model via Z3's bit-vector theory; right shift is arithmetic)
Restrictions (MVP):
- Division/mod are supported for the non-negative subset where the division-remainder
identity
a % b == a - (a / b) * bholds; predicates must constrain operands accordingly (for example0 <= a && 0 < b). - By default, no non-linear arithmetic (
x * ywhere both operands are symbolic). A product whose left or right factor is a division/modulo term is linear in Z3's Int theory and is accepted (e.g.(a / b) * bin the modulo identity).
Curlee supports a documented higher-power verification mode behind explicit opt-in:
- Enable with environment variable:
CURLEE_VERIFY_HIGHER_POWER=1(also acceptstrue/on). - Scope change in this mode:
- non-linear integer multiplication (
x * ywith two symbolic operands) is permitted in predicate/expression lowering.
- non-linear integer multiplication (
- Default remains conservative:
- without opt-in, the verifier emits
non-linear multiplication is not supported.
- without opt-in, the verifier emits
- Determinism and safety behavior:
- unsupported/out-of-scope constructs still produce hard diagnostics;
- solver
unknownremains a verification failure path.
Enum values are supported for type checking, including match exhaustiveness checks.
Verifier behavior for match:
- If the scrutinee variant is syntactically known (for example
match (E::A(...))), verifier checking is path-sensitive and visits only the reachable arm. - For payload arms (
E::A(v)), the payload binder is introduced in arm scope; when a concrete scrutinee payload expression is available and lowerable, that binder is constrained to the matched payload value. - Full symbolic case-splitting over unknown enum values is still out of scope.
This keeps defaults decidable while improving practical path-sensitive checking for common enum patterns.
We can support k * x where k is a literal integer and x is an Int expression (linear), but we should reject x * y when both are symbolic.
If a requires / ensures / where predicate uses features outside this scope, Curlee must:
- Emit a diagnostic pointing at the unsupported construct.
- Refuse to run/verify (do not silently drop or weaken the predicate).
Examples that are out of scope in MVP:
- Refinements/predicates over
Vecvalues. - Quantified/vector-global properties (e.g. "all elements are >= 0").
- Predicates over fixed-size array elements (
q[i]inside a contract): the bounds obligation on body accesses is verified, but array contents are not part of the contract language (issue #278 MVP).
The freestanding target (see Running Programs) introduces three documented exceptions to the verification surface. All are freestanding-only (the VM rejects them) and are modeled conservatively — no fact is invented that the solver cannot support.
phys<U>(literal) is a trusted deref: the compiler believes the literal address points at
the declared element type, without proof. This is the explicit contract of the unsafe +
cap phys.mem surface:
- The address must be a compile-time literal (constant-address rule).
- The compiler emits a volatile
uintN_t*deref of the literal address. - No memory-safety obligation is generated for the deref itself (that is the "unsafe" part).
- The caller is responsible for the address being valid in the target environment.
A Phys<T>.read() result (and anything derived from it) is opaque to the solver:
-
phys_read : Phys<T> -> Tis an uninterpreted function with no axioms. - Contracts that reference an opaque read value cannot be proven and are rejected with a
hard diagnostic (e.g.
cannot prove contract on opaque MMIO read value,cannot prove requires on opaque MMIO read value,cannot prove ensures on opaque MMIO read result), each carrying the noteMMIO read is opaque; cannot prove this contract. - This is the conservative choice: the verifier never guesses a fact about hardware state.
For extern fn declarations, the contract on the extern is assumed, not verified:
- At a call site, the caller trusts the extern's declared signature (parameter types, return
type, and any
requires/ensureswritten on the declaration). - The verifier emits a Note (never an error) at the extern boundary:
extern boundary: contract assumed, not verified. - The extern body (none exists) is never checked; the runtime/host must provide a symbol whose behavior matches the declared contract.
Curlee performs checks as proof obligations.
At any program point we maintain a set of logical facts (assumptions), derived from:
- parameter refinements (
where) in the current function - local
letrefinements - active function
requiresassumptions for the current function - control-flow conditions (optional MVP+; see below)
When calling f(args...), Curlee must prove:
- current facts imply
f.requireswith parameters substituted
Formally, if current facts are
Implementation check:
- Ask Z3 if
$F \wedge \neg R$ is satisfiable.- If SAT: there exists a model / counterexample → reject with diagnostic + model.
- If UNSAT: obligation holds → call allowed.
For each return e; inside f, Curlee must prove:
(where ensures clauses).
x: Int where P(x) means: after x is in scope, we assume
If where references identifiers other than the refined variable, those must be in scope and well-typed.
-
Path sensitivity for
ifandwhileconditions:-
if (cond) { ... } else { ... }: then-branch assumescond, else-branch assumes!condwhencondcan be lowered to a Bool fact. -
while (cond) { ... }: the loop body assumescondwhencondcan be lowered to a Bool fact.
-
-
Loop invariants are supported with explicit syntax on
while:while (cond) [ invariant P; invariant Q; ] { ... }Verifier obligations:
- each invariant must hold at loop entry (
loop invariant not establishedon failure), - each invariant is available as a fact inside the loop body,
- each invariant is re-checked after body traversal (
loop invariant not preservedon failure).
- each invariant must hold at loop entry (
-
decreases D;variants are supported:Dmust be non-negative at loop entry and strictly decrease each iteration (decreases expression does not strictly decrease each iterationon failure), giving real termination proofs for bounded loops. -
Assignment (
x = expr;, see Language Syntax §2.3) mutates localletbindings and is how loop-carried variables change. Each declaration and each assignment rebinds the name to a fresh solver symbol constrained to the RHS value, so pre- and post-mutation values are distinct constants: adecreasesvariant is checked against the pre-iteration value and invariants are re-proven against the post-mutation state (parameters andghost letsnapshots are read-only and cannot be assigned). -
Loop limits retained:
- no automatic invariant inference,
- facts learned inside the body do not automatically flow out unless represented by declared invariants.
This is enough for early contract demos but will reject many provable programs.
- Broaden symbolic
matchreasoning to unknown scrutinees (sound case-splitting with payload constraints). - Consider bounded/controlled quantification behind explicit opt-in with clear diagnostics and tests.
- Add richer loop exit reasoning while preserving deterministic defaults.
When a proof obligation fails, diagnostics should include:
- Primary span (see Glossary): the call site or return expression.
- Note span: the contract clause (
requires/ensures) that failed. - Optional: a small model / counterexample rendered in terms of Curlee variables.
For “proof-carrying tasks”, we eventually want to ship:
- a bundle: bytecode + manifest + declared capabilities + build hash (see Glossary)
- verification metadata sufficient for a remote agent to re-check
MVP does not require Z3 proof certificates; it requires the receiver to re-run verification deterministically.
Add tests that demonstrate:
- Call-site requires failure with a model / counterexample:
-
divide(10, 0)rejected.
-
- Simple refinement propagation:
-
x: Int where x > 0allows a call requiringx != 0.
-
- Ensures checking at return:
-
ensures result >= 0fails forreturn -1.
-