--  Verada predefined library: Ada.Text_IO (Ada RM A.10): standard input, output and error and
--  the text files a program opens, by characters, strings and lines, with their layout
--  (columns, lines and pages, and the line and page lengths that bound them on output), and
--  numbers and enumeration values read and written by the generic packages below. The
--  primitives are run-time functions (rt/textio.c).
--
--  A file's state is the abstract state File_System, so input and output take their order
--  from the flow contracts. A procedure given a file that is not open, or not open in the
--  mode it needs, propagates Status_Error or Mode_Error, as Ada's does, and its
--  Exceptional_Cases say so: a program that knows its file is open in the right mode proves
--  that they never happen. A function (Mode, Col, End_Of_File, ...) propagates no exception,
--  so for a function that is a precondition. What depends on the world outside is an
--  exception the program may handle, as each Exceptional_Cases says: Name_Error and Use_Error when a file cannot be made, opened,
--  reset or deleted; End_Error when input runs out; Data_Error when a number or a value read
--  is not one; Layout_Error when a value does not fit where it is put. A file the program
--  closes while it is the current input or output leaves the standard one current again.
--  A program starts with no line or page length set (No_Lengths_Set): every one unbounded.
--  Which files are current (Is_Current) is part of the same state: each operation but
--  Set_Input, Set_Output, Set_Error, Close, Delete and a Reset that closes its file says it
--  leaves them as they were (Settings = Settings'Old), on return and with each
--  exception a program may handle (Name_Error, Use_Error, End_Error, Data_Error,
--  Layout_Error), and those say how they change them.
with Ada.IO_Exceptions;
with Verada.Storage;

package Ada.Text_IO
  with Abstract_State => File_System, Initializes => File_System,
       Initial_Condition => No_Lengths_Set
is
   --  (a file object is closed until Create or Open opens it)
   type File_Type is limited private
     with Default_Initial_Condition => not Is_Open (File_Type);

   type File_Mode is (In_File, Out_File, Append_File);

   type Count is range 0 .. Natural'Last;
   subtype Positive_Count is Count range 1 .. Count'Last;
   Unbounded : constant Count := 0;

   subtype Field is Integer range 0 .. Integer'Last;
   subtype Number_Base is Integer range 2 .. 16;

   type Type_Set is (Lower_Case, Upper_Case);

   Status_Error : exception renames Ada.IO_Exceptions.Status_Error;
   Mode_Error   : exception renames Ada.IO_Exceptions.Mode_Error;
   Name_Error   : exception renames Ada.IO_Exceptions.Name_Error;
   Use_Error    : exception renames Ada.IO_Exceptions.Use_Error;
   Device_Error : exception renames Ada.IO_Exceptions.Device_Error;
   End_Error    : exception renames Ada.IO_Exceptions.End_Error;
   Data_Error   : exception renames Ada.IO_Exceptions.Data_Error;
   Layout_Error : exception renames Ada.IO_Exceptions.Layout_Error;

   --  ---- files ----

   function Is_Open (File : File_Type) return Boolean
     with Global => null;

   function Mode (File : File_Type) return File_Mode
     with Pre => Is_Open (File), Global => null;

   --  a new file (an empty Name: a temporary one, deleted when it is closed), open in Mode;
   --  or, when it cannot be made, Name_Error or Use_Error and the file still closed
   procedure Create (File : in out File_Type; Mode : File_Mode := Out_File; Name : String := ""; Form : String := "")
     with Post => Settings = Settings'Old and then Is_Open (File) and then Ada.Text_IO.Mode (File) = Mode
                  and then Line_Bound (File) = 0 and then Page_Bound (File) = 0
                  and then not Is_Current (File),
          Global => (In_Out => File_System),
          Exceptional_Cases => (Status_Error => Is_Open (File)'Old,
                                Name_Error | Use_Error => Settings = Settings'Old and then not Is_Open (File));

   --  an existing file, open in Mode; or Name_Error (there is no such file) or Use_Error
   procedure Open (File : in out File_Type; Mode : File_Mode; Name : String; Form : String := "")
     with Post => Settings = Settings'Old and then Is_Open (File) and then Ada.Text_IO.Mode (File) = Mode
                  and then Line_Bound (File) = 0 and then Page_Bound (File) = 0
                  and then not Is_Current (File),
          Global => (In_Out => File_System),
          Exceptional_Cases => (Status_Error => Is_Open (File)'Old,
                                Name_Error | Use_Error => Settings = Settings'Old and then not Is_Open (File));

   --  (an output file's last line is ended first; where it was current, the standard file is
   --  current again)
   procedure Close (File : in out File_Type)
     with Post => Settings = Without (Settings, File)'Old and then not Is_Open (File),
          Global => (In_Out => File_System),
          Exceptional_Cases => (Status_Error => not Is_Open (File)'Old);

   --  the file closed and deleted; Use_Error when it cannot be deleted
   procedure Delete (File : in out File_Type)
     with Post => Settings = Without (Settings, File)'Old and then not Is_Open (File),
          Global => (In_Out => File_System),
          Exceptional_Cases => (Status_Error => not Is_Open (File)'Old,
                                Use_Error => Settings = Without (Settings, File)'Old and then not Is_Open (File));

   --  the file again from its start, in Mode (its mode when none is given); Use_Error when it
   --  cannot be, the file then closed; Mode_Error when the file is a current default file and
   --  Mode is not its mode (RM A.10.2(4)), the file left as it was
   procedure Reset (File : in out File_Type; Mode : File_Mode)
     with Post => Settings = Reset_Of (Settings, File)'Old and Is_Current (File) = Is_Current (File)'Old
                  and (Is_Open (File) and then Ada.Text_IO.Mode (File) = Mode
                       and then Line_Bound (File) = 0 and then Page_Bound (File) = 0),
          Global => (In_Out => File_System),
          Exceptional_Cases => (Status_Error => not Is_Open (File)'Old,
                                Mode_Error => Is_Current (File)'Old and then Is_Open (File)
                                              and then Mode /= Ada.Text_IO.Mode (File),
                                Use_Error => Settings = Without (Settings, File)'Old and then not Is_Open (File));

   procedure Reset (File : in out File_Type)
     with Post => Settings = Reset_Of (Settings, File)'Old and Is_Current (File) = Is_Current (File)'Old
                  and (Mode (File)'Old = Mode (File) and then Is_Open (File)
                       and then Line_Bound (File) = 0 and then Page_Bound (File) = 0),
          Global => (In_Out => File_System),
          Exceptional_Cases => (Status_Error => not Is_Open (File)'Old,
                                Use_Error => Settings = Without (Settings, File)'Old and then not Is_Open (File));

   --  the file's name (a temporary file's own), and its form: Verada takes none
   function Name (File : File_Type) return String
     with Pre => Is_Open (File), Global => File_System,
          Post => Name'Result'Length < Verada.Storage.Length_Bound;

   function Form (File : File_Type) return String
     with Pre => Is_Open (File), Global => null,
          Post => Form'Result'Length = 0;

   --  ---- the standard and current files ----

   function Standard_Input return File_Type
     with Global => null,
          Post => Is_Open (Standard_Input'Result) and then Mode (Standard_Input'Result) = In_File;
   function Standard_Output return File_Type
     with Global => null,
          Post => Is_Open (Standard_Output'Result) and then Mode (Standard_Output'Result) = Out_File;
   function Standard_Error return File_Type
     with Global => null,
          Post => Is_Open (Standard_Error'Result) and then Mode (Standard_Error'Result) = Out_File;

   function Current_Input return File_Type
     with Global => File_System,
          Post => Is_Open (Current_Input'Result) and then Mode (Current_Input'Result) = In_File;
   function Current_Output return File_Type
     with Global => File_System,
          Post => Is_Open (Current_Output'Result) and then Mode (Current_Output'Result) /= In_File;
   function Current_Error return File_Type
     with Global => File_System,
          Post => Is_Open (Current_Error'Result) and then Mode (Current_Error'Result) /= In_File;

   procedure Set_Input (File : File_Type)
     with Global => (In_Out => File_System),
          Post => Settings = With_Input (Settings'Old, File),
          Exceptional_Cases => (Status_Error => not Is_Open (File),
                                Mode_Error => Is_Open (File) and then Mode (File) /= In_File);
   procedure Set_Output (File : File_Type)
     with Global => (In_Out => File_System),
          Post => Settings = With_Output (Settings'Old, File),
          Exceptional_Cases => (Status_Error => not Is_Open (File),
                                Mode_Error => Is_Open (File) and then Mode (File) = In_File);
   procedure Set_Error (File : File_Type)
     with Global => (In_Out => File_System),
          Post => Settings = With_Error (Settings'Old, File),
          Exceptional_Cases => (Status_Error => not Is_Open (File),
                                Mode_Error => Is_Open (File) and then Mode (File) = In_File);

   procedure Flush (File : File_Type)
     with Global => (In_Out => File_System),
          Post => Settings = Settings'Old,
          Exceptional_Cases => (Status_Error => not Is_Open (File),
                                Mode_Error => Is_Open (File) and then Mode (File) = In_File);
   procedure Flush
     with Global => (In_Out => File_System),
          Post => Settings = Settings'Old;

   --  ---- line and page lengths of an output file (0, Unbounded, for none) ----

   procedure Set_Line_Length (File : File_Type; To : Count)
     with Global => (In_Out => File_System),
          Post => Settings = With_Line_Length (Settings, File, To)'Old
                  and Line_Bound (File) = To and Page_Bound (File) = Page_Bound (File)'Old,
          Exceptional_Cases => (Status_Error => not Is_Open (File),
                                Mode_Error => Is_Open (File) and then Mode (File) = In_File);
   procedure Set_Line_Length (To : Count)
     with Global => (In_Out => File_System),
          Post => Settings = With_Line_Length (Settings'Old, To)
                  and then Line_Length = To;
   procedure Set_Page_Length (File : File_Type; To : Count)
     with Global => (In_Out => File_System),
          Post => Settings = With_Page_Length (Settings, File, To)'Old
                  and Page_Bound (File) = To and Line_Bound (File) = Line_Bound (File)'Old,
          Exceptional_Cases => (Status_Error => not Is_Open (File),
                                Mode_Error => Is_Open (File) and then Mode (File) = In_File);
   procedure Set_Page_Length (To : Count)
     with Global => (In_Out => File_System),
          Post => Settings = With_Page_Length (Settings'Old, To)
                  and then Page_Length = To;

   --  (the current output's are the default forms')
   function Line_Length (File : File_Type) return Count
     with Pre => Is_Open (File) and then Mode (File) /= In_File, Global => File_System,
          Post => (if Is_Current_Output (File) then Line_Length'Result = Line_Length);
   function Line_Length return Count
     with Global => File_System;
   function Page_Length (File : File_Type) return Count
     with Pre => Is_Open (File) and then Mode (File) /= In_File, Global => File_System,
          Post => (if Is_Current_Output (File) then Page_Length'Result = Page_Length);
   function Page_Length return Count
     with Global => File_System;

   --  ---- columns, lines and pages ----

   --  Spacing line terminators (and a page terminator after a page's last line)
   procedure New_Line (File : File_Type; Spacing : Positive_Count := 1)
     with Global => (In_Out => File_System),
          Post => Settings = Settings'Old,
          Exceptional_Cases => (Status_Error => not Is_Open (File),
                                Mode_Error => Is_Open (File) and then Mode (File) = In_File);
   procedure New_Line (Spacing : Positive_Count := 1)
     with Global => (In_Out => File_System),
          Post => Settings = Settings'Old;

   --  past Spacing line terminators; End_Error when the file ends before the last (at its end
   --  on entry, or later when Spacing is above 1). (Its End_Error case names End_Of_File on
   --  entry, which needs the file open for input: so that is a precondition here, as for
   --  Skip_Page and the procedure Get_Line, rather than Status_Error and Mode_Error.)
   procedure Skip_Line (File : File_Type; Spacing : Positive_Count := 1)
     with Pre => Is_Open (File) and then Mode (File) = In_File,
          Global => (In_Out => File_System),
          Post => Settings = Settings'Old,
          Exceptional_Cases => (End_Error => Settings = Settings'Old and (End_Of_File (File)'Old or else Spacing > 1));
   procedure Skip_Line (Spacing : Positive_Count := 1)
     with Global => (In_Out => File_System),
          Post => Settings = Settings'Old,
          Exceptional_Cases => (End_Error => Settings = Settings'Old and (End_Of_File'Old or else Spacing > 1));

   function End_Of_Line (File : File_Type) return Boolean
     with Pre => Is_Open (File) and then Mode (File) = In_File, Global => File_System,
          Post => (if Is_Current_Input (File) then End_Of_Line'Result = End_Of_Line);
   function End_Of_Line return Boolean
     with Global => File_System;

   --  a page terminator (the current line ended first)
   procedure New_Page (File : File_Type)
     with Global => (In_Out => File_System),
          Post => Settings = Settings'Old,
          Exceptional_Cases => (Status_Error => not Is_Open (File),
                                Mode_Error => Is_Open (File) and then Mode (File) = In_File);
   procedure New_Page
     with Global => (In_Out => File_System),
          Post => Settings = Settings'Old;

   --  past the next page terminator (or to the end of the file); End_Error when the file is at
   --  its end on entry
   procedure Skip_Page (File : File_Type)
     with Pre => Is_Open (File) and then Mode (File) = In_File,
          Global => (In_Out => File_System),
          Post => Settings = Settings'Old,
          Exceptional_Cases => (End_Error => Settings = Settings'Old and End_Of_File (File)'Old);
   procedure Skip_Page
     with Global => (In_Out => File_System),
          Post => Settings = Settings'Old,
          Exceptional_Cases => (End_Error => Settings = Settings'Old and End_Of_File'Old);

   function End_Of_Page (File : File_Type) return Boolean
     with Pre => Is_Open (File) and then Mode (File) = In_File, Global => File_System,
          Post => (if Is_Current_Input (File) then End_Of_Page'Result = End_Of_Page);
   function End_Of_Page return Boolean
     with Global => File_System;

   function End_Of_File (File : File_Type) return Boolean
     with Pre => Is_Open (File) and then Mode (File) = In_File, Global => File_System,
          Post => (if Is_Current_Input (File) then End_Of_File'Result = End_Of_File);
   function End_Of_File return Boolean
     with Global => File_System;

   --  (for contracts: the line length, or the page length, of a file open for output; 0, as
   --  for no bound, for a file that is not)
   function Line_Bound (File : File_Type) return Count
     with Ghost, Global => File_System,
          Post => Line_Bound'Result = (if Is_Open (File) and then Mode (File) /= In_File then Line_Length (File) else 0);
   function Page_Bound (File : File_Type) return Count
     with Ghost, Global => File_System,
          Post => Page_Bound'Result = (if Is_Open (File) and then Mode (File) /= In_File then Page_Length (File) else 0);

   --  (for contracts: the files' settings, as one value: which files are the current default
   --  input, output and error, and every file's line and page lengths. Whether File is a
   --  current file (open, then), or the current input or output. The settings after
   --  Set_Input, Set_Output or Set_Error of File; after Set_Line_Length or Set_Page_Length of
   --  File; after File is reset (its lengths unbounded), or closed (where it was current, the
   --  standard file is again))
   type File_Settings is private;
   function Settings return File_Settings
     with Ghost, Global => File_System;
   --  (whether no line or page length has been set since the program started, so that every
   --  one is unbounded: Text_IO's Initial_Condition)
   function No_Lengths_Set return Boolean
     with Ghost, Global => File_System;
   function Is_Current (File : File_Type) return Boolean
     with Ghost, Global => File_System;
   function Is_Current_Input (File : File_Type) return Boolean
     with Ghost, Global => File_System;
   function Is_Current_Output (File : File_Type) return Boolean
     with Ghost, Global => File_System;
   function With_Input (Files : File_Settings; File : File_Type) return File_Settings
     with Ghost, Global => null;
   function With_Output (Files : File_Settings; File : File_Type) return File_Settings
     with Ghost, Global => null;
   function With_Error (Files : File_Settings; File : File_Type) return File_Settings
     with Ghost, Global => null;
   function With_Line_Length (Files : File_Settings; File : File_Type; To : Count) return File_Settings
     with Ghost, Global => null;
   function With_Page_Length (Files : File_Settings; File : File_Type; To : Count) return File_Settings
     with Ghost, Global => null;
   --  (the current output's)
   function With_Line_Length (Files : File_Settings; To : Count) return File_Settings
     with Ghost, Global => null;
   function With_Page_Length (Files : File_Settings; To : Count) return File_Settings
     with Ghost, Global => null;
   function Reset_Of (Files : File_Settings; File : File_Type) return File_Settings
     with Ghost, Global => null;
   function Without (Files : File_Settings; File : File_Type) return File_Settings
     with Ghost, Global => null;

   --  output: spaces (after a new line, when past it) up to column To; Layout_Error when the
   --  line length is bounded and To is beyond it (never for column 1). Input: characters
   --  skipped up to column To; End_Error when the file ends first
   procedure Set_Col (File : File_Type; To : Positive_Count)
     with Global => (In_Out => File_System),
          Post => Settings = Settings'Old,
          Exceptional_Cases => (Status_Error => not Is_Open (File),
                                Layout_Error => Settings = Settings'Old and (Line_Bound (File)'Old /= 0 and then To > Line_Bound (File)'Old),
                                End_Error => Settings = Settings'Old and then Is_Open (File) and then Mode (File) = In_File);
   procedure Set_Col (To : Positive_Count)
     with Global => (In_Out => File_System),
          Post => Settings = Settings'Old,
          Exceptional_Cases => (Layout_Error => Settings = Settings'Old and (Line_Length'Old /= 0 and then To > Line_Length'Old));

   --  output: new lines (after a new page, when past it) up to line To; Layout_Error when the
   --  page length is bounded and To is beyond it (never for line 1). Input: lines skipped up
   --  to line To; End_Error when the file ends first
   procedure Set_Line (File : File_Type; To : Positive_Count)
     with Global => (In_Out => File_System),
          Post => Settings = Settings'Old,
          Exceptional_Cases => (Status_Error => not Is_Open (File),
                                Layout_Error => Settings = Settings'Old and (Page_Bound (File)'Old /= 0 and then To > Page_Bound (File)'Old),
                                End_Error => Settings = Settings'Old and then Is_Open (File) and then Mode (File) = In_File);
   procedure Set_Line (To : Positive_Count)
     with Global => (In_Out => File_System),
          Post => Settings = Settings'Old,
          Exceptional_Cases => (Layout_Error => Settings = Settings'Old and (Page_Length'Old /= 0 and then To > Page_Length'Old));

   function Col (File : File_Type) return Positive_Count
     with Pre => Is_Open (File), Global => File_System,
          Post => (if Is_Current_Output (File) then Col'Result = Col);
   function Col return Positive_Count
     with Global => File_System;
   function Line (File : File_Type) return Positive_Count
     with Pre => Is_Open (File), Global => File_System,
          Post => (if Is_Current_Output (File) then Line'Result = Line);
   function Line return Positive_Count
     with Global => File_System;
   function Page (File : File_Type) return Positive_Count
     with Pre => Is_Open (File), Global => File_System,
          Post => (if Is_Current_Output (File) then Page'Result = Page);
   function Page return Positive_Count
     with Global => File_System;

   --  ---- characters ----

   --  the next character, line and page terminators skipped; End_Error at the file's end
   procedure Get (File : File_Type; Item : out Character)
     with Global => (In_Out => File_System),
          Post => Settings = Settings'Old,
          Exceptional_Cases => (Status_Error => not Is_Open (File),
                                Mode_Error => Is_Open (File) and then Mode (File) /= In_File,
                                End_Error => Settings = Settings'Old);
   procedure Get (Item : out Character)
     with Global => (In_Out => File_System),
          Post => Settings = Settings'Old,
          Exceptional_Cases => (End_Error => Settings = Settings'Old);

   procedure Put (File : File_Type; Item : Character)
     with Global => (In_Out => File_System),
          Post => Settings = Settings'Old,
          Exceptional_Cases => (Status_Error => not Is_Open (File),
                                Mode_Error => Is_Open (File) and then Mode (File) = In_File);
   procedure Put (Item : Character)
     with Global => (In_Out => File_System),
          Post => Settings = Settings'Old;

   --  the next character, not taken, or End_Of_Line when the line has ended
   procedure Look_Ahead (File : File_Type; Item : out Character; End_Of_Line : out Boolean)
     with Global => (In_Out => File_System),
          Post => Settings = Settings'Old,
          Exceptional_Cases => (Status_Error => not Is_Open (File),
                                Mode_Error => Is_Open (File) and then Mode (File) /= In_File);
   procedure Look_Ahead (Item : out Character; End_Of_Line : out Boolean)
     with Global => (In_Out => File_System),
          Post => Settings = Settings'Old;

   --  ---- strings and lines ----

   --  Item'Length characters, as Get reads them
   procedure Get (File : File_Type; Item : out String)
     with Global => (In_Out => File_System),
          Post => Settings = Settings'Old,
          Exceptional_Cases => (Status_Error => not Is_Open (File),
                                Mode_Error => Is_Open (File) and then Mode (File) /= In_File,
                                End_Error => Settings = Settings'Old);
   procedure Get (Item : out String)
     with Global => (In_Out => File_System),
          Post => Settings = Settings'Old,
          Exceptional_Cases => (End_Error => Settings = Settings'Old);

   procedure Put (File : File_Type; Item : String)
     with Global => (In_Out => File_System),
          Post => Settings = Settings'Old,
          Exceptional_Cases => (Status_Error => not Is_Open (File),
                                Mode_Error => Is_Open (File) and then Mode (File) = In_File);
   procedure Put (Item : String)
     with Global => (In_Out => File_System),
          Post => Settings = Settings'Old;

   procedure Put_Line (File : File_Type; Item : String)
     with Global => (In_Out => File_System),
          Post => Settings = Settings'Old,
          Exceptional_Cases => (Status_Error => not Is_Open (File),
                                Mode_Error => Is_Open (File) and then Mode (File) = In_File);
   procedure Put_Line (Item : String)
     with Global => (In_Out => File_System),
          Post => Settings = Settings'Old;

   --  a line's characters to Item from Item'First, and Last the index of the last one
   --  (Item'First - 1 for none); the line terminator is skipped when it is reached (not when
   --  Item is filled just before it). End_Error when no line is left to read (the file at its
   --  end on entry); an empty Item reads nothing
   procedure Get_Line (File : File_Type; Item : out String; Last : out Natural)
     with Pre => Is_Open (File) and then Mode (File) = In_File and then Item'First > 0,
          Post => Settings = Settings'Old
                  and then (if Item'Length > 0 then Last in Item'First - 1 .. Item'Last else Last = Item'First - 1),
          Global => (In_Out => File_System),
          Exceptional_Cases => (End_Error => Settings = Settings'Old and End_Of_File (File)'Old);
   procedure Get_Line (Item : out String; Last : out Natural)
     with Pre => Item'First > 0,
          Post => Settings = Settings'Old
                  and then (if Item'Length > 0 then Last in Item'First - 1 .. Item'Last else Last = Item'First - 1),
          Global => (In_Out => File_System),
          Exceptional_Cases => (End_Error => Settings = Settings'Old and End_Of_File'Old);

   --  a line of any length: a function with side effects, since each call reads the next
   --  line, so it is called only as a declaration's or an assignment's value; a function
   --  propagates no exception, so a line must be left to read
   function Get_Line return String
     with Side_Effects, Pre => not End_Of_File, Global => (In_Out => File_System),
          Post => Settings = Settings'Old
                  and then Get_Line'Result'Length < Verada.Storage.Length_Bound;

   function Get_Line (File : File_Type) return String
     with Side_Effects, Pre => Is_Open (File) and then Mode (File) = In_File and then not End_Of_File (File),
          Global => (In_Out => File_System),
          Post => Settings = Settings'Old
                  and then Get_Line'Result'Length < Verada.Storage.Length_Bound;

   --  the next character, a control character (a line or page terminator's) too, at once
   --  (RM A.10.7); End_Error at the file's end
   procedure Get_Immediate (File : File_Type; Item : out Character)
     with Global => (In_Out => File_System),
          Post => Settings = Settings'Old,
          Exceptional_Cases => (Status_Error => not Is_Open (File),
                                Mode_Error => Is_Open (File) and then Mode (File) /= In_File,
                                End_Error => Settings = Settings'Old);
   procedure Get_Immediate (Item : out Character)
     with Global => (In_Out => File_System),
          Post => Settings = Settings'Old,
          Exceptional_Cases => (End_Error => Settings = Settings'Old);

   --  the same without waiting: Available is False when no character has arrived yet (a
   --  file's are always there; standard input's when it has been typed), Item then ' '
   procedure Get_Immediate (File : File_Type; Item : out Character; Available : out Boolean)
     with Global => (In_Out => File_System),
          Post => Settings = Settings'Old,
          Exceptional_Cases => (Status_Error => not Is_Open (File),
                                Mode_Error => Is_Open (File) and then Mode (File) /= In_File,
                                End_Error => Settings = Settings'Old);
   procedure Get_Immediate (Item : out Character; Available : out Boolean)
     with Global => (In_Out => File_System),
          Post => Settings = Settings'Old,
          Exceptional_Cases => (End_Error => Settings = Settings'Old);

   --  ---- numbers and enumeration values ----
   --
   --  Get reads, after any blanks and line terminators, the longest run of characters that
   --  can be the number's or the value's literal (a Width other than 0 reads that many
   --  characters instead), and takes its value with Verada.Scanning, as `'Value` does:
   --  Data_Error when it is not one of the type's, End_Error when the input has ended. Get
   --  from a string reads its characters from From'First, and Last is the last read. Put
   --  writes the literal right-justified in Width characters (more when it needs them), as one
   --  item (RM A.10.6(8)): when the line length is bounded, a new line first 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. Put to a string fills To, and Layout_Error when it does not fit.

   generic
      type Num is range <>;
   package Integer_IO is
      Default_Width : Field := Num'Width;
      Default_Base : Number_Base := 10;

      procedure Get (File : File_Type; Item : out Num; Width : Field := 0)
        with Global => (In_Out => File_System),
             Post => Settings = Settings'Old,
             Exceptional_Cases => (Status_Error => not Is_Open (File),
                                   Mode_Error => Is_Open (File) and then Mode (File) /= In_File,
                                   Data_Error | End_Error => Settings = Settings'Old);
      procedure Get (Item : out Num; Width : Field := 0)
        with Global => (In_Out => File_System),
             Post => Settings = Settings'Old,
             Exceptional_Cases => (Data_Error | End_Error => Settings = Settings'Old);

      --  (for contracts: how many characters Put writes for Item, Width and Base)
      function Put_Length (Item : Num; Width : Field; Base : Number_Base) return Count
        with Ghost, Global => null,
             Post => Put_Length'Result >= Count (Width) and then Put_Length'Result <= Count (Integer'Max (Width, 133));

      procedure Put (File : File_Type; Item : Num; Width : Field := Default_Width; Base : Number_Base := Default_Base)
        with Global => (In_Out => File_System),
             Post => Settings = Settings'Old,
             Exceptional_Cases => (Status_Error => not Is_Open (File),
                                   Mode_Error => Is_Open (File) and then Mode (File) = In_File,
                                   Layout_Error => Settings = Settings'Old
                                                   and (Line_Bound (File)'Old /= 0
                                                        and then Put_Length (Item, Width, Base) > Line_Bound (File)'Old));
      procedure Put (Item : Num; Width : Field := Default_Width; Base : Number_Base := Default_Base)
        with Global => (In_Out => File_System),
             Post => Settings = Settings'Old,
             Exceptional_Cases => (Layout_Error => Settings = Settings'Old
                                                   and (Line_Length'Old /= 0
                                                        and then Put_Length (Item, Width, Base) > Line_Length'Old));

      procedure Get (From : String; Item : out Num; Last : out Positive)
        with Global => null,
             Post => Last in From'Range,
             Exceptional_Cases => (Data_Error | End_Error => True);
      procedure Put (To : out String; Item : Num; Base : Number_Base := Default_Base)
        with Global => null,
             Exceptional_Cases => (Layout_Error => True);
   end Integer_IO;

   generic
      type Num is mod <>;
   package Modular_IO is
      Default_Width : Field := Num'Width;
      Default_Base : Number_Base := 10;

      procedure Get (File : File_Type; Item : out Num; Width : Field := 0)
        with Global => (In_Out => File_System),
             Post => Settings = Settings'Old,
             Exceptional_Cases => (Status_Error => not Is_Open (File),
                                   Mode_Error => Is_Open (File) and then Mode (File) /= In_File,
                                   Data_Error | End_Error => Settings = Settings'Old);
      procedure Get (Item : out Num; Width : Field := 0)
        with Global => (In_Out => File_System),
             Post => Settings = Settings'Old,
             Exceptional_Cases => (Data_Error | End_Error => Settings = Settings'Old);

      --  (for contracts: how many characters Put writes for Item, Width and Base)
      function Put_Length (Item : Num; Width : Field; Base : Number_Base) return Count
        with Ghost, Global => null,
             Post => Put_Length'Result >= Count (Width) and then Put_Length'Result <= Count (Integer'Max (Width, 133));

      procedure Put (File : File_Type; Item : Num; Width : Field := Default_Width; Base : Number_Base := Default_Base)
        with Global => (In_Out => File_System),
             Post => Settings = Settings'Old,
             Exceptional_Cases => (Status_Error => not Is_Open (File),
                                   Mode_Error => Is_Open (File) and then Mode (File) = In_File,
                                   Layout_Error => Settings = Settings'Old
                                                   and (Line_Bound (File)'Old /= 0
                                                        and then Put_Length (Item, Width, Base) > Line_Bound (File)'Old));
      procedure Put (Item : Num; Width : Field := Default_Width; Base : Number_Base := Default_Base)
        with Global => (In_Out => File_System),
             Post => Settings = Settings'Old,
             Exceptional_Cases => (Layout_Error => Settings = Settings'Old
                                                   and (Line_Length'Old /= 0
                                                        and then Put_Length (Item, Width, Base) > Line_Length'Old));

      procedure Get (From : String; Item : out Num; Last : out Positive)
        with Global => null,
             Post => Last in From'Range,
             Exceptional_Cases => (Data_Error | End_Error => True);
      procedure Put (To : out String; Item : Num; Base : Number_Base := Default_Base)
        with Global => null,
             Exceptional_Cases => (Layout_Error => True);
   end Modular_IO;

   --  floating point: Put writes Fore characters before the point (the sign among them), Aft
   --  digits after it, and, when Exp is not 0, an exponent of Exp characters (its sign among
   --  them); the digits are the value's, correctly rounded
   generic
      type Num is digits <>;
   package Float_IO is
      Default_Fore : Field := 2;
      Default_Aft : Field := Num'Digits - 1;
      Default_Exp : Field := 3;

      procedure Get (File : File_Type; Item : out Num; Width : Field := 0)
        with Global => (In_Out => File_System),
             Post => Settings = Settings'Old,
             Exceptional_Cases => (Status_Error => not Is_Open (File),
                                   Mode_Error => Is_Open (File) and then Mode (File) /= In_File,
                                   Data_Error | End_Error => Settings = Settings'Old);
      procedure Get (Item : out Num; Width : Field := 0)
        with Global => (In_Out => File_System),
             Post => Settings = Settings'Old,
             Exceptional_Cases => (Data_Error | End_Error => Settings = Settings'Old);

      --  (for contracts: how many characters Put writes for Item, Fore, Aft and Exp)
      function Put_Length (Item : Num; Fore, Aft, Exp : Field) return Count
        with Ghost, Global => null;

      procedure Put (File : File_Type; Item : Num; Fore : Field := Default_Fore; Aft : Field := Default_Aft; Exp : Field := Default_Exp)
        with Global => (In_Out => File_System),
             Post => Settings = Settings'Old,
             Exceptional_Cases => (Status_Error => not Is_Open (File),
                                   Mode_Error => Is_Open (File) and then Mode (File) = In_File,
                                   Layout_Error => Settings = Settings'Old
                                                   and (Line_Bound (File)'Old /= 0
                                                        and then Put_Length (Item, Fore, Aft, Exp) > Line_Bound (File)'Old));
      procedure Put (Item : Num; Fore : Field := Default_Fore; Aft : Field := Default_Aft; Exp : Field := Default_Exp)
        with Global => (In_Out => File_System),
             Post => Settings = Settings'Old,
             Exceptional_Cases => (Layout_Error => Settings = Settings'Old
                                                   and (Line_Length'Old /= 0
                                                        and then Put_Length (Item, Fore, Aft, Exp) > Line_Length'Old));

      procedure Get (From : String; Item : out Num; Last : out Positive)
        with Global => null,
             Post => Last in From'Range,
             Exceptional_Cases => (Data_Error | End_Error => True);
      procedure Put (To : out String; Item : Num; Aft : Field := Default_Aft; Exp : Field := Default_Exp)
        with Global => null,
             Exceptional_Cases => (Layout_Error => True);
   end Float_IO;

   --  fixed point: as floating point, the digits the value's exactly, rounded to the nearest
   --  (halves away from zero); a value read is truncated to a multiple of the small, as a
   --  conversion to the type is
   generic
      type Num is delta <>;
   package Fixed_IO is
      Default_Fore : Field := Num'Fore;
      Default_Aft : Field := Num'Aft;
      Default_Exp : Field := 0;

      procedure Get (File : File_Type; Item : out Num; Width : Field := 0)
        with Global => (In_Out => File_System),
             Post => Settings = Settings'Old,
             Exceptional_Cases => (Status_Error => not Is_Open (File),
                                   Mode_Error => Is_Open (File) and then Mode (File) /= In_File,
                                   Data_Error | End_Error => Settings = Settings'Old);
      procedure Get (Item : out Num; Width : Field := 0)
        with Global => (In_Out => File_System),
             Post => Settings = Settings'Old,
             Exceptional_Cases => (Data_Error | End_Error => Settings = Settings'Old);

      --  (for contracts: how many characters Put writes for Item, Fore, Aft and Exp)
      function Put_Length (Item : Num; Fore, Aft, Exp : Field) return Count
        with Ghost, Global => null;

      procedure Put (File : File_Type; Item : Num; Fore : Field := Default_Fore; Aft : Field := Default_Aft; Exp : Field := Default_Exp)
        with Global => (In_Out => File_System),
             Post => Settings = Settings'Old,
             Exceptional_Cases => (Status_Error => not Is_Open (File),
                                   Mode_Error => Is_Open (File) and then Mode (File) = In_File,
                                   Layout_Error => Settings = Settings'Old
                                                   and (Line_Bound (File)'Old /= 0
                                                        and then Put_Length (Item, Fore, Aft, Exp) > Line_Bound (File)'Old));
      procedure Put (Item : Num; Fore : Field := Default_Fore; Aft : Field := Default_Aft; Exp : Field := Default_Exp)
        with Global => (In_Out => File_System),
             Post => Settings = Settings'Old,
             Exceptional_Cases => (Layout_Error => Settings = Settings'Old
                                                   and (Line_Length'Old /= 0
                                                        and then Put_Length (Item, Fore, Aft, Exp) > Line_Length'Old));

      procedure Get (From : String; Item : out Num; Last : out Positive)
        with Global => null,
             Post => Last in From'Range,
             Exceptional_Cases => (Data_Error | End_Error => True);
      procedure Put (To : out String; Item : Num; Aft : Field := Default_Aft; Exp : Field := Default_Exp)
        with Global => null,
             Exceptional_Cases => (Layout_Error => True);
   end Fixed_IO;

   --  decimal fixed point: as fixed point
   generic
      type Num is delta <> digits <>;
   package Decimal_IO is
      Default_Fore : Field := Num'Fore;
      Default_Aft : Field := Num'Aft;
      Default_Exp : Field := 0;

      procedure Get (File : File_Type; Item : out Num; Width : Field := 0)
        with Global => (In_Out => File_System),
             Post => Settings = Settings'Old,
             Exceptional_Cases => (Status_Error => not Is_Open (File),
                                   Mode_Error => Is_Open (File) and then Mode (File) /= In_File,
                                   Data_Error | End_Error => Settings = Settings'Old);
      procedure Get (Item : out Num; Width : Field := 0)
        with Global => (In_Out => File_System),
             Post => Settings = Settings'Old,
             Exceptional_Cases => (Data_Error | End_Error => Settings = Settings'Old);

      --  (for contracts: how many characters Put writes for Item, Fore, Aft and Exp)
      function Put_Length (Item : Num; Fore, Aft, Exp : Field) return Count
        with Ghost, Global => null;

      procedure Put (File : File_Type; Item : Num; Fore : Field := Default_Fore; Aft : Field := Default_Aft; Exp : Field := Default_Exp)
        with Global => (In_Out => File_System),
             Post => Settings = Settings'Old,
             Exceptional_Cases => (Status_Error => not Is_Open (File),
                                   Mode_Error => Is_Open (File) and then Mode (File) = In_File,
                                   Layout_Error => Settings = Settings'Old
                                                   and (Line_Bound (File)'Old /= 0
                                                        and then Put_Length (Item, Fore, Aft, Exp) > Line_Bound (File)'Old));
      procedure Put (Item : Num; Fore : Field := Default_Fore; Aft : Field := Default_Aft; Exp : Field := Default_Exp)
        with Global => (In_Out => File_System),
             Post => Settings = Settings'Old,
             Exceptional_Cases => (Layout_Error => Settings = Settings'Old
                                                   and (Line_Length'Old /= 0
                                                        and then Put_Length (Item, Fore, Aft, Exp) > Line_Length'Old));

      procedure Get (From : String; Item : out Num; Last : out Positive)
        with Global => null,
             Post => Last in From'Range,
             Exceptional_Cases => (Data_Error | End_Error => True);
      procedure Put (To : out String; Item : Num; Aft : Field := Default_Aft; Exp : Field := Default_Exp)
        with Global => null,
             Exceptional_Cases => (Layout_Error => True);
   end Decimal_IO;

   --  enumeration values: Put writes the literal in Set's case (a character literal as it is),
   --  left-justified in Width characters
   generic
      type Enum is (<>);
   package Enumeration_IO is
      Default_Width : Field := 0;
      Default_Setting : Type_Set := Upper_Case;

      procedure Get (File : File_Type; Item : out Enum)
        with Global => (In_Out => File_System),
             Post => Settings = Settings'Old,
             Exceptional_Cases => (Status_Error => not Is_Open (File),
                                   Mode_Error => Is_Open (File) and then Mode (File) /= In_File,
                                   Data_Error | End_Error => Settings = Settings'Old);
      procedure Get (Item : out Enum)
        with Global => (In_Out => File_System),
             Post => Settings = Settings'Old,
             Exceptional_Cases => (Data_Error | End_Error => Settings = Settings'Old);

      --  (for contracts: how many characters Put writes for Item and Width)
      function Put_Length (Item : Enum; Width : Field) return Count
        with Ghost, Global => null,
             Post => Put_Length'Result >= Count (Width);

      procedure Put (File : File_Type; Item : Enum; Width : Field := Default_Width; Set : Type_Set := Default_Setting)
        with Global => (In_Out => File_System),
             Post => Settings = Settings'Old,
             Exceptional_Cases => (Status_Error => not Is_Open (File),
                                   Mode_Error => Is_Open (File) and then Mode (File) = In_File,
                                   Layout_Error => Settings = Settings'Old
                                                   and (Line_Bound (File)'Old /= 0
                                                        and then Put_Length (Item, Width) > Line_Bound (File)'Old));
      procedure Put (Item : Enum; Width : Field := Default_Width; Set : Type_Set := Default_Setting)
        with Global => (In_Out => File_System),
             Post => Settings = Settings'Old,
             Exceptional_Cases => (Layout_Error => Settings = Settings'Old
                                                   and (Line_Length'Old /= 0
                                                        and then Put_Length (Item, Width) > Line_Length'Old));

      procedure Get (From : String; Item : out Enum; Last : out Positive)
        with Global => null,
             Post => Last in From'Range,
             Exceptional_Cases => (Data_Error | End_Error => True);
      procedure Put (To : out String; Item : Enum; Set : Type_Set := Default_Setting)
        with Global => null,
             Exceptional_Cases => (Layout_Error => True);
   end Enumeration_IO;
private
   --  a handle of the run time's (0 for a closed file): 1 standard output, 2 standard error,
   --  3 standard input, 4 on the files the program opened; limited, so passed by reference
   type File_Type is limited record
      Handle : Integer := 0;
      Mode   : File_Mode := Out_File;
   end record;

   function Is_Open (File : File_Type) return Boolean is (File.Handle /= 0);
   function Mode (File : File_Type) return File_Mode is (File.Mode);
   function Form (File : File_Type) return String is ("");

   function Standard_Input return File_Type is ((Handle => 3, Mode => In_File));
   function Standard_Output return File_Type is ((Handle => 1, Mode => Out_File));
   function Standard_Error return File_Type is ((Handle => 2, Mode => Out_File));

   --  the settings: the current files' handles (the run time's), and a token of every file's
   --  lengths, which Length_Of reads (a scalar, so that two settings are equal exactly when
   --  their parts are)
   type Lengths_Token is range 0 .. 2 ** 62;
   type File_Settings is record
      Input, Output, Error : Integer := 0;
      Lengths : Lengths_Token := 0;
   end record;

   --  (the run time's lengths, for contracts: the token now, the length What, 3 the line's or
   --  4 the page's, of a handle, and the token after the lengths of Handle are set, where Line
   --  and Page say)
   function Lengths return Lengths_Token
     with Ghost, Import, Convention => Ada, External_Name => "verada_rt_tio_lengths", Global => File_System;
   function Length_Of (Lengths : Lengths_Token; Handle, What : Integer) return Count
     with Ghost, Import, Convention => Ada, External_Name => "verada_rt_tio_length_of", Global => null,
          Post => (if Lengths = 0 then Length_Of'Result = 0);
   function With_Lengths (Lengths : Lengths_Token; Handle : Integer; Line_Length, Page_Length : Count;
                          Line, Page : Boolean) return Lengths_Token
     with Ghost, Import, Convention => Ada, External_Name => "verada_rt_tio_with_lengths", Global => null,
          Post => (for all H in Integer =>
                     Length_Of (With_Lengths'Result, H, 3) = (if H = Handle and then Line then Line_Length else Length_Of (Lengths, H, 3))
                     and Length_Of (With_Lengths'Result, H, 4) = (if H = Handle and then Page then Page_Length else Length_Of (Lengths, H, 4)));

   --  (Which: 0 input, 1 output, 2 error)
   function Current_Handle (Which : Integer) return Integer
     with Import, Convention => Ada, External_Name => "verada_rt_tio_current", Global => File_System,
          Post => Current_Handle'Result in 1 .. Integer'Last;

   --  (What: 0 the column, 1 the line, 2 the page, 3 the line length, 4 the page length; here
   --  so that the lengths are published, which Put's Layout_Error cases name in instances)
   function Position (Handle : Integer; What : Integer) return Count
     with Import, Convention => Ada, External_Name => "verada_rt_tio_position", Global => File_System,
          Post => (if What <= 2 then Position'Result >= 1)
                  and (if What in 3 .. 4 then Position'Result = Length_Of (Lengths, Handle, What));

   function Line_Length (File : File_Type) return Count is (Position (File.Handle, 3));
   function Line_Length return Count is (Position (Current_Handle (1), 3));
   function Page_Length (File : File_Type) return Count is (Position (File.Handle, 4));
   function Page_Length return Count is (Position (Current_Handle (1), 4));
   function Line_Bound (File : File_Type) return Count is
     (if Is_Open (File) and then File.Mode /= In_File then Position (File.Handle, 3) else 0);
   function Page_Bound (File : File_Type) return Count is
     (if Is_Open (File) and then File.Mode /= In_File then Position (File.Handle, 4) else 0);

   function Settings return File_Settings is
     ((Input => Current_Handle (0), Output => Current_Handle (1), Error => Current_Handle (2), Lengths => Lengths));
   function No_Lengths_Set return Boolean is (Lengths = 0);
   function Is_Current_Input (File : File_Type) return Boolean is
     (File.Handle /= 0 and then File.Handle = Settings.Input);
   function Is_Current_Output (File : File_Type) return Boolean is
     (File.Handle /= 0 and then File.Handle = Settings.Output);
   function Is_Current (File : File_Type) return Boolean is
     (Is_Current_Input (File) or else Is_Current_Output (File)
      or else (File.Handle /= 0 and then File.Handle = Settings.Error));

   function With_Input (Files : File_Settings; File : File_Type) return File_Settings is
     ((Files with delta Input => File.Handle));
   function With_Output (Files : File_Settings; File : File_Type) return File_Settings is
     ((Files with delta Output => File.Handle));
   function With_Error (Files : File_Settings; File : File_Type) return File_Settings is
     ((Files with delta Error => File.Handle));
   --  (by handle, for the run time's operations)
   function With_Lengths_Of (Files : File_Settings; Handle : Integer; Line_Length, Page_Length : Count;
                             Line, Page : Boolean) return File_Settings is
     ((Files with delta Lengths => With_Lengths (Files.Lengths, Handle, Line_Length, Page_Length, Line, Page)))
     with Ghost;
   --  (a file reset, or closed, has its lengths unbounded; a standard file is neither)
   function Reset_Handle_Of (Files : File_Settings; Handle : Integer) return File_Settings is
     (With_Lengths_Of (Files, Handle, 0, 0, Handle >= 4, Handle >= 4))
     with Ghost;
   function Without_Handle (Files : File_Settings; Handle : Integer) return File_Settings is
     ((Input => (if Handle >= 4 and then Files.Input = Handle then 3 else Files.Input),
       Output => (if Handle >= 4 and then Files.Output = Handle then 1 else Files.Output),
       Error => (if Handle >= 4 and then Files.Error = Handle then 2 else Files.Error),
       Lengths => With_Lengths (Files.Lengths, Handle, 0, 0, Handle >= 4, Handle >= 4)))
     with Ghost;
   function After_Reset (Files : File_Settings; Handle : Integer; Done : Boolean) return File_Settings is
     (if Done then Reset_Handle_Of (Files, Handle) else Without_Handle (Files, Handle))
     with Ghost;
   function With_Line_Length (Files : File_Settings; File : File_Type; To : Count) return File_Settings is
     (With_Lengths_Of (Files, File.Handle, To, 0, True, False));
   function With_Page_Length (Files : File_Settings; File : File_Type; To : Count) return File_Settings is
     (With_Lengths_Of (Files, File.Handle, 0, To, False, True));
   function With_Line_Length (Files : File_Settings; To : Count) return File_Settings is
     (With_Lengths_Of (Files, Files.Output, To, 0, True, False));
   function With_Page_Length (Files : File_Settings; To : Count) return File_Settings is
     (With_Lengths_Of (Files, Files.Output, 0, To, False, True));
   function Reset_Of (Files : File_Settings; File : File_Type) return File_Settings is
     (Reset_Handle_Of (Files, File.Handle));
   function Without (Files : File_Settings; File : File_Type) return File_Settings is
     (Without_Handle (Files, File.Handle));
end Ada.Text_IO;
