DEVELOPMENT EDITION · 769442725406
Vibe syntax
On this page

Vibe iteration, pipelines, and multidimensional arrays

Vibe uses Python's iterator spellings for statically typed iteration and pipelines that are consumed where they are written; no iterator object exists at run time. Each source, filter, stopping condition and consumer has its proof obligations. See shared pipelines and composite types.

Range and ordinary container loops

range(stop) starts at zero and excludes stop. range(start, stop) includes start and excludes stop. A range whose start is greater than its final included value is empty. The compiler normally represents the upper bound as stop - 1, which has its own numeric well-definedness obligations; the pattern range(start, n + 1) can avoid that subtraction by recognizing the inclusive bound n.

reversed(range(...)) walks a range backwards, and reversed(a) of an array walks its elements from the last, as Ada's for E of reverse A. Loop parameters are read-only values.

for element in source works for suitable arrays and supplied iterable containers. Lists iterate values in their order, dictionaries iterate keys in insertion order, and sets iterate their elements in ascending order. An array's loop iterates its components; loop over a range of positions, or m.indices(k), when the index itself is needed.

Lockstep sources

for left, right in zip(a, b):
    compare(left, right)

for position, value in enumerate(values):
    inspect(position, value)

These body fragments assume typed sources and suitable operations. A zip loop has one simple target name for each source, and stops when the shortest source is exhausted. enumerate takes one source and counts from zero, whatever the array's first index.

One source of a zip may be reversed, and pairs as Python does: zip(reversed(range(3)), range(3)) gives (2, 0), (1, 1), (0, 2). Two reversed sources are B0401; walk both forwards instead.

Take-while and filter order

Import the unqualified helper with from itertools import takewhile. The supported form is takewhile(lambda element: condition, source), with a one-parameter inline lambda. It stops at the first element failing the Boolean condition. The lambda is syntax for this stage, not an allocated closure usable everywhere else.

The order of filtering and stopping changes the result, and Vibe keeps the order written. A filter after the stage applies to the elements before the stop:

sum(x for x in takewhile(lambda y: y > 0, values) if keep(x))

stops at the first value that is not positive, then adds the kept ones among those before it. A filter inside the source applies first, so the stop condition sees only the kept values:

sum(x for x in takewhile(lambda y: y > 0, (x for x in values if keep(x))))

Both fragments require a pure Boolean keep and suitably bounded values. A for loop over a takewhile stage, with an if in its body, behaves as the first form.

Reductions and quantifiers

Form Consumer
sum(values) Add elements starting with zero
sum(expression for x in source if condition) Map/filter then add
all(predicate for x in source) Universal Boolean condition
any(predicate for x in source) Existential Boolean condition
all(x <= y for x, y in zip(a, b)) Lockstep quantified pipeline
sum(int(x) for i, x in enumerate(values) if i % 2 == 0) Counter-aware filtered reduction

A generator expression has one for clause and is consumed where it is written. Use a loop when a reduction cannot express the state or the stopping behavior required.

Consumers and stages must obey purity/type rules. The compiler can generate accumulator and bounds invariants, but it cannot prove a sum safe without sufficient bounds on element magnitudes and length. For example, the pipeline corpus bounds percentage values and sequence length before summing products. A fold that overflows must be repaired or given a different representation, not hidden behind generator syntax.

sum over floats is the compensated (Neumaier) sum, as CPython's is; plain_sum adds left to right, and lane_sum is the reproducible eight-lane sum. Neither proves equal to the exact real sum. APL's +/ inside apl("...") is the plain sum; the APL chapter describes it and the rest of the array vocabulary.

List comprehensions versus array aggregates

A typed local list declaration has a dedicated filling path:

squares: list[int] = [n * n for n in range(10) if n % 3 == 0]

This function-body fragment builds a list by appending selected values. Its target is a name and its source a range or a typed container; the compiler adds a length invariant for a range. A comprehension assigned to a typed array is that array's aggregate, one component for each element of the source (collections).

Arrays and matrices

An Array class fixes its component type. size, index, shape, and rank determine its indexing/bounds model:

from verada import Array, RangeInt

class Small(RangeInt, ge=-100, le=100):
    pass

class Board(Array[Small], shape=(2, 3)):
    pass

class Matrix(Array[Small], rank=2):
    pass

These module fragments correspond to the matrix corpus. A Board has fixed two-by-three shape. A Matrix has two dimensions whose bounds/shape belong to its value. Construct using a compatible rectangular aggregate, for example a contextually typed nested list supplied to the array class. Nonrectangular data does not become a ragged Python list. Each dimension, length and index must satisfy the shared type/shape rules.

Use matrix[i, j] to index both dimensions. matrix.shape[0] and matrix.shape[1] read dimension lengths; the dimension selector must be a nonnegative literal. A rank-array index is a zero-based position relative to that value's bounds. A fixed index=SomeType array instead uses actual values of its declared index subtype, which may begin elsewhere, and so does a rank array declared with an index type, rank=2, index=Positive (Ada's array (Positive range <>, Positive range <>)), whose values made from lists start at 1. len(matrix) is the first dimension's length, not the total number of scalar components.

A value's bounds are a.first and a.last (its only or first dimension), and m.first_index(k) and m.last_index(k) for dimension k, counted from 0 as shape[k] counts it; for i in m.indices(k) walks dimension k's indices. The dimension is a nonnegative literal.

Rows are not separate list objects: a multidimensional array is one rectangular, contiguous block with its bounds. Assignments require compatible shapes and apply the value and bounds rules. a @ b is the matrix product and m.T the transpose; other whole-array operations are written in APL. Vibe arrays are typed values, not NumPy arrays: there is no broadcasting or dtype conversion.

Traversal and proof of matrices

for element in matrix traverses scalar components in the shared row-major order, with the last dimension varying fastest. It does not yield Python row lists. Explicit nested loops over range(matrix.shape[k]) make indices and row/column invariants available:

for i in range(m.shape[0]):
    for j in range(m.shape[1]):
        m[i, j] = Small(int(m[i, j]) * factor)

This mutating-body fragment requires a declared Small, suitable component bounds, and a bound on factor; the complete corpus supplies preconditions making the multiplication safe. Components and index checks must prove in every iteration. APL operations have their own chapter.

Source evidence: iterator/reduction/array lowering, pipeline representation, and complete matrix programs.