DEVELOPMENT EDITION · 769442725406
Programmer Guide
On this page

Arrays, streams and numerical work

Arrays make bounds visible. Pipelines express common loops (sum these, keep those, stop at the first of that) without writing the accumulator by hand. Both still have proof obligations: a compact notation does not make arithmetic unbounded or storage unlimited.

Start with the shelf's domain

The stockroom shelf has three slots indexed by Bin, 0 through 2, and each slot holds a Quantity of at most 100. The display loop visits exactly that domain, so every index is proved valid by the loop variable's range.

An array passed in by a caller may have any bounds. Loop over its own range (A'Range in Vada, a::Range in Varc), or over the elements themselves (for x in a in Vibe), never over a range copied from a different array.

Vada indexes with Items(I); Varc and Vibe with items[i]. A two-dimensional array takes two indexes at once: M(I, J) or m[i, j].

Bounds and sliding

An array's lower bound need not be 0 or 1, and two arrays of the same length can have different bounds. Assigning one whole array to another slides the elements into the target's bounds; Verada checks that the lengths match and each element fits the target's element type.

A slice keeps the bounds of the part it selects, which matters when you pass it on. The composite types chapter gives the exact rules.

Vibe's fixed Array classes have a declared index type; a list is indexed from zero. An index does not count from the end: a[-1] is rejected, and a[len(a) - 1] names the last element. A slice's bounds are Python's, so a[1:3] and a[-2:] (the last two elements) work as they do in Python.

Replace an accumulator when the meaning stays clear

A sum starts at zero and adds each value. The stockroom's three-bin total is small enough to write out. For an array of any length, a reduction says the same thing:

Syntax Sum of the scores at or above mark
Vada [for X of A when X >= Mark => X]'Reduce ("+", 0)
Varc a.filter(x => x >= mark).sum()
Vibe sum(x for x in a if x >= mark)

The pipeline specification in the compiler's tests defines a bounded score array and its total with a length precondition. Each element is at most 100 and the length is bounded, which bounds the accumulator; the result's subtype alone would not prove the intermediate additions safe. When a reduction fails to prove, the diagnostic often suggests the loop invariant that bounds the accumulator.

Replacing a hand-written total with a reduction can change what a caller can prove from the contract. Keep the relationship the caller needs in the postcondition.

Map, filter, stop and collect

A map transforms each element, a filter drops the elements that fail a test, a take-while stops at the first element that fails a test, and a collect builds an array or container from what remains. The stages apply in the order written: in Varc, a.take_while(x => x > 1).filter(x => x != 1).sum() on [2, 1, 3] stops at 1 and gives 2.

A filter can establish a fact a later map relies on, such as a nonzero divisor. A filtered collection can be shorter than its source, so a fixed-size destination needs a length that fits. Stages are fused into one loop with no intermediate storage, but the final collection still occupies memory.

A pipeline is consumed where it is written: it ends in a consumer (sum, collect, find, all and so on), and it cannot be stored and resumed later like a Python generator. Lambdas in a pipeline are part of that pipeline, not first-class function values.

Walk sources together

Lockstep iteration (zip) advances several sources together and ends when the shortest runs out. If every element of every source must be used, state equal lengths as a precondition.

An enumerate counter counts from zero; it is not the array's index unless the array starts at zero. In Vada, iterating several sources together or with a while clause is a Verada extension enabled by pragma Extensions_Allowed (On);. The pipelines chapter has the forms in all three syntaxes.

Search without inventing a sentinel

When a search may find nothing, return an optional value rather than a "not found" index that looks like a real one. Varc's .find(...), .first() and .last() return an Optional of the element type, from an instance of Verada.Optionals the program declares and uses (package Score_Options = new Verada.Optionals<Score>; use Score_Options;): test Has_Value before taking Value. Vibe writes T | None and tests x is not None. See optional consumers and arenas and optional values.

Matrices

Multidimensional arrays work in all three syntaxes and are traversed in row-major order. A matrix product needs matching inner dimensions, and a transpose swaps the shape; state these in the contract. The compiler's Vada, Varc and Vibe matrix projects are complete examples, and embedded APL works on the same arrays.

Floating-point sums

Floating-point addition is not associative: adding the same numbers in a different order can give a different result. Vibe's sum of floats uses compensated summation, as Python's does, so sum of ten 0.1 values is exactly 1.0, while a plain loop gives 0.9999999999999999. A proof about real-number sums says nothing about either, so state the property you need about the algorithm you use. See numerical reasoning.

When compact notation stops helping

If a pipeline's proof needs a relationship the generated invariant cannot express, write the loop yourself, or use the editor's quick fix that expands the pipeline into a loop. Then state an invariant about the processed prefix and show how it gives the postcondition at the exit. This gives your explanation a place; it does not weaken any check.