comp.lang.ada
 help / color / mirror / Atom feed
* Two generic families: I'm confused by error msg
@ 2004-11-03  5:40 John Woodruff
  2004-11-03  8:46 ` Dmitry A. Kazakov
  2004-11-03 10:01 ` Martin Dowie
  0 siblings, 2 replies; 5+ messages in thread
From: John Woodruff @ 2004-11-03  5:40 UTC (permalink / raw)


I'm puzzled by an Ada error.  The error arises in the attached
program, which uses two different (parent-child) pairs of generic
packages.  And the error seems to indicate a hard-for-me-to-understand
issue of visibility.

The first two packages (numeric_io and it's child *.matrix_io) do some
IO on vectors and matrices of floating numbers.  Just about like the
new Ada.Numerics.Generic_Real_Arrays.Generic_IO.

Two more packages (name_io and its child *.matrix_name_io) add
services that parse name-directed input for object-oriented
applications.  This pair uses instances of numeric_io, so that vectors
are the same regardless of which services are invoked.

It all works fine, and I'm in the final stages of preparing this to
share.  Maybe on adaworld or adapower if they'll have me....

BUT

Numeric_io defines a subtype "real" that is just the actual type given
for its argument.  That's how the two children manage to work on the
same types (they are children of different, unrelated, parents).

So along comes the client (I named it "Unreal" in this exhibit). It
laces the two generic families together, and it works perfectly  IF and
ONLY IF it *happens* to use the identifier "Real" somewhere.  This
client does not need to declare any *usage* of the subtype "Real", nor
use it in instantiating the generics.

Unfortunately, poor client has no way of knowing he needs to declare
"real".  And I'm not sure what is the language-law that connects the
type Scalar_IO.Real in the generic actual package with the subtype
identifier Real in the client.

Can someone help me make sense of this?  Can someone suggest a way to
rephrase these relationships so I don't require the client to "say the
magic word"?

<<< gnatchop the stuff after this line to see the puzzle >>>

generic
   type Floating_Type is digits <> ;
package Numeric_Io is
   subtype Real is Floating_Type ;
end Numeric_Io ;


generic
   type Vector_Ix is  (<>) ;
   type Vector is array (Vector_Ix range <>) of Real ;
package Numeric_Io.Matrix_IO is
end Numeric_Io.Matrix_IO ;

with Numeric_Io;
generic
   type Floating_Type is digits <> ;
   with package Scalar_Io is new Numeric_Io (Floating_Type) ;
package Name_Io is
end Name_Io;

with Numeric_Io.Matrix_Io ;
generic
   type Vector_Ix is  (<>) ;
   type Vector is array (Vector_Ix range <>) of Scalar_Io.Real ;
   with package Matrix_Io is new Scalar_Io.Matrix_Io
     (Vector_Ix, Vector) ;
package Name_Io.Matrix_name_IO is
end Name_Io.Matrix_name_IO ;


with Numeric_Io.Matrix_Io ;
with Name_Io.Matrix_Name_Io ;

procedure Unreal is
   type Unreal is digits 6 ;
   
--------------------------------------------------------------------------
-- If this line "subtype Real is Unreal"  is ABSENT, then error at line 26:
-- Component subtype of actual does not match that of formal "Vector" 
--------------------------------------------------------------------------
   subtype Real is Unreal;
   
   type Vector is array (Integer range <>) of Unreal ;

   package Scalar_IO is new Numeric_IO (Unreal) ;

   package Matrix_IO is new Scalar_IO.Matrix_IO
     (Vector_Ix  => Integer,
      Vector     => Vector);

   package Nio is new Name_Io (Floating_Type => Unreal,
                               Scalar_Io     => Scalar_IO);

   package Mat_IO is new Nio.Matrix_Name_IO
     (Vector_Ix  => Integer,
      Vector     => Vector,   -- the error is here
      Matrix_Io  => Matrix_IO) ;

begin
   null ;
end Unreal ;



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

* Re: Two generic families: I'm confused by error msg
  2004-11-03  5:40 Two generic families: I'm confused by error msg John Woodruff
@ 2004-11-03  8:46 ` Dmitry A. Kazakov
  2004-11-03 10:01 ` Martin Dowie
  1 sibling, 0 replies; 5+ messages in thread
From: Dmitry A. Kazakov @ 2004-11-03  8:46 UTC (permalink / raw)


On 2 Nov 2004 21:40:29 -0800, John Woodruff wrote:

> I'm puzzled by an Ada error.  The error arises in the attached
> program, which uses two different (parent-child) pairs of generic
> packages.  And the error seems to indicate a hard-for-me-to-understand
> issue of visibility.
> 
> The first two packages (numeric_io and it's child *.matrix_io) do some
> IO on vectors and matrices of floating numbers.  Just about like the
> new Ada.Numerics.Generic_Real_Arrays.Generic_IO.
> 
> Two more packages (name_io and its child *.matrix_name_io) add
> services that parse name-directed input for object-oriented
> applications.  This pair uses instances of numeric_io, so that vectors
> are the same regardless of which services are invoked.
> 
> It all works fine, and I'm in the final stages of preparing this to
> share.  Maybe on adaworld or adapower if they'll have me....
> 
> BUT
> 
> Numeric_io defines a subtype "real" that is just the actual type given
> for its argument.  That's how the two children manage to work on the
> same types (they are children of different, unrelated, parents).
> 
> So along comes the client (I named it "Unreal" in this exhibit). It
> laces the two generic families together, and it works perfectly  IF and
> ONLY IF it *happens* to use the identifier "Real" somewhere.  This
> client does not need to declare any *usage* of the subtype "Real", nor
> use it in instantiating the generics.
> 
> Unfortunately, poor client has no way of knowing he needs to declare
> "real".  And I'm not sure what is the language-law that connects the
> type Scalar_IO.Real in the generic actual package with the subtype
> identifier Real in the client.
> 
> Can someone help me make sense of this?  Can someone suggest a way to
> rephrase these relationships so I don't require the client to "say the
> magic word"?
> 
> <<< gnatchop the stuff after this line to see the puzzle >>>
> 
> generic
>    type Floating_Type is digits <> ;
> package Numeric_Io is
>    subtype Real is Floating_Type ;
> end Numeric_Io ;
> 
> 
> generic
>    type Vector_Ix is  (<>) ;
>    type Vector is array (Vector_Ix range <>) of Real ;
> package Numeric_Io.Matrix_IO is
> end Numeric_Io.Matrix_IO ;
> 
> with Numeric_Io;
> generic
>    type Floating_Type is digits <> ;
>    with package Scalar_Io is new Numeric_Io (Floating_Type) ;
> package Name_Io is
> end Name_Io;
> 
> with Numeric_Io.Matrix_Io ;
> generic
>    type Vector_Ix is  (<>) ;
>    type Vector is array (Vector_Ix range <>) of Scalar_Io.Real ;
>    with package Matrix_Io is new Scalar_Io.Matrix_Io
>      (Vector_Ix, Vector) ;
> package Name_Io.Matrix_name_IO is
> end Name_Io.Matrix_name_IO ;

Why do not you do it as:

with Numeric_Io.Matrix_Io ;
generic
   with package Matrix_Io is new Scalar_Io.Matrix_Io (<>);
package Name_Io.Matrix_name_IO is

Here you can refer the actual types of Matrix_IO instantiation as
Matrix_IO.Vector_Ix and Matrix_IO.Vector. This would also make
instantiations easier.

> with Numeric_Io.Matrix_Io ;
> with Name_Io.Matrix_Name_Io ;
> 
> procedure Unreal is
>    type Unreal is digits 6 ;
>    
> --------------------------------------------------------------------------
> -- If this line "subtype Real is Unreal"  is ABSENT, then error at line 26:
> -- Component subtype of actual does not match that of formal "Vector" 
> --------------------------------------------------------------------------
>    subtype Real is Unreal;
>    
>    type Vector is array (Integer range <>) of Unreal ;
> 
>    package Scalar_IO is new Numeric_IO (Unreal) ;
> 
>    package Matrix_IO is new Scalar_IO.Matrix_IO
>      (Vector_Ix  => Integer,
>       Vector     => Vector);
> 
>    package Nio is new Name_Io (Floating_Type => Unreal,
>                                Scalar_Io     => Scalar_IO);
> 
>    package Mat_IO is new Nio.Matrix_Name_IO
>      (Vector_Ix  => Integer,
>       Vector     => Vector,   -- the error is here
>       Matrix_Io  => Matrix_IO) ;

Yes, I saw that problem too and many times. To me it looks like a compiler
bug, but wait what our language lawyers will say.

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



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

* Re: Two generic families: I'm confused by error msg
  2004-11-03  5:40 Two generic families: I'm confused by error msg John Woodruff
  2004-11-03  8:46 ` Dmitry A. Kazakov
@ 2004-11-03 10:01 ` Martin Dowie
  2004-11-03 13:37   ` Georg Bauhaus
  2004-11-05  3:45   ` John Woodruff
  1 sibling, 2 replies; 5+ messages in thread
From: Martin Dowie @ 2004-11-03 10:01 UTC (permalink / raw)


John Woodruff wrote:
> --------------------------------------------------------------------------
> -- If this line "subtype Real is Unreal"  is ABSENT, then error at
> line 26:
> -- Component subtype of actual does not match that of formal "Vector"
> --------------------------------------------------------------------------
>    subtype Real is Unreal;

Looks like a GNAT-ism. ObjectAda 7.2.2 is fine with or without the subtype.





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

* Re: Two generic families: I'm confused by error msg
  2004-11-03 10:01 ` Martin Dowie
@ 2004-11-03 13:37   ` Georg Bauhaus
  2004-11-05  3:45   ` John Woodruff
  1 sibling, 0 replies; 5+ messages in thread
From: Georg Bauhaus @ 2004-11-03 13:37 UTC (permalink / raw)


Martin Dowie <martin.dowie@baesystems.com> wrote:
: John Woodruff wrote:
:> --------------------------------------------------------------------------
:> -- If this line "subtype Real is Unreal"  is ABSENT, then error at
:> line 26:
:> -- Component subtype of actual does not match that of formal "Vector"
:> --------------------------------------------------------------------------
:>    subtype Real is Unreal;
: 
: Looks like a GNAT-ism. ObjectAda 7.2.2 is fine with or without the subtype.

(A very speculative guess: Having seen several cases of "behavior only
noticed when something is mentioned" recently, does GNAT adopt a C++
style of detecting things on use? :-)


-- Georg



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

* Re: Two generic families: I'm confused by error msg
  2004-11-03 10:01 ` Martin Dowie
  2004-11-03 13:37   ` Georg Bauhaus
@ 2004-11-05  3:45   ` John Woodruff
  1 sibling, 0 replies; 5+ messages in thread
From: John Woodruff @ 2004-11-05  3:45 UTC (permalink / raw)


"Martin Dowie" <martin.dowie@baesystems.com> wrote in message news:<4188ab00$1_1@baen1673807.greenlnk.net>...
> John Woodruff wrote:
> > --------------------------------------------------------------------------
> > -- If this line "subtype Real is Unreal"  is ABSENT, then error at
> > line 26:
> > -- Component subtype of actual does not match that of formal "Vector"
> > --------------------------------------------------------------------------
> >    subtype Real is Unreal;
> 
> Looks like a GNAT-ism. ObjectAda 7.2.2 is fine with or without the subtype.

Dmitry's suggestion was such a good one, that when I tried it the
original symptom disappeared!

It seems that the issue was likely an artifact of gnat 3.15p.  I
pretty-much-always avoid attributing my errors to compiler glitches,
but this time ...

Thanks, gents

John



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

end of thread, other threads:[~2004-11-05  3:45 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-11-03  5:40 Two generic families: I'm confused by error msg John Woodruff
2004-11-03  8:46 ` Dmitry A. Kazakov
2004-11-03 10:01 ` Martin Dowie
2004-11-03 13:37   ` Georg Bauhaus
2004-11-05  3:45   ` John Woodruff

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