comp.lang.ada
 help / color / mirror / Atom feed
* Generic warning
@ 2008-06-17 16:44 Sébastien Morand
  2008-06-17 17:17 ` Dmitry A. Kazakov
  2008-06-17 18:20 ` Jeffrey R. Carter
  0 siblings, 2 replies; 15+ messages in thread
From: Sébastien Morand @ 2008-06-17 16:44 UTC (permalink / raw)


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi,

I'm implementing a B-Tree in Ada using indefinite private type:

generic
   --  Element to be stored in the btree
   type Element (<>) is private;
   --  Null element when not found
   Element_Null: Element;
   --  Key of the element in the B-Tree wich must be compareable
   type Key (<>) is private;
   with function "<"(k1: in Key; k2: in Key) return Boolean;
   with function ">"(k1: in Key; k2: in Key) return Boolean;
   with procedure Put_Line(k: in Key);
   --  Order of the B-Tree
   Order: Positive;
package SCMAL.Tools.BTrees is

   --  Omitted

end SCMAL.Tools.BTrees;


When I create an instance of the package using the following code:

 9   package IT is new SCMAL.Tools.BTrees(
10      Element => Integer,
11      Element_Null => 0,
12      Key => Integer,
13      "<" => "<",
14      ">" => ">",
15      Put_Line => Put_Line,
16      Order => 1
17   );

I got the following warnings:
btreetests.adb:9:04: warning: in instantiation at scmal-tools-btrees.adb:159
btreetests.adb:9:04: warning: value not in range of subtype of
"Standard.Integer" defined at scmal-tools-btrees.ads:82, instance at line 9
btreetests.adb:9:04: warning: "Constraint_Error" will be raised at run time
btreetests.adb:9:04: warning: in instantiation at scmal-tools-btrees.adb:160
btreetests.adb:9:04: warning: value not in range of subtype of
"Standard.Integer" defined at scmal-tools-btrees.ads:83, instance at line 9
btreetests.adb:9:04: warning: "Constraint_Error" will be raised at run time
btreetests.adb:9:04: warning: in instantiation at scmal-tools-btrees.adb:161
btreetests.adb:9:04: warning: value not in range of subtype of
"Standard.Integer" defined at scmal-tools-btrees.ads:84, instance at line 9
btreetests.adb:9:04: warning: "Constraint_Error" will be raised at run time
btreetests.adb:9:04: warning: in instantiation at scmal-tools-btrees.adb:177
btreetests.adb:9:04: warning: value not in range of subtype of
"Standard.Integer" defined at scmal-tools-btrees.ads:82, instance at line 9
btreetests.adb:9:04: warning: "Constraint_Error" will be raised at run time
btreetests.adb:9:04: warning: in instantiation at scmal-tools-btrees.adb:178
btreetests.adb:9:04: warning: value not in range of subtype of
"Standard.Integer" defined at scmal-tools-btrees.ads:83, instance at line 9
btreetests.adb:9:04: warning: "Constraint_Error" will be raised at run time

but I run time it works fine and I have no constraint error.

Where theses warnings come from? How to correct my code?
Is there a way to avoid to take the default "<" and ">" function?

Thanks by advance,
Sebastien
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (Cygwin)

iD8DBQFIV+na+zV9xm4PlDQRAn1vAJ4hWuphvyP5DAU/zTB2iH4jP+DuMQCdF252
4ZGus6+yxjCwqug3lqycgsY=
=XSuj
-----END PGP SIGNATURE-----



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

* Re: Generic warning
  2008-06-17 16:44 Generic warning Sébastien Morand
@ 2008-06-17 17:17 ` Dmitry A. Kazakov
  2008-06-17 18:33   ` Sébastien Morand
  2008-06-17 18:20 ` Jeffrey R. Carter
  1 sibling, 1 reply; 15+ messages in thread
From: Dmitry A. Kazakov @ 2008-06-17 17:17 UTC (permalink / raw)


On Tue, 17 Jun 2008 16:44:10 +0000, S�bastien Morand wrote:

> When I create an instance of the package using the following code:
> 
>  9   package IT is new SCMAL.Tools.BTrees(
> 10      Element => Integer,
> 11      Element_Null => 0,
> 12      Key => Integer,

As a guess, there could be a conflict upon instantiation when Element and
Key are the same type. I had once such a problem. GNAT always had problems
with generics.

> Where theses warnings come from? How to correct my code?

Maybe it is just a compiler bug. But you should post a complete compilable
code.

> Is there a way to avoid to take the default "<" and ">" function?

I don't see you using defaults for the formal parameters "<" and ">". You
have:

   with function "<"(k1: in Key; k2: in Key) return Boolean;
   with function ">"(k1: in Key; k2: in Key) return Boolean;

not

   with function "<"(k1: in Key; k2: in Key) return Boolean is <>;
   with function ">"(k1: in Key; k2: in Key) return Boolean is <>;

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



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

* Re: Generic warning
  2008-06-17 16:44 Generic warning Sébastien Morand
  2008-06-17 17:17 ` Dmitry A. Kazakov
