comp.lang.ada
 help / color / mirror / Atom feed
* Question on modular types
@ 1997-01-04  0:00 Jerry van Dijk
  1997-01-04  0:00 ` Robert Dewar
                   ` (2 more replies)
  0 siblings, 3 replies; 11+ messages in thread
From: Jerry van Dijk @ 1997-01-04  0:00 UTC (permalink / raw)



Much to my supprise, the following compiles and runs:

   with Ada.Text_IO; use Ada.Text_IO;
   with Interfaces.C; use Interfaces.C;

   procedure Test is

      type My_Int is mod 2 ** 32;

      uint  : unsigned;
      A_Int : My_Int;

   begin

      uint := -1;                         -- Well, its a C type... :-)
      if uint'Valid then
         Put_Line ("uint Valid");
      else
         Put_Line ("uint Invalid");
      end if;

      A_Int := -1;                        -- (A)
      if A_Int'Valid then
         Put_Line ("A_Int Valid");
      else
         Put_Line ("A_Int Invalid");
      end if;

      A_Int := My_Int (uint);             -- (B)
      if A_Int'Valid then
         Put_Line ("A_Int Valid");
      else
         Put_Line ("A_Int Invalid");
      end if;

      if A_Int = -1 then                  -- Compare works ???
         Put_Line ("A_Int is -1 !?!");
      else
         Put_Line ("A_Int is" & A_Int'Img);
      end if;

   end Test;

Clearly, all values are invalid, and that without inporting any values
from the outside world.

I expected the line marked (A) to give an compilation error because of
3.5.4(10) or else the line marked (B) to generate a constraint error
because of 3.5.4(19).

I suspect that (B) doesn't fail since type_conversion is not one of the
operations mentioned in 4.5, and that (A) doesn't result in an error
because of the second sentence in 3.5.4(10) since its base type is
Integer
according to 3.5.4(1).

Still, its not what I expected, so perhaps does someone has an
explantion ?

(BTW using GNAT 3.05 on DOS and 3.07 on Linux, with -gnato)

Thanks,
Jerry.





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

* Re: Question on modular types
  1997-01-04  0:00 Question on modular types Jerry van Dijk
@ 1997-01-04  0:00 ` Robert Dewar
  1997-01-06  0:00   ` Jerry van Dijk
  1997-01-06  0:00 ` Joel VanLaven
  1997-01-08  0:00 ` Stan and/or Jill Grimes
  2 siblings, 1 reply; 11+ messages in thread
From: Robert Dewar @ 1997-01-04  0:00 UTC (permalink / raw)



Jerry said

"Clearly, all values are invalid, and that without inporting any values
from the outside world."

No! If it is clear to you, you need your Ada prescription fixed :-)

   A := -1;

-1 is not a literal here, it is an expression, and the unary minus
operator is a modular unary minus. This is a useful, intended, and
clearly documented facility.

Note that if you did

    Minus1 : constant := -1;

    uint := minus1;

then you would get the constraint error:

     1. procedure b is
     2.    type x is mod 256;
     3.    k : constant := -1;
     4.    r : x;
     5.
     6. begin
     7.    r := -1;
     8.    r := k;
                |
        >>> value not in range of type "x" defined at line 2
        >>> static expression raises "constraint_error"

     9. end;

That's because the minus operator in line 3 is the one that works on
root integer (I would really prefer to say universal integer, but I
think root integer is correct), and really does give minus one, and
that is indeed out of range, as detected (statically in this particular
case).

Note that for modular types of sizes corresponding to hardware sizes
(typically 8,16,32) there will of course be no invalid values ever.





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

* Re: Question on modular types
  1997-01-04  0:00 ` Robert Dewar
@ 1997-01-06  0:00   ` Jerry van Dijk
  1997-01-06  0:00     ` Robert A Duff
  0 siblings, 1 reply; 11+ messages in thread
From: Jerry van Dijk @ 1997-01-06  0:00 UTC (permalink / raw)




Robert Dewar <dewar@merv.cs.nyu.edu> wrote in article
<dewar.852430006@merv>...

>> "Clearly, all values are invalid, and that without inporting any
>> values from the outside world."

> No! If it is clear to you, you need your Ada prescription fixed :-)

Mmmm, I'm afraid if I asked my docter for that, I might wind up with a
totally different prescription... :-)

>   A := -1;

> -1 is not a literal here, it is an expression, and the unary minus
> operator is a modular unary minus. This is a useful, intended, and
> clearly documented facility.

Yes, I found it:

4.5.4(3): For modular integer types, the unary adding operator -, when
          given a nonzero operand, returns the result of subtracting
          the value of the operand from the modulus; for a zero
          operand, the result is zero.

   type Byte is mod 256;
   A_Byte : Byte;

   A_Byte := -1; -- means A_Byte := Byte (Byte'Modulus - 1);

However, 4.5.4(1) mentions that all unary adding operators have their
conventional
meaning, and since the note is not technically part of the standard,
that leaves
me wondering wat that 'conventional meaning; might be. And
'conventional' to whom ?

Thanks,
Jerry.





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

* Re: Question on modular types
  1997-01-04  0:00 Question on modular types Jerry van Dijk
  1997-01-04  0:00 ` Robert Dewar
