4 ms·
All of these have a problem: they evaluate the places multiple times, which produces surprising behavior if the place expressions contain side effects and is in
by kazinator 9y ago
All of these have a problem: they evaluate the places multiple times, which produces surprising behavior if the place expressions contain side effects and is inefficient if they invoke expensive computation.
TXR Lisp:
(defmacro swap (a b)
(with-gensyms (ga gb gt)
^(placelet ((,ga ,a) (,gb ,b))
(let ((,gt ,ga))
(set ,ga ,gb)
(set ,gb ,gt)))))
REPL:
1> (sys:expand '(swap (car (g)) (cdr (h))))
(let ((#:g0255 (g)))
(let ((#:g0254 (h)))
(let ((#:g0241 (car #:g0255)))
(sys:rplaca #:g0255 (cdr #:g0254))
(sys:rplacd #:g0254 #:g0241))))
The place expressions here contain calls to functions g and h; but as you can see, these functions are each called just once.
placelet: http://www.nongnu.org/txr/txr-manpage.html#N-0393C970 http://www.nongnu.org/txr/txr-manpage.html#N-0393C970
placelet lets you easily turn incorrect, multiply-evaluating macrology for manipulation of places into correct singly-evaluating code while retaining its basic form. The kernel of the above swap looks like the native three-point rotation. The only difference is that it's done through the placelet aliases for a and b, rather than directly with those expressions.
This is just one of the items of my personal research into the advancement of practical Lisp that I imbued into TXR.
The implementation of placelet is tricky:
http://www.kylheku.com/cgit/txr/tree/share/txr/stdlib/place.tl#n891 http://www.kylheku.com/cgit/txr/tree/share/txr/stdlib/place....
The places implementation has no concept of lexically scoped place expansion: places are are all global. (Just like "setf expansions" in Common Lisp are global). However, placelet needs to create a scoped place expansion: a lexical alias for a place. That's why you see the unwind-protect form and the manipulation of the * place-update-expander * hash. The effectively local place expander produces getter/setter macrolets which just forward to the real ones.
Though the hash table is a special variable, this is happening at macro-expansion time during the tree walk so it is effectively a lexical mechanism: the dynamic scope of the code walker at expansion time corresponds to a piece of lexical scope of the code.
For reference, here is the actual swap from the TXR Lisp library (same source file as placelet):
(defmacro swap (place-0 place-1 :env env)
(with-gensyms (tmp)
(with-update-expander (getter-0 setter-0) place-0 env
(with-update-expander (getter-1 setter-1) place-1 env
^(let ((,tmp (,getter-0)))
(,setter-0 (,getter-1))
(,setter-1 ,tmp))))))
It doesn't use placelet but update expanders directly, The placelet aliasing mechanism is built on this, basically. It produces exactly the same expansion.
This shows how the "update expanders" implementation of places also has the nice property that you can use update expanders to write straight-forward code templates to implement place updates.
Update expanders differ from setf expanders in Common Lisp but achieve the same thing.
(There are also "clobber expanders" for situations when a place is overwritten without being accessed and "delete expanders": TXR Lisp supports "place deletion".)