@ 2008-06-17 18:20 ` Jeffrey R. Carter
  2008-06-18  8:55   ` Sébastien Morand
  1 sibling, 1 reply; 15+ messages in thread
From: Jeffrey R. Carter @ 2008-06-17 18:20 UTC (permalink / raw)


S�bastien Morand wrote:
> 
> I got the following warnings:
> btreetests.adb:9:04: warning: in instantiation at scmal-tools-btrees.adb:159
> btreetests.adb:9:04: warning: value not in range of subtype of
> "Standard.Integer" defined at scmal-tools-btrees.ads:82, instance at line 9
> btreetests.adb:9:04: warning: "Constraint_Error" will be raised at run time
> btreetests.adb:9:04: warning: in instantiation at scmal-tools-btrees.adb:160
> btreetests.adb:9:04: warning: value not in range of subtype of
> "Standard.Integer" defined at scmal-tools-btrees.ads:83, instance at line 9
> btreetests.adb:9:04: warning: "Constraint_Error" will be raised at run time
> btreetests.adb:9:04: warning: in instantiation at scmal-tools-btrees.adb:161
> btreetests.adb:9:04: warning: value not in range of subtype of
> "Standard.Integer" defined at scmal-tools-btrees.ads:84, instance at line 9
> btreetests.adb:9:04: warning: "Constraint_Error" will be raised at run time
> btreetests.adb:9:04: warning: in instantiation at scmal-tools-btrees.adb:177
> btreetests.adb:9:04: warning: value not in range of subtype of
> "Standard.Integer" defined at scmal-tools-btrees.ads:82, instance at line 9
> btreetests.adb:9:04: warning: "Constraint_Error" will be raised at run time
> btreetests.adb:9:04: warning: in instantiation at scmal-tools-btrees.adb:178
> btreetests.adb:9:04: warning: value not in range of subtype of
> "Standard.Integer" defined at scmal-tools-btrees.ads:83, instance at line 9
> btreetests.adb:9:04: warning: "Constraint_Error" will be raised at run time
> 
> but I run time it works fine and I have no constraint error.
> 
> Where theses warnings come from? How to correct my code?

We can't help you, because you haven't shown us the code for SCMAL.Tools.Btrees, 
or even for the specified lines.

Note that the ordered components of Ada.Containers have a similar data structure 
underlying them. Too bad Ada.Containers doesn't follow the Ada philosophy of 
providing basic building blocks that would make that data structure available.

Of course, Ada compilers have to have unbounded integer and real packages, and 
the standard doesn't make them available, either.

-- 
Jeff Carter
"I'm a kike, a yid, a heebie, a hook nose! I'm Kosher,
Mum! I'm a Red Sea pedestrian, and proud of it!"
Monty Python's Life of Brian
77



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

* Re: Generic warning
  2008-06-17 17:17 ` Dmitry A. Kazakov
@ 2008-06-17 18:33   ` Sébastien Morand
  2008-06-17 19:04     ` Dmitry A. Kazakov
  0 siblings, 1 reply; 15+ messages in thread
From: Sébastien Morand @ 2008-06-17 18:33 UTC (permalink / raw)


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

> Maybe it is just a compiler bug. But you should post a complete compilable
> code.

Here is a complete code with the same warning:

procedure Main is

   generic
      --  Element to be stored in the btree
      type Element (<>) is private;
      Order: Positive;
   package TestGen is

      type ElementPtr is access all Element;

      Buffer: array(1 .. Order*2) of ElementPtr;
      procedure Store(E: in Element; index: in Positive);

   end TestGen;

   package body TestGen is

      procedure Store(E: in Element; index: in Positive) is
      begin
         --  In my real procedure, index is calculated and there is some
         --  check. Index can't be > Order*2
         for i in Positive range Order+2 .. index-1 loop
            Buffer(i) := new Element'(E);
         end loop;
      end Store;

   end TestGen;

   package IntTestGen is new TestGen(
      Element => Integer,
      Order => 1
   );
   use IntTestGen;

begin

   Store(1, 4);

end Main;

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (Cygwin)

iD8DBQFIWAOF+zV9xm4PlDQRAqdzAJ9Oa+0fS1VgOMo8dypPhkDgpNC0pQCfWwC1
cCWtqImCosykrr1tzGBf8gY=
=J0c5
-----END PGP SIGNATURE-----



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

* Re: Generic warning
  2008-06-17 18:33   ` Sébastien Morand
@ 2008-06-17 19:04     ` Dmitry A. Kazakov
  2008-06-18  8:41       ` Sébastien Morand
  0 siblings, 1 reply; 15+ messages in thread
From: Dmitry A. Kazakov @ 2008-06-17 19:04 UTC (permalink / raw)


On Tue, 17 Jun 2008 18:33:42 +0000, S�bastien Morand wrote:

>> Maybe it is just a compiler bug. But you should post a complete compilable
>> code.

No, it is not. The compiler is correct.

> Here is a complete code with the same warning:
> 
> procedure Main is
> 
>    generic
>       --  Element to be stored in the btree
>       type Element (<>) is private;
>       Order: Positive;
>    package TestGen is
>       type ElementPtr is access all Element;
>       Buffer: array(1 .. Order*2) of ElementPtr;
>       procedure Store(E: in Element; index: in Positive);
>    end TestGen;
> 
>    package body TestGen is
>       procedure Store(E: in Element; index: in Positive) is
>       begin
>          --  In my real procedure, index is calculated and there is some
>          --  check. Index can't be > Order*2
>          for i in Positive range Order+2 .. index-1 loop
                                         
The problem is here. You instantiate TestGen with Order = 1. So the upper
bound of Buffer is 2. Therefore the range is 1+2..Index, which should cause
Constraint_Error in Buffer(I).

Bravo GNAT! (Though the warning message could be better)

>             Buffer(i) := new Element'(E);
>          end loop;
>       end Store;
>    end TestGen;
>    package IntTestGen is new TestGen(
>       Element => Integer,
>       Order => 1
>    );
>    use IntTestGen;
> begin
>    Store(1, 4);
> end Main;

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



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

* Re: Generic warning
  2008-06-17 19:04     ` Dmitry A. Kazakov
@ 2008-06-18  8:41       ` Sébastien Morand
  2008-06-18 14:16         ` Brian Drummond
  0 siblings, 1 reply; 15+ messages in thread
From: Sébastien Morand @ 2008-06-18  8:41 UTC (permalink / raw)


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1


> The problem is here. You instantiate TestGen with Order = 1. So the upper
> bound of Buffer is 2. Therefore the range is 1+2..Index, which should cause
> Constraint_Error in Buffer(I).

I understand, if Order = 1, then the code could never occur.

Anyway, There is a test just before in the real code that avoid the
impossible situation (index is between 1 and Order*2 always).

I could remove all the warnings except one that I had to change in a
While clause. But I put a comment in the code to explain why.

Thanks to show what I actually should have seen by myself.

Sebastien
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (Cygwin)

iD8DBQFIWMo7+zV9xm4PlDQRAuDMAJ9/qDRM1TL617fgDICj1JIYfgxcCgCgh2aI
Xsga9iFVpUAYBO5FKHJ6wXk=
=qGlV
-----END PGP SIGNATURE-----



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

* Re: Generic warning
  2008-06-17 18:20 ` Jeffrey R. Carter
@ 2008-06-18  8:55   ` Sébastien Morand
  0 siblings, 0 replies; 15+ messages in thread
From: Sébastien Morand @ 2008-06-18  8:55 UTC (permalink / raw)


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

> We can't help you, because you haven't shown us the code for
> SCMAL.Tools.Btrees, or even for the specified lines.
> 
> Note that the ordered components of Ada.Containers have a similar data
> structure underlying them. Too bad Ada.Containers doesn't follow the Ada
> philosophy of providing basic building blocks that would make that data
> structure available.
> 
> Of course, Ada compilers have to have unbounded integer and real
> packages, and the standard doesn't make them available, either.

Actually you are right I could use B-Tree or dynamic structure using
internal Ada library. But I'm still learning language and implementing a
B-Tree is a good way to learn the language and in the same time, to
review the algorithmic main topics (Stack, Queues, Linked List, Sort(s),
Base64 encoding, etc.).

Sebastien

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (Cygwin)

iD8DBQFIWM2L+zV9xm4PlDQRAh86AJ9HVZF+YUccbJw+GPJ+lFyr6/lpqwCfWSLO
OmFPkpEDXvnmQXylQ+CUKRo=
=uXOI
-----END PGP SIGNATURE-----



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

* Re: Generic warning
  2008-06-18  8:41       ` Sébastien Morand