@ 1997-01-06  0:00 ` Joel VanLaven
  1997-01-08  0:00 ` Stan and/or Jill Grimes
  2 siblings, 0 replies; 11+ messages in thread
From: Joel VanLaven @ 1997-01-06  0:00 UTC (permalink / raw)



  I think I know what the confusion is.  The problem is that -1 is not a
literal!  -1 is actually -(1).  1 is a perfectly valid uint and the "-"
operator is the modular "-" and so reduces the result back into the valid
range.  The problem really comes from the problem that we humans take -1
to be a literal but the language doesn't.  I have been confused by that
a few times myself, but in cases where it was more obvious what the problem
was (subtyping an integer type and then not finding the "-" operator).

-- 
-- Joel VanLaven




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

* Re: Question on modular types
  1997-01-06  0:00   ` Jerry van Dijk
@ 1997-01-06  0:00     ` Robert A Duff
  1997-01-08  0:00       ` Robert Dewar
  0 siblings, 1 reply; 11+ messages in thread
From: Robert A Duff @ 1997-01-06  0:00 UTC (permalink / raw)



In article <01bbfbf8$4fda7a40$5f2d5c8b@jerryware>,
Jerry van Dijk <jvandyk@ibm.net> wrote:
>However, 4.5.4(1) mentions that all unary adding operators have their
>conventional
>meaning, and since the note is not technically part of the standard,
>that leaves
>me wondering wat that 'conventional meaning; might be. And
>'conventional' to whom ?

Convention to those who have read the RM, and understand that arithmetic
on modular types is always wrap-around arithmetic.  ;-)

We called them "modular", and not "unsigned", after all.  If you just
want nonnegative values, then "type T is range 0..2**8-1;" or some such
thing is more likely what you want.  The problem with *that*, of course,
is that it doesn't work if the upper bound is higher than the upper
bound of the largest *signed* integer type -- e.g. "type T is range
0..2**64-1;" will not work in GNAT.

- Bob




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

* Re: Question on modular types
  1997-01-06  0:00     ` Robert A Duff
@ 1997-01-08  0:00       ` Robert Dewar
  1997-01-09  0:00         ` Robert A Duff
  1997-01-09  0:00         ` Tucker Taft
  0 siblings, 2 replies; 11+ messages in thread
From: Robert Dewar @ 1997-01-08  0:00 UTC (permalink / raw)



iRobert Duff said

"Convention to those who have read the RM, and understand that arithmetic
on modular types is always wrap-around arithmetic.  ;-)"


I cannot find anywhere in the RM where it says that unary minus has
wrap around arithmetic semantics, can you point me to the place where
this is said.





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

* Re: Question on modular types
  1997-01-04  0:00 Question on modular types Jerry van Dijk
  1997-01-04  0:00 ` Robert Dewar
  1997-01-06  0:00 ` Joel VanLaven
@ 1997-01-08  0:00 ` Stan and/or Jill Grimes
  2 siblings, 0 replies; 11+ messages in thread
From: Stan and/or Jill Grimes @ 1997-01-08  0:00 UTC (permalink / raw)



First in your revealing, you did not mention what the output of the put
statements 
gave you, I suspect it was 2^32... If not, try reducing the range and see
if you get an constraint error then. Many bugs are quite creatures, perhaps
the -1 was interpreted first as 32 ones then as an unsigned integer.Because
I do not have the output this is just guessing. (no time to try, just time
to chat).
   Carryon, we will see you in the trenches... or where ever..StanGz

Jerry van Dijk <jvandyk@ibm.net> wrote in article
<01bbfa96$66d516a0$8d2d5c8b@jerryware>...





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

* Re: Question on modular types
  1997-01-08  0:00       ` Robert Dewar
  1997-01-09  0:00         ` Robert A Duff
@ 1997-01-09  0:00         ` Tucker Taft
  1997-01-11  0:00           ` Robert Dewar
  1 sibling, 1 reply; 11+ messages in thread
From: Tucker Taft @ 1997-01-09  0:00 UTC (permalink / raw)



Robert Dewar (dewar@merv.cs.nyu.edu) wrote:

: Robert Duff said

