3 ms·
In TXR Lisp, I implemented a design which achieves a harmony between the separation of function/value bindings, and their union. You can program in both styles
by kazinator 10y ago
In TXR Lisp, I implemented a design which achieves a harmony between the separation of function/value bindings, and their union. You can program in both styles in the same scope.
http://www.nongnu.org/txr/txr-manpage.html#N-02DC9E04 http://www.nongnu.org/txr/txr-manpage.html#N-02DC9E04
TL;DR: The underlying Lisp dialect is a Lisp-2. However, when a form is denoted by square brackets, Lisp-1-style evaluation applies to each of its positions: every position is evaluated as a form, and any form which is a symbol is evaluated in a conflated single namespace. This rule does not recurse into the forms: any nested forms also have to use square brackets to work the same way.
Thus we can do:
[mapcar cons '(1 2 3) '(a b c)] ;; no (fun cons)
[f arg] ;; f is a variable
And so on.
This works by making [...] a syntactic sugar for (dwim ...) where dwim is a special operator. This operator is deeply integrated into the language (both macro-expansion semantics and evaluation), which is why it is able to change the semantics of name lookup.
A rule I imposed is that macros are not allowed in this notation:
[let ((a 3)) ...] ;; nonsense: let not recognized as operator here
so it is actually implements the Lips-1 mantra literally: "evaluate all positions of the form equally". There is no exception for recognizing a function-like macro in the leftmost position of a form, but not doing so in other positions.
- kazinator 10y agoIt gets tricky. Bracket/dwim forms can designate access to objects, because sequences are funcallable. So we can do: (set [a b] c) What does that look like? 1> (sys:expand '(set [a b] c)) (let ((#:g0138 (sys:lisp1-value b)) (#:g0137 c)) (sys:lisp1-setq a (sys:dwim-set (sys:lisp1-value a) #:g0138 #:g0137)) #:g0137) The a and b forms turned into (sys:lisp1-value a) and (sys:lisp1-value b). These secret operators bring in the name lookup semantics anywhere it is required, disembodied from the dwim operator. Also sys:lisp1-setq is used to store the updated sequence back into a. sys:setq can't be used because a could be a function binding in the given scope. There are hacks you can do in Common Lisp to achieve a bit of a Lisp-1 style, but not to such detail. See this piece of documentation under labels and flet: http://www.nongnu.org/txr/txr-manpage.html#N-0209307D http://www.nongnu.org/txr/txr-manpage.html#N-0209307D "Furthermore, function bindings introduced by labels and flet also shadow symbol macros defined by symacrolet, when those symbol macros occur as arguments of a dwim form." A test case for this is the following, which must produce (1 1 1): (let ((g (lambda (x) 0))) (symacrolet ((f g)) (flet ((f (x) 1)) [mapcar f '(a a a)]))) ;; Lisp-1 f: flet shadows symbol macro However, this must produce (0 0 0): (let ((g (lambda (x) 0))) (symacrolet ((f g)) (flet ((f (x) 1)) (mapcar f '(a a a))))) ;; now Lisp-2 var reference