comp.lang.ada
 help / color / mirror / Atom feed
* GNAT GPL 2009 - issue with floating points?
@ 2009-10-09 20:36 Damien Carbonne
  2009-10-09 21:06 ` Adam Beneschan
  0 siblings, 1 reply; 14+ messages in thread
From: Damien Carbonne @ 2009-10-09 20:36 UTC (permalink / raw)


Hi,

When compiling the following sample code with GNAT GPL 2009 on Linux:

generic
    type Real is digits <>;
package Pack04 is
    Size1 : constant := Real'Size;
    Size2 : constant Integer := Real'Size;
end Pack04;

I get this:

-- pack04.ads:4:24: non-static expression used in number declaration
-- pack04.ads:4:28: size attribute is only static for scalar type (RM 
4.9(7,8))


RM 3.5(1): "scalar types comprise enumeration types, integer types, and 
real types."

RM 3.5.6 (1): "Real types provide approximations to the real numbers, 
with relative bounds on errors for floating point types, and with 
absolute bounds for fixed point types."

So Real is supposed to be a scalar!

Is this a problem with (misleading) message or with (effectively) wrong 
code?

Initially I wanted to create a generic package similar to:
generic
    type Real is digits <>;
package Generic_Float_Util is
    function Is_Positive (X : Real) return Boolean;
end Generic_Float_Util;

The intent is to get the sign of a floating point by directly reading 
the sign bit (assuming float numbers are encoded as IEEE), without any test.

Regards,

Damien



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

* Re: GNAT GPL 2009 - issue with floating points?
  2009-10-09 20:36 GNAT GPL 2009 - issue with floating points? Damien Carbonne
@ 2009-10-09 21:06 ` Adam Beneschan
  2009-10-09 22:15   ` Damien Carbonne
  0 siblings, 1 reply; 14+ messages in thread
From: Adam Beneschan @ 2009-10-09 21:06 UTC (permalink / raw)


On Oct 9, 1:36 pm, Damien Carbonne <damien.carbo...@free.fr> wrote:
> Hi,
>
> When compiling the following sample code with GNAT GPL 2009 on Linux:
>
> generic
>     type Real is digits <>;
> package Pack04 is
>     Size1 : constant := Real'Size;
>     Size2 : constant Integer := Real'Size;
> end Pack04;
>
> I get this:
>
> -- pack04.ads:4:24: non-static expression used in number declaration
> -- pack04.ads:4:28: size attribute is only static for scalar type (RM
> 4.9(7,8))
>
> RM 3.5(1): "scalar types comprise enumeration types, integer types, and
> real types."
>
> RM 3.5.6 (1): "Real types provide approximations to the real numbers,
> with relative bounds on errors for floating point types, and with
> absolute bounds for fixed point types."

Did you read the RM reference that the GNAT error message pointed you
to???

Also read 4.9(26).

> So Real is supposed to be a scalar!
>
> Is this a problem with (misleading) message or with (effectively) wrong
> code?

The code is wrong.

And anyway, the declaration of Size2 is legal (even though the
declaration of Size1 isn't), so there's no problem.

                             -- Adam




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

* Re: GNAT GPL 2009 - issue with floating points?
  2009-10-09 21:06 ` Adam Beneschan
