comp.lang.ada
 help / color / mirror / Atom feed
* Membership in "set" of characters
@ 1993-03-19 17:06 Debora Weber-Wulff
  1993-03-19 19:57 ` Mark A Biggar
  0 siblings, 1 reply; 8+ messages in thread
From: Debora Weber-Wulff @ 1993-03-19 17:06 UTC (permalink / raw)


This is another one of those "why won't this work?" for the experts.
I'm stumped. As far as I can decode the LRM and
see from 2 textbooks I consulted, this ought to work.

-- muell3.ada
--
-- Why won't membership work here?
--
with text_io; use text_io;

procedure muell3 is
type affirmatives is ('y', 'Y', 'j', 'J', 'o', 'O');
  ch : CHARACTER;
  okay : Boolean;

begin
  get (ch);
  -- The next statement gets flagged with
  -- type clash in membership test [LRM 4.5.2/10]
  okay := ch IN affirmatives;

  -- Is this because there's the clause
  -- for CHARACTER use (1, 2, ... 128) in the standard?

end muell3;

This one was one of my favorites in Pascal! I'm currently 
quite frustrated with the type system after writing a little program
to calculate the return on investment. I had declared
one type for the interest and one for the DM amount, one was a float and
one was a fixed value. After hours of agony to get the conversion right
as the silly FIXEDs needed an explicit conversion after each * and /
I found that 1000 DM (FIXED) * 0.03 (FLOAT) was equal to 31.98 !!!. 
I'd love for the bank to calculate my interest that way! It finally
worked when both were FLOAT values. Now all I wanted to add was a loop
to calculate the values again, and I get blown out of the water by 
Meridian's 4.1 type checker again. 
Is it me? Is it Meridian? Or is this the agony of using Ada?
8
-- 
Debora Weber-Wulff, Professorin fuer Softwaretechnik
snail: Technische Fachhochschule, FB Informatik, 
       Luxemburgerstr. 10, 1000 Berlin 65
email: dww@informatik.tfh-berlin.dbp.de 



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

* Re: Membership in "set" of characters
  1993-03-19 17:06 Membership in "set" of characters Debora Weber-Wulff
@ 1993-03-19 19:57 ` Mark A Biggar
  1993-03-22 10:37   ` Andrew Dunstan,,2285592,
  1993-03-22 15:49   ` Ada 9X references M. Ramchandran
  0 siblings, 2 replies; 8+ messages in thread
From: Mark A Biggar @ 1993-03-19 19:57 UTC (permalink / raw)


In article <J6QBBP3L@math.fu-berlin.de> dww@math.fu-berlin.de (Debora Weber-Wulff) writes:
>with text_io; use text_io;
>procedure muell3 is
>type affirmatives is ('y', 'Y', 'j', 'J', 'o', 'O');
>  ch : CHARACTER;
>  okay : Boolean;
>begin
>  get (ch);
>  -- The next statement gets flagged with
>  -- type clash in membership test [LRM 4.5.2/10]
>  okay := ch IN affirmatives;
>end muell3;

The error message from the compiler is correct type CHARACTER clashes with
type affirmatives.  The only way to do what you want is to cheat using 'value
and 'image like so

okay := affirmatives'VALUE(CHARACTER'IMAGE(ch)) in affirmatives;

and even this may not work because not all values of type CHARACTER have
a 'IMAGE.

A much better method of doing this in Ada is to use a table lookup like so:

type CHAR_SET is array(CHARACTER) of BOOLEAN;
AFFIRMATIVES : CHAR_SET := CHAR_SET'('y'|'Y'|'j'|'J'|'o'|'O' => TRUE,
				others => FALSE);

then use the following for the test

okay := AFFIRMATIVES(ch);

--
Mark Biggar
mab@wdl1.wdl.loral.com






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

* Re: Membership in "set" of characters
  1993-03-19 19:57 ` Mark A Biggar
@ 1993-03-22 10:37   ` Andrew Dunstan,,2285592,
  1993-03-22 12:22     ` Debora Weber-Wulff
  1993-03-22 15:49   ` Ada 9X references M. Ramchandran
  1 sibling, 1 reply; 8+ messages in thread
From: Andrew Dunstan,,2285592, @ 1993-03-22 10:37 UTC (permalink / raw)


From article <1993Mar19.195712.9187@wdl.loral.com>, by mab@wdl39.wdl.loral.com (Mark A Biggar):
> In article <J6QBBP3L@math.fu-berlin.de> dww@math.fu-berlin.de (Debora Weber-Wulff) writes:
>>with text_io; use text_io;
>>procedure muell3 is
>>type affirmatives is ('y', 'Y', 'j', 'J', 'o', 'O');
>>  ch : CHARACTER;
>>  okay : Boolean;
>>begin
>>  get (ch);
>>  -- The next statement gets flagged with
>>  -- type clash in membership test [LRM 4.5.2/10]
>>  okay := ch IN affirmatives;
>>end muell3;
> 
> The error message from the compiler is correct type CHARACTER clashes with
> type affirmatives.  The only way to do what you want is to cheat using 'value
> and 'image like so
>

