comp.lang.ada
 help / color / mirror / Atom feed
* A question re meaning/use of the "for ... use ..."
@ 2004-12-05 15:27 Erik J Pessers
  2004-12-05 15:47 ` Martin Krischik
  2004-12-05 15:59 ` Stephen Leake
  0 siblings, 2 replies; 23+ messages in thread
From: Erik J Pessers @ 2004-12-05 15:27 UTC (permalink / raw)


Hi,

A question re meaning/use of the "for ... use ..." construct.

Given the code snippet at the bottom of this mail, I was sort of
expecting to see an output reading like:

	Pos for AA : 0
	Pos for BB : 2
	Pos for CC : 3



Instead (when compiling with bot Gnat3.15p and Gnat-3.4.2-2),
the actual code output reads:

	Pos for AA : 0
	Pos for BB : 1
	Pos for CC : 2


Anybody who can shed some light, and explain what will be
going on behind the curtains?

TIA, Erik Pessers


===
   with Ada.Text_IO ;

   procedure t is

      type enm is (aa, bb, cc) ;
      for enm use
      	(aa     => 0,
       	bb     => 2,
       	cc     => 3);

   begin
      for i in enm'Range loop
       	Ada.Text_IO.Put_Line ("Pos for " & enm'Image(i) & " :" &
				Integer'Image(enm'Pos(i))) ;
      end loop ;
   end t ;



^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: A question re meaning/use of the "for ... use ..."
  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
  1 sibling, 0 replies; 23+ messages in thread
From: Martin Krischik @ 2004-12-05 15:47 UTC (permalink / raw)


Erik J Pessers wrote:

> Hi,
> 
> A question re meaning/use of the "for ... use ..." construct.
> 
> Given the code snippet at the bottom of this mail, I was sort of
> expecting to see an output reading like:
> 
> Pos for AA : 0
> Pos for BB : 2
> Pos for CC : 3

That is what I would like as well. However the 'Pos( Attribute is defined
differently by the standard.

> Instead (when compiling with bot Gnat3.15p and Gnat-3.4.2-2),
> the actual code output reads:
> 
> Pos for AA : 0
> Pos for BB : 1
> Pos for CC : 2
> 
> 
> Anybody who can shed some light, and explain what will be
> going on behind the curtains?

AFAIK the representation clause sets the internal representation which you
can access with Unchecked_Convertion or which is used for writing to files
and 'Pos will allways return the position regardless of internal
representation.

One of the very very few designs in Ada which I find stupid.

With Regards

Martin
-- 
mailto://krischik@users.sourceforge.net
http://www.ada.krischik.com



^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: A question re meaning/use of the "for ... use ..."
  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
  1 sibling, 1 reply; 23+ messages in thread
From: Stephen Leake @ 2004-12-05 15:59 UTC (permalink / raw)
  To: comp.lang.ada; +Cc: David Botton

Erik J Pessers <epessers@tiscali.be> writes:

> Hi,
> 
> A question re meaning/use of the "for ... use ..." construct.
> 
> Given the code snippet at the bottom of this mail, I was sort of
> expecting to see an output reading like:
> 
> 	Pos for AA : 0
> 	Pos for BB : 2
> 	Pos for CC : 3
> 
> 
> 
> Instead (when compiling with bot Gnat3.15p and Gnat-3.4.2-2),
> the actual code output reads:
> 
> 	Pos for AA : 0
> 	Pos for BB : 1
> 	Pos for CC : 2

This is definitely a FAQ, but I don't see it at AdaPower. David; the
Ada FAQ says it is maintained by the users, but I don't see any way to
submit new FAQs. Please consider this a submission.

The enumeration representation clause (see ARM 13.4) specifies the
internal representation of an enumeral. The 'Pos attribute (see ARM
Annex K) returns the position number of an enumeral. 

Position numbers start at 0 and increment by one for each enumeral.

There is no standard attribute that returns the internal
representation specified by the enumeration representation clause.
However, GNAT provides the non-standard 'Enum_Rep for this purpose.

> Anybody who can shed some light, and explain what will be going on
> behind the curtains?

Typical reasons for specifying an enum rep clause are for interfacing
to another language, or for interfacing to hardware. In both cases,
the value to be passed is specified by the external object, and the
Ada code must match it. So for your type:

>       type enm is (aa, bb, cc) ;
>       for enm use
>       	(aa     => 0,
>        	bb     => 2,
>        	cc     => 3);

if you pass "bb" to a C program, it will get the value 2.

-- 
-- Stephe




