3 ms·
Why wait for a language; here is a macro: Firstly, we define a place (temporal x y) such that when this place is accessed, it denotes x, and when it is assigne
by kazinator 4y ago
Why wait for a language; here is a macro:
Firstly, we define a place (temporal x y) such that when this place is accessed, it denotes x, and when it is assigned, the assignment goes to y:
(defplace (temporal-place var var*) body
(getter setter
^(macrolet ((,getter () ,var)
(,setter (val) ^(set ,',var* ,val)))
,body))
(setter
^(macrolet ((,setter (val) ^(set ,',var* ,val)))
,body)))
(define-place-macro temporal (var var*) ^(temporal-place ,var ,var*))
(defmacro temporal (var var*) var)
We can then use that as a building block whereby we define an apparent variable v as a symbol macro which expands to (temporal vold vnew). All accesses to v get the value of vold, but assignments to v do not affect vold; they go to vnew.
Here is a let-like macro which sets up the specified variable as temporal. The initial values propagate into both the old and new location.
Then before the last form of the body is evalulated, the new values are copied to the old values.
(defmacro temporal-let (vars . body)
(let ((olds (mapcar (ret (gensym)) vars))
(news (mapcar (ret (gensym)) vars)))
^(let* (,*(zip olds [mapcar cadr vars])
,*(zip news olds))
(symacrolet ,(zip [mapcar car vars]
[mapcar (op list 'temporal) olds news])
,*(butlast body)
(set ,*(weave olds news))
,*(last body)))))
Test. e.g:
1> (temporal-let ((x 1) (y 2))
(set x y)
(set y x)
(list x y))
(2 1)
The expansion is:
2> (expand '(temporal-let ((x 1) (y 2))
(set x y)
(set y x)
(list x y)))
(let* ((#:g0024 1)
(#:g0025 2)
(#:g0026 #:g0024)
(#:g0027 #:g0025))
(sys:setq #:g0026
#:g0025)
(sys:setq #:g0027
#:g0024)
(progn (sys:setq #:g0024
#:g0026)
(sys:setq #:g0025
#:g0027))
(list #:g0024 #:g0025))
Let's see how it compiles:
2> (disassemble (compile-toplevel '(temporal-let ((x 1) (y 2))
(set x y)
(set y x)
(list x y))))
data:
0: 1
1: 2
syms:
0: list
code:
0: 20020002 gcall t2 0 d1 d0
1: 04010000
2: 00000400
3: 10000002 end t2
instruction count:
2
#<sys:vm-desc: 903ec50>
Oops, it got compiled to a single call instruction invoking (list 2 1). Let's try opt-level zero:
1> (let ((*opt-level* 0))
(disassemble (compile-toplevel '(temporal-let ((x 1) (y 2))
(set x y)
(set y x)
(list x y)))))
data:
0: 1
1: 2
syms:
0: list
code:
0: 04020004 frame 2 4
1: 2C800400 movsr v00000 d0
2: 2C810401 movsr v00001 d1
3: 2C820800 movsr v00002 v00000
4: 2C830801 movsr v00003 v00001
5: 2C820801 movsr v00002 v00001
6: 2C830800 movsr v00003 v00000
7: 2C800802 movsr v00000 v00002
8: 2C810803 movsr v00001 v00003
9: 20020002 gcall t2 0 v00000 v00001
10: 08000000
11: 00000801
12: 10000002 end t2
13: 10000002 end t2
instruction count:
12
#<sys:vm-desc: 9de1b50>
TXR Lisp's compiler performs frame elimination optimization whereby the v registers get reassigned to t registers, eliminating the frame setup. The t registers are subject to some decent data flow optimizations and can disappear entirely, like in this case.
- aappleby 4y agoAuthor here - If the transition takes place "before the last form of the body is evalulated", how does this work in the context of potentially deeply-nested function calls or assignments to sub-fields of some complex data structure?
- kazinator 4y agoIt doesn't. This is like the minimum viable temporal construct we should come up with in under 30 minutes of coding, in a single let-like statement. For more general packaging on my division something like a macro with establishes a temporal contour. The effects are settled when this contour terminates. Within that contour we have special constructs which indicates that certain variables have temporal semantics. We clearly need some way to and close multiple temporal blocks so that they behave as a unit. In a temporal language this contour which binds together the blocks will disappear because part of the semantics of any block, such as a function body. We can make some dedicated helper constructs like defun-temporal, whose body is a temporal contour. Assignments to subfields are an interesting problem. I made the simplifying assumption that a temporal place binds together two symbolic places: the old and new. I could quite easily redefine this place so that its arguments, or rather it's left argument, is an arbitrary place. Reading the number location will access that place. Writing the place will go to the temporary variable, which eventually is committed to the underlying location. When the place is just a variable this will generate the same code as it does now.