From: Karstens Rage
Subject: Structure interdependent slots? (newbie)
Date: 
Message-ID: <-Zednd1ar7qg5LLeRVn-vw@comcast.com>
Maybe I'm reading this wrong but if I have two structures:

(defstruct starship
  (captain nil)
  (name nil)
  (shields 'down)
  (condition 'green)
  (speed 0))

(defstruct captain
  (name nil)
  (age 0)
  (starship nil))

is there supposed to be an interdependency between captain and starship
(structure name vs. slot name)

So for instance if I do:

(setf enterprise (make-starship :name "Enterprise"))

and

(setf kirk (make-captain :name "James T. Kirk" :age 35))

then should

(setf (starship-captain enterprise) kirk)

somehow set kirk's :starship to enterprise? I think you have to do that
explicitly?

k

From: Pascal Bourguignon
Subject: Re: Structure interdependent slots? (newbie)
Date: 
Message-ID: <87fys0s9hy.fsf@thalassa.informatimago.com>
Karstens Rage <········@rage.com> writes:

> Maybe I'm reading this wrong but if I have two structures:
>
> (defstruct starship
>   (captain nil)
>   (name nil)
>   (shields 'down)
>   (condition 'green)
>   (speed 0))
>
> (defstruct captain
>   (name nil)
>   (age 0)
>   (starship nil))
>
> is there supposed to be an interdependency between captain and starship
> (structure name vs. slot name)
>
> So for instance if I do:
>
> (setf enterprise (make-starship :name "Enterprise"))
>
> and
>
> (setf kirk (make-captain :name "James T. Kirk" :age 35))
>
> then should
>
> (setf (starship-captain enterprise) kirk)
>
> somehow set kirk's :starship to enterprise? 
>
> I think you have to do that explicitly?

You seem to have a strange usage for '?'.   Sometimes your syntax
looks like a question but you don't write any question mark.
Sometimes your syntax looks like an affirmation but you add a question
mark.

It makes hard for us to assertain what is fact and what is the question.


Anyways, making wild guesses, I could say that it all depends on the
level we're considering.

What are the specifications?
What is the meaning of the data built with these data structures?


If you just give use these two s-expressions:

> (defstruct starship
>   (captain nil)
>   (name nil)
>   (shields 'down)
>   (condition 'green)
>   (speed 0))
>
> (defstruct captain
>   (name nil)
>   (age 0)
>   (starship nil))

I would say that yes, it looks like it's expected to have

(defun <=> (&rest preds) (or (every (function null) preds)
                             (not (some (function null) preds))))

(assert (<=> (eq (captain-starship c) s)
             (eq (starship-captain s) c)))


More over, with no other definition, then writing just:


> (setf (starship-captain enterprise) kirk)

would break the above invariant, so it would not be enough; you'd have to write:

(progn (setf (starship-captain enterprise) kirk)
       (setf (captain-starship kirk) enterprise))



But I'm making strong assumptions.  You could have redefined (setf
starship-captain) as:

(defun (setf starship-captain) (new-captain star-ship)
   (setf (slot-value star-ship   'captain)   new-captain
         (slot-value new-captain 'starship)  star-ship)
    new-captain)


Note: this (setf starship-captain) is totally implementation
dependant. CLHS slot-value:

    The specific behavior depends on object's metaclass.  An error is
    never signaled if object has metaclass standard-class.  An error
    is always signaled if object has metaclass built-in-class.  The
    consequences are unspecified if object has any other metaclass-an
    error might or might not be signaled in this situation.  Note in
    particular that the behavior for conditions and structures is not
    specified.

The metaclass of structures is specified to be structure-class, which
happens to be a standard-class in this implementation, but this is not
necessarily the case.  If it was not the case, one could save the old
(setf starship-captain) function and used it instead of slot-value, or
just use classes instead of structures.



So now you can write:

[45]> (setf *print-circle* t)
T
[46]>  (setf (starship-captain enterprise) kirk)
#1=#S(CAPTAIN :NAME "James T. Kirk" :AGE 35
      :STARSHIP
      #S(STARSHIP :CAPTAIN #1# :NAME "Enterprise" :SHIELDS DOWN :CONDITION GREEN
         :SPEED 0))
[47]> (assert (<=> (eq (captain-starship kirk) enterprise)
                   (eq (starship-captain enterprise) kirk)))
NIL
[55]> 


But the actual specifications could as well ask for something else.

-- 
__Pascal Bourguignon__                     http://www.informatimago.com/
Cats meow out of angst
"Thumbs! If only we had thumbs!
We could break so much!"
From: Karstens Rage
Subject: Re: Structure interdependent slots? (newbie)
Date: 
Message-ID: <5fWdnVRtfLgoFbLeRVn-uA@comcast.com>
Pascal Bourguignon wrote:
> 
> You seem to have a strange usage for '?'.   Sometimes your syntax
> looks like a question but you don't write any question mark.
> Sometimes your syntax looks like an affirmation but you add a question
> mark.
> 
> It makes hard for us to assertain what is fact and what is the question.
> 
I apologize for that. That has been mentioned before. I'll try to be
more grammatically correct.

My questions were:
Is there supposed to be an interdependency between captain and starship
(structure name vs. slot name)?

and

should (setf (starship-captain enterprise) kirk) somehow affect the
:starship slot for kirk?


> 
> Anyways, making wild guesses, I could say that it all depends on the
> level we're considering.
> 
> What are the specifications?
> What is the meaning of the data built with these data structures?
> 
> 
I'm not even sure I understand these two questions. Maybe a little
background would be helpful. The structures are from Chapter 12 of
Common Lisp: A Gentle Introduction to Symbolic Computation by David
Touretzky. On page 379 there is an exercise to create the captain
structure with "NAME, AGE and SHIP." Note that the slot name is "SHIP"
not "STARSHIP."

Then the exercise goes on to say "Make the Enterprise point back to Kirk
through its CAPTAIN component." "Enterprise" is a previous STARSHIP from
the text. Then the confusing part was the next sentence. "Notice that
when you print Kirk, you see his ship as well."

So I modified the defstruct of CAPTAIN to change the slot name from SHIP
to STARSHIP to see if CLISP 2.35 would make some kind of association
between the slot name STARSHIP in the CAPTAIN structure and the STARSHIP
structure.

> If you just give use these two s-expressions:
> 
> 
>>(defstruct starship
>>  (captain nil)
>>  (name nil)
>>  (shields 'down)
>>  (condition 'green)
>>  (speed 0))
>>
>>(defstruct captain
>>  (name nil)
>>  (age 0)
>>  (starship nil))
> 
> 
> I would say that yes, it looks like it's expected to have
> 
> (defun <=> (&rest preds) (or (every (function null) preds)
>                              (not (some (function null) preds))))
> 
> (assert (<=> (eq (captain-starship c) s)
>              (eq (starship-captain s) c)))
> 
>
I did not have the expectation that it would make the associations
automatically. From what I could tell from the HyperSpec, there was no
magic association between slot names in different structures.
From: Pascal Bourguignon
Subject: Re: Structure interdependent slots? (newbie)
Date: 
Message-ID: <87br2os25z.fsf@thalassa.informatimago.com>
Karstens Rage <········@rage.com> writes:

> Pascal Bourguignon wrote:
>> 
>> You seem to have a strange usage for '?'.   Sometimes your syntax
>> looks like a question but you don't write any question mark.
>> Sometimes your syntax looks like an affirmation but you add a question
>> mark.
>> 
>> It makes hard for us to assertain what is fact and what is the question.
>> 
> I apologize for that. That has been mentioned before. I'll try to be
> more grammatically correct.
>
> My questions were:
> Is there supposed to be an interdependency between captain and starship
> (structure name vs. slot name)?
>
> and
>
> should (setf (starship-captain enterprise) kirk) somehow affect the
> :starship slot for kirk?
>
>
>> 
>> Anyways, making wild guesses, I could say that it all depends on the
>> level we're considering.
>> 
>> What are the specifications?
>> What is the meaning of the data built with these data structures?
>> 
>> 
> I'm not even sure I understand these two questions. Maybe a little
> background would be helpful. The structures are from Chapter 12 of
> Common Lisp: A Gentle Introduction to Symbolic Computation by David
> Touretzky. On page 379 there is an exercise to create the captain
> structure with "NAME, AGE and SHIP." Note that the slot name is "SHIP"
> not "STARSHIP."


Well, "should" and "must" indicate some kind of obligation, but the
questions are:

- who is the authority that forces this obligation?
- who is the party that is forced by this obligation?
- how can the authority enforce this obligation? (by what means?)

Here, we may consider as authorities:
- the ANSI Common Lisp specifications (as documented in CLHS for example),
- the customer of the software,
- the author of the book and example code.

And the party that must enforce the obligation:
- the Common Lisp implementation,
- the programmer writting the software.


So when you ask:

> Is there supposed to be an interdependency between captain and starship
> (structure name vs. slot name)?

the answer depends on who does the suppositions.


The ANSI Common Lisp specifications cannot make such a
supposition. While lisp has been used to research and implement AI,
they don't assume that an AI inteligent enough to make out such an
interdependency from the name of the structure types can be
implemented.

The customers of a captains and starship management system would most
certainly assume that such an interdependency is implemented in the
software, since it's a well known interdependency in their domain.

It would be the work of the programmer to ensure that such an
interdependency is implemented.

And when you ask:

> should (setf (starship-captain enterprise) kirk) somehow affect the
> :starship slot for kirk?

again, the answer depends on the authority.

ANSI CL Spec says no.

The customer says yes.

The programmers must therefore enforce this obligation, if he wants to
satisfy the customer.  You've seen how it could be done in my previous
message.


> Then the exercise goes on to say "Make the Enterprise point back to Kirk
> through its CAPTAIN component." "Enterprise" is a previous STARSHIP from
> the text. Then the confusing part was the next sentence. "Notice that
> when you print Kirk, you see his ship as well."
>
> So I modified the defstruct of CAPTAIN to change the slot name from SHIP
> to STARSHIP to see if CLISP 2.35 would make some kind of association
> between the slot name STARSHIP in the CAPTAIN structure and the STARSHIP
> structure.
> [...]
> I did not have the expectation that it would make the associations
> automatically. From what I could tell from the HyperSpec, there was no
> magic association between slot names in different structures.

Indeed, the ANSI Common Lisp specifications don't ask for such an
obligation, because they can't expect the Common Lisp interpreters or
compilers to undestand the meaning of the words captain and starship
and the relationships that may exist between a captain and a starship.



I don't see exactly what's confusing. Once you've defined kirk and
enterprise, you can print them and see that kirk's starship slot and
enterprise's captain slot both contain NIL:

[7]> kirk
#S(CAPTAIN :NAME "James T. Kirk" :AGE 35 :STARSHIP NIL)
[8]> enterprise
#S(STARSHIP :CAPTAIN NIL :NAME "Enterprise" :SHIELDS DOWN :CONDITION GREEN
   :SPEED 0)

Then when you modify the captain slot of the enterprise, you can see
that now the captain slot of the enterprise contains the kirk
structure:

[9]> (setf (starship-captain enterprise) kirk)
#S(CAPTAIN :NAME "James T. Kirk" :AGE 35 :STARSHIP NIL)
[10]> enterprise
#S(STARSHIP :CAPTAIN #S(CAPTAIN :NAME "James T. Kirk" :AGE 35 :STARSHIP NIL)
   :NAME "Enterprise" :SHIELDS DOWN :CONDITION GREEN :SPEED 0)

Note that the kirk structure is not physically copied into the
enterprise structure. Lisp works with references, and only a reference
to the same structure refered by the variable kirk is put in the
enterprise structure. You can see it with:


[11]> (setf *print-circle* t)
T
[12]> (list kirk enterprise)
(#1=#S(CAPTAIN :NAME "James T. Kirk" :AGE 35 :STARSHIP NIL)
 #S(STARSHIP :CAPTAIN #1# :NAME "Enterprise" :SHIELDS DOWN :CONDITION GREEN
    :SPEED 0))

Both kirk and (starship-captain enterprise) refer to the same
structure (identified with #1= and #1# in the above s-expression).




Perhaps the in the creation of the captain kirk the starship was specified:

[13]> (setf enterprise (make-starship :name "Enterprise"))

#S(STARSHIP :CAPTAIN NIL :NAME "Enterprise" :SHIELDS DOWN :CONDITION GREEN
   :SPEED 0)
[14]> (setf kirk (make-captain :name "James T. Kirk" :age 35 :starship enterprise))
#S(CAPTAIN :NAME "James T. Kirk" :AGE 35
   :STARSHIP
   #S(STARSHIP :CAPTAIN NIL :NAME "Enterprise" :SHIELDS DOWN :CONDITION GREEN
      :SPEED 0))

Then we have a kirk ---> enterprise reference, and we need only to set
the back reference:

[15]> (setf (starship-captain enterprise) kirk)
#1=#S(CAPTAIN :NAME "James T. Kirk" :AGE 35
      :STARSHIP
      #S(STARSHIP :CAPTAIN #1# :NAME "Enterprise" :SHIELDS DOWN :CONDITION GREEN
         :SPEED 0))

[16]> (list kirk enterprise)
(#1=#S(CAPTAIN :NAME "James T. Kirk" :AGE 35
       :STARSHIP #2=#S(STARSHIP :CAPTAIN #1# :NAME "Enterprise" :SHIELDS DOWN
                               :CONDITION GREEN :SPEED 0))
 #2#)