^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: A question re meaning/use of the "for ... use ..."
  2004-12-05 15:59 ` Stephen Leake
@ 2004-12-05 16:52   ` Jeffrey Carter
  2004-12-06 19:59     ` Randy Brukardt
  0 siblings, 1 reply; 23+ messages in thread
From: Jeffrey Carter @ 2004-12-05 16:52 UTC (permalink / raw)


Stephen Leake wrote:

> There is no standard attribute that returns the internal
> representation specified by the enumeration representation clause.
> However, GNAT provides the non-standard 'Enum_Rep for this purpose.

I have no problem with 'Pos returning the abstract position number, but 
do think something like GNAT's 'Enum_Rep should be standard, along with 
a conversion the other way, equivalent to 'Val ('Enum_Val?).

For the OP, an enum rep is often used for interfacing to H/W, along with 
what is known as "change of representation". Consider a memory-mapped 
sensor. The application may interface to it via a package:

package Sensor_IF is
    type Raw_Sensor_Value is (Not_Ready, Empty, Normal, Full, Overflow);
    subtype Sensor_Value is Raw_Sensor_Value range Empty .. Overflow;

    Hardware_Failure : exception;

    function Read return Sensor_Value;
end Sensor_IF;

The H/W memory location uses 4 bits, with the following interpretation:

2#0000# = Not ready
2#0001# = Empty
2#0010# = Normal
2#0100# = Full
2#1000# = Overflow

So the implementation of the package might contain something like:

package body Sensor_IF is
    type HW_Value is new Raw_Sensor_Value;
    for HW_Value use (Not_Ready => 2#0000#,
                      Empty     => 2#0001#,
                      Normal    => 2#0010#,
                      Full      => 2#0100#,
                      Overflow  => 2#1000#);

    Register_Address : constant System.Address := ...;

    -- This is a pedagogical example and ignores many of the complexities
    -- of real systems of this type, which have to deal with values like
    -- 2#0101# and MSBs that have to be masked off. Often readings have
    -- to be triggered by writing to a control register, and there are
    -- timing issues ...

    Register : HW_Value;
    for Register'Address use Register_Address;
    pragma Volatile (Register);

    function Read return Sensor_Value is
       Max_Tries : constant := ...;

       Result : HW_Value;
    begin -- Read
       Try : for I in 1 .. Max_Tries loop
          Result := Register;

          if Result /= Not_Ready then
             return Sensor_Value (Result);
          end if;
       end loop Try;

       raise Hardware_Failure;
    end Read;
end Sensor_IF;

-- 
Jeff Carter
"We burst our pimples at you."
Monty Python & the Holy Grail
16




^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: A question re meaning/use of the "for ... use ..."
  2004-12-05 16:52   ` Jeffrey Carter
@ 2004-12-06 19:59     ` Randy Brukardt
  2004-12-07  1:36       ` Jeffrey Carter
  2004-12-08  3:18       ` Keith Thompson
  0 siblings, 2 replies; 23+ messages in thread
From: Randy Brukardt @ 2004-12-06 19:59 UTC (permalink / raw)


"Jeffrey Carter" <spam@spam.com> wrote in message
news:H1Hsd.1987$yr1.256@newsread3.news.pas.earthlink.net...
> Stephen Leake wrote:
>
> > There is no standard attribute that returns the internal
> > representation specified by the enumeration representation clause.
> > However, GNAT provides the non-standard 'Enum_Rep for this purpose.
>
> I have no problem with 'Pos returning the abstract position number, but
> do think something like GNAT's 'Enum_Rep should be standard, along with
> a conversion the other way, equivalent to 'Val ('Enum_Val?).

The ARG discussed this long ago, and concluded that such a facility isn't
needed. That's because Unchecked_Conversion provides the needed support.
Indeed, this is one of the few cases where the result of
Unchecked_Conversion is defined by the language (using it in this way will
work on all Ada compilers). There was some discussion about syntax guides
that prohibit the use of Unchecked_Conversion, but there is a lot of
discomfort about changing the language just because some people's style
guides are broken...

                Randy.






^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: A question re meaning/use of the "for ... use ..."
  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  3:18       ` Keith Thompson
  1 sibling, 2 replies; 23+ messages in thread
From: Jeffrey Carter @ 2004-12-07  1:36 UTC (permalink / raw)


Randy Brukardt wrote:

> The ARG discussed this long ago, and concluded that such a facility isn't
> needed. That's because Unchecked_Conversion provides the needed support.
> Indeed, this is one of the few cases where the result of
> Unchecked_Conversion is defined by the language (using it in this way will
> work on all Ada compilers). There was some discussion about syntax guides
> that prohibit the use of Unchecked_Conversion, but there is a lot of
> discomfort about changing the language just because some people's style
> guides are broken...

I know the ARG has considered and discarded the idea, but that doesn't 
mean I have to agree with them.

This is such a common stumbling block that it deserves the extra space 
that having 2 distinct attributes ensures. The OP would never had posted 
his question had both attributes been in the ARM.

Over all, I don't feel very strongly about this issue. I haven't needed 
access to the underlying representation very often, even when it's 
non-standard.

I'm more interested in areas of inconsistency. Why is exit the only 
transfer of control with a when clause?

-- 
Jeff Carter
"Mr. President, we must not allow a mine-shaft gap!"
Dr. Strangelove
33




