comp.lang.ada
 help / color / mirror / Atom feed
From: jgv@swl.msd.ray.com (John Volan)
Subject: Re: SOLVED! Decoupled Mutual Recursion Challenger
Date: Mon, 17 Oct 1994 15:48:12 GMT
Date: 1994-10-17T15:48:12+00:00	[thread overview]
Message-ID: <1994Oct17.154812.9104@swlvx2.msd.ray.com> (raw)
In-Reply-To: 1994Oct12.224944.25566@swlvx2.msd.ray.com

In article <1994Oct12.224944.25566@swlvx2.msd.ray.com> I (John Volan) wrote:

> Now let's go back and consider how to implement the Identity package.
> To implement the Identity.Value type, all we need is some kind of
> "black box" that is guaranteed to be the same storage size as an
> access type.  Almost anything would do, but System.Address seems the
> most convenient for this purpose.

> (Er ... I've been scouring the RM9X 5.0 and much to my surprise I can't
> seem to find positive confirmation for the fact that System.Address will
> have the same bit-representation as access types.  Tucker, if you're
> listening, can you give me a hand here?)

Actually, there wasn't any need to involve System.Address after all.
A better solution would be to just declare Identity.Value as an access
type pointing to some "dummy" designated type. 

[Thanks to Tim Coslet for pointing out this possibility to me.]

Here's an improved spec for package Identity.  The public part is
identical to my previous solution, so it still provides a reusable
way of achieving mutual recursion in a decoupled fashion:

    ----------------------------------------------------------------------

    generic
    package Identity is

      -- Identity.Value
      type Value is private; 

      -- Identity.None    
      None : constant Identity.Value;

      -- Identity.Translation
      generic
        type Object (<>) is limited private; -- matches any type
        type Pointer is access Object;
      package Translation is

        -- Identity.Translation.To_Pointer
        function To_Pointer (The_Identity : in Identity.Value) return Pointer;
        pragma Inline (To_Pointer);

        -- Identity.Translation.To_Identity
        function To_Identity (The_Pointer : in Pointer) return Identity.Value;
        pragma Inline (To_Identity);

      end Translation;
    
    private -- Identity

      type Void is null record; -- "dummy" designated type
    
      -- Identity.Value
      type Value is access Void;

      -- Identity.None      
      None : constant Identity.Value := null;

    end Identity;

    ----------------------------------------------------------------------

Implementing Identity.Value an access-to-dummy-Void-type has the
following advantages:

(1) It avoids portability problems.  It's conceivable that a given Ada
compiler might not implement System.Address the same as access types.
However, it is a safe bet that all access types will be implemented
the same way (within a given Ada implementation).

(2) It avoids coupling the Identity package to package System.

(3) It still preserves type-safety, because an Identity.Value will
never actually be used *as* an access-to-dummy-Void.  (Identity.Value
is still a "black box", after all.)

(4) The default initialization of Identity.Values to Identity.None
is implemented directly in terms of the default initialization of
access types to null.  There's no need to muck around with a record
type.

(5) The bodies of the Translation functions can be implemented
directly as instantiations of Unchecked_Conversion, since Ada9X allows
renaming declarations to act as subprogram bodies:

    ----------------------------------------------------------------------

    with Ada.Unchecked_Conversion;

    package body Identity is

      One_Translation_Registered_Already : Boolean := False;

      -----------------------
      -- Identity.Translation
      -----------------------

      package body Translation is

        function Identity_To_Pointer is new 
          Ada.Unchecked_Conversion (Identity.Value, Pointer);

        function Pointer_To_Identity is new
          Ada.Unchecked_Conversion (Pointer, Identity.Value);

        ----------------------------------
        -- Identity.Translation.To_Pointer
        ----------------------------------

        function To_Pointer (The_Identity : in Identity.Value) return Pointer
          renames Identity_To_Pointer;

        -----------------------------------
        -- Identity.Translation.To_Identity
        -----------------------------------

        function To_Identity (The_Pointer : in Pointer) return Identity.Value
          renames Pointer_To_Identity;

        -- Since To_Pointer and To_Identity are really just instantiations of
        -- Unchecked_Conversion, and since Unchecked_Conversion is intrinsic,
        -- the cost of calling these subprograms very likely will be *zero*.

      begin -- Translation
        if One_Translation_Registered_Already then
          raise Standard.Program_Error;
        else
          One_Translation_Registered_Already := True;
        end if;
      end Translation;

    end Identity;

    ----------------------------------------------------------------------