@ 2009-10-09 22:15   ` Damien Carbonne
  2009-10-09 22:44     ` Ludovic Brenta
  2009-10-09 23:26     ` Adam Beneschan
  0 siblings, 2 replies; 14+ messages in thread
From: Damien Carbonne @ 2009-10-09 22:15 UTC (permalink / raw)


Adam Beneschan a �crit :
> On Oct 9, 1:36 pm, Damien Carbonne <damien.carbo...@free.fr> wrote:
>> Hi,
>>
>> When compiling the following sample code with GNAT GPL 2009 on Linux:
>>
>> generic
>>     type Real is digits <>;
>> package Pack04 is
>>     Size1 : constant := Real'Size;
>>     Size2 : constant Integer := Real'Size;
>> end Pack04;
>>
>> I get this:
>>
>> -- pack04.ads:4:24: non-static expression used in number declaration
>> -- pack04.ads:4:28: size attribute is only static for scalar type (RM
>> 4.9(7,8))
>>
>> RM 3.5(1): "scalar types comprise enumeration types, integer types, and
>> real types."
>>
>> RM 3.5.6 (1): "Real types provide approximations to the real numbers,
>> with relative bounds on errors for floating point types, and with
>> absolute bounds for fixed point types."
> 
> Did you read the RM reference that the GNAT error message pointed you
> to???
Yes I did !
It is very possible that this code is wrong (this is not really the 
problem here), but why does the message say that "size attribute is only 
static for scalar types" as an explanation, when the type the size 
attribute is applied to is precisely a scalar ! At least, I thought that 
floating points were scalars in Ada till this evening !
I read this message as: "As Real is a scalar type, I can apply size 
attribute to Real and obtain a static expression." This reading is 
apparently wrong.
Do you mean that the formal generic parameter Real is not a scalar?
I'm certainly no an Ada expert, but I really find the message confusing.
> 
> Also read 4.9(26).
> 
>> So Real is supposed to be a scalar!
>>
>> Is this a problem with (misleading) message or with (effectively) wrong
>> code?
> 
> The code is wrong.
Sure, but I still find the message misleading. Isn't Real a scalar type?
> 
> And anyway, the declaration of Size2 is legal (even though the
> declaration of Size1 isn't), so there's no problem.
The problem is that even if Size2 is a correct declaration, there are 
other latter issues with its use, not shown in the sample code.
> 
>                              -- Adam
> 



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

* Re: GNAT GPL 2009 - issue with floating points?
  2009-10-09 22:15   ` Damien Carbonne
@ 2009-10-09 22:44     ` Ludovic Brenta
  2009-10-10  6:33       ` Damien Carbonne
  2009-10-10 14:55       ` Samuel Tardieu
  2009-10-09 23:26     ` Adam Beneschan
  1 sibling, 2 replies; 14+ messages in thread
From: Ludovic Brenta @ 2009-10-09 22:44 UTC (permalink / raw)


Damien Carbonne wrote on comp.lang.ada:
> Adam Beneschan a écrit :
> > On Oct 9, 1:36 pm, Damien Carbonne <damien.carbo...@free.fr> wrote:
> >> Hi,
>
> >> When compiling the following sample code with GNAT GPL 2009 on Linux:
>
> >> generic
> >>     type Real is digits <>;
> >> package Pack04 is
> >>     Size1 : constant := Real'Size;
> >>     Size2 : constant Integer := Real'Size;
> >> end Pack04;
>
> >> I get this:
>
> >> -- pack04.ads:4:24: non-static expression used in number declaration
> >> -- pack04.ads:4:28: size attribute is only static for scalar type (RM
> >> 4.9(7,8))

> Do you mean that the formal generic parameter Real is not a scalar?
> I'm certainly no an Ada expert, but I really find the message confusing.

Real is scalar all right but it is not static; the first error message
is explicit about that.  I agree that the second message is confusing;
it should read "size attribute is only static for a static scalar
subtype (RM 4.9(7,8,26))".  Generic formal subtypes are non-static.

HTH

--
Ludovic Brenta.



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

* Re: GNAT GPL 2009 - issue with floating points?
  2009-10-09 22:15   ` Damien Carbonne
  2009-10-09 22:44     ` Ludovic Brenta
@ 2009-10-09 23:26     ` Adam Beneschan
  2009-10-10  6:39       ` Damien Carbonne
  1 sibling, 1 reply; 14+ messages in thread
From: Adam Beneschan @ 2009-10-09 23:26 UTC (permalink / raw)


On Oct 9, 3:15 pm, Damien Carbonne <damien.carbo...@free.fr> wrote:
> Adam Beneschan a écrit :
> >> -- pack04.ads:4:24: non-static expression used in number declaration
> >> -- pack04.ads:4:28: size attribute is only static for scalar type (RM
> >> 4.9(7,8))
>
> >> RM 3.5(1): "scalar types comprise enumeration types, integer types, and
> >> real types."
>
> >> RM 3.5.6 (1): "Real types provide approximations to the real numbers,
> >> with relative bounds on errors for floating point types, and with
> >> absolute bounds for fixed point types."
>
> > Did you read the RM reference that the GNAT error message pointed you
> > to???
>
> Yes I did !

The RM reference says (in paragraph 6) that the attribute reference
has to have a prefix that denotes a "static scalar subtype" (which is
defined later on in 4.9).  The GNAT message says just "scalar
subtype"; as Ludovic points out, that could have been worded better
and should probably have said "static scalar subtype".  However, one
should always trust the RM in preference to an error message, since
the RM is a standard.

Also, keep in mind that a "static" expression is basically one that
the compiler can determine at compile time when it sees the
expression, and in this case it can't, because it has no idea what
sort of float will be used to instantiate the package.

                                         -- Adam




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

