DEVELOPMENT EDITION · 769442725406
Library Reference
On this page

Bounded formal vectors

Verada.Containers.Formal.Vectors stores an ordered sequence of at most a capacity chosen when the generic package is instantiated. Choose it when a fixed storage bound is useful and the application can prove that it never appends to a full vector. Elements use positions starting at 1; zero is a search result meaning that no position was returned.

The complete declaration reference is generated from the visible specification. This page explains those declarations without replacing their exact contracts.

Instantiate and use the package

The generic takes Element_Type, a definite nonlimited private type, and Capacity, a Positive value. The actual element type must supply the operations required by the declarations, including equality used by searches and contracts. A zero capacity is not accepted.

The complete Vada example uses natural numbers and four element slots:

with Verada.Containers.Formal.Vectors;

procedure Vectors is
   package Numbers is new Verada.Containers.Formal.Vectors (Natural, 4);
   Stock : Numbers.Vector;
   -- (a container aggregate: Empty_Vector, then Append of each element in order)
   Few : constant Numbers.Vector := [1, 2, 3];
begin
   pragma Assert (Numbers.Length (Few) = 3 and then Numbers.Last_Element (Few) = 3);
   pragma Assert (Numbers.Is_Empty (Numbers.Empty_Vector));
   Numbers.Append (Stock, 10);
   Numbers.Append (Stock, 20);
   pragma Assert (Numbers.Length (Stock) = 2
                  and then Numbers.Element (Stock, 1) = 10
                  and then Numbers.Last_Element (Stock) = 20);
   Numbers.Replace_Element (Stock, 1, 11);
   Numbers.Delete_Last (Stock);
   pragma Assert (Numbers.Length (Stock) = 1
                  and then Numbers.First_Element (Stock) = 11);
   Numbers.Clear (Stock);
   pragma Assert (Numbers.Is_Empty (Stock));
end Vectors;

From the repository root, prove and run it:

veradic prove docs/manuals/examples/formal-vectors/vectors.vadb
veradic run docs/manuals/examples/formal-vectors/vectors.vadb

The program prints nothing. Its assertions check the sequence after each group of updates. Few is written as a container aggregate (Ada 2022, RM 4.3.5): the type's Aggregate aspect makes [1, 2, 3] start from Empty_Vector and Append each element in order, so each Append's precondition (room left) is proved like any other call's, and its postcondition gives the length and the last element. The default initial condition establishes that Stock starts empty, so the first append has room. The operation contracts preserve enough information to prove the following calls.

The complete Varc example instantiates the same generic inside its main subprogram:

import Verada.Containers.Formal.Vectors;
fn Vectors() {
    package Numbers = new Verada.Containers.Formal.Vectors<Natural, 4>;
    var Stock: Numbers.Vector;
    Numbers.Append(&Stock, 10);
    Numbers.Append(&Stock, 20);
    assert Numbers.Length(Stock) == 2 && Numbers.Element(Stock, 1) == 10 && Numbers.Last_Element(Stock) == 20;
    Numbers.Replace_Element(&Stock, 1, 11);
    Numbers.Delete_Last(&Stock);
    assert Numbers.Length(Stock) == 1 && Numbers.First_Element(Stock) == 11;
    Numbers.Clear(&Stock);
    assert Numbers.Is_Empty(Stock);
}

Calls use the declared library spelling, such as Numbers.Append(&Stock, 10) for a mutable vector argument. Vada is case-insensitive; Varc names are case-sensitive. Prove and run the Varc file with the same commands, replacing vectors.vadb with vectors.va. It also succeeds without printing output.

Vibe's list[T] is the unbounded vector, with zero-based indexing, not this package. Vibe does not instantiate library generics: to use a bounded vector from Vibe, wrap it in a Vada or Varc package that exports the operations needed.

Types, size, and defaults

Declaration Meaning
Extended_Index The subtype 0 .. Capacity; used for lengths and search results
Index_Type The subtype 1 .. Capacity; an index must also identify a currently present element
Vector A private, nonlimited vector value; its default initial condition is Length = 0
Length The number of present elements, between zero and capacity
Is_Empty Exactly Length = 0
Is_Full Exactly Length = Capacity

An Index_Type value can still be beyond the current length. For example, index 4 belongs to a capacity-four instance but cannot read a vector containing only two elements. The Element precondition makes that second distinction.

Reading elements and searching

Operation Caller obligation Result promised by the public contract
Element(V, I) I belongs to Index_Type and I <= Length(V) The element at position I
First_Element(V) not Is_Empty(V) Equal to Element(V, 1)
Last_Element(V) not Is_Empty(V) Equal to Element(V, Length(V))
Find_Index(V, Item) No additional precondition A value in 0 .. Capacity; a nonzero result is at most the length and identifies an equal item
Contains(V, Item) No additional precondition Exactly Find_Index(V, Item) /= 0

Find_Index scans from the first position and returns the first match, or zero. Its postcondition states only what a nonzero result establishes: it does not say that zero means the item is absent, or that a match is the first. To prove absence, state it with a quantified expression over Element.

Updating a vector

Each operation has Global => null: it uses no package-global state. A mutation changes the vector supplied as its in out parameter, so this aspect does not mean that the operation has no effect on its argument.

Operation Precondition Postcondition
Clear(V) None Length becomes zero
Append(V, Item) not Is_Full(V) Length increases by one; the last element is Item; every old element is preserved at its position
Replace_Element(V, I, Item) I belongs to Index_Type and I <= Length(V) Length is unchanged; position I becomes Item; all other present elements are preserved
Delete_Last(V) not Is_Empty(V) Length decreases by one; all remaining elements are preserved

There is no automatic growth, arbitrary insertion, middle deletion, writable element view, or cursor type in this API. To append conditionally, first establish not Is_Full(V); a call whose precondition the prover cannot show is reported as B5101.

Iteration and position validity

The Iterable aspect permits for E of V and quantified expressions over the present elements. Positions advance from 1 through Length(V) in order. The four visible hooks are:

Hook Definition or obligation
Iter_First(V) Returns 1, including for an empty vector
Iter_Next(V, P) Returns P + 1 below Natural'Last, otherwise P
Iter_Has_Element(V, P) Exactly P in 1 .. Length(V)
Iter_Element(V, P) Requires Iter_Has_Element(V, P) and returns Element(V, P)

The loop checks whether an element exists before reading it, so an empty vector has no iterations even though its first candidate position is 1. Iter_Next stops at Natural'Last, so a capacity below Natural'Last keeps the end of iteration distinct.

Saved positions are numbers, not stable element identities. Clear invalidates all previously present positions, and Delete_Last invalidates the old last position. Replace_Element changes the value at its position. Append preserves old positions and their values. The API returns element values, not references into its storage.

Functional model for contracts

Models is an instance of Verada.Containers.Functional.Sequences(Element_Type). Model(V) is a ghost function returning the current elements in order. Its contract says:

The model lets contracts describe the sequence without exposing the vector's private storage. It is ghost: a build leaves it out of the program. The functional-model chapter and the Verification Manual explain reasoning with models.

Storage, copying, and cost

The implementation contains a length and an array with Capacity element slots inside each vector. It does not allocate a growable backing buffer. Actual object size also depends on the element representation and target layout; element types may manage their own resources. Vector is nonlimited, so assignment copies it, element by element.

Clear and Delete_Last reduce the length; they do not overwrite the removed slots. Length, emptiness and fullness queries, indexed reads, append, replacement, clear and deletion take constant time; Find_Index compares up to Length elements.

What a proof trusts

A client proof uses these contracts and does not prove the package body again; see library assurance.