[comp.lang.modula2] Structured Types

markh@csd4.milw.wisc.edu (Mark William Hopkins) (04/17/88)

     Consider the following two segments:

     A := 3;                A[1] := 3;
     B := A;                B := A;
     WriteCard(B)           WriteCard(B[1])

They both do the same thing.  In the first, we can see that 3 will be
written out by performing the successive transformations (beta reductions):

A := 3; B := A; WriteCard(B) --> B := 3; WriteCard(B) --> WriteCard(3)

However, were we to try this with the latter segment, we would be blocked.
There is nothing that explicitly says that B[1] is assigned the value of
A[1].

     This points out a major flaw in Modula-2 and Pascal.  They totally
lack FIRST-CLASS structured types.  It is an inconsistency that provides
major obstacles to elegant programming.  On one hand assignments to
structured types are allowed, on the other there are no actual VALUES to be
assigned.

     There are two ways to go with this.  Either one could eliminate
assignments to structured types -- essentially B := A is just a syntatic
sugaring of the corresponding for loop -- or one could include structured
EXPRESSIONS and CONSTANTS.

     The first alternative is really a step backwards ... and it is not
in line with the implicit goals of Modula-2's design.  The second, I 
believe, COMPLETES the design of the language.

     We need to extend the expression syntax to include STRUCTURED 
EXPRESSIONS, examples:

             ARRAY I : 1..3 OF 1:4, 2:5, 3:8 END

	     ARRAY I : 3..6 OF 2*I END

	     RECORD Field : Value, Field : Value ... END

and the like.

     Assignments to components would then be interpreted as in the following
example:

    A[1]:= 3    becomes      A := ARRAY I:Range OF 1:3 ELSE A[I]

which is much like an update statement such as:  X := X + 1.

     Evaluation takes place in the obvious manner, examples:

     ARRAY I:2..3 OF 2:4, 3:5 END [2] --> 4

     RECORD Field1 : Val1, Field2: Val2 END . Field1 --> Val1


     These ideas entail a small modification in the language.  However
the ramifications for implementation need to be discussed.  For example,
can the static type-checking scheme of the Pascal-like languages handle
such an extension?

   The key to these observations is that Modula-2 and Pascal both lack
first-class structured types.  As far as they incorporate strucured types
in their syntax, this represents an inconsistency that often forces the
user to develop roundabout means of expressing what the language restricts.

seitz@cory.Berkeley.EDU (Matthew Eric Seitz) (04/17/88)

In article <5572@uwmcsd1.UUCP> markh@csd4.milw.wisc.edu (Mark William Hopkins) writes:
>
"     Consider the following two segments:
"
"     A := 3;                A[1] := 3;
"     B := A;                B := A;
"     WriteCard(B)           WriteCard(B[1])
"
"They both do the same thing.  In the first, we can see that 3 will be
"written out by performing the successive transformations (beta reductions):
"
"A := 3; B := A; WriteCard(B) --> B := 3; WriteCard(B) --> WriteCard(3)
"
"However, were we to try this with the latter segment, we would be blocked.
"There is nothing that explicitly says that B[1] is assigned the value of
"A[1].
"[...]
"     There are two ways to go with this.  Either one could eliminate
"assignments to structured types -- essentially B := A is just a syntatic
"sugaring of the corresponding for loop -- or one could include structured
"EXPRESSIONS and CONSTANTS.
"[...]

	I don't see why you are blocked.  Modula-2 does implement your first
suggestion:  it is a sugaring of the corresponding for loop.  See _Programming_
In_Modula-2_ (3rd ed.) pp. 37, 1O5.  This does explicitly say that B[1] is 
assigned the value of A[1].  Also, this does not eliminate assignments to 
structured types.

						Matthew Seitz
						seitz@cory.berkeley.edu