* Re: GNAT GPL 2009 - issue with floating points?
  2009-10-09 22:44     ` Ludovic Brenta
@ 2009-10-10  6:33       ` Damien Carbonne
  2009-10-10 14:55       ` Samuel Tardieu
  1 sibling, 0 replies; 14+ messages in thread
From: Damien Carbonne @ 2009-10-10  6:33 UTC (permalink / raw)


Ludovic Brenta a �crit :
> Damien Carbonne wrote on comp.lang.ada:
>> Adam Beneschan a �crit :
>>> On Oct 9, 1:36 pm, Damien Carbonne <damien.carbo...@free.fr> wrote:
>>>> Hi,
>>>> When compiling the following sample code with GNAT GPL 2009 on Linux:
>>>> generic
>>>>     type Real is digits <>;
>>>> package Pack04 is
>>>>     Size1 : constant := Real'Size;
>>>>     Size2 : constant Integer := Real'Size;
>>>> end Pack04;
>>>> I get this:
>>>> -- pack04.ads:4:24: non-static expression used in number declaration
>>>> -- pack04.ads:4:28: size attribute is only static for scalar type (RM
>>>> 4.9(7,8))
> 
>> Do you mean that the formal generic parameter Real is not a scalar?
>> I'm certainly no an Ada expert, but I really find the message confusing.
> 
> Real is scalar all right but it is not static; the first error message
> is explicit about that.  I agree that the second message is confusing;
> it should read "size attribute is only static for a static scalar
> subtype (RM 4.9(7,8,26))".  Generic formal subtypes are non-static.
> 
> HTH
> 
> --
> Ludovic Brenta.
Thanks for clarification.
Damien



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

* Re: GNAT GPL 2009 - issue with floating points?
  2009-10-09 23:26     ` Adam Beneschan
@ 2009-10-10  6:39       ` Damien Carbonne
  2009-10-10  7:16         ` Dmitry A. Kazakov
  0 siblings, 1 reply; 14+ messages in thread
From: Damien Carbonne @ 2009-10-10  6:39 UTC (permalink / raw)


Adam Beneschan a �crit :
> On Oct 9, 3:15 pm, Damien Carbonne <damien.carbo...@free.fr> wrote:
>> Adam Beneschan a �crit :
>>>> -- pack04.ads:4:24: non-static expression used in number declaration
>>>> -- pack04.ads:4:28: size attribute is only static for scalar type (RM
>>>> 4.9(7,8))
>>>> RM 3.5(1): "scalar types comprise enumeration types, integer types, and
>>>> real types."
>>>> RM 3.5.6 (1): "Real types provide approximations to the real numbers,
>>>> with relative bounds on errors for floating point types, and with
>>>> absolute bounds for fixed point types."
>>> Did you read the RM reference that the GNAT error message pointed you
>>> to???
>> Yes I did !
> 
> The RM reference says (in paragraph 6) that the attribute reference
> has to have a prefix that denotes a "static scalar subtype" (which is
> defined later on in 4.9).  The GNAT message says just "scalar
> subtype"; as Ludovic points out, that could have been worded better
> and should probably have said "static scalar subtype".  However, one
> should always trust the RM in preference to an error message, since
> the RM is a standard.
> 
> Also, keep in mind that a "static" expression is basically one that
> the compiler can determine at compile time when it sees the
> expression, and in this case it can't, because it has no idea what
> sort of float will be used to instantiate the package.
> 
>                                          -- Adam
> 
Thanks, now things are clearer. I understand that the compiler can not 
determine Real'Size when it compiles the generic package, but when it is 
instantiated, it should have then necessary knowledge. This should be 
true in simple cases (e.g., by instantiating the package with Float). 
Perhaps that things are not always so simple.

Thanks again,
Damien



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

* Re: GNAT GPL 2009 - issue with floating points?
  2009-10-10  6:39       ` Damien Carbonne
@ 2009-10-10  7:16         ` Dmitry A. Kazakov
  2009-10-11  9:33           ` Dirk Herrmann
  0 siblings, 1 reply; 14+ messages in thread
From: Dmitry A. Kazakov @ 2009-10-10  7:16 UTC (permalink / raw)


On Sat, 10 Oct 2009 08:39:47 +0200, Damien Carbonne wrote:

> Thanks, now things are clearer. I understand that the compiler can not 
> determine Real'Size when it compiles the generic package, but when it is 
> instantiated, it should have then necessary knowledge.

Not if the generic body is shared, rather than expanded.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de



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

* Re: GNAT GPL 2009 - issue with floating points?
  2009-10-09 22:44     ` Ludovic Brenta
  2009-10-10  6:33       ` Damien Carbonne
@ 2009-10-10 14:55       ` Samuel Tardieu
  2009-10-10 16:12         ` Ludovic Brenta
  2009-10-10 16:38         ` Damien Carbonne
  1 sibling, 2 replies; 14+ messages in thread
