Wednesday, September 19, 2012

AdaTutor - Advanced Topics

Packages Standard and Ada.Characters.Latin_1

Ada comes with a package Standard.  However, unlike all the other packages, Standard is needed by every Ada compilation unit.  Therefore, Standard is automatically withed and used in every compilation.  It need not be mentioned in a context clause.  Standard contains the definitions built into the Ada language, such as type Boolean is (False, True);.  A listing of the package specification is in Annex A.1 of the Ada 95 RM.  Thus, the full name for the type Boolean is Standard.Boolean, the full name for Integer is Standard.Integer, etc.  Naturally, this normally need not concern the programmer.  The dot notation is automatic because Standard is automatically used in every compilation.

Ada 95 has a package Ada.Characters.Latin_1, defined in Annex A.3.3 of the Ada 95 RM.  This package gives names to all of the unprintable 8-bit characters, and some of the printable ones.  For example, the unprintable "bell" character is named BEL.  If our program withs and uses this package and Ada.Text_IO, then Put(BEL); will beep the terminal.

Ada 83 doesn't have Ada.Characters.Latin_1, but it has a package ASCII inside package Standard.  This package gives names to all of the unprintable ASCII characters and some of the printable ones.  (The ASCII characters are the first 128 of the 256 8-bit characters.)  Ada 95 also has package ASCII for compatibility, but with Ada 95 it's better to use Ada.Characters.Latin_1.

Since ASCII is inside Standard, we never have to write a with clause for it.  But ASCII isn't automatically used.  If we want the dot notation for ASCII to be automatic, we have to provide a use clause.

For example, either of these Ada 83 programs will beep the terminal:

   with Text_IO; use Text_IO;    with Text_IO; use Text_IO;
   procedure Beep is             procedure Beep is
   begin                           use ASCII;
     Put(ASCII.BEL);             begin
   end Beep;                       Put(BEL);
                                 end Beep;

Note the placement of use ASCII; in the second example.  It's similar to the placement of use My_Int_IO; in ADD.ADA, which we discussed early in the course.

An Alternative to Infix Notation, and Use Type

Earlier we learned to define and use infix operators like

   type Vector is array(Integer range <>) of Float;
   function "*"(Left, Right : in Vector) return Float;
   A, B : Vector(1 .. 10);
   F    : Float;
   F := A * B;

An alternative notation equivalent to F := A * B; is F := "*"(A, B);.  Why would anyone want to use this clumsier notation?  If our function is in a package Math that the calling program withs but for some reason doesn't use, we could use dot notation and write F := Math."*"(A, B);.  But we couldn't use dot notation directly with infix operators, as in F := A Math.* B; or even F := A Math."*" B;.  Both of those are illegal.  The alternative notation can also be used to emphasize that an operator comes from package Standard.  For example, if I, J, and K are Integers, we could write I := Standard."*"(J, K);, which is equivalent to I := J * K;.

In Ada 95, a context clause can say use type, followed by the name of a type or subtype in a package.  This makes the dot notation automatic only for infix operators for that type or subtype.   For example, if we have a package Math containing the definition of a type Vector and a * operator for Vectors, as above, we can write

    with Math; use type Math.Vector;
      A, B : Math.Vector(1 .. 10);
      F    : Float;
      F := A * B;

Here we were able to write F := A * B; instead of F := "*"(A, B); or F := Math."*"(A, B); because we said use type Math.Vector; and the * here is an infix operator for that type.  However, in declaring A and B, we still had to write Math.Vector, because use type applies only to infix operators for the type or subtype named.


Assuming X, Y, and Z have been declared Float, which one of the following is illegal?
  1. X := Y / Z;
  2. X := Y Standard."/" Z;
  3. X := Standard."/"(Y, Z);

< prev   next >

No comments: