comp.lang.ada
 help / color / mirror / Atom feed
* Default comparison of tagged variant records
@ 2006-12-08 13:49 msimonides
  2006-12-08 18:30 ` Adam Beneschan
  0 siblings, 1 reply; 5+ messages in thread
From: msimonides @ 2006-12-08 13:49 UTC (permalink / raw)


I'm having problem with default comparison of tagged, variant records
not calling user-defined "=" operators for record components. At least
that's, what I think.

Here is an example:

with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
with Ada.Text_IO; use Ada.Text_IO;

procedure Test is

   type Thing is tagged
      record
         null;
      end record;

   type Bigger_Thing (Discriminant : Boolean) is new Thing with
      record
         case Discriminant is
            when True =>
               Text           : Unbounded_String;
            when False =>
               Number         : Natural;
         end case;
      end record;

   Big_Something        : constant Bigger_Thing := Bigger_Thing'(
      Discriminant => True, Text => Null_Unbounded_String);
   Big_Something_Copy   : constant Bigger_Thing := Big_Something;

begin
   Put_Line ("Are they equal?");
   Put_Line (Boolean'Image (Big_Something = Big_Something_Copy));
end Test;


It prints "FALSE" and does it for both values of Discriminant, so even
when Text component isn't used.
However, comparison works properly when either there is no
Unbounded_String, the record isn't tagged or the record doesn't have a
discriminant.

I have read a few threads on predefined operator reemergance on this
list and have looked at relevant parts of RM (ie. on generic formal
parameters, predefined operations and equality operator inheritance)
but it seems that my case isn't covered there.

Could you please shed some light on it? For now I have defined an
equality operator for the type in question.

I'm using GNAT GPL 2006.
-- 
Marcin Simonides




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

* Re: Default comparison of tagged variant records
  2006-12-08 13:49 Default comparison of tagged variant records msimonides
@ 2006-12-08 18:30 ` Adam Beneschan
  2006-12-09  1:38   ` Randy Brukardt
  0 siblings, 1 reply; 5+ messages in thread
From: Adam Beneschan @ 2006-12-08 18:30 UTC (permalink / raw)


msimonides@power.com.pl wrote:
> I'm having problem with default comparison of tagged, variant records
> not calling user-defined "=" operators for record components. At least
> that's, what I think.
>
> Here is an example:
>
> with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
> with Ada.Text_IO; use Ada.Text_IO;
>
> procedure Test is
>
>    type Thing is tagged
>       record
>          null;
>       end record;
>
>    type Bigger_Thing (Discriminant : Boolean) is new Thing with
>       record
>          case Discriminant is
>             when True =>
>                Text           : Unbounded_String;
>             when False =>
>                Number         : Natural;
>          end case;
>       end record;
>
>    Big_Something        : constant Bigger_Thing := Bigger_Thing'(
>       Discriminant => True, Text => Null_Unbounded_String);
>    Big_Something_Copy   : constant Bigger_Thing := Big_Something;
>
> begin
>    Put_Line ("Are they equal?");
>    Put_Line (Boolean'Image (Big_Something = Big_Something_Copy));
> end Test;
>
>
> It prints "FALSE" and does it for both values of Discriminant, so even
> when Text component isn't used.
> However, comparison works properly when either there is no
> Unbounded_String, the record isn't tagged or the record doesn't have a
> discriminant.

This looks like a compiler bug to me.  It should return TRUE in those
all cases.

                              -- Adam




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

* Re: Default comparison of tagged variant records
  2006-12-08 18:30 ` Adam Beneschan
@ 2006-12-09  1:38   ` Randy Brukardt
  2006-12-11  7:40     ` msimonides
  0 siblings, 1 reply; 5+ messages in thread
From: Randy Brukardt @ 2006-12-09  1:38 UTC (permalink / raw)


