[net.lang.ada] Implementing mutually dependent access types in separate packages

geoff@boulder.UUCP (Geoffrey M. Clemm) (07/08/85)

*

Problem :
   I have a large program with data structures whose implementations
   are all mutually dependent.  For example, a "FileHeader" is a structure
   containing a field that is a pointer to a "Parameter", a "Parameter" is a
   structure containing a field that is a pointer to an "Item", and an "Item"
   is a structure containing a field that is a pointer back to a "FileHeader".
   (For a simple example, see section 3.8.1 of the ADA standard).
   
   The desireable implementation would consist of one package for each data
   structure, where a package performs the usual data abstraction principle
   of providing accessing functions while hiding the physical implementation
   of the data structure.

   In the above example, the package for "FileHeader" must provide (amoung
   other things) the type "FileHeaderPtr" (ACCESS FileHeader) and an accessing
   function "FileHeader_Parameter" that takes a FileHeaderPtr as an input
   argument and returns a ParameterPtr.  Similarly for each of the other
   packages.

   Unfortunately, I can find no way of doing this in ADA.  The obvious
   implementation generates a "USE" cycle that is illegal according to
   the language standard (not to mention being rejected by our compiler).

   The only method I can discover for doing this in ADA is to take all
   of the data structure definitions out of their respective packages
   and then clump them together into a single "implementation package".
   All the individual packages then "USE" this implementation package.

   This is very unfortunate for a variety of reasons.  It's especially
   annoying since a variety of languages earlier than ADA allow this form
   of data abstraction.

Question :
   Can anyone come up with a way of leaving the implementation of each
   data structure in its own package, while providing the desired accessing
   functions and not violating the ADA standard ?

Note:
   If one was not required to provide accessing functions of the form
   "FileHeader_Parameter(FileHeaderPtr) RETURN ParameterPtr", there of
   course would be no problem, but these accessing functions are critical.

Geoffrey Clemm

geoff@boulder.UUCP (Geoffrey M. Clemm) (07/08/85)

Note :

Mutually dependent access types can be implemented in separate generic
packages, but then must all be instantiated together in a single 
"implementation" package.  This approach shares many of the problems of
simply clumping all the mutually dependent types into a single package
(since in effect, that is what is being done).

Geoffrey Clemm

markb@sdcrdcf.UUCP (Mark Biggar) (07/11/85)

You should have read section 3.8.1 more carefully.

	If the incomplete type declaration occurs immediately within the
	private part of a package, then the full type declaration must occur
	later and immediately within either the private part itself,
	OR THE DECLARATIVE PART OF THE CORRESPONDING PACKAGE BODY.
		(3.8.1 para 3, last sentence)

This means that you can do what you want using something similar to the
following:

package A is
    type ATYPEPTR is private;
private
    type ATYPE;
    type ATYPEPTR is access ATYPE;
end A;

package B is
    type BTYPEPTR is private;
private
    type BTYPE;
    type BTYPEPTR is access BTYPE;
end B;

package C is
    type CTYPEPTR is private;
private
    type CTYPE;
    type CTYPEPTR is access CTYPE;
end C;

with C; use C;
package body A is
    type ATYPE is record
	C: CTYPEPTR;
    end record;
end A;

with A; use A;
package body B is
    type BTYPE is record
	A: ATYPEPTR;
    end record;
end B;

with B; use B;
package body C is
    type CTYPE is record
	B: BTYPEPTR;
    end record;
end C;

Note that there is no cycle in the compilation order as the package specs
can be compiled before any of the bodies.

Happy Ada hacking
Mark Biggar
{allegra,burdvax,cbosgd,hplabs,ihnp4,akgua,sdcsvax}!sdcrdcf!markb

geoff@boulder.UUCP (Geoffrey M. Clemm) (08/09/85)

A month ago I solicited suggestions on how to place the parts of a
mutually recursive set of data structures into different packages.  All
parts refer to other parts through access types, and therefore the various
record definitions should appear in the package bodies to avoid unnecessary
compilation dependencies.

In article <2142@sdcrdcf.UUCP> markb@sdcrdcf.UUCP (Mark Biggar) writes:
>You should have read section 3.8.1 more carefully.
(Mark, you should have read the posting more carefully, see below :-)
>
>[This section] says that you can do what you want using something similar
>to the following:
>
>package A is
>    type ATYPEPTR is private;
>private
>    type ATYPE;
>    type ATYPEPTR is access ATYPE;
>end A;
>
... same for B
>
>with B; use B;
>package body A is
>    type ATYPE is record
>	B: BTYPEPTR;
>    end record;
>end A;
... etc.
>
>Note that there is no cycle in the compilation order as the package specs
>can be compiled before any of the bodies.

From the original posting :
>Note:
>   If one was not required to provide accessing functions of the form
>   "ATYPE_BTYPE(ATYPEPTR) RETURN BTYPEPTR", there of
>   course would be no problem, but these accessing functions are critical.

Since I have only received two responses which both left out the critical
accessing functions, should I assume that it cannot be done properly in ADA ?

(Where "properly" is defined as 
   1. separate packages for each data structure that is a part of
      the mutually recursive set of data structures
   2. record implementations hidden inside the package bodies
)