On this page
I/O, command lines and exceptions
These packages hold the parts of Ada's I/O, command-line and exception packages that Verada programs use, with contracts that flow analysis and the prover understand. Files, the standard streams and the program's arguments are external inputs: a proof says what the program does with them, not what they contain.
Text I/O types and state
Ada.Text_IO has all of Ada's A.10: a limited File_Type with the modes In_File, Out_File and Append_File; standard input, output and error, and the current input, output and error that Set_Input, Set_Output and Set_Error choose; files by characters, strings and lines, with their layout; and numbers and enumeration values read and written by the generic packages Integer_IO, Modular_IO, Float_IO, Fixed_IO, Decimal_IO and Enumeration_IO. Is_Open observes a file; Mode, Name and the operations on a file need it open. Count, Positive_Count and Field (0 .. Integer'Last) are the counts and widths. The eight exception names rename the Ada.IO_Exceptions declarations, preserving exception identity.
The abstract state File_System stands for the files and streams: operations that read or write them name it in their Global contracts, so flow analysis follows I/O effects like any other state, and two tasks that both use it are subject to the data-race rules.
A procedure given a file that is not open propagates Status_Error, and one given a file open in a mode it cannot use (Get from an output file, Put to an input file) Mode_Error, as Ada's do; each one's Exceptional_Cases says so (Status_Error => not Is_Open (File), Mode_Error => Is_Open (File) and then Mode (File) /= In_File). A program that knows its file is open in the right mode, from Create's or Open's postcondition or by testing Is_Open and Mode first, proves that neither happens (and Reset's Mode_Error, below, from which files are current). A function (Mode, Name, Col, Line, End_Of_File, the function Get_Line, ...) propagates no exception, so for a function an open file in the right mode is a precondition, and so it is for Skip_Line, Skip_Page and the procedure Get_Line, whose End_Error case names End_Of_File on entry. A File_Type object is closed until Create or Open opens it (its Default_Initial_Condition). Create and Open propagate Status_Error for a file already open; Create defaults to Out_File, an empty Name (a temporary file, deleted when it is closed) and an empty Form, and Open needs an existing file. Each may propagate Name_Error (no such file, or a name that cannot be one) or Use_Error (any other failure), and then leaves the file closed. Close, Delete and Reset propagate Status_Error for a closed file; Delete may propagate Use_Error, and Reset Use_Error with the file then closed; Reset of a current default input, output or error file to another mode propagates Mode_Error, the file left open as it was (RM A.10.2(4)). A File_Type is not closed when its scope ends: close each file you open. The number of open files has no limit of Verada's.
Which files are current, and every file's line and page lengths, are one value for contracts, the ghost function Settings. Each operation that changes neither says so, Settings = Settings'Old, on return and with each exception a program may handle (Name_Error, Use_Error, End_Error, Data_Error, Layout_Error); Set_Input, Set_Output and Set_Error, Set_Line_Length and Set_Page_Length, Reset, Close and Delete say how they change it (With_Output (Settings'Old, File), Without (Settings, File)'Old, ...). The ghost function Is_Current (File) is whether File is the current default input, output or error file: a file Create or Open makes is not, and stays not current until the program makes it so, so its Reset to another mode is proved never to propagate Mode_Error and needs no handler; after Set_Output (F) it does. Is_Current_Input and Is_Current_Output tie the forms without a File to the current files: the current output's Line_Length, Page_Length, Col, Line and Page are those of the forms without one, and the current input's End_Of_Line, End_Of_Page and End_Of_File. A program starts with standard output current and no line or page length set (Text_IO's Initial_Condition, the ghost function No_Lengths_Set). A whole program, proved, built or run with its main, that calls Set_Line_Length or Set_Page_Length nowhere (in no form: the wide units', an instance's, a renaming's, through an access value) keeps every line unbounded, so its proof knows No_Lengths_Set everywhere: on entry to each subprogram and task and after every call, and a number put anywhere in it needs no handler. Where a program does set a length, or where it is not whole (a library unit proved alone, the REPL, the IDE's partial projects), a subprogram that puts a number without knowing the length states it, Pre => Line_Length = 0, or handles Layout_Error, and a loop that puts lines keeps what is known of the settings when its invariant says so (Settings = Settings'Loop_Entry). A file closed while current leaves the standard one current again, and so does a Reset that cannot reopen it.
Unbounded and bounded strings
Ada.Text_IO.Unbounded_IO gives Put, Put_Line and Get_Line (as functions and procedures) of an Ada.Strings.Unbounded Unbounded_String, with Text_IO's contracts for the String forms. The generic Ada.Text_IO.Bounded_IO, instantiated with an instance of Generic_Bounded_Length, gives the same for its Bounded_String; Get_Line there is the procedure only, which raises Ada.Strings.Length_Error for a line longer than the bound (a Verada function propagates no exception, so the function forms are not declared).
Writing and reading
Put of a Character or a String, Put_Line and New_Line write to a file open for output, or to the current output. Output to standard output and standard error encodes each Latin-1 character as UTF-8; a named file receives the characters' Latin-1 bytes. Each file keeps its column, line and page (Col, Line, Page): a line ends with a line terminator, a page with a page terminator after it (New_Page), and the end of a file ends its last line and page (an empty file written in Out_File mode is one empty line), so Skip_Line over a file's last line leaves it at line 1 of the next page; a line or page length (Set_Line_Length, Set_Page_Length, 0 for none) ends lines and pages on output when they are full. Set_Col and Set_Line move forward on output, with spaces or line terminators, and on input skip to that column or line. Their exceptional cases say exactly when Layout_Error can propagate: only on output, when the line (page) length is bounded and To is beyond it, as it was on entry. The ghost functions Line_Bound and Page_Bound, a file's length when it is open for output and 0 otherwise, name it for a file, so Set_Col (1), or any column while the length is 0 (the default), is proved never to propagate it and needs no handler; since every other operation keeps the lengths, a length the program set is known wherever it is used. Flush writes out what a file holds.
Get of a Character skips line and page terminators; Look_Ahead shows the next character without taking it; Get_Immediate takes the next character as it is, a terminator too, and its form with Available does not wait for standard input. Get of a String reads Item'Length characters as Get does. These propagate End_Error at the end of the file. End_Of_Line, End_Of_Page and End_Of_File look ahead without taking anything.
Get_Line reads a line: for a buffer, Last is the last stored index (Item'First - 1 when the line is empty), and the line terminator is skipped when it is reached, but not when Item is filled just before it (the next Get_Line then reads an empty line); the function forms read a whole line of any length. The procedure Get_Line, Skip_Line and Skip_Page propagate End_Error when nothing is left to read, as Ada's do, and their exceptional cases say when: only when the file was at its end on entry (End_Of_File (F)'Old), or for Skip_Line also when Spacing is above 1 and the file ends first. A loop that tests not End_Of_File (F) before each Get_Line is therefore proved never to propagate it, and a loop may instead read until it does. A function propagates no exception, so the function Get_Line, and Unbounded_IO's and Bounded_IO's Get_Line, which call it, have the precondition not End_Of_File (F): a line must be left to read. An empty Item reads nothing, at the end too.
Set_Col on input reads up to a character in column To, skipping a line shorter than that, and Set_Line skips lines up to line To; each propagates End_Error rather than read the end of the file (after a file's last line, the next line is the first of a page past its end). Get of a number takes a based literal's : in place of #, the same delimiter both times (2:110:), and stops before a delimiter that does not match.
This program reads a file's lines until Get_Line propagates End_Error:
with Ada.Text_IO; use Ada.Text_IO;
procedure Main is
Name : constant String := "/tmp/verada-manual-end-error.txt";
F : File_Type;
Line : String (1 .. 80) := (others => ' ');
Last : Natural;
begin
Create (F, Out_File, Name);
Put_Line (F, "alpha");
Put_Line (F, "beta");
Close (F);
Open (F, In_File, Name);
begin
for Count in 1 .. 100 loop
Get_Line (F, Line, Last);
Put_Line (Line (1 .. Last));
end loop;
exception
when End_Error =>
Put_Line ("(end)");
end;
Close (F);
exception
when Name_Error | Use_Error =>
Put_Line ("no file");
end Main;It writes alpha, beta, then (end).
Formatting generics
Integer_IO and Modular_IO read and write integers in any base from 2 to 16 (16#FF#), Float_IO floating point values with Fore digits before the point, Aft after it and an exponent of Exp characters (none when Exp is 0), and Enumeration_IO a type's literals, in upper or lower case (Type_Set). Each has its Default_ variables, Get and Put on a file or the current input and output, and Get and Put on a string. Put writes a number right-justified in Width characters (more when it needs them) and an enumeration literal left-justified, as one item (RM A.10.6(8)): when the line length is bounded, on a new line when the item does not fit in what is left of the line, and Layout_Error, nothing written, when it is longer than the line. Each Put's exceptional case says exactly that, with the ghost function Put_Length, the number of characters Put writes for its arguments (Integer_IO's and Modular_IO's at least Width and at most Width or 133), so a Put while the length is 0 needs no handler. Put to a string fills it, or propagates Layout_Error when the image does not fit. A float's digits are its value's, correctly rounded (to nearest, a half to even).
Fixed_IO and Decimal_IO write fixed-point values as Float_IO writes floats, Fore, Aft and Exp alike, with Num'Fore and Num'Aft as their defaults and no exponent. The digits are the value's exactly, rounded to the nearest (halves away from zero, as 'Image rounds them), however many Aft asks for. A value read is the literal's exact value divided by the type's small and truncated toward zero, as 'Value takes it: -3.3 read as a value of a type whose small is 0.125 is -26 smalls, -3.25.
Ada.Integer_Text_IO, Long_Integer_Text_IO and Long_Long_Integer_Text_IO are Integer_IO's instances for Integer, Long_Integer and Long_Long_Integer, and Ada.Float_Text_IO and Long_Float_Text_IO are Float_IO's for Float and Long_Float, as Ada declares them. A program names one in a with clause instead of instantiating the generic itself.
Get skips blanks and line terminators, then reads the longest run of characters that begins a literal of the kind (a Width other than 0 reads that many characters instead), and takes its value as 'Value does, with Verada.Scanning: Data_Error when the characters are not a value of the type, End_Error when the input ends first. Get from a string gives Last, the index of the last character read.
with Ada.Text_IO; use Ada.Text_IO;
procedure Main is
package Int_IO is new Integer_IO (Integer);
package Real_IO is new Float_IO (Long_Float);
N : Integer;
X : Long_Float;
Last : Positive;
begin
Int_IO.Get (" 42 apples", N, Last);
Int_IO.Put (N, Width => 5);
Int_IO.Put (N, Width => 8, Base => 16);
New_Line;
Real_IO.Get ("2.5E1", X, Last);
Real_IO.Put (X, Fore => 3, Aft => 2, Exp => 0);
New_Line;
exception
when Data_Error | End_Error =>
Put_Line ("not a number");
end Main;with Ada.Text_IO; use Ada.Text_IO;
procedure Main is
type Volts is delta 0.125 range -10.0 .. 10.0;
type Money is delta 0.01 digits 10;
package Volts_IO is new Fixed_IO (Volts);
package Money_IO is new Decimal_IO (Money);
V : Volts;
M : Money;
Last : Positive;
begin
Volts_IO.Get ("-3.3", V, Last);
Volts_IO.Put (V, Fore => 3, Aft => 3);
New_Line;
Money_IO.Get ("1_234.567", M, Last);
Money_IO.Put (M, Fore => 6);
Money_IO.Put (M, Fore => 2, Aft => 1, Exp => 2);
New_Line;
exception
when Data_Error | End_Error =>
Put_Line ("not a value");
end Main;It writes -3.250, then 1234.56 1.2E+3.
Wide characters
Ada.Wide_Text_IO and Ada.Wide_Wide_Text_IO are Text_IO with characters and strings of Wide_Character and Wide_Wide_Character: every operation, with Text_IO's contracts, Status_Error and Mode_Error as its exceptional cases, and Integer_IO, Modular_IO, Float_IO, Fixed_IO, Decimal_IO and Enumeration_IO, whose Get and Put from and to a wide string read and write what Text_IO's do. Both are instances of one generic, Ada.Text_IO.Wide_Generic, over the character and string types. A file holds its characters as UTF-8, and a column is a character, not a byte. Bytes that are not UTF-8, and in Wide_Text_IO a character above 16#FFFF#, are read as U+FFFD, the replacement character. The files are Text_IO's, and so are the current input, output and error: Set_Output in one unit sets it for the three.
with Ada.Wide_Text_IO; use Ada.Wide_Text_IO;
with Ada.Wide_Wide_Text_IO;
procedure Main is
Word : constant Wide_String := "λόγος";
begin
Put (Word);
Put_Line (" takes" & Count'Wide_Image (Col - 1) & " columns");
Ada.Wide_Wide_Text_IO.Put_Line ("and one beyond: " & Wide_Wide_Character'Val (16#1F600#));
end Main;It writes λόγος takes 5 columns, then and one beyond: 😀.
Directories and files
Ada.Directories makes, renames, copies and deletes directories and files, answers whether a name exists and its kind, size and time, and searches a directory. A procedure that changes the file system propagates Name_Error (no such file or directory) or Use_Error (the host refuses), as its Exceptional_Cases say; Kind, Size and Modification_Seconds have the precondition Exists (Name), since a function propagates no exception. Verada leaves out Modification_Time, an Ada.Calendar time (B0501), and adds Modification_Seconds, seconds since the epoch. A search returns the matching names in the order of their bytes, as they were when it started (*, ? and [...] in its pattern). The name functions work on the text alone and take any string. The host's files are Text_IO's File_System state, so directory operations and file input and output keep their order in the flow contracts. Search's Process parameter is an anonymous access type, which cannot state the globals its procedure may use, so that procedure uses none; a loop of Start_Search, More_Entries and Get_Next_Entry is the usual way to read a directory.
with Ada.Directories; use Ada.Directories;
with Ada.Text_IO;
procedure Main
with Exceptional_Cases => (Name_Error | Use_Error | Status_Error | Ada.Text_IO.Mode_Error => True)
is
Folder : constant String := Compose (Current_Directory, "notes");
Search : Search_Type;
Item : Directory_Entry_Type;
Seed : Ada.Text_IO.File_Type;
begin
Create_Path (Folder);
Ada.Text_IO.Create (Seed, Ada.Text_IO.Out_File, Compose (Folder, "seed", "txt"));
Ada.Text_IO.Put_Line (Seed, "procedure Copy is begin null; end Copy;");
Ada.Text_IO.Close (Seed);
Copy_File (Compose (Folder, "seed", "txt"), Compose (Folder, "copy", "adb"));
Start_Search (Search, Folder, "*.vadb", (Ordinary_File => True, others => False));
while More_Entries (Search) loop
Get_Next_Entry (Search, Item);
Ada.Text_IO.Put_Line (Simple_Name (Item) & ", " & Base_Name (Simple_Name (Item))
& " and " & Extension (Full_Name (Item)));
end loop;
End_Search (Search);
Delete_Tree (Folder);
end Main;It writes copy.vadb, copy and adb, and leaves the directory as it found it.
Command-line arguments
Ada.Command_Line provides Argument_Count, Argument(Number) and Command_Name. Argument takes Positive and requires Number<=Argument_Count. The count excludes the command name. Arguments are those of the command that starts an executable built with veradic build; veradic run takes source files, not program arguments, and a program it runs has Argument_Count zero. Argument returns a copy of the argument.
Environment variables
Ada.Environment_Variables reads and changes the process's environment: Exists, Value (with or without a Default), Set, Clear of one variable or of all, and Iterate, which calls a procedure with each name and value as they were when it began. Value of a variable that does not exist, and Set of a name that is empty or holds =, are ruled out by preconditions (Is_Name, Is_Value) where Ada raises Constraint_Error. The ghost function Model gives a variable's value in contracts: after Set (N, V), Model (N) = V, and Value returns Model. A NUL in a name or value is excluded, as the host's environment cannot hold one.
with Ada.Environment_Variables; use Ada.Environment_Variables;
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
with Ada.Text_IO.Unbounded_IO;
procedure Main is
Greeting : Unbounded_String := To_Unbounded_String ("hello");
begin
Set ("VERADA_GREETING", "world");
pragma Assert (Model ("VERADA_GREETING") = "world");
Append (Greeting, ", " & Value ("VERADA_GREETING"));
Ada.Text_IO.Unbounded_IO.Put_Line (Greeting);
Clear ("VERADA_GREETING");
Ada.Text_IO.Put_Line (Value ("VERADA_GREETING", Default => "(unset)"));
end Main;It writes hello, world, then (unset). The assertion is proved from Set's postcondition, and Value's precondition from Exists, which Set's postcondition also gives.
Exception occurrences
Ada.Exceptions supplies a limited occurrence value, Exception_Name, Exception_Message and Current_Occurrence. A named handler occurrence snapshots the current exception information. Names are uppercase qualified exception names; messages carry the raise message up to the runtime's 200-character bound, or empty. Current_Occurrence gives the occurrence being handled, inside a handler.
A procedure that propagates an exception lists it in Exceptional_Cases. Failed language checks are proved not to happen rather than handled: a handler for Constraint_Error is rejected (B1018). See the Language and Verification manuals for handlers and propagation.
Assertions
Ada.Assertions declares Assert as a procedure, with or without a message, and Assertion_Error. The check is Assert's precondition, so a call is proved as pragma Assert is, and Assertion_Error is never raised.
with Ada.Assertions;
with Ada.Float_Text_IO;
with Ada.Integer_Text_IO;
with Ada.Text_IO;
procedure Main is
Total : Integer := 0;
Last : Positive;
N : Integer;
begin
Ada.Integer_Text_IO.Get ("17 ", N, Last);
if N in 0 .. 1_000 then
Total := N * 3;
Ada.Assertions.Assert (Total <= 3_000, "the total is bounded");
end if;
Ada.Integer_Text_IO.Put (Total, Width => 4);
Ada.Float_Text_IO.Put (0.125, Fore => 2, Aft => 3, Exp => 0);
Ada.Text_IO.New_Line;
exception
when Ada.Text_IO.Data_Error | Ada.Text_IO.End_Error =>
Ada.Text_IO.Put_Line ("not a number");
end Main;It writes 51 0.125. The call of Assert is proved from the test before it.
Ending the program
GNAT.OS_Lib declares OS_Exit, which ends the whole program with a status. It has the Program_Exit aspect, so a subprogram may call it only where it may exit the program itself, and its own Program_Exit condition must hold there (B5120).
Imports and practical use
Vada uses with Ada.Text_IO; (and likewise for the other units). Varc uses import with the declared member spellings, and & on file, buffer and out arguments. Vibe's print writes through Ada.Text_IO, and a Vibe module can import these units' names with from Ada.Text_IO import Put_Line; see the Vibe reference for the forms it has.