The 'character' literals are not characters, but the (overloaded)
names of the values of an enumeration type. That is why there is a
type clash.

Better still, let's ask WHY you want to do this.

Are the affirmatives the only valid responses? If so, then use
enumeration_io and trap the exception data_error. 

If not, if any character value will do, use the approach already
suggested by Mark.

If the range of valid responses is more limited, decalre an
enumeration type with all the affirmative responses at one end and the
rest at the other end, and then declare affirmative as a subrange of
valid_responses. then get your response (using enumeration_io and
check to see if it is in the subrange.


cheers

andrew dunstan.
#######################################################################
#  Andrew Dunstan                   #   There's nothing good or bad   #
#  Department of Computer Science   #   but thinking makes it so.     #
#  University of Adelaide           #                                 #



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

* Re: Membership in "set" of characters
  1993-03-22 10:37   ` Andrew Dunstan,,2285592,
@ 1993-03-22 12:22     ` Debora Weber-Wulff
  1993-03-22 17:11       ` Robert I. Eachus
  0 siblings, 1 reply; 8+ messages in thread
From: Debora Weber-Wulff @ 1993-03-22 12:22 UTC (permalink / raw)



Thanks to all who wrote with information about my problems
with membership and FIXED/FLOAT. Here's a short summary of the
other answers I received:

FIXED/FLOAT

Just using 
  type DM is delta 0.01 range 0.0 .. 1000.0; 
gets me in trouble because the delta is a positive fraction
that causes rounding errors. I need
  for DM'small use 0.01;
to make it work as I expect, but I've been advised to leave FIXED
alone until I understand more about Ada :-) 

I've included a complete letter with some great advice at the end
of this summary.

There's an article in Ada Europe'92 proceedings [is that published
in LNCS? I've not heard of it before, but they seem to be in
their 13th year! Is there a FAQ for this group??? ] by Ben Brosgol,
Robert Eachus and Dave Emery about Ada Decimal Arithmetic.

ajpo.sei.cmu.edu (128.237.2.253) has a library of representations
for decimal arithmetic at ADAR-1.0.tar.Y and a book called
"Ada Quality and Style"

Membership Problem

Lots of suggestions!
* Use subtypes!
* use a case statement!
* make a function is_in (c:character; s:string) return Boolean is ...
* use okay := affirmatives'VALUE (CHARACTER'IMAGE(ch)) IN affirmatives;
* roll my own set as described below - that sounds like the
way I want to go. It reads a tad easier than the previous 
suggestion :-)

----- included file -----
In article <J6QBBP3L@math.fu-berlin.de> you write:
> This is another one of those "why won't this work?" for the experts.
> I'm stumped. As far as I can decode the LRM and
> see from 2 textbooks I consulted, this ought to work.

nope

> -- Why won't membership work here?

here's the short answer:

  for the type of thing you are doing, you can create a set ADT.
a simple way to do that in Ada for a relatively small discrete universe
like character is to use a boolean array.  you can pack it to make it
smaller, which effectively creates a bitmap. this is what Pascal sets
basically are - and this way Ada doesnt have to explicitly support
sets in the language. its just one kind of data type that you can build.

type char_predicates is array (character) of boolean;
pragma pack (char_predicates);
affirmative : constant char_predicates
 := char_predicates'('y'|'Y'|'j'|'J'|'o'|'O' => true, others => false);
...

okay := affirmative(ch);

you could also make affirmative a function instead of a constant array
to save space.  you could do this later if necessary without changing
the clients because the syntax for a function call is identical
to that of an array reference.

its a little more typing than Pascal in this case, but its
much easier to tell whats going on.

> I'm currently quite frustrated with the type system after writing a little program
> to calculate the return on investment.
> Is it me? Is it Meridian? Or is this the agony of using Ada?

It takes a while to get used to Ada's strong typing.  It seems overly
strict at first, but once you understand the rules well, they dont get
in the way very much at all.  in fact, they often help prevent errors.
still that initial period can be rough, since its easy to make things
much stricter than you need without realizing it.  and then blame the
language when it is enforcing exactly the rules that you asked for.
derived types are a frequent culprit.  they can be very helpful when
you need them, but can easily make it difficult to write any
expression if over-used.

you can get very similar behavior to other languages like Pascal with
respect to numeric types if you wish.

1.  avoid derived types until you have learned the rest of Ada well.
  then use them judiciously and carefully.
  (derived types have the form "type xxx is new yyy")
  (also avoid the form "type xxx is range a .. b" at first)

2. use subtypes instead, "subtype xxx is yyy range a .. b"

3. avoid fixed point, use float instead.

4. if you frequently use a mixed mode operation (i.e. multiply integers
and floats to get floats) then define a function so that you dont
have to keep performing conversions.

  function "*" (left : integer; right : float) return float is ...

5. when you feel comfortable with Ada, get a few books and try out
derived types and floats. they do have good uses.  the book
Ada Quality and Style available on ajpo.sei.cmu.edu has a section
on derived types and subtypes that might help (disclaimer:
I helped write that part)  the FAQ on the same machine describes
several good books as well (thanks to Mike Feldman).

good luck. 

P.S. Meridian is a pretty good compiler, but if you get the chance
or have something really serious, use DECs.
-- 
---------------------------------------------------
Alex Blakemore alex@cs.umd.edu   NeXT mail accepted

----- end included file -----

-- 
Debora Weber-Wulff, Professorin fuer Softwaretechnik
snail: Technische Fachhochschule, FB Informatik, 
       Luxemburgerstr. 10, 1000 Berlin 65
email: dww@informatik.tfh-berlin.dbp.de 



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

* Ada 9X references
  1993-03-19 19:57 ` Mark A Biggar
  1993-03-22 10:37   ` Andrew Dunstan,,2285592,
@ 1993-03-22 15:49   ` M. Ramchandran
  1993-03-22 21:50     ` Alex Blakemore
  1993-03-23  4:16     ` Michael Feldman
  1 sibling, 2 replies; 8+ messages in thread
From: M. Ramchandran @ 1993-03-22 15:49 UTC (permalink / raw)


Can anyone send me a list of Ada 9X references and I will also be grateful if
anyone can send me some copies of actual papers if possible?


Chandran
School of computing and Maths
Liverpool John Moores Uni.
Liverpool L3 3AF



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

* Re: Membership in "set" of characters
  1993-03-22 12:22     ` Debora Weber-Wulff
@ 1993-03-22 17:11       ` Robert I. Eachus
  0 siblings, 0 replies; 8+ messages in thread
From: Robert I. Eachus @ 1993-03-22 17:11 UTC (permalink / raw)



    Just to avoid giving fixed point a bad name...

   > Just using 
   >   type DM is delta 0.01 range 0.0 .. 1000.0; 
   > gets me in trouble because the delta is a positive fraction
   > that causes rounding errors. I need
   >   for DM'small use 0.01;
   > to make it work as I expect, but I've been advised to leave FIXED
   > alone until I understand more about Ada :-) 

   The 'small clause may make things work the way that you expect
