home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.lisp
- Path: sparky!uunet!darwin.sura.net!mips!sdd.hp.com!usc!elroy.jpl.nasa.gov!ufo!Aig.Jpl.Nasa.Gov!charest
- From: charest@Aig.Jpl.Nasa.Gov (Len Charest)
- Subject: Re: local macro definitions
- Message-ID: <1992Jul27.220158.28453@jpl-devvax.jpl.nasa.gov>
- Followup-To: comp.lang.lisp
- Sender: usenet@jpl-devvax.jpl.nasa.gov (For NNTP so rrn will be able to post)
- Nntp-Posting-Host: ai-cyclops
- Reply-To: charest@aig.jpl.nasa.gov
- Organization: NASA/Jet Propulsion Laboratory
- References: <4133@inca.comlab.ox.ac.uk>
- Date: Mon, 27 Jul 1992 22:01:58 GMT
- Lines: 63
-
- In article <4133@inca.comlab.ox.ac.uk>, gordon@robots.oxford.ac.uk (Gordon Buxton) writes:
- |> What I would really
- |> like to do is to let the user refer to the other slot names in the
- |> frame as if they were locally bound variables:
-
- All of the approaches suggested here imply that the list of frame slots
- is available at compile-time (really macroexpansion-time). Is this
- truly the case?
-
- |>
- |> 1. A list of all of the slots present in a frame is available; the
- |> user code calling sequence could bind each slot name to the value of
- |> the (val ..) function called for that slot.
- |>
- |> -- Inefficient to say the least. In fact it gets more hideous
- |> the more you think about it.
-
- Why does is this intrinsically inefficent? You could hide the binding
- mechanism from the user code by wrapping the following code fragment
- around the user code:
-
- (let ((slots <list of frame slots>))
- (flet ((val-in-self (slot)
- (val self slot)))
- ;;dynamically bind the slot names to values
- (progv slots (mapcar #'val-in-self slots)
- ...<user code here>...
- )))
-
- The only (potential) drawback I can see is that calling VAL on each of
- the slots of the frame SELF is going to force execution of the user
- code in some cases (e.g, 'unbound' slots as you mentioned), possibly
- resulting in infinite recursion.
-
- |> 2. Do some parsing on the user code, to work out which slots are
- |> needed, and just go and get them before executing the code.
- |>
- |> -- Doesn't strike me as a good idea.
-
- This is a great idea! If you are certain that a slot name (i.e.,
- symbol) is really just a symbolic reference to the slot's value in this
- context then you could re-write the body of the user code so that every
- reference to a slot name is replaced by the corresponding (eq) value.
- For example
-
- (flet ((insert-value (slot body)
- (nsubst (val self slot) slot body :test #'eq)))
- (let ((body (copy-tree <user code>)))
- (loop for slot in <list of frame slots>
- do (setq body (insert-value slot body)))
- body))
-
- You might even choose to 'optimize' other parts of the user code at
- this point.
-
- |> 3. Bind each of the slot names to a macro (macrolet ... ?) that then
- |> gets expanded to (val self '<slot-name>) upon execution.
-
- As barmar has already pointed out, the form to use is SYMBOL-MACROLET.
- ..................................................
- Len Charest, Jr.
- JPL Artificial Intelligence Group
- charest@aig.jpl.nasa.gov
-