DEVELOPMENT EDITION · 769442725406
Programmer Guide
On this page

Everyday types, values and control flow

Start by describing the values the program may hold. A type chooses the operations and identity of its values; a subtype narrows an existing type to the values that are valid. Verada proves facts from these choices before you write a single contract.

Give data meaningful bounds

Our stockroom has three bins, numbered 0 through 2, and a bin holds at most 100 items:

Concept Vada Varc Vibe
Quantity subtype subtype Quantity is Natural range 0 .. 100; subtype Quantity = Natural range 0..=100; type Quantity = Annotated[Natural, Interval(ge=0, le=100)]
Bin subtype subtype Bin is Natural range 0 .. 2; subtype Bin = Natural range 0..=2; type Bin = Annotated[Natural, Interval(ge=0, le=2)]
Fixed shelf type Shelf is array (Bin) of Quantity; type Shelf = [Bin]Quantity; class Shelf(Array[Quantity], index=Bin): pass

The stockroom projects show these declarations in context; Vibe imports Annotated, Interval, Natural and Array from verada.

A Quantity is still an Integer, only constrained, so it mixes freely with other integers. When mixing two kinds of number should be an error, declare a new type instead: Vada type Temperature is range -50 .. 60;, Varc type Temperature = range -50..=60;, or a Vibe class class Temperature(RangeInt, ge=-50, le=60): pass.

Integers are finite. Natural stops at 9,223,372,036,854,775,807 (2⁶³ − 1), and Vibe's int is the same 64-bit Integer: Python-shaped source does not make arithmetic unbounded. The integer ranges chapter explains overflow, subtype checks and how to widen before an operation.

Variables and initialization

Vada declares variables with Name : Type := Value; and marks unchangeable ones constant. Varc uses var for variables and let for constants, with = for assignment. Vibe uses ordinary assignments; parameters and results need annotations, and some locals do too (an empty list needs its element type: xs: list[int] = []).

Verada rejects reading a variable before it has a value, and returning before an output is set. Give each variable its value where the value is known. Initializing everything to zero just to quiet the compiler hides the case where a branch forgot to compute its result.

Varc and Vibe infer a local's type from its initializer. The inferred type is the type of that expression; to keep a narrower range, declare it.

Conditions are facts on a path

When the program tests whether a request fits the current stock, the branch where the test succeeds knows that it fits, so subtracting the request is proved safe there. The other branch reports a status and subtracts nothing. The stockroom's reservation is written this way: Vada, Varc, Vibe.

Short-circuit operators guard a second test with the first: Vada P /= null and then P.Value > 0, Varc p != null && p.value > 0, Vibe p is not None and p.value > 0. Vada's plain and evaluates both sides.

Vada and Varc reject a mix of logical operators without parentheses, such as Varc a && b || c; write (a && b) || c. Don't rely on another language's precedence table.

Loop over the actual domain

A for loop over a range introduces its loop variable with that range, so a loop over Bin (or Vibe range(3)) can index the shelf without any further proof.

Arrays need not start at 0 or 1. Loop over the array's own bounds (A'Range, or the index type) rather than guessing an origin. A multidimensional array has bounds in each dimension.

A loop that computes something more than a simple bound needs a loop invariant: a statement of what the loop has done so far. Reasoning about programs shows how to write one.

Choose a record or a collection

A record (Varc struct, Vibe @dataclass) groups named fields of a fixed shape, such as an order with a bin and a quantity. A fixed array holds a value for each index of a declared domain. A vector or list grows and shrinks.

The library offers bounded containers with a fixed capacity, unbounded containers that allocate, and functional containers for use in contracts. Vibe's list[T], dict[K, V], set[T] and tuples are typed: every element has the declared type. The Vibe collections page and the Library Reference help you choose.

Strings and numbers

Vada and Varc have Ada's String, an array of Latin-1 characters with bounds, Wide_Wide_String for any Unicode code point, and the library's copyable Text. Vibe's str is Text: it holds code points, len counts characters and print writes UTF-8. See strings and text.

Modular integer types wrap around their modulus on purpose; signed integers never wrap. Floating-point arithmetic rounds as the machine does, so it is not exact real arithmetic. Fixed-point types count in fixed steps. A decimal one, such as type Money is delta 0.01 digits 12;, steps in exact hundredths, which suits money.

Run the integer ranges project in your syntax. It prints:

natural max: 9223372036854775807
percent: 42
reading: 1000

and asserts how division and remainder treat negative numbers. In Vada, -7 mod 3 means -(7 mod 3), which is −1, because the minus sign applies to the whole term; write (-7) mod 3, which is 2, to take the remainder of −7.

Practice

Before adding a field or collection to the stockroom, answer three questions: which values are valid, who can change them, and where does the value first get set? Then choose the type. "A quantity is 0 through 100" belongs in a subtype. "A successful request reduces exactly one bin" belongs in the operation's postcondition.

The exercises give you changes to try.