^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: A question re meaning/use of the "for ... use ..."
  2004-12-07  1:36       ` Jeffrey Carter
@ 2004-12-07  2:40         ` David C. Hoos, Sr.
  2004-12-07 20:59         ` Randy Brukardt
  1 sibling, 0 replies; 23+ messages in thread
From: David C. Hoos, Sr. @ 2004-12-07  2:40 UTC (permalink / raw)
  Cc: comp.lang.ada

Is a case statement not a "transfer of control?"

Are the exception choices of exception handler(s)
not a "transfer of control?"

----- Original Message ----- 
From: "Jeffrey Carter" <spam@spam.com>
Newsgroups: comp.lang.ada
To: <comp.lang.ada@ada-france.org>
Sent: December 06, 2004 7:36 PM
Subject: Re: A question re meaning/use of the "for ... use ..."


> Randy Brukardt wrote:
> 
>> The ARG discussed this long ago, and concluded that such a facility isn't
>> needed. That's because Unchecked_Conversion provides the needed support.
>> Indeed, this is one of the few cases where the result of
>> Unchecked_Conversion is defined by the language (using it in this way will
>> work on all Ada compilers). There was some discussion about syntax guides
>> that prohibit the use of Unchecked_Conversion, but there is a lot of
>> discomfort about changing the language just because some people's style
>> guides are broken...
> 
> I know the ARG has considered and discarded the idea, but that doesn't 
> mean I have to agree with them.
> 
> This is such a common stumbling block that it deserves the extra space 
> that having 2 distinct attributes ensures. The OP would never had posted 
> his question had both attributes been in the ARM.
> 
> Over all, I don't feel very strongly about this issue. I haven't needed 
> access to the underlying representation very often, even when it's 
> non-standard.
> 
> I'm more interested in areas of inconsistency. Why is exit the only 
> transfer of control with a when clause?
> 
> -- 
> Jeff Carter
> "Mr. President, we must not allow a mine-shaft gap!"
> Dr. Strangelove
> 33
> 
> _______________________________________________
> comp.lang.ada mailing list
> comp.lang.ada@ada-france.org
> http://www.ada-france.org/mailman/listinfo/comp.lang.ada
> 
>



^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: A question re meaning/use of the "for ... use ..."
  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
  1 sibling, 2 replies; 23+ messages in thread
From: Randy Brukardt @ 2004-12-07 20:59 UTC (permalink / raw)


"Jeffrey Carter" <spam@spam.com> wrote in message
news:vO7td.3201$0r.388@newsread1.news.pas.earthlink.net...
> I'm more interested in areas of inconsistency. Why is exit the only
> transfer of control with a when clause?

That was proposed early in the Ada 9x process (that is, "when expr" on
returns, raise, etc.). It eventually got dropped as not important enough
when the scope of Ada 9x was reduced.

Generally, we haven't looked at "nice to haves" or things that got dropped
from Ada 9x (conditional expressions is another thing that comes to mind).
There wasn't exactly a groundswell of comments in those areas, either.

                     Randy.






^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: A question re meaning/use of the "for ... use ..."
  2004-12-07 20:59         ` Randy Brukardt
@ 2004-12-08  1:41           ` Jeffrey Carter
  2004-12-08  8:40           ` Martin Dowie
  1 sibling, 0 replies; 23+ messages in thread
From: Jeffrey Carter @ 2004-12-08  1:41 UTC (permalink / raw)


Randy Brukardt wrote:

> That was proposed early in the Ada 9x process (that is, "when expr" on
> returns, raise, etc.). It eventually got dropped as not important enough
> when the scope of Ada 9x was reduced.

As one of those who submitted such a proposal, I'm well aware of this.

> Generally, we haven't looked at "nice to haves" or things that got dropped
> from Ada 9x (conditional expressions is another thing that comes to mind).
> There wasn't exactly a groundswell of comments in those areas, either.

I don't consider conditional expressions as promoting the consistency of 
the language.

-- 
Jeff Carter
"Brave Sir Robin ran away."
Monty Python and the Holy Grail
59



^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: A question re meaning/use of the "for ... use ..."
  2004-12-06 19:59     ` Randy Brukardt
  2004-12-07  1:36       ` Jeffrey Carter
@ 2004-12-08  3:18       ` Keith Thompson
  2004-12-08 13:48         ` David C. Hoos
  2004-12-08 19:50         ` Randy Brukardt
  1 sibling, 2 replies; 23+ messages in thread
From: Keith Thompson @ 2004-12-08  3:18 UTC (permalink / raw)


"Randy Brukardt" <randy@rrsoftware.com> writes:
> "Jeffrey Carter" <spam@spam.com> wrote in message
> news:H1Hsd.1987$yr1.256@newsread3.news.pas.earthlink.net...
>> Stephen Leake wrote:
>>
>> > There is no standard attribute that returns the internal
>> > representation specified by the enumeration representation clause.
>> > However, GNAT provides the non-standard 'Enum_Rep for this purpose.
>>
>> I have no problem with 'Pos returning the abstract position number, but
>> do think something like GNAT's 'Enum_Rep should be standard, along with
>> a conversion the other way, equivalent to 'Val ('Enum_Val?).
>
> The ARG discussed this long ago, and concluded that such a facility isn't
> needed. That's because Unchecked_Conversion provides the needed support.
> Indeed, this is one of the few cases where the result of
> Unchecked_Conversion is defined by the language (using it in this way will
> work on all Ada compilers). There was some discussion about syntax guides
> that prohibit the use of Unchecked_Conversion, but there is a lot of
> discomfort about changing the language just because some people's style
> guides are broken...

How do you portably choose the target type for the Unchecked_Conversion?

The 'Pos attribute returns a result of type universal_integer; there's
no way to make an Unchecked_Conversion return a universal_integer.

-- 
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.



^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: A question re meaning/use of the "for ... use ..."
  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
  1 sibling, 1 reply; 23+ messages in thread
From: Martin Dowie @ 2004-12-08  8:40 UTC (permalink / raw)


Randy Brukardt wrote:
> That was proposed early in the Ada 9x process (that is, "when expr" on
> returns, raise, etc.). It eventually got dropped as not important
> enough when the scope of Ada 9x was reduced.
>
> Generally, we haven't looked at "nice to haves" or things that got
> dropped from Ada 9x (conditional expressions is another thing that
> comes to mind). There wasn't exactly a groundswell of comments in
> those areas, either.

Clearly this is a lot less important than extra libraries, interfaces, etc.
but I have wondered this before myself and I think it would improve program
readability quite a bit.

One for Ada201Z perhaps! :-)






^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: A question re meaning/use of the "for ... use ..."
  2004-12-08  3:18       ` Keith Thompson
