On this page
- Aspect syntax and placement
- Preconditions, postconditions and old values
- Cases and exceptional outcomes
- Assertions, assumptions and loops
- Ghost code and mathematical models
- Effects, state abstraction and initialization
- Type-level contracts
- Allocation and explicit cleanup
- Reclaimed values and copy adjustment
Vada contracts, effects and ownership syntax
Contracts are declarations about an API and its implementation. They are part of the program Verada checks, not documentation comments. This chapter gives Vada source forms; the shared contract rules and Verification Manual explain reasoning, proof outcomes and assurance.
Aspect syntax and placement
An aspect specification begins with with and contains comma-separated aspect names, optionally followed by => Value:
procedure Increment (Value : in out Natural)
with Pre => Value < Natural'Last,
Post => Value = Value'Old + 1;An aspect without a value is a marker where the aspect permits that form, for example with Ghost or with Always_Terminates. This is not a general shorthand for any arbitrary Boolean aspect expression.
Type/object/subtype declarations put aspects after their definition or initializer and before ;. A package puts them before is. A normal subprogram body puts them after the profile and before is. Expression functions, null procedures and abstract declarations put aspects after the expression/null/abstract and before ;.
Each aspect has its permitted placements and values, checked by the compiler. Some Ada aspects are accepted for compatibility without effect; the aspect and pragma catalog lists every name and its effect.
Preconditions, postconditions and old values
A Pre condition is evaluated/reasoned about at entry. A caller must establish it; the callee assumes it while proving its body. A Post condition describes normal return. For functions, Function_Name'Result refers to the returned value. Expression'Old captures the relevant value from entry for a postcondition.
procedure Add (Value : in out Natural; Amount : Natural)
with Pre => Amount <= Natural'Last - Value,
Post => Value = Value'Old + Amount;These are specification fragments. Arithmetic used to express a boundary can itself require safe evaluation; rearrange bounds so the contract does not introduce the very overflowing intermediate it is meant to exclude.
'Old is not a snapshot statement placed anywhere in a body. Its legal prefix cannot depend on names created only at return or an inappropriate quantifier binding. Conditional postconditions have explicit old-value evaluation restrictions. Capture intended entry state through supported prefixes and models rather than assuming every arbitrary expression can be frozen.
Class-wide contract spellings are Pre'Class and Post'Class on the appropriate dispatching operations. Other 'Class aspect forms are excluded. Dispatch contract inheritance is defined by the shared subprogram rules.
Cases and exceptional outcomes
Contract cases partition behavior by entry-state guards:
function Absolute (Value : Integer) return Natural
with Pre => Value > Integer'First,
Contract_Cases =>
(Value >= 0 => Absolute'Result = Value,
others => Absolute'Result = -Value);This is an interface fragment. Each guard/consequence has the supported contract-case interpretation and coverage obligations; others is not a fallthrough imperative branch.
A procedure that can propagate a user exception declares its consequences with Exceptional_Cases:
procedure Withdraw (Balance : in out Natural; Amount : Natural)
with Post => Balance = Balance'Old - Amount,
Exceptional_Cases =>
(Insufficient_Funds => Balance = Balance'Old);The exception must already be declared. The normal postcondition and exceptional consequences describe different exits. Parameter-passing mode and whether a type is passed by copy/reference affect which changes the caller can observe after an exception. See exception rules, rather than assuming every in out formal copies back on every exit.
Assertions, assumptions and loops
Statement pragmas are separate statements:
pragma Assert (Count <= Limit);
pragma Assert (Count <= Limit, "count must fit");
pragma Assume (External_Input <= Limit);
pragma Loop_Invariant (Total <= Limit);
pragma Loop_Variant (Decreases => Remaining);
pragma Loop_Variant (Increases => Position);Assert creates a property to prove. Its optional second argument, Ada's message, is a String that is no part of the property: it is computed only when the assertion fails (in a build that keeps assertions, --no-prove or --advisory), and the failure's report ends with it. Computing it must be free of run-time errors where the assertion is false, which is proved with the rest. Assume adds a trusted premise and must be recorded as such; it does not demonstrate the property. Loop invariants must be established and preserved. A variant supplies a well-founded progress measure where required. Multiple supported variant components use named associations in the pragma's argument list.
The position of an invariant/variant matters. It is evaluated at that statement location, which is also a proof cut point. Invariants are not silently assumed true for a first iteration that cannot establish them.
A subprogram's recursive progress is specified with Subprogram_Variant, for example Subprogram_Variant => (Decreases => N). Always_Terminates requires termination of a procedure that would not otherwise carry the universal function requirement. Ordinary procedures are not all proved terminating merely because they compile.
Ghost code and mathematical models
A declaration with Ghost exists for specification/proof use:
Steps : Natural := 0 with Ghost;
function Model (Value : Container) return Model_Type with Ghost;These context fragments assume the referenced types. Ghost state cannot influence ordinary executable results in a way that violates ghost restrictions. Mutable ghost state still has initialization/effect obligations. Ghost functions are useful for abstract container models and mathematical numbers without committing the public contract to a storage layout.
Proof modes differ in which contract/ghost operations can be executed at runtime. Do not infer that every quantified mathematical contract becomes a runtime check in advisory mode. Model-library declarations and runtime enforcement are described in the Verification Manual.
Effects, state abstraction and initialization
Global declares a subprogram's state footprint:
procedure Step (Amount : Natural)
with Global => (Input => Limit, In_Out => Count, Output => Last_Amount),
Depends => (Count => (Count, Amount), Last_Amount => Amount);A singleton or list of read-only globals may use the compact Global => (A, B) form. Global => null denotes no global state. A dependency association gives an output followed by the inputs that influence it; Depends => null applies where the output dependency set is empty. The exact distinction between omitted and declared contracts, supported dependency shorthands, and inferred contracts is defined by flow contracts.
Package Abstract_State exposes an abstract state name. Refined_State connects it to private/body objects; Part_Of assigns an object/state constituent to its owner. Initializes describes package elaboration, and Initial_Condition states what holds when it ends; it is proved there and assumed by the main program. Bodies refine visible effect contracts with Refined_Global and Refined_Depends, and add body-level postconditions with Refined_Post, which is proved along with Post.
Do not declare concrete hidden globals in a public effect contract merely to make a proof pass: use the supported abstract state boundary. See the worked flow corpus for complete declarations and bodies.
Type-level contracts
A private/full type declaration can carry Type_Invariant or Default_Initial_Condition. A subtype can carry Static_Predicate, Dynamic_Predicate or the supported Predicate spelling. Examples are contextual declarations:
subtype Even is Integer
with Dynamic_Predicate => Even mod 2 = 0;The subtype name in a predicate denotes the current instance under that rule. Predicates have specified check points, and invariants use public/private-view boundaries rather than requiring an arbitrary partially updated representation to satisfy the public invariant after every internal statement.
A scalar type can name the value its objects start with, and an array type the value of its components:
type Level is range 0 .. 10 with Default_Value => 3;
type Levels is array (1 .. 3) of Level with Default_Component_Value => 7;The value is static. Objects, components, allocators and <> associations of these types made without a value hold it, flow analysis counts them as initialized, and derived types inherit it.
Relaxed_Initialization lets an object be partly initialized, with 'Initialized in contracts and assertions saying which parts are; reading an unset part is still an error.
Allocation and explicit cleanup
The standard owning linked structure uses an incomplete type followed by an access type:
type Node;
type List is access Node;
type Node is record
Value : Integer := 0;
Next : List;
end record;An allocation such as new Node'(Value => 7, Next => null) yields an owner. An ownership assignment can move that owner; later reads through the moved-from path are restricted. There is no special move keyword in Vada.
Explicit deallocation instantiates the predefined generic:
procedure Free is new Ada.Unchecked_Deallocation (Node, List);The unit must be visible through its context clause. Despite the inherited library name, Verada checks ownership/reclamation obligations around the call. Overwriting or losing a live owner is not a permitted substitute for freeing its allocation.
A supported observer is written access constant Node; a borrower uses anonymous access Node in its permitted scope. Access parameters and traversal results have their own accessibility/borrowing rules. The complete linked-structure corpus shows allocation, observers, borrowers, traversal, updates and cleanup in actual unit context.
'Unchecked_Access, 'Unrestricted_Access and access discriminants are excluded. A general access type (access all T) and 'Access of an aliased object obey the general access rules.
Reclaimed values and copy adjustment
A reclaimed type connects its cleanup operation through the GNAT Finalizable extension:
type Resource is limited private
with Finalizable =>
(Finalize => Release, Relaxed_Finalization => True);
This is a shape pattern: the operation must have the required profile and declaration visibility. A limited reclaimed value is not freely copyable. A supported nonlimited reclaimed type supplies an Adjust operation for copies in addition to Finalize. Finalizers/adjusters cannot have entry preconditions, and a copyable reclaimed representation cannot hold ordinary owned access fields whose copying would share ownership.
Code generation may place permitted copies at different points; use value semantics rather than externally observable copy counts. Finalization covers normal and exceptional exits. GNAT accepts these forms with -gnatX. The shared reclaimed-value rules and library reference cover public text/container APIs built on this mechanism.