:   "Convention to those who have read the RM, and understand that arithmetic
:    on modular types is always wrap-around arithmetic.  ;-)"


: I cannot find anywhere in the RM where it says that unary minus has
: wrap around arithmetic semantics, can you point me to the place where
: this is said.

Two places: 3.5.4(19) gives the general rule that anytime the result
of a predefined operator of a modular type is outside the base range of
the type, the result is reduced modulo the modulus of the type.  
The second is in a note, 4.5.4(3).

-Tucker Taft   stt@inmet.com   http://www.inmet.com/~stt/
Intermetrics, Inc.  Cambridge, MA  USA




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

* Re: Question on modular types
  1997-01-08  0:00       ` Robert Dewar
@ 1997-01-09  0:00         ` Robert A Duff
  1997-01-09  0:00         ` Tucker Taft
  1 sibling, 0 replies; 11+ messages in thread
From: Robert A Duff @ 1997-01-09  0:00 UTC (permalink / raw)



In article <dewar.852779585@merv>, Robert Dewar <dewar@merv.cs.nyu.edu> wrote:
>iRobert Duff said
>
>"Convention to those who have read the RM, and understand that arithmetic
            ^al
>on modular types is always wrap-around arithmetic.  ;-)"

By the way, the above smiley was meant to imply some sympathy for the
earlier poster's view, that calling wrap-around arithmetic
"conventional" is obfuscatory.  It wasn't intended as an admonishment to
RTFM.

>I cannot find anywhere in the RM where it says that unary minus has
>wrap around arithmetic semantics, can you point me to the place where
>this is said.

3.5.4(19).

- Bob




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

* Re: Question on modular types
  1997-01-09  0:00         ` Tucker Taft
@ 1997-01-11  0:00           ` Robert Dewar
  1997-01-12  0:00             ` Joel VanLaven
  0 siblings, 1 reply; 11+ messages in thread
From: Robert Dewar @ 1997-01-11  0:00 UTC (permalink / raw)



Tuck said (answering my query about unary minus on modular types)

"Two places: 3.5.4(19) gives the general rule that anytime the result
of a predefined operator of a modular type is outside the base range of
the type, the result is reduced modulo the modulus of the type.
The second is in a note, 4.5.4(3)."


Ah, yes, silly me, expecting to find the semantics of unary minus in
the section on operator semantics (or at least in the chapter on
expression semantics) :-)

Yes, I saw the note, but the question was where that note came from,
especially since 4.5.4(1) says that "-" on modular types has its
conventional meaning (which I can only take as the mathematical
meaning), and hence contradicts 3.5.4(19).





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

* Re: Question on modular types
  1997-01-11  0:00           ` Robert Dewar
@ 1997-01-12  0:00             ` Joel VanLaven
  0 siblings, 0 replies; 11+ messages in thread
From: Joel VanLaven @ 1997-01-12  0:00 UTC (permalink / raw)



Robert Dewar (dewar@merv.cs.nyu.edu) wrote:
: Tuck said (answering my query about unary minus on modular types)

: "Two places: 3.5.4(19) gives the general rule that anytime the result
: of a predefined operator of a modular type is outside the base range of
: the type, the result is reduced modulo the modulus of the type.
: The second is in a note, 4.5.4(3)."


: Ah, yes, silly me, expecting to find the semantics of unary minus in
: the section on operator semantics (or at least in the chapter on
: expression semantics) :-)

: Yes, I saw the note, but the question was where that note came from,
: especially since 4.5.4(1) says that "-" on modular types has its
: conventional meaning (which I can only take as the mathematical
: meaning), and hence contradicts 3.5.4(19).

  It seems to me that the conventional mathematical meaning of "-" for
a modular type IS the definition given in the note.  I am sure that what
is the conventional, mathematical meaning is if not the modular
definition then at least debatable, and if debatable then the note is
intended to show which side of the debate the RM/RM writers came out on.
Perhaps it could be worded better so as not to cause confusion.
-- 
-- Joel VanLaven




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

end of thread, other threads:[~1997-01-12  0:00 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1997-01-04  0:00 Question on modular types Jerry van Dijk
1997-01-04  0:00 ` Robert Dewar
1997-01-06  0:00   ` Jerry van Dijk
1997-01-06  0:00     ` Robert A Duff
1997-01-08  0:00       ` Robert Dewar
1997-01-09  0:00         ` Robert A Duff
1997-01-09  0:00         ` Tucker Taft
1997-01-11  0:00           ` Robert Dewar
1997-01-12  0:00             ` Joel VanLaven
1997-01-06  0:00 ` Joel VanLaven
1997-01-08  0:00 ` Stan and/or Jill Grimes

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