From: Samuel Tardieu @ 2009-10-10 14:55 UTC (permalink / raw)


>>>>> "Ludovic" == Ludovic Brenta <ludovic@ludovic-brenta.org> writes:

Ludovic> I agree that the second message is confusing; it should read
Ludovic> "size attribute is only static for a static scalar subtype (RM
Ludovic> 4.9(7,8,26))".

Right. GCC 4.5 will have an enhanced message.
-- 
Samuel Tardieu -- sam@rfc1149.net -- http://www.rfc1149.net/



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

* Re: GNAT GPL 2009 - issue with floating points?
  2009-10-10 14:55       ` Samuel Tardieu
@ 2009-10-10 16:12         ` Ludovic Brenta
  2009-10-10 16:38         ` Damien Carbonne
  1 sibling, 0 replies; 14+ messages in thread
From: Ludovic Brenta @ 2009-10-10 16:12 UTC (permalink / raw)


Samuel Tardieu wrote on comp.lang.ada:
> >>>>> "Ludovic" == Ludovic Brenta <ludo...@ludovic-brenta.org> writes:
>
> Ludovic> I agree that the second message is confusing; it should read
> Ludovic> "size attribute is only static for a static scalar subtype (RM
> Ludovic> 4.9(7,8,26))".
>
> Right. GCC 4.5 will have an enhanced message.

Thanks for writing and submitting the patch, and thanks to Robert
Dewar for approving it on such short notice.

--
Ludovic Brenta.



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

* Re: GNAT GPL 2009 - issue with floating points?
  2009-10-10 14:55       ` Samuel Tardieu
  2009-10-10 16:12         ` Ludovic Brenta
@ 2009-10-10 16:38         ` Damien Carbonne
  1 sibling, 0 replies; 14+ messages in thread
From: Damien Carbonne @ 2009-10-10 16:38 UTC (permalink / raw)


Samuel Tardieu a �crit :
>>>>>> "Ludovic" == Ludovic Brenta <ludovic@ludovic-brenta.org> writes:
> 
> Ludovic> I agree that the second message is confusing; it should read
> Ludovic> "size attribute is only static for a static scalar subtype (RM
> Ludovic> 4.9(7,8,26))".
> 
> Right. GCC 4.5 will have an enhanced message.
Thanks for doing this.
Damien



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

* Re: GNAT GPL 2009 - issue with floating points?
  2009-10-10  7:16         ` Dmitry A. Kazakov
@ 2009-10-11  9:33           ` Dirk Herrmann
  2009-10-11 10:27             ` Dmitry A. Kazakov
  0 siblings, 1 reply; 14+ messages in thread
From: Dirk Herrmann @ 2009-10-11  9:33 UTC (permalink / raw)


Dmitry A. Kazakov wrote:
> On Sat, 10 Oct 2009 08:39:47 +0200, Damien Carbonne wrote:
> 
>> Thanks, now things are clearer. I understand that the compiler can not 
>> determine Real'Size when it compiles the generic package, but when it is 
>> instantiated, it should have then necessary knowledge.
> 
> Not if the generic body is shared, rather than expanded.

It seems that the rules for generics are designed to allow sharing.  It 
surprises me, however, that there is no means to state that sharing is not 
required, like, for example, a pragma or attribute.  Such generics would offer 
more implementation possibilities, since more information would be static.  I 
understand the desire to support sharing, but for me it is a resource issue 
(affecting ROM size and execution time) that might better be put under control 
of the developer.

--
Regards,
Dirk Herrmann



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

* Re: GNAT GPL 2009 - issue with floating points?
  2009-10-11  9:33           ` Dirk Herrmann
@ 2009-10-11 10:27             ` Dmitry A. Kazakov
  2009-10-12 23:26               ` Randy Brukardt
  0 siblings, 1 reply; 14+ messages in thread
From: Dmitry A. Kazakov @ 2009-10-11 10:27 UTC (permalink / raw)


