Skip to content

Finite collection expressions

QVR applies a function across a tensor of fixed shape in two ways. A pure map builds a value, and traverse runs a computation once per element, each run finishing before the next begins. Both keep the collection's shape in the result type.

Operation reference

Form Position Result Main restriction
map(xs, x -> body) pure expression tensor with the leading extent of xs body is pure
traverse(xs, x -> computation(...)) right side of <- tensor of computation results leading extent is known statically
fold(xs, init, acc -> x -> body) pure expression type of init leading extent is known statically
length(xs) pure expression Int leading extent is a literal
logsumexp_over(xs, x -> score) pure expression Real score returns one Real per item

All five operations run along the leading axis. In the function position of map, fold, logsumexp_over, and traverse, a lambda may be replaced by any function value: a parameter or local of function type, or the name of a computation with no static arguments (Function values). If xs has type A[Fin n, Fin d] and the lambda returns B[Fin k], then map and traverse return B[Fin n, Fin k].

Pure map

map takes an inline lambda or a lambda bound by a local pure binding:

define standardized(xs : Real[Fin 3], center : Real, scale : Real) : Real[Fin 3] !{} =
    let z_score = x -> (x - center) / scale
    return map(xs, z_score)
example
expect standardized([1.0, 2.0, 3.0], 2.0, 0.5) == [-2, 0, 2] exactly

The lambda may capture pure values in scope, such as center and scale, and its parameter shadows an outer name with the same spelling. Elaboration to the Quivers Indexed Effect Core (QIEC) lowers the form to a typed array comprehension, so the reference machine, the library's interpreter for QIEC, runs it and a target that supports comprehensions renders it.

Traverse with effects

traverse passes each item to a named computation and sequences the requests those calls make. It is computation syntax and uses <-, even when the callee performs no effect:

define center(x : Real, mean : Real) : Real !{} =
    return x - mean

define center_all(xs : Real[Fin 3], mean : Real) : Real[Fin 3] !{} =
    let centered <- traverse(xs, x -> center(x, mean))
    return centered
example
expect center_all([1.0, 2.0, 3.0], 2.0) == [-1, 0, 1] exactly

The lambda body is one call of a named computation, and the callee's effect row joins the row of the enclosing computation. QIEC expands a fixed collection into calls and bindings in source order, which gives each call its own stable structural path, so interpreters and transpilers need no separate traverse primitive.

The lambda's callee receives no static arguments. When the callee has a static telescope, wrap the specialized call in a helper with none.

Function values

A function parameter, a function local, or a named computation stands where a lambda would:

define add(total : Real, x : Real) : Real !{} =
    return total + x

define summarize(xs : Real[Fin 3], f : Real -> Real !{}) : Real !{} =
    let mapped = map(xs, f)
    let total = fold(xs, 0.0, add)
    let soft = logsumexp_over(xs, f)
    let walked <- traverse(xs, f)
    return mapped[2] + total + soft + walked[1]

A named computation given to fold takes the accumulator and the item as its two parameters. An operation applying a function value is expanded at its literal leading extent, one application per item, since the function is not known until the call. A pure operation refuses a computation whose row is not empty, pointing to traverse.

Fold and derived reductions

fold is a left fold. Its lambda is curried, taking the accumulator first and the current item second:

define weighted_total(values : Real[Fin 3], weights : Real[Fin 3]) : Real !{} =
    let products = map([0, 1, 2], i -> values[i] * weights[i])
    return fold(products, 0.0, total -> value -> total + value)
example
expect weighted_total([1.0, 2.0, 3.0], [0.5, 0.25, 0.25]) == 7/4 exactly

QIEC elaborates a fold at its fixed extent, one application of the body per item. When the body reads the accumulator more than once, or reads it inside a lambda or factor whose body runs once per item, each intermediate accumulator is bound once and every read refers to that binding. The elaborated term thus grows linearly in the collection's length, and each step runs once, even for a body that reads the whole state at every step, such as the message update of the forward algorithm (Rabiner, 1989), in which each new component reads every old one:

define forward(start : Real[Fin 2], transition : Real[Fin 2, Fin 2], emissions : Real[Fin 5, Fin 2]) : Real !{} =
    let alpha = fold(emissions, start, message -> emission -> map([0, 1], j -> logsumexp_over([0, 1], i -> message[i] + transition[i, j]) + emission[j]))
    return logsumexp_over(alpha, score -> score)

The same sharing applies to a map or fold source that is not a variable, and to a lambda bound by a local binding whose body reads its argument more than once: the source or argument is evaluated once and bound.

logsumexp_over(xs, f) computes \(\log \sum_x \exp f(x)\) over the items of xs, the log-sum-exp of map(xs, f) computed stably. It normalizes a finite posterior given its log joint, as in normalization:

define normalize(log_joint : Real[Fin 3]) : Real[Fin 3] !{} =
    let log_evidence = logsumexp_over(log_joint, score -> score)
    return map(log_joint, score -> exp(score - log_evidence))
example
expect normalize([0.0, 0.0, 0.0]) == [1/3, 1/3, 1/3] within 1e-12

length(xs) returns the leading extent as an Int. An extent bound by a static binder is an index term, not a runtime integer, so it is specialized before length or fold can use it.

Dynamic filtering

The checked surface refuses filter(xs, predicate) with the stable diagnostic qiec-collection:filter:dynamic-shape. The length of a filtered tensor depends on runtime values, and every QIEC tensor has a fixed shape, so typing the result would need a length quantified existentially or a ragged tensor type. A Boolean mask with a reduction keeps the original shape and expresses the same computation. The eager PyTorch expression evaluator accepts filter outside the checked surface.

define positives(xs : Real[Fin 3]) : Real[Fin 3] !{} =
    return filter(xs, x -> x > 0.0)

Diagnostics and target support

Collection errors are ordinary checked diagnostics. A wrong arity, a source that is not a tensor, a function argument that is not a function of the item, an effectful function in a pure operation, a leading extent known only at run time, or a result of the wrong type fails during qvr check. A program that cannot elaborate is reported as qiec-program-gap and is never omitted from the checked module without a diagnostic.

The reference machine runs every checked collection form. What each transpile target accepts is listed under Transpilation, and qvr check --target TARGET reports the features a target lacks before transpiling.

The finite-grid Bayes tutorial computes a complete posterior with these forms.

Cited sources

L. R. Rabiner. A tutorial on hidden Markov models and selected applications in speech recognition. Proceedings of the IEEE, 77(2):257–286, 1989. URL: https://doi.org/10.1109/5.18626, doi:10.1109/5.18626.