[comp.lang.ada] single valued variables

"SVDSD::PETCHER@ti-eg.CSNET".UUCP (04/03/87)

I am not an Ada expert, so correct me if I'm wrong, but...
When one declares a variable range to be (true..true) is that not in itself a 
contradiction?  A variable that can have only one value is, by definition, not 
a variable, but a constant.  In that context, it seems a compiler should 
create a constant, issue a diagnostic for any subsequent code that attempts to 
change the constant, and generate no executeable code for same.  I realize the 
most likely place for a single-valued variable to occur would be in 
instantiation of a generic, but if a generic so instantiated does not treat 
single-valued variables as constants then the program is basically in error.

Malcolm

firth@sei.cmu.edu.UUCP (04/06/87)

In article <8704031535.AA26545@ucbvax.Berkeley.EDU> "SVDSD::PETCHER@ti-eg.CSNET" writes:
>I am not an Ada expert, so correct me if I'm wrong, but...
>When one declares a variable range to be (true..true) is that not in itself a 
>contradiction?  A variable that can have only one value is, by definition, not 
>a variable, but a constant.  In that context, it seems a compiler should 
>create a constant, issue a diagnostic for any subsequent code that attempts to 
>change the constant, and generate no executeable code for same.  I realize the 
>most likely place for a single-valued variable to occur would be in 
>instantiation of a generic, but if a generic so instantiated does not treat 
>single-valued variables as constants then the program is basically in error.
>
>Malcolm

Thanks for an interesting question!  First, two Ada points.  It is
indeed legal to constrain a subrange to a single value, and then
to declare variables of that subrange.  It is then legal to assign
to those variables, but of course there is only one value you can
assign.

Indeed, the proverbial "optimising" compiler could allocate no
storage for the variables, since the value in them is known.  It
must still do the range checks, of course.  And the assignment
is still legal, it just does nothing.

There is no contradiction in such a subtype.  It seems pretty
silly written out explicitly, but recall that the bounds of
a range can be dynamic, and might just happen to define a range
with one (or even no) members.  This could happen, for instance,
during a generic instantiation.  As long as the code works, there
seems no reason to rule it illegal.

As a more familiar analogy, consider an array with no components.
We expect sort routines &c to work on such arrays, because we might
generate them dynamically in the normal course of running our code.
Boundary conditions should be handled even when seemingly meaningless.

drw@cullvax.UUCP (04/07/87)

"SVDSD::PETCHER@ti-eg.CSNET" writes:
> I am not an Ada expert, so correct me if I'm wrong, but...
> When one declares a variable range to be (true..true) is that not in itself a 
> contradiction?  A variable that can have only one value is, by definition, not 
> a variable, but a constant.  In that context, it seems a compiler should 
> create a constant, issue a diagnostic for any subsequent code that attempts to 
> change the constant, and generate no executeable code for same.  I realize the 
> most likely place for a single-valued variable to occur would be in 
> instantiation of a generic, but if a generic so instantiated does not treat 
> single-valued variables as constants then the program is basically in error.

I'm not well-versed in Ada, but methinks that such a variable is
inherently different from a constant in that one can/should be able to
assign to it--although there is only one value that can be assigned,
so it doesn't do very much!  Anything else leads to serious
nonuniformity.

Dale
-- 
Dale Worley		Cullinet Software
UUCP: ...!seismo!harvard!mit-eddie!cullvax!drw
ARPA: cullvax!drw@eddie.mit.edu
Un*x (a generic name for a class of OS's) != Unix (AT&T's brand of such)

gore@nucsrl.UUCP (04/08/87)

/ nucsrl:comp.lang.ada / "SVDSD::PETCHER@ti-eg.CSNET" /  6:14 am  Apr  3, 1987 /
>[...]  A variable that can have only one value is, by definition, not 
>a variable, but a constant.  In that context, it seems a compiler should 
>create a constant, issue a diagnostic for any subsequent code that attempts to 
>change the constant, and generate no executeable code for same.  [...]

A variable that can have at most one value can also have no value -- it may be
uninitialized; constants may not be.

Jacob Gore
Northwestern University, Computer Science Research Lab
{ihnp4,chinet}!nucsrl!gore