@ 2004-12-08 13:48         ` David C. Hoos
  2004-12-08 19:50         ` Randy Brukardt
  1 sibling, 0 replies; 23+ messages in thread
From: David C. Hoos @ 2004-12-08 13:48 UTC (permalink / raw)
  To: Keith Thompson; +Cc: comp.lang.ada@ada.eu.org

The Unchecked_Conversion is not between the 'Pos attribute and
the representation, but rather between the enumeration type and its
representation.

All that is necessary for portability is to constrain the 'Size of both
the enumeration type, and the integer type of its representation to
have identical sizes.

----- Original Message ----- 
From: "Keith Thompson" <kst-u@mib.org>
Newsgroups: comp.lang.ada
To: <comp.lang.ada@ada-france.org>
Sent: Tuesday, December 07, 2004 9:18 PM
Subject: Re: A question re meaning/use of the "for ... use ..."


> "Randy Brukardt" <randy@rrsoftware.com> writes:
> > "Jeffrey Carter" <spam@spam.com> wrote in message
> > news:H1Hsd.1987$yr1.256@newsread3.news.pas.earthlink.net...
> >> Stephen Leake wrote:
> >>
> >> > There is no standard attribute that returns the internal
> >> > representation specified by the enumeration representation clause.
> >> > However, GNAT provides the non-standard 'Enum_Rep for this purpose.
> >>
> >> I have no problem with 'Pos returning the abstract position number, but
> >> do think something like GNAT's 'Enum_Rep should be standard, along with
> >> a conversion the other way, equivalent to 'Val ('Enum_Val?).
> >
> > The ARG discussed this long ago, and concluded that such a facility
isn't
> > needed. That's because Unchecked_Conversion provides the needed support.
> > Indeed, this is one of the few cases where the result of
> > Unchecked_Conversion is defined by the language (using it in this way
will
> > work on all Ada compilers). There was some discussion about syntax
guides
> > that prohibit the use of Unchecked_Conversion, but there is a lot of
> > discomfort about changing the language just because some people's style
> > guides are broken...
>
> How do you portably choose the target type for the Unchecked_Conversion?
>
> The 'Pos attribute returns a result of type universal_integer; there's
> no way to make an Unchecked_Conversion return a universal_integer.
>
> -- 
> 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
>




^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: A question re meaning/use of the "for ... use ..."
  2004-12-08  8:40           ` Martin Dowie
@ 2004-12-08 16:23             ` Georg Bauhaus
  0 siblings, 0 replies; 23+ messages in thread
From: Georg Bauhaus @ 2004-12-08 16:23 UTC (permalink / raw)


Martin Dowie <martin.dowie@baesystems.com> wrote:
: Clearly this is a lot less important than extra libraries, interfaces, etc.
: but I have wondered this before myself and I think it would improve program
: readability quite a bit.

As for "when expression" everywhere, I do not think so.

In
  exit when expression

it is quite clear what is going to happen when expression is true.

In
  return expression when expression

the second expression might be off the radar when you read
  return expression.
In fact, I've used this kind of syntactical arrangement a lot in Perl
programs and was told not to do so. I think this was good advice.
Reading "return E when E" requires a lot of "conditional shuffling"
and "reading back". Worse, you could write

  if expression then
    return expression when expression

ending up with a well hidden multiple exit problem due to TIMTOWTDI.
(I hope I have not repeated the Ada comments arguments.)


-- Georg



^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: A question re meaning/use of the "for ... use ..."
  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
  1 sibling, 1 reply; 23+ messages in thread
From: Randy Brukardt @ 2004-12-08 19:50 UTC (permalink / raw)


"Keith Thompson" <kst-u@mib.org> wrote in message
news:ln8y8932ei.fsf@nuthaus.mib.org...
> "Randy Brukardt" <randy@rrsoftware.com> writes:
> > "Jeffrey Carter" <spam@spam.com> wrote in message
> > news:H1Hsd.1987$yr1.256@newsread3.news.pas.earthlink.net...
> >> Stephen Leake wrote:
> >>
> >> > There is no standard attribute that returns the internal
> >> > representation specified by the enumeration representation clause.
> >> > However, GNAT provides the non-standard 'Enum_Rep for this purpose.
> >>
> >> I have no problem with 'Pos returning the abstract position number, but
> >> do think something like GNAT's 'Enum_Rep should be standard, along with
> >> a conversion the other way, equivalent to 'Val ('Enum_Val?).
> >
> > The ARG discussed this long ago, and concluded that such a facility
isn't
> > needed. That's because Unchecked_Conversion provides the needed support.
> > Indeed, this is one of the few cases where the result of
> > Unchecked_Conversion is defined by the language (using it in this way
will
> > work on all Ada compilers). There was some discussion about syntax
guides
> > that prohibit the use of Unchecked_Conversion, but there is a lot of
> > discomfort about changing the language just because some people's style
> > guides are broken...
>
> How do you portably choose the target type for the Unchecked_Conversion?
>
> The 'Pos attribute returns a result of type universal_integer; there's
> no way to make an Unchecked_Conversion return a universal_integer.

You have to declare a type for that purpose, but otherwise there is no
problem:

    First_Rep : constant := <rep-of-First>;
    Last_Rep : constant := <rep-of-Last>;
    type Enum is (First, ...., Last);
    for Enum use (First => First_Rep, .... Last => Last_Rep);
    type Enum_Rep is range First_Rep .. Last_Rep;
    for Enum_Rep'Size use Enum'Size;
    function To_Rep is new Ada.Unchecked_Conversion (Enum, Enum_Rep);
    function From_Rep is new Ada.Unchecked_Conversion (Enum_Rep, Enum);

All of this is portable, and required for any Annex C compliant compiler.
The only loss here is writing a bit of extra text (these declarations, and
type conversions on the results of the functions). This is a rare enough
need that that doesn't seem too bad.

                  Randy.






^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: A question re meaning/use of the "for ... use ..."
  2004-12-08 19:50         ` Randy Brukardt
@ 2004-12-08 23:00           ` Keith Thompson
  2004-12-09 23:06             ` Randy Brukardt
  2004-12-10  3:13             ` David C. Hoos
  0 siblings, 2 replies; 23+ messages in thread
From: Keith Thompson @ 2004-12-08 23:00 UTC (permalink / raw)


"Randy Brukardt" <randy@rrsoftware.com> writes:
> "Keith Thompson" <kst-u@mib.org> wrote in message
> news:ln8y8932ei.fsf@nuthaus.mib.org...
[...]
>> How do you portably choose the target type for the Unchecked_Conversion?
>>
>> The 'Pos attribute returns a result of type universal_integer; there's
>> no way to make an Unchecked_Conversion return a universal_integer.
>
> You have to declare a type for that purpose, but otherwise there is no
> problem:
>
>     First_Rep : constant := <rep-of-First>;
>     Last_Rep : constant := <rep-of-Last>;
>     type Enum is (First, ...., Last);
>     for Enum use (First => First_Rep, .... Last => Last_Rep);
>     type Enum_Rep is range First_Rep .. Last_Rep;
>     for Enum_Rep'Size use Enum'Size;
>     function To_Rep is new Ada.Unchecked_Conversion (Enum, Enum_Rep);
>     function From_Rep is new Ada.Unchecked_Conversion (Enum_Rep, Enum);
>
> All of this is portable, and required for any Annex C compliant compiler.
> The only loss here is writing a bit of extra text (these declarations, and
> type conversions on the results of the functions). This is a rare enough
> need that that doesn't seem too bad.

That's fine if the author of the code that declares the type Enum has
also bothered to declare the type Enum_Rep (and has done so
correctly).

But suppose I'm using some third-party package that declares an
enumeration type.  I have no control over the coding of the package.
The type may or may not have a representation clause; if it doesn't,
it may in the next version.  As a client of the package, I don't have
enough information to declare the type Enum_Rep myself.

It would also be nice to have an operation like From_Rep that raises
Constraint_Error if I call it with an invalid value.

The language could have made this easier, and made enumeration
representation clauses more useful.  In my opinion, it went to far in
trying to hide representation details from the user.

-- 
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.



^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: A question re meaning/use of the "for ... use ..."
  2004-12-08 23:00           ` Keith Thompson
@ 2004-12-09 23:06             ` Randy Brukardt
  2004-12-10  2:26               ` Keith Thompson
  2004-12-10  3:13             ` David C. Hoos
  1 sibling, 1 reply; 23+ messages in thread
From: Randy Brukardt @ 2004-12-09 23:06 UTC (permalink / raw)


"Keith Thompson" <kst-u@mib.org> wrote in message
news:lnmzwoh01r.fsf@nuthaus.mib.org...
> "Randy Brukardt" <randy@rrsoftware.com> writes:
> > "Keith Thompson" <kst-u@mib.org> wrote in message
> > news:ln8y8932ei.fsf@nuthaus.mib.org...
> [...]
> >> How do you portably choose the target type for the
Unchecked_Conversion?
> >>
> >> The 'Pos attribute returns a result of type universal_integer; there's
> >> no way to make an Unchecked_Conversion return a universal_integer.
> >
> > You have to declare a type for that purpose, but otherwise there is no
> > problem:
> >
> >     First_Rep : constant := <rep-of-First>;
> >     Last_Rep : constant := <rep-of-Last>;
> >     type Enum is (First, ...., Last);
> >     for Enum use (First => First_Rep, .... Last => Last_Rep);
> >     type Enum_Rep is range First_Rep .. Last_Rep;
> >     for Enum_Rep'Size use Enum'Size;
> >     function To_Rep is new Ada.Unchecked_Conversion (Enum, Enum_Rep);
> >     function From_Rep is new Ada.Unchecked_Conversion (Enum_Rep, Enum);
> >
> > All of this is portable, and required for any Annex C compliant
compiler.
> > The only loss here is writing a bit of extra text (these declarations,
and
> > type conversions on the results of the functions). This is a rare enough
> > need that that doesn't seem too bad.
>
> That's fine if the author of the code that declares the type Enum has
> also bothered to declare the type Enum_Rep (and has done so
> correctly).
>
> But suppose I'm using some third-party package that declares an
> enumeration type.  I have no control over the coding of the package.
> The type may or may not have a representation clause; if it doesn't,
> it may in the next version.  As a client of the package, I don't have
> enough information to declare the type Enum_Rep myself.