(assuming your compiler supports it).  In any case the ADAR packages
should help.  Fixed point arithmetic--independant of Ada--is hard,
however if you know how to use it you can and will get much more
accurate results than using floating point.  Floating point can be
easily used to give you an answer which is very close to correct,
fixed point allows you to always get the exact right answer, but is
much harder to use.  The trick is that you must at every point be
aware of the ACCURACY and PRECISION of the data--and be aware that
they are two different things.  When I am writing such code, I find
myself doing several passes looking at each operation to be sure I
know if and when any rounding, truncation or change in precision
occurs.




--

					Robert I. Eachus

with Standard_Disclaimer;
use  Standard_Disclaimer;
function Message (Text: in Clever_Ideas) return Better_Ideas is...



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

* Re: Ada 9X references
  1993-03-22 15:49   ` Ada 9X references M. Ramchandran
@ 1993-03-22 21:50     ` Alex Blakemore
  1993-03-23  4:16     ` Michael Feldman
  1 sibling, 0 replies; 8+ messages in thread
From: Alex Blakemore @ 1993-03-22 21:50 UTC (permalink / raw)


chandran@comp.lancs.ac.uk (M. Ramchandran) writes:
> Can anyone send me a list of Ada 9X references?

ftp to ajpo.sei.cmu.edu:/public/ada9x/ada9x.reports
-- 
---------------------------------------------------
Alex Blakemore alex@cs.umd.edu   NeXT mail accepted



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

* Re: Ada 9X references
  1993-03-22 15:49   ` Ada 9X references M. Ramchandran
  1993-03-22 21:50     ` Alex Blakemore
@ 1993-03-23  4:16     ` Michael Feldman
  1 sibling, 0 replies; 8+ messages in thread
From: Michael Feldman @ 1993-03-23  4:16 UTC (permalink / raw)


In article <1993Mar22.154915.5081@comp.lancs.ac.uk> chandran@comp.lancs.ac.uk (M. Ramchandran) writes:
>Can anyone send me a list of Ada 9X references and I will also be grateful if
>anyone can send me some copies of actual papers if possible?
>
>
ftp to ajpo.sei.cmu.edu, change to directory public/ada9x, and browse away.
All the 9x reports are there, and a lot of language notes representing
discussions by the team.

ftp is the easiest way to get this stuff, especially across the Pond
(unless there's a good source of the hardcopy over there).


Mike Feldman



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

end of thread, other threads:[~1993-03-23  4:16 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1993-03-19 17:06 Membership in "set" of characters Debora Weber-Wulff
1993-03-19 19:57 ` Mark A Biggar
1993-03-22 10:37   ` Andrew Dunstan,,2285592,
1993-03-22 12:22     ` Debora Weber-Wulff
1993-03-22 17:11       ` Robert I. Eachus
1993-03-22 15:49   ` Ada 9X references M. Ramchandran
1993-03-22 21:50     ` Alex Blakemore
1993-03-23  4:16     ` Michael Feldman

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