"Adam Beneschan" <adam@irvine.com> wrote in message
news:1165602630.553040.134960@n67g2000cwd.googlegroups.com...
> msimonides@power.com.pl wrote:
> > I'm having problem with default comparison of tagged, variant records
> > not calling user-defined "=" operators for record components. At least
> > that's, what I think.
> >
> > Here is an example:
> >
> > with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
> > with Ada.Text_IO; use Ada.Text_IO;
> >
> > procedure Test is
> >
> >    type Thing is tagged
> >       record
> >          null;
> >       end record;
> >
> >    type Bigger_Thing (Discriminant : Boolean) is new Thing with
> >       record
> >          case Discriminant is
> >             when True =>
> >                Text           : Unbounded_String;
> >             when False =>
> >                Number         : Natural;
> >          end case;
> >       end record;
> >
> >    Big_Something        : constant Bigger_Thing := Bigger_Thing'(
> >       Discriminant => True, Text => Null_Unbounded_String);
> >    Big_Something_Copy   : constant Bigger_Thing := Big_Something;
> >
> > begin
> >    Put_Line ("Are they equal?");
> >    Put_Line (Boolean'Image (Big_Something = Big_Something_Copy));
> > end Test;
> >
> >
> > It prints "FALSE" and does it for both values of Discriminant, so even
> > when Text component isn't used.
> > However, comparison works properly when either there is no
> > Unbounded_String, the record isn't tagged or the record doesn't have a
> > discriminant.
>
> This looks like a compiler bug to me.  It should return TRUE in those
> all cases.

I agree with Adam that it *looks* like a compiler bug. OTOH, I tried it on
Janus/Ada and it also gave FALSE. Something else is going on...

...(much later)...

The Janus/Ada issue looks like a compiler bug. The fact that
Unbounded_String is not visibly tagged is fooling the compiler into using
the predefined rather than the primitive equality. There is code to handle
this, but it doesn't work. The direct call to "=" works right.

...(much, much later)...

...and so does the call to "=". Your program should print TRUE and this is
clearly a Gnat bug. ;-)

