On this page
Vibe functions, methods, and results
Write functions at module level and methods inside a supported class. Every ordinary parameter needs a type annotation, and the result annotation is required. Use -> None when the operation returns no value. This allows the compiler to extract a stable typed interface before proving the body.
from verada import Natural, requires, ensures, result
def increment(value: Natural) -> Natural:
requires(value < Natural.last)
ensures(result == value + 1)
return value + 1This is a module declaration fragment; the complete increment project has a separate main program. The input annotation alone admits Natural.last; the precondition supplies the missing bound. See contracts for placement and proof meaning.
Parameters and defaults
A parameter is an input unless the body changes its components. Changing point.x, an array element, or a container through an argument makes the parameter writable, and the caller sees the change, as in Python. A parameter the body assigns as a whole (n = n * 2) is the function's own copy, as Python's name is: the body starts from the caller's value and the caller's variable does not change. One that the body both assigns and changes in place is B0401, since Python changes the caller's object up to the assignment and not after; give the copy a new name, xs_copy = copy(xs).
Defaults are expressions in the typed subprogram profile, checked according to the shared default-argument rules. They are not mutable Python objects retained between dynamic calls. A call to a module function can pass arguments by name. The reference project calls both adjusted(Small(3)) and adjusted(delta=Small(2), x=Small(3)) for:
def adjusted(x: Small, delta: Small = 1) -> int:
return int(x) + int(delta)This fragment assumes the declared Small integer type. Every parameter can be passed by position or by name, so Python's / and * markers have no place in a Vibe parameter list (B0401). A Vibe function has a fixed list of typed parameters: *args and **kwargs are not Vibe; pass a list or an array when the number of values varies.
Inferred input and writable modes
An unchanged parameter is an input. A parameter whose components are assigned, or on which a changing container method is called (append, insert, pop, extend, clear, add), becomes an input/output parameter. Changes made through a local view of the parameter count too, and so do calls: a parameter passed to a function of the module that changes that argument, or whose method that changes its object is called, is changed as well, as Python's object is the same object in both. So def outer(zs: list[int]) -> None: inner(zs) changes zs when inner appends to its argument. The inferred mode is part of the function's public interface, so changing the body can change how callers may pass that argument; --show-flow shows the effects the compiler inferred.
A scalar argument is passed by value, as in Python: a function cannot change its caller's integer. Return the new value, as the increment above does.
Mutation with a result
A function that changes a parameter's components and returns a value is represented as a procedure with an additional final output result. This preserves the familiar method pattern while respecting Verada's rule that an ordinary function expression is pure. The reference project includes:
def bump(self) -> int:
requires(self.x < Small.last)
ensures(self.x == old(self.x) + 1 and result == int(self.x))
self.x += 1
return int(self.x)This method fragment belongs to its Point dataclass and requires the imported contract helpers and declared Small type. Because the call changes its arguments, it is a statement underneath: call it as the whole right-hand side of a typed declaration, or of an assignment to a target already declared:
answer: int = point.bump()The same holds for a module function: the Stockroom application writes accepted: bool = reserve(items, 1, 5), and reserve(items, position=1, amount=5) passes the same arguments by name. Such a call cannot stand inside a larger expression (print(point.bump()), point.bump() + 1) or in a contract; give its result a name first. Untyped, answer = point.bump() is B2001, whose message asks for the type.
A module function that does input or output (calls print or input, or another function that does), that may let an exception out (raises(E) first in its body, D188), or that assigns a variable of the module (global x, or a class's ClassVar), is a procedure with a result in the same way, since a Verada function does none of these. Called by name inside an expression, as Python's are (print(area(3, 4) + 1)), it is made first, into a local of its result's type, and the expression reads that; x = area(3, 4) needs no type, since the function's result annotation gives it. Its arguments are evaluated in Python's order, and a call in a branch of and, or or a conditional expression is B2005: name its result first.
Protected entries and protected operations that change components use the same final-result technique. Their synchronization and barrier behavior are described under concurrency.
Imported scalar outputs
Seen from Vibe, a Vada or Varc procedure's out parameters and its scalar in out parameters are its results, as Python returns several values: the call passes the inputs (the in and in out parameters, by position or by name) and is the whole right-hand side of a typed declaration or of an assignment, whose targets receive the results in order. For
procedure Twice (Value : Integer; Answer : out Integer)
with Pre => Value in 0 .. 100;in package Foreign, Vibe writes:
import foreign
def main() -> None:
answer: int = foreign.twice(3)
print(answer)The foreign-output example is a complete project. With procedure Increment (X : in out Integer) and procedure Split (N : Integer; Low, High : out Integer) in the same package, value = foreign.increment(value) passes value and assigns it the result, and low, high = foreign.split(7) declares both names, each of its parameter's type. A name that from foreign import twice brings in is called the same way. Tuples returned by Vibe functions are ordinary typed tuple values; see tuple assignment.
Methods and dispatch
A method takes self first. Its annotation can be omitted because the containing class supplies it; every other parameter needs a type. A function that needs no receiver is a module function, written outside the class: Vibe has no @staticmethod, @classmethod or @property (B0401). Dataclass construction and equality are the compiler's, so a class does not define __init__ or __eq__.
A tagged class parameter accepts its supported subclasses, and a call to an abstract/overridden primitive can dispatch using the runtime tag. In a method of a derived class, super().describe() calls the parent's own describe on self. A regular untagged dataclass is a record value. A class implementing a Protocol must name it as a base; structural duck typing does not create that interface. See classes.
Nested functions
A def inside a function is a nested function (Ada's nested subprogram), defined from that point to the end of the statements around it; functions defined one after another there may call each other. It reads the enclosing function's names as they are when it is called, and assigns one only after nonlocal x, written first in its body (before its contracts), as in Python; without it, an assignment makes a local of its own. A nested function that assigns through nonlocal and returns a value is a procedure underneath, as a method that changes its object is. A nested function is no value: returning or storing it (a closure that outlives the function) is B0401.
def main() -> None:
count = 0
def bump(by: int) -> None:
nonlocal count
requires(0 <= by <= 10 and 0 <= count <= 50)
ensures(count == old(count) + by)
count += by
bump(3)
print(count)Function bodies, recursion, and generic calls
A non-generic pure function whose post-contract body is a single return expression is represented as an expression function; its body can help callers' proofs. Other bodies use ordinary modular contracts. The compiler may know less after a call than after an equivalent inline calculation: state useful postconditions, including preserved collection lengths.
Generic function syntax is def choose[T](...) -> T, with a bound as def larger[T: Ordered](...) -> T. A call infers the actual types from its arguments. Details and module identity rules are under classes and generics.
A value-returning function must terminate, and the prover shows it. A recursive one states what each call makes smaller with decreases(e) first in its body, beside its requires and ensures (Vada's Subprogram_Variant => (Decreases => E)); B5108 asks for it when it is missing. In a loop's body, decreases(e) is the loop's variant instead.
def triangle(n: int) -> int:
requires(0 <= n <= 1000)
decreases(n)
return 0 if n == 0 else n + triangle(n - 1)@terminates asks the same of a procedure (a function returning None). @ghost marks a function for contracts and proof only; it is not compiled into the program. A decorator other than Vibe's is B0401 (compatibility).
Source evidence: function lowering, procedure-result assignments, and generic-call inference.