Rationale for Ada 2005
1.3.6 Overview: Standard library
There are significant improvements to the standard
library in Ada 2005. One of the strengths of Java is the huge library
that comes with it. Ada has tended to take the esoteric view that it
is a language for constructing programs from components and has in the
past rather assumed that the components would spring up by magic from
the user community. There has also perhaps been a reluctance to specify
standard components in case that preempted the development of better
ones. However, it is now recognized that standardizing useful stuff is
a good thing. And moreover, secondary ISO standards are not very helpful
because they are almost invisible. Ada 95 added quite a lot to the predefined
library and Ada 2005 adds more.
First, there are packages for manipulating vectors
and matrices already mentioned in Section
1.3.5
when discussing formal package parameters. There are two packages,
Ada.Numerics.Generic_Real_Arrays
for real vectors and matrices and
Ada.Numerics.Generic_Complex_Arrays
for complex vectors and matrices. They can be instantiated according
to the underlying floating point type used. There are also nongeneric
versions as usual.
These packages export
types for declaring vectors and matrices and many operations for manipulating
them. Thus if we have an expression in mathematical notation such as
y = Ax + z
where x, y
and z are vectors and A is
a square matrix, then this calculation can be simply programmed as
X, Y, Z: Real_Vector(1 .. N);
A: Real_Matrix(1 .. N, 1 .. N);
...
Y := A * X + Z;
and the appropriate operations will be invoked. The
packages also include subprograms for the most useful linear algebra
computations, namely, the solution of linear equations, matrix inversion
and determinant evaluation, plus the determination of eigenvalues and
eigenvectors for symmetric matrices (Hermitian in the complex case).
Thus to determine X given Y,
Z and A in the
above example we can write
X := Solve(A, Y – Z);
It should not be thought that these Ada packages
in any way compete with the very comprehensive BLAS (Basic Linear Algebra
Subprograms). The purpose of the Ada packages is to provide simple implementations
of very commonly used algorithms (perhaps for small embedded systems
or for prototyping) and to provide a solid framework for developing bindings
to the BLAS for more demanding situations. Incidentally, they are in
the Numerics annex.
Another (but very trivial) change to the Numerics
annex is that nongeneric versions of Ada.Text_IO.Complex_IO
have been added in line with the standard principle of providing nongeneric
versions of generic predefined packages for convenience. Their omission
from Ada 95 was an oversight.
There is a new predefined
package in Annex A for accessing tree-structured file systems. The scope
is perhaps indicated by this fragment of its specification
with ...
package Ada.Directories is
-- Directory and file operations
function Current_Directory return String;
procedure Set_Directory(Directory: in String);
...
-- File and directory name operations
function Full_Name(Name: in String) return String;
function Simple_Name(Name: in String) return String;
...
-- File and directory queries
type File_Kind is (Directory, Ordinary_File, Special_File);
type File_Size is range 0 .. implementation-defined;
function Exists(Name: in String) return Boolean;
...
-- Directory searching
type Directory_Entry_Type is limited private;
type Filter_Type is array (File_Kind) of Boolean;
...
-- Operations on directory entries
...
end Ada.Directories;
The package contains facilities which will be useful
on any Unix or Windows system. However, it has to be recognized that
like Ada.Command_Line it might not be supportable
on every environment.
There is also a package
Ada.Environment_Variables
for accessing the environment variables that occur in most operating
systems.
A number of additional subprograms have been added
to the existing string handling packages. There are several problems
with the Ada 95 packages. One is that conversion between bounded and
unbounded strings and the raw type String
is required rather a lot and is both ugly and inefficient. For example,
searching only part of a bounded or unbounded string can only be done
by converting it to a String and then searching
the appropriate slice (or by making a truncated copy first).
In brief the additional subprograms are as follows
- Three further versions of function
Index with an additional parameter From
indicating the start of the search are added to each of Strings.Fixed,
Strings.Bounded and Strings.Unbounded.
- A further version of function Index_Non_Blank
is similarly added to all three packages.
- A procedure Set_Bounded_String
with similar behaviour to the function To_Bounded_String
is added to Strings.Bounded. This avoids the
overhead of using a function. A similar procedure Set_Unbounded_String
is added to Strings.Unbounded.
- A function and procedure Bounded_Slice
are added to Strings.Bounded. These avoid
conversions from type String. A similar function
and procedure Unbounded_Sliceare added to
Strings.Unbounded.
As well as these additions there is a new package
Ada.Text_IO.Unbounded_IO for the input and
output of unbounded strings. This again avoids unnecessary conversion
to the type String. Similarly, there is a
generic package Ada.Text_IO.Bounded_IO; this
is generic because the package Strings.Bounded
has an inner generic package which is parameterized by the maximum string
length.
Finally, two functions Get_Line
are added to Ada.Text_IO itself. These avoid
difficulties with the length of the string which occurs with the existing
procedures Get_Line.
In Ada 83, program identifiers used the 7-bit ASCII
set. In Ada 95 this was extended to the 8-bit Latin-1 set. In Ada 2005
this is extended yet again to the entire ISO/IEC 10646:2003 character
repertoire. This means that identifiers can now use Cyrillic and Greek
characters. Thus we could extend the animal example by
Сталин : access Pig renames Napoleon;
Πεγασυς : Horse;
In order to encourage
us to write our mathematical programs nicely the additional constant
π : constant := Pi;
has been added to the package Ada.Numerics
in Ada 2005.
In a similar way types Wide_String
and Wide_Character were added to Ada 95. In
Ada 2005 this process is also extended and a set of wide-wide types and
packages for 32-bit characters are added. Thus we have types Wide_Wide_Character
and Wide_Wide_String and so on.
A major addition to the predefined library is the
package
Ada.Containers and its children plus
some auxiliary child functions of
Ada.Strings.
These are very important and considerable additions to the predefined
capability of Ada and bring the best in standard data structure manipulation
to the fingers of every Ada programmer. The scope is perhaps best illustrated
by listing the units involved.
Ada.Containers –
This is the root package and just declares types Hash_Type
and Count_Type which are an implementation-defined
modular and integer type respectively.
Ada.Strings.Hash
–
This function hashes a string into the type Hash_Type.
There are also versions for bounded and unbounded strrings.
Ada.Containers.Vectors
–
This is a generic package with parameters giving the index type and element
type of a vector plus "=" for the
element type. This package declares types and operations for manipulating
vectors. (These are vectors in the sense of flexible arrays and not the
mathematical vectors used for linear algebra as in the vectors and matrices
packages mentioned earlier.) As well as subprograms for adding, moving
and removing elements there are also generic subprograms for searching,
sorting and iterating over vectors.
Ada.Containers.Doubly_Linked_Lists
–
This is a generic package with parameters giving the element type and
"=" for the element type. This package
declares types and operations for manipulating doubly-linked lists. It
has similar functionality to the vectors package. Thus, as well as subprograms
for adding, moving and removing elements there are also generic subprograms
for searching, sorting and iterating over lists.
Ada.Containers.Hashed_Maps
–
This is a generic package with parameters giving a key type and an element
type plus a hash function for the key, a function to test for equality
between keys and "=" for the element
type. It declares types and operations for manipulating hashed maps.
Ada.Containers.Ordered_Maps
–
This is a similar generic package for ordered maps with parameters giving
a key type and an element type and "<"
for the key type and "=" for the
element type.
Ada.Containers.Hashed_Sets
–
This is a generic package with parameters giving the element type plus
a hash function for the elements and a function to test for equality
between elements. It declares types and operations for manipulating hashed
sets.
Ada.Containers.Ordered_Sets
–
This is a similar generic package for ordered sets with parameters giving
the element type and "<" and
"=" for the element type.
There are then another six packages with similar
functionality but for indefinite types with corresponding names such
as Ada.Containers.Indefinite_Vectors.
Ada.Containers.Generic_Array_Sort
–
This is a generic procedure for sorting arrays. The generic parameters
give the index type, the element type, the array type and
"<"
for the element type. The array type is unconstrained.
Finally there is a very similar generic procedure
Ada.Containers.Generic_Constrained_Array_Sort
but for constrained array types.
It is hoped that the above list gives a flavour of
the capability of the package
Containers.
Some examples of the use of the facilities will be given in a later chapter
(see
8, “
Containers”).
Finally, there are further packages for manipulating
times (that is of type
Ada.Calendar.Time and
not
Ada.Real_Time.Time and thus more appropriate
in a discussion of the predefined library than the real-time features).
The package
Ada.Calendar has a number of obvious
omissions and in order to rectify this the following packages are added.
Ada.Calendar.Time_Zones
–
This declares a type Time_Offset describing
in minutes the difference between two time zones and a function UTC_Time_Offset
which given a time returns the difference between the time zone of Calendar
at that time and UTC (Coordinated Universal Time which is close to Greenwich
Mean Time). It also has an exception which is raised if the time zone
of Calendar is not known (maybe the clock
is broken).
Ada.Calendar.Arithmetic
–
This declares various types and operations for coping with leap seconds.
Ada.Calendar.Formatting
–
This declares further types and operations for dealing with formatting
and related matters.
Most of the new calendar
features are clearly only for the chronological addict but the need for
them does illustrate that this is a tricky area. However, a feature that
all will appreciate is that the package Ada.Calendar.Formatting
includes the following declarations
type Day_Name is (Monday, Tuesday, Wednesday,
Thursday, Friday, Saturday, Sunday);
function Day_Of_Week(Date: Time) return Day_Name;
There is also a small
change in the parent package Ada.Calendar
itself. The subtype Year_Number is now
subtype Year_Number is Integer range 1901 .. 2399;
This reveals confidence in the future of Ada by adding
another three hundred years to the range of dates.
© 2005, 2006 John Barnes Informatics.
Sponsored in part by: