comp.lang.ada
 help / color / mirror / Atom feed
* Discriminant as default initial value
@ 1997-03-29  0:00 Scott Renfro
  1997-03-29  0:00 ` Matthew Heaney
                   ` (2 more replies)
  0 siblings, 3 replies; 17+ messages in thread
From: Scott Renfro @ 1997-03-29  0:00 UTC (permalink / raw)



Greetings,

I'm new to Ada and have scoured the FAQ, RM, and Rationale, but haven't
found an answer to my question that I understand.

I want to use a discriminant in a record as both a bound in the index
constraint of a component declaration and as a default initial value. 
I'm not sure that this is allowed by the RM.

The following example, while contrived, exhibits behavior that I
definitely do not understand.  Using GNAT 3.09 for NT with Max defined
before the Position Matrix, the component record is initialized as
expected.  When Max is defined after the Position Matrix within the
record, Max is initialized to a large value that is not even within the
subtype's range.  No exception is raised.

Is this expected behavior?  Is it permissible to use the same
discriminant as both an index bound and a default initial value?  Should
the behavior change when the order of declaration within the record
changes?

Thanks in advance,
Scott

-----------------------------------------------------
--
-- Test_Discrim.adb
--
-----------------------------------------------------
with Ada.Text_IO, Ada.Command_Line;
use  Ada.Text_IO;

procedure Test_Discrim is

   type Matrix is array (Integer range <>, Integer range <>) of Integer;
   subtype Matrix_Range is Natural range 1 .. 80;
   
   type Matrix_Record (Columns : Matrix_Range) is
      record
--         Max      : Natural := Columns;   -- if declared here, all is
well
         Position : Matrix (1 .. Columns, 1 .. Columns);
         Max      : Matrix_Range := Columns;   -- if declared here, max
improper
      end record;

   width : Matrix_Range := 1;
   
