DEVELOPMENT EDITION · 769442725406
Vada syntax
On this page

Vada expressions

An expression denotes a value. Its type comes from its operands, declarations and the expected type at the use site. A numeral can acquire a concrete integer type from context; a value already declared with a nominal type does not automatically change into an unrelated type.

The shared expression reference defines evaluation and proof semantics. This chapter specifies Vada's spellings, grouping and context rules.

Operator precedence

The table runs from tighter binding to looser binding. Operators on one row share a grouping level, but a row can have further grammar restrictions.

Level Forms Grouping rule
Primary and suffixes Literals, names, (E), calls/indexing, selection, attributes, qualification, allocation Suffixes extend their prefix
Factor Primary ** Primary, abs Primary, not Primary Exponentiation cannot be chained; parenthesize a complex operand
Multiplication * / mod rem Left to right
Leading sign Optional + or - before a term Applies to the whole following multiplicative term
Addition + - & Left to right
Relation = /= < <= > >=, in, not in A relation has one comparison or membership operation
Logic and and then or or else xor Repeat one logical operator; parenthesize mixtures

The leading-sign position is part of a simple expression, not an independently repeatable prefix at every arithmetic operand. Use A * (-B) and A + (-B) when necessary. In particular:

(-7) mod 3        -- 2
-7 mod 3         -- -(7 mod 3), which is -1
-(X ** 2)        -- negate the square
(-X) ** 2        -- square the negated value

Neither A < B < C nor A ** B ** C is accepted. Write A < B and then B < C, and choose an explicit exponentiation grouping. A and B or C also needs parentheses. and and and then count as different operators for that restriction.

For Boolean values, and then evaluates its right operand only when its left operand is true; or else does so only when the left is false. This guards checks, as in P /= null and then P.Value > 0. Plain and and or do not provide that guard.

Names, selections and attributes

A name begins with an identifier or operator-symbol string and can acquire suffixes:

Name.Component
Pointer.all
Prefix(Arguments)
Prefix'Attribute
Prefix'Attribute(Arguments)
Type'(Value)

A selection can identify a package member or record component. .all explicitly dereferences an access value; supported field selection can implicitly dereference as well. Null and ownership rules still apply.

An attribute is not an ordinary method call: A'Length describes an array's length; A'Length(2) describes a chosen dimension; T'Image(X) renders a value; T'Class is a class-wide type. The attribute catalog gives every supported designator, argument count, valid prefix and restriction. Merely placing a name after an apostrophe does not create a user-defined attribute.

Qualification T'(Expression) states the expected type of the value without being the same operation as a conversion T(Expression). It is particularly useful for an aggregate whose type cannot otherwise be resolved.

Calls, indexing and slices

Vada uses parentheses for several resolved forms:

Form Interpretation
Clamp(Value, 0, 100) Function call
Count A parameterless function call when resolved as a function
Values(I) Array indexing
Matrix(I, J) Multidimensional indexing
Values(Low .. High) Slice
Integer(Reading) Type conversion
Container.Element(Key => K) Named association in a call

Actual parameters use commas. Positional actuals precede named actuals; after Low => 0, a later unlabelled argument is invalid. A call may omit actuals with declared defaults. A parameterless call is written as its name, not an empty () argument list.

Whether a prefix is callable, indexable or a type is established by name resolution and types. No Python-style negative-index convention is added to array indexing: indices are checked against that array's actual bounds.

Calls in expression positions must obey Verada's effect rules. Passing an in out actual does not turn a procedure into a value expression. See subprogram syntax and shared call rules.

Aggregates and updates

An aggregate constructs a composite value. Supply a type context or use qualification.

Values : Vector := [10, 20, 30];
Zeros  : Vector := [others => 0];
Origin : Point := (X => 0, Y => 0);

These fragments assume suitable Vector and Point types. Parenthesized array aggregates are accepted, but brackets distinguish modern array syntax from grouping and record aggregates. Empty array/container aggregate notation is []; () is not an empty tuple. A null record aggregate is (null record).

Named associations use component/index choices followed by =>. Choices can be separated by |; others covers remaining applicable choices. <> requests the supported default initialization for an associated component. Coverage, duplicate choices, component presence, lengths and expected types are checked.

A delta aggregate copies a base value while replacing selected components:

Moved : Point := (Origin with delta X => 10);

The older attribute spelling Origin'Update (X => 10) is accepted and means the same delta aggregate; write the aggregate form in new code.

An extension aggregate supplies the parent part of a tagged extension and its new components:

Child_Value : Child := (Parent_Value with Extra => 1);

These are type-dependent fragments; extension aggregates do not create structural conversion between arbitrary records.

As in Ada, an aggregate is not a name, so (Origin with delta X => 10).X is a syntax error. Qualify the aggregate to select from it, Point'(Origin with delta X => 10).X, or bind it to an object first.

Iterated aggregates and reductions

An iterated component association produces values from an iterator:

Squares : Vector := [for I in 1 .. 4 => I * I];

A filter follows the iterator domain, before =>:

[for X of Scores when X >= Mark => X]

The latter is an expression fragment needing an expected array/container type or a consumer. [for ...] is not a general persistent lazy sequence. The result must be consumed in a supported aggregate/reduction context.

A reduction names a reducer and initial accumulator:

Scores'Reduce ("+", 0)
[for X of Scores when X >= Mark => X]'Reduce ("+", 0)

A reducer can also be a suitable declared function, for example Score'Max. Bounds, accumulator overflow and generated loop invariants remain proof obligations. See pipelines.

Conditional and case expressions

A conditional expression selects a value:

(if Value < Low then Low elsif Value > High then High else Value)

Use parentheses when embedding the form in an ordinary expression. A context already delimited as a single argument or aspect value can accept the value form directly. The omitted else form is restricted by the Boolean implication interpretation; use a complete else for ordinary value selection.

A case expression uses comma-separated alternatives:

(case Kind is
   when First | Second => 10,
   when Third => 20,
   when others => 0)

Unlike a case statement, its branches yield values and there is no end case inside the parenthesized expression. Alternatives must satisfy the shared coverage/type rules.

Quantified expressions

for all is universal quantification; for some is existential quantification.

(for all I in A'Range => A(I) >= 0)
(for some X of A => X = Key)

The bound name belongs to the expression. Iterator filters can restrict the quantified domain. Empty-domain behavior follows logic: a universal statement is true and an existential statement false. A finite source can still be too expensive for runtime contract checking in development modes. Do not confuse proof-model quantification with executing an arbitrarily large search.

Declare expressions

A declare expression gives names to local values and renamings before yielding one value:

(declare
   Twice : constant Integer := Value * 2;
 begin
   Twice + 1)

This is an expression fragment, not a statement block. Its accepted local items have a subtype plus initialization or renaming. There are no arbitrary statement sequences between begin and the result expression. Initialization and arithmetic in the local items must still be safe.

Allocation, null, raises and the target name

null is the null access value. new Node allocates a designated subtype; initialized allocation uses qualification, for example new Node'(Value => 7, Next => null). Ownership and eventual reclamation are required; new is not a way to obtain unrestricted shared references.

A raise expression has the form raise Exception_Name or raise Exception_Name with Message. A conditional expression can use it as an alternative only when the exception and contract rules permit propagation.

@ is the target-name expression in supported assignment contexts: it denotes the target's prior value without repeating the target spelling. It is not a general implicit receiver or address operator. Effectful target evaluation and supported contexts are governed by assignment rules.

Compact expression grammar

This grammar describes grouping; semantic categories and exact supported attributes remain defined by their reference entries.

expression       ::= relation { same-logical-operator relation }
relation         ::= simple [comparison simple | ["not"] "in" membership-choices]
                   | "raise" name ["with" simple]
simple           ::= ["+" | "-"] term {("+" | "-" | "&") term}
term             ::= factor {("*" | "/" | "mod" | "rem") factor}
factor           ::= primary ["**" primary] | ("abs" | "not") primary
primary          ::= literal | name-with-suffixes | "null" | allocator
                   | parenthesized-value | aggregate | "@"
argument         ::= [formal "=>"] (value | low ".." high | "<>")
aspect           ::= name ["'Class"] ["=>" value]

value includes an ordinary expression and the contextual conditional/case/quantified/declare forms described above. Lists of arguments and aggregate associations have their own comma and choice separators. This grammar intentionally does not promise that every syntactically recognizable combination is a legal Verada expression.