← Feed
@psf@oldbytes.space
2026-07-02 21:34 UTC
@Kazinator@mstdn.ca muLISP has dynamic scope and provides no mechanism to close a function over a particular environment. So strictly speaking it has only open lambdas, not closed lambdas. It certainly doesn't have lexical closures.
In certain dynamically scoped Lisps, there is a special form that solves the FUNARG problem by associating a function with a given environment so it can be called in the same environment later. Sometimes the primitive for this is called FUNCTION. muLISP doesn't have that, and looking through manuals for Maclisp and Interlisp, it is not necessarily a reliable solution for the FUNARG problem in those dialects either. I welcome corrections from anyone who has used closures in these old dialects!
What is more common, is to find a hacky system for simulating closures by packaging a lambda together with a specified alist of values (not an environment). muLISP provides one: this is what "CLOSURE" is. Lisp Machine Lisp works the same way. In these dialects, the closest you get to a "closure" is something like,
(LAMBDA CLOSURE-ARGLIST (CLOSURE-APPLY (QUOTE (LAMBDA (X) (* X A))) (QUOTE ((A . 5))) CLOSURE-ARGLIST))
where a lambda (LAMBDA (X) (* X A)) and an alist ((A . 5)) holding the values "closed over" are stored as arguments of CLOSURE-APPLY. The alist is mutable and used to retain the closure's state between calls.
What I've done with my rplaca nonsense is manipulate several of these simulated "closures" to share the same alist, which yields the behavior I wanted, similar to writing
(let ...
(lambda ...)
(lambda ...))
in a lexically scoped Lisp.
In any modern system you would have lexical scope directly available, so you would get working closures without the need to perform such unnatural acts.
Replies (2)
-
@psf@oldbytes.space I see from the MuLISP manual that closure is a dynamic closure mechanism. But it is not a true dynamic closure mechanism. The funtion takes a list of variable symbols, and captures their current values. Then when the closure is applied, those values are temporarily installed into the dynamic scope. If the constructor were modified to take the pseudo-environment directly, you wouldn't have to do the RPLACA manipulation.
A true dynamic closure captures a porton of the dynamic environment itself (not simply copying the values) and then installs it when the closure is called.
I implemented such a thing 25 years ago in a dynamically scoped Lisp. Newly funcalled dynamic frames were specially marked with a "top" bit. When a lambda was evaluated, it captured a slice of the dynamic environment up to the closest frame marked as top. Lambda invocation grafted the captured frames to the top of the current dynamic environment.
lambdas captured under the same environment shared the variables properly.
Open ##3562914
-
@Kazinator@mstdn.ca full historical context - https://groups.google.com/g/comp.lang.lisp/c/3CkNwKawXos/m/8L2uDCaItroJ - what a mess, eh?
Open ##5287717