Ada Programming/Packages
From Wikibooks, the open-content textbooks collection
Contents |
One of the biggest advantages of Ada over most other programming languages is its well defined system of modularization and separate compilation. Even though Ada allows separate compilation, it maintains the strong type checking among the various compilations by enforcing rules of compilation order and compatibility checking. Ada uses separate compilation (like Modula-2, Java and C#), and not independent compilation (as C/C++ does), in which the various parts are compiled with no knowledge of the other compilation units with which they will be combined.
A note to C/C++ users: Yes, you can use the preprocessor to emulate separate compilation — but it is only an emulation and the smallest mistake leads to very hard to find bugs. It is telling that all C/C++ successor languages including D have turned away from the independent compilation and the use of the preprocessor.
So it's good to know that Ada has had separate compilation ever since Ada-83 and is probably the most sophisticated implementation around.
The following is a quote from the current Ada Reference Manual Section 7: Packages. RM 7(1) (Annotated)
Packages are program units that allow the specification of groups of logically related entities. Typically, a package contains the declaration of a type (often a private type or private extension) along with the declaration of primitive subprograms of the type, which can be called from outside the package, while their inner workings remain hidden from outside users.
[edit] Parts of a package
A package generally consists of two parts, the specification and the body. A package specification can be further divided in two logical parts, the visible part and the private part. Only the visible part of the specification is mandatory: you can have package specification without package body and the private part of the specification is optional.
[edit] The package specification — the visible part
The visible part of a package specification describes all the subprogram specifications, variables, types, constants etc that are visible to anyone who wishes to use the package.
package Public_Only_Package is type Range_10 is range 1 .. 10; end Public_Only_Package;
[edit] The private part
The private part of a package serves two purposes:
- To complete the deferred definition of private types and constants.
- To export entities only visible to the children of the package
package Package_With_Private is type Private_Type is private; private type Private_Type is array (1 .. 10) of Integer; end Package_With_Private;
[edit] The package body
The package body defines the implementation of the package. All the subprograms defined in the specification have to be implemented in the body. New subprograms, types and objects can be defined in the body that are not visible to the users of the package.
package Package_With_Body is type Basic_Record is private; procedure Set_A (This : in out Basic_Record; An_A : in Integer); function Get_A (This : Basic_Record) return Integer; private type Basic_Record is record A : Integer; end record ; pragma Pure_Function (Get_A); pragma Inline (Get_A); pragma Inline (Set_A); end Package_With_Body;
package body Package_With_Body is procedure Set_A (This : in out Basic_Record; An_A : in Integer) is begin This.A := An_A; end Set_A; function Get_A (This : Basic_Record) return Integer is begin return This.A; end Get_A; end Package_With_Body;
- pragma Pure_Function
- Only available when using GNAT.
[edit] Two Flavors of Package
The packages above each define a type together with operations of the type. When the type's composition is placed in the private part of a package, the package then exports what is known to be an Abstract Data Type or ADT for short. Objects of the type are then constructed by calling one of the subprograms associated with the respective type.
A different kind of package is the Abstract State Machine. A package will be modeling a single item of the problem domain, such as the motor of a car. If a program controls one car, there is typically just one motor, or the motor. The public part of the package specification only declares the operations of the module (of the motor, say), but no type. All data of the module are hidden in the body of the package where they act as state variables to be queried, or manipulated by the subprograms of the package. The initialization part sets the state variables to their initial values.
package Package_With_Body is procedure Set_A (An_A : in Integer); function Get_A return Integer; private pragma Pure_Function (Get_A); end Package_With_Body;
package body Package_With_Body is The_A: Integer; procedure Set_A (An_A : in Integer) is begin The_A := An_A; end Set_A; function Get_A return Integer is begin return The_A; end Get_A; begin The_A := 0; end Package_With_Body;
(A note on construction: The package initialization part after begin corresponds to a construction subprogram of an ADT package. However, as a state machine is an “object” already, “construction” is happening during package initialization. (Here it sets the state variable The_A to its initial value.) An ASM package can be viewed as a singleton.)
[edit] Using packages
To utilize a package it's needed to name it in a with clause, whereas to have direct visibility of that package it's needed to name it in a use clause.
For C++ programmers, Ada's with clause is analogous to the C++ preprocessor's #include and Ada's use is similar to the using namespace statement in C++. In particular, use leads to the same namespace pollution problems as using namespace and thus should be used sparingly. Renaming can shorten long compound names to a manageable length, while the use type clause makes a type's operators visible. These features reduce the need for plain use.
[edit] Standard with
The standard with clause provides visibility for the public part of a unit to the following defined unit. The imported package can be used in any part of the defined unit, including the body when the clause is used in the specification.
[edit] Private with
This language feature is only available in Ada 2005 standard.
private with Ada.Strings.Unbounded; package Private_With is -- The package Ada.String.Unbounded is not visible at this point type Basic_Record is private; procedure Set_A (This : in out Basic_Record; An_A : in String); function Get_A (This : Basic_Record) return String; private -- The visibility of package Ada.String.Unbounded starts here package Unbounded renames Ada.Strings.Unbounded; type Basic_Record is record A : Unbounded.Unbounded_String; end record; pragma Pure_Function (Get_A); pragma Inline (Get_A); pragma Inline (Set_A); end Private_With;
package body Private_With is -- The private withed package is visible in the body too procedure Set_A (This : in out Basic_Record; An_A : in String) is begin This.A := Unbounded.To_Unbounded_String (An_A); end Set_A; function Get_A (This : Basic_Record) return String is begin return Unbounded.To_String (This.A); end Get_A; end Private_With;
[edit] Limited with
This language feature is only available in Ada 2005 standard.
limited with Departments; package Employees is type Employee is tagged private; procedure Assign_Employee (E : in out Employee; D : access Departments.Department'Class); type Dept_Ptr is access all Departments.Department'Class; function Current_Department(E : in Employee) return Dept_Ptr; ... end Employees;
limited with Employees; package Departments is type Department is tagged private; procedure Choose_Manager (Dept : in out Department; Manager : access Employees.Employee'Class); ... end Departments;
[edit] Making operators visible
Suppose you have a package Universe that defines some numeric type T.
with Universe; procedure P is V: Universe.T := 10.0; begin V := V * 42.0; -- illegal end P;
This program fragment is illegal since the operators implicitly defined in Universe are not directly visible.
You have four choices to make the program legal.
Use a use_package_clause. This makes all declarations in Universe directly visible (provided they are not hidden because of other homographs).
with Universe; use Universe; procedure P is V: Universe.T := 10.0; begin V := V * 42.0; end P;
Use renaming. This is error prone since if you rename many operators, cut and paste errors are probable.
with Universe; procedure P is function "*" (Left, Right: Universe.T) return Universe.T renames Universe."*"; function "/" (Left, Right: Universe.T) return Universe.T renames Universe."*"; -- oops V: Universe.T := 10.0; begin V := V * 42.0; end P;
Use qualification. This is extremly ugly and unreadable.
with Universe; procedure P is V: Universe.T := 10.0; begin V := Universe."*" (V, 42.0); end P;
Use the use_type_clause. This makes only the operators in Universe directly visible.
with Universe; procedure P is V: Universe.T := 10.0; use type Universe.T; begin V := V * 42.0; end P;
There is a special beauty in the use_type_clause. Suppose you have a set of packages like so:
with Universe; package Pack is subtype T is Universe.T; end Pack;
with Pack; procedure P is V: Pack.T := 10.0; begin V := V * 42.0; -- illegal end P;
Now you've got into trouble. Since Universe is not made visible, you cannot use a use_package_clause for Universe to make the operator directly visible, nor can you use qualification for the same reason. Also a use_package_clause for Pack does not help, since the operator is not defined in Pack. The effect of the above construct means that the operator is not namable, i.e. it cannot be renamed in a renaming statement.
Of course you can add Universe to the context clause, but this may be impossible due to some other reasons (e.g. coding standards); also adding the operators to Pack may be forbidden or not feasible. So what to do?
The solution is simple. Use the use_type_clause for Pack.T and all is well!
with Pack; procedure P is V: Pack.T := 10.0; use type Pack.T; begin V := V * 42.0; end P;
[edit] Package organisation
[edit] Nested packages
A nested package is a package declared inside a package. Like a normal package, it has a public part and a private part. From outside, items declared in a nested package N will have visibility as usual; the programmer may refer to these items using a full dotted name like P.N.X. (But not P.M.Y.)
package P is D: Integer; -- a nested package: package N is X: Integer; private Foo: Integer; end N; E: Integer; private -- another nested package: package M is Y: Integer; private Bar: Integer; end M; end P;
Inside a package, declarations become visible as they are introduced, in textual order. That is, a nested package N that is declared after some other declaration D can refer to this declaration D. A declaration E following N can refer to items of N[1]. But neither can “look ahead” and refer to any declaration that goes after them. For example, spec N above cannot refer to M in any way.
In the following example, a type is derived in both of the two nested packages Disks and Books. Notice that the full declaration of parent type Item appears before the two nested packges.
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded; package Shelf is pragma Elaborate_Body; -- things to put on the shelf type ID is range 1_000 .. 9_999; type Item (Identifier : ID) is abstract tagged limited null record; type Item_Ref is access constant Item'class; function Next_ID return ID; -- a fresh ID for an Item to Put on the shelf package Disks is type Music is ( Jazz, Rock, Raga, Classic, Pop, Soul); type Disk (Style : Music; Identifier : ID) is new Item (Identifier) with record Artist : Unbounded_String; Title : Unbounded_String; end record; end Disks; package Books is type Literature is ( Play, Novel, Poem, Story, Text, Art); type Book (Kind : Literature; Identifier : ID) is new Item (Identifier) with record Authors : Unbounded_String; Title : Unbounded_String; Year : Integer; end record; end Books; -- shelf manipulation procedure Put (it: Item_Ref); function Get (identifier : ID) return Item_Ref; function Search (title : String) return ID; private -- keeping private things private package Boxes is type Treasure(Identifier: ID) is limited private; private type Treasure(Identifier: ID) is new Item(Identifier) with null record; end Boxes; end Shelf;
A package may also be nested inside a subprogram. In fact, packages can be declared in any declarative part, including those of a block.
[edit] Child packages
Ada allows one to extend the functionality of a unit (package) with so-called children (child packages). With certain exceptions, all the functionality of the parent is available to a child. This means that all public and private declarations of the parent package are visible to all child packages.
The above example, reworked as a hierarchy of packages, looks like this. Notice that the package Ada.Strings.Unbounded is not needed by the top level package Shelf, hence its with clause doesn't appear here. (We have added a match function for searching a shelf, though):
package Shelf is pragma Elaborate_Body; type ID is range 1_000 .. 9_999; type Item (Identifier : ID) is abstract tagged limited null record; type Item_Ref is access constant Item'Class; function Next_ID return ID; -- a fresh ID for an Item to Put on the shelf function match (it : Item; Text : String) return Boolean is abstract; -- see whether It has bibliographic information matching Text -- shelf manipulation procedure Put (it: Item_Ref); function Get (identifier : ID) return Item_Ref; function Search (title : String) return ID; end Shelf;
The name of a child package consists of the parent unit's name followed by the child package's identifier, separated by a period (dot) `.'.
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded; package Shelf.Books is type Literature is ( Play, Novel, Poem, Story, Text, Art); type Book (Kind : Literature; Identifier : ID) is new Item (Identifier) with record Authors : Unbounded_String; Title : Unbounded_String; Year : Integer; end record; function match(it: Book; text: String) return Boolean; end Shelf.Books;
Book has two components of type Unbounded_String, so Ada.Strings