Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.forth > #52260
| Newsgroups | comp.lang.forth |
|---|---|
| Date | 2016-10-27 20:31 -0700 |
| References | <4e481228-bd80-4e39-a629-90a690e50756@googlegroups.com> |
| Message-ID | <698dcf39-e8f2-4c0a-868a-b736e2ed889f@googlegroups.com> (permalink) |
| Subject | Re: Stack Operator Symbols |
| From | hughaguilar96@gmail.com |
On Monday, October 24, 2016 at 8:43:00 PM UTC-7, trans wrote:
> In any case, the operations from a coder's perspective are probably the more pertinent operations for a symbolic notation, the fundamental operations seem to me to be:
>
> drop
> dup
> swap
> push (>r)
> pull (r>)
>
> With these five I think all others are possible. Do you agree?
When I was at Testra in 1994, the MiniForth was originally going to be a very crude Forth processor with 32 primitives in hardware and everything else written in terms of these (similar to what Bernd Paysan's B16 is now). At this time I gave some thought to this question of which primitives are absolutely necessary, allowing what would normally be primitives to be written in terms of these. That phase of the MiniForth development lasted about one week and then was discarded --- it is a dead end with no hope for efficiency --- you are better off to have an assembly language that primitives can be written in, and then have hundreds of primitives (many of which are meta-compiled rather than hand-written).
Nowadays I'm designing Straight Forth which is for 64-bit desktop computers.
I don't have R> anymore --- I do have >R R@ and RDROP --- to get the effect of R> use R@ RDROP instead.
In colon words, it is not necessary to balance >R with RDROP because EXIT will automatically discard any >R data still on the return-stack. So, for the most part, RDROP is never used. Quotations don't have this feature though, so RDROP is needed to explicitely discard >R data before exiting --- this shouldn't be a problem; quotations are usually short and simple and don't need to use >R at all anyway.
Here is an excerpt from my Straight Forth document discussing |DIP and |KEEP etc., which are borrowed from DIP and KEEP etc. in Factor (in Straight Forth the naming convention is that HOFs always have a bar in front):
----------------------------------------------------------------------------
section 1.5. --- light-weight HOFs
==================================
We have some light-weight HOFs that don't iterate and aren't associated with any data-structure.
If the top value of the data-stack is in the way, the Forth programmer would typically use >R to get it out of the way temporarily, then use R> to put it back. This isn't done in Straight Forth because >R nd R> are not idiomatic. In Straight Forth it is idiomatic to use |DIP that is borrowed from Factor. We have several related HOFs:
|DIP ( i*x a xt -- j*x a ) execute the xt with A out of the way, then put A back
|2DIP ( i*x a b xt -- j*x a b ) execute the xt with A and B out of the way, then put A and B back
|3DIP ( i*x a b c xt -- j*x a b c ) execute the xt with A, B and C out of the way, then put A, B and C back
|KEEP ( i*x a xt -- j*x a ) execute the xt with A kept on the stack, then put A back
|2KEEP ( i*x a b xt -- j*x a b ) execute the xt with A and B kept on the stack, then put A and B back
|3KEEP ( i*x a b c xt -- j*x a b c ) execute the xt with A, B and C kept on the stack, then put A, B and C back
Here is an ANS-Forth function:
: test ( x y a -- x+y a )
>r + r> ;
Here is another ANS-Forth function that does the same thing:
: test ( x y a -- x+y a )
-rot + swap ;
Here is a Straight Forth function that does this:
: test ( x y a -- x+y a )
{ + } dip ;
DIP holds A out of the way (on the return-stack) while it executes the xt which is a quotation in this example. There are some pitfalls to be aware of. For one thing, using RBUF inside of the quotation is a bad idea because the RBUF memory-block will get discarded when the quotation exits, so RT won't be valid afterward. Also, EXIT can't be used inside of the quotation to exit from the function, because the quotation is a sub-function itself. In addition to |DIP, we also have |2DIP and |3DIP that are similar except that they hold 2 or 3 data out of the way.
Here is another ANS-Forth function:
: test ( x a -- x+a a )
dup >r + r> ;
Here it is with stack-juggling:
: test ( x a -- x+a a )
tuck + swap ;
Here is the Straight Forth function that does this:
: test ( x a -- x+a a )
{ + } keep ;
KEEP holds a copy of A out of the way (on the return-stack) while it executes the xt which is a quotation in this example. When the xt executes, A is still kept on the data-stack. In addition to |KEEP, we also have |2KEEP and |3KEEP that are similar except that they keep 2 or 3 data.
subsection 1.5.1. --- |TRUE and |TRUE/FALSE
===========================================
In Straight Forth we have IF ELSE THEN just like in traditional Forth --- this is the idiomatic way to do conditionals.
As an alternative we also have these HOFs:
|TRUE { flag true-xt -- }
Executes TRUE-XT if FLAG is TRUE.
|TRUE-FALSE { flag true-xt false-xt-- }
Executes TRUE-XT if FLAG is TRUE, else executes FALSE-XT.
For example, here is some idiomatic code:
: maybe-negate ( a flag -- -a|a )
if negate then ;
: maybe-plus/minus ( a b flag -- a+b|a-b )
if + else - then ;
Here are some alternate versions:
: maybe-negate ( a flag -- -a|a )
{ negate } |true ;
: maybe-plus/minus ( a b flag -- a+b|a-b )
{ + } { - } |true-false ;
I think that |DIP and |KEEP etc., which are derived from Factor, improve readability a lot. |TRUE and |TRUE-FALSE are also derived from Factor, but it is debatable if they improve or degrade readability as compared to the idiomatic versions --- they are provided so we can have the debate, but they may be removed from Straight Forth in the future if nobody wants to use them.
|TRUE and |TRUE-FALSE have some limitations that you don't get with IF ELSE THEN which are traditional. On limitation is that if RBUF is used inside of the quotation it doesn't remain valid outside of the quotation. Another limitation is that REG and ACC can't be used to pass data out of the quotation. Another limitation is that EXIT can't be used inside of the quotation to exit from the function outside of the quotation.
Back to comp.lang.forth | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Stack Operator Symbols trans <transfire@gmail.com> - 2016-10-24 20:42 -0700
Re: Stack Operator Symbols hughaguilar96@gmail.com - 2016-10-25 01:58 -0700
Re: Stack Operator Symbols november.nihal@gmail.com - 2016-10-25 10:52 -0700
Re: Stack Operator Symbols Rod Pemberton <NeedNotReplyHere@xrsevnneqk.cem> - 2016-10-27 19:13 -0400
Re: Stack Operator Symbols "Elizabeth D. Rather" <erather@forth.com> - 2016-10-27 17:08 -1000
Re: Stack Operator Symbols "HAA" <someone@microsoft.com> - 2016-10-28 21:53 +1000
Re: Stack Operator Symbols hughaguilar96@gmail.com - 2016-10-28 20:42 -0700
Re: Stack Operator Symbols Walter Banks <walter@bytecraft.com> - 2016-10-29 17:15 -0400
Re: Stack Operator Symbols hughaguilar96@gmail.com - 2016-10-29 17:27 -0700
Re: Stack Operator Symbols "Elizabeth D. Rather" <erather@forth.com> - 2016-10-28 20:56 -1000
Re: Stack Operator Symbols Paul Rubin <no.email@nospam.invalid> - 2016-10-29 00:46 -0700
Re: Stack Operator Symbols Andrew Haley <andrew29@littlepinkcloud.invalid> - 2016-10-29 04:30 -0500
Re: Stack Operator Symbols rickman <gnuarm@gmail.com> - 2016-10-29 16:58 -0400
Re: Stack Operator Symbols Paul Rubin <no.email@nospam.invalid> - 2016-10-29 14:21 -0700
Re: Stack Operator Symbols Andrew Haley <andrew29@littlepinkcloud.invalid> - 2016-10-30 04:03 -0500
Re: Stack Operator Symbols Paul Rubin <no.email@nospam.invalid> - 2016-10-30 18:06 -0700
Re: Stack Operator Symbols Spiros Bousbouras <spibou@gmail.com> - 2016-10-31 01:43 +0000
Re: Stack Operator Symbols Paul Rubin <no.email@nospam.invalid> - 2016-10-30 19:23 -0700
Re: Stack Operator Symbols Andrew Haley <andrew29@littlepinkcloud.invalid> - 2016-10-31 05:13 -0500
Re: Stack Operator Symbols albert@cherry.spenarnc.xs4all.nl (Albert van der Horst) - 2016-10-31 12:42 +0100
Re: Stack Operator Symbols anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-10-31 12:12 +0000
Re: Stack Operator Symbols anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-10-31 14:49 +0000
Re: Stack Operator Symbols mhx@iae.nl - 2016-10-31 13:17 -0700
Re: Stack Operator Symbols anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-11-03 16:38 +0000
Re: Stack Operator Symbols Spiros Bousbouras <spibou@gmail.com> - 2016-11-20 14:33 +0000
Re: Stack Operator Symbols anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-10-31 08:35 +0000
Re: Stack Operator Symbols anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-10-30 11:01 +0000
Re: Stack Operator Symbols hughaguilar96@gmail.com - 2016-10-30 10:01 -0700
Re: Stack Operator Symbols hughaguilar96@gmail.com - 2016-11-03 14:46 -0700
Re: Stack Operator Symbols anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-11-04 23:04 +0000
Re: Stack Operator Symbols Rod Pemberton <NeedNotReplyHere@xrsevnneqk.cem> - 2016-11-05 03:16 -0400
Re: Stack Operator Symbols peter.m.falth@gmail.com - 2016-10-29 09:13 -0700
Re: Stack Operator Symbols "HAA" <someone@microsoft.com> - 2016-10-30 08:47 +1100
Re: Stack Operator Symbols trans <transfire@gmail.com> - 2016-10-29 14:57 -0700
Re: Stack Operator Symbols "HAA" <someone@microsoft.com> - 2016-10-30 09:20 +1100
Re: Stack Operator Symbols Rod Pemberton <NeedNotReplyHere@xrsevnneqk.cem> - 2016-10-29 18:46 -0400
Re: Stack Operator Symbols Paul Rubin <no.email@nospam.invalid> - 2016-10-29 19:45 -0700
Re: Stack Operator Symbols hughaguilar96@gmail.com - 2016-10-29 20:29 -0700
Re: Stack Operator Symbols hughaguilar96@gmail.com - 2016-10-29 20:47 -0700
Re: Stack Operator Symbols Cecil Bayona <cbayona@cbayona.com> - 2016-10-29 23:15 -0500
Re: Stack Operator Symbols "Elizabeth D. Rather" <erather@forth.com> - 2016-10-29 21:37 -1000
Re: Stack Operator Symbols hughaguilar96@gmail.com - 2016-10-30 10:07 -0700
Re: Stack Operator Symbols "Elizabeth D. Rather" <erather@forth.com> - 2016-10-29 21:35 -1000
Re: Stack Operator Symbols "HAA" <someone@microsoft.com> - 2016-10-31 13:11 +1100
Re: Stack Operator Symbols hughaguilar96@gmail.com - 2016-10-30 20:02 -0700
Re: Stack Operator Symbols "Elizabeth D. Rather" <erather@forth.com> - 2016-10-30 22:08 -1000
Re: Stack Operator Symbols anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-10-31 09:02 +0000
Re: Stack Operator Symbols "HAA" <someone@microsoft.com> - 2016-11-03 20:49 +1100
Re: Stack Operator Symbols anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-11-03 15:33 +0000
Re: Stack Operator Symbols Rod Pemberton <NeedNotReplyHere@xrsevnneqk.cem> - 2016-11-03 16:55 -0400
Re: Stack Operator Symbols anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-11-03 22:21 +0000
Re: Stack Operator Symbols hughaguilar96@gmail.com - 2016-10-27 20:31 -0700
Re: Stack Operator Symbols november.nihal@gmail.com - 2016-10-28 09:52 -0700
Re: Stack Operator Symbols hughaguilar96@gmail.com - 2016-10-28 20:32 -0700
Re: Stack Operator Symbols Mark Wills <markwills1970@gmail.com> - 2016-10-30 04:49 -0700
Re: Stack Operator Symbols Coos Haak <htrofhc@gmail.com> - 2016-10-30 14:49 +0100
Re: Stack Operator Symbols Matthias Koch <matthias.koch@hot.uni-hannover.de> - 2016-10-31 13:31 +0100
Re: Stack Operator Symbols Coos Haak <htrofhc@gmail.com> - 2016-10-31 14:12 +0100
Re: Stack Operator Symbols Matthias Koch <matthias.koch@hot.uni-hannover.de> - 2016-10-31 15:03 +0100
Re: Stack Operator Symbols Mark Wills <markwills1970@gmail.com> - 2016-11-03 14:22 -0700
Re: Stack Operator Symbols Coos Haak <htrofhc@gmail.com> - 2016-11-04 00:46 +0100
Re: Stack Operator Symbols "HAA" <someone@microsoft.com> - 2016-11-05 13:14 +1100
Re: Stack Operator Symbols trans <transfire@gmail.com> - 2016-10-30 06:50 -0700
Re: Stack Operator Symbols trans <transfire@gmail.com> - 2016-10-30 08:56 -0700
Re: Stack Operator Symbols albert@cherry.spenarnc.xs4all.nl (Albert van der Horst) - 2016-10-30 18:19 +0100
csiph-web