DEVELOPMENT EDITION · 769442725406
Vibe syntax
On this page

Vibe collections and tuples

Vibe collections are typed instances of supplied Verada libraries. Their operations have preconditions, models, ownership rules, and finite runtime size limits. They are not dynamic Python containers implemented by a Python interpreter.

Type and construction mapping

Annotation Underlying library/type Index/key convention
list[T] Verada.Containers.Unbounded.Vectors(T).Vector Zero-based Vibe positions mapped to one-based vector slots
dict[K, V] Verada.Containers.Unbounded.Dictionaries(K, V).Dictionary Actual key values; insertion order retained
set[T] Verada.Containers.Unbounded.Ordered_Sets(T).Set Membership by value; ordered library representation
tuple[A, B, ...], two to twelve components Verada.Vibe.Tuple_2 through Tuple_12 Literal zero-based component positions
list[str] Verada.Strings.Text_Lists.Vector (the list of texts that split gives) As list[T]
list[list[T]] Verada.Containers.Unbounded.Nested_Vectors(list[T]'s instance).Vector As list[T], for the rows and within each row

Use typed local declarations:

numbers: list[int] = [4, 5]
counts: dict[int, int] = {7: 2, 9: 1}
seen: set[int] = {3, 1}
pair: tuple[int, int] = (2, 8)

Empty typed forms are []/list(), {}/dict(), and set(); {} is an empty dictionary, as in Python. Give the element, key and value types even for a nonempty literal: an unannotated list literal is B2010.

A typed container declaration creates an empty container, then adds the literal's elements in order; each insertion meets the library's preconditions. Fixed arrays are a different type family: an Array class has declared bounds and does not grow.

Lists

The following operations have supplied vector counterparts:

Vibe operation Effect/obligation
len(xs) Current logical length
xs[i] Requires 0 <= i < len(xs); returns the element value
xs[i] = value Replaces a present element; preserves length and other elements
xs.append(value) Adds at the end; length must remain within library bounds
xs.insert(i, value) Inserts before zero-based position i; requires a valid insertion position, including the end
value: T = xs.pop() Removes/returns the last element; requires nonempty list
value: T = xs.pop(i) Removes/returns a valid zero-based position
xs.pop(), xs.pop(i), del xs[i] as statements Remove the last element, or the one at i, discarding it
xs.extend(ys) Appends the source sequence, subject to size and aliasing obligations
xs + ys, xs * n, n * xs A new list: the two joined, or n copies (none when n <= 0); the length must fit
ys: list[T] = xs.copy() Creates a separate list by the library's explicit Copy operation
xs.clear() Makes the vector empty according to its library contract
for value in xs Iterates present elements in order
value in xs Whether an element equals value (not in, whether none does)
xs.index(value) The first position of an equal element; requires value in xs (Python's ValueError)
xs.count(value) How many elements equal value
xs.remove(value) Removes the first equal element; requires value in xs
xs.reverse(), list(reversed(xs)) The elements in reverse order, in place or as a new list
xs.sort(), sorted(xs) In increasing order of <, in place or as a new list; stable, as Python's
min(xs), max(xs) The first least, or greatest, element by <; requires a nonempty list
sum(xs), sum(xs, start) The sum, from 0 or after start (of a list written out, sum([1, 2, 3]), too)
print(xs), str(xs), repr(xs), f"{xs}" Python's repr, [1, 2] and ['a', 'b'] (a dict's {'a': 1}, a set's {1, 2} or set(), a tuple's (1, 'a')), of elements that are int, float, bool or str; a set shows its elements in increasing order, where CPython's order is its hash table's
any(xs), all(xs) Of a list[bool]: whether some, or every, element is true

pop changes the list and returns a value, so used for its value it is the whole right-hand side of a typed declaration or assignment, as other changing calls are; used as a statement, its value is discarded. Positions count from 0 and must be present (a negative number written as an index counts from the end, xs[-1]), and insert's position must be at most len(xs), where Python would clamp it. wanted in xs asks whether a list holds a value (not in whether it does not). xs[a:b] is a new list of the elements from position a up to b, its bounds kept within the list as Python keeps them (slices).

The operations that compare elements (sort, sorted, min, max) take the element type's <, which must be a strict order (an int, a float without NaNs, a str, an enumeration, or a class with a < of its own): their preconditions say so for the list's elements. The contracts state the results for proofs: a sorted list is in order and a permutation of the old one, count is the number of equal elements, index the first equal one, and xs + ys holds xs's elements and then ys's.

The list has no capacity chosen in advance, but its length is finite and it can exhaust storage. It is not copied by assignment: copy it with .copy(), and see sharing for moves and views.

Lists of lists

A list[list[T]] holds its rows in place, each a list[T] (Nested_Vectors):

grid: list[list[int]] = [[1, 2], [3, 4]]
board: list[list[int]] = [[0] * 3 for _ in range(2)]
row: list[int] = [5, 6, 7]
grid.append(row)          # a copy of row, as a new last row
grid[0][1] = 20           # an element of a row replaced
grid[1].append(9)         # a row grown in place
first = grid[0]           # a copy of the row, where first is only read
for line in board:        # each row in turn
    line.append(1)        # changed in place: the row is moved out and back
print(len(grid), len(grid[1]), grid[0][1])

len(grid), len(grid[i]), grid[i][j], grid[i][j] = x, grid[i].append(x) and grid.append(row) are the library's operations, which keep every other row's length, so a proof knows the rows a statement did not touch. A name bound to grid[i] and changed, or a for over the rows whose body changes its row, is an element view (D127): the row is moved out and put back where the name's statements end, and grid itself is not used meanwhile (B0401 says so where it is). grid.append(row) stores a copy, where Python would share row; changing row afterwards is refused, as for any stored value. A comprehension of rows [[x] * k for _ in range(n)] states in its loop that every row has k elements.

Three Python forms are refused with B0401, each with what to write: [[0] * 3] * 2, whose rows Python makes one shared list (write a comprehension, a row of its own for each); print(grid) and the other strs of a whole list of lists (print the rows); and a name bound to a row while grid is used again before it ends.

Dictionaries

d[key] requires the key to be present. State key in d before lookup, or establish it through an insertion/postcondition. d[key] = value includes a new key or replaces an existing value. Replacing preserves that key's first-insertion position; a newly added key comes last. len(d) is the number of keys. for key in d iterates keys in insertion order. These rules are exercised with numeric and text keys in the collection corpus.

Vibe operation Effect/obligation
d.get(key, default) The key's value, or default when it is not a key
del d[key], d.pop(key) as a statement Removes the key and its value; requires key in d (Python's KeyError); the other keys keep their order
value: V = d.pop(key) As del, giving the value
d.update(other) Each of other's keys takes other's value, new keys last in other's order
e: dict[K, V] = d.copy() A separate dictionary with the same keys and values
for k, v in d.items() The keys and their values, in insertion order
for k in d.keys(), for v in d.values() The keys, or the values, in insertion order
d.clear() Empties the dictionary

Lookup searches the keys in order, so it takes time proportional to the dictionary's size. Keys are compared with the key type's equality. d.get(key) without a default is a V | None, None for a missing key, which needs its type where it is used (age: int | None = ages.get("ann"), see optional values); d.get(key, default) is the default for a missing key.

Sets

s.add(value) inserts a value that is not present and leaves the set unchanged otherwise. len(s), value in s, iteration, .clear() and t: set[T] = s.copy() work as in Python. s.discard(value) removes a value if present; s.remove(value) requires it (Python's KeyError). s | t, s & t, s - t and s ^ t are new sets (union, intersection, difference, symmetric difference), and s <= t, s < t, s >= t, s > t and s == t compare sets as Python does (subset, proper subset, and so on). The element type needs an ordering, and iteration follows it, from the least element; CPython promises no set order at all.

Tuples

A tuple is a record value with two to twelve typed components. The annotation tuple[int, str] fixes the types of positions 0 and 1. A tuple is indexed by a literal position (pair[1]); for an index computed at run time, use an array. == and != compare every component, and <, <=, > and >= order tuples as Python does, by their first components that differ (each component's type must be ordered). Tuple packing, assignment and unpacking follow statement rules.

Comprehensions and returning collections

A comprehension is the initial value of a typed list, dictionary or set:

values: list[T] = [expression for name in source if predicate]
squares: dict[int, int] = {x: x * x for x in xs}
odd: set[int] = {x for x in xs if x % 2 == 1}
pairs: list[int] = [x * 10 + y for x in xs for y in ys]

Each for clause, in order, is a loop over a range or container (or enumerate, zip, or a dictionary's items()), nested in the one before, with its if filters; each selected value is appended, included (a later key's value replaces an earlier one's, as in Python), or added. With one for over a range or a named container, the length is bounded by the elements seen so far, so each insertion's precondition is proved. With several for clauses the result can be as long as the product of the sources' lengths, which the prover cannot bound: their insertions' preconditions are not proved, so where a proof is needed write the loops, with an invariant bounding the result's length.

A comprehension assigned to a typed array, r: Row = [n * n for n in range(3)], is the array's aggregate, one component for each element of the source that passes the if filters ([n for n in range(7) if n % 2 == 0] has four). enumerate(xs, start) (or start=) counts from start.

A function returning a container returns a comprehension (return [x for x in xs if x > 0], whose length is bounded as above), or builds its result in place: declare a typed local container, fill it, and return it. A complete example is word counting.

Element values and instance identity

Element reads yield values; changes through local element views are explained under sharing. Keeping a view can constrain later operations on the containing collection. Storing a record into a collection is a value-copy operation, not unconditionally Python's shared-reference insertion. Use explicit copy when that difference matters.

A collection of a class is the instance its defining module declares; see the generic and import rules.

Client proofs rely on embedded library contracts; they do not re-prove those bodies on each invocation. The bounded-vector guide illustrates reading contracts and limitations, but its capacity-bearing generic is not Vibe's list[T]. Source evidence: collection lowering, method/index resolution, and unbounded library specifications.