DEVELOPMENT EDITION · 769442725406
Library Reference
On this page

Unbounded containers

“Unbounded” means that an instance has no Capacity: the container grows as elements are added, up to a length bound stated in each contract and the memory available. Vectors, maps and ordered sets are limited owners: they are copied only by an explicit Copy, and reclaimed when they end. Dictionaries hold two such vectors. Indices are one-based; Vibe's list indexing is zero-based and the compiler translates it.

Vectors and the Vibe list route

Unbounded.Vectors starts empty and has Length below Verada.Storage.Length_Bound (2⁶⁰), as memory holds no more. Model returns the current elements as an Element_Array, an unconstrained array indexed from one. Empty constructs an empty vector for container aggregates. Element requires Index <= Length. This array-valued Model is a different type from the ghost functional sequence that bounded vectors use.

Append adds one last element (Storage_Error when memory cannot hold it, as for every operation that lengthens a container: none has a precondition on sizes). Insert accepts a positive Before <= Length+1, inserting before that position (at Length+1 it appends). Pop with an index removes and returns that element; Pop without an index removes/returns the last. Delete_Last removes without returning. Extend appends another vector's sequence. Replace_Element preserves length and all other positions.

Copy replaces the target with a separate copy; Move transfers the source's elements and empties it. Clear empties the vector and releases its storage. A vector is reclaimed when its scope ends; assignment of a whole vector is not allowed, since the type is limited.

Same_Except, Last_Of, Appended, Inserted, Removed, Concatenated, Element_At and Without_Last are ghost functions over the model arrays, used in contracts to describe each change; their own preconditions (an index in range, a nonempty array) apply in proof code too. Iteration visits positions 1 .. Length.

The backing buffer doubles when it is full, so appending costs amortized constant time. Insertion and removal shift the elements after the position, and Copy and Model copy the whole sequence. After an insertion or removal, a saved position can hold a different value.

