comp.lang.ada
 help / color / mirror / Atom feed
From: "Adam Beneschan" <adam@irvine.com>
Subject: Predefined equality, reemergence
Date: 4 Apr 2006 12:30:13 -0700
Date: 2006-04-04T12:30:13-07:00	[thread overview]
Message-ID: <1144179013.753791.169830@u72g2000cwu.googlegroups.com> (raw)

Hi, everyone,

I'm having trouble understanding how the language rules about
predefined equality, specifically 4.5.2(24), behave when the component
type involved may be seen as "tagged" or "untagged" depending on
whether you're inside an instance.

Specifically, with regard to the program below: I would think that the
program would set Bool_1 to FALSE and Bool_2 to TRUE.  The way the
rules look to me, there's an implicit "=" declared in the generic for
Rec, and this "=" would be the predefined equality for Rec, which, when
matching components, uses the *predefined* (not the primitive) equals
for the matching component Comp1, since the formal type T is untagged.
The definition of Equal would use this version of "=".  Then, in the
instance, this implicit "=" is copied, and since Equal would use the
implicit "=", it should return FALSE when given two records whose
Comp1.Comp2 are different (5 and -5).  However, 12.3 says a whole new
set of primitive subprograms is implicitly declared for use outside the
instance, and it would differ since the type Rec in the instance
depends on the properties of the actual type Tagged_Rec, and I would
think that "="(Left,Right:Rec) is one of those primitive subprograms
that would differ; thus, outside the instance, "=" would refer to the
predefined equality that calls the *primitive* equals operator on the
component Comp1, since (outside the instance) the component type is
tagged, and thus the user-defined "=" would be called, which would
return TRUE if the absolute values of Comp2 are the same.

When compiled with the version of GNAT I'm using (which is not the
latest version), the program sets both Bool_1 and Bool_2 to TRUE, which
is not what I expected.

What's the correct output?  What's my mistake, if any, in interpreting
the rules?

Thanks for any help you can provide,

                      -- Adam


generic
    type T is private;
package Pak1 is
    type Rec is record
        Comp1 : T;
    end record;
    function Equal (Left, Right : Rec) return Boolean;
end Pak1;

package body Pak1 is
    function Equal (Left, Right : Rec) return Boolean is
    begin
        return Left = Right;
    end Equal;
end Pak1;


package Pak2 is
    type Tagged_Rec is tagged record
        Comp2 : Integer;
    end record;
    function "=" (Left, Right : Tagged_Rec) return Boolean;
end Pak2;

package body Pak2 is
    function "=" (Left, Right : Tagged_Rec) return Boolean is
    begin
        return abs (Left.Comp2) = abs (Right.Comp2);
    end "=";
end Pak2;


with Pak1;
with Pak2;
with Text_IO;
procedure Test69 is
    package New_Pak1 is new Pak1 (Pak2.Tagged_Rec);
    X : New_Pak1.Rec := (Comp1 => (Comp2 => 5));
    Y : New_Pak1.Rec := (Comp1 => (Comp2 => -5));
    Bool_1 : Boolean := New_Pak1.Equal (X, Y);
    Bool_2 : Boolean := New_Pak1."=" (X, Y);
begin
    Text_IO.Put_Line (Boolean'Image (Bool_1));
    Text_IO.Put_Line (Boolean'Image (Bool_2));    
end Test69;




             reply	other threads:[~2006-04-04 19:30 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-04-04 19:30 Adam Beneschan [this message]
2006-04-04 19:46 ` Predefined equality, reemergence Adam Beneschan
2006-04-04 23:00   ` Randy Brukardt
2006-04-04 23:57     ` Adam Beneschan
2006-04-05 12:16       ` christoph.grein
2006-04-05 18:47         ` Adam Beneschan
2006-04-05 22:01           ` Robert A Duff
replies disabled

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