comp.lang.ada
 help / color / mirror / Atom feed
* constrained subtypes
@ 2002-03-11 15:00 George Stevens
  2002-03-11 16:16 ` Stephen Leake
                   ` (4 more replies)
  0 siblings, 5 replies; 16+ messages in thread
From: George Stevens @ 2002-03-11 15:00 UTC (permalink / raw)


We're trying to use subtypes of base integer, constraining them 1 .. 8
(i.e. static values)
When we use these types in a case statement, covering values 1 - 8, the
Aonix Ada compiler (7.1.2) complains that we haven't defined "others".
However, we shouldn't need to, as all cases are within the constrained
limits.  I've looked at Programming in Ada 95 - Barnes (p106) which seems to
suggest that we're doing the right thing language-wise, so long as the range
limits are static types.

Is this a compiler funny or an Ada funny?





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

* Re: constrained subtypes
  2002-03-11 15:00 constrained subtypes George Stevens
@ 2002-03-11 16:16 ` Stephen Leake
  2002-03-11 16:50 ` Jeffrey Carter
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 16+ messages in thread
From: Stephen Leake @ 2002-03-11 16:16 UTC (permalink / raw)


"George Stevens" <george.stevens@baesystems.com> writes:

> We're trying to use subtypes of base integer, constraining them 1 .. 8
> (i.e. static values)
> When we use these types in a case statement, covering values 1 - 8, the
> Aonix Ada compiler (7.1.2) complains that we haven't defined "others".
> However, we shouldn't need to, as all cases are within the constrained
> limits.  I've looked at Programming in Ada 95 - Barnes (p106) which seems to
> suggest that we're doing the right thing language-wise, so long as the range
> limits are static types.
> 
> Is this a compiler funny or an Ada funny?

