home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!mcsun!news.funet.fi!hydra!klaava!cc.helsinki.fi!pirinen
- From: pirinen@cc.helsinki.fi
- Newsgroups: comp.lang.lisp
- Subject: Re: Adding FUNCTION to a shallow-binding interp.
- Summary: external value cells
- Keywords: closure shallow-binding
- Message-ID: <1992Nov12.144242.1@cc.helsinki.fi>
- Date: 12 Nov 92 12:42:42 GMT
- References: <1992Nov2.182028.22168@uk03.bull.co.uk>
- Sender: news@klaava.Helsinki.FI (Uutis Ankka)
- Organization: University of Helsinki
- Lines: 31
-
- In article <1992Nov2.182028.22168@uk03.bull.co.uk>, bbirch@hemel.bull.co.uk (Bill Birch) writes:
- > I have a shallow-binding interpreter, which has no lexical closure.
- > My attempts to implement (function ) have been only partially succesful,
- > am I right in assuming that this is impossible?
- You're right: it is impossible with the tools you're using. Basically,
- since the same name should have different bindings in each different
- scope, you need a distinct storage location for each, and SETQ of a
- local variable has to know about them. A good test case is:
-
- (defun two-functions(&aux x)
- (list #'(lambda () x) #'(lambda (y) (setq x y))))
-
- Each call of TWO-FUNCTIONS has to create a new binding of X, which the
- two functions share.
-
- The standard solution for shallow-binding systems is external value
- cells: a closed-over variable has a pointer to an external value cell,
- which is a dynamically allocated object that holds the value of the
- variable in that scope; each closure has a list of variables it closes
- over and their external value cells. For example, TWO-FUNCTIONS might
- return something like this:
-
- (#<closure (lambda () x) ((x . #1=#<external-value-cell nil))>
- #<closure (lambda (y) (setq x y)) ((x . #1#))>)
-
- with new closures containing a new external value cell for each
- invocation. There are quite a few pitfalls in getting closures just
- right, good luck!
- ___
- Pekka P. Pirinen pekka.pirinen@helsinki.fi
- Lisp implementor
-