DEVELOPMENT EDITION · 769442725406
Language Reference
On this page

Integer values, ranges, and arithmetic

This chapter describes the development compiler. The complete executable examples are in examples/first-proof. Commands below start at the repository root and use a built veradic on PATH; from a source checkout, prefix them with stack exec veradic -- in place of veradic.

An integer value has a type with finite bounds. A subtype can restrict those values further. Verada checks both that arithmetic fits its base type and that a value fits the subtype at a boundary such as an assignment, a parameter, or a function result. In a strict build, these checks must be proved before the program is built.

Predefined integer types

These bounds are defined by this Verada implementation. They do not depend on the host's C int or pointer width.

Name Values Relationship
Integer −2⁶³ through 2⁶³ − 1 (−9,223,372,036,854,775,808 through 9,223,372,036,854,775,807) Signed 64-bit integer type
Natural 0 through 2⁶³ − 1 Subtype of Integer
Positive 1 through 2⁶³ − 1 Subtype of Integer
Short_Short_Integer −128 through 127 Signed 8-bit integer type
Short_Integer −32,768 through 32,767 Signed 16-bit integer type
Long_Integer −2⁶³ through 2⁶³ − 1 Signed 64-bit integer type
Long_Long_Integer −2⁶³ through 2⁶³ − 1 Another signed 64-bit integer type
Long_Long_Long_Integer −2¹²⁷ through 2¹²⁷ − 1 Signed 128-bit integer type

Integer, Long_Integer and Long_Long_Integer have the same bounds but remain distinct types. For an exact width, use Interfaces.Integer_32 and its siblings; for C's int, use Interfaces.C.int, which is 32 bits. A conversion is required where values of distinct integer types meet. Natural and Positive keep the identity of Integer; their narrower constraints still apply when storing or passing values.

In Vibe, int names Integer. A Vibe int is therefore bounded even though CPython integers can grow beyond 64 bits. Natural and Positive are imported from verada.

Declaring a type or a subtype

A new integer type gives values a distinct type identity. A subtype gives an existing type a name with a constraint. For example, a percentage can be a constrained Integer or a distinct integer type; choose according to whether it should mix with other integers without an explicit conversion.

The following are declaration fragments. In Vada, put them in a package specification or a declarative part. In Varc, put them in a package or a function block. In Vibe, the module is the package and the imports are part of the source.

Vada

subtype Percent is Integer range 0 .. 100;
type Reading is range -1000 .. 1000;

Varc

subtype Percent = Integer range 0...100;
type Reading = range -1000...1000;

Vibe

from verada import Annotated, Interval, RangeInt

type Percent = Annotated[int, Interval(ge=0, le=100)]

class Reading(RangeInt, ge=-1000, le=1000):
    pass

Percent remains an Integer with an inclusive constraint. Reading is a new integer type. The new type's defining bounds must be static: known during compilation. A subtype's bounds can be dynamic where the accepted declaration syntax permits it.

The implementation chooses the smallest signed base range of 8, 16, 32, 64, or 128 bits that contains a new integer type's declared bounds. Its declared first subtype still has exactly the written range. The range -1000 .. 1000 therefore has a 16-bit base, while values stored as Reading must remain between −1000 and 1000. Defining a signed type whose bounds cannot fit the supported 128-bit base range is rejected.

The declaration patterns are:

Purpose Vada Varc Vibe
New signed type type T is range Low .. High; type T = range Low...High; class T(RangeInt, ge=Low, le=High): ...
Constrained subtype subtype S is T range Low .. High; subtype S = T range Low...High; type S = Annotated[T, Interval(ge=Low, le=High)]
Lowest subtype value S'First S::First S.first
Highest subtype value S'Last S::Last S.last
Explicit conversion T(Value) T(value) T(value)

In Varc, Low...High and Low..=High both include the upper bound. In the Vibe class row, the ellipsis stands for a supported class body such as pass. For the predefined Vibe types and RangeInt classes, first and last expose the bounds. Check compatibility separately when using such properties on constrained typing aliases in CPython; a Verada-accepted alias is not a Python runtime class.

Arithmetic and subtype boundaries

For a signed integer type, +, -, multiplication, negation, division, and exponentiation must produce a result within the applicable base range. A value that does not fit is an overflow failure. Signed overflow does not silently wrap.

An arithmetic expression on a constrained subtype does not have to stay within that subtype at every intermediate step. Its result must fit the base type, then any required target subtype. This distinction explains two different failures:

The same distinctions apply in Varc and Vibe. Writing Natural on a parameter does not make all arithmetic involving it nonnegative, and it does not make incrementing its maximum safe.

The complete decrement-before project demonstrates the second failure. Its Previous function has no precondition excluding zero: subtraction can produce −1, and returning that value as Natural fails B4102. The equivalent Varc and Vibe projects fail for the same range obligation. Their main programs call with 1; the function's interface still permits zero.

At a conversion, the result must fit the target type and its applicable constraint. At an assignment, initialization, argument boundary, or result boundary, Verada checks the required subtype. The compiler may discharge an obvious check without calling a solver. A source expression that is already invalid during static evaluation can be rejected before proof.

A complete bounded increment

This function adds one to a Natural and returns a Natural. Its precondition excludes the one input whose increment would overflow. Its postcondition states the intended result.

