Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.forth > #14008 > unrolled thread

Re: Auotations revisited

Started byAndrew Haley <andrew29@littlepinkcloud.invalid>
First post2012-07-15 03:45 -0500
Last post2012-07-15 17:08 +0200
Articles 4 — 3 participants

Back to article view | Back to comp.lang.forth

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Auotations revisited Andrew Haley <andrew29@littlepinkcloud.invalid> - 2012-07-15 03:45 -0500
    Re: Quotations revisited mhx@iae.nl (Marcel Hendrix) - 2012-07-15 12:52 +0200
      Re: Quotations revisited mhx@iae.nl (Marcel Hendrix) - 2012-07-15 16:08 +0200
        Re: Quotations revisited Bernd Paysan <bernd.paysan@gmx.de> - 2012-07-15 17:08 +0200

#14008 — Re: Auotations revisited

FromAndrew Haley <andrew29@littlepinkcloud.invalid>
Date2012-07-15 03:45 -0500
SubjectRe: Auotations revisited
Message-ID<EO6dnTRcUKqmHp_NnZ2dnUVZ8v2dnZ2d@supernews.com>
Marcel Hendrix <mhx@iae.nl> wrote:
> Recently, the subject of quotations popped up again and I remembered 
> an unfinished project.
> 
> It proves that in iForth (a simple form of) quotations takes only three 
> lines of code. The main limitation is that LOCALS are not (yet) possible
> inside a quotation. 
> 
> You can use a quotation outside of the definition where it was created, 
> but it does not 'remember' any state.

0 VALUE dict : myEXIT -R [ -OPT ] ;
: [: ( -- )      POSTPONE AHEAD  HERE TO dict  -OPT POSTPONE >R ; IMMEDIATE
: ;] ( -- xt )   POSTPONE myEXIT  POSTPONE THEN  dict POSTPONE LITERAL ; IMMEDIATE

My first version of quotations for SwiftForth was much like this.
Then someone pointed out that RECURSE had to work.  Then somone
pointed out that locals had to work...

Andrew.

[toc] | [next] | [standalone]


#14009 — Re: Quotations revisited

Frommhx@iae.nl (Marcel Hendrix)
Date2012-07-15 12:52 +0200
SubjectRe: Quotations revisited
Message-ID<86839188968435@frunobulax.edu>
In reply to#14008
Re: Andrew Haley <andrew29@littlepinkcloud.invalid> writes Re: Quotations revisited

> Marcel Hendrix <mhx@iae.nl> wrote:
>> Recently, the subject of quotations popped up again and I remembered 
>> an unfinished project.
[..] 
> My first version of quotations for SwiftForth was much like this.
> Then someone pointed out that RECURSE had to work.  Then somone
> pointed out that locals had to work...

0 value dict     : MY-EXIT -R [ -OPT ] ;
: [: ( -- )      POSTPONE AHEAD  HERE TO dict  -OPT POSTPONE >R ; immediate 
: ;] ( -- xt )   POSTPONE MY-EXIT  POSTPONE THEN  dict POSTPONE LITERAL ; IMMEDIATE
: MYSELF ( -- )  dict POSTPONE LITERAL POSTPONE EXECUTE ; immediate

: test-recursion ( flag -- u )
	#10 SWAP
	   IF  [: ( n1 -- n2 ) DUP 1 > IF  DUP 1-  MYSELF  SWAP 2-  MYSELF +  ENDIF ;] \ fib
	 ELSE  [: ( n1 -- n2 ) DUP     IF  DUP 1-  MYSELF  SWAP *   ELSE  1+  ENDIF ;] \ fac
	ENDIF  EXECUTE . ;

FORTH> true test-recursion 55  ok
FORTH> false test-recursion 3628800  ok

When [: and ;] switch to some private wordlist, EXIT and RECURSE can be used instead of 
MY-EXIT and MYSELF. LOCALs are possible with a little extra effort. Was there anything else
people wanted?

-marcel

[toc] | [prev] | [next] | [standalone]


#14013 — Re: Quotations revisited

Frommhx@iae.nl (Marcel Hendrix)
Date2012-07-15 16:08 +0200
SubjectRe: Quotations revisited
Message-ID<91951988968435@frunobulax.edu>
In reply to#14009
mhx@iae.nl (Marcel Hendrix) wrote Re: Quotations revisited
[..]
> When [: and ;] switch to some private wordlist, EXIT and RECURSE can be used instead of 
> MY-EXIT and MYSELF. LOCALs are possible with a little extra effort. Was there anything else
> people wanted?

Although a private set of locals doesn't work, accessing outer locals
may work sufficiently close to what is intended:

: test-local ( -- )
	11 1 2 LOCALS| c b a |  
	CR a . b . c . 
	[: -1 TO a  CR a . b . c . ;] EXECUTE
	CR a b + c + . ;

FORTH> test-local
11 1 2
-1 1 2
2  ok

However, Knuth's Man-or-Boy test fails for calldepths > 3. (At that point 
the locals stack is empty, so apparently the recursion is supposed to make 
copies?)

-marcel

-- -------------------------------
DEFER A 
0 VALUE B  
:NONAME  0 ; =: F0
:NONAME  1 ; =: F1
:NONAME -1 ; =: Fn1

:NONAME ( k x1 x2 x3 x4 x5 -- n )
	LOCALS| x5 x4 x3 x2 x1  k |
	[: -1 +TO k   k B x1 x2 x3 x4  A ;] TO B
	k 0<= IF  x4 EXECUTE x5 EXECUTE +  ELSE  B EXECUTE  ENDIF ; IS A

1 value calldepth \ 0, 1, 2 .. 3 works, higher crash!
: ManOrBoy  CR calldepth ( #10 ) F1 Fn1 Fn1 F1 F0  A ;

[toc] | [prev] | [next] | [standalone]


#14014 — Re: Quotations revisited

FromBernd Paysan <bernd.paysan@gmx.de>
Date2012-07-15 17:08 +0200
SubjectRe: Quotations revisited
Message-ID<jtumec$7a1$1@online.de>
In reply to#14013
Marcel Hendrix wrote:

> mhx@iae.nl (Marcel Hendrix) wrote Re: Quotations revisited
> [..]
>> When [: and ;] switch to some private wordlist, EXIT and RECURSE can
>> be used instead of MY-EXIT and MYSELF. LOCALs are possible with a
>> little extra effort. Was there anything else people wanted?
> 
> Although a private set of locals doesn't work, accessing outer locals
> may work sufficiently close to what is intended:

In Gforth, you can rather have a private set of locals, no access to 
outer locals (i.e. no closures).

> However, Knuth's Man-or-Boy test fails for calldepths > 3. (At that
> point the locals stack is empty, so apparently the recursion is
> supposed to make copies?)

It's supposed to create a closure.  A closure contains both the function 
itself *and* the values of the locals at the time of its creation.  The 
livetime of a closure can exceed the livetime of its creating word:

: incrementer ( by-n -- xt ) { by-n } [: by-n + ;] ;
5 incrementer Alias 5+

If I would implement closures in Forth, I'd rather prefer a more 
explicit construction that lexical scoping.  My closures would look 
like:

: incrementer ( by-n -- xt ) [:{ by-n } by-n + ;] ;

What [:{ does at runtime:  It allocates a buffer for a structure similar 
to words defined by create does>, and the space for by-n, which is 
initialized from the stack.

-- 
Bernd Paysan
"If you want it done right, you have to do it yourself"
http://bernd-paysan.de/

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.forth


csiph-web