[comp.lang.pascal] An ISO Pascal question...

mootaz@titan.rice.edu (Elmootazbellah Nabil Elnozahy) (03/11/89)

Given the following piece of code, can anybody familiar with the ISO
standard tell me if it is legal or not:


Type
	T = real;
	R = Record
		T: integer;
		D: T;
	    end;

	B = Record
		T: integer;
		C: Record
			D: T;
		   end;
	     end;

In particular, is field D in record R of type T or is it an illegal definition?
Similarly for field D in record C. I am not interested in what a particular
implementation does, just what the standard says about it.

Thanks in advance...

jb3o+@andrew.cmu.edu (Jon Allen Boone) (03/12/89)

I believe that it is an illegal declaration, since it would first assign T to
type Real, and then change it to a variable of type Integer...so D would be
assigned to an undefined type .... at least that was what I was taught...

Iain

abcscnuk@csuna.csun.edu (Naoto Kimura) (03/12/89)

In article <2824@kalliope.rice.edu> mootaz@titan.rice.edu (Elmootazbellah Nabil Elnozahy) writes:
]Given the following piece of code, can anybody familiar with the ISO
]standard tell me if it is legal or not:
]
]
]Type
]	T = real;
]	R = Record
]		T: integer;
		^^^^^^^^^^	T is now a field of type 'integer'
				in the record type 'R' this scope.
]		D: T;
		   ^		You'll run into trouple here,
				because 'T' is a field of 'R'
]	    end;
]
] ... (declaration deleted, had same problem) ...
] ... (text deleted) ...

I don't have the Doug Cooper book (I know, I know, shame on me for not
having J&W) with me now, since I've lent it to someone, but as far as I
know it is fine to have the field 'T' of type integer, but within
the scope of the record type 'R', 'T' is a field of type
integer, not the type 'T'.

                //-n-\\			 Naoto Kimura
        _____---=======---_____		 (abcscnuk@csuna.csun.edu)
    ====____\   /.. ..\   /____====
  //         ---\__O__/---         \\	Enterprise... Surrender or we'll
  \_\                             /_/	send back your *&^$% tribbles !!

abcscnuk@csuna.csun.edu (Naoto Kimura) (03/12/89)

Ooops, forgot to answer the question...  No the declarations as in the
original posting are not legal.  There isn't a problem in having a field
'T' in the record, but you can't use 'T' as a type, because within the
scope of the record type, 'T' is a field of type integer, not the same
'T' which is a type that is real.

The following program may help to clarify some things... :-)
---- cut here -------- cut here -------- cut here ----
program strange (output);
const
   writeln =  1;
   readln  = 50;
type
   real    = writeln..readln;
   write   = (eof, eoln);
   read    = char;
   boolean = packed array [ real ] of read;
var
   get     : read;
   true,
   false   : write;
   dispose : boolean;

procedure maxint  ( var eoln : text;
                        get  : boolean;
                        eof,
                        read : real     );
   forward;

procedure integer  ( var eof   : text;
                         get   : boolean;
                         read,
                         write : real    );
   begin
      if read < write then begin
         maxint ( eof, get, write, read+1 );
         eof^ := get[read];
         put (eof);
      end;
   end;

procedure maxint;
   begin
      if read < eof then begin
         integer ( eoln, get, read, eof-1 );
         eoln^ := get[eof];
         put (eoln);
      end;
   end;

begin
   true  := eof;
   false := eof;
   dispose := '     .ri rvgitmsso agr iT hspormde oehn eywed     ';
   if true = false then begin
      integer ( output, dispose, writeln, readln );
   end;
end.
---- cut here -------- cut here -------- cut here ----

BTW.  The above program should compile on compilers that follow the ISO
standard.

                //-n-\\			 Naoto Kimura
        _____---=======---_____		 (abcscnuk@csuna.csun.edu)
    ====____\   /.. ..\   /____====
  //         ---\__O__/---         \\	Enterprise... Surrender or we'll
  \_\                             /_/	send back your *&^$% tribbles !!

piet@ruuinf (Piet van Oostrum) (03/14/89)

In article <2824@kalliope.rice.edu>, mootaz@titan (Elmootazbellah Nabil Elnozahy) writes:
 `Given the following piece of code, can anybody familiar with the ISO
 `standard tell me if it is legal or not:
 `
 `
 `Type
 `	T = real;
 `	R = Record
 `		T: integer;
 `		D: T;
 `	    end;
 `
 `	B = Record
 `		T: integer;
 `		C: Record
 `			D: T;
 `		   end;
 `	     end;
 `
 `In particular, is field D in record R of type T or is it an illegal definition?
 `Similarly for field D in record C. I am not interested in what a particular
 `implementation does, just what the standard says about it.
 `

Legal, because field names are in a different ``namespace'' than other
identifiers. In fact each record type has its own namespace.
-- 
Piet van Oostrum, Dept of Computer Science, University of Utrecht
Padualaan 14, P.O. Box 80.089, 3508 TB Utrecht, The Netherlands
Telephone: +31-30-531806. piet@cs.ruu.nl (mcvax!hp4nl!ruuinf!piet)

randy@m2xenix.UUCP (Randy Bush) (03/15/89)

mootaz@titan.rice.edu (Elmootazbellah Nabil Elnozahy) asks if the
following is legal Pascal:

  T = real;
  R = Record
    T: integer;
    D: T;
    end;
  B = Record
    T: integer;
    C: Record
      D: T;
      end;
    end;

To the best of my knowledge, the above fragment is quite correct.  [ Being
somewhat prudent, I also looked it up. ]

The fieldnames are in a separate 'region', and thus do not conflict with
the name of the type T.

Of course, this is not the clearest possible programming style, but we all
knew that.

I would be interested in knowing of compilers which do not accept this
fragment, and of those which do.

By net mail please, of course, and I will summarize if it is enlightening.

randy
-- 
{ mcvax!uunet!oresoft, tektronix!percival!qiclab } !m2xenix!randy  Randy Bush

milne@ics.uci.edu (Alastair Milne) (03/15/89)

mootaz@titan.rice.edu (Elmootazbellah Nabil Elnozahy) writes:
>... can anybody familiar with the ISO standard tell me if it is legal or not:
>
>Type
>	T = real;
>	R = Record
>		T: integer;
>		D: T;
>	    end;

    I am interested in hearing exactly what ISO says about this.  I think the
    question is: at exactly what point in the declarations does the type R
    become defined (at some point within it, or only after its "end"?); and 
    at what point do its components become defined?  If, for instance, 
    the field "T" is not yet formally defined when "D" is declared, there is 
    nothing wrong with this declaration; but if it is, then within the scope
    of the record, "T" doesn't name a type.

    Alastair Milne

corbett@beatnix.UUCP (Bob Corbett) (03/16/89)

In article <1195@ruuinf.UUCP> piet@ruuinf (Piet van Oostrum) writes:
>In article <2824@kalliope.rice.edu>, mootaz@titan (Elmootazbellah Nabil Elnozahy) writes:
> `Given the following piece of code, can anybody familiar with the ISO
> `standard tell me if it is legal or not:
> `
> `
> `Type
> `	T = real;
> `	R = Record
> `		T: integer;
> `		D: T;
> `	    end;
> `
> `	B = Record
> `		T: integer;
> `		C: Record
> `			D: T;
> `		   end;
> `	     end;
> `
> `In particular, is field D in record R of type T or is it an illegal definition?
> `Similarly for field D in record C. I am not interested in what a particular
> `implementation does, just what the standard says about it.
> `
>
>Legal, because field names are in a different ``namespace'' than other
>identifiers. In fact each record type has its own namespace.
>-- 
>Piet van Oostrum, Dept of Computer Science, University of Utrecht
>Padualaan 14, P.O. Box 80.089, 3508 TB Utrecht, The Netherlands
>Telephone: +31-30-531806. piet@cs.ruu.nl (mcvax!hp4nl!ruuinf!piet)

