1
0
Fork 0
cl-sites/www.cs.cmu.edu/Groups/AI/html/cltl/clm/node276.html
2023-10-25 11:23:21 +02:00

142 lines
7.4 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3O//DTD W3 HTML 2.0//EN">
<!Converted with LaTeX2HTML 0.6.5 (Tue Nov 15 1994) by Nikos Drakos (nikos@cbl.leeds.ac.uk), CBLU, University of Leeds >
<HEAD>
<TITLE>28.1.5.2. Examples</TITLE>
</HEAD>
<BODY>
<meta name="description" value=" Examples">
<meta name="keywords" value="clm">
<meta name="resource-type" value="document">
<meta name="distribution" value="global">
<P>
<b>Common Lisp the Language, 2nd Edition</b>
<BR> <HR><A NAME=tex2html5106 HREF="node277.html"><IMG ALIGN=BOTTOM ALT="next" SRC="icons/next_motif.gif"></A> <A NAME=tex2html5104 HREF="node274.html"><IMG ALIGN=BOTTOM ALT="up" SRC="icons/up_motif.gif"></A> <A NAME=tex2html5100 HREF="node275.html"><IMG ALIGN=BOTTOM ALT="previous" SRC="icons/previous_motif.gif"></A> <A NAME=tex2html5108 HREF="node1.html"><IMG ALIGN=BOTTOM ALT="contents" SRC="icons/contents_motif.gif"></A> <A NAME=tex2html5109 HREF="index.html"><IMG ALIGN=BOTTOM ALT="index" SRC="icons/index_motif.gif"></A> <BR>
<B> Next:</B> <A NAME=tex2html5107 HREF="node277.html"> Generic Functions and </A>
<B>Up:</B> <A NAME=tex2html5105 HREF="node274.html"> Determining the Class </A>
<B> Previous:</B> <A NAME=tex2html5101 HREF="node275.html"> Topological Sorting</A>
<HR> <P>
<H3><A NAME=SECTION003215200000000000000>28.1.5.2. Examples</A></H3>
<P>
<img align=bottom alt="change_begin" src="gif/change_begin.gif"><br>
This example determines a class precedence list for the
class <tt>pie</tt>. The following classes are defined:
<P>
<P><pre>
(defclass pie (apple cinnamon) ())
(defclass apple (fruit) ())
(defclass cinnamon (spice) ())
(defclass fruit (food) ())
(defclass spice (food) ())
(defclass food () ())
</pre><P>
The set <i>S</i> = <tt>{pie, apple, cinnamon, fruit, spice, food, standard-object, t}</tt>.
The set <i>R</i> = <tt>{(pie, apple), (apple, cinnamon), (cinnamon,
standard-object), (apple, fruit), (fruit, standard-object), (cinnamon,
spice), (spice, standard-object), (fruit, food), (food,
standard-object), (spice, food), (standard-object, t)}</tt>
<P>
[The original CLOS specification [<A HREF="node368.html#SIGPLANCLOS">5</A>,<A HREF="node368.html#LASCCLOSPART1">6</A>]
contained a minor error in this example: the pairs
<tt>(cinnamon, standard-object)</tt>,
<tt>(fruit, standard-object)</tt>, and
<tt>(spice, standard-object)</tt>
were inadvertently omitted from <i>R</i> in the preceding paragraph.
It is important to understand that <tt>defclass</tt> implicitly appends the
class <tt>standard-object</tt> to the list of superclasses when the metaclass
is <tt>standard-class</tt> (the normal situation),
in order to insure that <tt>standard-object</tt> will be a superclass
of every instance of <tt>standard-class</tt> except <tt>standard-object</tt> itself
(see section <A HREF="node263.html#ClassesSECTION">28.1.2</A>).
<IMG ALIGN=BOTTOM ALT="" SRC="_24769_tex2html_wrap43499.gif"> is then generated from this augmented list of superclasses;
this is where the extra pairs come from. I have corrected the example
by adding these pairs as appropriate throughout the example. The final result,
the class precedence list for <tt>pie</tt>,
is unchanged.-GLS]
<P>
The class <tt>pie</tt> is not preceded by anything, so it comes first;
the result so far is <tt>(pie)</tt>. Remove <tt>pie</tt> from <i>S</i> and pairs
mentioning <tt>pie</tt> from <i>R</i> to get
<br><i>S</i> = <tt>{apple, cinnamon, fruit, spice, food, standard-object, t}</tt>
and <i>R</i> = <tt>{(apple, cinnamon), (cinnamon, standard-object),
(apple, fruit), (fruit, standard-object), (cinnamon, spice), (spice,
standard-object), (fruit, food), (food, standard-object), (spice,
food), (standard-object, t)}</tt>.
<P>
The class <tt>apple</tt> is not preceded by anything, so it is next; the
result is <tt>(pie apple)</tt>. Removing <tt>apple</tt> and the relevant
pairs results in
<br><i>S</i> = <tt>{cinnamon, fruit, spice, food, standard-object, t}</tt>
and <i>R</i> = <tt>{(cinnamon, standard-object), (fruit,
standard-object), (cinnamon, spice), (spice, standard-object), (fruit,
food), (food, standard-object), (spice, food), (standard-object, t)}</tt>.
<P>
The classes <tt>cinnamon</tt> and <tt>fruit</tt> are not preceded by
anything, so the one with a direct subclass rightmost in the class
precedence list computed so far goes next. The class <tt>apple</tt> is a
direct subclass of <tt>fruit</tt>, and the class <tt>pie</tt> is a direct
subclass of <tt>cinnamon</tt>. Because <tt>apple</tt> appears to the right
of <tt>pie</tt> in the precedence list, <tt>fruit</tt> goes next, and the
result so far is <tt>(pie apple fruit)</tt>.
<br><i>S</i> = <tt>{cinnamon, spice, food, standard-object, t}</tt>
and <i>R</i> = <tt>{(cinnamon, standard-object), (cinnamon, spice), (spice, standard-object), (food, standard-object), (spice, food), (standard-object, t)}</tt>.
<P>
The class <tt>cinnamon</tt> is next, giving the result so far as
<tt>(pie apple fruit cinnamon)</tt>. At this point
<br><i>S</i> = <tt>{spice, food, standard-object, t}</tt>
and <i>R</i> = <tt>{(spice, standard-object), (food, standard-object), (spice, food), (standard-object, t)}</tt>.
<P>
The classes <tt>spice</tt>, <tt>food</tt>, <tt>standard-object</tt>, and
<tt>t</tt> are then added in that order, and the final class precedence list for <tt>pie</tt> is
<P><pre>
(pie apple fruit cinnamon spice food standard-object t)
</pre><P>
<P>
It is possible to write a set of class definitions that cannot be
ordered. For example:
<P>
<P><pre>
(defclass new-class (fruit apple) ())
(defclass apple (fruit) ())
</pre><P>
<P>
The class <tt>fruit</tt> must precede <tt>apple</tt> because the local
ordering of superclasses must be preserved. The class <tt>apple</tt> must
precede <tt>fruit</tt> because a class always precedes its own
superclasses. When this situation occurs, an error is signaled when
the system tries to compute the class precedence list.
<P>
The following might appear to be a conflicting set of definitions:
<P>
<P><pre>
(defclass pie (apple cinnamon) ())
(defclass pastry (cinnamon apple) ())
(defclass apple () ())
(defclass cinnamon () ())
</pre><P>
<P>
The class precedence list for <tt>pie</tt> is
<P><pre>
(pie apple cinnamon standard-object t)
</pre><P>
<P>
The class precedence list for <tt>pastry</tt> is
<P><pre>
(pastry cinnamon apple standard-object t)
</pre><P>
<P>
It is not a problem for <tt>apple</tt> to precede <tt>cinnamon</tt> in the
ordering of the superclasses of <tt>pie</tt> but not in the ordering for
<tt>pastry</tt>. However, it is not possible to build a new class that
has both <tt>pie</tt> and <tt>pastry</tt> as superclasses.
<br><img align=bottom alt="change_end" src="gif/change_end.gif">
<P>
<BR> <HR><A NAME=tex2html5106 HREF="node277.html"><IMG ALIGN=BOTTOM ALT="next" SRC="icons/next_motif.gif"></A> <A NAME=tex2html5104 HREF="node274.html"><IMG ALIGN=BOTTOM ALT="up" SRC="icons/up_motif.gif"></A> <A NAME=tex2html5100 HREF="node275.html"><IMG ALIGN=BOTTOM ALT="previous" SRC="icons/previous_motif.gif"></A> <A NAME=tex2html5108 HREF="node1.html"><IMG ALIGN=BOTTOM ALT="contents" SRC="icons/contents_motif.gif"></A> <A NAME=tex2html5109 HREF="index.html"><IMG ALIGN=BOTTOM ALT="index" SRC="icons/index_motif.gif"></A> <BR>
<B> Next:</B> <A NAME=tex2html5107 HREF="node277.html"> Generic Functions and </A>
<B>Up:</B> <A NAME=tex2html5105 HREF="node274.html"> Determining the Class </A>
<B> Previous:</B> <A NAME=tex2html5101 HREF="node275.html"> Topological Sorting</A>
<HR> <P>
<HR>
<P><ADDRESS>
AI.Repository@cs.cmu.edu
</ADDRESS>
</BODY>