@ 2008-06-18 14:16         ` Brian Drummond
  2008-06-18 14:32           ` Sébastien Morand
  0 siblings, 1 reply; 15+ messages in thread
From: Brian Drummond @ 2008-06-18 14:16 UTC (permalink / raw)


On Wed, 18 Jun 2008 08:41:31 +0000, S�bastien Morand
<seb.morand@gmail.com> wrote:

>-----BEGIN PGP SIGNED MESSAGE-----
>Hash: SHA1
>
>
>> The problem is here. You instantiate TestGen with Order = 1. So the upper
>> bound of Buffer is 2. Therefore the range is 1+2..Index, which should cause
>> Constraint_Error in Buffer(I).
>
>I understand, if Order = 1, then the code could never occur.
>
>Anyway, There is a test just before in the real code that avoid the
>impossible situation (index is between 1 and Order*2 always).

(Newbie qiestion, coming from VHDL, halfway through the Barnes book)

Can you not create a subtype of Positive to reflect that, and declare
both the array and index using that subtype?

- Brian




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

* Re: Generic warning
  2008-06-18 14:16         ` Brian Drummond
@ 2008-06-18 14:32           ` Sébastien Morand
  2008-06-18 14:39             ` Ed Falis
  2008-06-18 14:42             ` Dmitry A. Kazakov
  0 siblings, 2 replies; 15+ messages in thread
From: Sébastien Morand @ 2008-06-18 14:32 UTC (permalink / raw)


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

> (Newbie qiestion, coming from VHDL, halfway through the Barnes book)
> 
> Can you not create a subtype of Positive to reflect that, and declare
> both the array and index using that subtype?

I can't create a subtype using a parameter.

generic
	Order: Positive;
package Test is
	--  Can't work since Order is not defined at compile time
	type MyType is range 1 .. Order;
End Test;

Sebastien
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (Cygwin)

iD8DBQFIWRx8+zV9xm4PlDQRAlB6AJ9darSe1+aMBg1YbbUbilg8mZZ79wCdE/kY
bF8Rs7+gGRERMktKnmwp080=
=wYbq
-----END PGP SIGNATURE-----



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

* Re: Generic warning
  2008-06-18 14:32           ` Sébastien Morand
@ 2008-06-18 14:39             ` Ed Falis
  2008-06-18 14:42             ` Dmitry A. Kazakov
  1 sibling, 0 replies; 15+ messages in thread
From: Ed Falis @ 2008-06-18 14:39 UTC (permalink / raw)


Try:

generic
         Order: Positive;
package Test is
         subtype MyType is Positive range 1 .. Order;
End Test;

Subtype ranges based on Order are ok.



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

* Re: Generic warning
  2008-06-18 14:32           ` Sébastien Morand
  2008-06-18 14:39             ` Ed Falis
@ 2008-06-18 14:42             ` Dmitry A. Kazakov
  2008-06-18 15:10               ` Robert A Duff
  1 sibling, 1 reply; 15+ messages in thread
From: Dmitry A. Kazakov @ 2008-06-18 14:42 UTC (permalink / raw)


On Wed, 18 Jun 2008 14:32:28 +0000, S�bastien Morand wrote:

>> (Newbie qiestion, coming from VHDL, halfway through the Barnes book)
>> 
>> Can you not create a subtype of Positive to reflect that, and declare
>> both the array and index using that subtype?
> 
> I can't create a subtype using a parameter.

No, no, you mean a *type*.

> generic
> 	Order: Positive;
> package Test is
> 	--  Can't work since Order is not defined at compile time
> 	type MyType is range 1 .. Order;

This would be a type. But you do can create a subtype:

   subtype Index is range 1..Order; -- This is OK

Subtypes can be dynamically constrained.

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



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

* Re: Generic warning
  2008-06-18 14:42             ` Dmitry A. Kazakov