It's important to (setf *print-circle* t) before working with such
circular structures.


In general when we modelize this kind of relationship, it's better to
reify them somewhat, and to put them out of the objects in the
relationship.  We can keep the references inside the object
(structures) as an optimization, but it's better to manipulate them
independantly.  And added benefit to this reification is that most
often you'll find your domain needs to attach attributes to the
relationship itself.  For example, Kirk was captain of the Enterprise
only from stardate 2245 to 2250, and shortly twice in 2285, if I read
correctly:
http://www.startrek.com/startrek/view/library/ships/article/70377.html

So we could have:

(defstruct command captain starship from-date to-date)

(defparameter *commands*
  (list (make-command :captain kirk :starship enterprise
              :from-date (stardate 2245 1 1) :to-date (stardate 2250 12 31))
        (make-command :captain kirk :starship enterprise
              :from-date (stardate 2285 1 1) :to-date (stardate 2285 2  28))
        (make-command :captain kirk :starship enterprise
              :from-date (stardate 2285 7 1) :to-date (stardate 2285 8  14))
        (make-command :captain decker :starship enterprise
              :from-date (stardate 2270 1 1) :to-date (stardate 2275 12 31))))

Then it would be difficult to set (starship-captain enterprise) to
some definite value since the enterprise had several captains, and
vice-versa for the captains.