Contains, Index (the first equal element's position, requiring Contains), Count (the number of equal elements, by the ghost Count_Of), Remove (the first equal element), Delete (by position), Reverse_Elements and Reversed, "+" (two vectors joined) and "*" (copies, both orders) are Python's list operations; each states its result through the model. The generics Generic_Sort, Generic_Sorted, Generic_Minimum and Generic_Maximum take an element "<": Generic_Sort leaves the vector in order and a permutation of what it was (Is_Permutation), and keeps equal elements in their order (a fact its contract does not state); the minimum and maximum are the first least and greatest elements, which needs "<" to be a strict weak order over the vector's elements (their preconditions).

In Vibe, xs: list[int] = [1, 2], indexing, and the list methods and builtins of the Vibe reference use this family, with Python's zero-based positions; xs.sort(), sorted(xs), min(xs) and max(xs) are instances of the generics, made where they are called. A negative index counts from the end, as Python's does (xs[-1] is xs[len(xs) - 1]). list[str] is the library's own instance, Verada.Strings.Text_Lists, which split gives.

Unbounded.Nested_Vectors is a vector of vectors: its formal package Inner is the Unbounded.Vectors instance of the rows, which it holds in place, so it is limited too. Its model is each row's length (Lengths) and each row's elements (Row), from 1; Append of a row stores a copy, Append and Replace_Element at a row change that row in place and keep every row's length, Row_Copy gives a row of its own, and Take and Put_Back move a row out and back. Vibe's list[list[T]] is its instance whose Inner is list[T]'s, so a row and a list[T] are of one type.

Linked lists

Linked_Lists is a doubly linked list proved whole as a proof enclave: its cells are linked both ways, and the heap engine proves each operation against Model, a functional sequence of the elements, and proves that it changes only its own list's cells. Clients therefore reason about a list as its model. Append and Prepend add at either end; Remove_First and Remove_Last need a nonempty list and give back the element they remove; First_Element and Last_Element read the ends; an instance of Update_Element changes the I'th element in place with a procedure of the client's, which a ghost function describes. The list is limited and has no cursors; Clear frees every cell, and a list is cleared when it ends.

Graphs

Graphs is a directed graph proved whole as a proof enclave, like Linked_Lists: vertices are cells shared by every edge into them, and edges may make cycles. Model gives each vertex's out-degree and the edges in the order they were added. Add_Vertex numbers a new vertex after the others; Add_Edge adds an edge not already there; Out_Degree and Adjacent read the graph; Clear frees every cell, and a graph is cleared when it ends. Only memory limits a graph's vertices and edges.

Hash maps

Hash_Maps is a hash map from integers to any element type, a proof enclave: its entries live in place in one table, open addressing with linear probing, which doubles when it is half full. Model gives the keys, each once, in the order they were first added, and the element of each. Include adds a key or replaces its element; Contains, Element and Length read the map; Clear frees the table, and a map is cleared when it ends. Only memory limits the keys. Every body is proved at the default proof budget.

Hash_Sets is a set of integers made of a hash map: its members are the map's keys, Model gives them in the order they were first included, and Include, Contains, Length and Clear are the map's operations.

Unbounded maps

Unbounded.Maps uses parallel one-based arrays of unique keys and values, in insertion order. Length/Keys_Model/Elements_Model observe those arrays. Key/Element use a present numeric position. Find returns zero iff absent or a matching position; unlike the bounded map contract, its public nonzero postcondition does not promise the first match. Value requires Contains; it is a key lookup, not a positional read.

Insert requires absence, appends the pair and preserves both old arrays. Replace requires presence, preserves keys/length and all unrelated values through Same_Elements_Except. Copy makes the target a separate copy; Clear releases the arrays and empties the map. The package has no Remove, no Include (insert-or-replace) and no iteration; Vibe's dict uses the dictionaries below instead.

Lookup compares keys one by one, and each insertion builds new arrays, so the map suits small tables; for integer keys, Hash_Maps is hashed. An allocation that fails raises Storage_Error.

Dictionaries and the Vibe dict route

Unbounded.Dictionaries supplies insertion-order upsert. Include replaces an existing value without moving the key, or appends a missing key. Key_At/Value_At use positions; Key_Model/Value_Model expose the ordered arrays. Position gives the first matching key position, zero iff absent, and stays within Length. Contains/Value use key presence. Clear clears both owned vectors.

Get gives a key's value or a default. Delete removes a key and its value, the others keeping their order (Removed, for the key and value models), and Pop does so giving the value; both require Contains. Update includes each of another dictionary's keys with its value, and Copy makes a separate copy. Each key is held once (the type invariant).

Iteration returns the keys in the order they were first added. Same_Except and Kept_Unless are ghost predicates the postconditions use to say which values are kept. Dictionary is limited, because it holds two vectors.

Vibe maps dict[K, V], subscript read and write, len, in, iteration, items(), keys(), values(), get, del, pop, update, copy and clear to this family. Reading d[key] requires the key to be present: prove key in d first, or have a contract establish it. Lookup compares keys one by one with the key type's equality.

Unbounded ordered sets

Unbounded.Ordered_Sets supplies Length, Is_Empty, Element, Contains and an array Model. Has and Only_Added are ghost membership/preservation helpers. Insert needs absence. Include leaves an existing item present, inserting only when missing. Copy creates a separate logical set value in its target; Clear releases storage. Iteration follows the increasing representation order.

Exclude removes an element if present and Remove one that is (its precondition); "or", "and", "-" and "xor" are the union, intersection, difference and symmetric difference, their membership stated both ways through the models; "<=", "<", ">=", ">" and "=" compare sets by inclusion. Each element is held once (the type invariant).

In Vibe, a set[T] annotation selects this package: .add calls Include, .discard Exclude, .remove Remove, and |, &, -, ^ and the comparisons are the operators above; len, in, iteration, clear and .copy have counterparts. The functional sets are a different, ghost-only family. The element type's < must be a strict order. Insert searches and builds a new array, and positions after the inserted element shift by one; the set operators build their results by insertion.

Instantiate and test

Vada: package Items is new Verada.Containers.Unbounded.Vectors (Integer); after the matching with clause. Varc: package Items = new Verada.Containers.Unbounded.Vectors<Integer>; after import; arguments the call changes take &. Vibe uses its own list, dict and set annotations, which instantiate these packages for it.