Elektrine lite

← Feed

@Kazinator@mstdn.ca

2026-07-02 21:02 UTC

@psf@oldbytes.space What is that? Are closures just tree structure in muLISP, whereby you can use rplaca to replace some element, like the closed-over environment? Which is just a naive alist? (Why does that need the (quote ...) wrapping). So you made a couple of closures which reference a free variable, and then you injected a shared environment which closes the variable. Whether you can do that in another Lisp depends on whether such a manipulation of its functions is allowed. There may also be multiple representations to deal with: one way to manipulate the environment of a compiled closure vs. interpreted one. I suspect that in compiled Lisps, you may not be able to do it exactly like that. If a closure is compiled whose body refers to a free variable x, the generated code will not be accessing that x through the environment vector: the x references get compiled differently based on its scoping discipline.

Replies (1)

  • @psf@oldbytes.space 2026-07-02 21:34

    @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.

    Open ##3562915