Baloney. All you need is the 'Size value.

     type Enum_Rep_Subtype is range 0 .. 1;
     for Enum_Rep'Size use Enum'Size;
     function To_Rep is new Ada.Unchecked_Conversion (Enum, Enum_Rep'Base);
     function From_Rep is new Ada.Unchecked_Conversion (Enum_Rep'Base,
Enum);

You do lose any range checking, but that's relatively unimportant.

> It would also be nice to have an operation like From_Rep that raises
> Constraint_Error if I call it with an invalid value.

You've got 'Valid for that purpose. The ARG has made it clear that a program
that uses Unchecked_Conversion to create a value that is immediately checked
with 'Valid is not erroneous, no matter what the value is. (The RM is not
clear on this point.) And certainly you need to check for problems if there
is a possibility of one.

                   Randy.

P.S. Thanks for reminding me about 'Valid; I need to use it to fix a bug
that I ran across yesterday, and I'd forgotten how...








^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: A question re meaning/use of the "for ... use ..."
  2004-12-09 23:06             ` Randy Brukardt
@ 2004-12-10  2:26               ` Keith Thompson
  2004-12-10 19:42                 ` Randy Brukardt
  0 siblings, 1 reply; 23+ messages in thread
From: Keith Thompson @ 2004-12-10  2:26 UTC (permalink / raw)


"Randy Brukardt" <randy@rrsoftware.com> writes:
> "Keith Thompson" <kst-u@mib.org> wrote in message
> news:lnmzwoh01r.fsf@nuthaus.mib.org...
[...]
>> But suppose I'm using some third-party package that declares an
>> enumeration type.  I have no control over the coding of the package.
>> The type may or may not have a representation clause; if it doesn't,
>> it may in the next version.  As a client of the package, I don't have
>> enough information to declare the type Enum_Rep myself.
>
> Baloney. All you need is the 'Size value.
>
>      type Enum_Rep_Subtype is range 0 .. 1;
>      for Enum_Rep'Size use Enum'Size;
>      function To_Rep is new Ada.Unchecked_Conversion (Enum, Enum_Rep'Base);
>      function From_Rep is new Ada.Unchecked_Conversion (Enum_Rep'Base,
> Enum);
>
> You do lose any range checking, but that's relatively unimportant.

I presume your "type Enum_Rep_Subtype" should be "type Enum_rep".

Here's an example where it doesn't work:

with Ada.Unchecked_Conversion;
with Ada.Text_IO; use Ada.Text_IO;
procedure Foo is
    
    type Enum is (Zeros, Ones);
    for Enum use (Zeros => 0, Ones => 255);

    type Enum_Rep is range 0 .. 1;
    for Enum_Rep'Size use Enum'Size;
    function To_Rep is new Ada.Unchecked_Conversion (Enum, Enum_Rep'Base);
    function From_Rep is new Ada.Unchecked_Conversion (Enum_Rep'Base, Enum);

begin
    Put_Line("To_Rep(Zeros) = " & Enum_Rep'Image(To_Rep(Zeros)));
    Put_Line("To_Rep(Ones) = " & Enum_Rep'Image(To_Rep(Ones)));
end Foo;

The output (GNAT, gcc 3.3.3, x86 Linux) is:

To_Rep(Zeros) =  0
To_Rep(Ones) = -1

If I change the "Ones => 255" to "Ones => 127", I get the correct
output, but GNAT warns:

foo.adb:10:05: warning: types for unchecked conversion have different sizes
foo.adb:11:05: warning: types for unchecked conversion have different sizes

-- 
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.



^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: A question re meaning/use of the "for ... use ..."
  2004-12-08 23:00           ` Keith Thompson
  2004-12-09 23:06             ` Randy Brukardt
@ 2004-12-10  3:13             ` David C. Hoos
  2004-12-10  9:23               ` Keith Thompson
  1 sibling, 1 reply; 23+ messages in thread
From: David C. Hoos @ 2004-12-10  3:13 UTC (permalink / raw)
  To: Keith Thompson; +Cc: comp.lang.ada@ada.eu.org

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;

Program output:

Sparse_Enum representations:

ONE =>  1
TEN =>  10
ONE_HUNDRED =>  100
ONE_THOUSAND =>  1000


Attempts to convert possible representations to Sparse_Enum.

Exception name: CONSTRAINT_ERROR
Message:  0 is an invalid representation of type Enums.Sparse_Enum.
Call stack traceback locations:
0x8049ecc 0x804a0c3 0x8049d7a 0x42015502

 1 => ONE

Exception name: CONSTRAINT_ERROR
Message:  2 is an invalid representation of type Enums.Sparse_Enum.
Call stack traceback locations:
0x8049ecc 0x804a0c3 0x8049d7a 0x42015502

Exception name: CONSTRAINT_ERROR
Message:  3 is an invalid representation of type Enums.Sparse_Enum.
Call stack traceback locations:
0x8049ecc 0x804a0c3 0x8049d7a 0x42015502

Exception name: CONSTRAINT_ERROR
Message:  4 is an invalid representation of type Enums.Sparse_Enum.
Call stack traceback locations:
0x8049ecc 0x804a0c3 0x8049d7a 0x42015502

Exception name: CONSTRAINT_ERROR
Message:  5 is an invalid representation of type Enums.Sparse_Enum.
Call stack traceback locations:
0x8049ecc 0x804a0c3 0x8049d7a 0x42015502

Exception name: CONSTRAINT_ERROR
Message:  6 is an invalid representation of type Enums.Sparse_Enum.
Call stack traceback locations:
0x8049ecc 0x804a0c3 0x8049d7a 0x42015502

Exception name: CONSTRAINT_ERROR
Message:  7 is an invalid representation of type Enums.Sparse_Enum.
Call stack traceback locations:
0x8049ecc 0x804a0c3 0x8049d7a 0x42015502

Exception name: CONSTRAINT_ERROR
Message:  8 is an invalid representation of type Enums.Sparse_Enum.
Call stack traceback locations:
0x8049ecc 0x804a0c3 0x8049d7a 0x42015502

Exception name: CONSTRAINT_ERROR
Message:  9 is an invalid representation of type Enums.Sparse_Enum.
Call stack traceback locations:
0x8049ecc 0x804a0c3 0x8049d7a 0x42015502

 10 => TEN

Exception name: CONSTRAINT_ERROR
Message:  11 is an invalid representation of type Enums.Sparse_Enum.
Call stack traceback locations:
0x8049ecc 0x804a0c3 0x8049d7a 0x42015502

Exception name: CONSTRAINT_ERROR
Message:  12 is an invalid representation of type Enums.Sparse_Enum.
Call stack traceback locations:
0x8049ecc 0x804a0c3 0x8049d7a 0x42015502

Process run finished


----- Original Message ----- 
From: "Keith Thompson" <kst-u@mib.org>
Newsgroups: comp.lang.ada
To: <comp.lang.ada@ada-france.org>
Sent: Wednesday, December 08, 2004 5:00 PM
Subject: Re: A question re meaning/use of the "for ... use ..."


> "Randy Brukardt" <randy@rrsoftware.com> writes:
> > "Keith Thompson" <kst-u@mib.org> wrote in message
> > news:ln8y8932ei.fsf@nuthaus.mib.org...
> [...]
> >> How do you portably choose the target type for the
Unchecked_Conversion?
> >>
> >> The 'Pos attribute returns a result of type universal_integer; there's
> >> no way to make an Unchecked_Conversion return a universal_integer.
> >
> > You have to declare a type for that purpose, but otherwise there is no
> > problem:
> >
> >     First_Rep : constant := <rep-of-First>;
> >     Last_Rep : constant := <rep-of-Last>;
> >     type Enum is (First, ...., Last);
> >     for Enum use (First => First_Rep, .... Last => Last_Rep);
> >     type Enum_Rep is range First_Rep .. Last_Rep;
> >     for Enum_Rep'Size use Enum'Size;
> >     function To_Rep is new Ada.Unchecked_Conversion (Enum, Enum_Rep);
> >     function From_Rep is new Ada.Unchecked_Conversion (Enum_Rep, Enum);
> >
> > All of this is portable, and required for any Annex C compliant
compiler.
> > The only loss here is writing a bit of extra text (these declarations,
and
> > type conversions on the results of the functions). This is a rare enough
> > need that that doesn't seem too bad.
>
> That's fine if the author of the code that declares the type Enum has
> also bothered to declare the type Enum_Rep (and has done so
> correctly).
>
> But suppose I'm using some third-party package that declares an
> enumeration type.  I have no control over the coding of the package.
> The type may or may not have a representation clause; if it doesn't,
> it may in the next version.  As a client of the package, I don't have
> enough information to declare the type Enum_Rep myself.
>
> It would also be nice to have an operation like From_Rep that raises
> Constraint_Error if I call it with an invalid value.
>
> The language could have made this easier, and made enumeration
> representation clauses more useful.  In my opinion, it went to far in
> trying to hide representation details from the user.
>
> -- 
> 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
>




^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: A question re meaning/use of the "for ... use ..."
  2004-12-10  3:13             ` David C. Hoos