You'll have to show the code; if you do this correctly, all Ada
compilers support it (I've done it on both Aonix and GNAT).


-- 
-- Stephe



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

* Re: constrained subtypes
  2002-03-11 15:00 constrained subtypes George Stevens
  2002-03-11 16:16 ` Stephen Leake
@ 2002-03-11 16:50 ` Jeffrey Carter
  2002-03-11 21:05 ` Anh_Vo
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 16+ messages in thread
From: Jeffrey Carter @ 2002-03-11 16:50 UTC (permalink / raw)


George Stevens wrote:
> 
> We're trying to use subtypes of base integer, constraining them 1 .. 8
> (i.e. static values)
> When we use these types in a case statement, covering values 1 - 8, the
> Aonix Ada compiler (7.1.2) complains that we haven't defined "others".
> However, we shouldn't need to, as all cases are within the constrained
> limits.  I've looked at Programming in Ada 95 - Barnes (p106) which seems to
> suggest that we're doing the right thing language-wise, so long as the range
> limits are static types.
> 
> Is this a compiler funny or an Ada funny?

Without seeing your code, I can't say. The ARM says

The possible values of the expression shall be covered as follows: 

       * If the expression is a name (including a type_conversion or a
function_call) having a static and constrained nominal subtype, or is a
         qualified_expression whose subtype_mark denotes a static and
constrained scalar subtype, then each non-others discrete_choice
         shall cover only values in that subtype, and each value of that
subtype shall be covered by some discrete_choice (either explicitly or
by
         others). 

       * If the type of the expression is root_integer,
universal_integer, or a descendant of a formal scalar type, then the
case_statement shall
         have an others discrete_choice. 

       * Otherwise, each value of the base range of the type of the
expression shall be covered (either explicitly or by others).

If your case expression falls under the first bullet, then it's a
compiler bug. If it falls under the other 2 bullets, though, it is an
error in your understanding.

From the compiler message information you've provided, it sounds as if
the compiler thinks your expression falls under the second bullet
(others choice required).

-- 
Jeffrey Carter



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

* Re: constrained subtypes
  2002-03-11 15:00 constrained subtypes George Stevens
  2002-03-11 16:16 ` Stephen Leake
  2002-03-11 16:50 ` Jeffrey Carter
@ 2002-03-11 21:05 ` Anh_Vo
  2002-03-12  9:39 ` George Stevens
  2002-03-13 22:09 ` Wannabe h4x0r
  4 siblings, 0 replies; 16+ messages in thread
From: Anh_Vo @ 2002-03-11 21:05 UTC (permalink / raw)


"George Stevens" <george.stevens@baesystems.com> wrote in message news:<3c8cc63b$1@pull.gecm.com>...
> We're trying to use subtypes of base integer, constraining them 1 .. 8
> (i.e. static values)
> When we use these types in a case statement, covering values 1 - 8, the
> Aonix Ada compiler (7.1.2) complains that we haven't defined "others".
> However, we shouldn't need to, as all cases are within the constrained
> limits.  I've looked at Programming in Ada 95 - Barnes (p106) which seems to
> suggest that we're doing the right thing language-wise, so long as the range
> limits are static types.
> 
> Is this a compiler funny or an Ada funny?

Neither is with information given. That leaves to the third source.
Please post the source codes, so others can have a chance to look at
them. In fact, this post did not contain enough information to draw
any conclusion.

A. Vo



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

* Re: constrained subtypes
@ 2002-03-12  7:59 Christoph Grein
  0 siblings, 0 replies; 16+ messages in thread
From: Christoph Grein @ 2002-03-12  7:59 UTC (permalink / raw)


  subtype X is Integer range 1 .. 8;
  V : X;
  I: Integer;

begin

  case V is
    when 1 .. 7 =>
    when 8 =>
  end case;

  case I is
    when 1 .. 7 =>
    when 8 =>
    when others =>
  end case;

  case X'(I) is
    when 1 .. 7 =>
    when 8 =>
  end case;

Hope this helps.



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

* Re: constrained subtypes
  2002-03-11 15:00 constrained subtypes George Stevens
                   ` (2 preceding siblings ...)
  2002-03-11 21:05 ` Anh_Vo
@ 2002-03-12  9:39 ` George Stevens
  2002-03-12 10:38   ` Martin Dowie
  2002-03-12 16:42   ` Jeffrey Carter
  2002-03-13 22:09 ` Wannabe h4x0r
  4 siblings, 2 replies; 16+ messages in thread
From: George Stevens @ 2002-03-12  9:39 UTC (permalink / raw)


Problem seemed to be in using:

case (constrained_subtype)

rather than:

case constrained_subtype

Removing the brackets sorted the problem.  Not sure why this is a problem
for constrained subtypes, as the compilers cope fine for enums and (base)
integers.

Sorry I didn't put the original code in my post, but was away from my build
machine, so was relying on my memory.  Thanks for all reponses.


"George Stevens" <george.stevens@baesystems.com> wrote in message
news:3c8cc63b$1@pull.gecm.com...
> We're trying to use subtypes of base integer, constraining them 1 .. 8
> (i.e. static values)
> When we use these types in a case statement, covering values 1 - 8, the
> Aonix Ada compiler (7.1.2) complains that we haven't defined "others".
> However, we shouldn't need to, as all cases are within the constrained
> limits.  I've looked at Programming in Ada 95 - Barnes (p106) which seems
to
> suggest that we're doing the right thing language-wise, so long as the
range
> limits are static types.
>
> Is this a compiler funny or an Ada funny?
>
>





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

* Re: constrained subtypes
@ 2002-03-12 10:37 Christoph Grein
  0 siblings, 0 replies; 16+ messages in thread
From: Christoph Grein @ 2002-03-12 10:37 UTC (permalink / raw)


    subtype S is Integer range 1 .. 4;
    V : S

begin

    case V is
	when 1 =>
	    [statement]
	when 2 .. 3 =>
	    [statement]
	when 4 =>
	    [statement]
    end case;
    
    case (V) is          -- (V) is an expression of type Integer
	when 1 =>
	    [statement]
	when 2 .. 3 =>
	    [statement]
	when 4 =>
	    [statement]
	when others =>   -- needed
	    [statement]
    end case;

> Problem seemed to be in using:
> 
> case (constrained_subtype)
> 
> rather than:
> 
> case constrained_subtype
> 
> Removing the brackets sorted the problem.  Not sure why this is a problem
> for constrained subtypes, as the compilers cope fine for enums and (base)
> integers.
> 
> Sorry I didn't put the original code in my post, but was away from my build
> machine, so was relying on my memory.  Thanks for all reponses.
> 
> 
> "George Stevens" <george.stevens@baesystems.com> wrote in message
> news:3c8cc63b$1@pull.gecm.com...
> > We're trying to use subtypes of base integer, constraining them 1 .. 8
> > (i.e. static values)
> > When we use these types in a case statement, covering values 1 - 8, the
> > Aonix Ada compiler (7.1.2) complains that we haven't defined "others".
> > However, we shouldn't need to, as all cases are within the constrained
> > limits.  I've looked at Programming in Ada 95 - Barnes (p106) which seems
> to
> > suggest that we're doing the right thing language-wise, so long as the
> range
> > limits are static types.
> >
> > Is this a compiler funny or an Ada funny?
> >
> >
> 
> 
> _______________________________________________
> comp.lang.ada mailing list
> comp.lang.ada@ada.eu.org
> http://ada.eu.org/mailman/listinfo/comp.lang.ada



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

* Re: constrained subtypes
  2002-03-12  9:39 ` George Stevens
@ 2002-03-12 10:38   ` Martin Dowie
  2002-03-12 11:57     ` George Stevens
  2002-03-12 16:42   ` Jeffrey Carter
  1 sibling, 1 reply; 16+ messages in thread
From: Martin Dowie @ 2002-03-12 10:38 UTC (permalink / raw)


"George Stevens" <george.stevens@baesystems.com> wrote in message
news:3c8dcc9d$1@pull.gecm.com...
> Problem seemed to be in using:
>
> case (constrained_subtype)
>
> rather than:
>
> case constrained_subtype
>
> Removing the brackets sorted the problem.  Not sure why this is a problem
> for constrained subtypes, as the compilers cope fine for enums and (base)
> integers.
>
> Sorry I didn't put the original code in my post, but was away from my
build
> machine, so was relying on my memory.  Thanks for all reponses.

GNAT 3.13p agrees with this one.

Is this because it is an "parenthesized expression", the result of the
expression being in terms of the parent type "Integer"?

RM 4.4-ish?






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

* Re: constrained subtypes
  2002-03-12 10:38   ` Martin Dowie
@ 2002-03-12 11:57     ` George Stevens
  2002-03-12 12:02       ` Martin Dowie
  2002-03-12 12:03       ` Martin Dowie
  0 siblings, 2 replies; 16+ messages in thread
From: George Stevens @ 2002-03-12 11:57 UTC (permalink / raw)



"Martin Dowie" <martin.dowie@nospam.baesystems.com> wrote

> GNAT 3.13p agrees with this one.

Sorry not a news group regular (or indeed Ada regular), not sure what GNAT
3.13p is?

We tried it through the GNAT compiler as well as the Aonix one and it gave
the same error message, suggesting it was indeed a code error rather than a
compiler error.

> Is this because it is an "parenthesized expression", the result of the
> expression being in terms of the parent type "Integer"?

Apparently so, it seems.  The compiler seems to be interpreting it as a
parent type due to the brackets.  Is this a freezing rule situation?

> RM 4.4-ish?

Again, not sure what RM 4.4 is?





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

* Re: constrained subtypes
  2002-03-12 11:57     ` George Stevens
@ 2002-03-12 12:02       ` Martin Dowie
  2002-03-12 12:03       ` Martin Dowie
  1 sibling, 0 replies; 16+ messages in thread
From: Martin Dowie @ 2002-03-12 12:02 UTC (permalink / raw)


> Sorry not a news group regular (or indeed Ada regular), not sure what GNAT
> 3.13p is?

3.13p is a version of the GNAT compiler (3.13 public)


> Again, not sure what RM 4.4 is?

RM is short for Reference Manual - the language bible.





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

* Re: constrained subtypes
  2002-03-12 11:57     ` George Stevens
  2002-03-12 12:02       ` Martin Dowie
@ 2002-03-12 12:03       ` Martin Dowie
  1 sibling, 0 replies; 16+ messages in thread
From: Martin Dowie @ 2002-03-12 12:03 UTC (permalink / raw)


> Is this a freezing rule situation?

No, I don't think so.







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

* Re: constrained subtypes
  2002-03-12  9:39 ` George Stevens
  2002-03-12 10:38   ` Martin Dowie
@ 2002-03-12 16:42   ` Jeffrey Carter
  1 sibling, 0 replies; 16+ messages in thread
From: Jeffrey Carter @ 2002-03-12 16:42 UTC (permalink / raw)


George Stevens wrote:
> 
> Problem seemed to be in using:
> 
> case (constrained_subtype)
> 
> rather than:
> 
> case constrained_subtype
> 
> Removing the brackets sorted the problem.  Not sure why this is a problem
> for constrained subtypes, as the compilers cope fine for enums and (base)
> integers.

With parentheses, you have an expression, which is of the unconstrained
base subtype (Integer'Base), not the constrained subtype. Expressions
are always evaluated using the unconstrained base type. This allows
things such as

subtype X is Integer range 1 .. 8;
Y : Integer := 16;
Z : X;
...
Z := Y / 2;
Y := 3 * Z;

to be legal. "Y / 2" is not necessarily in X, so a constraint check is
performed before assignment. "3 * Z" is not of subtype X, but is legal,
even though Z is of subtype X, because it is an expression.

This is why "case Z" and "case (Z)" are different. "Case Y" and "case
(Y)" cover the same range, so you don't see a difference (though it's
there).

-- 
Jeffrey Carter



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

* Re: constrained subtypes
@ 2002-03-13  6:22 Christoph Grein
  2002-03-13 14:18 ` Robert Dewar
  0 siblings, 1 reply; 16+ messages in thread
From: Christoph Grein @ 2002-03-13  6:22 UTC (permalink / raw)


> From: Jeffrey Carter <jeffrey.carter@boeing.com>

> With parentheses, you have an expression, which is of the unconstrained
> base subtype (Integer'Base), not the constrained subtype. Expressions
> are always evaluated using the unconstrained base type. This allows
> things such as
> 
> subtype X is Integer range 1 .. 8;
> Y : Integer := 16;
> Z : X;
> ...
> Z := Y / 2;
> Y := 3 * Z;
> 
> to be legal. "Y / 2" is not necessarily in X, so a constraint check is
> performed before assignment. "3 * Z" is not of subtype X, but is legal,
> even though Z is of subtype X, because it is an expression.
> 
> This is why "case Z" and "case (Z)" are different. "Case Y" and "case
> (Y)" cover the same range, so you don't see a difference (though it's
> there).
> 

Nice exegesis, I also thought it was this way. But when I looked into the RM, I 
found only:

4.4(7) primary ::= numeric_literal | null | string_literal | aggregate
                 | name | qualified_expression | allocator | (expression) 

4.4(9) Each expression has a type; it specifies the computation or retrieval
       of a value of that type. 

4.4(10) The value of a primary that is a name denoting an object is the
        value of the object. 

Thus (X) has to be further reduced to X, and then X denotes an object,
so where is the difference between "case X is" and "case (X) is"?

I would like to see a formal exegesis with the RM.



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

* Re: constrained subtypes
  2002-03-13  6:22 Christoph Grein
@ 2002-03-13 14:18 ` Robert Dewar
  0 siblings, 0 replies; 16+ messages in thread
From: Robert Dewar @ 2002-03-13 14:18 UTC (permalink / raw)


Christoph Grein <christoph.grein@eurocopter.com> wrote in message > Nice exegesis, I also thought it was this way. But when I looked into the RM, I found only:

<<irrelevant quotes from chapter 4 snipped>>

Why look in chapter 4 here. If you are interested in case
statements, look in chapter 5 at the section on case
statements. Sometimes it is indeed hard to find things in
the RM, but not in this case!

5.4 Case Statements

7  If the expression is a name (including a type_conversion or
afunction_call) having a static and constrained nominal subtype, or is
a qualified_expression whose subtype_mark denotes a static and
constrained scalar subtype, then each non-others discrete_choice shall
cover only values in that subtype, and each value of that subtype
shall be covered by some discrete_choice (either explicitly or by
others).

Pretty clear if you ask me, obviously (x) is not of the syntactic form
of a name.

> I would like to see a formal exegesis with the RM.

Well this is hardly obscure enough to warrant the term
exegesis :-)



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

* Re: constrained subtypes
  2002-03-11 15:00 constrained subtypes George Stevens
                   ` (3 preceding siblings ...)
  2002-03-12  9:39 ` George Stevens
@ 2002-03-13 22:09 ` Wannabe h4x0r
  4 siblings, 0 replies; 16+ messages in thread
From: Wannabe h4x0r @ 2002-03-13 22:09 UTC (permalink / raw)


On Mon, 11 Mar 2002 10:00:10 -0500, George Stevens wrote:

> We're trying to use subtypes of base integer, constraining them 1 .. 8
> (i.e. static values)
> When we use these types in a case statement, covering values 1 - 8, the
> Aonix Ada compiler (7.1.2) complains that we haven't defined "others".
> However, we shouldn't need to, as all cases are within the constrained
> limits.  I've looked at Programming in Ada 95 - Barnes (p106) which
> seems to suggest that we're doing the right thing language-wise, so long
> as the range limits are static types.
> 
> Is this a compiler funny or an Ada funny?
> 
> 

You might need to create an exception just in case the
value, for some odd reason, just happens to fall outside
the expected range. Though I cant say for sure.

Chris



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

* Re: constrained subtypes
@ 2002-03-14 11:01 Christoph Grein
  0 siblings, 0 replies; 16+ messages in thread
From: Christoph Grein @ 2002-03-14 11:01 UTC (permalink / raw)


> Christoph Grein <christoph.grein@eurocopter.com> wrote in message > Nice 
exegesis, I also thought it was this way. But when I looked into the RM, I found 
only:
> 
> <<irrelevant quotes from chapter 4 snipped>>
> 
> Why look in chapter 4 here. If you are interested in case
> statements, look in chapter 5 at the section on case
> statements. Sometimes it is indeed hard to find things in
> the RM, but not in this case!

I was interested in evaluating expressions, so this seemed the obvious place.

> 5.4 Case Statements
> 
snip
> 
> Well this is hardly obscure enough to warrant the term
> exegesis :-)

Of course not :-) Robert's accurate as always :-)

Thank you for pointing out the correct chapter and verse.



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

end of thread, other threads:[~2002-03-14 11:01 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-03-11 15:00 constrained subtypes George Stevens
2002-03-11 16:16 ` Stephen Leake
2002-03-11 16:50 ` Jeffrey Carter
2002-03-11 21:05 ` Anh_Vo
2002-03-12  9:39 ` George Stevens
2002-03-12 10:38   ` Martin Dowie
2002-03-12 11:57     ` George Stevens
2002-03-12 12:02       ` Martin Dowie
2002-03-12 12:03       ` Martin Dowie
2002-03-12 16:42   ` Jeffrey Carter
2002-03-13 22:09 ` Wannabe h4x0r
  -- strict thread matches above, loose matches on Subject: below --
2002-03-12  7:59 Christoph Grein
2002-03-12 10:37 Christoph Grein
2002-03-13  6:22 Christoph Grein
2002-03-13 14:18 ` Robert Dewar
2002-03-14 11:01 Christoph Grein

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