DEVELOPMENT EDITION · 769442725406
Library Reference
On this page

Numeric libraries

The predefined numeric types belong to the Language Reference. These packages add named constants, big numbers, five elementary functions and lemmas for proofs.

Ada.Numerics and Big_Numbers

Ada.Numerics provides named-number Pi and e with the decimal digits shown in the source; conversion to a concrete floating type introduces its rounding. Its Argument_Error is never raised: the elementary functions' preconditions keep their arguments in their domains. Big_Numbers provides the Field (0 .. Integer'Last) and Number_Base (2 .. 16) subtypes.

Big integers

Big_Integers has Big_Integer, To_Big_Integer from Integer, In_Range, checked To_Integer and To_String. To_Integer requires the mathematical value inside Integer bounds. To_String uses an Ada-style leading space for nonnegative values or a minus sign for negative ones. Equality/order, unary minus/abs, arithmetic +,-,*,/,mod,rem and nonnegative exponentiation are recognized model/runtime operations. Division/mod/rem require a nonzero denominator; mod and rem follow their distinct signed arithmetic rules.

The prover treats these as mathematical integers, and so does the code: a value has as many digits as it needs, and only memory limits it (an operation whose result memory cannot hold raises Storage_Error, as an allocation does). The conversions are those listed: from and to Integer, and To_String. A small value (below 2**62 in magnitude) takes no storage of its own; a larger one is on the heap, shared by its copies and freed by the last of them.

Big reals

Big_Reals is ghost. Big_Real is an exact rational model; To_Big_Real converts a Big_Integer, and division of two big integers constructs their exact quotient with nonzero denominator. In_Range/order/equality, unary minus/abs, +,-,*,/,Min, Max and exponentiation supply the listed model operations. Big_Real division requires nonzero right operand. Exponentiation takes an Integer exponent and requires it nonnegative.

Because the package is ghost, Big_Real values exist only in contracts and ghost code, and a build leaves them out of the program.

Elementary functions

Generic_Elementary_Functions takes a floating actual and declares Sqrt, Exp, Log (of a base too), Sin, Cos, Tan, Arcsin, Arccos, Arctan, Sinh, Cosh, Tanh, Arcsinh, Arccosh and Arctanh. Elementary_Functions is the supplied Float instance and Long_Elementary_Functions the Long_Float one, which APL text's *, ⍟ and ○ call.

Function Required input Public result guarantee
Sqrt X >= 0 Nonnegative and at most 1 for X<=1, otherwise at most X
Exp X <= 80 Nonnegative
Log X > 0 Between -800 and 800; at least 0.69 from 2, at most -0.69 to 0.5
Log (X, Base) X > 0; Base >= 2 or 0 < Base <= 0.5
Sin / Cos No extra declared Pre Between -1 and 1
Tan No extra declared Pre Between -1.0E17 and 1.0E17
Arcsin / Arccos X in -1 .. 1 -1.6 .. 1.6 / 0 .. 3.2
Arctan No extra declared Pre -1.6 .. 1.6
Sinh / Cosh X in -80 .. 80 -3.0E34 .. 3.0E34 / 1 .. 3.0E34
Tanh No extra declared Pre Between -1 and 1
Arcsinh / Arccosh / Arctanh none / X >= 1 / -1 < X < 1 Between -800 and 800 (Arccosh from 0)

The functions call the C library's libm, converting to and from the floating type, and their contracts are trusted. The table is everything a proof knows about each result: no identity such as Sqrt (X) * Sqrt (X) = X, no error bound, and no promise that two libm versions agree. The package declares these functions only.

Vada: package Math is new Ada.Numerics.Generic_Elementary_Functions(Long_Float);. Varc uses package Math = new Ada.Numerics.Generic_Elementary_Functions<Long_Float>; after import. Vibe has no math module; it imports the Float instance's functions by name. Vibe's float is Long_Float, so convert the argument and the result:

from Ada.Numerics.Elementary_Functions import Sqrt
from verada import Float

def main() -> None:
    x: float = 2.0
    print(float(Sqrt(Float(x))))

It prints 1.4142135381698608: the square root computed in Float's precision.

Random numbers

Float_Random and Discrete_Random (RM A.5.2) generate uniformly distributed values from a Generator the program declares and passes. Random is a function with side effects on its in out generator (so it is called only as the whole value of an assignment or declaration, B2029): the generator's change is the call's own effect, and no state is hidden from the proof. A result is known only to lie in its subtype (Float_Random's Uniformly_Distributed, 0.0 .. 1.0, or Discrete_Random's Result_Subtype, or First .. Last for the Ada 2022 form), so no proof rests on which value comes.

The generator is xoshiro256** (Blackman and Vigna, 2018): a state of four 64-bit words and a period of 2256 - 1, fast and statistically sound, though not for cryptography. Reset with an Integer seeds the four words by SplitMix64, as its authors recommend, so an initiator always gives the same values; Reset without one seeds them from the monotonic clock; a generator never reset starts as Reset (Gen, 0) leaves it. A float is the top 24 bits of the next word scaled by 2(-24) (so 1.0 never comes); a discrete value is the next word modulo the range's count, from its first position (each value's probability is within count / 2**64 of 1 / count). Save and Reset copy the state; Image writes its four words in hexadecimal (Max_Image_Width, 64 characters) and Value reads them, of either case, Is_Image being its precondition where the RM raises Constraint_Error.

Reset (G, 42);
X := Random (G);
Dice.Reset (D, 42);
N := Dice.Random (D, 5, 6);

Integer lemmas

Verada.Lemmas.Arithmetic names facts of integer arithmetic that a solver does not find by itself inside a larger proof: order under multiplication and division by values of known sign, the sign and size of products, squares, and quotients with their remainders. Products are stated in Long_Long_Integer, so a lemma has no overflow to rule out first. Each is a ghost procedure whose postcondition is proved, not assumed:

Verada.Lemmas.Arithmetic.Lemma_Mult_Is_Monotonic (Count, Limit, Width);
pragma Assert (Long_Long_Integer (Count) * Long_Long_Integer (Width) <= Long_Long_Integer (Limit) * Long_Long_Integer (Width));

Floating-point lemmas

Verada.Lemmas.Floating_Point, under the namespace Verada.Lemmas, names what rounding to nearest preserves, for Float and Long_Float: order under addition and subtraction, order under multiplication by a value of known sign (kept or reversed), and exact conversion of whole numbers within the significand's range. Each is a ghost procedure with a null body, and each postcondition is proved, not assumed. The prover already knows these facts (its error model treats rounding as monotone and exact on such whole numbers), so a proof seldom needs a call; one states the fact by name where a proof wants it as a step:

Verada.Lemmas.Floating_Point.Lemma_Add_Is_Monotonic (Low, High, Step);
pragma Assert (Low + Step <= High + Step);