comp.lang.ada
 help / color / mirror / Atom feed
From: "David C. Hoos" <david.c.hoos.sr@ada95.com>
To: "Keith Thompson" <kst-u@mib.org>
Cc: "comp.lang.ada@ada.eu.org" <comp.lang.ada@ada-france.org>
Subject: Re: A question re meaning/use of the "for ... use ..."
Date: Fri, 10 Dec 2004 07:24:37 -0600
Date: 2004-12-10T07:24:37-06:00	[thread overview]
Message-ID: <mailman.184.1102685125.10401.comp.lang.ada@ada-france.org> (raw)
In-Reply-To: lnwtvqecjk.fsf@nuthaus.mib.org

If we consider the target type for the conversion not to be some unknown
type of integer, but simply a bit pattern of length Sparse_Enum'Size, then
the solution I provided is completely portable.  The bit pattern of 11 ones
has the value -1 if interpreted as a twos-complement signed integer, or
2047 if interpreted as an unsigned integer.

As to the fact that the value -1 was skipped by my small demonstration
program, that is because to abbreviate the output for purposes of
shortening my posting, if the program had looped over the entire range of
Sparse_Enum_Rep, then all valid representations would have been
properly converted to the corresponding enumeral, and all invalid
representations would have raised Constraint_Error.

The representations of such enumeration types are usually not modeling
numbers, but some bit pattern associated with computer hardware or an
external device.

Granted, the _ordering_ of the representation (if it has any significance
to the thing being modeled) differs according to whether the bit pattern is
considered as signed or unsigned.  But the ordering (again if it has any
meaning at all) is governed by the enumeration type itself.

One lack of enumeration types that I would have liked to have been absent
from the language is the inability to make an enumeration type "modular,"
or "cyclic."
By this I mean to have the ability to declare an enumeration type such that
Enum'Succ (Enum'Last) = Enum'First, and
Enum'Pred (Enum'First) = Enum'Last.

----- Original Message ----- 
From: "Keith Thompson" <kst-u@mib.org>
Newsgroups: comp.lang.ada
To: <comp.lang.ada@ada-france.org>
Sent: Friday, December 10, 2004 3:23 AM
Subject: Re: A question re meaning/use of the "for ... use ..."


> "David C. Hoos" <david.c.hoos.sr@ada95.com> writes:
> > Is there some reason the following code is not portable"
> >
> > package Enums
> > is
> >    type Sparse_Enum is (One, Ten, One_Hundred, One_Thousand);
> >    for Sparse_Enum use
> >       (One => 1, Ten => 10, One_Hundred => 100, One_Thousand => 1000);
> > end Enums;
> >
> > with Enums;
> > with Ada.Exceptions;
> > with Ada.Text_IO;
> > with Ada.Unchecked_Conversion;
> > procedure Test_Enums
> > is
> >    type Sparse_enum_Rep is range 0 .. 2**Enums.Sparse_Enum'Size - 1;
> >
> >    function To_Rep is new Ada.Unchecked_Conversion
> >      (Source => Enums.Sparse_Enum, Target => Sparse_enum_Rep);
> >
> >    function To_Enum is new Ada.Unchecked_Conversion
> >      (Target => Enums.Sparse_Enum, Source => Sparse_enum_Rep);
> >
> >    function From_Rep (Rep : Sparse_enum_Rep) return Enums.Sparse_Enum
> >    is
> >       Enum : Enums.Sparse_Enum := To_Enum (Rep);
> >    begin
> >       if Enum'Valid then
> >          return Enum;
> >       end if;
> >       Ada.Exceptions.Raise_Exception
> >         (E => Constraint_Error'Identity,
> >          Message => Sparse_enum_Rep'Image (Rep) &
> >            " is an invalid representation of type Enums.Sparse_Enum.");
> >    end From_Rep;
> >
> > begin
> >    Ada.Text_IO.Put_Line ("Sparse_Enum representations:");
> >    Ada.Text_IO.New_Line;
> >
> >    for N in Enums.Sparse_Enum loop
> >       Ada.Text_IO.Put_Line
> >         (Enums.Sparse_Enum'Image (N) & " => " &
> >          Sparse_enum_Rep'Image (To_Rep (N)));
> >    end loop;
> >
> >    Ada.Text_IO.New_Line (2);
> >
> >    Ada.Text_IO.Put_Line
> >      ("Attempts to convert possible representations to Sparse_Enum.");
> >    Ada.Text_IO.New_Line;
> >
> >    for I in Sparse_enum_Rep'First .. Sparse_enum_Rep'First + 12 loop
> >       begin
> >          Ada.Text_IO.Put_Line
> >            (Sparse_enum_Rep'Image (I) & " => " &
> >             Enums.Sparse_Enum'Image (From_Rep (I)));
> >          Ada.Text_IO.New_Line;
> >       exception
> >          when E: others =>
> >             Ada.Text_IO.Put_Line (Ada.Exceptions.Exception_Information
(E));
> >       end;
> >    end loop;
> >
> > end Test_Enums;
>
> Yes.  Try changing the declaration of Sparse_Enum to:
>
>    type Sparse_Enum is (Minus_One, One, Ten, One_Hundred, One_Thousand);
>    for Sparse_Enum use
>       (Minus_One => -1, One => 1, Ten => 10, One_Hundred => 100,
>        One_Thousand => 1000);
>
> It shows the representation of Minus_One as 2047 rather than -1,
> and the loop runs from 0 to 12, skipping Minus_One.
>
> -- 
> Keith Thompson (The_Other_Keith) kst-u@mib.org
<http://www.ghoti.net/~kst>
> San Diego Supercomputer Center             <*>
<http://users.sdsc.edu/~kst>
> We must do something.  This is something.  Therefore, we must do this.
> _______________________________________________
> comp.lang.ada mailing list
> comp.lang.ada@ada-france.org
> http://www.ada-france.org/mailman/listinfo/comp.lang.ada
>




      reply	other threads:[~2004-12-10 13:24 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-12-05 15:27 A question re meaning/use of the "for ... use ..." Erik J Pessers
2004-12-05 15:47 ` Martin Krischik
2004-12-05 15:59 ` Stephen Leake
2004-12-05 16:52   ` Jeffrey Carter
2004-12-06 19:59     ` Randy Brukardt
2004-12-07  1:36       ` Jeffrey Carter
2004-12-07  2:40         ` David C. Hoos, Sr.
2004-12-07 20:59         ` Randy Brukardt
2004-12-08  1:41           ` Jeffrey Carter
2004-12-08  8:40           ` Martin Dowie
2004-12-08 16:23             ` Georg Bauhaus
2004-12-08  3:18       ` Keith Thompson
2004-12-08 13:48         ` David C. Hoos
2004-12-08 19:50         ` Randy Brukardt
2004-12-08 23:00           ` Keith Thompson
2004-12-09 23:06             ` Randy Brukardt
2004-12-10  2:26               ` Keith Thompson
2004-12-10 19:42                 ` Randy Brukardt
2004-12-10 21:18                   ` Keith Thompson
2004-12-11  0:53                     ` Keith Thompson
2004-12-10  3:13             ` David C. Hoos
2004-12-10  9:23               ` Keith Thompson
2004-12-10 13:24                 ` David C. Hoos [this message]
replies disabled

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