No, it is not legal.  Pascal is not C.  The text that follows is from
ANSI/IEEE 770 X3.97-1983 (left my copy of ISO 7185 at home).  On this
point, and most others, the two standards agree.

    6.2.2.1.  Each identifier or label contained by the program-block shall have
    a defining-point.

    6.2.2.2.  Each defining-point shall have a region that is a part of the
    program text, and a scope that is a part or all of that region.

    6.2.2.5.  When an identifier or label has a defining-point for region A and
    another identifier or label having the same spelling has a defining-point
    for some region B enclosed by A, then region B and all regions enclosed by
    B shall be excluded from the scope of the defining-point for region A.

    6.2.2.6.  The region that is the field-specifier of a field-designator shall
    be excluded from the enclosing scopes.

    From Section 6.4.3.3:
    The occurrence of an identifier in the identifier-list of a record-section
    of a fixed-part of a field-list shall constitute its defining-point as a
    field-identifier for the region that is the record-type closest-containing
    the field-list, and shall associate the field-identifier with a distinct
    component, which shall be designated a field, of the record-type and of the
    field-list.

Section 6.2.2.6 makes field references legal where the field-identifier has
the same spelling as an identifier defined in an enclosing region.  It does
not apply to the cases shown in the example.

Section 6.4.3.3 specifies that the region of a field-identifier defined in a
record type is the closest-containing record type.  Therefore, the scope of
any identifier with the same spelling as a field-identifier defined in a
record-type is excluded from the region that is the record type.  Therefore,
in both record-types, T refers to the field-identifiers, not the
type-identifiers.

					       Yours truly,
					       Bob Corbett
					       uunet!elxsi!corbett
					       ucbvax!sun!elxsi!corbett

bobd@ihf1.UUCP (Bob Dietrich) (03/21/89)

In article <2546@elxsi.UUCP> uunet!elxsi!corbett (Bob Corbett) writes:

>No, it is not legal.  Pascal is not C.  The text that follows is from
>ANSI/IEEE 770 X3.97-1983 (left my copy of ISO 7185 at home).  On this
>point, and most others, the two standards agree.
>
>					       Bob Corbett

I believe Mr. Corbett is correct in his statements that the example is
incorrect Pascal. Another way of thinking about the problem is that an
identifier can be used in only one way within a region; that is, it cannot be
used as a type and a field identifier within the same scope (see 6.2.2.8).

The example declarations define a field T within the scope of the record
declaration (the region between "record" and "end"). This establishes the use
of T within that scope as a field identifier. Then another field is declared
using T as if it was a type identifier. This conflicts with the rule that an
identifier can only be used in one way within a scope, and thus is a
violation.

To clarify a bit, the term "region" refers to what can be thought of a piece
of program text. A block (declarations plus the compound statement), an
identifier, or a record declaration are all examples of regions.

The notion of "scope" in Pascal uses regions. Scope defines what an
identifier means within a particular set of regions. These regions may be
nested, as with nested procedure declarations, or they may be disjoint. The
latter case occurs when you forward declare a procedure. The parameters are
defined for a scope consisting of a region that is the procedure heading and
a region that is the block of the procedure. This actually occurs for normal
procedure declarations, but is easier to think of when forward is used. There
are other cases as well as this one, but I think you get the point.

usenet:	uunet!littlei!intelhf!ihf1!bobd		Bob Dietrich
  or	tektronix!ogccse!omepd!ihf1!bobd	Intel Corp., Hillsboro, Oregon
  or	tektronix!psu-cs!omepd!ihf1!bobd	(503) 696-2092

scl@virginia.acc.virginia.edu (Steve Losen) (03/22/89)

>mootaz@titan.rice.edu (Elmootazbellah Nabil Elnozahy) asks if the
>following is legal Pascal:
>
>  T = real;
>  R = Record
>    T: integer;
>    D: T;
>    end;
>  B = Record
>    T: integer;
>    C: Record
>      D: T;
>      end;
>    end;

The above is not legal ISO standard Pascal.  While it is legal to
redefine an identifier used in an enclosing scope, under no
circumstances is it legal for an identifier to have two "meanings"
in the same scope.  All "namable" objects in Pascal share the same
name space.  Thus you may refer to a type X and then define a
variable called X in the same scope.  Looking at the example:

  T = real;      { At first the scope of type T is the whole block }
                 { and everything it encloses }.
  R = Record
    T: integer;  { By defining a field named T, we have removed record R }
                 { from the scope of type T }
    D: T;        { illegal.  T refers to the previous field. }
    end;

  B = Record
    T: integer;  { By defining a field T we have removed record B and }
                 { everything it encloses from the scope of type T }
    C: Record
      D: T;      { illegal.  Refers to field T in record B }
      end;
    end;

For some real fun, lets change the field orderings around and see what
happens.

  T = real;
  R = Record
    D: T;         { By referencing type T, we have irrevocably kept }
                  { record R within the scope of type T }

    T: integer;   { illegal.  We cannot redefine T because the Standard }
                  { insists that within the scope of an identifier all }
                  { occurrences of the identifier shall refer to the same }
                  { thing. }
    end;

  B = Record
    C: Record
      D: T;       { This reference to type T irrevocably keeps this }
                  { anonymous record within the scope of type T.  Record B }
                  { (excluding this anonymous record) can still be }
                  { removed from the scope of type T as we shall see.}
      end;
    T: integer;   { This is perfectly legal.  The scope of field T is }
                  { now record B (excluding the anonymous record, which }
                  { is within the scope of type T). }

If you don't believe me, did you just finish writing an ISO Standard
Pascal compiler?
-- 
Steve Losen     scl@virginia.edu
University of Virginia Academic Computing Center

corbett@beatnix.UUCP (Bob Corbett) (03/23/89)

In article <2158@virginia.acc.virginia.edu> scl@virginia.acc.Virginia.EDU
(Steve Losen) writes:

>>mootaz@titan.rice.edu (Elmootazbellah Nabil Elnozahy) asks if the
>>following is legal Pascal:
>>
>>  T = real;
>>  B = Record
>>    T: integer;
>>    C: Record
>>      D: T;
>>      end;
>>    end;
>
>The above is not legal ISO standard Pascal.  While it is legal to
>redefine an identifier used in an enclosing scope, under no
>circumstances is it legal for an identifier to have two "meanings"
>in the same scope.

So far, so good.  But...

>For some real fun, lets change the field orderings around and see what
>happens.
>
>  B = Record
>    C: Record
>      D: T;       { This reference to type T irrevocably keeps this }
>                  { anonymous record within the scope of type T.  Record B }
>                  { (excluding this anonymous record) can still be }
>                  { removed from the scope of type T as we shall see.}
>      end;
>    T: integer;   { This is perfectly legal.  The scope of field T is }
>                  { now record B (excluding the anonymous record, which }
>                  { is within the scope of type T). }
>

This construction is not legal.  The following statements are from ISO 7185:

    6.2.2.5  When an identifier or label has a defining-point for region A
    and another identifier or label having the same spelling has a defining
    point for some region B enclosed by A, then region B and all regions
    enclosed by B shall be excluded from the scope of the defining-point
    for region A.

Note the phrase "and all regions enclosed by B."  In the example above, the
inner record-type is a region enclosed by the region that is the outer
record-type.

    From Section 6.4.3.3:
    The occurrence of an identifier in the identifier-list of a record-section
    of a fixed-part of a field-list shall constitute its defining-point as a
    field-identifier for the region that is the record-type closest-containing
    the field-list, and shall associate the field-identifier with a distinct
    component, which shall be designated a field, of the record-type and of
    the field-list.

The region associated with the defining-point of the field-identifier T is
the outer record-type and all regions enclosed by the outer record-type,
which includes the inner record-type.  Therefore, the inner record-type is
excluded from the scope of the defining-point of the type-identifier T.
Therefore, the use of T as a type-identifier in the inner record-type is
illegal.

>If you don't believe me, did you just finish writing an ISO Standard
>Pascal compiler?

No, I just started.

>-- 
>Steve Losen     scl@virginia.edu
>University of Virginia Academic Computing Center

					Yours truly,
					Bob Corbett
					uunet!elxsi!corbett
					ucbvax!sun!elxsi!corbett

pepers@cpsc.ucalgary.ca (Bradley Pepers) (03/25/89)

Where can I get the full ISO and ASCII specifications for Pascal? A snailmail
address would be best.