Surprised by Joy

Massimo Dentico m.dentico@teseo.it
Sun, 12 Mar 2000 15:56:14 +0100


Massimo Dentico wrote:
> 
> btanksley@hifn.com wrote:
> >
> > [...]
> >
> > >From: iepos@tunes.org [mailto:iepos@tunes.org]
> >
> > >It would be possible to make types first-class... a type could
> > >be thought of as a pure procedure which takes a thing off the
> > >stack, and pushes a proposition (True or False) indicating
> > >whether the popped thing was a member or not. But, maybe this would
> > >not be a good idea... I'm not sure...
> >
> > RTTI, in other words.
> 
> Not necessarily  Run-Time Type Information.  You can utilize this
> information (when possible)  at compile-time or in any stage in a
> multi-stage programming environment.

A brief note for the readers not familiar with Forth. The concept
of multi-stage programming is not extraneous to it. In Forth it's
possible to distinguish 3 stages: Interpretation, Compilation and
Execution  (ICE,  an acronym  coined  by Moore,  the inventor  of
Forth).

It  is  possible  to switch  freely  between  interpretation  and
compilation.   Moreover,  there is  the possibility  to expressly 
annotate  a word (function/procedure)  with staging  information.
For example with a word like IMMEDIATE, which indicate to execute
immediately a word during a compilation.

An  example  to  clarify,  adapted  from  "Real Time Forth",  Tim
Hendtlass.  Let's type  at the prompt in F-PC,  a non-stand-alone
MS-DOS version of Forth, this definition:

  : NOISE beep ;

the word  ":" (colon)  switches  to compilation  mode, creates an
entry into the dictionary,  with the name "NOISE",  then attaches
the compiled code for "beep" to "NOISE"  (Forth is an incremental
compiler,  often, but not necessarly in any case, at the level of
a single word). The word ";" (semi-colon) switches to interpreta-
tion mode.  Now,  typing at  the prompt  (interactively,  but the
effect is the same  if you load the code from a "screen"  (a sort
of numbered module) or from a file):

  NOISE ok

you'll hear a beep  ("ok" is the Forth prompt).   If you type (or
load) this colon definition:

  : NOW-NOISE beep ; IMMEDIATE

you'll  define  an immediate word  (note the "staging annotation"
via the "IMMEDIATE" word). Now typing this colon definition:

  : ALWAYS-NOISE NOW-NOISE NOISE ;

you'll hear IMMEDIATEly a beep,  at compilation stage,  caused by
the presence of "NOW-NOISE" in the definition, and typing:

  ALWAYS-NOISE ok

you'll hear another beep, at interpretation/execution stage.

To end  this note:  with this simple (in many implementation it's
only necessary  to set/test a flag)  but  powerful mechanism,  in
Forth isn't necessary,  for example, a special construct or, even
worse, an ad-hoc preprocessor for macros; you simply have another
word, "IMMEDIATE",  in the Forth environment.  However, IMO, it's
necessary a systematization and formalization  (there are already 
some  formalization  in literature) of this and other features of
Forth,   at  the  end  of  facilitate  reasoning  about   program
constructions and transformations.

-- 
Massimo Dentico