(If anyone cares, the problem with Janus/Ada was that it was not picking up
the primitive "=" for the untagged private type Unbounded_String, as it was
only looking for primitive *dispatching* operations. Thus it was using the
predefined one, and that always returned False unless the objects were the
same object. Gnat's problem may very well be different.)

                                      Randy.






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

* Re: Default comparison of tagged variant records
  2006-12-09  1:38   ` Randy Brukardt
@ 2006-12-11  7:40     ` msimonides
  2006-12-11 23:12       ` Randy Brukardt
  0 siblings, 1 reply; 5+ messages in thread
From: msimonides @ 2006-12-11  7:40 UTC (permalink / raw)



Randy Brukardt napisal(a):
> "Adam Beneschan" <adam@irvine.com> wrote in message
> news:1165602630.553040.134960@n67g2000cwd.googlegroups.com...

> > This looks like a compiler bug to me.  It should return TRUE in those
> > all cases.
>
> I agree with Adam that it *looks* like a compiler bug. OTOH, I tried it on
> Janus/Ada and it also gave FALSE. Something else is going on...
>
> ...(much later)...
>
> The Janus/Ada issue looks like a compiler bug. The fact that
> Unbounded_String is not visibly tagged is fooling the compiler into using
> the predefined rather than the primitive equality. There is code to handle
> this, but it doesn't work. The direct call to "=" works right.

Yes, there was a discussion on this topic on this group some ten years
ago, I should've referred to it in  my post: "Equality on
Ada.Strings.Unbounded" [1] that mentioned this.

> ...(much, much later)...
>
> ...and so does the call to "=". Your program should print TRUE and this is
> clearly a Gnat bug. ;-)

Do you mean comparing the Unbounded_String components of record? This
works in Gnat too and I've used it to define equality for the
troublesome type as a workaround.

> (If anyone cares, the problem with Janus/Ada was that it was not picking up
> the primitive "=" for the untagged private type Unbounded_String, as it was
> only looking for primitive *dispatching* operations. Thus it was using the
> predefined one, and that always returned False unless the objects were the
> same object. Gnat's problem may very well be different.)

In GNAT "=" also returns True when comparing the same object with
itself.

It looks like there might be some explanation that has its roots
somewhere in ARM, that's why I asked.

Thanks for your time.
--
Marcin Simonides

[1]
http://groups.google.com/group/comp.lang.ada/browse_thread/thread/7ff7c4bb20276552/40a0d3db1f82b6c0?q=ada+unbounded+string+equality&lnk=ol&hl=pl&




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

* Re: Default comparison of tagged variant records
  2006-12-11  7:40     ` msimonides
@ 2006-12-11 23:12       ` Randy Brukardt
  0 siblings, 0 replies; 5+ messages in thread
From: Randy Brukardt @ 2006-12-11 23:12 UTC (permalink / raw)


<msimonides@power.com.pl> wrote in message
news:1165822851.141177.13460@79g2000cws.googlegroups.com...
>
> Randy Brukardt napisal(a):
> > "Adam Beneschan" <adam@irvine.com> wrote in message
> > news:1165602630.553040.134960@n67g2000cwd.googlegroups.com...
>
> > > This looks like a compiler bug to me.  It should return TRUE in those
> > > all cases.
> >
> > I agree with Adam that it *looks* like a compiler bug. OTOH, I tried it
on
> > Janus/Ada and it also gave FALSE. Something else is going on...
...
> > ...and so does the call to "=". Your program should print TRUE and this
is
> > clearly a Gnat bug. ;-)
>
> Do you mean comparing the Unbounded_String components of record? This
> works in Gnat too and I've used it to define equality for the
> troublesome type as a workaround.

No, I mean the original full record compare should work properly.

> > (If anyone cares, the problem with Janus/Ada was that it was not picking
up
> > the primitive "=" for the untagged private type Unbounded_String, as it
was
> > only looking for primitive *dispatching* operations. Thus it was using
the
> > predefined one, and that always returned False unless the objects were
the
> > same object. Gnat's problem may very well be different.)
>
> In GNAT "=" also returns True when comparing the same object with
> itself.
>
> It looks like there might be some explanation that has its roots
> somewhere in ARM, that's why I asked.

Not really. Ada 95 was rather silent on this issue: in particular, there was
no requirement that the predefined "=" work right for language-defined
types. So some implementations caused trouble by not considering this issue;
re-emergence could be a problem. The ARG thought that this was bad for
usability, so that problem was addressed in the Corrigendum (2001) by adding
4.5.2(32.1/1):

For all nonlimited types declared in language-defined packages, the "=" and
"/=" operators of the type shall behave as if they were the predefined
equality operators for the purposes of the equality of composite types and
generic formal types.

There is a further note to implementers in the AARM:

If any language-defined types are implemented with a user-defined "="
operator, then either the full type must be tagged, or the compiler must use
"magic" to implement equality for this type. A normal user-defined "="
operator for an untagged type does not meet this requirement.

Janus/Ada did indeed implement Unbounded_String as a tagged type, but a
compiler bug prevented the compiler from noticing this when creating "=" for
a type with Unbounded_String components.

GNAT seems to have a similar bug; but make no mistake, it is a bug, because
the behavior reported for GNAT violates 4.5.2(32.1/1). [As well as good
taste! ;-)]

I suspect that the main reason that implementations have bugs in this area
is that there is no ACATS test for 4.5.2(32.1/1). It was considered a low
priority to test that last time such things were considered; perhaps that
judgement was wrong.

                                        Randy.




                                       Randy.





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

end of thread, other threads:[~2006-12-11 23:12 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-12-08 13:49 Default comparison of tagged variant records msimonides
2006-12-08 18:30 ` Adam Beneschan
2006-12-09  1:38   ` Randy Brukardt
2006-12-11  7:40     ` msimonides
2006-12-11 23:12       ` Randy Brukardt

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