Re: [Factor-talk] What exactly is the retain stack?

2014-05-16 Thread Joe Groff
On Fri, May 16, 2014 at 7:38 AM, Björn Lindqvist  wrote:

> 2014-05-16 0:29 GMT+02:00 Jon Purdy :
> >> Is that it's only use? Then why? dip can easily be formulated using
> >> non-retain stack using primitives:
> >>
> >> For example: "a" "b" "c" [ append ] dip -> "a" "b" "c" -rot append swap
> >>
> >
> > That implementation assumes the quotation takes two operands and
> > produces one result, which is not always the case. More generally, the
> > functional argument of “dip” is not really supposed to be able to
> > touch the argument it’s operating under. If you don’t have types or a
> > stack checker enforcing this, the formulations with a retain stack or
> > dynamically composing quotations are safe by construction, but the
> > “-rot” version is not. Consider “[ 3drop ] dip” or “[ append dup ]
> > dip”.
>
> But factor *does* have a stack checker. Since the stack effect of the
> quotation given to dip can be inferred, you can always (I think?)
> rewrite them using nothing but normal stack shuffling operations. Like
> so:
>
> : make-shuffle-effect ( n dir -- effect )
> swap 1 + iota swap dupd  [ >array ] bi@  ;
>
> : emit-dip ( quot -- )
> dup infer
> [ nip in>> length -1 make-shuffle-effect , \ shuffle-effect , ]
> [ swap , , \ call-effect , ]
> [ nip out>> length 1 make-shuffle-effect , \ shuffle-effect , ] 2tri ;
>
> : rewrite-dip ( quot -- quot' )
> first2 drop [ emit-dip ] [ ] make ;
>
> [ [ append over ] dip ] rewrite-dip will output the quotation:
>
> [
> ( 0 1 2 3 -- 3 0 1 2 ) shuffle-effect
> [ append over ] ( x x x -- x x x ) call-effect
> ( 0 1 2 3 -- 1 2 3 0 ) shuffle-effect
> ]
>
> Now neither shuffle-effect nor call-effect are Factor primitives but
> they easily could have been and then dip would only need to touch the
> data stack.


There are still escape hatches from the static checker, like
'with-datastack', 'clear', 'execute( -- )', etc., and before the compiler
comes online, the VM JIT uses a dynamic stack model. The retain stack could
however be folded into the callstack, as is traditionally done in Forth,
since even the dynamic stack model relies on retain stack balance being
preserved. That's one of those little optimizations we never got around to
doing.

-Joe
--
"Accelerate Dev Cycles with Automated Cross-Browser Testing - For FREE
Instantly run your Selenium tests across 300+ browser/OS combos.
Get unparalleled scalability from the best Selenium testing platform available
Simple to use. Nothing to install. Get started now for free."
http://p.sf.net/sfu/SauceLabs___
Factor-talk mailing list
Factor-talk@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/factor-talk


Re: [Factor-talk] What exactly is the retain stack?

2014-05-16 Thread Rupert Swarbrick
Björn Lindqvist 
writes:
> Hi!
>
> Is that it's only use? Then why? dip can easily be formulated using
> non-retain stack using primitives:
>
> For example: "a" "b" "c" [ append ] dip -> "a" "b" "c" -rot append swap

What if the contents of the quotation use more than one item from the
stack? How would you implement [ + ] dip, for example? Or [ + + ] dip?
etc. etc.


Rupert


pgpDjqZUFweIY.pgp
Description: PGP signature
--
"Accelerate Dev Cycles with Automated Cross-Browser Testing - For FREE
Instantly run your Selenium tests across 300+ browser/OS combos.
Get unparalleled scalability from the best Selenium testing platform available
Simple to use. Nothing to install. Get started now for free."
http://p.sf.net/sfu/SauceLabs___
Factor-talk mailing list
Factor-talk@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/factor-talk


Re: [Factor-talk] What exactly is the retain stack?

2014-05-16 Thread Björn Lindqvist
2014-05-16 0:29 GMT+02:00 Jon Purdy :
>> Is that it's only use? Then why? dip can easily be formulated using
>> non-retain stack using primitives:
>>
>> For example: "a" "b" "c" [ append ] dip -> "a" "b" "c" -rot append swap
>>
>
> That implementation assumes the quotation takes two operands and
> produces one result, which is not always the case. More generally, the
> functional argument of “dip” is not really supposed to be able to
> touch the argument it’s operating under. If you don’t have types or a
> stack checker enforcing this, the formulations with a retain stack or
> dynamically composing quotations are safe by construction, but the
> “-rot” version is not. Consider “[ 3drop ] dip” or “[ append dup ]
> dip”.

But factor *does* have a stack checker. Since the stack effect of the
quotation given to dip can be inferred, you can always (I think?)
rewrite them using nothing but normal stack shuffling operations. Like
so:

: make-shuffle-effect ( n dir -- effect )
swap 1 + iota swap dupd  [ >array ] bi@  ;

: emit-dip ( quot -- )
dup infer
[ nip in>> length -1 make-shuffle-effect , \ shuffle-effect , ]
[ swap , , \ call-effect , ]
[ nip out>> length 1 make-shuffle-effect , \ shuffle-effect , ] 2tri ;

: rewrite-dip ( quot -- quot' )
first2 drop [ emit-dip ] [ ] make ;

[ [ append over ] dip ] rewrite-dip will output the quotation:

[
( 0 1 2 3 -- 3 0 1 2 ) shuffle-effect
[ append over ] ( x x x -- x x x ) call-effect
( 0 1 2 3 -- 1 2 3 0 ) shuffle-effect
]

Now neither shuffle-effect nor call-effect are Factor primitives but
they easily could have been and then dip would only need to touch the
data stack.


-- 
mvh/best regards Björn Lindqvist

--
"Accelerate Dev Cycles with Automated Cross-Browser Testing - For FREE
Instantly run your Selenium tests across 300+ browser/OS combos.
Get unparalleled scalability from the best Selenium testing platform available
Simple to use. Nothing to install. Get started now for free."
http://p.sf.net/sfu/SauceLabs
___
Factor-talk mailing list
Factor-talk@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/factor-talk