DEVELOPMENT EDITION · 769442725406
Language Reference
On this page

Varc printing, texts, tuples, optionals and collections

Varc writes Verada's library types and operations with the spellings C-family, Rust, Swift and Kotlin programmers expect. Each form means one of the library's proved units, the same ones Vibe uses, so a program written with them proves as any other does: every call keeps its contract, and a precondition the program cannot show is reported where the form is written.

Printing

println("n is", n, "and ok is", ok);   // n is 42 and ok is True
print("no line end, ");
println("then one");

println writes each argument's image with a space between them and ends the line (an optional's image is its value's, or null); print does not end it. A string or a text is written as its characters, a number without Ada's leading space, an interpolated string as its pieces in turn. The output is Verada.Vibe's Write, as Vibe's print is.

Interpolated strings

let n: Integer = 42;
println($"n = {n}, padded [{n:>5}], hex {n:x}, signed {n:+}");
// n = 42, padded [   42], hex 2a, signed +42
let greeting: Text = $"hello, {name}";

$"..." is a text made of its pieces: the text between fields, and each field's value. A field is an expression between { and }; after a : it takes a format spec in the mini-language Rust and Python share ([[fill]align][sign][#][0][width][,][.precision][type], such as >8, 08.3f, x, ,). A brace in the text is written {{ or }}. The value is a text where a text is wanted and a String where a String is, so Ada.Text_IO.Put_Line($"...") writes it. Each spec's precondition (a type it fits) is proved where the string is written; a spec the mini-language does not read is a syntax error.

Text operations

A Text (Verada.Strings.Texts) has Rust's names for its operations, which call Verada.Strings.Text_Operations:

Varc Meaning
s.len() its length in code points
s.to_upper(), s.to_lower() (or to_uppercase, to_lowercase) the text in upper or lower case
s.trim(), s.trim_start(), s.trim_end() without leading and trailing white space
s.starts_with(t), s.ends_with(t), s.contains(t) whether t is at the start, at the end or anywhere
s.find(t) the position (from 0) of the first t, a Natural?: null when there is none (Rust's)
s.replace(a, b), s.count(t), s.split(t) each a replaced by b; how many t; the pieces between
s[i] the text of the code point at position i (from 0)

A text is written with println(s), or with Verada.Strings.Texts' Put and Put_Line. Where a String is wanted, such as Ada.Text_IO's Put_Line, convert it with To_String(s) (the diagnostic says so).

Tuples

fn divide(a: Integer, b: Integer) -> (Integer, Integer)
    requires b > 0 && a >= 0
= (a / b, a % b);

let p: (Integer, Integer) = divide(17, 5);
let (q, r) = p;
println(p.0, p.1, q + r);   // 3 2 5

(A, B) is a tuple type of 2 to 12 items (Verada.Vibe's Tuple_2 to Tuple_12, one type for the same item types wherever it is written). (x, y) is a value where a tuple is wanted, p.0 its first item, and let (a, b) = p; (or var (a, b) = p;) names its items. Tuples compare with ==.

Optionals

fn half(n: Integer) -> Integer?
= if n % 2 == 0 { n / 2 } else { null };

let h: Integer? = half(10);
println(h ?? -1);           // 5
if let v = h {
    println("half of ten is", v);
} else {
    println("odd");
}
if h != null { println(h + 1); }

T? is a value of T that may be absent (Verada.Optionals' Optional). null, or Rust's None, is the absent value; a value of T where a T? is wanted is that value, present. o ?? d is o's value when it has one, else d (Verada.Optionals' Value_Or, which reads o once). if let v = o { ... } names o's value in its first block, which runs only when o has one. o == null and o != null test it. Where an optional is used as its value (h + 1 above), its presence is a proof obligation, which a test such as h != null or if let discharges, as Kotlin's smart casts do.

Collections

var v: List<Integer> = [];
v.push(3);
v.push(5);
v.insert(0, 1);
v[1] = 4;
println(v.len(), v[0], v.contains(5));

var m: Map<Integer, Integer> = [];
m.insert(1, 10);
m[3] = 30;
println(m[1], m.contains_key(2), m.get_or(9, -1));

var s: Set<Integer> = [];
s.insert(4);
println(s.contains(4), s.len());

List<T>, Map<K, V> and Set<T> are Verada's unbounded vector, dictionary and ordered set (Verada.Containers.Unbounded's Vectors, Dictionaries and Ordered_Sets). [] is an empty one, and [1, 2, 3] a list of those elements. Python's {} is not an empty map in Varc, where braces hold a block: it is reported, with [] as its fix. A list is indexed from 0; a map by key. Their operations, by Rust's names:

List Map Set Meaning
push(x) adds x at the end
insert(i, x) insert(k, v) insert(x) adds x before position i; sets key k to v; adds x if it is not there
remove(i) remove(k) remove(x) removes the element at i; the key k; x if it is there
pop() removes the last element and gives it
xs[i], xs[i] = x m[k], m[k] = v the element at i or key k, and its store
contains(x) contains_key(k) contains(x) whether it holds x, or the key k
get_or(k, d) the value at k, or d
len(), is_empty(), clear() the same the same its length, whether it is empty, and emptying it
extend(ys), reverse(), sort() appends ys; reverses; sorts by <

for x in v { } walks a list in order, a map's keys or a set's elements. Each operation's precondition (a position within the list, a key the map has) is proved where it is called. An operation of a type's own with the same name is called on a value of that type.

In the REPL (veradic repl --syntax varc) these forms need no imports either: an input that uses them is given the library units it names, and the instances a T?, a tuple or a collection needs stay in the session for later inputs.

Shifts

x << n and x >> n shift an Integer (Rust's arithmetic shifts: -9 >> 1 is -5, x << n is x * 2 ** n within Integer's range) or a value of one of Interfaces' unsigned types, Unsigned_8 to Unsigned_64, by their own Shift_Left and Shift_Right. They group between + and the comparisons; an arithmetic operand of a shift is parenthesised, since C and Swift group x << 1 + 2 differently.

The with statement

fn enter(l: inout Log) { l.open = True; }
fn exit(l: inout Log) { l.open = False; }

var l: Log = (open: False);
with l {
    use_log(l);
}
with f = (open: False) as Log {
    use_log(f);
}

with x { ... } calls enter(&x), runs the block, and calls exit(&x) on every way out of it: at its end, by return, break or continue, and by an exception, which is then raised again (Vibe's with statement, Python's context managers). with f = e { } declares the variable for the statement. Several objects, with a, b { }, nest, the first outermost.

Complete programs

The test programs under test/programs/run show each form in a whole program that proves and runs: varc-printing, varc-tuples-optionals, varc-collections, varc-chain-loops, varc-shifts-with, varc-with-exits, varc-text-methods and varc-file-functions-guards.