* Re: Untyped "constant"?
@ 1991-09-24 21:22 Norman H . Cohen
0 siblings, 0 replies; 5+ messages in thread
From: Norman H . Cohen @ 1991-09-24 21:22 UTC (permalink / raw)
In article <1991Sep23.164446.13412@aplcen.apl.jhu.edu>,
gralia@ddsdx2.jhuapl.edu (Mars J. Gralia) writes:
|> a) Suppose I have a compiler/RTE in which integer'last is 32_767.
|> b) And suppose the system has a long_integer type in which
|> long_integer'last = 2_147_483_647.
|> c) And suppose I have the program:
|> procedure Named1 is
|> X : constant := integer'last+5;
|> begin
|> for j in 1..X loop
|> null;
|> end loop;
|> end Named1;
|>
|> d) Finally, suppose the "loop" variables are really used. E.g., by
|> disabling optimization when I invoke the compiler.
|>
|>
|> Then: is this a legal program or not?
As Dave Collard points out, Integer'Last+5 is of type Integer, and the
initial-value expression for a named number must be of type
universal_integer, so this is not legal. However, we can get around this
technicality by rewriting the initial-value expression as
Integer'Pos(Integer'Last)+5
since the 'Pos attribute yields a universal_integer result.
However, RM 3.6.1(2), when deciphered, states that a discrete range in
which each bound is either a named number, an integer literal, or an
attribute with a universal_integer value has each bound implicitly
converted to type Integer. Thus 1 .. X is equivalent to
Integer(1) .. Integer(X). Since X has a value greater than Integer'Last,
the conversion in the upper bound raises Constraint_Error.
|> Some compilers, admittedly archaic or for very small computers,
|> will raise a Constraint_Error, thereby forcing me to say:
|> X : constant long_integer := integer'last+5;
|>
|> I would prefer the program given above, because it would appear to be
|> more portable.
Well, it's portable in the sense that it is guaranteed to raise
Constraint_Error (except when the loop is optimized away, in which case
the implicit conversion that would have raised Constraint_Error can be
eliminated pursuant to RM 11.6(7)).
A more useful form of portability would come from defining your own
integer type with the required range:
procedure Named1 is
type Loop_Index_Type is range 1 .. Integer'Last+5;
begin
for J in Loop_Index_Type loop
null;
end loop;
end Named1;
Implementations for which Integer'Last = System.Max_Int will reject this
at compile time. Implementations for which System.Max_Int >=
Integer'Last+5 are guaranteed to execute without an exception.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Untyped "constant"?
@ 1991-09-27 14:13 cis.ohio-state.edu!zaphod.mps.ohio-state.edu!rpi!bu.edu!inmet!stt
0 siblings, 0 replies; 5+ messages in thread
From: cis.ohio-state.edu!zaphod.mps.ohio-state.edu!rpi!bu.edu!inmet!stt @ 1991-09-27 14:13 UTC (permalink / raw)
Re: Untyped "constant"? by gralia@ddsdx2.jhuapl.edu
> I have a question about Ada. It has to do with "named numbers" and
> their subsequent use.
>
> a) Suppose I have a compiler/RTE in which integer'last is 32_767.
> b) And suppose the system has a long_integer type in which
> long_integer'last = 2_147_483_647.
> c) And suppose I have the program:
> procedure Named1 is
> X : constant := integer'last+5;
This is not legal Ada, since the type of integer'last+5 is integer,
not universal-integer. This can be made legal as follows:
X : constant := Integer'POS(Integer'LAST) + 5;
This works because int-type'POS returns the value of its operand
converted to univ-integer type.
> begin
> for j in 1..X loop
This will not work in general, because when both bounds can
be interpreted as universal integer (as above), the
type of J will be STANDARD.INTEGER. Therefore, you should
get CONSTRAINT_ERROR on this line.
> null;
> end loop;
> end Named1;
>
> d) Finally, suppose the "loop" variables are really used. E.g., by
> disabling optimization when I invoke the compiler.
>
>
> Then: is this a legal program or not?
No, it is not legal as written (see above for correction).
> Thanks,
> Mars Gralia
>
>
>
> DISCUSSION
>
> Some compilers, admittedly archaic or for very small computers,
> will raise a Constraint_Error, thereby forcing me to say:
> X : constant long_integer := integer'last+5;
This is also illegal, because, again, the type on the right
is Integer, not Long_Integer. It could be made legal as follows:
X : constant Long_Integer := Long_Integer(Integer'LAST) + 5;
In any case, *all* compilers should raise CONSTRAINT_ERROR
if you say "for J in 1..X" when X is a universal-integer > Integer'LAST.
By the way, "X : constant := ..." must *never* raise constraint error
on the assigment, though some subexpression on the right hand side
might raise constraint error, presuming it is inside a 'POS (as above).
All static univ-integer calculations must be performed to arbitrary
range (no overflow allowed).
> I would prefer the program given above, because it would appear to be
> more portable.
By using 'POS, you can make the declaration of X work without
constraint error, but you will still get a constraint error
on the loop unless you specify a type for J that includes
the low and high bound. Furthermore, there exist compilers where
Integer'LAST is equal to SYSTEM.MAX_INT, and in that case
you are out of luck.
S. Tucker Taft stt@inmet.inmet.com
Intermetrics, Inc.
Cambridge, MA 01238
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Untyped "constant"?
@ 1991-09-24 15:08 John Goodenough
0 siblings, 0 replies; 5+ messages in thread
From: John Goodenough @ 1991-09-24 15:08 UTC (permalink / raw)
In article Untyped "constant"? of 23 Sep 91 16:44:46 GMT
gralia@ddsdx2.jhuapl.edu (Mars J. Gralia) writes:
>a) Suppose I have a compiler/RTE in which integer'last is 32_767.
>b) And suppose the system has a long_integer type in which
> long_integer'last = 2_147_483_647.
>c) And suppose I have the program:
> procedure Named1 is
> X : constant := integer'last+5;
> begin
> for j in 1..X loop
> null;
> end loop;
> end Named1;
>Then: is this a legal program or not?
The named number declaration is illegal, because INTEGER'LAST is of type
INTEGER (see 3.5(9)) and the initialization expression for a named number must
have a universal type (see 3.2.2(1)). So suppose you fix this problem by
writing:
X : constant := INTEGER'POS(INTEGER'LAST) + 5;
Note that INTEGER'POS returns a value of type universal integer (see 3.5.5(6)),
INTEGER'LAST is a static expression (4.9(8)), and INTEGER'POS(INTEGER'LAST) is
static (4.9(8)).
But this doesn't solve the problem, since
for J in 1..X loop
is now equivalent to
for J in 1..32_767+5 loop
The upper and lower bounds of this range will be converted to the predefined
INTEGER type (3.6.1(2)), and this conversion will raise CONSTRAINT_ERROR.
John B. Goodenough Goodenough@sei.cmu.edu
Software Engineering Institute 412-268-6391
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Untyped "constant"?
@ 1991-09-23 17:32 agate!spool.mu.edu!caen!uakari.primate.wisc.edu!aplcen!ddsdx2.jhuapl.edu!
0 siblings, 0 replies; 5+ messages in thread
From: agate!spool.mu.edu!caen!uakari.primate.wisc.edu!aplcen!ddsdx2.jhuapl.edu! @ 1991-09-23 17:32 UTC (permalink / raw)
In <1991Sep23.164446.13412@aplcen.apl.jhu.edu> gralia@ddsdx2.jhuapl.edu (Mars J
. Gralia) writes:
>I have a question about Ada. It has to do with "named numbers" and
>their subsequent use.
>a) Suppose I have a compiler/RTE in which integer'last is 32_767.
>b) And suppose the system has a long_integer type in which
> long_integer'last = 2_147_483_647.
>c) And suppose I have the program:
> procedure Named1 is
> X : constant := integer'last+5;
> begin
> for j in 1..X loop
> null;
> end loop;
> end Named1;
>d) Finally, suppose the "loop" variables are really used. E.g., by
> disabling optimization when I invoke the compiler.
>Then: is this a legal program or not?
Nope. The declaration
X : constant := integer'last+5;
is illegal. The expression given for the initialization of a number
declaration must always be an expression of universal type. The attribute
integer'last returns an integer, not a universal_integer. Therefore, the
result of the addition will resolve to integer. Universal types must be made
up entirely of expressions involving only other universal types (literals and
other named numbers).
--Thor
>DISCUSSION
>Some compilers, admittedly archaic or for very small computers,
>will raise a Constraint_Error, thereby forcing me to say:
> X : constant long_integer := integer'last+5;
This, being a constant declaration and not a number declaration should
be fine if you convert the result of 'last to long_integer:
X : constant long_integer := long_integer(integer'last) + 5.
>I would prefer the program given above, because it would appear to be
>more portable.
>
--Thor
collard@capsrv.jhuapl.edu
^ permalink raw reply [flat|nested] 5+ messages in thread
* Untyped "constant"?
@ 1991-09-23 16:44 cis.ohio-state.edu!zaphod.mps.ohio-state.edu!uakari.primate.wisc.edu!aplc
0 siblings, 0 replies; 5+ messages in thread
From: cis.ohio-state.edu!zaphod.mps.ohio-state.edu!uakari.primate.wisc.edu!aplc @ 1991-09-23 16:44 UTC (permalink / raw)
I have a question about Ada. It has to do with "named numbers" and
their subsequent use.
a) Suppose I have a compiler/RTE in which integer'last is 32_767.
b) And suppose the system has a long_integer type in which
long_integer'last = 2_147_483_647.
c) And suppose I have the program:
procedure Named1 is
X : constant := integer'last+5;
begin
for j in 1..X loop
null;
end loop;
end Named1;
d) Finally, suppose the "loop" variables are really used. E.g., by
disabling optimization when I invoke the compiler.
Then: is this a legal program or not?
Thanks,
Mars Gralia
DISCUSSION
Some compilers, admittedly archaic or for very small computers,
will raise a Constraint_Error, thereby forcing me to say:
X : constant long_integer := integer'last+5;
I would prefer the program given above, because it would appear to be
more portable.
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~1991-09-27 14:13 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1991-09-24 21:22 Untyped "constant"? Norman H . Cohen
-- strict thread matches above, loose matches on Subject: below --
1991-09-27 14:13 cis.ohio-state.edu!zaphod.mps.ohio-state.edu!rpi!bu.edu!inmet!stt
1991-09-24 15:08 John Goodenough
1991-09-23 17:32 agate!spool.mu.edu!caen!uakari.primate.wisc.edu!aplcen!ddsdx2.jhuapl.edu!
1991-09-23 16:44 cis.ohio-state.edu!zaphod.mps.ohio-state.edu!uakari.primate.wisc.edu!aplc
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox