comp.lang.ada
 help / color / mirror / Atom feed
From: "Jeffrey R. Carter" <spam.jrcarter.not@spam.not.acm.org>
Subject: Re: Recursive algebraic data types
Date: Tue, 6 Mar 2018 21:54:20 +0100
Date: 2018-03-06T21:54:20+01:00	[thread overview]
Message-ID: <p7mv5s$rig$1@dont-email.me> (raw)
In-Reply-To: <p7mtlh$s1r$1@franka.jacob-sparre.dk>

On 03/06/2018 09:28 PM, Randy Brukardt wrote:
> 
> A "recursive data type" is illegal in Ada: a type can never depend on
> itself, directly or indirectly. You have to break this sort of thing with an
> intervening access type, which brings a world of complications in storage
> management.

You have to break it with something, but that doesn't need to be an access type. 
Consider S expressions (usually shortened to sexes), which are either an atom or 
a list of zero or more sexes:

    type Sex (Is_Atom : Boolean := False) is tagged private;
private
    type Root is interface;

    package Sex_Lists is new
       Ada.Containers.Indefinite_Vectors (Positive, Root'Class);

    type Sex (Is_Atom : Boolean := False) is new Root with record
       case Is_Atom is
       when False =>
          List : Sex_Lists.Vector;
       when True =>
          Atom : Atom_Value;
       end case;
    end record;

The only downside of this is that any element you obtain from S.List has to be 
converted to Sex, which is just noise. Better would be if we could instantiate 
Indefinite_Vectors with type Sex directly, before it's full definition is seen.

-- 
Jeff Carter
"He that hath no beard is less than a man."
Much Ado About Nothing
132

  reply	other threads:[~2018-03-06 20:54 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-03-06 19:46 Recursive algebraic data types hnptz
2018-03-06 19:58 ` hnptz
2018-03-06 20:45   ` gautier_niouzes
2018-03-07  5:53     ` Niklas Holsti
2018-03-07  8:31       ` Dmitry A. Kazakov
2018-03-06 20:28 ` Randy Brukardt
2018-03-06 20:54   ` Jeffrey R. Carter [this message]
2018-03-06 21:14   ` Dmitry A. Kazakov
2018-03-06 21:25 ` Dmitry A. Kazakov
2018-03-06 21:54 ` hnptz
2018-03-07  7:58   ` Jacob Sparre Andersen
replies disabled

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