On Sun, 11 Oct 2009 11:33:04 +0200, Dirk Herrmann wrote:

> Dmitry A. Kazakov wrote:
>> On Sat, 10 Oct 2009 08:39:47 +0200, Damien Carbonne wrote:
>> 
>>> Thanks, now things are clearer. I understand that the compiler can not 
>>> determine Real'Size when it compiles the generic package, but when it is 
>>> instantiated, it should have then necessary knowledge.
>> 
>> Not if the generic body is shared, rather than expanded.
> 
> It seems that the rules for generics are designed to allow sharing.

They were.

Presently, I don't know if shared / contracted model is any alive.
Considering:

   generic
      type J is interface and I;
   package P is
      type T is new J with null record;
   end P;

When compiling P you cannot know if the actual for J does not have an
abstract operation which T does not implement.

Nevertheless one tries to pretend that generics still have some kind of
"contracts".

> It 
> surprises me, however, that there is no means to state that sharing is not 
> required, like, for example, a pragma or attribute. Such generics would offer 
> more implementation possibilities, since more information would be static.

In that case it should be a syntax element, not a pragma.

> I 
> understand the desire to support sharing, but for me it is a resource issue 
> (affecting ROM size and execution time) that might better be put under control 
> of the developer.

Then the solution is wrong. The desire is not to share bodies, but to have
contracts. With generics this goal is most likely unachievable.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de



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

* Re: GNAT GPL 2009 - issue with floating points?
  2009-10-11 10:27             ` Dmitry A. Kazakov
@ 2009-10-12 23:26               ` Randy Brukardt
  0 siblings, 0 replies; 14+ messages in thread
From: Randy Brukardt @ 2009-10-12 23:26 UTC (permalink / raw)


"Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de> wrote in message 
news:1y6imbz0dijq$.1ofvdwq3q3cz7.dlg@40tude.net...
> On Sun, 11 Oct 2009 11:33:04 +0200, Dirk Herrmann wrote:
>
>> Dmitry A. Kazakov wrote:
>>> On Sat, 10 Oct 2009 08:39:47 +0200, Damien Carbonne wrote:
>>>
>>>> Thanks, now things are clearer. I understand that the compiler can not
>>>> determine Real'Size when it compiles the generic package, but when it 
>>>> is
>>>> instantiated, it should have then necessary knowledge.
>>>
>>> Not if the generic body is shared, rather than expanded.
>>
>> It seems that the rules for generics are designed to allow sharing.
>
> They were.
>
> Presently, I don't know if shared / contracted model is any alive.
> Considering:
>
>   generic
>      type J is interface and I;
>   package P is
>      type T is new J with null record;
>   end P;
>
> When compiling P you cannot know if the actual for J does not have an
> abstract operation which T does not implement.

Right, but that is present in Ada 95 as well. That's why all rules are 
rechecked in the visible part of an instance. In this particular example, 
the generic is legal, but an instance could be illegal if an operation that 
requires overriding is inherited.

Note that type extensions like this are always illegal in generic unit 
bodies, so there is no problem with that. The basic principle (as described 
by the Ada 9x design team) is to "assume the best (and recheck)" in generic 
specs and "assume the worst" in generic bodies.

> Nevertheless one tries to pretend that generics still have some kind of
> "contracts".

The body has a strong contract, the specification a weaker one.

...
> Then the solution is wrong. The desire is not to share bodies, but to have
> contracts. With generics this goal is most likely unachievable.

Neverhteless, Ada achieves it. Of course, the Ada definition of a generic 
contract may not match your ideal...

                                     Randy.





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

end of thread, other threads:[~2009-10-12 23:26 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-10-09 20:36 GNAT GPL 2009 - issue with floating points? Damien Carbonne
2009-10-09 21:06 ` Adam Beneschan
2009-10-09 22:15   ` Damien Carbonne
2009-10-09 22:44     ` Ludovic Brenta
2009-10-10  6:33       ` Damien Carbonne
2009-10-10 14:55       ` Samuel Tardieu
2009-10-10 16:12         ` Ludovic Brenta
2009-10-10 16:38         ` Damien Carbonne
2009-10-09 23:26     ` Adam Beneschan
2009-10-10  6:39       ` Damien Carbonne
2009-10-10  7:16         ` Dmitry A. Kazakov
2009-10-11  9:33           ` Dirk Herrmann
2009-10-11 10:27             ` Dmitry A. Kazakov
2009-10-12 23:26               ` Randy Brukardt

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