concept-classes.tex 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920
  1. % -*- Mode: TeX -*-
  2. %% Classes
  3. While the \CLOS\ is general enough to describe all \term{standardized} \term{classes}
  4. (including, for example, \typeref{number}, \typeref{hash-table}, and
  5. \typeref{symbol}), \thenextfigure\ contains a list of \term{classes} that are
  6. especially relevant to understanding the \CLOS.
  7. \DefineFigure{ObjectSystemClasses}
  8. \displaythree{Object System Classes}{
  9. built-in-class&method-combination&standard-object\cr
  10. class&standard-class&structure-class\cr
  11. generic-function&standard-generic-function&structure-object\cr
  12. method&standard-method&\cr
  13. }
  14. \beginsubSection{Introduction to Classes}
  15. A \newterm{class} is an \term{object} that determines the structure and behavior
  16. of a set of other \term{objects}, which are called its \newtermidx{instances}{instance}.
  17. A \term{class} can inherit structure and behavior from other \term{classes}.
  18. A \term{class} whose definition refers to other \term{classes} for the purpose
  19. of inheriting from them is said to be a \term{subclass} of each of
  20. those \term{classes}. The \term{classes} that are designated for purposes of
  21. inheritance are said to be \term{superclasses} of the inheriting \term{class}.
  22. A \term{class} can have a \term{name}. The \term{function} \funref{class-name}
  23. takes a \term{class} \term{object} and returns its \term{name}.
  24. The \term{name} of an anonymous \term{class} is \nil. A \term{symbol}
  25. can \term{name} a \term{class}. The \term{function} \funref{find-class} takes a
  26. \term{symbol} and returns the \term{class} that the \term{symbol} names.
  27. A \term{class} has a \term{proper name} if the \term{name} is a \term{symbol}
  28. and if the \term{name} of the \term{class} names that \term{class}.
  29. That is, a \term{class}~$C$ has the \term{proper name}~$S$ if $S=$
  30. \f{(class-name $C$)} and $C=$ \f{(find-class $S$)}.
  31. Notice that it is possible for
  32. \f{(find-class $S\sub 1$)} $=$ \f{(find-class $S\sub 2$)}
  33. and $S\sub 1\neq S\sub 2$.
  34. If $C=$ \f{(find-class $S$)}, we say that $C$ is the \term{class} \term{named} $S$.
  35. A \term{class} $C\sub{1}$ is
  36. a \newterm{direct superclass} of a \term{class} $C\sub{2}$
  37. if $C\sub{2}$ explicitly designates $C\sub{1}$
  38. as a \term{superclass} in its definition.
  39. In this case $C\sub{2}$ is a \newterm{direct subclass} of $C\sub{1}$.
  40. A \term{class} $C\sub{n}$ is a \newterm{superclass} of
  41. a \term{class} $C\sub{1}$ if there exists a series of
  42. \term{classes} $C\sub{2},\ldots,C\sub{n-1}$ such that
  43. $C\sub{i+1}$ is a \term{direct superclass} of $C\sub{i}$ for $1 \leq i<n$.
  44. In this case, $C\sub{1}$ is a \newterm{subclass} of $C\sub{n}$.
  45. A \term{class} is considered neither a \term{superclass} nor a \term{subclass} of itself.
  46. That is, if $C\sub{1}$ is a \term{superclass} of $C\sub{2}$,
  47. then $C\sub{1} \neq C\sub{2}$.
  48. The set of \term{classes} consisting of some given \term{class} $C$
  49. along with all of its \term{superclasses} is called ``$C$ and its superclasses.''
  50. Each \term{class} has a \newterm{class precedence list},
  51. which is a total ordering on the set of the given \term{class} and its \term{superclasses}.
  52. The total ordering is expressed as a list ordered from most specific to least specific.
  53. The \term{class precedence list} is used in several ways. In general, more
  54. specific \term{classes} can \newterm{shadow}\meaning{1} features that would
  55. otherwise be inherited from less specific \term{classes}.
  56. The \term{method} selection and combination process uses
  57. the \term{class precedence list} to order \term{methods}
  58. from most specific to least specific.
  59. When a \term{class} is defined, the order in which its direct \term{superclasses}
  60. are mentioned in the defining form is important. Each \term{class} has a
  61. \newterm{local precedence order}, which is a \term{list} consisting of the
  62. \term{class} followed by its \term{direct superclasses} in the order mentioned
  63. in the defining \term{form}.
  64. A \term{class precedence list} is always consistent with the
  65. \term{local precedence order} of each \term{class} in the list.
  66. The \term{classes} in each \term{local precedence order} appear
  67. within the \term{class precedence list} in the same order.
  68. If the \term{local precedence orders} are inconsistent with each other,
  69. no \term{class precedence list} can be constructed, and an error is signaled.
  70. The \term{class precedence list} and its computation is discussed
  71. in \secref\DeterminingtheCPL.
  72. \term{classes} are organized into a directed acyclic graph.
  73. There are two distinguished \term{classes}, named \typeref{t} and \typeref{standard-object}.
  74. The \term{class} named \typeref{t} has no \term{superclasses}.
  75. It is a \term{superclass} of every \term{class} except itself.
  76. The \term{class} named \typeref{standard-object} is an \term{instance} of
  77. \theclass{standard-class} and is a \term{superclass} of
  78. every \term{class} that is an \term{instance} of \theclass{standard-class} except itself.
  79. \reviewer{Barmar: This or something like it needs to be said in the introduction.}%!!!
  80. There is a mapping from the object system \term{class} space into
  81. the \term{type} space. Many of the standard \term{types} specified
  82. in this document have a corresponding \term{class} that has the same
  83. \term{name} as the \term{type}. Some \term{types} do not have a
  84. corresponding \term{class}. The integration of the \term{type} and \term{class}
  85. systems is discussed in \secref\IntegratingTypesAndClasses.
  86. \term{Classes} are represented by \term{objects} that are themselves
  87. \term{instances} of \term{classes}.
  88. The \term{class} of the \term{class} of an \term{object} is termed
  89. the \newterm{metaclass} of that \term{object}. When no misinterpretation is
  90. possible, the term \term{metaclass} is used to refer to a \term{class}
  91. that has \term{instances} that are themselves \term{classes}. The \term{metaclass}
  92. determines the form of inheritance used by the \term{classes} that are its
  93. \term{instances} and the representation of the \term{instances} of those \term{classes}.
  94. The \CLOS\ provides a default \term{metaclass}, \typeref{standard-class}, that is
  95. appropriate for most programs.
  96. %The meta-object protocol provides
  97. %mechanisms for defining and using new metaclasses.
  98. Except where otherwise specified, all \term{classes} mentioned in this
  99. standard are \term{instances} of \theclass{standard-class},
  100. all \term{generic functions} are \term{instances}
  101. of \theclass{standard-generic-function},
  102. and all \term{methods} are \term{instances} of \theclass{standard-method}.
  103. \endsubSection%{Classes}
  104. %\beginsubsubsection{Metaclasses}
  105. %??? Is the following paragraph necessary in this specification???
  106. %
  107. %The \newterm{metaclass} of an object is the class of its class. The
  108. %metaclass determines the representation of instances of its instances and
  109. %the forms of inheritance used by its instances for slot descriptions and
  110. %method inheritance. The metaclass mechanism can be used to provide
  111. %particular forms of optimization or to tailor the \CLOS\ for particular
  112. %uses.
  113. %The protocol for defining metaclasses is discussed in the chapter
  114. %``The \CLOS\ Meta-Object Protocol.''
  115. %\endsubsubsection%{Metaclasses}
  116. \beginsubsubsection{Standard Metaclasses}
  117. The \CLOS\ provides a number of predefined \term{metaclasses}.
  118. These include the \term{classes} \typeref{standard-class},
  119. \typeref{built-in-class}, and \typeref{structure-class}:
  120. \beginlist
  121. \itemitem{\bull}
  122. \Theclass{standard-class} is the default \term{class} of
  123. \term{classes} defined by \macref{defclass}.
  124. \itemitem{\bull} \Theclass{built-in-class} is the \term{class} whose
  125. \term{instances} are \term{classes} that have special implementations with
  126. restricted capabilities. Any \term{class} that corresponds to a standard
  127. \term{type} might be an \term{instance} of \typeref{built-in-class}.
  128. The predefined \term{type} specifiers that are required to have
  129. corresponding \term{classes} are listed in \figref\ClassTypeCorrespondence.
  130. It is \term{implementation-dependent} whether each of these \term{classes}
  131. is implemented as a \term{built-in class}.
  132. \itemitem{\bull}
  133. All \term{classes} defined by means of \macref{defstruct} are
  134. \term{instances} of \theclass{structure-class}.
  135. \endlist
  136. \endsubsubsection%{Standard Metaclasses}
  137. \beginsubSection{Defining Classes}
  138. The macro \macref{defclass} is used to define a new named \term{class}.
  139. %The syntax for \macref{defclass} is given in Figure ??
  140. The definition of a \term{class} includes:
  141. \beginlist
  142. \itemitem{\bull} The \term{name} of the new \term{class}.
  143. For newly-defined \term{classes} this \term{name} is a \term{proper name}.
  144. \itemitem{\bull} The list of the direct \term{superclasses} of the new \term{class}.
  145. \itemitem{\bull} A set of \newtermidx{slot specifiers}{slot specifier}.
  146. Each \term{slot specifier} includes the \term{name} of the \term{slot}
  147. and zero or more \term{slot} options. A \term{slot} option pertains
  148. only to a single \term{slot}. If a \term{class} definition contains
  149. two \term{slot specifiers} with the same \term{name}, an error is signaled.
  150. \itemitem{\bull} A set of \term{class} options.
  151. Each \term{class} option pertains to the \term{class} as a whole.
  152. \endlist
  153. The \term{slot} options and \term{class} options of
  154. the \macref{defclass} form provide mechanisms for the following:
  155. \beginlist
  156. \itemitem{\bull} Supplying a default initial value \term{form}
  157. for a given \term{slot}.
  158. \itemitem{\bull} Requesting that \term{methods} for \term{generic functions}
  159. be automatically generated for reading or writing \term{slots}.
  160. \itemitem{\bull} Controlling whether a given \term{slot} is shared by
  161. all \term{instances}
  162. of the \term{class} or whether each
  163. \term{instance} of the \term{class} has its own \term{slot}.
  164. \itemitem{\bull} Supplying a set of initialization arguments and initialization
  165. argument defaults to be used in \term{instance} creation.
  166. %\itemitem{\bull} Requesting that a constructor function be automatically
  167. %generated for making instances of the new class.
  168. \itemitem{\bull} Indicating that the \term{metaclass} is to be other
  169. than the default. The \kwd{metaclass} option is reserved for future use;
  170. an implementation can be extended to make use of the \kwd{metaclass}
  171. option.
  172. \itemitem{\bull} Indicating the expected \term{type} for the value stored
  173. in the \term{slot}.
  174. \itemitem{\bull} Indicating the \term{documentation string} for the \term{slot}.
  175. \endlist
  176. \endsubSection%{Defining Classes}
  177. \goodbreak
  178. \beginsubSection{Creating Instances of Classes}
  179. The generic function \funref{make-instance} creates and returns a new
  180. \term{instance} of a \term{class}.
  181. The \OS\ provides several mechanisms for
  182. specifying how a new \term{instance} is to be initialized. For example, it
  183. is possible to specify the initial values for \term{slots} in newly created
  184. \term{instances}
  185. either by giving arguments to \funref{make-instance} or by
  186. providing default initial values. Further initialization activities
  187. can be performed by \term{methods} written for \term{generic functions}
  188. that are
  189. part of the initialization protocol. The complete initialization
  190. protocol is described in \secref\ObjectCreationAndInit.
  191. \endsubSection%{Creating Instances of Classes}
  192. \beginsubSection{Inheritance}
  193. \DefineSection{Inheritance}
  194. A \term{class} can inherit \term{methods}, \term{slots},
  195. and some \macref{defclass} options from its \term{superclasses}.
  196. Other sections describe the inheritance of \term{methods},
  197. the inheritance of \term{slots} and \term{slot} options,
  198. and the inheritance of \term{class} options.
  199. \beginsubsubsection{Examples of Inheritance}
  200. \code
  201. (defclass C1 ()
  202. ((S1 :initform 5.4 :type number)
  203. (S2 :allocation :class)))
  204. (defclass C2 (C1)
  205. ((S1 :initform 5 :type integer)
  206. (S2 :allocation :instance)
  207. (S3 :accessor C2-S3)))
  208. \endcode
  209. \term{Instances} of the class \f{C1} have a \term{local slot} named \f{S1},
  210. whose default initial value is 5.4 and
  211. whose \term{value} should always be a \term{number}.
  212. The class \f{C1} also has a \term{shared slot} named \f{S2}.
  213. There is a \term{local slot} named \f{S1} in \term{instances} of \f{C2}.
  214. The default initial value of \f{S1} is 5.
  215. The value of \f{S1} should always be of type \f{(and integer number)}.
  216. There are also \term{local slots} named \f{S2} and \f{S3} in \term{instances} of \f{C2}.
  217. The class \f{C2} has a \term{method} for \f{C2-S3} for reading the value of slot \f{S3};
  218. there is also a \term{method} for \f{(setf C2-S3)} that writes the value of \f{S3}.
  219. \endsubsubsection%{Examples of Inheritance}
  220. \beginsubsubsection{Inheritance of Class Options}
  221. The \kwd{default-initargs} class option is inherited. The set of
  222. defaulted initialization arguments for a \term{class} is the union of the
  223. sets of initialization arguments supplied in
  224. the \kwd{default-initargs} class options of the \term{class} and its \term{superclasses}.
  225. When more than one default initial value \term{form} is supplied for a given
  226. initialization argument, the default initial value \term{form} that is used
  227. is the one supplied by the \term{class} that is most specific according to
  228. the \term{class precedence list}.
  229. If a given \kwd{default-initargs} class option specifies an
  230. initialization argument of the same \term{name} more than once, an
  231. error \oftype{program-error} is signaled.
  232. \endsubsubsection%{Inheritance of Class Options}
  233. \endsubSection%{Inheritance}
  234. \beginsubSection{Determining the Class Precedence List}
  235. \DefineSection{DeterminingtheCPL}
  236. The \macref{defclass} form for a \term{class} provides a total ordering
  237. on that \term{class} and its direct \term{superclasses}. This ordering is
  238. called the \newterm{local precedence order}. It is an ordered list of the
  239. \term{class} and its direct \term{superclasses}. The
  240. \newterm{class precedence list} for a class $C$ is a total ordering on
  241. $C$ and its \term{superclasses} that is consistent with the
  242. \term{local precedence orders} for each of $C$ and its \term{superclasses}.
  243. A \term{class} precedes its direct \term{superclasses},
  244. and a direct \term{superclass} precedes all other
  245. direct \term{superclasses} specified to its right
  246. in the \term{superclasses} list of the \macref{defclass} form.
  247. For every class $C$, define $$R\sub C=\{(C,C\sub 1),(C\sub 1,C\sub
  248. 2),\ldots,(C\sub {n-1},C\sub n)\}$$ where $C\sub 1,\ldots,C\sub n$ are
  249. the direct \term{superclasses} of $C$ in the order in which
  250. they are mentioned in the \macref{defclass} form. These ordered pairs
  251. generate the total ordering on the class $C$ and its direct
  252. \term{superclasses}.
  253. Let $S\sub C$ be the set of $C$ and its \term{superclasses}. Let $R$ be
  254. $$R=\bigcup\sub{c\in {S\sub C}}R\sub c$$.
  255. \reviewer{Barmar: ``Consistent'' needs to be defined, or maybe we should say
  256. ``logically consistent''?}%!!!
  257. The set $R$ might or might not generate a partial ordering, depending on
  258. whether the $R\sub c$, $c\in S\sub C$, are
  259. consistent; it is assumed
  260. that they are consistent and that $R$ generates a partial ordering.
  261. When the $R\sub c$ are not consistent, it is said that $R$ is inconsistent.
  262. %This partial ordering is generated by taking the the transitive
  263. %closure of the set $R\cup \{(c,c) \vert c\in {S\sub C}\}$. When
  264. %$(C\sub 1,C\sub 2)\in R$\negthinspace, it is said that $C\sub 1$
  265. %{\bit precedes or equals} $C\sub 2$. Intuitively, $C\sub 1$ precedes
  266. %or equals $C\sub 2$ when $C\sub 1=C\sub 2$ or $C\sub 2$ is a
  267. %superclass of $C\sub 1$.
  268. %
  269. %Recall that a partial ordering of the set $S$ is a relation between
  270. %objects of $S$ that is transitive, reflexive, and antisymmetric. The
  271. %set $\{(c,c) \vert c\in {S\subC}\}$ was added to the transitive
  272. %closure of the set $R$ in order to make the relation reflexive. In
  273. %the remainder of this section the set of precedence relations $R$ and
  274. %not the partial ordering will be used.
  275. To compute the \term{class precedence list} for~$C$\negthinspace,
  276. topologically sort the elements of $S\sub C$ with respect to the
  277. partial ordering generated by $R$\negthinspace. When the topological
  278. sort must select a \term{class} from a set of two or more
  279. \term{classes}, none of
  280. which are preceded by other \term{classes} with respect to~$R$\negthinspace,
  281. the \term{class} selected is chosen deterministically, as described below.
  282. If $R$ is inconsistent, an error is signaled.
  283. \goodbreak
  284. \beginsubsubsection{Topological Sorting}
  285. % !!!
  286. % Barmar: Sometimes $C$ refers to the original class and sometimes it refers
  287. % to an element of $S\sub C$.
  288. % KAB: Really? Where? I don't see any.
  289. %I didn't have time to figure out this comment but maybe someone will another time.
  290. % -kmp 11-Oct-90
  291. Topological sorting proceeds by finding a class $C$ in~$S\sub C$ such
  292. that no other \term{class} precedes that element according to the elements
  293. in~$R$\negthinspace. The class $C$ is placed first in the result.
  294. Remove $C$ from $S\sub C$, and remove all pairs of the form $(C,D)$,
  295. $D\in S\sub C$, from $R$\negthinspace. Repeat the process, adding
  296. \term{classes} with no predecessors to the end of the result. Stop when no
  297. element can be found that has no predecessor.
  298. If $S\sub C$ is not empty and the process has stopped, the set $R$ is
  299. inconsistent. If every \term{class} in the finite set of
  300. \term{classes} is preceded
  301. by another, then $R$ contains a loop. That is, there is a chain of
  302. classes $C\sub 1,\ldots,C\sub n$ such that $C\sub i$ precedes
  303. $C\sub{i+1}$, $1\leq i<n$, and $C\sub n$ precedes $C\sub 1$.
  304. Sometimes there are several \term{classes} from $S\sub C$ with no
  305. predecessors. In this case select the one that has a direct
  306. \term{subclass} rightmost in the \term{class precedence list} computed so far.
  307. %Because a direct superclass precedes all other direct superclasses to
  308. %its right, there can be only one such candidate class.
  309. %%Barmar thinks the next sentence is redundant with previous paragraph,
  310. %but I think it's useful for emphasis. I put it in parens to deemphasize it. -kmp 11-Oct-90
  311. (If there is no such candidate \term{class}, $R$ does not generate
  312. a partial ordering---the $R\sub c$, $c\in S\sub C$, are inconsistent.)
  313. In more precise terms, let $\{N\sub 1,\ldots,N\sub m\}$, $m\geq 2$, be
  314. the \term{classes} from $S\sub C$ with no predecessors. Let $(C\sub
  315. 1\ldots C\sub n)$, $n\geq 1$, be the \term{class precedence list}
  316. constructed so far. $C\sub 1$ is the most specific \term{class}, and $C\sub
  317. n$ is the least specific. Let $1\leq j\leq n$ be the largest number
  318. such that there exists an $i$ where $1\leq i\leq m$ and $N\sub i$
  319. is a direct \term{superclass} of $C\sub j$; $N\sub i$ is placed next.
  320. % The effect of this rule for selecting from a set of \term{classes} with no
  321. % predecessors is that the \term{classes} in a simple \term{superclass} chain are
  322. % adjacent in the \term{class precedence list} and that \term{classes} in each
  323. % relatively separated subgraph are adjacent in the \term{class precedence list}.
  324. % For example, let $T\sub 1$ and $T\sub 2$ be subgraphs
  325. % whose only element in common is the class $J$\negthinspace. Suppose
  326. % that no \term{superclass} of $J$ appears in either $T\sub 1$ or $T\sub 2$.
  327. % Let $C\sub 1$ be the bottom of $T\sub 1$; and let $C\sub 2$ be the
  328. % bottom of $T\sub 2$. Suppose $C$ is a \term{class} whose direct \term{superclasses}
  329. % are $C\sub 1$ and $C\sub 2$ in that order, then the
  330. % \term{class precedence list}
  331. % for $C$ will start with $C$ and will be followed by all \term{classes}
  332. % in $T\sub 1$ except $J$. All the \term{classes} of $T\sub 2$ will be next.
  333. % The class $J$ and its \term{superclasses} will appear last.
  334. %
  335. %% Replaced as follows per RPG. -kmp 13-Jan-92
  336. The effect of this rule for selecting from a set of \term{classes} with no
  337. predecessors is that the \term{classes} in a simple \term{superclass} chain are
  338. adjacent in the \term{class precedence list} and that \term{classes} in each
  339. relatively separated subgraph are adjacent in the \term{class precedence list}.
  340. For example, let $T\sub 1$ and $T\sub 2$ be subgraphs whose only
  341. element in common is the class $J$\negthinspace.
  342. % Suppose that no \term{superclass} of $J$ appears in
  343. % either $T\sub 1$ or $T\sub 2$ and that $J$
  344. % is an indirect superclass of every class in both $T\sub 1$ and $T\sub 2$.
  345. %% Rewritten by RPG to avoid use of "indirect superclass". -kmp 13-Jan-92
  346. Suppose that no superclass of $J$ appears in either $T\sub 1$ or $T\sub 2$,
  347. and that $J$ is in the superclass chain of every class in both $T\sub 1$ and $T\sub 2$.
  348. Let $C\sub 1$ be the bottom of $T\sub 1$;
  349. and let $C\sub 2$ be the bottom of $T\sub 2$.
  350. Suppose $C$ is a \term{class} whose direct \term{superclasses}
  351. are $C\sub 1$ and $C\sub 2$ in that order, then the \term{class precedence list}
  352. for $C$ starts with $C$ and is followed by
  353. all \term{classes} in $T\sub 1$ except $J$.
  354. All the \term{classes} of $T\sub 2$ are next.
  355. The \term{class} $J$ and its \term{superclasses} appear last.
  356. \endsubsubsection%{Topological Sorting}
  357. \beginsubsubsection{Examples of Class Precedence List Determination}
  358. This example determines a \term{class precedence list} for the
  359. class \f{pie}. The following \term{classes} are defined:
  360. \code
  361. (defclass pie (apple cinnamon) ())
  362. (defclass apple (fruit) ())
  363. (defclass cinnamon (spice) ())
  364. (defclass fruit (food) ())
  365. (defclass spice (food) ())
  366. (defclass food () ())
  367. \endcode
  368. % $S$ => "$S\sub{pie}$ per Barmar and Laubsch.
  369. The set $S\sub{pie}$~$=$ $\{${\tt pie, apple, cinnamon, fruit, spice, food,
  370. standard-object, t}$\}$. The set $R$~$=$ $\{${\tt (pie, apple),
  371. (apple, cinnamon), (apple, fruit), (cinnamon, spice), \hfil\break
  372. (fruit, food), (spice, food), (food, standard-object), (standard-object,
  373. t)}$\}$.
  374. The class \f{pie} is not preceded by anything, so it comes first;
  375. the result so far is {\tt (pie)}. Remove \f{pie} from $S$ and pairs
  376. mentioning \f{pie} from $R$ to get $S$~$=$ $\{${\tt apple, cinnamon,
  377. fruit, spice, food, standard-object, t}$\}$ and $R$~$=$~$\{${\tt
  378. (apple, cinnamon), (apple, fruit), (cinnamon, spice),\hfil\break (fruit,
  379. food), (spice, food), (food, standard-object),
  380. (standard-object, t)}$\}$.
  381. The class \f{apple} is not preceded by anything, so it is next; the
  382. result is {\tt (pie apple)}. Removing \f{apple} and the relevant
  383. pairs results in $S$~$=$ $\{${\tt cinnamon, fruit, spice, food,
  384. standard-object, t}$\}$ and $R$~$=$ $\{${\tt (cinnamon, spice),
  385. (fruit, food), (spice, food), (food, standard-object),\hfil\break
  386. (standard-object, t)}$\}$.
  387. The classes \f{cinnamon} and {\tt fruit} are not preceded by
  388. anything, so the one with a direct \term{subclass} rightmost in the
  389. \term{class precedence list} computed so far goes next. The class \f{apple} is a
  390. direct \term{subclass} of {\tt fruit}, and the class \f{pie} is a direct
  391. \term{subclass} of \f{cinnamon}. Because \f{apple} appears to the right
  392. of \f{pie} in the \term{class precedence list},
  393. {\tt fruit} goes next, and the
  394. result so far is {\tt (pie apple fruit)}. $S$~$=$ $\{${\tt cinnamon,
  395. spice, food, standard-object, t}$\}$; $R$~$=$ $\{${\tt (cinnamon,
  396. spice), (spice, food),\hfil\break (food, standard-object),
  397. (standard-object, t)}$\}$.
  398. The class \f{cinnamon} is next, giving the result so far as {\tt
  399. (pie apple fruit cinnamon)}. At this point $S$~$=$ $\{${\tt spice,
  400. food, standard-object, t}$\}$; $R$~$=$ $\{${\tt (spice, food), (food,
  401. standard-object), (standard-object, t)}$\}$.
  402. The classes \f{spice}, \f{food}, \typeref{standard-object}, and
  403. \typeref{t} are added in that order, and the \term{class precedence list}
  404. is \f{(pie apple fruit cinnamon spice food standard-object t)}.
  405. It is possible to write a set of \term{class} definitions that cannot be
  406. ordered. For example:
  407. \code
  408. (defclass new-class (fruit apple) ())
  409. (defclass apple (fruit) ())
  410. \endcode
  411. The class \f{fruit} must precede \f{apple}
  412. because the local ordering of \term{superclasses} must be preserved.
  413. The class \f{apple} must precede \f{fruit}
  414. because a \term{class} always precedes its own \term{superclasses}.
  415. When this situation occurs, an error is signaled, as happens here
  416. when the system tries to compute the \term{class precedence list}
  417. %Barmar suggested we add:
  418. of \f{new-class}.
  419. The following might appear to be a conflicting set of definitions:
  420. \code
  421. (defclass pie (apple cinnamon) ())
  422. (defclass pastry (cinnamon apple) ())
  423. (defclass apple () ())
  424. (defclass cinnamon () ())
  425. \endcode
  426. The \term{class precedence list} for \f{pie} is
  427. \f{(pie apple cinnamon standard-object t)}.
  428. The \term{class precedence list} for \f{pastry} is
  429. \f{(pastry cinnamon apple standard-object t)}.
  430. It is not a problem for \f{apple} to precede \f{cinnamon} in the
  431. ordering of the \term{superclasses} of \f{pie} but not in the ordering for
  432. \f{pastry}. However, it is not possible to build a new \term{class} that
  433. has both \f{pie} and \f{pastry} as \term{superclasses}.
  434. \endsubsubsection%{Examples of Class Precedence List Determination}
  435. \endsubSection%{Determining the Class Precedence List}
  436. \beginsubSection{Redefining Classes}
  437. \DefineSection{ClassReDef}
  438. %"instance" => "direct instance" per Barrett,Barmar,Moon.
  439. A \term{class} that is a \term{direct instance} of \typeref{standard-class} can
  440. be redefined if the new \term{class} is also
  441. %"instance" => "direct instance" per Barrett,Barmar,Moon.
  442. a \term{direct instance} of \typeref{standard-class}.
  443. Redefining a \term{class} modifies the existing
  444. \term{class} \term{object} to reflect the new \term{class} definition; it does not
  445. create a new \term{class} \term{object} for the \term{class}.
  446. Any \term{method} \term{object} created by a \kwd{reader}, \kwd{writer},
  447. or \kwd{accessor} option specified by the old \macref{defclass} form is
  448. removed from the corresponding \term{generic function}.
  449. \term{Methods} specified by the new \macref{defclass} form are added.
  450. % any function specified by the \kwd{constructor} option of the
  451. % old \macref{defclass} form is removed from the corresponding symbol function cell.
  452. When the class $C$ is redefined, changes are propagated to its \term{instances}
  453. and to \term{instances} of any of its \term{subclasses}. Updating such an
  454. \term{instance} occurs at an \term{implementation-dependent} time, but no later than
  455. the next time a \term{slot}
  456. of that \term{instance} is read or written. Updating an
  457. \term{instance}
  458. does not change its identity as defined by \thefunction{eq}.
  459. The updating process may change the \term{slots} of that
  460. particular \term{instance},
  461. but it does not create a new \term{instance}. Whether
  462. updating an \term{instance} consumes storage is \term{implementation-dependent}.
  463. Note that redefining a \term{class} may cause \term{slots} to be added or
  464. deleted. If a \term{class} is redefined in a way that changes the set of
  465. \term{local slots} \term{accessible} in \term{instances}, the \term{instances}
  466. are updated. It is \term{implementation-dependent} whether \term{instances}
  467. are updated if a \term{class} is redefined in a way that does not change
  468. the set of \term{local slots} \term{accessible} in \term{instances}.
  469. The value of a \term{slot}
  470. that is specified as shared both in the old \term{class}
  471. and in the new \term{class} is retained.
  472. If such a \term{shared slot} was unbound
  473. in the old \term{class}, it is unbound in the new \term{class}.
  474. \term{Slots} that
  475. were local in the old \term{class} and that are shared in the new
  476. \term{class} are
  477. initialized. Newly added \term{shared slots} are initialized.
  478. Each newly added \term{shared slot} is set to the result of evaluating the
  479. %captured \kwd{initform} form
  480. \term{captured initialization form} for the \term{slot} that was specified
  481. in the \macref{defclass} \term{form} for the new \term{class}.
  482. %If there is no \kwd{initform} form,
  483. If there was no \term{initialization form}, the \term{slot} is unbound.
  484. If a \term{class} is redefined in such a way that the set of
  485. \term{local slots} \term{accessible} in an \term{instance} of the \term{class}
  486. is changed, a two-step process of updating the \term{instances} of the
  487. \term{class} takes place. The process may be explicitly started by
  488. invoking the generic function \funref{make-instances-obsolete}. This
  489. two-step process can happen in other circumstances in some implementations.
  490. For example, in some implementations this two-step process is
  491. triggered if the order of \term{slots} in storage is changed.
  492. The first step modifies the structure of the \term{instance} by adding new
  493. \term{local slots} and discarding \term{local slots} that are not
  494. defined in the new version of the \term{class}. The second step
  495. initializes the newly-added \term{local slots} and performs any other
  496. user-defined actions. These two steps are further specified
  497. in the next two sections.
  498. \beginsubsubsection{Modifying the Structure of Instances}
  499. \reviewer{Barmar: What about shared slots that are deleted?}%!!!
  500. The first step modifies the structure of \term{instances} of the redefined
  501. \term{class} to conform to its new \term{class} definition.
  502. \term{Local slots} specified
  503. by the new \term{class} definition that are not specified as either local or
  504. shared by the old \term{class} are added, and \term{slots}
  505. not specified as either
  506. local or shared by the new \term{class} definition that are specified as
  507. local by the old \term{class} are discarded.
  508. The \term{names} of these added and discarded
  509. \term{slots} are passed as arguments
  510. to \funref{update-instance-for-redefined-class}
  511. as described in the next section.
  512. The values of \term{local slots} specified by both the new and old
  513. \term{classes} are retained. If such a \term{local slot} was unbound,
  514. it remains unbound.
  515. The value of a \term{slot} that is specified as shared in the old
  516. \term{class} and as local in the new \term{class} is retained. If such
  517. a \term{shared slot} was unbound, the \term{local slot} is unbound.
  518. \endsubsubsection%{Modifying the Structure of the Instance}
  519. \beginsubsubsection{Initializing Newly Added Local Slots}
  520. The second step initializes the newly added \term{local slots} and performs
  521. any other user-defined actions. This step is implemented by the generic
  522. function \funref{update-instance-for-redefined-class}, which is called after
  523. completion of the first step of modifying the structure of the
  524. \term{instance}.
  525. The generic function \funref{update-instance-for-redefined-class} takes
  526. four required arguments: the \term{instance} being updated after it has
  527. undergone the first step, a list of the names of \term{local slots} that were
  528. added, a list of the names of \term{local slots} that were discarded, and a
  529. property list containing the \term{slot} names and values of
  530. \term{slots} that were
  531. discarded and had values. Included among the discarded \term{slots} are
  532. \term{slots} that were local in the old \term{class} and that are shared in the new
  533. \term{class}.
  534. The generic function \funref{update-instance-for-redefined-class} also
  535. takes any number of initialization arguments. When it is called by
  536. the system to update an \term{instance} whose \term{class}
  537. has been redefined, no
  538. initialization arguments are provided.
  539. There is a system-supplied primary \term{method} for
  540. \funref{update-instance-for-redefined-class} whose \term{parameter specializer}
  541. for its \term{instance} argument is \theclass{standard-object}.
  542. First this \term{method} checks the validity of initialization arguments and signals an
  543. error if an initialization argument is supplied that is not declared
  544. as valid. (For more information, \seesection\DeclaringInitargValidity.)
  545. Then it calls the generic function
  546. \funref{shared-initialize} with the following arguments: the
  547. \term{instance},
  548. the list of \term{names} of
  549. the newly added \term{slots}, and the initialization
  550. arguments it received.
  551. \endsubsubsection%{Initializing Newly added Local Slots}
  552. \beginsubsubsection{Customizing Class Redefinition}
  553. \reviewer{Barmar: This description is hard to follow.}%!!!
  554. \term{Methods} for \funref{update-instance-for-redefined-class} may be
  555. defined to specify actions to be taken when an \term{instance} is updated.
  556. If only \term{after methods} for \funref{update-instance-for-redefined-class} are
  557. defined, they will be run after the system-supplied primary \term{method} for
  558. initialization and therefore will not interfere with the default
  559. behavior of \funref{update-instance-for-redefined-class}. Because no
  560. initialization arguments are passed to \funref{update-instance-for-redefined-class}
  561. when it is called by the system, the
  562. %\kwd{initform} forms
  563. \term{initialization forms} for \term{slots}
  564. that are filled by \term{before methods} for \funref{update-instance-for-redefined-class}
  565. will not be evaluated by \funref{shared-initialize}.
  566. \term{Methods} for \funref{shared-initialize} may be defined to customize
  567. \term{class} redefinition. For more information, \seesection\SharedInitialize.
  568. \endsubsubsection%{Customizing Class Redefinition}
  569. %% The following was removed by request of Symbolics, who point out that other
  570. %% extensions may well be permitted too, and there's no reason to give special
  571. %% advertising to these. -kmp 9-Oct-90
  572. %
  573. % \beginsubsubsection{Extensions}
  574. %
  575. % There are two allowed extensions to \term{class} redefinition:
  576. %
  577. % \beginlist
  578. %
  579. % \itemitem{\bull} The \OS\ may be extended to permit the new \term{class}
  580. % to be an \term{instance} of a \term{metaclass}
  581. % other than the \term{metaclass} of the
  582. % old \term{class}.
  583. %
  584. % \itemitem{\bull} The \OS\ may be extended to support an updating process
  585. % when either the old or the new \term{class} is an \term{instance} of a
  586. % \term{class}
  587. % other than \typeref{standard-class} that is not a \term{built-in class}.
  588. %
  589. % \endlist
  590. %
  591. % \endsubsubsection%{Extensions}
  592. \beginsubSection{Integrating Types and Classes}
  593. \DefineSection{IntegratingTypesAndClasses}
  594. The \CLOS\ maps the space of \term{classes} into the space of \term{types}.
  595. Every \term{class} that has a proper name has a corresponding \term{type}
  596. with the same \term{name}.
  597. The proper name of every \term{class} is a valid \term{type specifier}. In
  598. addition, every \term{class} \term{object} is a valid \term{type specifier}.
  599. Thus the expression \f{(typep \param{object} \param{class})} evaluates to
  600. \term{true} if the \term{class} of \param{object} is \param{class} itself or
  601. a \term{subclass} of \term{class}. The evaluation of the expression
  602. \f{(subtypep class1 class2)} returns the values
  603. %was {\tt t~t} but came out as "t~t" in formatted form. -kmp 10-Oct-90
  604. %\t~\t\ if \f{class1} is a subclass of \f{class2} or if they are the
  605. \term{true} and \term{true} if \f{class1} is a subclass of \f{class2} or if they are the
  606. same \term{class}; otherwise it returns the values
  607. %was {\tt nil~t}
  608. %\nil~\t.
  609. \term{false} and \term{true}.
  610. % If $I$ is an \term{instance} of some \term{class}
  611. % $C$ named $S$ and $C$ is an \term{instance} of \typeref{standard-class},
  612. % the evaluation of the expression \f{(type-of $I$\/)} will return $S$ if
  613. % $S$ is the proper name of $C$\negthinspace; if $S$ is not the proper
  614. % name of $C$\negthinspace, the expression \f{(type-of $I$\/)} will
  615. % return $C$\negthinspace.
  616. %% Barmar thought the above was too complicated.
  617. %% This is a partial simplification, not quite what he wanted, but hopefully
  618. %% an improvement.
  619. If $I$ is an \term{instance} of some \term{class} $C$ named $S$
  620. and $C$ is an \term{instance} of \typeref{standard-class},
  621. the evaluation of the expression \f{(type-of $I$\/)} returns $S$
  622. if $S$ is the \term{proper name} of $C$;
  623. otherwise, it returns $C$.
  624. Because the names of \term{classes}
  625. and \term{class} \term{objects} are \term{type specifiers}, they may
  626. be used in the special form \specref{the} and in type declarations.
  627. Many but not all of the predefined \term{type specifiers} have a
  628. corresponding \term{class} with
  629. the same proper name as the \term{type}. These type
  630. specifiers are listed in \figref\ClassTypeCorrespondence.
  631. For example, \thetype{array} has
  632. a corresponding \term{class} named \typeref{array}.
  633. No \term{type specifier} that is a
  634. list, such as {\tt (vector double-float 100)}, has a corresponding \term{class}.
  635. The \term{operator} \macref{deftype} does not create any \term{classes}.
  636. Each \term{class} that corresponds to a predefined \term{type specifier} can
  637. be implemented in one of three ways, at the discretion of each implementation.
  638. It can be a \term{standard class},
  639. %% Not necessary. -kmp 12-Feb-92
  640. % (of the kind defined by \macref{defclass}),
  641. a \term{structure class},
  642. %% Not necessary. -kmp 12-Feb-92
  643. % (defined by \macref{defstruct}),
  644. \issue{METACLASS-OF-SYSTEM-CLASS:UNSPECIFIED}
  645. %or a \term{built-in class}.
  646. or a \term{system class}.
  647. \endissue{METACLASS-OF-SYSTEM-CLASS:UNSPECIFIED}
  648. %% Not necessary. -kmp 12-Feb-92
  649. % (implemented in a special, non-extensible way).
  650. %"instances" => "generalized instances" per Barmar,Moon.
  651. A \term{built-in class} is one whose \term{generalized instances} have restricted capabilities
  652. or special representations. Attempting to use \macref{defclass} to define
  653. \term{subclasses} of a \typeref{built-in-class} signals an error.
  654. %"instance" => "generalized instance" per Barmar,Moon.
  655. Calling \funref{make-instance} to create a \term{generalized instance} of a
  656. \term{built-in class} signals an error. Calling \funref{slot-value} on a
  657. %"instance" => "generalized instance" per Barmar,Moon.
  658. \term{generalized instance} of a \term{built-in class} signals an error.
  659. Redefining a \term{built-in class} or using \funref{change-class} to change
  660. %"instance" => "object" per Barmar,Moon.
  661. the \term{class} of an \term{object} to or from a \term{built-in class} signals an error.
  662. However, \term{built-in classes} can be used as \term{parameter specializers}
  663. in \term{methods}.
  664. %The \OS\ specifies that all predefined \term{type specifiers}
  665. %listed in \figref\ClassTypeCorrespondence\ are built-in classes, but a particular
  666. %implementation is allowed to extend the \OS\ to define some of them as
  667. %standard classes or as structure classes.
  668. It is possible to determine whether a \term{class} is a \term{built-in class}
  669. by checking the \term{metaclass}.
  670. A \term{standard class} is an \term{instance} of \theclass{standard-class},
  671. a \term{built-in class} is an \term{instance} of \theclass{built-in-class}, and
  672. a \term{structure class} is an \term{instance} of \theclass{structure-class}.
  673. Each \term{structure} \term{type} created by \macref{defstruct} without
  674. using the \kwd{type} option has a corresponding \term{class}.
  675. %"instance" => "generalized instance" per Barmar,Moon.
  676. This \term{class} is a \term{generalized instance} of \theclass{structure-class}.
  677. %A portable program must assume that
  678. %\typeref{structure-class} is a subclass of \typeref{built-in-class} and has the
  679. %same restrictions as built-in classes. Whether \theclass{structure-class}
  680. %in fact is a subclass of \typeref{built-in-class} is
  681. %\term{implementation-dependent}.
  682. The \kwd{include} option of \macref{defstruct} creates a direct
  683. \term{subclass} of the \term{class}
  684. that corresponds to the included \term{structure}
  685. %Added "type" -kmp
  686. \term{type}.
  687. % I moved the following two paragraphs here from the dictionary entry
  688. % for CLASS. --sjl 7 Mar 92
  689. \issue{CONDITION-SLOTS:HIDDEN}
  690. It is \term{implementation-dependent} whether \term{slots} are involved in the
  691. operation of \term{functions} defined in this specification
  692. on \term{instances} of \term{classes} defined in this specification,
  693. except when \term{slots} are explicitly defined by this specification.
  694. If in a particular \term{implementation} a \term{class} defined in this specification
  695. has \term{slots} that are not defined by this specfication, the names of these \term{slots}
  696. must not be \term{external symbols} of \term{packages} defined in this specification nor
  697. otherwise \term{accessible} in \thepackage{cl-user}.
  698. \endissue{CONDITION-SLOTS:HIDDEN}
  699. The purpose of specifying that many of the standard \term{type specifiers} have a
  700. corresponding \term{class} is to enable users to write \term{methods} that
  701. discriminate on these \term{types}. \term{Method} selection requires that a
  702. \term{class precedence list} can be determined for each \term{class}.
  703. The hierarchical relationships among the \term{type specifiers} are mirrored by
  704. relationships among the \term{classes} corresponding to those \term{types}.
  705. %The existing type hierarchy is used for determining the
  706. %\term{class precedence list}
  707. %for each \term{class} that corresponds to a predefined
  708. %\term{type}. In some cases,
  709. %a \term{local precedence order} is not specifiied for two \term{supertypes}
  710. %of a
  711. %given \term{type specifier}. For example, \typeref{null} is a \term{subtype}
  712. %of both \typeref{symbol} and \typeref{list}, but it is not specified
  713. %whether \typeref{symbol} is more specific or less
  714. %specific than \typeref{list}. The \CLOS\ defines those
  715. %relationships for all such \term{classes}.
  716. \figref\ClassTypeCorrespondence\ lists the set of \term{classes}
  717. that correspond to predefined \term{type specifiers}.
  718. \issue{REAL-NUMBER-TYPE:X3J13-MAR-89}
  719. \DefineFigure{ClassTypeCorrespondence}
  720. \displaythree{Classes that correspond to pre-defined type specifiers}{
  721. arithmetic-error&generic-function&simple-error\cr
  722. array&hash-table&simple-type-error\cr
  723. bit-vector&integer&simple-warning\cr
  724. broadcast-stream&list&standard-class\cr
  725. built-in-class&logical-pathname&standard-generic-function\cr
  726. cell-error&method&standard-method\cr
  727. character&method-combination&standard-object\cr
  728. class&null&storage-condition\cr
  729. complex&number&stream\cr
  730. concatenated-stream&package&stream-error\cr
  731. condition&package-error&string\cr
  732. cons&parse-error&string-stream\cr
  733. control-error&pathname&structure-class\cr
  734. division-by-zero&print-not-readable&structure-object\cr
  735. echo-stream&program-error&style-warning\cr
  736. end-of-file&random-state&symbol\cr
  737. error&ratio&synonym-stream\cr
  738. file-error&rational&t\cr
  739. file-stream&reader-error&two-way-stream\cr
  740. float&readtable&type-error\cr
  741. floating-point-inexact&real&unbound-slot\cr
  742. floating-point-invalid-operation&restart&unbound-variable\cr
  743. floating-point-overflow&sequence&undefined-function\cr
  744. floating-point-underflow&serious-condition&vector\cr
  745. function&simple-condition&warning\cr
  746. }
  747. \endissue{REAL-NUMBER-TYPE:X3J13-MAR-89}
  748. The \term{class precedence list} information specified in the entries for
  749. each of these \term{classes} are those that are required by the \OS.
  750. Individual implementations may be extended to define other type
  751. specifiers to have a corresponding \term{class}. Individual implementations
  752. may be extended to add other \term{subclass} relationships and to add other
  753. \term{elements} to the \term{class precedence lists} as long as
  754. they do not violate the type relationships and disjointness
  755. requirements specified by this standard.
  756. A standard \term{class} defined with no direct \term{superclasses} is guaranteed to
  757. be disjoint from all of the \term{classes} in the table, except for the
  758. class named \typeref{t}.
  759. \endsubSection%{Integrating Types and Classes}