Article: 15401 of comp.lang.forth
Path: news.tuwien.ac.at!mips.complang.tuwien.ac.at!anton
From: anton@mips.complang.tuwien.ac.at (Anton Ertl)
Newsgroups: comp.lang.forth
Subject: Re: Stack manipulations
Date: 12 Apr 1995 17:23:44 GMT
Organization: Institut fuer Computersprachen, Technische Universitaet Wien
Lines: 111
Distribution: world
Message-ID: <3mh2b0$mfl@news.tuwien.ac.at>
References: <3l60lh$s8j@ixnews4.ix.netcom.com> <AB9CE84796685A50A@charlton.demon.co.uk> <3lrmbc$hjd@ixnews2.ix.netcom.com> <ABA783019668125C9@charlton.demon.co.uk> <wilbadenD6qGGw.Et9@netcom.com> <3mbmkt$d22@controversy.math.lsa.umich.edu>
NNTP-Posting-Host: mips.complang.tuwien.ac.at

In article <3mbmkt$d22@controversy.math.lsa.umich.edu>, williams@williams (David N. Williams) writes:
|> : gcd-with-locals  ( m n -- gcd )
|>  0 ( place holder for t) LOCALS| t n m |
...

A plea to all: DON'T USE "LOCALS|" !!!

We have only few conventions in the Forth community, and one of them
is that the top of stack is on the right side. "LOCALS|" declares the
locals in reverse order. If nobody uses "LOCALS|", there's hope it
will vanish in a future standard.

As a replacement, use the syntax described in Hayes92 and implemented
in several systems, e.g., TILE and gforth. An ANS Forth implementation
(inspired by Hayes92) can be found on
ftp://ftp.complang.tuwien.ac.at/pub/forth/anslocal.fs. It has the
locals in the standard order. The example above would look like this:

: gcd-with-locals  ( m n -- gcd )
 0 ( place holder for t) { m n t }

Actually, John Hayes describes a more general syntax, where the
example would come out as

: gcd-with-locals  { m n | t -- gcd }

but my implementation does not implement the "|".

David Williams' better gcd implementation looks like this with the
better locals syntax:

: better-gcd-with-locals { m n -- gcd }
  BEGIN n WHILE
   n ( old)
   m n MOD TO n
   ( old n) TO m
  REPEAT m ;


For the desert, an improvement that is not possible within the scope
of the standard: Using "TO" with locals is not good for
readability. In the gcd example, you first thing that m and n are the
input. Only late in the word you see that they are something else and
you have to reread the word to understand it. The following version is
easier to read:

: gcd { m n -- gcd }
  m n
  BEGIN { x y }
    y
  WHILE
    y
    x y MOD
  REPEAT
  x ;

Here it is clear from the start that x and y change every
iteration. Also, we do not have the problem of sequencing the "TO"s
that led to the code above that needed the comments "( old)" and
"( old n)".

If you want to learn more about how locals definitions within control
structures can be implemented and what their semantics are, read my
EuroForth'94 paper on locals.

References:
@Article{hayes92,
  author = 	 "John R. Hayes",
  title = 	 "User-Defined Local Variable Syntax with {ANS Forth}",
  journal =	 sigforth,
  year =	 "1992",
  volume =	 "4",
  number =	 "2",
  OPTpages =	 "19, 20, 26",
  annote =	 "Shows how to define a nice locals syntax using the ANS
		  Forth locals wordset."
}

@InProceedings{ertl94l,
  author = 	 "M. Anton Ertl",
  title = 	 "Automatic Scoping of Local Variables",
  crossref =	 "euroforth94",
  pages =	 "31--37",
  OPTnote =	 "available via ftp://ftp.complang.tuwien.ac.at/pub/papers/ertl94l.ps.Z",
  abstract = 	 "In the process of lifting the restrictions on using
		  locals in Forth, an interesting problem poses
		  itself: What does it mean if a local is defined in a
		  control structure? Where is the local visible? Since
		  the user can create every possible control structure
		  in ANS Forth, the answer is not as simple as it may
		  seem. Ideally, the local is visible at a place if
		  the control flow {\em must} pass through the
		  definition of the local to reach this place. This
		  paper discusses locals in general, the visibility
		  problem, its solution, the consequences and the
		  implementation as well as related programming style
		  questions."
}

@Proceedings{euroforth94,
  title = 	 "EuroForth~'94 Conference Proceedings",
  booktitle = 	 "EuroForth~'94 Conference Proceedings",
  year = 	 "1994",
  key =		 "EuroForth '94",
  address =	 "Winchester, UK",
}

- anton
-- 
M. Anton Ertl                    Some things have to be seen to be believed
anton@mips.complang.tuwien.ac.at Most things have to be believed to be seen
