[comp.lang.ada] Enumeration_IO

mfeldman@seas.gwu.edu (Mike Feldman) (05/08/90)

In preparing some programs for a book I am working on, I have gotten
interested in the idiosyncracies of Enumeration_IO. Specifically, the
PUT operations in this generic package are supposed to have a parameter
SET of type TYPE_SET, where TYPE_SET is (LOWER_CASE, UPPER_CASE).

Well, consider an enumeration type
  type COLORS is (red, blue, green);
and an instance 
  package COLOR_IO is new TEXT_IO.ENUMERATION_IO(ENUM => COLORS);
and a variable
  C : COLORS := blue;
and an output operation
  COLOR_IO.PUT(ITEM => C);

All is well so far, everything compiles and runs except that the output
value is BLUE, because the default is Upper_Case. Now change the output
operation to
  COLOR_IO.PUT(ITEM => C, SET => LOWER_CASE);

Irvine Ada (HP835), IntegrAda (IBM-PC), VADS (Sun3) and AdaVantage (Sun3)
ALL bail out on a "LOWER_CASE is an undefined identifier" diagnostic.
Clearly something is going on here. According to LRM 14.3.9 this should be
allowed. Can anyone provide some enlightenment here?

I will post a program if anyone wants to try this out.
---------------------------------------------------------------------------
Prof. Michael Feldman
Department of Electrical Engineering and Computer Science
The George Washington University
Washington, DC 20052
+1-202-994-5253
mfeldman@seas.gwu.edu
---------------------------------------------------------------------------

blakemor@software.org (Alex Blakemore) (05/08/90)

In article <1846@sparko.gwu.edu> mfeldman@seas.gwu.edu (Mike Feldman) writes:
> In preparing some programs for a book I am working on ...
> Can anyone provide some enlightenment here?

  You forgot to specify the complete name of the enumeration literal.
  They are only directly visible where the type is directly visible.

  Change
    > COLOR_IO.PUT(ITEM => C, SET => LOWER_CASE);
  to
      COLOR_IO.PUT(ITEM => C, SET => TEXT_IO.LOWER_CASE);
                                     ^^^^^^^^
P.S. You're not going to use all caps for everything in your book are you?
     Too many people think Ada requires LONG_NAMES_WITH_ALL_CAPS_FOR_EVERYTHING

-----------------------------------------------------------------------
Alex Blakemore                       Internet: blakemore@software.org
Software Productivity Consortium     UUNET:    ...!uunet!software!blakemore
2214 Rock Hill Rd, Herndon VA 22070  Bellnet:  (703) 742-7125

dritz@ANTARES.MCS.ANL.GOV (05/09/90)

Having seen Michael Feldman's test program for Enumeration_IO, I think I can
say what the problem is.  The type Type_Set and its enumeration literals
Lower_Case and Upper_Case are declared in Text_IO, not in
Text_IO.Enumeration_IO.  The test program only "with"ed Text_IO; it did not
"use" it.  Hence the enumeration literal Lower_Case is not directly visible.
I would expect that adding "use Text_IO;" or replacing "Lower_Case" by
"Text_IO.Lower_Case" would solve the problem.

Ken Dritz
dritz@mcs.anl.gov

mfeldman@seas.gwu.edu (Mike Feldman) (05/09/90)

In article <1117@software.software.org> blakemor@software.org (Alex Blakemore) writes:
>In article <1846@sparko.gwu.edu> mfeldman@seas.gwu.edu (Mike Feldman) writes:
>
>  You forgot to specify the complete name of the enumeration literal.
>  They are only directly visible where the type is directly visible.
Right. And thanks to the 2 dozen or so folks who pointed this out by e-mail.
I really like getting these notes from Ada folks I've not met before.
 
>  Change
>    > COLOR_IO.PUT(ITEM => C, SET => LOWER_CASE);
>  to
>      COLOR_IO.PUT(ITEM => C, SET => TEXT_IO.LOWER_CASE);
>                                     ^^^^^^^^
Indeed. After eight years of developing Ada code for all kinds of things,
but especially classroom projects and examples, I am going over all my code
and converting to eliminate unnecessary "use" clauses. It's taken me a long
time to buy in to the "avoid the USE" philosophy, but - with some exceptions
- I agree. So now I'm tripping over all the little pebbles associated with
ripping out the "use"s. It's a good exercise. I recommend it. Good for the soul.

>P.S. You're not going to use all caps for everything in your book are you?
>     Too many people think Ada requires LONG_NAMES_WITH_ALL_CAPS_FOR_EVERYTHING
No, I'm not. I'm using a basic style similar to that in Cohen's book. 
(Reserved words in upper case, all else in mixed case, reasonable lengths)
The book is for CS1 students just learning to program. Those kids really
benefit from seeing the reserved words in upper case, because it really
highlights the structural templates used and re-used in programs.
(There is controversy about this; an author needs to make a decision and
stick to it consistently, especially when teaching freshmen...)

Other things Ada can offer freshmen, aside from the obvious stuff like
packages, generics, and the like:

1. Named association of parameters. Kids have trouble matching formals to
   actuals, in any language. The FORMAL => ACTUAL binding that comes so
   naturally with named association really makes learning programming easier!

2. Consistent treatment of enumeration types, especially Enumeration_IO.
   There are freshman books that devote nearly a whole chapter to showing
   how to _write_ Enumeration_IO in Pascal, just to read and write booleans.
   String <-> numeric conversions are also good for the soul, but it's an
   "advanced topic" in the freshman context. Enumeration_IO makes it easy
   to treat enumerations as full members of the scalar-type community, so
   we can encourage their use.

