[comp.lang.modula2] for loops

ROSS@UCF1VM.BITNET (Bri) (07/04/90)

In regards to my FOR loop question, Peter states that such a construct
might introduce many difficulties into the loop control code.

I can not find any such difficulties. In fact, most other high-level langauges
will support such a sequence of statements. The RECORD example that he showed
is still not understandable to me. To repose the question I ask instead of
a record how about a POINTER TO INTEGER?
Again, the same rule applies that such a variable can not be used as an index.
To me, there is no difference between p@ and i where p is a POINTER TO INTEGER
and i is an INTEGER.

Anyone else have any answers as to why Wirth designed such a restriction?

Bri

jensting@skinfaxe.diku.dk (Jens Tingleff) (07/04/90)

ROSS@UCF1VM.BITNET (Bri) writes:

[..]

>Anyone else have any answers as to why Wirth designed such a restriction?

could be a continuation of the restriction from Pascal, i.e. a FOR
loop index MUST be a locally declared simple integral variable. Having
the syntax say ``Ident'' instead of ``Designator'' hints at this 
restriction (if not enforcing it, UGH! semantics in grammmmar are ugly..).

The reason for demanding a local variable is (or could be ?) that a
local variable can be detected to be non-aliased. I.E. that no context
switch can alter the loop control variable (a context switch such 
as an interupt). Since only global procedures are allowed as co-routines
(see p. 167 of PIM2 ed 3), it's impossible to change the value of 
a FOR loop index with a co-routine. Modula-2 also requires a local 
var, incidently, PIM2 ed 3 p. 158.


The following is thus illegal (pseducode..)

PROCEDURE lotawork();

  VAR controlvar, bar : INTEGER;

  PROCEDURE Dosomework():INTEGER;
  VAR foo : CARDINAL;
  BEGIN foo := 0;
    FOR controlvar := 0 TO 700000 DO		(* Not allowed %1	*)
      foo := foo + 1;
    END;
    RETURN foo;
  END Dosomework;

  PROCEDURE Stompit;
  BEGIN
    controlvar := 1000000; 
    ContinueAfterInterupt();
  END Stompit;

BEGIN (* lotawork *)
  SetToInteruptEverySplitSecond(Stompit);	(* Not allowed, %2 *)
  bar := Dosomework();
END lotawork;

where the value of `bar' just might be weird.
%1	Nonlocal variable leads to disaster for any call to Stompit.
%2	local procedure an actual for PROCESS formal parameter, not allowed.
So, this mayhem is illegal (phew).


This is, however, legal (???)

MODULE globalmodule;

FROM SYSTEM INPORT ADR;

TYPE intptrtyp = POINTER TO INTEGER;
VAR intptr : intptrtyp;

  PROCEDURE Stompit;
  BEGIN
    intptr^ := 10000000;
    ContinueAfterInterupt();
  END Stompit;

  PROCEDURE notsosafe;
    VAR i : INTEGER;
  BEGIN
    intptr := ADR(i);		(* Boo, aliasing *)
    FOR i := 0 TO 700000 DO 
      whatever();
    END;
  END notsosafe;

BEGIN (* globalmodule *)
  SetToInteruptEverySplitSecond(Stompit);
  notsosafe;
END globalmodule;

This last example, possible aliasing of `i', CAN be detected by the 
compiler, at which point a warning may be printed out.


ANYWAY. A slightly more levelheaded reason for requiring the FOR control
var to be simple and local is that it's easier to decide to make 
the loop-cntr. var a register var...... .

By The Way, new question :
  Is the FOR loop control var defined after the FOR statement ? Wirth
  (PIM2 ed 3 p 158) doesn't say not, in fact the paragraph reads

 	"FOR v := S TO B BY C DO SS END
	 expresses repeated execution of the stat. seq. SS with
	 v successively assuming the values A, A+C, A+2C, ..,
	 A+nC where A+nC is the last term not exceeding B."

  It says "v successively assuming the values", but not what v is AFTER
  the statement. Hmmm.

  Modula-3 (see page 25 of the revised report) says

	"FOR id := first TO last BY step DO S END;
	 .. The identifier id denotes a readonly variable whose
	 scope is S and whose type is the common basetype of first & last."
	 ^^^^^^^^^^
  right on. Likewise Occam-2 (and algol68 ?)