You could write:

(defun captain-starship (captain date)
  (find-if (lambda (command)
              (and (eq captain (command-captain command))
                   (stardate<= (command-from-date command) date)
                   (stardate<= date (command-to-date command))))
           *commands*))

and:

(defun starship-captain (starship date)
  (find-if (lambda (command)
              (and (eq starship (command-starship command))
                   (stardate<= (command-from-date command) date)
                   (stardate<= date (command-to-date command))))
           *commands*))

But then you'd have difficulies for when the enterprise comes back in
time: you'd have to specify the time line with the stardates...


-- 
"Indentation! -- I will show you how to indent when I indent your skull!"
From: Alan Crowe
Subject: Re: Structure interdependent slots? (newbie)
Date: 
Message-ID: <867jdbttqk.fsf@cawtech.freeserve.co.uk>
Karstens Rage ponders
> My questions were:
> Is there supposed to be an interdependency between captain and starship
> (structure name vs. slot name)?
>
> and
>
> should (setf (starship-captain enterprise) kirk) somehow affect the
> :starship slot for kirk?

No and no. The built-in defstruct is a straight forward
facility that ignores any coincidences between structure
names and slot names.

Apart from creating types, you could do defstruct's job for
it by typing in a few defuns. For example, you could implement
(defstruct r a b) using vectors with