I'll quit here, lest you think I'm pushing a book. I'll be glad to discuss
teaching philosophy by private e-mail. My bottom line is that it's not only
important to teach Ada to college students because it's good for Ada and
industry. It's important because Ada has a _lot_ to contribute to computer
science.
---------------------------------------------------------------------------
Prof. Michael Feldman
Department of Electrical Engineering and Computer Science
The George Washington University
Washington, DC 20052
+1-202-994-5253
mfeldman@seas.gwu.edu
---------------------------------------------------------------------------

umace03@doc.ic.ac.uk (M Y Ben Gershon) (05/09/90)

Newsgroups: comp.lang.ada
Subject: Re: Enumeration_IO
Summary: 
Expires: 
References: <1846@sparko.gwu.edu>
Sender: 
Reply-To: umace03@doc.ic.ac.uk (M Y Ben Gershon)
Followup-To: 
Distribution: 
Organization: Imperial College Department of Computing
Keywords: 

In article <1846@sparko.gwu.edu> mfeldman@seas.gwu.edu (Mike Feldman) writes:
>In preparing some programs for a book I am working on, I have gotten
>interested in the idiosyncracies of Enumeration_IO. Specifically, the
>PUT operations in this generic package are supposed to have a parameter
>SET of type TYPE_SET, where TYPE_SET is (LOWER_CASE, UPPER_CASE).
>
>Well, consider an enumeration type
>  type COLORS is (red, blue, green);
>and an instance 
>  package COLOR_IO is new TEXT_IO.ENUMERATION_IO(ENUM => COLORS);
>and a variable
>  C : COLORS := blue;
>and an output operation
>  COLOR_IO.PUT(ITEM => C);
>
>All is well so far, everything compiles and runs except that the output
>value is BLUE, because the default is Upper_Case. Now change the output
>operation to
>  COLOR_IO.PUT(ITEM => C, SET => LOWER_CASE);
>
>Irvine Ada (HP835), IntegrAda (IBM-PC), VADS (Sun3) and AdaVantage (Sun3)
>ALL bail out on a "LOWER_CASE is an undefined identifier" diagnostic.
>Clearly something is going on here. According to LRM 14.3.9 this should be
>allowed. Can anyone provide some enlightenment here?



I can't claim to be an Ada expert, but altering it to the following:

  Color_IO.Put (Item => C, Set => Text_IO.Lower_Case);

seems to work fine in AdaStudent.  Alternatively, you could leave it unchanged
and have a 'use Text_IO' at the top, after 'with Text_IO'.  However, it
doesn't quite seem to make sense, and if the following is tried:

  Color_IO.Put (Item => C, Set => Color_IO.Lower_Case);

it doesn't work (as in the original example).  Why this should be I cannot
imagine, but when someone can explain it to us on the net, I'm sure we'll all
have learned something about generic packages that we didn't know before.

Michael Ben-Gershon
umace03@doc.ic.ac.uk

umace03@doc.ic.ac.uk (M Y Ben Gershon) (05/10/90)

>In article <1850@sparko.gwu.edu> mfeldman@seas.gwu.edu (Mike Feldman) writes:

>  Change
>    > COLOR_IO.PUT(ITEM => C, SET => LOWER_CASE);
>  to
>      COLOR_IO.PUT(ITEM => C, SET => TEXT_IO.LOWER_CASE);
>                                     ^^^^^^^^

Yes, OK.  But why doesn't COLOR_IO.LOWER_CASE work, seeing that we are using

package COLOR_IO is new TEXT_IO.ENUMERATION_IO(ENUM => COLORS);

shouldn't it follow that LOWER_CASE is distinct for each separate instantiation
of TEXT_IO.<enum_type> ?  Yet, if I put COLOR_IO.LOWER_CASE it doesn't work,
giving the same error message as before.

Michael Ben-Gershon
umace03@doc.ic.ac.uk

mfeldman@seas.gwu.edu (Mike Feldman) (05/10/90)

In article <1874@gould.doc.ic.ac.uk> umace03@doc.ic.ac.uk (M Y Ben Gershon) writes:
>
>>In article <1850@sparko.gwu.edu> mfeldman@seas.gwu.edu (Mike Feldman) writes:
>
>>      COLOR_IO.PUT(ITEM => C, SET => TEXT_IO.LOWER_CASE);
>>                                     ^^^^^^^^
>
>Yes, OK.  But why doesn't COLOR_IO.LOWER_CASE work, seeing that we are using
>
>package COLOR_IO is new TEXT_IO.ENUMERATION_IO(ENUM => COLORS);
>
>shouldn't it follow that LOWER_CASE is distinct for each separate instantiation
>of TEXT_IO.<enum_type> ?  Yet, if I put COLOR_IO.LOWER_CASE it doesn't work,
>giving the same error message as before.

Well, now that I'm such an expert, this one is easy. It's because the
enumeration type TYPE_SET (which contains the literals UPPER_CASE and
LOWER_CASE) is exported from TEXT_IO (i.e. the outer layer) and NOT from
any of the enclosed generic packages. Checking the LRM or a good text's
appendix for the full spec of TEXT_IO will confirm this. So there's really
only one set of literals, and it ain't exported from COLOR_IO!

---------------------------------------------------------------------------
Prof. Michael Feldman
Department of Electrical Engineering and Computer Science
The George Washington University
Washington, DC 20052
+1-202-994-5253
mfeldman@seas.gwu.edu
---------------------------------------------------------------------------