Path: utzoo!attcan!uunet!husc6!mailrus!ames!pasteur!ucbvax!durham.ac.UK!Barry_Cornelius
From: Barry_Cornelius@durham.ac.UK
Newsgroups: comp.lang.modula2
Subject: more about ORD and VAL
Message-ID: 
Date: 27 Jun 88 11:17:41 GMT
Sender: daemon@ucbvax.BERKELEY.EDU
Reply-To: Info-Modula2 Distribution List 
Organization: The Internet
Lines: 226

I thank Gernot Heiser for his comments (24Jun at 0812GMT) on my
description of type changes in Modula-2.   I essentially agree with
Gernot.

I submitted a reasonably in-depth analysis of the problem over VAL
with INTEGERs to "Modus Quarterly" last September --- it got accepted
but MQ hasn't appeared for a long time.   (* Insert an appropriate
e-mail smile/groan symbol at this point *)

My article also attempted an analysis of the definitions of
ORD and VAL with subrange types.

The rest of this message contains the article.
==
Barry Cornelius
==
Address:
   Computer Science Group, School of Engineering and Applied Science,
   University of Durham, Durham, DH1 3LE, England
Telephone:
   My office: Durham (091 or +44 91) 374 2638
   Secretary: Durham (091 or +44 91) 374 2630
   Fax:       Durham (091 or +44 91) 374 3740
Electronic Mail Addresses:
   JANET:       Barry_Cornelius@uk.ac.dur.mts
   Internet:    Barry_Cornelius%mts.dur.ac.uk@cunyvm.cuny.edu
   UUCP:        ...ukc!cs.nott.ac.uk!bjc
   BITNET/EARN: Barry_Cornelius%DUR.MTS@AC.UK
==
            Problems with the Definitions of ORD and VAL

                    Issue 2:   1st September 1987

                           Barry Cornelius

                   Computer Science Subject Group
              School of Engineering and Applied Science
                        University of Durham
                      Durham  DH1 3LE  England


1. Introduction

The Modula-2 Report defines ORD(x) by:

      ordinal number (of type CARDINAL) of x in the set of values
      defined by type T of x.   T is any enumeration type, CHAR,
      INTEGER, or CARDINAL.

and defines VAL(T,x) by:

      the value with ordinal number x and with type T.   T is any
      enumeration type, or CHAR, INTEGER, or CARDINAL.
      VAL(T,ORD(x))=x, if x of type T.

However, there are difficulties with these definitions when T is a
subrange type or is the type INTEGER.  It is the aim of this paper to
explain these difficulties.


2. Terminology

The terms "whole-number-type" and "exception" are used in this paper.

      By a "whole-number-type" we mean an integer or cardinal type.
      (We speak of "a" cardinal type in case there is more than one.)
      By an "exception" we mean a run-time event beyond which the
      semantics of the program is undefined.  (Implementations may
      produce a warning on an exception, provide some non-standard
      recovery or just continue processing.)

The above definitions are taken from the paper "Type Conversions in
Modula-2" by Brian Wichmann ("MODUS Quarterly" Issue 6, pp. 21-24).


3. Use of ORD with Subrange types

Given:

      TYPE
         months=[1..12];
         lengths=[28..31];
      VAR
         m:months;
         n:lengths;
      ...
      m:= 1;
      n:= 28;

what is the value of ORD(n)?

Some have argued that ORD(n) should deliver 0 because the value of n
is the first value in the set of values of the type of n.  However,
this interpretation would cause difficulties for calls like ORD(n-1)
and ORD(n+m).  It would also mean that the ordinal number of a value
changes depending on the subrange that is chosen.

Instead the value of ORD(n) is 28.  This is because of the following
reasons.  In general, the parameter to ORD is an expression.  Now,
any operand in an expression which is a variable of a subrange type
is treated as if it were of the host type of the subrange type.
Thus, the n in ORD(n) is considered to be of type CARDINAL and so
ORD(n) has the value 28.  If this approach is adopted then all the
problems disappear.  Pascal does it in this way --- section 6.7.1 of
the ISO Pascal Standard says:

      Any factor whose type is S, where S is a subrange of T,
      shall be treated as of type T.


4. Use of VAL with Subrange types

Given the types:

      TYPE
         day=(sun, mon, tue, wed, thu, fri, sat);
         work=[mon..fri];

then there is probably no disagreement that VAL(day,1) has the value
mon.  But how about VAL(work,1)?

It might be argued that VAL ought to be illegal when T is a subrange
type because Wirth's definition of VAL states that "T is any
enumeration type, or CHAR, INTEGER, or CARDINAL" and thus subrange
types are not included.  However, I guess most people would argue
that this was not intended.

Although some would argue that VAL(work,1) ought to have the value
tue, I believe that VAL(work,1) also has the value mon.  Recall that
VAL(T,x) is defined as "the value with ordinal number x and with
type T". Now, the ordinal number of the value mon is 1 and mon is
also a value of the type work.  Hence, it satisfies the definition.
Note that the condition VAL(T,ORD(x))=x also holds.

From this, it follows that VAL(work,0) should lead to an exception
since there is no value of the type work that has ordinal number 0.


5. Use of ORD and VAL with the type INTEGER

What is the value of ORD(-1)?  There seems to be (at least) four
possible answers:

(a)   -MIN(INTEGER)-1

(b)   -1

(c)   1

(d)   an exception

I'll look at each of these in turn.


5.1 ORD(-1) has the value -MIN(INTEGER)-1

The Modula-2 Report states that ORD delivers a value of type
CARDINAL.  The way to map all the INTEGER values onto CARDINAL values
is as follows:

      ORD(-32768) =     0
      ...
      ORD(    -1) = 32767
      ORD(     0) = 32768
      ORD(     1) = 32769
      ...
      ORD( 32767) = 65535

Here I have assumed particular values for MIN(INTEGER) and
MAX(INTEGER) to help me understand things!

There are problems with this proposal:

(a)   It means that the value of ORD(1) depends on its context.
      It may be equal to 1 or 32769 depending on whether an INTEGER
      or CARDINAL value is expected.

(b)   If c is a CARDINAL variable having the value 1 then
      VAL(INTEGER,c) would have the value -32767.  Thus, to convert a
      numerical value from CARDINAL to INTEGER, one would need to use
      something like VAL(INTEGER,c+32768).

(c)   It assumes that the number of values of type INTEGER is not
      more than the number of values of type CARDINAL.

(d)   It will need some amendment to cope with the ordinal numbers of
      the type LONGINT.


5.2 ORD(-1) has the value -1

Section 6.4.2.2 of the ISO Pascal Standard states that "the ordinal
number of a value of integer-type shall be the value itself".  Hence,
in Pascal, ORD(-1) has the value -1.  The major difficulty with using
this in Modula-2 is that, in Modula-2, ORD delivers a CARDINAL.
Altering ORD to produce an INTEGER would cause problems for examples
like ORD(MAX(CARDINAL)).


5.3 ORD(-1) has the value 1

One obvious way of avoiding the negative numbers is to say that "the
ordinal number of a value of the type INTEGER shall be its absolute
value".  This, of course, leads to a problem with VAL(INTEGER,1).
Is this equal to -1 or 1?


5.4 ORD(-1) leads to an exception

On behalf of the BSI's Modula-2 Working Group, Don Ward and I have
recently been considering the formal definition of Modula-2's
standard procedures.  We propose that:

(a)   ORD delivers a CARDINAL

(b)   when n is of some whole-number-type, ORD(n) has the same
      numerical value as n, no matter what the type of n is provided
      that the numerical value of n belongs to the type CARDINAL

(c)   when n is of some whole-number-type, ORD(n) leads to an
      exception if the numerical value of n is not a value of the
      type CARDINAL

This proposal is written in terms of "whole-number-type".  Thus, it
is applicable not only when n is of type INTEGER but also when n is,
say, of the type LONGINT.