(defun make-r (&key a b)
  (vector a b))

(defun r-a (r)
  (aref r 0))

(defun (setf r-a)(new-value r)
  (setf (aref r 0) new-value))

(defun r-b (x)
  (aref x 1))

(defun (setf r-b)(new-value r)
  (setf (aref r 1) new-value))

(defun copy-r (r)
  (vector (r-a r)
	  (r-b r)))

There is no fancy back linking going on.

It that is too much typing you could write your own
defstruct (minus the type stuff). I've taken excessive care
to make sure that with my defstruct, called defbasic, I get
the names that I create in the sames package as the name
regardless of which package the slots go in. (Barely tested)

(defun prefix (string symbol)
  (mumble-fix :prefix string symbol))

(defun postfix (string symbol)
  (mumble-fix :postfix string symbol))

(defun mumble-fix (order string-or-symbol symbol)
  (let ((first (string string-or-symbol))
	(last (symbol-name symbol)))
    (ecase order
      (:prefix nil);correct order already
      (:postfix (rotatef first last)))
    (intern (concatenate 'string
			 first
			 "-"
			 last)
	  (symbol-package symbol))))

(defmacro defbasic (name &rest slots)
  `(progn
     (defun ,(prefix 'make name) (&key ,@slots)
       (vector ,@slots))
     ,@(loop for slot in slots
	     append (list `(defun ,(postfix slot name) (thing)
			     (aref thing ,(position slot slots)))
			  `(defun (setf ,(postfix slot name))(new-value thing)
			     (setf (aref thing ,(position slot slots))
				   new-value))))
     (defun ,(prefix 'copy name) (thing)
       (copy-sequence thing))
     (quote ,name)))

But if you can define your own (more or less) what is to
stop you writing a fancier one that does indeed check for
back links? Nothing. It might go like this

;;; Now for the fancy idea - spot when two symbols X and Y
;;; are in the relationship that X is a slot in mesh Y and Y
;;; is a slot in mesh X.

;;; Respond by contriving that setting slot X in Y to a mesh of type X
;;; sets slot Y in X to Y.

(defmacro defmesh (name &rest slots)
  `(progn
     (setf (get ',name 'mesh-slots) ',slots)
     (defun ,(prefix 'make name) (&key ,@slots)
       (vector ',name ,@slots))
     ,@(loop for slot in slots
	     append (list `(defun ,(postfix slot name) (thing)
			     (aref thing ,(+ 1 (position slot slots))))
			  `(defun (setf ,(postfix slot name))(new-value thing)
			     (setf (aref thing ,(+ 1 (position slot slots)))
				   new-value))))
     ,@(extra-bit name slots)
     (defun ,(prefix 'copy name) (thing)
       (copy-sequence thing))
     (quote ,name)))

(defun extra-bit (name slots)
  (let ((extra-defuns '()))
    (dolist (slot slots extra-defuns)
      (when (find name (get slot 'mesh-slots))
	(push `(defun (setf ,(postfix slot name))(new-value thing)
		 (setf (aref thing ,(+ 1 (position slot slots)))
		       (cond ((null new-value) nil) ;simple notion of unbound slot
			     ((eql (aref new-value 0)
				   ',slot)
			      ;; right kind of thing for slot
			      (progn
				;; install back pointer
				(setf (aref new-value
					    ,(+ 1 (position name
							    (get slot
								 'mesh-slots))))
				      thing)
				new-value))
			     (t (error "Cannot put ~A in slot ~A."
				       new-value ',slot)))))
	      extra-defuns)
	(push `(defun (setf ,(postfix name slot))(new-value thing)
		 (setf (aref thing ,(+ 1 (position name (get slot 'mesh-slots))))
		       (cond ((null new-value) nil)
			     ((eql (aref new-value 0)
				   ',name)
			      (progn
				(setf (aref new-value
					    ,(+ 1 (position slot slots)))
				      thing)
				new-value))
			     (t (error "Cannot put ~A in slot ~A."
				       new-value ',name)))))
	      extra-defuns)))))

Same idea but with a extra bit. The extra bit generates
extra defuns with more gubbins inside that adds backlinks.

Here is the boring way the built-in defstruct works:

* (defstruct captain name starship)
CAPTAIN

* (defstruct starship name engines crew captain)
STARSHIP

* (defparameter kirk (make-captain :name "Kirk"))
KIRK

* (defparameter enterprise (make-starship :name "Enterprise"
					  :engines 2
					  :crew 530))
ENTERPRISE

* (values enterprise kirk)
#S(STARSHIP :NAME "Enterprise" :ENGINES 2 :CREW 530 :CAPTAIN NIL)
#S(CAPTAIN :NAME "Kirk" :STARSHIP NIL)

* (setf *print-circle* t)
T

* (setf (starship-captain enterprise) kirk)
#S(CAPTAIN :NAME "Kirk" :STARSHIP NIL)

* (values enterprise kirk)
#S(STARSHIP
     :NAME "Enterprise"
     :ENGINES 2
     :CREW 530
     :CAPTAIN #S(CAPTAIN :NAME "Kirk" :STARSHIP NIL))
#S(CAPTAIN :NAME "Kirk" :STARSHIP NIL)

Starfleet is trying to make Kirk captain of the Enterprise,
but setf only does half the job, updating the Enterprises
records. We need another setf to finish the job.

However 

* (defmesh captain name starship)
CAPTAIN

* (defmesh starship name engines crew captain)
STARSHIP

* (defparameter kirk (make-captain :name "Kirk"))
KIRK

* (defparameter enterprise (make-starship :name "Enterprise"
					  :engines 2
					  :crew 530))
ENTERPRISE

* (values enterprise kirk)
#(STARSHIP "Enterprise" 2 530 NIL)
#(CAPTAIN "Kirk" NIL)

* (setf (starship-captain enterprise) kirk)
#1=#(CAPTAIN "Kirk" #(STARSHIP "Enterprise" 2 530 #1#))

* (values enterprise kirk)
#1=#(STARSHIP "Enterprise" 2 530 #(CAPTAIN "Kirk" #1#))
#1=#(CAPTAIN "Kirk" #(STARSHIP "Enterprise" 2 530 #1#))

There are probably bugs lurking in this code. I wrote it
just now because it seemed like a fun challenge to see if I
could. 

> From what I could tell from the HyperSpec, there was no
> magic association between slot names in different structures.

Correct. There is no magic association between slot names in
different structures, but there could be if you wrote the
code for it :-)

Alan Crowe
Edinburgh
Scotland
From: Thomas A. Russ
Subject: Re: Structure interdependent slots? (newbie)
Date: 
Message-ID: <ymill1r4s0u.fsf@sevak.isi.edu>
Karstens Rage <········@rage.com> writes:

> Maybe I'm reading this wrong but if I have two structures:
> 
> (defstruct starship
>   (captain nil)
>   (name nil)
>   (shields 'down)
>   (condition 'green)
>   (speed 0))
> 
> (defstruct captain
>   (name nil)
>   (age 0)
>   (starship nil))
> 
> is there supposed to be an interdependency between captain and starship
> (structure name vs. slot name)
> 
> So for instance if I do:
> 
> (setf enterprise (make-starship :name "Enterprise"))
> 
> and
> 
> (setf kirk (make-captain :name "James T. Kirk" :age 35))
> 
> then should
> 
> (setf (starship-captain enterprise) kirk)
> 
> somehow set kirk's :starship to enterprise? I think you have to do that
> explicitly?

Well, you would either have to do that explicitly, or else write an
appropriate SETF method on both STARSHIP and CAPTAIN to handle the
updates.

But at this point, you are starting to move from mere structures into
the realm of knowledge representation systems.  In those systems, you
can often declare particular slots or relations to be inverses of each
other and then have the system automatically manage the constraints.

Blowing my own horn, if you want to look at a good knowledge
representation system implemented in Common Lisp, go to our Loom page:

  http://www.isi.edu/isd/LOOM/

The syntax is a bit different from structures, but it is an example of
one of the specialized languages embedded in Common Lisp that is often
spoken of as a characteristic of Lisp-style problem solving.  And it
relies heavily on macrology to implement its language and the
interactions of the parts of the system.

-- 
Thomas A. Russ,  USC/Information Sciences Institute
From: Kaz Kylheku
Subject: Re: Structure interdependent slots? (newbie)
Date: 
Message-ID: <1127239040.503755.276680@z14g2000cwz.googlegroups.com>
Karstens Rage wrote:
> Maybe I'm reading this wrong but if I have two structures:
>
> (defstruct starship
>   (captain nil)
>   (name nil)
>   (shields 'down)
>   (condition 'green)
>   (speed 0))
>
> (defstruct captain
>   (name nil)
>   (age 0)
>   (starship nil))
>
> is there supposed to be an interdependency between captain and starship
> (structure name vs. slot name)

No, there is no such interdependence. The STARSHIP slot in the CAPTAIN
structure has nothing to do with the STARSHIP struct type.

If you want to introduce auto-computed dependencies among class slots,
look into Kenny Tilton's ``Cells''.

http://common-lisp.net/project/cells/

> So for instance if I do:
>
> (setf enterprise (make-starship :name "Enterprise"))
>
> and
>
> (setf kirk (make-captain :name "James T. Kirk" :age 35))
>
> then should
>
> (setf (starship-captain enterprise) kirk)
>
> somehow set kirk's :starship to enterprise? I think you have to do that
> explicitly?

The language implementation would have to be clairvoyant to infer that
relationship. In effect, your Lisp environment would have to understand
the semantics of the words ``captain'' and ``starship''; that there is
a one-to-one bijective relationship between the two, so that if a
starship object X has Y as its captain, then captain object Y should
have X as its ship.

In general, it's not true that when two structure or class types hold
references to each other, that the objects must be instantiated in
pairs that refer to each other. The actual instantiated objects could
be in a chain. A starship could have some captain whose starship slot
is some other ship, which has some other captain and so on.
From: Kenny Tilton
Subject: Re: Structure interdependent slots? (newbie)
Date: 
Message-ID: <sBYXe.13449$x43.2817171@twister.nyc.rr.com>
Kaz Kylheku wrote:
> Karstens Rage wrote:
> 
>>Maybe I'm reading this wrong but if I have two structures:
>>
>>(defstruct starship
>>  (captain nil)
>>  (name nil)
>>  (shields 'down)
>>  (condition 'green)
>>  (speed 0))
>>
>>(defstruct captain
>>  (name nil)
>>  (age 0)
>>  (starship nil))
>>
>>is there supposed to be an interdependency between captain and starship
>>(structure name vs. slot name)
> 
> 
> No, there is no such interdependence. The STARSHIP slot in the CAPTAIN
> structure has nothing to do with the STARSHIP struct type.
> 
> If you want to introduce auto-computed dependencies among class slots,
> look into Kenny Tilton's ``Cells''.

I was going to mention that, but....

> 
> http://common-lisp.net/project/cells/
> 
> 
>>So for instance if I do:
>>
>>(setf enterprise (make-starship :name "Enterprise"))
>>
>>and
>>
>>(setf kirk (make-captain :name "James T. Kirk" :age 35))
>>
>>then should
>>
>>(setf (starship-captain enterprise) kirk)
>>
>>somehow set kirk's :starship to enterprise? I think you have to do that
>>explicitly?
> 
> 
> The language implementation would have to be clairvoyant to infer that
> relationship.

Indeed, but to make things worse the OP said they were /not/ looking for 
an automatic solution. They seem to be asking if their database should 
be consistent, given that it is redundant. Duh. I think we should be 
asking them what the hell they are thinking, not trying to help them. 
And if it turns out they do want an automatic solution, it would be 
valuable to know where they have seen such a thing before.

I smell a troll.

-- 
Kenny

Why Lisp? http://wiki.alu.org/RtL_Highlight_Film

"I've wrestled with reality for 35 years, Doctor, and I'm happy to state 
I finally won out over it."
     Elwood P. Dowd, "Harvey", 1950
From: Kaz Kylheku
Subject: Re: Structure interdependent slots? (newbie)
Date: 
Message-ID: <1127252503.714725.198150@g43g2000cwa.googlegroups.com>
Kenny Tilton wrote:
> I smell a troll.

But then again, never attribute to malice what can be explained by ...
From: Karstens Rage
Subject: Re: Structure interdependent slots? (newbie)
Date: 
Message-ID: <ytidnY7UkfVvD63eRVn-hg@comcast.com>
Kenny Tilton wrote:
> Indeed, but to make things worse the OP said they were /not/ looking for
> an automatic solution. They seem to be asking if their database should
> be consistent, given that it is redundant. Duh. I think we should be
> asking them what the hell they are thinking, not trying to help them.
> And if it turns out they do want an automatic solution, it would be
> valuable to know where they have seen such a thing before.
>
In a follow up message, to Pascal, I clarified why I was asking. I was
thinking along the lines that structure slots are totally independent
and there is no way for the interpreter to make arbitrary associations
between slots. The responses so far have been way over the top in terms
of answering what I thought was a simple misunderstanding of something I
read in a book.

I quote the entire passage from Exercise 12.5 on page 379 of Common
Lisp: A Gentle Introduction to Symbolic Computation by David Touretzky.

"Create a defstruct for CAPTAIN with fields NAME, AGE, and SHIP.
Make a structure describing James T. Kirk, captain of the Enterprise,
age 35. Make the Enterprise point back to Kirk through its CAPTAIN
component. Notice that when you print Kirk, you see his ship as well.
Now define a print function for CAPTAIN that displays only the name,
such as #<CAPTAIN "James T. Kirk">."

The sentence that confused me was "Notice that when you print Kirk, you
see his ship as well."

I didnt see how that would happen automagically. I understand fully (and
thank you for all the huge amounts of code) that the associations
between slots can be made explicitly but I still don't understand the
confusing statement. I have found what I believe to be other errors in
the book so maybe its just another error.


> I smell a troll.
> 
Take a bath?

k
From: Thomas A. Russ
Subject: Re: Structure interdependent slots? (newbie)
Date: 
Message-ID: <ymifyrz48z8.fsf@sevak.isi.edu>
Karstens Rage <········@rage.com> writes:

> I quote the entire passage from Exercise 12.5 on page 379 of Common
> Lisp: A Gentle Introduction to Symbolic Computation by David Touretzky.
> 
> "Create a defstruct for CAPTAIN with fields NAME, AGE, and SHIP.
> Make a structure describing James T. Kirk, captain of the Enterprise,
> age 35.
                                             ^^^^^^^^^^^^^^^^^^^^^^^^^

OK.  I think here is where it is specified that you make the Kirk
captain structure point to the enterprise.

>  Make the Enterprise point back to Kirk through its CAPTAIN
> component. Notice that when you print Kirk, you see his ship as well.
> Now define a print function for CAPTAIN that displays only the name,
> such as #<CAPTAIN "James T. Kirk">."

Hmmm.  This actually raises a different point.  If you use the default
structure printer, which prints all of the slots, it looks like you
would end up with printing problems unless *PRINT-CIRCLE* were set to T,
since the Kirk's ship slot would point to Enterprise, whose captain's
slot would point to Kirk, etc.

> The sentence that confused me was "Notice that when you print Kirk, you
> see his ship as well."

Well, the reason for that, is that the printer for structures shows all
slot values, so the value for the ship slot will print the starship
structure as well, with the potential to cause circular printing problems.

> I didnt see how that would happen automagically. I understand fully (and
> thank you for all the huge amounts of code) that the associations
> between slots can be made explicitly but I still don't understand the
> confusing statement. I have found what I believe to be other errors in
> the book so maybe its just another error.

I think that the instructions are perhaps a bit terse and require some
interpretation.  Perhaps not really the best policy for exercises.

-- 
Thomas A. Russ,  USC/Information Sciences Institute
From: Karstens Rage
Subject: Re: Structure interdependent slots? (newbie)
Date: 
Message-ID: <X9idnb4vr-VtW63eRVn-hg@comcast.com>
Thomas A. Russ wrote:
>>"Create a defstruct for CAPTAIN with fields NAME, AGE, and SHIP.
>>Make a structure describing James T. Kirk, captain of the Enterprise,
>>age 35.
> 
>                                              ^^^^^^^^^^^^^^^^^^^^^^^^^
> 
> OK.  I think here is where it is specified that you make the Kirk
> captain structure point to the enterprise.
> 
> 

I see the light. I misinterpreted the instructions (I do that a lot when
it's late and I'm tired). I misinterpreted the "captain of the
Enterprise" as something relating to text only and not the way to
initialize the structure. The confusing sentence could have come right
after that "Make a structure.." sentence and then I might have caught
it. But the way it was written it seemed to indicate that after making
the Enterprise point back to Kirk, somehow modified Kirk and confusion
ensued.

>> Make the Enterprise point back to Kirk through its CAPTAIN
>>component. Notice that when you print Kirk, you see his ship as well.
>>Now define a print function for CAPTAIN that displays only the name,
>>such as #<CAPTAIN "James T. Kirk">."
> 
> 
> Hmmm.  This actually raises a different point.  If you use the default
> structure printer, which prints all of the slots, it looks like you
> would end up with printing problems unless *PRINT-CIRCLE* were set to T,
> since the Kirk's ship slot would point to Enterprise, whose captain's
> slot would point to Kirk, etc.
> 
> 
I never got to this point so no problem. But as the subsequent poster
pointed out; I have learned A LOT. This is an amazing group.

>>The sentence that confused me was "Notice that when you print Kirk, you
>>see his ship as well."
> 
> 
> Well, the reason for that, is that the printer for structures shows all
> slot values, so the value for the ship slot will print the starship
> structure as well, with the potential to cause circular printing problems.
> 
> 
>>I didnt see how that would happen automagically. I understand fully (and
>>thank you for all the huge amounts of code) that the associations
>>between slots can be made explicitly but I still don't understand the
>>confusing statement. I have found what I believe to be other errors in
>>the book so maybe its just another error.
> 
> 
> I think that the instructions are perhaps a bit terse and require some
> interpretation.  Perhaps not really the best policy for exercises.
> 

I have been frustrated with this book but I mostly attribute it to my
impatience. I am very inspired by LISP but am having a difficult time
with it. I continue on my journey....

k
From: ······@earthlink.net
Subject: Re: Structure interdependent slots? (newbie)
Date: 
Message-ID: <1127259401.154738.301440@o13g2000cwo.googlegroups.com>
Karstens Rage wrote:
> "Create a defstruct for CAPTAIN with fields NAME, AGE, and SHIP.
> Make a structure describing James T. Kirk, captain of the Enterprise,
> age 35. Make the Enterprise point back to Kirk through its CAPTAIN
> component. Notice that when you print Kirk, you see his ship as well.
> Now define a print function for CAPTAIN that displays only the name,
> such as #<CAPTAIN "James T. Kirk">."
>
> The sentence that confused me was "Notice that when you print Kirk, you
> see his ship as well."

Did you do it?  If you did, you'd have seen kirk's ship's structure
when
you printed kirk's structure because ks has a pointer to said ss. And,
if
you'd printed said ship's structure, you'd have seen kirk's structure
for
a similar's reason.

Note that there's nothing "lispy" going on here - you'd have seen the
same thing in any other language, assuming that it had a print function
that worked.  (Note that there's a circular reference here, so a dumb
print function will loop.)
From: Joe Marshall
Subject: Re: Structure interdependent slots? (newbie)
Date: 
Message-ID: <slvzgvug.fsf@alum.mit.edu>
Karstens Rage <········@rage.com> writes:

> In a follow up message, to Pascal, I clarified why I was asking. I was
> thinking along the lines that structure slots are totally independent
> and there is no way for the interpreter to make arbitrary associations
> between slots. The responses so far have been way over the top in terms
> of answering what I thought was a simple misunderstanding of something I
> read in a book.

It wasn't obvious whether you were asking a simple question about the
interpreter or a very complex question about ontological relationships.

> I quote the entire passage from Exercise 12.5 on page 379 of Common
> Lisp: A Gentle Introduction to Symbolic Computation by David Touretzky.
>
> "Create a defstruct for CAPTAIN with fields NAME, AGE, and SHIP.

(defstruct starship
  (captain nil)
  (name nil)
  (shields 'down)
  (condition 'green)
  (speed 0))

(defstruct captain
  (name nil)
  (age 0)
  (starship nil))

(setf enterprise (make-starship :name "Enterprise"))

and

(setf kirk (make-captain :name "James T. Kirk" :age 35))


> Make a structure describing James T. Kirk, captain of the Enterprise,
> age 35. 

You didn't supply the ship field.  He's James T. Kirk, age 35, captain
of nothing.

(defvar *enterprise* (make-starship :name "Enterprise"))

(defvar *kirk* (make-captain :name "James T. Kirk" :ship *enterprise* :age 35))
From: Rob Warnock
Subject: Re: Structure interdependent slots? (newbie)
Date: 
Message-ID: <3tCdncw7MMOtcq3eRVn-uQ@speakeasy.net>
Joe Marshall  <·········@alum.mit.edu> wrote:
+---------------
| > Make a structure describing James T. Kirk, captain of the Enterprise,
| > age 35. 
| 
| You didn't supply the ship field.  He's James T. Kirk, age 35,
| captain of nothing.
| 
| (defvar *enterprise* (make-starship :name "Enterprise"))
| (defvar *kirk* (make-captain :name "James T. Kirk"
|                              :ship *enterprise* :age 35))
+---------------

Uh...                          :STARSHIP, yes?

That said, then:

    > *kirk*
    #S(CAPTAIN
	 :NAME "James T. Kirk"
	 :AGE 35
	 :STARSHIP #S(STARSHIP
			:CAPTAIN NIL
			:NAME "Enterprise"
			:SHIELDS DOWN
			:CONDITION GREEN
			:SPEED 0))
    > 


-Rob

-----
Rob Warnock			<····@rpw3.org>
627 26th Avenue			<URL:http://rpw3.org/>
San Mateo, CA 94403		(650)572-2607
From: Kenny Tilton
Subject: Re: Structure interdependent slots? (newbie)
Date: 
Message-ID: <Zu0Ye.13458$x43.2830601@twister.nyc.rr.com>
Karstens Rage wrote:
> Kenny Tilton wrote:
> 
>>Indeed, but to make things worse the OP said they were /not/ looking for
>>an automatic solution. They seem to be asking if their database should
>>be consistent, given that it is redundant. Duh. I think we should be
>>asking them what the hell they are thinking, not trying to help them.
>>And if it turns out they do want an automatic solution, it would be
>>valuable to know where they have seen such a thing before.
>>
> 
> In a follow up message, to Pascal, I clarified why I was asking. I was
> thinking along the lines that structure slots are totally independent
> and there is no way for the interpreter to make arbitrary associations
> between slots. The responses so far have been way over the top in terms
> of answering what I thought was a simple misunderstanding of something I
> read in a book.
> 
> I quote the entire passage from Exercise 12.5 on page 379 of Common
> Lisp: A Gentle Introduction to Symbolic Computation by David Touretzky.
> 
> "Create a defstruct for CAPTAIN with fields NAME, AGE, and SHIP.
> Make a structure describing James T. Kirk, captain of the Enterprise,
> age 35. Make the Enterprise point back to Kirk through its CAPTAIN
> component. Notice that when you print Kirk, you see his ship as well.
> Now define a print function for CAPTAIN that displays only the name,
> such as #<CAPTAIN "James T. Kirk">."
> 
> The sentence that confused me was "Notice that when you print Kirk, you
> see his ship as well."

I think he is talking about how the print statement shows you the slots 
as well, not about slots getting set up automagically by some Prologian 
wizardry, which I realize you could well have been anticipating, having 
heard that Lisp is Unbearably Awesomeness of its Powerf.

> 
> I didnt see how that would happen automagically. I understand fully (and
> thank you for all the huge amounts of code) that the associations
> between slots can be made explicitly but I still don't understand the
> confusing statement. I have found what I believe to be other errors in
> the book so maybe its just another error.
> 
> 
> 
>>I smell a troll.
>>
> 
> Take a bath?

Trolls are always quick to take offense. And make wild leaps from "the 
print statement does this" to "black helicopters managing slot values" 
and or a textual erratum.

The Troll SWAT Team is moving to standby. YHIHF.

-- 
Kenny

Why Lisp? http://wiki.alu.org/RtL_Highlight_Film

"I've wrestled with reality for 35 years, Doctor, and I'm happy to state 
I finally won out over it."
     Elwood P. Dowd, "Harvey", 1950
From: GP lisper
Subject: Re: Structure interdependent slots? (newbie)
Date: 
Message-ID: <1127264582.a2ec6464232b893592cb6cde91eb4b45@teranews>
On Tue, 20 Sep 2005 18:32:56 GMT, <·······@nyc.rr.com> wrote:
>
> I smell a troll.

Well, I learned a great deal in the many replies, so maybe the SWAT
team can stand down this round.  More of a pedagogical query.


-- 
In Common Lisp quite a few aspects of pathname semantics are left to
the implementation.
From: Kenny Tilton
Subject: Re: Structure interdependent slots? (newbie)
Date: 
Message-ID: <re4Ye.13485$x43.2845825@twister.nyc.rr.com>
GP lisper wrote:

> On Tue, 20 Sep 2005 18:32:56 GMT, <·······@nyc.rr.com> wrote:
> 
>>I smell a troll.
> 
> 
> Well, I learned a great deal in the many replies, so maybe the SWAT
> team can stand down this round.

I was not concerned about the /replies/. :)

-- 
Kenny

Why Lisp? http://wiki.alu.org/RtL_Highlight_Film

"I've wrestled with reality for 35 years, Doctor, and I'm happy to state 
I finally won out over it."
     Elwood P. Dowd, "Harvey", 1950