Jens

Jens Tingleff MSc EE, Institute of Computer Science, Copenhagen University
Snail mail: DIKU Universitetsparken 1 DK2100 KBH O
"It never runs around here; it just comes crashing down"
	apologies to  Dire Straits 

steve@cs.su.oz (Stephen Russell) (07/04/90)

In article <1990Jul4.074634.645@diku.dk> jensting@skinfaxe.diku.dk (Jens Tingleff) writes:
>ROSS@UCF1VM.BITNET (Bri) writes:
>>Anyone else have any answers as to why Wirth designed such a restriction?
>
>could be a continuation of the restriction from Pascal ...

This is the likely history.

>The reason for demanding a local variable is (or could be ?) that a
>local variable can be detected to be non-aliased. I.E. that no context
>switch ...

Context switches are not an issue, as they didn't exist in Pascal. Rather,
the restriction allows efficient implementation of For loops. Many loop
optimisations, such as induction variables for array access, rely on a
simple sequence of index variable values. The Pascal restriction (and the
intention in Modula2?) allows the compiler to easily check that no
assignment statements, or procedures called, in the body of the loop can
change the index variable.

Detecting changes to the index variable is made more difficult by
allowing arbitrary designators. For example,

	FOR a[i][k][j] := 1 TO 10 DO ...

requires that the compiler checks for changes to i, j, and k in the
loop. Aliasing makes things even tougher.  For example, if the body
of the above loop contained

	a[x][y][z] := 100

then the index variable is changed iff x=i, y=k and z=j. Similar
problems occur with pointer variables:

	FOR p^ := 1 TO 10 DO ... q^ := 100; ... END;

If p = q, then the index variable is changed, but proving that p # q
is impossible for the compiler in the general case.

So, by making designators illegal, the compiler can safely perform these
worthwhile optimisations.

As well, some machines provide efficient loop instructions (such as
decrement and loop, etc).  Code generation is again improved if the
index variable is simple.

Some historical comments: recall that FORTRAN imposed some tight
restrictions on DO loops, and for the same reasons: to generate tight
loop code. As well, in designing Pascal, Wirth was trying to avoid the
inefficiency of Algol's baroque loop construct.

>ANYWAY. A slightly more levelheaded reason for requiring the FOR control
>var to be simple and local is that it's easier to decide to make 
>the loop-cntr. var a register var...... .

Quite right.


Cheers,

Steve.

aubrey@rpp386.cactus.org (Aubrey McIntosh) (07/05/90)

In article <INFO-M2%90070316593556@UCF1VM.BITNET> Modula2 List <INFO-M2%UCF1VM.BITNET@PSUVM.PSU.EDU> writes:
>Anyone else have any answers as to why Wirth designed such a restriction?

Perhaps Randy Bush, or some ETH folk would care to comment on the following:


Although one is perhaps overextended to speak for Wirth, I have at least
read several of his papers, and --whether he intends or not-- I consistently
come up with a few design philosophies in his work.

1)  His papers have the 'flavor' or a mathemeticians writing.  My BA degree
is in math.  So...
   a)  A project should be complete for the original job/specification in
       mind.
   b)  A project should be minimal.
       'complete' and 'minimal' attain special meaning to a mathemetician.
       in lay terms, they keep their usual meaning, but it's pretty strong.

Everything else seems subordinate to this.

2)  The program is a document (formalized to a 'Text' now, in Oberon).
    a)  Excessive obscureness, for performance, typically can be avoided
        through better algorithm abstraction, and failing that, better
        hardware.  Failing that, N.W. seems able to design a new language.
        
        Before I carelessly start a flame war, let me point this out about
        my background.  Once, in 1974, a Ms Comp. Sci major spent 4 months
        working in a 4Kb RAM environment trying to squeese 13 bytes out of
        a piece of code.  Memory was about $1,000 / Kb, and a month's house
        payment was $85.  I still tend to 'Hack,' but after studying N.W.'s
        papers, I leave it to the week before delivery, and my bug rate is
        fairly low.

        Anyway, I think this point focuses the preferences between the
        Pascal vs. C communities.

    b)  Textual locality of reference actually improves program comprehension.

3)  It is permissable to design the language, in part, to make the software
    development environment 'good.'

    a)  Fast Compilation --> Simple language.
    b)  Flexible or Custom Environment --> Libraries external to language.
    c)  Correct products --> Safe language constructs, simple compilers.

4)  Compiler optimization doesn't strongly interest him.  At a talk, I
    heard him say that his one pass Modula-2 compiler used 20 year old
    compiler technology, he knew that, and one can only do so much with
    a handful of graduate students.  Also, he believes that the language
    design itself influences the quality of code, and that optimization of
    a cumbersom language raises the risk of an incorrect compiler.


---------------------

If these abstracts really represent(ed) Wirth's thinking, then I think the
business of the FOR loop is kept simple, the references local, the optimization
is built in (no assignments are legal to the loop variable within the
loop:  It is safe to put the value into any special place, such as registers,
without execution time safeguards.


-- 
Aubrey McIntosh  	"Find hungry samurai." -- The Old Man        
1502 Devon Circle       comp.os.minix, comp.lang.modula2         
Austin, TX 78723 
1-(512)-452-1540  (v)

aubrey@rpp386.cactus.org (Aubrey McIntosh) (07/05/90)

In article <1990Jul4.074634.645@diku.dk> jensting@skinfaxe.diku.dk (Jens Tingleff) writes:
>ROSS@UCF1VM.BITNET (Bri) writes:
>the loop-cntr. var a register var...... .
>
>By The Way, new question :
>  Is the FOR loop control var defined after the FOR statement ? Wirth
>  (PIM2 ed 3 p 158) doesn't say not, in fact the paragraph reads
>
> 	"FOR v := S TO B BY C DO SS END
>	 expresses repeated execution of the stat. seq. SS with
>	 v successively assuming the values A, A+C, A+2C, ..,
>	 A+nC where A+nC is the last term not exceeding B."
>	apologies to  Dire Straits 

At the same talk, at Stride Faire, Reno, 1985 (?) Wirth discussed this
in the context of 'be careful both of what the standard says, and what
it remains silent on.'

He went on to say that many implementors and programmers assume that
this standard requires the "consecutive," i.e. monotonic assignment of
values to v.  He specifically stated that such requirement is not anywhere
in his definitions, and it would be just as fine to do all the even
numbers first, then the odd numbers.  Part of his point was that, it
cannot be defined what the last value of v was during execution.  Any
compiler that assigns each correct value to v and executes the loop once
for that value would be legal.

In terms of reconciling why he would do that, I model in my mind that
he would want a correctly written matrix multiply program to compile and
run on a parallel machine, and leaving the execution order undefined
allows one to build (in principle) a compiler to parcel out the various
jobs for each loop to the available processors.

Again, he seemed to consider his definitions important not only on what they
do say, but equally on what avoid saying.

-------------------

I am writing what I remember from several years ago.  My memory may be
faulty, and Wirth surely has a more refined idea on many topics now than
he did then (he says so in his Oberon article, so I'm safe in saying that.)
I surely welcome having sharp minds try to pick apart what I've written,
but be nice so I'll read it.

-- 
Aubrey McIntosh  	"Find hungry samurai." -- The Old Man        
1502 Devon Circle       comp.os.minix, comp.lang.modula2         
Austin, TX 78723 
1-(512)-452-1540  (v)

moss@cs.umass.edu (Eliot Moss) (07/05/90)

Just for fun, I note that Modula-3 (and Ada for that matter) make the index
variable of a FOR loop a *new* variable, defined only between the DO and END.
This makes it even more clear that the optimization can be performed and that
the value after the cannot be depended upon. If you need the last value after
the loop, you must explicitly assign it to a variable defined in the broader
scope. This seems to me to be the best way to design a looping construct;
CLU's iterators generalize what you can loop over, but the index variable's
scope is as for Modula-3 and Ada. CLU's usage predates those other languages,
but I am not sure where this style of definition first arose.
--

		J. Eliot B. Moss, Assistant Professor
		Department of Computer and Information Science
		Lederle Graduate Research Center
		University of Massachusetts
		Amherst, MA  01003
		(413) 545-4206; Moss@cs.umass.edu

randy@m2xenix.psg.com (Randy Bush) (07/05/90)

In <18442@rpp386.cactus.org> aubrey@rpp386.cactus.org (Aubrey McIntosh) writes:

> I think the business of the FOR loop is kept simple, the references local,
> the optimization is built in (no assignments are legal to the loop variable
> within the loop: It is safe to put the value into any special place, such as
> registers, without execution time safeguards.

Compiler writers loathe the FOR statement; efficiency at execution time often
implies trying to detect all sorts of horrors (e.g., aliasing) at compilation
time.  So, if the language is defined to restrict the control variable (local,
not 'threatened' (passed as a VAR parameter, used as an lvalue in an enclosed
procedure, ...), ...) one complicates the language spec, but neither the
compiler nor the user's code.

The US committee momentarily considered a language change where FOR control
variables had only the scope of the FOR statement, as in Ada (thanks Robert
Firth).  As it implied a language change, we disgarded the idea with some
sadness.  [ Sadly, other national bodies were less cautious about the
language, see the DP. ]

Is the FOR statement still absent in the current version of Oberon?
-- 
..!{uunet,qiclab,intelhf}!m2xenix!randy   randy@psg.com   randy@m2xenix.uucp

mfranz@ethz.UUCP (Michael Franz) (07/05/90)

It is probably worthwile to note that the FOR construct is completely
absent from Oberon.  I for one haven't missed it yet.

Michael Franz   Computersysteme   ETH Zurich   Switzerland.

touch@dsl.cis.upenn.edu (Joe Touch) (07/05/90)

In article <MOSS.90Jul5090826@ibis.cs.umass.edu> moss@cs.umass.edu writes:
>Just for fun, I note that Modula-3 (and Ada for that matter) make the index
>variable of a FOR loop a *new* variable, defined only between the DO and END.

Just to note, I asked the people at DEC (who wrote and distributed
Modula-3), and Modula-3 is **not** a superset of Modula-2.  The
two are related, but substantially different.

And a question - if the FOR loop variable is defined only within
the loop, where is its type defined?  For example:
	FOR i = a TO b
		statement...
is this always OK, provided the compiler can unify the types of
a, b, and any use of i inside the statement?  if so, the decision of
the type correctness of i cannot occur until after the entire loop 
has been scanned.

	Joe

moss@cs.umass.edu (Eliot Moss) (07/07/90)

The type of "id" in the Modula-3 loop

	FOR id := first TO last BY step DO S END

is the common basetype of "first" and "last". This will be either INTEGER or
some enumeration type; it will never be a subrange, though if first and last
are compile-time known then the compiler can clearly take advantage of that in
doing array bounds checks, assignments to variables having subrange types,
etc. The index variable "id" may not be assigned within the loop or passed as
a VAR parameter; technically, it is called "readonly".

Joe Touch is right that Modula-3 is not a pure superset of Modula-2.
Personally, I consider it an improvement over Pascal and Modula-2, because of
its addition of garbage collection, objects with single inheritance,
exceptions and handlers, and true concurrent threads; it also deleted some
items that I feel are needlessly complex. Not everyone agrees with this view;
some feel that Oberon is more attractive because it is smaller. That is,
ignoring the addition of objects/extensions, Modula-3 looks more like a
superset of Modula-2 and Oberon looks more like a subset. Each to their own,
but I think garbage collection and exceptions are definite wins in developing
quality software.						Eliot
--

		J. Eliot B. Moss, Assistant Professor
		Department of Computer and Information Science
		Lederle Graduate Research Center
		University of Massachusetts
		Amherst, MA  01003
		(413) 545-4206; Moss@cs.umass.edu

lins@Apple.COM (Chuck Lins) (07/10/90)

In article <1990Jul5.153926.11683@m2xenix.psg.com> randy@m2xenix.psg.com (Randy Bush) writes:
>Is the FOR statement still absent in the current version of Oberon?
	Yes it is still not present.

-- 
Chuck Lins               | "Is this the kind of work you'd like to do?"
Apple Computer, Inc.     | -- Front 242
20525 Mariani Avenue     | Internet:  lins@apple.com
Mail Stop 37-BD          | AppleLink: LINS@applelink.apple.com
Cupertino, CA 95014      | "Self-proclaimed Object Oberon Evangelist"
The intersection of Apple's ideas and my ideas yields the empty set.

Jason.Kankiewicz@f345.n109.z1.fidonet.org (Jason Kankiewicz) (07/11/90)

Eliot, was Niklaus Wirth himself responsible, in whole or in part, for 
Modula-3.  Also, when was M3 introduced and what is garbage collection  
(the deletion of useless variables?)



--  
uucp: uunet!m2xenix!puddle!109!345!Jason.Kankiewicz
Internet: Jason.Kankiewicz@f345.n109.z1.fidonet.org

moss@cs.umass.edu (Eliot Moss) (07/15/90)

In article <1610.269E1B17@puddle.fidonet.org>
Jason.Kankiewicz@f345.n109.z1.fidonet.org (Jason Kankiewicz) writes:

   Eliot, was Niklaus Wirth himself responsible, in whole or in part, for 
   Modula-3.  Also, when was M3 introduced and what is garbage collection  
   (the deletion of useless variables?)

Wirth was *not* responsible for Modula-3, but he *did* give permission for the
name to be used. (Note, *I* am *not* one of the original developers of
Modula-3, though I have met with a number of the designers and have made
suggestions for future changes (and even gotten one or two minor changes in).)
The history of Modula-3 is related a little bit in the Modula-3 Report
(Revised), and more on a video tape by Jim Donahue (head of the Olivetti
Research Center (now mostly defunct), which participated, along with Digital's
Systems Research Center, in the Modula-3 design effort). I think that a draft
that was in many respects similar to today's Modula-3 was prepared about 2
years ago. The most recent Report was issued last October (PostScript for
which can be ftp'ed from gatekeeper.dec.com, as can the Modula-3 compiler,
etc.; read stuff about the license (which is pretty liberal)).

Garbage collection is the automatic reclamation of unreachable heap allocated
data, i.e., things created by NEW that can no longer be reached by any path of
pointers from global variables or stacks of still existing threads
(processes). If you interpret the words "deletion", "useless", and "variable"
appropriately, then yes, it is "deletion of useless variables", but I would
not describe it that way because "variable" has many different meanings to
different people, because "useless" is a bit imprecise (to me at least), and
because "deletion" is also misleading. Our version of Modula-3 will sport a
generation scavenging type collector, which should run quite fast, essentially
incrementally, and will compact heap space.

I hope this helps your understanding of Modula-3 ..... Eliot
--

		J. Eliot B. Moss, Assistant Professor
		Department of Computer and Information Science
		Lederle Graduate Research Center
		University of Massachusetts
		Amherst, MA  01003
		(413) 545-4206; Moss@cs.umass.edu

muller@src.dec.com (Eric Muller) (07/19/90)

[I am posting this for Greg Nelson. I would add that we are very close
 to a new release of SRC Modula-3; we will send a message to this newsgroup]


In article <1610.269E1B17@puddle.fidonet.org>
Jason.Kankiewicz@f345.n109.z1.fidonet.org (Jason Kankiewicz) writes:

   Eliot, was Niklaus Wirth himself responsible, in whole or in part, for 
   Modula-3.  Also, when was M3 introduced and what is garbage collection  
   (the deletion of useless variables?)

Eliot Moss answers

    Wirth was *not* responsible for Modula-3, but he *did* give
    permission for the name to be used.  (Note, *I* am *not* one of
    the original developers of Modula-3, though I have met with a number
    of the designers and have made suggestions for future changes (and
    even gotten one or two minor changes in).)

I'm a member of the Modula-3 committee, and would like to add that
Wirth listened to several reviews of the language and graciously offered
advice, which was very useful to us.

In response to other questions on this bboard about Modula-3 and its
availability: the language report was published in August 1988.
Implementation efforts began immediately at DEC SRC and Olivetti.  As
a result of these, the language was revised, and a revised report was
published in November 1989.  Olivetti closed the lab that supported
Modula-3, and seems to be on the verge of putting their system in the
public domain.  The SRC system is available under a liberal license.
It has gone through several releases.  A book on the language will
appear around the end of the year.

Greg Nelson