DEVELOPMENT EDITION · 769442725406
Language Reference
On this page

APL-style array programming and embedded APL

Verada reads a subset of APL inside Vada, Varc and Vibe programs. APL text is a code string: a literal the compiler reads when it compiles, over the host program's own typed arrays, scalars and functions. It is typed, lowered to the same array expressions the host syntax would have written by hand (iterated aggregates and reductions), proved like them and compiled like them. There is no APL run time and no run-time evaluation of text.

A first look

The same function in each syntax: the mean of a vector, rounded down.

Syntax Spelling
Vada function Mean (X : Vector) return Integer is (APL ("⌊ (+/ X) ÷ ≢ X")) with Pre => X'Length in 1 .. 1_000;
Varc pub fn Mean(X: Vector) -> Integer requires X::Length in 1..=1000 = apl"⌊ (+/ X) ÷ ≢ X";
Vibe def mean(x: Vector) -> int: / requires(1 <= len(x) and len(x) <= 1000) / return apl("⌊ (+/ x) ÷ ≢ x")

+/ X sums X, ≢ X counts it, ÷ divides, and ⌊ rounds down. Under ⌊, a division of integers is integer floor division, so no float is involved; veradic translate --to vada --plain-ada shows what the text means as Ada:

function Mean (X : Vector) return Integer is ((((([for Apl_K0 in 0 .. X'Length - 1 => X (X'First + Apl_K0)]'Reduce ("+", Integer (0))) - (([for Apl_K0 in 0 .. X'Length - 1 => X (X'First + Apl_K0)]'Reduce ("+", Integer (0))) mod X'Length)) / X'Length))) with Pre => X'Length in 1 .. 1000;

The precondition is what lets the sum prove: with Vector's components in -100 .. 100 and at most 1,000 of them, the sum cannot overflow. A complete tour in all three syntaxes is in examples/apl.

Writing APL text in each syntax

Syntax Form Notes
Vada APL ("...") Needs pragma Extensions_Allowed (On); at the head of the file; without it the text is B0502, with a fix-it that adds the pragma
Varc apl"..." A tagged literal; no pragma needed. Varc prints code strings raw, so APL's \ is not an escape
Vibe apl("..."), or apl(r"...") when the text holds \ A plain call, so the module stays valid Python; any argument other than a string literal is refused (B0401)

Rules common to all three:

Values, positions and shapes

An APL value has an element type, a rank fixed at compile time, and a shape whose extents may be run-time lengths. A host array of k dimensions is a rank-k value; a host scalar has rank 0. Element types are integer types, floating-point types and Boolean; a comparison or a mask gives Boolean, and 0s and 1s written out in a mask are Booleans.

Positions count from 0 on every axis, whatever the host array's index type: position 0 is the host dimension's 'First. So ⍳ 3 is 0 1 2, and the results of ⍋, ⍒ and dyadic ⍳ are positions from 0, even when the array they index is Positive-indexed. A result is materialised in the target type's bounds: assigned to an unconstrained Positive vector it starts at 1, to a Vibe array at 0.

Matrices are rectangular and laid out as one contiguous block (row-major), so +/ along a row and +⌿ down a column are both plain loops over that block. There are no nested (boxed) arrays and no ragged rows.

The subset

Every form below runs in all three syntaxes. The tour's output shows each on the vectors X ← 3 1 4 1 5 9, Y ← 2 7 1 8 2 8 and the matrix A ← 2 3 ⍴ 1 2 3 4 5 6.

Scalar functions

Glyph Monadic Dyadic Notes
+ identity add overflow is a proof obligation
- negate subtract
× sign: × X - 3 is 0 ¯1 1 ¯1 1 1 multiply
÷ reciprocal divide, with a float result see Division
⌈ ⌊ ceiling, floor of a float (⌈ 1.5 2.5 is 2.0 3.0); an integer is its own maximum, minimum
| magnitude residue: a | b is b mod a APL's order and sign
* e to the power, a float power: 2 * 0 1 2 3 is 1 2 4 8 * x needs x <= 80
⍟ natural logarithm, a float b ⍟ x, the logarithm of x to the base b: 2 ⍟ 8 is 3.0 positive arguments; a base of at least 2 or at most 0.5
○ pi times k ○ x, circular function k (below) each function's domain
= ≠ < ≤ > ≥ comparisons, giving Boolean
∧ ∨ ⍲ ⍱ and, or, nand, nor (Booleans only) no GCD or LCM on integers
~ not
! factorial of a non-negative integer: ! 1 + ⍳ 5 is 1 2 6 24 120 binomial: k ! n is n choose k, 2 ! 4 5 6 7 is 6 10 15 21 integers only; overflow is an obligation

*, ⍟ and ○ are the functions of Ada.Numerics.Long_Elementary_Functions, of Long_Float values (the result is of the values' float type when the text shows one). Each function's domain is its precondition, so ⍟ x asks the prover to show x > 0.0 at each element. A unit whose APL text uses one of them needs no with clause for it. k ○ x takes k written out:

k k ○ x k k ○ x
0 (1 - x²) to the power ½ ¯4 (x² - 1) to the power ½
1 sine ¯1 arcsine
2 cosine ¯2 arccosine
3 tangent ¯3 arctangent
4 (1 + x²) to the power ½
5 6 7 hyperbolic sine, cosine, tangent ¯5 ¯6 ¯7 their inverses

Dyadic scalar functions apply element by element to two arrays of the same shape, or to an array and a scalar (10 × X is 30 10 40 10 50 90). A one-element vector is extended as a scalar is (APL's singleton extension): (1 ↑ Y) + X is 5 3 6 3 7 11. Otherwise the shapes must agree, a check the prover reports where it cannot show it (see Shapes and proof).

Structure

Form Meaning Tour result
⍳ n 0 1 … n-1 ⍳ 3 is 0 1 2
≢ x the count of the first axis
⍴ x the shape
s ⍴ x reshape, cycling x in row-major order 2 3 ⍴ X is 3 1 4 | 1 5 9
, x ravel (row-major) , A is 1 2 3 4 5 6
x , y catenate along the last axis (vectors, scalars, matrices) X , 7 8 ends 9 7 8; A , A is 1 2 3 1 2 3 | 4 5 6 4 5 6
⍪ x table: the first axis kept, the rest ravelled (a vector is a column) ⍪ 1 2 is 1 | 2
x ⍪ y catenate along the first axis (a vector beside a matrix is a row) A ⍪ 7 8 9 ends | 7 8 9
⌽ x, ⊖ x reverse along the last, first axis ⌽ X is 9 5 1 4 1 3
k ⌽ x rotate left by k (modulo the length) 2 ⌽ X is 4 1 5 9 3 1
k ⊖ x rotate along the first axis 1 ⊖ A is 4 5 6 | 1 2 3
⍉ x transpose ⍉ A is 1 4 | 2 5 | 3 6
n ↑ x take; negative n from the end; past the end pads with 0 ¯2 ↑ X is 5 9, 8 ↑ X ends 0 0, ¯5 ↑ 3 1 4 is 0 0 3 1 4
n ↓ x drop; negative n from the end 2 ↓ X is 4 1 5 9
x[i] indexing by positions from 0 (x[⍋ x])
i ⌷ x index: the positions i (written out) along the leading axes 2 ⌷ X is 4; 1 ⌷ A is 4 5 6
≡ x depth: 0 for a scalar, 1 for an array (Verada's arrays are not nested)
x ≡ y, x ≢ y match, not match: the same shape and elements X ≡ X is true
⊣ ⊢ left, right (⊢ separates a rank operand from its argument)

A reshape's left argument must have a length the text shows (numbers written out, or ⍴ y), because the result's rank must be known when compiling.

Selection, search and order

Form Meaning Tour result
m / x compress by a Boolean mask (a comparison, a membership, 0s and 1s, or a host array of Boolean) (X > 3) / X is 4 5 9
⍸ m where: the positions a Boolean mask keeps (m / ⍳ ≢ m); of counts, each position as many times ⍸ X > 3 is 2 4 5
c / x replicate by non-negative counts 2 0 1 0 1 1 / X is 3 3 4 5 9
x ∊ y membership, a Boolean mask of x's shape X ∊ Y is 0 1 0 1 0 0
x ⍳ y index of: each y's first position in x, ≢ x where absent Y ⍳ 1 8 5 is 2 3 6
⍋ x, ⍒ x grade up, down (stable) ⍋ X is 1 3 0 2 4 5
x[⍋ x] sort 1 1 3 4 5 9

A compress has a length known only at run time. It may be the text's whole value (a vector collected in one pass) or be reduced (+/ (X > 3) / X, a reduction with the mask as its filter); used anywhere else, B0711 says where it may stand. A negative count in replicate is an error the prover must rule out. Masks combine: (x ∊ y) / x keeps what x shares with y.

Reductions and scans

Form Meaning Tour result
f/ x reduce along the last axis; a matrix gives a vector of row results +/ X is 23, +/ A is 6 15
f⌿ x reduce along the first axis +⌿ A is 5 7 9
f\ x, f⍀ x scan: each prefix's reduction +\ X is 3 4 8 9 14 23

The reducers are + × ⌈ ⌊ ∧ ∨ and -. Reductions follow APL's right-to-left order: -/ 1 2 3 is 1 - (2 - 3), which is 2, and -/ X is the alternating sum 1. An empty axis gives the reducer's identity in the values' type: 0 for +, 1 for ×, True for ∧, False for ∨, and T'First and T'Last for ⌈/ and ⌊/ (so ⌈/ of an empty Integer vector is -9223372036854775808). The values' type comes from the first host array or literal in the reduced text; text with neither is refused (B0711).

+/ over floats is the plain sum. It is not Vibe's compensated sum() and not the lane sum; use those consumers in the host when the rounding matters.

Operators, dfns and trains

Form Meaning
f¨ x each: f applied to every element
f⍨ commute: x f⍨ y is y f x, and f⍨ x is x f x
x ∘.f y outer product: (1 + ⍳ 3) ∘.× 1 + ⍳ 4 is a 3 by 4 multiplication table
x f.g y inner product: A +.× B is the matrix product
f⍤k x rank: f on each cell of rank k; (+/)⍤1 ⊢ 3 2 ⍴ X is 4 5 14
{… ⍺ … ⍵ …} a dfn, with guards ({⍵ < 0: -⍵ ⋄ ⍵}) and local names; no recursion
(f g h), (g h) trains with Dyalog's meaning: (+/ ÷ ≢) X is the mean, a float
f∘g compose: (-∘|) x is - | x; dyadic x f∘g y is x f g y
A∘f, f∘A bind an argument: (1∘+) X adds 1, (×∘2) X doubles
f⍥g over: x f⍥g y is (g x) f (g y); monadic as f∘g
f⍛g before: (f⍛g) y is (f y) g y, x f⍛g y is (f x) g y
f⍣n power: f applied n times, n written out: (1∘+⍣2) X adds 2
v@i, f@i at: the elements at positions i along the first axis replaced by v (a scalar, or one for each position) or by f of them: 0@1 ⊢ X is 3 0 4 1 5 9

The rank is one or two integers written out; a negative rank counts the axes left out. Dyadic a f⍤j k b needs equal frames, or one side with a single cell. A dfn is expanded where it is used, with that use's types and ranks, so it costs nothing at run time.

Host functions

A Verada function is an APL function by its name:

Division and residue

÷ is APL's division: its result is a float, of the values' float type when the text shows one and Long_Float otherwise, so X ÷ 2 is 1.5 0.5 2.0 …. Under ⌊ or ⌈, a division of integers is integer floor or ceiling division (⌊ a ÷ b), exact and with no float in between. A zero divisor is a proof obligation.

Residue keeps APL's order and sign: a | b is Ada's b mod a, so 3 | X is 0 1 1 1 2 0 and ¯3 | ¯2 ¯4 ¯1 is ¯2 ¯1 ¯1. A left argument that is zero at run time gives b, as in APL, and so does a literal 0: 0 | X is X.

Words for the same operations

The pipelines and APL share one vocabulary: a word and the glyph that means the same lower to the same node, so they prove and translate alike. Each word keeps its home language's meaning where it differs from APL's (a word take never pads, fold runs left to right).

Operation APL Varc Vibe Vada
transpose ⍉ m m.transpose() m.T
rotate k ⌽ x x.rotate(k)
reshape r c ⍴ x x.reshape(r, c)
sort, grade x[⍋ x], ⍋ x x.sorted(), x.sorted_indices()
inner product x +.× y x.dot(y) a @ b
take, drop from the end ¯n ↑ x, ¯n ↓ x .take_last(n), .skip_last(n) (never pad) slices with 'Last
right-to-left fold f/ x .rfold(r, f)
scan f\ x .scan(init, f) (Rust's) accumulate(xs) (running sums) X'Scan (F, Init) (extension)

The words apply to named arrays, with names or whole numbers as arguments.

Outside the subset

B0711 names each of these with its reason:

Glyphs Reason
⍎ Verada reads APL text when it compiles, never at run time
⍕ format with the host's 'Image
⊂ ⊃ ⊆ Verada's arrays are not nested; the rank operator ⍤ covers work on each row
? values are proved; draw random numbers in the host
⍣≡, f⍣g a fixpoint's termination would need a proof; write the loop in the host (f⍣n with n written out is in)
⌹ matrix division: solve the system in the host
⎕IO and other system names never; positions always count from 0

B0711 also refuses expand (\ with an array on the left), dyadic transpose, monadic ↑, interval index (dyadic ⍸) and recursive dfns (∇). Write these in the host, or with the pipeline words.

Shapes, errors and proof

Ranks are checked when compiling: a phrase whose ranks do not fit is B0711. Shapes can depend on run-time lengths, so a shape requirement becomes a check the prover must discharge, reported at the glyph like any other. A function adding two vectors with no precondition:

function Add (X, Y : Vector) return Vector is (APL ("X + Y"));
Z : constant Vector := Add ((1, 2, 3), (10, 20));

is refused by prove, build and run, with the expansion of the text as a fix-it:

error[B5111]: `raise` might be reached
 --> main.vadb:7:59
  |
7 |    function Add (X, Y : Vector) return Vector is (APL ("X + Y"));
  |                                                           ^ `raise Constraint_Error` might be reached
help: write the APL text as what it means here
  |
7 |    function Add (X, Y : Vector) return Vector is ([for Apl_I0 in 1 .. (1 + ((if X'Length = Y'Length then X'Length else raise Constraint_Error with "the arguments' shapes differ"))) - 1 => (X (X'First + (Apl_I0 - 1)) + Y (Y'First + (Apl_I0 - 1)))]);

The repair is the precondition the shape needs, Pre => X'Length = Y'Length. Overflow works the same way: +/ X over unbounded Integer components does not prove, and the counterexample shows an accumulator near Integer'Last. Bound the components' subtype (as the tour's Small does) and the length (Pre => X'Length <= 1_000), and the generated invariants prove the sum. The expansion fix-it is also the way to see what any text means, and the place to add a Loop_Invariant when the generated facts are not enough.

What the prover knows about a result without help:

Speed

APL text compiles to the loops a hand-written version would have: element-wise phrases and reductions fuse into one pass without temporaries, and once proved they vectorise exactly as the hand-written loops do (a dot product and a maximum give the same vector code and the same run time). Know these costs before using the following on long vectors:

Operation Cost
scans (f\, f⍀, 'Scan, .scan, accumulate) quadratic: each position reduces its whole prefix
replicate by counts (c / x) quadratic
grades and sorting (⍋, ⍒, x[⍋ x], .sorted()) cubic
transpose not vectorised (a gather)
compress under --plain-ada quadratic in the Ada written for GNAT (Verada's own compress is one pass)

These follow from expressing every result as a function of its position, which keeps proofs simple. Keep scans, replicate and grades to short vectors, or write the loop in the host.

Tools

The REPL's APL mode

:apl in the REPL (Vada, Varc or Vibe) makes each line APL text over the session's names, and :apl again leaves. A value is shown with its shape; name ← text keeps a value as a session constant; a backtick and a key type a glyph. A Vada session:

verada> X : constant array (1 .. 5) of Integer := (3, 1, 4, 1, 5);
verada> :apl
APL mode: each line is APL text over the session's names; `name ← text` keeps a value; a backtick and a key give a glyph (`i is ⍳, `r is ⍴); :apl leaves
  apl> +/ X
 14
  apl> ⌽ X
⍴ 5: 5 1 4 1 3
  apl> (⍳ 3) ∘.× ⍳ 4
⍴ 3 4
 0 0 0 0
 0 1 2 3
 0 2 4 6
  apl> `$ X
⍴ 5: 1 3 0 2 4
  apl> Y ← 2 × X
  apl> +/ Y
 28
  apl> X ÷ 2
⍴ 5: 1.50000000000000E+00 5.00000000000000E-01 2.00000000000000E+00 5.00000000000000E-01 2.50000000000000E+00
  apl> :apl
back to the session's syntax
verada> Y'Length
 5

The mode shows values of Integer, Long_Float and Boolean, as scalars, vectors and tables. A mistake is reported at the line as typed, at its glyph. A Vibe session shows them with Python's images (14, True) and names its values as Vibe does (apl_result, int, float and bool, and array classes APL_Integers to APL_Real_Table).

Typing glyphs

A backtick and a key give a glyph, after Dyalog's layout, in the REPL's APL mode and as completions inside APL text in an editor (the language server's trigger character is `; not in comments or ordinary strings):

Key Glyph Key Glyph Key Glyph Key Glyph
`i ⍳ `r ⍴ `e ∊ `7 ≢
`a ⍺ `w ⍵ `y ↑ `u ↓
`s ⌈ `d ⌊ `- × `= ÷
`8 ≠ `4 ≤ `6 ≥ `t ~
`9 ∨ `0 ∧ `% ⌽ `& ⊖
`^ ⍉ `$ ⍋ `# ⍒ `j ∘
`[ ← `' ⋄ `1 ¨ `T ⍨
`J ⍤ `/ ⌿ `\ ⍀ `p ⊢
`h ⊣ `2 ¯ `{ ⍝

Hovering over APL text in an editor shows the type of the phrase at the glyph. Shapes are not shown in a hover.

Translation and GNAT

veradic translate keeps APL text as it is, in the target syntax's form (APL ("..."), apl"...", apl("..."), raw where the text holds \). translate --to vada --plain-ada writes each text as the plain Ada it means, so a Vada program with APL can be checked by GNAT: the tour prints the same under GNAT 15.1.2 as under Verada. A scan of a filtered sequence has no plain-Ada form and is reported so.

CPython

A Vibe program with apl(...) is valid Python, but CPython needs a verada module to run it. Verada's test suite has one (test/vibe/verada.py), with a small interpreter of the subset over Python numbers and lists, used to check that Verada and CPython print the same; it is a test oracle, not a distributed run time.

Further examples