@ 2004-12-10  9:23               ` Keith Thompson
  2004-12-10 13:24                 ` David C. Hoos
  0 siblings, 1 reply; 23+ messages in thread
From: Keith Thompson @ 2004-12-10  9:23 UTC (permalink / raw)


"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.



^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: A question re meaning/use of the "for ... use ..."
  2004-12-10  9:23               ` Keith Thompson
@ 2004-12-10 13:24                 ` David C. Hoos
  0 siblings, 0 replies; 23+ messages in thread
From: David C. Hoos @ 2004-12-10 13:24 UTC (permalink / raw)
  To: Keith Thompson; +Cc: comp.lang.ada@ada.eu.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
>




^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: A question re meaning/use of the "for ... use ..."
  2004-12-10  2:26               ` Keith Thompson
@ 2004-12-10 19:42                 ` Randy Brukardt
  2004-12-10 21:18                   ` Keith Thompson
  0 siblings, 1 reply; 23+ messages in thread
From: Randy Brukardt @ 2004-12-10 19:42 UTC (permalink / raw)


"Keith Thompson" <kst-u@mib.org> wrote in message
news:ln1xdygaf2.fsf@nuthaus.mib.org...
> Here's an example where it doesn't work:
...

If you want an unsigned type, then use a modular type:
    type Enum_Rep is mod 2**Enum'Size;
    for Enum_Rep'Size use Enum'Size;

I know that you're now going to say that that doesn't work for a signed
representation, but that's a red herring. You have the know whether the
representation is signed or unsigned, even for your hypothetical From_Rep
attribute, because you have to decide on what sort of type to put it into.
Otherwise, you're at risk of raising Constraint_Error. (On a typical
implementation, there are modular values that don't fit into any signed
type, and of course negative values don't fit into any modular type.) Even a
non-static universal expression isn't safe: it can raise Constraint_Error
for modular values that aren't in any signed type. One example:

    generic
         type Something is (<>);
    package P is
         ...
    end P;
    package body P
         ...
         if Something'Pos(Something'Last) > 10 then -- (!)
         ...
    end P;

The expression marked (!) can (and will on many implementations) raise
Constraint_Error, because Something'Pos(Something'Last) is outside of the
range of Root_Integer.

So you really have to know which you are dealing with. For enumeration
representations, assuming modular is probably the best option, as you are
usually dealing with bit patterns.

But this whole issue comes from people trying to use enumeration
representations for purposes for which they are not intended. They're only
intended to be used for *external* representations, which means that the
program itself has no reason to be concerned with them. The program should
use the internal representation.

If you really need a discontiguous counting type, that's something that Ada
doesn't provide and you should program it yourself. That's a very rare need;
you really need to think carefully if you really need such a type.

The issue is very similar to Bit_Order, which only has to do with the way
bits are numbered in the program; it has nothing to do with the bit or byte
order in the external world. But people keep hoping that it would be,
because that would make someone else solve their hard problems.

                     Randy.






^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: A question re meaning/use of the "for ... use ..."
  2004-12-10 19:42                 ` Randy Brukardt
