From: jeff@aiai.ed.ac.uk (Jeff Dalton)
Newsgroups: comp.lang.lisp
Subject: Re: creating LISP structures dynamically?
Date: 24 Jul 92 19:39:03 GMT
Organization: AIAI, University of Edinburgh, Scotland

In article <12ft7kINNbb3@early-bird.think.com> barmar@think.com (Barry Margolin) writes:

[Someone wants to create structures synamically in a planner/
scheduler, but their specific question is about how to create
_symbols_ dynamically.]

>MAKE-SYMBOL or INTERN, depending on whether you want the symbol to be
>interned in a package or not.

Or even GENTEMP or (sometimes) GENSYM.

>However, going back to your original problem description, I'm not sure what
>the purpose of these symbols is.  [...]

>  Your Subject line says that you want to create Lisp structures
>dynamically, and that's what MAKE-MOVE (which I assume is a constructor
>defined by DEFSTRUCT) does; but you somehow have gotten the notion that in
>order to create structures dynamically you also need to create symbols
>dynamically, but there's no such connection.

This puzzles me too.  I've seen it again and again in AI programs
such as planners and schedulers, and from time to time I've asked
the authors why they do it.  

Sometimes they think it's necessary to have an extra level of
indirection, but whenever I've pressed them on it (so far), it's
turned out that they aren't actually using the extra indirection
in any interesting way.

Other people have thought that unless they attached each structure 
to a symbol they would be getting _copies_ of the structures whenever
they passed them as arguments in function calls.  (They plan to pass
the symbols instead.)  That is, they are in the grip of a
misunderstanding about how Lisp works.

But I don't think that's the whole story.  Apart from various
confucions, there seem to a a couple of other factors at work:

  1. At one time, a common technique was to represent structures
     as symbols with the plists holding the slots.  Because of
     that, or for some other reason, the use of named objects
     became conventional in certain areas of AI, and new programmers
     learned this convention when they learned how to write AI
     programs.

  2. Some people find it more natural to have named objects.
     (Certainly some people find systems such as KEE that use
     named objects more natural that, say, CLOS.)

In any case, I have never succeeded (so far) in getting anyone to stop
using sybols or some other ids for structures.  The things that keep
them from making the change seem to be:

  1. They want to be able to type in an id and get back the struct.

  2. They want complex and circular structures to print nicely.
     For instance, if one struct contains (pointers to) others,
     they want the others to print as simple ids rather than as
     some complex contents in #s(...) notation.

My solution to these problems is to define :PRINT-FUNCTIONs that
produce ids (as needed) that can be typed-in to get back the same
struct.

However, because Common Lisp does not provide a way for a program to
find all the slots of a struct (unless maybe the CLOS MOP now does
it), defining a :PRINT-FUNCTION makes it difficult to get the #s(...)
version in the cases where you still need it.

-- jd
Newsgroups: comp.lang.lisp