begin
   width := Integer'value (Ada.Command_Line.Argument (1));
   declare
      Temp_Matrix : Matrix_Record (Width);
   begin
      Put_Line (Integer'Image (Temp_Matrix.Max));
   end;
end test_discrim;




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

* Re: Discriminant as default initial value
  1997-03-29  0:00 Scott Renfro
@ 1997-03-29  0:00 ` Matthew Heaney
  1997-03-30  0:00   ` Scott Renfro
  1997-03-30  0:00 ` Bob Klungle
  1997-03-31  0:00 ` Jon S Anthony
  2 siblings, 1 reply; 17+ messages in thread
From: Matthew Heaney @ 1997-03-29  0:00 UTC (permalink / raw)



In article <333DB640.59EA@sirinet.net>, Scott Renfro <srenfro@sirinet.net>
wrote:

>I want to use a discriminant in a record as both a bound in the index
>constraint of a component declaration and as a default initial value. 
>I'm not sure that this is allowed by the RM.

I'm curious: why are you setting the component Max to the value of the
discriminant?  Do you ever want to change Max?  If not, then why not just
read the discriminant directly (and get rid of the Max component)?

>The following example, while contrived, exhibits behavior that I
>definitely do not understand.  Using GNAT 3.09 for NT with Max defined
>before the Position Matrix, the component record is initialized as
>expected.  When Max is defined after the Position Matrix within the
>record, Max is initialized to a large value that is not even within the
>subtype's range.  No exception is raised.

This seems a bit strange.  The location of the components within the record
should not make any difference.  (Yes, there are some restrictions wrt
variant parts, but that doesn't apply here.)

Matt

--------------------------------------------------------------------
Matthew Heaney
Software Development Consultant
<mailto:matthew_heaney@acm.org>
(818) 985-1271




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

* Re: Discriminant as default initial value
  1997-03-30  0:00 Discriminant as default initial value Kees de Lezenne Coulande
@ 1997-03-30  0:00 ` Robert Dewar
  0 siblings, 0 replies; 17+ messages in thread
From: Robert Dewar @ 1997-03-30  0:00 UTC (permalink / raw)



<<Scott,
     I tried both versions of your example using GNAT 3.09 for OS/2, and
found no problems. Maybe it is just the Windows version of GNAT playing up
again.
                                 Kees de Lezenne Coulander>>

No, it works fine in the windows version too. This was a very vague report,
I paid little attention to it. If people think they have found a problem
in GNAT, it should be properly written up, formatted according to the
suggestions in gnatinfo.txt, and sent to report@gnat.com.

The trouble with vague reports is that you can't really clearly tell what
is being reported. In this case, I tried an instance of what I could best
guess was being reported, and it worked fine, but I do't really know,
since all we have is a vague claim that something did not work. In my
experience it is not worth chasing bug reports unless they are clear and
well documented -- they often turn out to be misunderstandings etc, and
even if they are real bugs, extracting the real bug can be difficult.





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

* Re: Discriminant as default initial value
@ 1997-03-30  0:00 Kees de Lezenne Coulande
  1997-03-30  0:00 ` Robert Dewar
  0 siblings, 1 reply; 17+ messages in thread
From: Kees de Lezenne Coulande @ 1997-03-30  0:00 UTC (permalink / raw)



Scott Renfro <srenfro@SIRINET.NET> wrote:

>I want to use a discriminant in a record as both a bound in the index
>constraint of a component declaration and as a default initial value.
>I'm not sure that this is allowed by the RM.
>
>The following example, while contrived, exhibits behavior that I
>definitely do not understand.  Using GNAT 3.09 for NT with Max defined
>before the Position Matrix, the component record is initialized as
>expected.  When Max is defined after the Position Matrix within the
>record, Max is initialized to a large value that is not even within the
>subtype's range.  No exception is raised.
>
>Is this expected behavior?  Is it permissible to use the same
>discriminant as both an index bound and a default initial value?  Should
>the behavior change when the order of declaration within the record
>changes?

Scott,
     I tried both versions of your example using GNAT 3.09 for OS/2, and
found no problems. Maybe it is just the Windows version of GNAT playing up
again.
                                 Kees de Lezenne Coulander
---------------------------------------------------------------------
  C.M. de Lezenne Coulander
  Amsterdam-Zuidoost
  The Netherlands
  E-mail: lezenne@compuserve.com

  Aircraft Development and Systems Engineering B.V.
  Schiphol-Rijk, The Netherlands
=====================================================================




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

* Re: Discriminant as default initial value
  1997-03-29  0:00 ` Matthew Heaney
@ 1997-03-30  0:00   ` Scott Renfro
  0 siblings, 0 replies; 17+ messages in thread
From: Scott Renfro @ 1997-03-30  0:00 UTC (permalink / raw)



Matthew Heaney wrote:
> 
> I'm curious: why are you setting the component Max to the value of the
> discriminant?  Do you ever want to change Max?  If not, then why not just
> read the discriminant directly (and get rid of the Max component)?

I can, and intend to, use the value of the discriminant as the max
rather than the Max component, but stumbled onto this behavior while
converting from a non-discriminant version of the same data structure. 
I had not yet removed the Max component and was surprised when I could
not initialize it's value from the discriminant.  Yes, I'm on a tangent,
but do want to understand this behavior since it surprised me.

> This seems a bit strange.  The location of the components within the record
> should not make any difference.  (Yes, there are some restrictions wrt
> variant parts, but that doesn't apply here.)

That's what I thought.  I just figured my lack of experience was a much
more likely root cause of this than a bug in GNAT.


Scott




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

* Re: Discriminant as default initial value
  1997-03-30  0:00 ` Bob Klungle
@ 1997-03-30  0:00   ` Robert Dewar
  1997-03-31  0:00     ` Bob Klungle
  0 siblings, 1 reply; 17+ messages in thread
From: Robert Dewar @ 1997-03-30  0:00 UTC (permalink / raw)



iBob Klungle said

<<Scott, I built your example on GNAT3.08/Linux and it worked OK.
Maybe NT. Have NT version installed but having trouble making it work.>>

That's odd, we never issued a 3.08 Linux. 3.08 was an NT only release.
Either Bob is confused, or the vesion he is using is one that no one
here knows anything about ....





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

* Re: Discriminant as default initial value
  1997-03-29  0:00 Scott Renfro
  1997-03-29  0:00 ` Matthew Heaney
@ 1997-03-30  0:00 ` Bob Klungle
  1997-03-30  0:00   ` Robert Dewar
  1997-03-31  0:00 ` Jon S Anthony
  2 siblings, 1 reply; 17+ messages in thread
From: Bob Klungle @ 1997-03-30  0:00 UTC (permalink / raw)



> 
> I'm new to Ada and have scoured the FAQ, RM, and Rationale, but haven't
> found an answer to my question that I understand.
> 
> I want to use a discriminant in a record as both a bound in the index
> constraint of a component declaration and as a default initial value. 
> I'm not sure that this is allowed by the RM.
> 
> The following example, while contrived, exhibits behavior that I
> definitely do not understand.  Using GNAT 3.09 for NT with Max defined
> before the Position Matrix, the component record is initialized as
> expected.  When Max is defined after the Position Matrix within the
> record, Max is initialized to a large value that is not even within the
> subtype's range.  No exception is raised.
> 
> Is this expected behavior?  Is it permissible to use the same
> discriminant as both an index bound and a default initial value?  Should
> the behavior change when the order of declaration within the record
> changes?

Scott, I built your example on GNAT3.08/Linux and it worked OK.
Maybe NT. Have NT version installed but having trouble making it work.

bob




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

* Re: Discriminant as default initial value
  1997-03-29  0:00 Scott Renfro
  1997-03-29  0:00 ` Matthew Heaney
  1997-03-30  0:00 ` Bob Klungle
@ 1997-03-31  0:00 ` Jon S Anthony
  1997-03-31  0:00   ` Robert Dewar
  1997-04-02  0:00   ` Jon S Anthony
  2 siblings, 2 replies; 17+ messages in thread
From: Jon S Anthony @ 1997-03-31  0:00 UTC (permalink / raw)



In article <333DB640.59EA@sirinet.net> Scott Renfro <srenfro@sirinet.net> writes:

> procedure Test_Discrim is
> 
>    type Matrix is array (Integer range <>, Integer range <>) of Integer;
>    subtype Matrix_Range is Natural range 1 .. 80;
>    
>    type Matrix_Record (Columns : Matrix_Range) is
>       record
> --         Max      : Natural := Columns;   -- if declared here, all is
> well
>          Position : Matrix (1 .. Columns, 1 .. Columns);
>          Max      : Matrix_Range := Columns;   -- if declared here, max
> improper
>       end record;
> 
>    width : Matrix_Range := 1;

If this is an accurate account, it's a bug in GNAT (whatever
version/platform you are using.)

/Jon
-- 
Jon Anthony
Organon Motives, Inc.
Belmont, MA 02178
617.484.3383
jsa@organon.com





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

* Re: Discriminant as default initial value
  1997-03-31  0:00     ` Bob Klungle
@ 1997-03-31  0:00       ` Robert Dewar
  0 siblings, 0 replies; 17+ messages in thread
From: Robert Dewar @ 1997-03-31  0:00 UTC (permalink / raw)



Bob said

<<Nothing odd here. The 9 is right next to the 8 and when typing fast
sometimes finger flicks occur (I have seen a few of yours without comment
[note the word vesion below]).>>

Fair enough, luckily most finger slips can be resolved by context (e.g.
vesion is probably close to understandable :-) but a 8 for a 9 can be
more confusing :-)






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

* Re: Discriminant as default initial value
  1997-03-31  0:00 ` Jon S Anthony
@ 1997-03-31  0:00   ` Robert Dewar
  1997-04-02  0:00   ` Jon S Anthony
  1 sibling, 0 replies; 17+ messages in thread
From: Robert Dewar @ 1997-03-31  0:00 UTC (permalink / raw)



<<If this is an accurate account, it's a bug in GNAT (whatever
version/platform you are using.)>>

The trouble is that the account was very incomplete, so it is impossible
to tell -- I agree that it may be a (rather surprising) bug in GNAT, but
from long experience, we have learned not to pay to much attention to
reports unless they are very specific and have exact source code!





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

* Re: Discriminant as default initial value
  1997-03-30  0:00   ` Robert Dewar
@ 1997-03-31  0:00     ` Bob Klungle
  1997-03-31  0:00       ` Robert Dewar
  0 siblings, 1 reply; 17+ messages in thread
From: Bob Klungle @ 1997-03-31  0:00 UTC (permalink / raw)



Nothing odd here. The 9 is right next to the 8 and when typing fast
sometimes finger flicks occur (I have seen a few of yours without comment
[note the word vesion below]).

bob

Robert Dewar <dewar@merv.cs.nyu.edu> wrote in article
<dewar.859761845@merv>...
> iBob Klungle said
> 
> <<Scott, I built your example on GNAT3.08/Linux and it worked OK.
> Maybe NT. Have NT version installed but having trouble making it work.>>
> 
> That's odd, we never issued a 3.08 Linux. 3.08 was an NT only release.
> Either Bob is confused, or the vesion he is using is one that no one
> here knows anything about ....
> 
> 




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

* Re: Discriminant as default initial value
@ 1997-04-02  0:00 Kees de Lezenne Coulande
  1997-04-02  0:00 ` Robert Dewar
  1997-04-02  0:00 ` Scott Renfro
  0 siblings, 2 replies; 17+ messages in thread
From: Kees de Lezenne Coulande @ 1997-04-02  0:00 UTC (permalink / raw)



I wrote:

<<Scott,
     I tried both versions of your example using GNAT 3.09 for OS/2, and
found no problems. Maybe it is just the Windows version of GNAT playing up
again.
                                 Kees de Lezenne Coulander>>

and Robert Dewar <dewar@MERV.CS.NYU.EDU> replied:

>No, it works fine in the windows version too. This was a very vague
>report,
>I paid little attention to it. If people think they have found a problem
>in GNAT, it should be properly written up, formatted according to the
>suggestions in gnatinfo.txt, and sent to report@gnat.com.
<snip>

     I know by now how you feel about these things. It is just that this
one intrigued me, because I had not encountered the use of a record
discriminant as the default value for a component before. I spent a couple
of minutes trying it out, found that it seemed to work, and decided that I
could just as well post the reply.
     At the end of last year I acquired a copy of Norman Cohen's magnum
opus, and I am slowly working through it by of a couple of pages a day. By
coincidence, just after posting the message, my daily dose brought me to
pages 381/382 where the allowable uses of discriminants are described. So,
although the original poster may not be any the wiser, I have certainly
learned something. And besides, it was the Easter weekend.
                              Kees de Lezenne Coulander
---------------------------------------------------------------------
  C.M. de Lezenne Coulander
  Amsterdam-Zuidoost
  The Netherlands
  E-mail: lezenne@compuserve.com

  Aircraft Development and Systems Engineering B.V.
  Schiphol-Rijk, The Netherlands
=====================================================================




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

* Re: Discriminant as default initial value
  1997-04-02  0:00 Kees de Lezenne Coulande
  1997-04-02  0:00 ` Robert Dewar
@ 1997-04-02  0:00 ` Scott Renfro
  1997-04-02  0:00   ` Robert Dewar
  1 sibling, 1 reply; 17+ messages in thread
From: Scott Renfro @ 1997-04-02  0:00 UTC (permalink / raw)



> Robert Dewar <dewar@MERV.CS.NYU.EDU> replied:
> 
> >No, it works fine in the windows version too. This was a very vague
> >report,
> >I paid little attention to it. If people think they have found a problem
> >in GNAT, it should be properly written up, formatted according to the
> >suggestions in gnatinfo.txt, and sent to report@gnat.com.

My newsfeed has not received Robert's message, so I do not know the
original context.  I am getting this from Kees de Lezenne Coulander's
reply.

The reason it is a very vague _report_ is that the original message was
_not_ a report.  It was a question by a new to Ada programmer trying to
learn the language, and clearly stated as such.

I did not believe at the time that I was suffering from a GNAT bug,
rather that I did not understand something about discriminants.  I did
not want to send a 'pilot error' question to gnat as a bug report.  I
had investigated the resources I have both online and in paper form and
could not find an answer, although Cohen's Ada as a Second Language pp.
381-382 came as close as I could find and seemed to indicate that it was
permissible.  Having exhausted my resources, I decided to ask the
knowledgeable people of c.l.a.

Unfortunately, everyone seems focused on GNAT bugs and not whether or
not the use of discriminants as default initial values is permissible
according to the RM.  This has lead me to conclude that it must be
permissible, or someone would have surely said something, so it must be
something worth reporting.  I will now reformat my question as a bug
report and send it to report@gnat.com.

I would have been happier, though, if someone had said yes what you are
trying to do is permissible, or no, that cannot be permissible according
to the RM or given another hint of a location in the RM to check.

Oh well, off to report@gnat.com I go.  

Scott




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

* Re: Discriminant as default initial value
  1997-04-02  0:00 Kees de Lezenne Coulande
@ 1997-04-02  0:00 ` Robert Dewar
  1997-04-07  0:00   ` Keith Thompson
  1997-04-02  0:00 ` Scott Renfro
  1 sibling, 1 reply; 17+ messages in thread
From: Robert Dewar @ 1997-04-02  0:00 UTC (permalink / raw)



Kees said

<<     I know by now how you feel about these things. It is just that this
one intrigued me, because I had not encountered the use of a record
discriminant as the default value for a component before. I spent a couple
of minutes trying it out, found that it seemed to work, and decided that I
could just as well post the reply.>>

Using a discriminant in the expression for a default value is perfectly
valid (it does not have to be on its own, it can be used within an
expression, e.g. you can have a default expression A+B where both A
and B are discriminants). This is legal because there is no rule that
says it is illegal. And it should work properly!





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

* Re: Discriminant as default initial value
  1997-04-02  0:00 ` Scott Renfro
@ 1997-04-02  0:00   ` Robert Dewar
  0 siblings, 0 replies; 17+ messages in thread
From: Robert Dewar @ 1997-04-02  0:00 UTC (permalink / raw)



<<The reason it is a very vague _report_ is that the original message was
_not_ a report.  It was a question by a new to Ada programmer trying to
learn the language, and clearly stated as such.>>

Even if you are just trying to learn the language, it is a good idea to
send EXACT code,, with an EXACT description of what you saw, and why you
expected to see something else.

The trouble with vague reports is that it has often been our experience
on CLA and elsewhere, that the vagueness covers up some fundamental
misconceptions that are the real cause of the problem.





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

* Re: Discriminant as default initial value
  1997-03-31  0:00 ` Jon S Anthony
  1997-03-31  0:00   ` Robert Dewar
@ 1997-04-02  0:00   ` Jon S Anthony
  1 sibling, 0 replies; 17+ messages in thread
From: Jon S Anthony @ 1997-04-02  0:00 UTC (permalink / raw)



In article <dewar.859869987@merv> dewar@merv.cs.nyu.edu (Robert Dewar) writes:

> <<If this is an accurate account, it's a bug in GNAT (whatever
> version/platform you are using.)>>
> 
> The trouble is that the account was very incomplete, so it is impossible
> to tell -- I agree that it may be a (rather surprising) bug in GNAT, but
> from long experience, we have learned not to pay to much attention to
> reports unless they are very specific and have exact source code!

A very wise policy.  No point in wasting a lot of time chasing
chimeras...  BTW, this works just fine on GNAT 3.09 Sparc.Solaris.

/Jon

-- 
Jon Anthony
Organon Motives, Inc.
Belmont, MA 02178
617.484.3383
jsa@organon.com





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

* Re: Discriminant as default initial value
  1997-04-02  0:00 ` Robert Dewar
@ 1997-04-07  0:00   ` Keith Thompson
  0 siblings, 0 replies; 17+ messages in thread
From: Keith Thompson @ 1997-04-07  0:00 UTC (permalink / raw)



In <dewar.859988477@merv> dewar@merv.cs.nyu.edu (Robert Dewar) writes:
[...]
> Using a discriminant in the expression for a default value is perfectly
> valid (it does not have to be on its own, it can be used within an
> expression, e.g. you can have a default expression A+B where both A
> and B are discriminants). This is legal because there is no rule that
> says it is illegal. And it should work properly!

Agreed.

For more details on the allowed uses of a discriminant, see RM95-3.8(12):

   12  A name that denotes a noninherited discriminant is allowed within
       the declaration of the type, but not within the discriminant_
       part.  If the discriminant is used to define the constraint of a
       component, the bounds of an entry family, or the constraint of
       the parent subtype in a derived_type_definition then its name
       shall appear alone as a direct_name (not as part of a larger
       expression or expanded name).  A discriminant shall not be used
       to define the constraint of a scalar component.

-- 
Keith Thompson (The_Other_Keith) kst@sd.aonix.com <http://www.aonix.com> <*>
TeleSo^H^H^H^H^H^H Alsy^H^H^H^H Thomson Softw^H^H^H^H^H^H^H^H^H^H^H^H^H Aonix
5040 Shoreham Place, San Diego, CA, USA, 92122-5989
"Humor is such a subjective thing." -- Cartagia




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

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

Thread overview: 17+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1997-03-30  0:00 Discriminant as default initial value Kees de Lezenne Coulande
1997-03-30  0:00 ` Robert Dewar
  -- strict thread matches above, loose matches on Subject: below --
1997-04-02  0:00 Kees de Lezenne Coulande
1997-04-02  0:00 ` Robert Dewar
1997-04-07  0:00   ` Keith Thompson
1997-04-02  0:00 ` Scott Renfro
1997-04-02  0:00   ` Robert Dewar
1997-03-29  0:00 Scott Renfro
1997-03-29  0:00 ` Matthew Heaney
1997-03-30  0:00   ` Scott Renfro
1997-03-30  0:00 ` Bob Klungle
1997-03-30  0:00   ` Robert Dewar
1997-03-31  0:00     ` Bob Klungle
1997-03-31  0:00       ` Robert Dewar
1997-03-31  0:00 ` Jon S Anthony
1997-03-31  0:00   ` Robert Dewar
1997-04-02  0:00   ` Jon S Anthony

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