APL, inside
The array language,
proved and native.
Write APL's famous one-liners inside Varc, Vibe or Ada. Verada reads them when it compiles, proves every shape and every sum, and turns them into the same fused loops you would have written by hand. No interpreter, no run time, no surprises.
A taste
One-liners with contracts.
Each function's body is a line of APL; its contract is what lets it
prove. ⌊ (+/ X) ÷ ≢ X is the mean: sum, count, divide,
round down. The precondition bounds the length, so the sum cannot
overflow, and Verada proves it.
// APL one-liners as functions, with the contracts that let them prove.
package Stats {
import Ada.Text_IO;
pub subtype Small = Integer range -100..=100;
pub type Vector = [Positive..]Small;
pub type Values = [Positive..]Integer;
pub type Table = [Positive.., Positive..]Small;
// the mean, rounded down: sum, count, divide, floor
pub fn Mean(X: Vector) -> Integer
requires X::Length in 1..=1000
= apl"⌊ (+/ X) ÷ ≢ X";
pub fn Dot(X, Y: Vector) -> Integer
requires (X::Length == Y::Length) & (X::Length <= 1000)
= apl"+/ X × Y";
// a local dfn: the sum of squares less the count
pub fn Spread(X: Vector) -> Integer
requires X::Length <= 100
= apl"sq ← {⍵ × ⍵} ⋄ (+/ sq X) - ≢ X";
// the values above a limit, in order
pub fn Above(X: Vector, Limit: Small) -> Values
requires X::Length <= 1000
= apl"(X > Limit) / X";
pub fn Sorted(X: Vector) -> Values = apl"X[⍋ X]";
// a multiplication table, N by N
pub fn Times(N: Natural) -> Table
requires N <= 10
ensures (result::Length(1) == N) & (result::Length(2) == N)
= apl"(1 + ⍳ N) ∘.× 1 + ⍳ N";
// each row's total
pub fn Totals(T: Table) -> Values
requires T::Length(2) <= 1000
= apl"+/ T";
pub fn Show(Label: String, V: Values) {
Ada.Text_IO.Put(Label);
Ada.Text_IO.Put(":");
for E in V {
Ada.Text_IO.Put(Integer::Image(E));
}
Ada.Text_IO.New_Line();
}
} import Ada.Text_IO; use Ada.Text_IO;
import Stats; use Stats;
fn Main() {
let X: Vector = (3, 1, 4, 1, 5, 9);
let Y: Vector = (2, 7, 1, 8, 2, 8);
Put_Line("mean:" ++ Integer::Image(Mean(X)));
Put_Line("dot:" ++ Integer::Image(Dot(X, Y)));
Put_Line("spread:" ++ Integer::Image(Spread(X)));
Show("above 3", Above(X, 3));
Show("sorted", Sorted(X));
Show("table totals", Totals(Times(4)));
} pragma Extensions_Allowed (On);
-- APL one-liners as functions, with the contracts that let them prove.
package Stats is
subtype Small is Integer range -100 .. 100;
type Vector is array (Positive range <>) of Small;
type Values is array (Positive range <>) of Integer;
type Table is array (Positive range <>, Positive range <>) of Small;
-- the mean, rounded down: sum, count, divide, floor
function Mean (X : Vector) return Integer is (APL ("⌊ (+/ X) ÷ ≢ X"))
with Pre => X'Length in 1 .. 1_000;
function Dot (X, Y : Vector) return Integer is (APL ("+/ X × Y"))
with Pre => X'Length = Y'Length and X'Length <= 1_000;
-- a local dfn: the sum of squares less the count
function Spread (X : Vector) return Integer is
(APL ("sq ← {⍵ × ⍵} ⋄ (+/ sq X) - ≢ X"))
with Pre => X'Length <= 100;
-- the values above a limit, in order
function Above (X : Vector; Limit : Small) return Values is (APL ("(X > Limit) / X"))
with Pre => X'Length <= 1_000;
function Sorted (X : Vector) return Values is (APL ("X[⍋ X]"));
-- a multiplication table, N by N
function Times (N : Natural) return Table is (APL ("(1 + ⍳ N) ∘.× 1 + ⍳ N"))
with Pre => N <= 10,
Post => Times'Result'Length (1) = N and Times'Result'Length (2) = N;
-- each row's total
function Totals (T : Table) return Values is (APL ("+/ T"))
with Pre => T'Length (2) <= 1_000;
procedure Show (Label : String; V : Values);
end Stats; """APL one-liners as functions, with the contracts that let them prove."""
from verada import *
type Small = Annotated[int, Interval(ge=-100, le=100)]
class Vector(Array[Small], rank=1):
pass
class Values(Array[int], rank=1):
pass
class Table(Array[Small], rank=2):
pass
# the mean, rounded down: sum, count, divide, floor
def mean(x: Vector) -> int:
requires(1 <= len(x) <= 1000)
return apl("⌊ (+/ x) ÷ ≢ x")
def dot(x: Vector, y: Vector) -> int:
requires(len(x) == len(y) and len(x) <= 1000)
return apl("+/ x × y")
# a local dfn: the sum of squares less the count
def spread(x: Vector) -> int:
requires(len(x) <= 100)
return apl("sq ← {⍵ × ⍵} ⋄ (+/ sq x) - ≢ x")
# the values above a limit, in order
def above(x: Vector, limit: Small) -> Values:
requires(len(x) <= 1000)
return apl("(x > limit) / x")
# a multiplication table, n by n
def times(n: int) -> Table:
requires(0 <= n <= 10)
ensures(result.shape[0] == n and result.shape[1] == n)
return apl("(1 + ⍳ n) ∘.× 1 + ⍳ n")
# each row's total
def totals(t: Table) -> Values:
requires(t.shape[1] <= 1000)
return apl("+/ t") $ veradic prove
proved 112 of 112 checks in 9 subprograms (21 statically, 91 by the solver)
$ veradic run
mean: 3
dot: 107
spread: 127
above 3: 4 5 9
sorted: 1 1 3 4 5 9
table totals: 10 20 30 40
The same package in Ada and in Vibe proves and prints the same,
and veradic translate turns the Varc into either. A
Vibe program stays a Python program: apl("...") is an
ordinary call, so CPython runs it too.
Typed
Your names, your types
APL text works on the program's own arrays, scalars and functions. Every phrase has an element type and a rank fixed when compiling, so a mistake is reported at the glyph, before anything runs.
Proved
Shapes and overflow, proved
Adding two vectors whose lengths might differ, a sum that might overflow, a function whose precondition might fail at one element: each is a proof obligation, discharged or reported like any other.
Compiled
Loops you'd have written
There is no APL interpreter and no run time. Each phrase becomes the fused loops a hand-written version would have, without temporaries, and proved loops vectorise exactly as hand-written ones do.
Shapes are proved
What if the lengths differ?
In APL, X + Y on vectors of different lengths is a
length error at run time. In Verada it is a proof obligation. Leave
out the precondition and the build stops, at the glyph, with the
APL written out as what it means so you can see exactly where the
check comes from. One line, requires X::Length == Y::Length,
and it proves.
package Vectors {
pub subtype Small = Integer range -100..=100;
pub type Vector = [Positive..]Small;
pub type Values = [Positive..]Integer;
// element by element: but what if the lengths differ?
pub fn Add(X, Y: Vector) -> Values = apl"X + Y";
} $ veradic prove
error[B5111]: `throw` might be reached
--> src/vectors.va:7:48
|
7 | pub fn Add(X, Y: Vector) -> Values = apl"X + Y";
| ^ `throw Constraint_Error` might be reached
|
= help: keep the `throw` from being reached: a precondition of `Vectors.Add`, or a test before it (a function lets no exception out)
help: write the APL text as what it means here
|
7 | pub fn Add(X, Y: Vector) -> Values = [for Apl_I0 in 1..=1 + ((if X::Length == Y::Length { X::Length } else { throw Constraint_Error with "the arguments' shapes differ" }) - 1) => (X(X::First + (Apl_I0 - 1)) + Y(Y::First + (Apl_I0 - 1)))];
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 error emitted
proved 11 of 12 checks in 1 subprogram (1 statically, 10 by the solver) No magic
It's Ada underneath.
APL text lowers to the array expressions the host language would
have written by hand: iterated aggregates and reductions.
veradic translate --plain-ada shows them, so a program
with APL in it can still be compiled by any Ada compiler, with the
same output.
APL
function Mean (X : Vector) return Integer is (APL ("⌊ (+/ X) ÷ ≢ X")) veradic translate --to vada --plain-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; Play with it
An APL mode in the REPL.
Type :apl and every line is APL over the session's
names. Values print with their shapes, name ← text
keeps one, and a backtick and a key type a glyph: no special
keyboard needed. The same keys complete to glyphs in your editor.
$ veradic repl
verada> X : constant array (1 .. 6) of Integer := (3, 1, 4, 1, 5, 9);
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
23
apl> ⌽ X
⍴ 6: 9 5 1 4 1 3
apl> (⍳ 3) ∘.× ⍳ 4
⍴ 3 4
0 0 0 0
0 1 2 3
0 2 4 6
apl> `$ X
⍴ 6: 1 3 0 2 4 5
apl> Y ← 2 × X
apl> +/ Y
46
apl> :apl
back to the session's syntax
verada> X'Length
6 The subset
The APL you reach for.
Flat arrays of any rank, positions counting from 0, reductions in APL's right-to-left order and trains with Dyalog's meaning. Verada functions join in too: a function of scalars applies to each element, with its precondition proved at every one.
Scalar functions
- + - × ÷
- arithmetic, element by element
- ⌈ ⌊
- max and min, ceiling and floor
- |
- residue and magnitude
- * !
- power and factorial
- = ≠ < ≤ > ≥
- comparisons, giving Booleans
- ∧ ∨ ~
- and, or, not
Structure
- ⍳ n
- 0 1 … n-1
- ⍴ ≢
- shape, reshape and count
- , ⍉
- ravel, catenate, transpose
- ⌽ ⊖
- reverse and rotate
- ↑ ↓
- take and drop, from either end
- x[i]
- indexing by positions
Selection and order
- m / x
- compress by a mask
- c / x
- replicate by counts
- ∊
- membership
- x ⍳ y
- index of
- ⍋ ⍒
- grade up and down, stable
- x[⍋ x]
- sort
Reductions and operators
- f/ f⌿
- reduce along either axis
- f\ f⍀
- scan: every prefix's reduction
- ∘.f
- outer product
- f.g
- inner product: +.× is matrix product
- f¨ f⍨ f⍤k
- each, commute, rank
- {⍺ ⍵}
- dfns with guards, and trains
Prefer words?
The same operations, spelled out.
Varc and Vibe share APL's vocabulary as methods and operators. A word and its glyph compile to the same thing, so they prove alike.
| Operation | APL | Varc | Vibe |
|---|---|---|---|
| transpose | ⍉ m | m.transpose() | m.T |
| inner product | x +.× y | x.dot(y) | a @ b |
| sort | x[⍋ x] | x.sorted() | |
| grade | ⍋ x | x.sorted_indices() | |
| rotate | k ⌽ x | x.rotate(k) | |
| reshape | r c ⍴ x | x.reshape(r, c) | |
| scan | +\ x | .scan(init, f) | accumulate(xs) |