The complete project in each syntax includes a main program that calls the function with 41 and prints answer: 42. The specifications below are accompanied by their actual source files; each project can be built independently.

Vada — bounded.vads

package Bounded is
   function Increment (Value : Natural) return Natural
     with Pre => Value < Natural'Last,
          Post => Increment'Result = Value + 1;
end Bounded;

Vada — bounded.vadb

package body Bounded is
   function Increment (Value : Natural) return Natural is
   begin
      return Value + 1;
   end Increment;
end Bounded;

Varc — bounded.va

package Bounded {
    pub fn increment(value: Natural) -> Natural
        requires value < Natural::Last
        ensures result == value + 1
    {
        return value + 1;
    }
}

Vibe — bounded.vb

from verada import Natural, ensures, requires, result


def increment(value: Natural) -> Natural:
    requires(value < Natural.last)
    ensures(result == value + 1)
    return value + 1

The body may assume its parameter is a Natural and its precondition holds. Hence the input lies between 0 and 9,223,372,036,854,775,806, and the result lies between 1 and 9,223,372,036,854,775,807. This proves the addition stays in Integer, the result fits Natural, and the postcondition holds. Every caller must establish the precondition for its actual argument. Calling with 41 does so; calling with Natural's highest value does not.

Choose one project and prove or run it:

veradic prove --project docs/manuals/examples/first-proof/vada/after/verada.toml
veradic run --project docs/manuals/examples/first-proof/vada/after/verada.toml

Replace vada with varc or vibe for the other project. The successful run prints:

answer: 42

The matching before projects omit the precondition. For example:

veradic prove --project docs/manuals/examples/first-proof/vada/before/verada.toml

This command fails with B4101 at the addition. A modular proof considers every input allowed by the interface, including 9,223,372,036,854,775,807; the fact that this main program calls with 41 does not repair the function's unconstrained interface. See B4101: integer overflow for the failure and repair workflow.

A stronger precondition is appropriate only when excluding those inputs matches the intended API. If callers must be able to increment the maximum, change the interface or algorithm: use a wider result type with a conversion before the addition, return an explicit failure result, or define another behavior at the bound. Converting a result after an overflowing addition is too late.

Division and remainders

Vada and Varc signed integer division truncate toward zero. Division also requires a nonzero divisor and a representable result. In particular, the most negative base value divided by −1 can overflow even though the divisor is nonzero.

Vada distinguishes rem and mod. A remainder from rem follows the dividend's sign; a nonzero result from mod follows the divisor's sign. Varc spells these % and %% respectively. For example, (-7) rem 3 is −1 and (-7) mod 3 is 2. Keep those parentheses in Vada: its unary sign applies to a term, so -7 mod 3 groups as -(7 mod 3) and gives −1. Varc and Vibe have their own precedence rules.

Vibe % maps to mod, and its // form implements floor division. Thus -7 // 3 is −3. The floor-division expansion also has arithmetic proof obligations; it is not an arbitrary precision operation. Vibe / is Python's true division: it yields a float even for integer operands, so 7 / 2 is 3.5. Use // when an integer quotient is intended. Floating-point division has its own proof rules and rounding behavior.

Static literals and large values

Integer literals and static expressions can be evaluated with arbitrary-precision arithmetic while the compiler determines their meaning. This allows expressions used to state type bounds; it does not make executable Integer values arbitrary precision. A literal used as an Integer must fit that type. For example, assigning 9,223,372,036,854,775,808 (2⁶³) directly to an Integer is invalid even though the compiler can represent that number while processing the source.

Use a suitable wider integer type for bounded executable calculations. A wider destination does not widen an already typed expression automatically: convert an operand before an operation when its intermediate result must use the wider type. For mathematical specifications or executable big-number operations, use the supported big-number library and its documented limits. Modular types provide deliberate wraparound under their modulus; they are a separate type family, not an overflow setting for signed integers.

Diagnostics and execution policy

Code Meaning here Typical repair
B2002 A static value is out of range or a static operation is undefined Correct the constant, operation, or chosen type
B4101 An arithmetic result may exceed its base range Establish a justified bound, change the algorithm, or widen before the operation
B4102 A value may lie outside the target subtype Establish the narrower range or change the target/API
B5101 A call may violate the increment function's precondition Establish the caller's bound or revise the callee's contract appropriately

check checks legality and flow; it is not proof that arithmetic is safe. prove attempts the obligations, and build proves them before compiling. run in a project uses the project's proof cache (--proof cached): checks the cache knows are proved are left out of the code, and the others stay as run-time checks (warning B8012). Advisory compilation (--advisory) keeps unproved checks at run time, and --no-prove keeps every check at run time instead of proving it. These modes do not enlarge the integer ranges or turn signed overflow into wrapping. A failed runtime check is reported by Verada; it should not be treated as an ordinary application exception handler strategy.

Proof establishes these arithmetic properties relative to the program's contracts and trusted assumptions. It does not establish absence of resource exhaustion or that the specified behavior is the behavior an application actually needs.

Complete examples

The examples above come from the maintained first-proof projects.

The declaration fragments also appear in complete Vada, Varc, and Vibe projects. These check, prove, and run; each prints the same bounds and values, while assertions exercise its syntax's division and remainder rules.