comp.lang.ada
 help / color / mirror / Atom feed
From: Adam Beneschan <adam@irvine.com>
Subject: Re: Weird behavior with modular type
Date: Tue, 7 Feb 2012 10:15:27 -0800 (PST)
Date: 2012-02-07T10:15:27-08:00	[thread overview]
Message-ID: <c2526dca-c168-4497-b880-f6458c444e53@h6g2000yqk.googlegroups.com> (raw)
In-Reply-To: op.v9bjzvhzule2fv@douda-yannick

On Feb 7, 9:24 am, Yannick Duchêne (Hibou57)
<yannick_duch...@yahoo.fr> wrote:
> Hi all,
>
> I expect the test to be shortened enough.
>
> The following compiles and run without an error:
>
>     with Ada.Text_IO;
>     procedure Bug is
>
>        type Modular_Type is mod 256;
>        subtype Index_Type is Natural range 1 .. 3;
>
>        E : Modular_Type := 255;
>        I : Index_Type := 3;
>        B : Boolean;
>
>     begin
>        B := E < 10 ** I;
>        Ada.Text_IO.Put_Line (Boolean'Image (B));
>     end Bug;
>
> It prints "FALSE"
>
> Now, the following, with added "Natural'Pos (...)", did not even compile:
>
>     with Ada.Text_IO;
>     procedure Bug is
>
>        type Modular_Type is mod 256;
>        subtype Index_Type is Natural range 1 .. 3;
>
>        E : Modular_Type := 255;
>        I : Index_Type := 3;
>        B : Boolean;
>
>     begin
>        B := E < Natural'Pos (10 ** I);
>        Ada.Text_IO.Put_Line (Boolean'Image (B));
>     end Bug;
>
> That's counter intuitive. I would expect the first to behave as the
> second, with either a failure to compile or range error at run-time.
>
> Someone remember a pointer to the most relevant part in the RM? I may be a
> bit tired, could not find it.
>
> Seems in the first case, the left-side of "E < 10 ** I" has a modulo
> applied, or else, I see no way to explain the result, "FALSE". But why
> would this modulo be applied with a subtype of Natural and not with an
> Universal_Integer?
>
> I don't like it (and must be idiot in some way, don't you think?).

I think my first attempt to respond may have failed... retrying...

In the first example, "**" is a function whose left operand is the
modular type Modular_Type, and whose right operand has type Integer
(actually Integer'Base).  Unlike most other binary operators, whose
two operands are of the same type, "**" always has a right operand of
type Integer [and sometimes subtype Natural], regardless of the type
of the left operand.  Perhaps that's what's confusing you?  Anyway,
this means that in 10**I, the type of 10 is the modular type
Modular_Type, which means that "**" is a modular operation, which
wraps around.

In the second example, the left operand of "**" has type Integer,
because of the Natural'Pos which means that the expression inside
parentheses has to be of type Integer (subtype Natural).  Thus, the
value of Natural'Pos (10**I) is 1024--this operation doesn't wrap
around.  And to use this value as an operand to "<", it has to be
converted to type Modular_Type, which will get a Constraint_Error
because 1024 is out of range of the modular type.  (Type conversions
don't automatically wrap.)  So this program, unlike the first, should
get a Constraint_Error.  It should compile, though; it appears to be a
compiler bug that it didn't compile.

                             -- Adam





  parent reply	other threads:[~2012-02-07 18:17 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-02-07 17:24 Weird behavior with modular type Yannick Duchêne (Hibou57)
2012-02-07 17:49 ` Jeffrey Carter
2012-02-07 18:06   ` Adam Beneschan
2012-02-07 18:21     ` Yannick Duchêne (Hibou57)
2012-02-08  6:53       ` J-P. Rosen
2012-02-08 15:29         ` Adam Beneschan
2012-02-09  3:37       ` Randy Brukardt
2012-02-07 18:15   ` Yannick Duchêne (Hibou57)
2012-02-07 17:53 ` Adam Beneschan
2012-02-07 17:54 ` Gautier write-only
2012-02-07 18:23   ` Yannick Duchêne (Hibou57)
2012-02-09  3:40     ` Randy Brukardt
2012-02-07 18:02 ` Adam Beneschan
2012-02-07 18:32   ` Georg Bauhaus
2012-02-07 18:13 ` Yannick Duchêne (Hibou57)
2012-02-07 18:15 ` Adam Beneschan [this message]
2012-02-07 18:35   ` Yannick Duchêne (Hibou57)
2012-02-07 18:40     ` Adam Beneschan
2012-02-07 19:22       ` Yannick Duchêne (Hibou57)
replies disabled

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