From: Claudio Jolowicz
Subject: structure containing structure - newbie
Date: 
Message-ID: <au861k$74j$00$1@news.t-online.com>
Hi!

How can I define a structure whose slots are structures themselves? Here's
some code to show what I mean:

(defstruct riverside
   missionaries
   cannibals
   boat)

(defstruct river
   (leftside :type riverside)
   (rightside :type riverside))

thx in advance,

Claudio (= newbie)

From: Barry Margolin
Subject: Re: structure containing structure - newbie
Date: 
Message-ID: <i6ON9.12$ak2.1214@paloalto-snr1.gtei.net>
In article <···············@news.t-online.com>,
Claudio Jolowicz <················@gmx.net> wrote:
>How can I define a structure whose slots are structures themselves? Here's
>some code to show what I mean:

Common Lisp doesn't require you to specify the types of slots, and there's
usually little gained by doing it.

>(defstruct riverside
>   missionaries
>   cannibals
>   boat)
>
>(defstruct river
>   (leftside :type riverside)
>   (rightside :type riverside))

Almost right, but before you can specify slot options you have to specify a
default value.  So it should be:

(defstruct river
  (leftside (make-riverside) :type riverside)
  (rightside (make-riverside) :type riverside))

-- 
Barry Margolin, ······@genuity.net
Genuity, Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.
From: JP Massar
Subject: Re: structure containing structure - newbie
Date: 
Message-ID: <3e07af3d.64186126@netnews.attbi.com>
On Tue, 24 Dec 2002 00:24:51 +0100, "Claudio Jolowicz"
<················@gmx.net> wrote:

>Hi!
>
>How can I define a structure whose slots are structures themselves? Here's
>some code to show what I mean:
>
>(defstruct riverside
>   missionaries
>   cannibals
>   boat)
>
>(defstruct river
>   (leftside :type riverside)
>   (rightside :type riverside))
>
>thx in advance,
>
 
You can't really.  In Lisp, (almost) everything is pointer based.

If you are familar with 'C', then you know that you could define
a structure that contains substructures as slots, or you could define
a structure whose slots are pointers to the substructures, and
these substructures would exist off somewhere else in memory.

In Lisp, you always use the latter when it comes to DEFSTRUCTS.

To get the kind of behavior you want you may want to consider
the Common Lisp Object System (CLOS) and CLASSES, which are in
some sense a superset of DEFSTRUCT functionality.

Hope this helps.
From: Gabe Garza
Subject: Re: structure containing structure - newbie
Date: 
Message-ID: <87isxktalq.fsf@ix.netcom.com>
······@alum.mit.edu (JP Massar) writes:

> On Tue, 24 Dec 2002 00:24:51 +0100, "Claudio Jolowicz"
> <················@gmx.net> wrote:
> 
> >Hi!
> >
> >How can I define a structure whose slots are structures themselves? 
>
> You can't really.  In Lisp, (almost) everything is pointer based.

Even if an implementation does everything "pointer based" it can still
make sense to supply types for structure slots.  The compiler can use
the provided information to do fast inline access (if the safety level
is appropriate) and can also use the information if it does any type
inferencing.  That's why the syntax for DEFSTRUCT allows you to supply
a type. :)

I also don't see why an implementation couldn't set aside the actual
storage for the object the slot will contain (if the specified type
has a known, fixed length) as part of the storage allocated for the
structure.  The spec explicitely states that redefining structures is
undefined and the slot-access mechanism is implementation dependent,
so it seems like this would be legal?  It doesn't seem like it would
complicate the implementation that much (though, having never
implemented a Lisp, I may be very wrong :)) and I'd guess the cache
would like it.

Gabe Garza
From: Kent M Pitman
Subject: Re: structure containing structure - newbie
Date: 
Message-ID: <sfwy96fhg6j.fsf@shell01.TheWorld.com>
Gabe Garza <·······@ix.netcom.com> writes:

> I also don't see why an implementation couldn't set aside the actual
> storage for the object the slot will contain (if the specified type
> has a known, fixed length) as part of the storage allocated for the
> structure. 

As far as I can tell, it could, but it's probably a bad idea.

It means that a slot access will often cons.  That is an
implementation change that requires a fair bit of bookkeeping.  The
compiler will want to do tons more flow analysis in order to avoid
this in cases where the accesses are only local.

Lisp programmers generally find this confusing and annoying because it
means repeated accesses to the same cell may not be EQ, and may cause
what they think are unnecessary needs for GC.

Further, for any type that was not a primitive type, there's the issue of
recursive structures.  A cons, for example, has a cons in its cdr.  Surely
you can't allocate storage for the rest of a list in the cdr of a list.
Unless you're thinking of it as just a pointer.  But Lisp programmers
don't make the distinction between a pointer and a non-pointer.  So this
distinction is unnatural.

And while for non-recursive primitive types you might want to inline
them, this works only for the slot access to the cell. e.g., consider
a frob with a location slot that was a point with x,y slots.  the
problem is the identity of the value of the location slot.  you still
have to allocate as part of the frob thing not only the x,y but also
the header information about the object that is a location, otherwise
you can't do dynamic type-of kinds of operations nor can you do
eql-like identity operations on the location object itself.

> It doesn't seem like it would complicate the implementation that much

The cost isn't just to the implementation, but to other user programs
that attempt to use these data structures.  It completely changes the
way in which the data is to be used.
From: Claudio Jolowicz
Subject: Re: structure containing structure - newbie
Date: 
Message-ID: <au9gc3$gbk$02$1@news.t-online.com>
Thank you all for your help!

Claudio