2014-05-16 0:29 GMT+02:00 Jon Purdy <evincarofaut...@gmail.com>:
>> 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 <rotated> [ >array ] bi@ <effect> ;

: 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

Reply via email to