@ 2008-06-18 15:10               ` Robert A Duff
  2008-06-18 15:25                 ` Dmitry A. Kazakov
  0 siblings, 1 reply; 15+ messages in thread
From: Robert A Duff @ 2008-06-18 15:10 UTC (permalink / raw)


"Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de> writes:

> On Wed, 18 Jun 2008 14:32:28 +0000, S�bastien Morand wrote:
>
>>> (Newbie qiestion, coming from VHDL, halfway through the Barnes book)
>>> 
>>> Can you not create a subtype of Positive to reflect that, and declare
>>> both the array and index using that subtype?
>> 
>> I can't create a subtype using a parameter.
>
> No, no, you mean a *type*.
>
>> generic
>> 	Order: Positive;
>> package Test is
>> 	--  Can't work since Order is not defined at compile time
>> 	type MyType is range 1 .. Order;
>
> This would be a type. But you do can create a subtype:
>
>    subtype Index is range 1..Order; -- This is OK
>
> Subtypes can be dynamically constrained.

Yes.  You can also create a new type:

type Index is new Positive range 1..Order;

- Bob



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

* Re: Generic warning
  2008-06-18 15:10               ` Robert A Duff
@ 2008-06-18 15:25                 ` Dmitry A. Kazakov
  2008-06-18 15:38                   ` Ed Falis
  0 siblings, 1 reply; 15+ messages in thread
From: Dmitry A. Kazakov @ 2008-06-18 15:25 UTC (permalink / raw)


On Wed, 18 Jun 2008 11:10:29 -0400, Robert A Duff wrote:

>  You can also create a new type:
> 
> type Index is new Positive range 1..Order;

You are right. But this type is not enough "new". It is merely

   type P is new Positive;
   subtype Index is P range 1..Order;

The hidden idea of

   type Index is range 1..Order;

is to let the compiler choose the base according to the actual value of
Order. That would not work. Alas.

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



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

* Re: Generic warning
  2008-06-18 15:25                 ` Dmitry A. Kazakov
@ 2008-06-18 15:38                   ` Ed Falis
  2008-06-18 17:04                     ` Dmitry A. Kazakov
  0 siblings, 1 reply; 15+ messages in thread
From: Ed Falis @ 2008-06-18 15:38 UTC (permalink / raw)


On Wed, 18 Jun 2008 11:25:24 -0400, Dmitry A. Kazakov  
<mailbox@dmitry-kazakov.de> wrote:

> The hidden idea of
>   type Index is range 1..Order;
> is to let the compiler choose the base according to the actual value of
> Order. That would not work. Alas.


But do you figure that was the intent of the original poster?  I don't.



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

* Re: Generic warning
  2008-06-18 15:38                   ` Ed Falis
@ 2008-06-18 17:04                     ` Dmitry A. Kazakov
  0 siblings, 0 replies; 15+ messages in thread
From: Dmitry A. Kazakov @ 2008-06-18 17:04 UTC (permalink / raw)


On Wed, 18 Jun 2008 15:38:01 GMT, Ed Falis wrote:

> On Wed, 18 Jun 2008 11:25:24 -0400, Dmitry A. Kazakov  
> <mailbox@dmitry-kazakov.de> wrote:
> 
>> The hidden idea of
>>   type Index is range 1..Order;
>> is to let the compiler choose the base according to the actual value of
>> Order. That would not work. Alas.
> 
> But do you figure that was the intent of the original poster?

Of course no. It is the choice of one language construct over another, that
makes an Ada reader suggest something about the intent.

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



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

end of thread, other threads:[~2008-06-18 17:04 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-06-17 16:44 Generic warning Sébastien Morand
2008-06-17 17:17 ` Dmitry A. Kazakov
2008-06-17 18:33   ` Sébastien Morand
2008-06-17 19:04     ` Dmitry A. Kazakov
2008-06-18  8:41       ` Sébastien Morand
2008-06-18 14:16         ` Brian Drummond
2008-06-18 14:32           ` Sébastien Morand
2008-06-18 14:39             ` Ed Falis
2008-06-18 14:42             ` Dmitry A. Kazakov
2008-06-18 15:10               ` Robert A Duff
2008-06-18 15:25                 ` Dmitry A. Kazakov
2008-06-18 15:38                   ` Ed Falis
2008-06-18 17:04                     ` Dmitry A. Kazakov
2008-06-17 18:20 ` Jeffrey R. Carter
2008-06-18  8:55   ` Sébastien Morand

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