(Well, folks, it just keeps looking better and better ... :-)

--John Volan

--------------------------------------------------------------------------------
--  Me : Person := (Name                => "John Volan",
--                  Company             => "Raytheon Missile Systems Division",
--                  E_Mail_Address      => "jgv@swl.msd.ray.com",
--                  Affiliation         => "Enthusiastic member of Team Ada!",
--                  Humorous_Disclaimer => "These opinions are undefined " &
--                                         "by my employer and therefore " &
--                                         "any use of them would be "     &
--                                         "totally erroneous.");
--------------------------------------------------------------------------------





  reply	other threads:[~1994-10-17 15:48 UTC|newest]

Thread overview: 45+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1994-10-12 22:49 SOLVED! Decoupled Mutual Recursion Challenger John Volan
1994-10-17 15:48 ` John Volan [this message]
1994-10-17 17:55   ` Bob Duff
1994-10-17 20:52     ` John Volan
1994-10-17 22:10       ` Bob Duff
1994-10-18 22:17         ` John Volan
1994-10-19  1:01           ` Bob Duff
1994-10-19  4:45             ` Jay Martin
1994-10-19 14:38               ` Mark A Biggar
     [not found]                 ` <38fi4r$l81@oahu.cs.ucla.edu>
1994-10-24 11:49                   ` Mutual Recursion Challenge Robert I. Eachus
1994-10-24 20:32                     ` John Volan
1994-10-26 11:42                       ` Generic association example (was Re: Mutual Recursion Challenge) Robert I. Eachus
1994-10-26 23:21                         ` John Volan
1994-10-27 10:53                           ` Robert I. Eachus
1994-10-31 17:34                             ` John Volan
1994-10-27 14:37                           ` Mark A Biggar
1994-10-24 17:42                   ` SOLVED! Decoupled Mutual Recursion Challenger John Volan
1994-10-24 22:37                     ` Jay Martin
1994-10-25  5:47                       ` Matt Kennel
1994-10-25 10:04                         ` David Emery
1994-10-25 16:43                         ` John Volan
1994-10-27  4:25                           ` Rob Heyes
1994-10-28  9:03                             ` Mutual Recursion (was Re: SOLVED! Decoupled Mutual Recursion Challenger) Robert I. Eachus
1994-10-28 15:04                             ` SOLVED! Decoupled Mutual Recursion Challenger Robb Nebbe
1994-10-25 15:54                       ` John Volan
1994-10-26  1:24                         ` Bob Duff
1994-10-28  4:28                         ` Jay Martin
1994-10-28 10:52                           ` Robert I. Eachus
1994-10-28 18:46                             ` Jay Martin
1994-11-02 14:56                               ` Robert I. Eachus
1994-10-29  0:38                           ` Bob Duff
1994-10-29  7:26                             ` Jay Martin
1994-10-29 11:59                             ` Richard Kenner
1994-10-31 13:17                               ` Robert Dewar
1994-10-31 14:13                               ` gcc distribution (was: SOLVED! Decoupled Mutual Recursion Challenger) Norman H. Cohen
1994-11-02 14:14                                 ` Richard Kenner
1994-11-04 23:56                                   ` Michael Feldman
1994-10-31 18:44                           ` SOLVED! Decoupled Mutual Recursion Challenger John Volan
1994-10-20 11:25               ` Robb Nebbe
1994-10-20 19:19                 ` John Volan
1994-10-26  0:07                 ` Mark S. Hathaway
1994-10-26 18:48                 ` gamache
1994-10-27  2:15                   ` John Volan
     [not found]           ` <CxwGJF.FwB@ois.com>
1994-10-19 16:35             ` John Volan
1994-10-17 22:54   ` Cyrille Comar
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox