Joy, FORTH, Variable-stack-words (was: CLIP some more!)

iepos@tunes.org iepos@tunes.org
Sat, 25 Mar 2000 12:36:33 -0800 (PST)


> >But, I still wonder if maybe playing with more things than 3 on the
> >stack wouldn't be so bad if the system were designed for it from
> >the ground up...
> 
> It's not so much the system as it is the human brain.  It's generally easy
> to hold seven static, sequential items in short-term memory; however, if any
> random accessing needs to be done, or if the items aren't really static,
> four is the limit.
> 
> It's also interesting that three is the number of items which most people
> can recognise as one gestalt; any more items, and your mind has to
> subdivide, which takes some more time.

I guess maybe I can buy this... Maybe it would be good if only small
numbers of things were kept on the stack...

> >For this purpose,
> >I think the pure-program-that-pushes-some-things-onto-the-stack
> >makes a good substitute for a list, so that a system would not need
> >special primitives for lists
> 
> Yes, this is true.
> 
> >(actually, this seems to be the way Joy does it)... 
> 
> Huh?  Joy _does_ have special primitives for lists.

Hmm... maybe a quote from the manual will help
(from "Atomic Programs of Joy"):

  A value of the list type is just a special case of a quotation in
  which the elements are themselves literals.

And, his definition of "literal":

  Atomic programs which push a simple or aggregate value onto the
  stack will be called literals.

Thus, the Joy system emulates lists using programs that
push things onto the stack; this is basically the same technique
I was thinking about above.

> A book would be a great way to get that experience, really; one of my
> favorites is Leo Brodie's "Thinking Forth" and "Starting Forth".

Thanks for the tip... hopefully I'll read such a book some time...

> >Sometime I'd like to make a small speedy Joy-like system, that is not
> >based on a slow interpreter loop. But this is probably too ambitious,
> >as I've never written or hacked compilers. 
> 
> I don't know.  I'm writing something like this myself, and I'm basing it on
> Chuck Moore's "Machine Forth", which makes writing the compiler utterly
> trivial. 

Cool... I'm curious to know what your basic approach is as far as
the target; are you compiling to ELF (and this is x86, right)?

> >- "iepos" (Brent Kerby)
> 
> -Billy

By the way, after experimenting a bit with my interpreter, I see
that "cons" provides a great way to wrap lists and "i" a great way
to unwrap them, if one accepts the metaphor of
lists-as-programs-that-push-things-onto-the-stack... For instance,
in Joy, if you do this:

  [] cons cons cons

then you have just taken three things off the stack and formed a
list containing just those three things; then, if you used "i",
you'd end up with the same thing you started with.

Also, as a random observation, dig3 can be implemented as

  [] cons cons cons dip

This is much more elegant than my original construction which used "swap".
Anyhow, this pattern could easily be extended for dig4 and up:
dig4 could be written, for instance:

  [] cons cons cons cons dip

And then, "dig1", or in other words, "swap":

  [] cons dip

And then, "dig0" (which does nothing except touch the top thing on
the stack):

  [] dip

which is what i'd used earlier, although I didn't recognize as
a special case of "dig"...

Then, there is the "dig"'s counterpart, "bury"... I don't currently
see such an elegant way to construct it... Here is the best I can
think of for "bury3" (which takes the top thing on the stack and
buries it underneath the three next things):

  [[] cons cons cons] dip swap i

but this is probably not the cleanest way. One could also use:

  dig3 dig2 dig1

but there is something lame about doing it that way... Anyway,
I just realized that I'm talking about these variable-stack words
now, which you really dislike, so I'd better stop, I guess :-)

- "iepos" (Brent Kerby)