@ 2004-12-10 21:18                   ` Keith Thompson
  2004-12-11  0:53                     ` Keith Thompson
  0 siblings, 1 reply; 23+ messages in thread
From: Keith Thompson @ 2004-12-10 21:18 UTC (permalink / raw)


[...]
> But this whole issue comes from people trying to use enumeration
> representations for purposes for which they are not intended. They're only
> intended to be used for *external* representations, which means that the
> program itself has no reason to be concerned with them. The program should
> use the internal representation.
[...]

People use things for purposes for which they are not intended all the
time.  Ada quite properly makes it easiest to use things in the way
they're intended, but in this case, in my opinion, it goes too far in
preventing the programmer from doing certain things.

The 'Pos and 'Val attributes deal with the position number, and for
loops iterate over the defined values.  That's fine.  But why make the
programmer jump through so many hoops to deal with the representation
values?

The fact that GNAT has the 'Enum_Rep attribute is evidence (but not
proof) that there's a need for this kind of thing.

I've implemented enumeration representation clauses, but I've never
found them to be useful.

-- 
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.



^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: A question re meaning/use of the "for ... use ..."
  2004-12-10 21:18                   ` Keith Thompson
@ 2004-12-11  0:53                     ` Keith Thompson
  0 siblings, 0 replies; 23+ messages in thread
From: Keith Thompson @ 2004-12-11  0:53 UTC (permalink / raw)


Keith Thompson <kst-u@mib.org> writes:
> [...]
>> But this whole issue comes from people trying to use enumeration
>> representations for purposes for which they are not intended. They're only
>> intended to be used for *external* representations, which means that the
>> program itself has no reason to be concerned with them. The program should
>> use the internal representation.
> [...]
>
> People use things for purposes for which they are not intended all the
> time.  Ada quite properly makes it easiest to use things in the way
> they're intended, but in this case, in my opinion, it goes too far in
> preventing the programmer from doing certain things.

I thought of another point after I posted this.

If I specify the address of an object:

    for Obj'Address use ...;

I can query the address with the Address attribute.  Likewise for the
Size attribute.  I can even query the layout of a record using the
Position, First_Bit, and Last_Bit attributes.  Arguably the program
shouldn't be concerned with these things, but the attributes are
available.  Given this, the lack of an attribute that lets me query
the representation of an enumeration value seems odd.

-- 
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.



^ permalink raw reply	[flat|nested] 23+ messages in thread

end of thread, other threads:[~2004-12-11  0:53 UTC | newest]

Thread overview: 23+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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 is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox