DEVELOPMENT EDITION · 769442725406
Vibe syntax
On this page

Vibe contracts, proof annotations, and exceptions

Contracts describe obligations, not ordinary Python runtime helper behavior. Verada checks their types and generates proof obligations. The default prove/build/run policy requires those obligations to pass. Annotations in a .pyi file or the test-only CPython helper do not supply that proof.

Function contract placement

After an optional leading docstring, put a contiguous sequence of requires, ensures, raises and decreases calls before ordinary function work:

from verada import Natural, requires, ensures, old, result

def increment(value: Natural) -> Natural:
    requires(value < Natural.last)
    ensures(result == value + 1)
    return value + 1

requires(condition) becomes a precondition: every caller establishes it, and the body assumes it. ensures(condition) becomes a postcondition proved on normal return. result names the function's result in a postcondition. old(expression) denotes the entry-state value used in a supported contract. Multiple leading declarations supply the corresponding obligations; each condition must be Boolean and must itself be well-defined.

decreases(value) among them is a recursive function's variant (Subprogram_Variant): each recursive call makes value smaller, and a value of a discrete type cannot go on getting smaller, so the recursion ends. The same call first in a loop's body is the loop's variant instead.

result and old are contract forms, not arbitrary runtime variables/functions to use in the body. Do not place a contract call after ordinary statements and expect it to be extracted retroactively. For mutating result operations, the compiler rewrites result to the generated output parameter while preserving the public return-style spelling.

Preserve facts needed by callers explicitly. A function that changes a list element should state an unchanged length when callers need that fact. Knowing a precondition before a call does not automatically preserve every property afterward. Read the shared contract rules and flow rules when the prover loses a fact at an abstraction boundary.

Assertions, assumptions, and loops

Form Meaning
assert condition A condition to prove at that point; usable afterward
assert condition, message The same, with a message (any value str() takes) that a failure's report ends with, computed only then
assume(condition) A trusted hypothesis, reported as B8002; not proved
invariant(condition) A loop invariant checked on establishment and preservation
decreases(value) A decreasing loop variant (first in a function's body: its recursion's variant)
increases(value) An increasing loop variant
@terminates Require termination of the decorated procedure
@ghost A function for contracts and proof only, not compiled into the program

Loop annotations belong directly in the loop body; put them first for clarity:

while remaining > 0:
    invariant(0 <= remaining <= initial)
    decreases(remaining)
    remaining -= 1

This fragment requires typed bounded locals remaining and initial and appropriate imports. The invariant states the property retained across iterations. It must hold initially and after an iteration; a variant needs progress and a lower/upper bound. Ordinary value-returning functions require termination already. @terminates is useful for no-result operations, including those called by a function.

A for range can supply visible finite iteration bounds, but generated pipeline/loop proof still needs arithmetic facts. Strengthening a precondition is legitimate only when it matches the application's accepted inputs. Do not use assume merely to make the proof green: an incorrect trusted assumption invalidates conclusions depending on it.

Quantified properties

all(predicate for x in domain) is universal; any(...) is existential. The domain is a range, an array or container, or a zip, enumerate or takewhile source; the generator has one for clause and may have if filters. Empty universal domains produce true, and empty existential domains false, under the shared quantifier semantics. These forms do not materialize a Python generator object.

sum is an arithmetic reduction with overflow and size obligations; over floats it is compensated, as CPython's is. See iteration and reductions.

Features without direct Vibe declarations

The contract forms above are Vibe's complete set. Global, Depends, Abstract_State, Refined_State, Contract_Cases, class-wide contracts, predicates other than range aliases, and Relaxed_Initialization are written in a Vada or Varc declaration, and Vibe code that imports it is checked against them; compatibility maps each shared aspect to its Vibe form. A decorator Vibe does not know is B0401, so no decorator can appear to add a property it does not check.

Exceptions

Declare a user exception with a module-level class directly based on Exception, containing pass or a docstring:

class Rejected(Exception):
    pass

It declares an exception, as Ada's Rejected : exception does. An exception carries a message and nothing else: its class has no fields, initializer or further bases.

Raise forms are raise Rejected, raise Rejected(), and raise Rejected("reason"), whose message is any value str() takes, an f-string say (kept as UTF-8, and given back by str(e)). raise without an expression reraises the exception being handled.

A propagating procedure lists exceptional outcomes using a leading contract:

from verada import raises

def reject() -> None:
    raises({Rejected: True})
    raise Rejected("not accepted")

This declaration fragment needs the Rejected class in its module. Each dictionary key names an exception and the condition describes the permitted exceptional exit. Use entry values with old(...) where required by the parameter/effect rules, rather than assuming Python-style mutable state survives every propagation boundary unchanged. A raise must be handled, permitted and proved against its exceptional contract, or proved unreachable. Ordinary pure value-returning functions cannot propagate exceptions; handle procedure exceptions internally or change the API to an appropriate procedure/result representation.

Handlers and occurrence information

try with except Rejected:, except (E1, E2):, a bare except:, or except Exception: handles user exceptions. A bare or Exception handler catches every user exception; it does not turn a failed proof or run-time check into an application error to recover from.

except Rejected as occurrence: binds the occurrence, which is an Ada.Exceptions.Exception_Occurrence. As in Python, print(occurrence), str(occurrence) and an f-string field give its message; ada.exceptions.exception_name gives its name, with import ada.exceptions to name that unit's functions:

def report_rejection() -> None:
    try:
        raise Rejected("not accepted")
    except Rejected as occurrence:
        print(occurrence)

This module fragment requires Rejected, and prints not accepted; the exception-message example is a complete project.

An explicit raise cannot leave the scope of a local element view before the element is stored back; see sharing. Owned objects and containers are released by the language's finalization rules on every exit, normal or exceptional.

Proof evidence and execution policy

An unproved scalar check is an error in strict proof/build/run. --advisory keeps the unproved checks as run-time checks; --no-prove keeps every check at run time without proving. Not every model-based contract can be checked at run time. A solver countermodel, a replay-confirmed failure, and B5115 unknown are different outcomes. The overflow diagnosis demonstrates the distinction. Resource exhaustion, foreign faults, and runtime check failure are not the user-exception propagation mechanism described above.

Source evidence: contract/exception lowering, exception legality, and exception corpus.