concept-gfs-and-methods.tex 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931
  1. % -*- Mode: TeX -*-
  2. %!!! Uses of "built-in" could be "\term{standardized}" here.
  3. \beginSubsection{Introduction to Generic Functions}
  4. \DefineSection{IntroToGFs}
  5. A \newterm{generic function} is a function whose behavior depends on
  6. the \term{classes} or identities of the \term{arguments} supplied to it.
  7. A \term{generic function} \term{object}
  8. %contains
  9. is associated with
  10. a set of \term{methods},
  11. a \term{lambda list},
  12. a \term{method combination}\meaning{2},
  13. and other information.
  14. Like an \term{ordinary function}, a \term{generic function} takes \term{arguments},
  15. performs a series of operations, and perhaps returns useful \term{values}.
  16. An \term{ordinary function} has a single body of \term{code} that is always \term{executed}
  17. when the \term{function} is called. A \term{generic function} has a set of bodies
  18. of \term{code} of which a subset is selected for \term{execution}. The selected
  19. bodies of \term{code} and the manner of their combination are determined by
  20. the \term{classes} or identities of one or more of the \term{arguments} to the
  21. \term{generic function} and by its \term{method combination}.
  22. \term{Ordinary functions} and \term{generic functions} are called with identical syntax.
  23. \term{Generic functions} are true \term{functions} that can be passed as \term{arguments}
  24. and used as the first \term{argument} to \funref{funcall} and \funref{apply}.
  25. A \term{binding} of a \term{function name} to a \term{generic function}
  26. can be \term{established} in one of several ways. It can be
  27. \term{established} in the \term{global environment} by
  28. \funref{ensure-generic-function},
  29. \macref{defmethod} (implicitly, due to \funref{ensure-generic-function})
  30. or
  31. \macref{defgeneric} (also implicitly, due to \funref{ensure-generic-function}).
  32. \issue{GENERIC-FLET-POORLY-DESIGNED:DELETE}
  33. No \term{standardized} mechanism is provided for \term{establishing} a
  34. \term{binding} of a \term{function name} to a \term{generic function}
  35. in the \term{lexical environment}.
  36. % It can be \term{established} in the \term{lexical environment} by
  37. \issue{WITH-ADDED-METHODS:DELETE}
  38. % \specref{with-added-methods},
  39. \endissue{WITH-ADDED-METHODS:DELETE}
  40. % \specref{generic-flet}
  41. % or
  42. % \specref{generic-labels}.
  43. % The \term{name} part of such a \term{binding}, like the name of an ordinary
  44. % \term{function}, can be either a \term{symbol} or a two-element
  45. % \term{list} whose first element is \misc{setf} and whose second element
  46. % is a \term{symbol}. This is true for both \term{lexical bindings} and
  47. % \term{global bindings}.
  48. %
  49. % The \specref{generic-flet} special form creates new local generic
  50. % functions using the set of methods specified by the method definitions
  51. % in the \specref{generic-flet} form. The scoping of generic function names
  52. % within a \specref{generic-flet} form is the same as for \specref{flet}.
  53. %
  54. % The \specref{generic-labels} special form creates a set of new mutually
  55. % recursive local generic functions using the set of methods specified
  56. % by the method definitions in the \specref{generic-labels} form. The
  57. % scoping of generic function names within a \specref{generic-labels} form
  58. % is the same as for \specref{labels}.
  59. \endissue{GENERIC-FLET-POORLY-DESIGNED:DELETE}
  60. \issue{WITH-ADDED-METHODS:DELETE}
  61. % The \specref{with-added-methods} special form creates new local generic
  62. % functions by adding the set of methods specified by the method
  63. % definitions with a given name in the \specref{with-added-methods} form to
  64. % copies of the methods of the lexically visible generic function of the
  65. % same name. If there is a lexically visible ordinary function of the
  66. % same name as one of specified generic functions, that function
  67. % becomes the method function of the default method for the new generic
  68. % function of that name.
  69. \endissue{WITH-ADDED-METHODS:DELETE}
  70. \issue{GENERIC-FLET-POORLY-DESIGNED:DELETE}
  71. % The \macref{generic-function} macro creates an anonymous generic
  72. % function with the set of methods specified by the method definitions
  73. % in the \macref{generic-function} form.
  74. \endissue{GENERIC-FLET-POORLY-DESIGNED:DELETE}
  75. When a \macref{defgeneric} form is evaluated, one of three actions
  76. is taken (due to \funref{ensure-generic-function}):
  77. %!!! Barrett observes that GENERIC-FLET and GENERIC-LABELS are not correctly
  78. % spoken for here because they are classified as method-defining.
  79. % But since they've been flushed, I guess it doesn't matter. -kmp 12-Feb-92
  80. \beginlist
  81. \itemitem{\bull} If a generic function of the given name already exists,
  82. the existing generic function object is modified. Methods specified
  83. by the current \macref{defgeneric} form are added, and any methods in the
  84. existing generic function that were defined by a previous \macref{defgeneric}
  85. form are removed. Methods added by the current \macref{defgeneric}
  86. form might replace methods defined by \macref{defmethod},
  87. \macref{defclass}, \macref{define-condition}, or \macref{defstruct}.
  88. No other methods in the generic function are affected
  89. or replaced.
  90. \itemitem{\bull} If the given name names
  91. an \term{ordinary function},
  92. a \term{macro},
  93. or a \term{special operator},
  94. an error is signaled.
  95. \itemitem{\bull} Otherwise a generic function is created with the
  96. methods specified by the method definitions in the \macref{defgeneric}
  97. form.
  98. \endlist
  99. Some \term{operators} permit specification of the options of a
  100. \term{generic function}, such as
  101. the \term{type} of \term{method combination} it uses
  102. or its \term{argument precedence order}.
  103. These \term{operators} will be referred to as
  104. ``operators that specify generic function options.''
  105. \issue{GENERIC-FLET-POORLY-DESIGNED:DELETE}
  106. % These \term{operators} are:
  107. % \macref{defgeneric},
  108. %% Reworded since there's only one.
  109. The only \term{standardized} \term{operator} in this category is \macref{defgeneric}.
  110. %\macref{generic-function},
  111. \issue{WITH-ADDED-METHODS:DELETE}
  112. %\specref{with-added-methods},
  113. \endissue{WITH-ADDED-METHODS:DELETE}
  114. % \specref{generic-flet},
  115. % and
  116. % \specref{generic-labels}.
  117. \endissue{GENERIC-FLET-POORLY-DESIGNED:DELETE}
  118. Some \term{operators} define \term{methods} for a \term{generic function}.
  119. These \term{operators} will be referred to as
  120. \newtermidx{method-defining operators}{method-defining operator};
  121. their associated \term{forms} are called \term{method-defining forms}.
  122. The \term{standardized} \term{method-defining operators} are listed in \thenextfigure.
  123. % The \term{standardized} \term{operators} in this category are:
  124. % \macref{defgeneric},
  125. % \macref{defmethod},
  126. % \issue{GENERIC-FLET-POORLY-DESIGNED:DELETE}
  127. % %\macref{generic-function},
  128. % % \specref{generic-flet},
  129. % % \specref{generic-labels},
  130. % \endissue{GENERIC-FLET-POORLY-DESIGNED:DELETE}
  131. % \issue{WITH-ADDED-METHODS:DELETE}
  132. % %\specref{with-added-methods},
  133. % \endissue{WITH-ADDED-METHODS:DELETE}%
  134. % \macref{defclass},
  135. % \macref{define-condition},
  136. % and
  137. % \macref{defstruct}.
  138. \DefineFigure{StdMethDefOps}
  139. \issue{GENERIC-FLET-POORLY-DESIGNED:DELETE}
  140. \issue{WITH-ADDED-METHODS:DELETE}
  141. %Removed GENERIC-FUNCTION, GENERIC-FLET, and GENERIC-LABELS.
  142. %Removed WITH-ADDED-METHODS.
  143. \displaythree{Standardized Method-Defining Operators}{
  144. defgeneric&defmethod&defclass\cr
  145. define-condition&defstruct&\cr
  146. }
  147. \endissue{WITH-ADDED-METHODS:DELETE}
  148. \endissue{GENERIC-FLET-POORLY-DESIGNED:DELETE}
  149. Note that of the \term{standardized} \term{method-defining operators}
  150. % all of the method-defining operators except
  151. % \macref{defclass},
  152. % \macref{defmethod},
  153. % \macref{define-condition},
  154. % and
  155. % \macref{defstruct}
  156. only \macref{defgeneric}
  157. can specify \term{generic function} options.
  158. \macref{defgeneric} and any \term{implementation-defined} \term{operators}
  159. that can specify \term{generic function} options
  160. are also referred to as ``operators that specify generic function options.''
  161. \endSubsection%{Introduction to Generic Functions}
  162. \beginSubsection{Introduction to Methods}
  163. \DefineSection{IntroToMethods}
  164. \term{Methods} define the class-specific or identity-specific behavior
  165. and operations of a \term{generic function}.
  166. %KAB was a bit unhappy with "contains" here.
  167. %He also didn't like the term "method function".
  168. A \term{method} \term{object}
  169. %contains
  170. is associated with
  171. % a method function,
  172. \term{code} that implements the method's behavior,
  173. a sequence of \term{parameter specializers}
  174. that specify when the given \term{method} is applicable,
  175. a \term{lambda list},
  176. and a sequence of \term{qualifiers} that are used by the method combination
  177. facility to distinguish among \term{methods}.
  178. A method object is not a function and cannot be invoked as a function.
  179. Various mechanisms in the \OS\ take a method object and invoke its method
  180. function, as is the case when a generic function is invoked. When this
  181. occurs it is said that the method is invoked or called.
  182. A method-defining form contains the \term{code} that is to be run when the
  183. arguments to the generic function cause the method that it defines to
  184. be invoked. When a method-defining form is evaluated, a method object
  185. is created and one of four actions is taken:
  186. \beginlist
  187. \itemitem{\bull} If a \term{generic function} of the given name already exists
  188. and if a \term{method object} already exists that agrees with the new one on
  189. \term{parameter specializers} and \term{qualifiers}, the new \term{method object} replaces
  190. the old one. For a definition of one method agreeing with another on
  191. \term{parameter specializers} and \term{qualifiers},
  192. \seesection\SpecializerQualifierAgreement.
  193. \itemitem{\bull} If a \term{generic function} of the given name already exists
  194. and if there is no \term{method object} that agrees with the new one on
  195. \term{parameter specializers} and \term{qualifiers}, the existing \term{generic function}
  196. \term{object} is modified to contain the new \term{method} \term{object}.
  197. \itemitem{\bull} If the given \term{name} names an \term{ordinary function}, a \term{macro},
  198. or a \term{special operator}, an error is signaled.
  199. \itemitem{\bull} Otherwise a \term{generic function} is created with the \term{method}
  200. specified by the \term{method-defining form}.
  201. \endlist
  202. If the \term{lambda list} of a new \term{method} is not
  203. \term{congruent} with the \term{lambda list} of the \term{generic function},
  204. an error is signaled. If a \term{method-defining operator} that cannot specify
  205. \term{generic function} options creates a new \term{generic function},
  206. a \term{lambda list} for that \term{generic function} is derived from the
  207. \term{lambda list} of the \term{method} in the \term{method-defining form} in such a way
  208. as to be \term{congruent} with it. For a discussion of \newterm{congruence},
  209. \seesection\GFMethodLambdaListCongruency.
  210. Each method has a \term{specialized lambda list}, which determines
  211. when that method can be applied. A \term{specialized lambda list} is like
  212. an \term{ordinary lambda list} except that a specialized parameter
  213. may occur instead of the name of a required parameter. A specialized parameter
  214. is a list \f{(\i{variable-name} \i{parameter-specializer-name})},
  215. where \i{parameter-specializer-name} is one of the following:
  216. \beginlist
  217. \itemitem{a \term{symbol}}
  218. denotes a \term{parameter specializer} which is the \term{class}
  219. named by that \term{symbol}.
  220. \issue{CLASS-OBJECT-SPECIALIZER:AFFIRM}
  221. %This was apparently introduced accidentally, but has been confirmed by X3J13 vote.
  222. % -kmp 08-Apr-91
  223. \itemitem{a \term{class}}
  224. denotes a \term{parameter specializer} which is the \term{class} itself.
  225. \endissue{CLASS-OBJECT-SPECIALIZER:AFFIRM}
  226. \itemitem{\f{(eql \i{form})}}
  227. denotes a \term{parameter specializer} which satisfies the \term{type specifier}
  228. \f{(eql \i{object})}, where \i{object} is the
  229. result of evaluating \i{form}. The form \i{form} is evaluated in
  230. the lexical environment in which the method-defining form is evaluated.
  231. Note that \i{form} is evaluated only once, at the time the method is
  232. defined, not each time the generic function is called.
  233. \endlist
  234. \term{Parameter specializer names} are used in macros intended as the
  235. user-level interface (\macref{defmethod}), while \term{parameter specializers}
  236. are used in the functional interface.
  237. Only required parameters may be specialized, and there must be a
  238. \term{parameter specializer} for each required parameter. For notational
  239. simplicity, if some required parameter in a \term{specialized lambda list} in
  240. a method-defining form is simply a variable name, its
  241. \term{parameter specializer} defaults to \theclass{t}.
  242. Given a generic function and a set of arguments, an applicable
  243. method is a method for that generic function whose parameter
  244. specializers are satisfied by their corresponding arguments. The
  245. following definition specifies what it means for a method to be
  246. applicable and for an argument to satisfy a \term{parameter specializer}.
  247. %Barmar: Review use of ``instance'' here. Also, instead of
  248. % ``$C=P\sub i$ or ...'' we should refer just to ``(TYPEP Ai Pi) is true.''
  249. %Since this is a hot topic on the mail right now, I'll leave this
  250. % until the dust settles. -kmp 22-Oct-90
  251. %KMP: I think this is finally fixed.
  252. Let $\langle A\sub 1, \ldots, A\sub n\rangle$ be the required
  253. arguments to a generic function in order. Let $\langle P\sub 1,
  254. \ldots, P\sub n\rangle$ be the \term{parameter specializers} corresponding to
  255. the required parameters of the method $M$ in order. The method $M$ is
  256. % applicable when each $A\sub i$ satisfies $P\sub i$.
  257. % If $P\sub i$ is a class, and if $A\sub i$ is a \term{direct instance} of a class
  258. % $C$\negthinspace, then it is said that $A\sub i$ satisfies
  259. % $P\sub i$ when $C=P\sub i$ or when $C$ is a subclass of $P\sub i$. If
  260. % $P\sub i$ is {\tt (eql \i{object})}, then it is said that
  261. % $A\sub i$ satisfies $P\sub i$ when
  262. % %\thefunction{eql} applied to $A\sub i$ and \i{object} \term{yields} \term{true}.
  263. % $A\sub i$ is the \term{same} as \i{object} (\ie under \funref{eql}).
  264. applicable when each $A\sub i$ is of the \term{type} specified by
  265. the \term{type specifier} $P\sub i$.
  266. % Because a \term{parameter specializer} is a \term{type specifier},
  267. % \thefunction{typep} can be used during method selection
  268. % to determine whether an
  269. % argument satisfies a \term{parameter specializer}.
  270. % %% For Moon
  271. % %In general
  272. % A
  273. % %arbitrary
  274. % \term{parameter specializer} cannot be a \term{compound type specifier}.
  275. % %Example moved to the glossary.
  276. % %such as {\tt (vector single-float)}.
  277. % The only \term{list} that can be a \term{parameter specializer} is \f{(eql \i{object})}.
  278. % %This requires that
  279. % %Common Lisp be modified to include the \term{type specifier} \funref{eql} to be
  280. % %defined as if the following were evaluated:
  281. % %
  282. % %$$\hbox{\f{(deftype eql (object) `(member ,object))}}$$
  283. % %
  284. %% Rewritten by KMP:
  285. %% This part isn't really needed because it's said just a few paragraphs before.
  286. % A \term{parameter specializer} can be a \term{class}, a \term{class} \term{name},
  287. % or \f{(eql \i{object})}.
  288. %% I think this can stand on its own.
  289. Because every valid \term{parameter specializer} is
  290. also a valid \term{type specifier}, \thefunction{typep} can be used during method
  291. selection to determine whether an argument satisfies a \term{parameter specializer}.
  292. A method all of whose \term{parameter specializers} are
  293. \theclass{t} is called a \newterm{default method}; it is always applicable but
  294. may be shadowed by a more specific method.
  295. Methods can have \term{qualifiers}, which give the method combination
  296. procedure a way to distinguish among methods. A method that has one
  297. or more \term{qualifiers} is called a \term{qualified method}.
  298. A method with no \term{qualifiers} is called an \term{unqualified method}.
  299. A \term{qualifier} is any \term{non-list}.
  300. % The \term{qualifiers} defined by standard method combination
  301. % and by the built-in method combination types are \term{symbols}.
  302. %% Simplification per Barrett:
  303. The \term{qualifiers} defined by the \term{standardized} method combination types
  304. are \term{symbols}.
  305. In this specification, the terms ``\term{primary method}'' and
  306. ``\term{auxiliary method}'' are used to partition \term{methods}
  307. within a method combination type according to their intended use.
  308. In standard method combination, \term{primary methods} are
  309. \term{unqualified methods}
  310. and \term{auxiliary methods} are methods with a single \term{qualifier}
  311. that is one of \kwd{around}, \kwd{before}, or \kwd{after}.
  312. \term{Methods} with these \term{qualifiers} are called \term{around methods},
  313. \term{before methods}, and \term{after methods}, respectively.
  314. When a method combination type is defined using the short form of
  315. \macref{define-method-combination}, \term{primary methods} are
  316. methods qualified with the name of the type of method combination,
  317. and auxiliary methods have the \term{qualifier} \kwd{around}.
  318. Thus the terms ``\term{primary method}'' and ``\term{auxiliary method}''
  319. have only a relative definition within a given method combination type.
  320. \endSubsection%{Introduction to Methods}
  321. \beginSubsection{Agreement on Parameter Specializers and Qualifiers}
  322. \DefineSection{SpecializerQualifierAgreement}
  323. Two \term{methods} are said to agree with each other on \term{parameter specializers}
  324. and \term{qualifiers} if the following conditions hold:
  325. \beginlist
  326. \itemitem{1.} Both methods have the same number of required parameters.
  327. Suppose the \term{parameter specializers} of the two methods are
  328. $P\sub{1,1}\ldots P\sub{1,n}$ and $P\sub{2,1}\ldots P\sub{2,n}$.
  329. \itemitem{2.} For each $1\leq i\leq n$, $P\sub{1,i}$ agrees with $P\sub{2,i}$.
  330. The \term{parameter specializer} $P\sub{1,i}$ agrees with $P\sub{2,i}$ if
  331. $P\sub{1,i}$ and $P\sub{2,i}$ are the same class or if
  332. $P\sub{1,i}=\hbox{{\tt(\b{eql} $\hbox{\i{object}}\sub 1$)}}$,
  333. $P\sub{2,i}=\hbox{{\tt(\b{eql} $\hbox{\i{object}}\sub 2$)}}$, and
  334. {\tt (\b{eql} $\hbox{\i{object}}\sub 1$ $\hbox{\i{object}}\sub 2$)}.
  335. Otherwise $P\sub{1,i}$ and $P\sub{2,i}$ do not agree.
  336. \itemitem{3.} The two \term{lists} of \term{qualifiers} are the \term{same}
  337. under \funref{equal}.
  338. % \itemitem{3.} The lists of \term{qualifiers} of both methods contain the same
  339. % \term{non-nil} atoms in the same order. That is, the lists are \funref{equal}.
  340. \endlist
  341. \endSubsection%{Agreement on Parameter Specializers and Qualifiers}
  342. \beginSubsection{Congruent Lambda-lists for all Methods of a Generic Function}
  343. \DefineSection{GFMethodLambdaListCongruency}
  344. These rules define the congruence of a set of \term{lambda lists}, including the
  345. \term{lambda list} of each method for a given generic function and the
  346. \term{lambda list} specified for the generic function itself, if given.
  347. \beginlist
  348. \itemitem{1.} Each \term{lambda list} must have the same number of required
  349. parameters.
  350. \itemitem{2.} Each \term{lambda list} must have the same number of optional
  351. parameters. Each method can supply its own default for an optional
  352. parameter.
  353. \itemitem{3.} If any \term{lambda list} mentions \keyref{rest} or \keyref{key}, each
  354. \term{lambda list} must mention one or both of them.
  355. \itemitem{4.} If the \term{generic function} \term{lambda list}
  356. mentions \keyref{key}, each
  357. method must accept all of the keyword names mentioned after \keyref{key},
  358. either by accepting them explicitly, by specifying \keyref{allow-other-keys},
  359. or by specifying \keyref{rest} but not \keyref{key}.
  360. Each method can accept additional keyword arguments of its own. The
  361. checking of the validity of keyword names is done in the generic
  362. function, not in each method.
  363. %!!! "Leftmost". Sigh.
  364. A method is invoked as if the keyword
  365. argument pair whose name is \kwd{allow-other-keys} and whose value
  366. is \term{true} were supplied, though no such argument pair will be passed.
  367. %!!! KAB: Alternatively, as if the lambda list of the method specified &allow-other-keys.
  368. \itemitem{5.} The use of \keyref{allow-other-keys} need not be consistent
  369. across \term{lambda lists}. If \keyref{allow-other-keys} is mentioned in
  370. the \term{lambda list} of any applicable \term{method} or of the \term{generic function},
  371. any keyword arguments may be mentioned in the call to the \term{generic function}.
  372. \itemitem{6.} The use of \keyref{aux} need not be consistent across methods.
  373. If a \term{method-defining operator} that cannot specify \term{generic function} options
  374. creates a \term{generic function}, and if the \term{lambda list} for the method
  375. mentions keyword arguments, the \term{lambda list} of the generic function
  376. will mention \keyref{key} (but no keyword arguments).
  377. \endlist
  378. \endSubsection%{Congruent Lambda-lists for All Methods of a Generic Function}
  379. %\newpage
  380. \beginSubsection{Keyword Arguments in Generic Functions and Methods}
  381. \DefineSection{KwdArgsInGFsAndMeths}
  382. When a generic function or any of its methods mentions
  383. \keyref{key} in a \term{lambda list}, the specific set of keyword
  384. arguments accepted by the generic function varies according to the
  385. applicable methods. The set of keyword arguments accepted by the
  386. generic function for a particular call is the union of the keyword
  387. arguments accepted by all applicable methods and the keyword arguments
  388. mentioned after \keyref{key} in the generic function definition,
  389. if any. A method that has \keyref{rest} but not \keyref{key} does not affect the
  390. set of acceptable keyword arguments. If
  391. the \term{lambda list} of any applicable method or of the generic
  392. function definition contains \keyref{allow-other-keys}, all
  393. keyword arguments are accepted by the generic function.
  394. The \term{lambda list} congruence rules require that each method
  395. accept all of the keyword arguments mentioned after \keyref{key} in the
  396. generic function definition, by accepting them explicitly, by
  397. specifying \keyref{allow-other-keys}, or by specifying \keyref{rest} but
  398. not \keyref{key}. Each method can accept additional keyword arguments
  399. of its own, in addition to the keyword arguments mentioned in the
  400. generic function definition.
  401. If a \term{generic function} is passed a keyword argument that no applicable
  402. method accepts, an error should be signaled; \seesection\FuncallErrorChecking.
  403. \beginsubsubsection{Examples of Keyword Arguments in Generic Functions and Methods}
  404. For example, suppose there are two methods defined for {\tt width}
  405. as follows:
  406. \code
  407. (defmethod width ((c character-class) &key font) ...)
  408. (defmethod width ((p picture-class) &key pixel-size) ...)
  409. \endcode
  410. \noindent Assume that there are no other methods and no generic
  411. function definition for {\tt width}. The evaluation of the
  412. following form should signal an error because
  413. the keyword argument \kwd{pixel-size} is not accepted by the applicable method.
  414. \code
  415. (width (make-instance `character-class :char #\\Q)
  416. :font 'baskerville :pixel-size 10)
  417. \endcode
  418. The evaluation of the following form should signal an error.
  419. \code
  420. (width (make-instance `picture-class :glyph (glyph #\\Q))
  421. :font 'baskerville :pixel-size 10)
  422. \endcode
  423. The evaluation of the following form will not signal an error
  424. if the class named {\tt character-picture-class} is a subclass of
  425. both {\tt picture-class} and {\tt character-class}.
  426. \code
  427. (width (make-instance `character-picture-class :char #\\Q)
  428. :font 'baskerville :pixel-size 10)
  429. \endcode
  430. \endsubsubsection%{Examples of Keyword Arguments in Generic Functions and Methods}
  431. \endSubsection%{Keyword Arguments in Generic Functions and Methods}
  432. \beginSubsection{Method Selection and Combination}
  433. \DefineSection{MethodSelectionAndCombination}
  434. When a \term{generic function} is called with particular arguments, it must
  435. determine the code to execute. This code is called the
  436. \newterm{effective method} for those \term{arguments}.
  437. The \term{effective method} is a
  438. combination of the \term{applicable methods} in the \term{generic function}
  439. that \term{calls} some or all of the \term{methods}.
  440. \issue{CLOS-ERROR-CHECKING-ORDER:NO-APPLICABLE-METHOD-FIRST}
  441. % If a \term{generic function} is
  442. % called and no \term{methods} are \term{applicable},
  443. % the \term{generic function} \funref{no-applicable-method} is invoked.
  444. If a \term{generic function} is called and no \term{methods} are
  445. \term{applicable}, the \term{generic function} \funref{no-applicable-method}
  446. is invoked, with the \term{results} from that call being used as the
  447. \term{results} of the call to the original \term{generic function}. Calling
  448. \funref{no-applicable-method} takes precedence over checking for acceptable
  449. keyword arguments; \seesection\KwdArgsInGFsAndMeths.
  450. \endissue{CLOS-ERROR-CHECKING-ORDER:NO-APPLICABLE-METHOD-FIRST}
  451. When the \term{effective method} has been determined,
  452. it is invoked with the same \term{arguments} as were passed to the \term{generic function}.
  453. Whatever \term{values} it returns are returned as the \term{values}
  454. of the \term{generic function}.
  455. \beginsubsubsection{Determining the Effective Method}
  456. \DefineSection{DeterminingtheEffectiveMethod}
  457. The effective method is determined by the following three-step procedure:
  458. \beginlist
  459. \itemitem{1.}{Select the applicable methods.}
  460. \itemitem{2.}{Sort the applicable methods by precedence order, putting
  461. the most specific method first.}
  462. \itemitem{3.}{Apply method combination to the sorted list of
  463. applicable methods, producing the effective method.}
  464. \endlist
  465. \beginsubsubsubsection{Selecting the Applicable Methods}
  466. \DefineSection{SelApplMeth}
  467. This step is described in \secref\IntroToMethods.
  468. \endsubsubsubsection%{Selecting the Applicable Methods}
  469. \beginsubsubsubsection{Sorting the Applicable Methods by Precedence Order}
  470. To compare the precedence of two methods, their \term{parameter specializers}
  471. are examined in order. The default examination order is from left to
  472. right, but an alternative order may be specified by the
  473. \kwd{argument-precedence-order} option to \macref{defgeneric} or to any of
  474. the other operators that specify generic function options.
  475. The corresponding \term{parameter specializers} from each method are
  476. compared. When a pair of \term{parameter specializers} agree, the next
  477. pair are compared for agreement. If all corresponding parameter
  478. specializers agree, the two methods must have different
  479. \term{qualifiers}; in this case, either method can be selected to precede the
  480. other. For information about agreement, \seesection\SpecializerQualifierAgreement.
  481. If some corresponding \term{parameter specializers} do not agree, the first
  482. pair of \term{parameter specializers} that do not agree determines the
  483. precedence. If both \term{parameter specializers} are classes, the more
  484. specific of the two methods is the method whose \term{parameter specializer}
  485. appears earlier in the \term{class precedence list} of the corresponding
  486. argument. Because of the way in which the set of applicable methods
  487. is chosen, the \term{parameter specializers} are guaranteed to be present in
  488. the class precedence list of the class of the argument.
  489. If just one of a pair of corresponding \term{parameter specializers} is {\tt (eql \i{object})},
  490. the \term{method} with that \term{parameter specializer} precedes the
  491. other \term{method}. If both \term{parameter specializers} are \funref{eql}
  492. \term{expressions}, the
  493. specializers must agree (otherwise the two \term{methods} would
  494. not both have been applicable to this argument).
  495. The resulting list of \term{applicable methods} has the most specific
  496. \term{method} first and the least specific \term{method} last.
  497. \endsubsubsubsection%{Sorting the Applicable Methods by Precedence Order}
  498. \beginsubsubsubsection{Applying method combination to the sorted list of applicable methods}
  499. \DefineSection{ApplyMethCombToSortedMethods}
  500. In the simple case---if standard method combination is used and all
  501. applicable methods are primary methods---the
  502. %!!! Barrett suggests that this is not the normal meaning of "effective method"
  503. effective method is the most specific method.
  504. That method can call the next most specific
  505. method by using \thefunction{call-next-method}. The method that
  506. \funref{call-next-method} will call is referred to as the
  507. \newterm{next method}. The predicate \funref{next-method-p} tests whether a next
  508. method exists. If \funref{call-next-method} is called and there is no
  509. next most specific method, the generic function \funref{no-next-method}
  510. is invoked.
  511. In general, the effective method is some combination of the applicable
  512. methods. It is described by a \term{form} that contains calls to some or
  513. all of the applicable methods, returns the value or values that will
  514. be returned as the value or values of the generic function, and
  515. optionally makes some of the methods accessible by means of
  516. \funref{call-next-method}.
  517. %% Moon wanted this removed. Barrett agrees. -kmp 9-Feb-92
  518. % This Lisp form is the body of the effective
  519. % method; it is augmented with an appropriate \term{lambda list} to
  520. % make it a function.
  521. The role of each method in the effective method is determined by its
  522. %\term{method}
  523. \term{qualifiers} and the specificity of the method. A \term{qualifier}
  524. serves to mark a method, and the meaning of a \term{qualifier} is
  525. determined by the way that these marks are used by this step
  526. of the procedure. If an applicable method has an unrecognized
  527. \term{qualifier}, this step signals an error and does not include that method
  528. in the effective method.
  529. When standard method combination is used together with qualified methods,
  530. the effective method is produced as described in \secref\StdMethComb.
  531. Another type of method combination can be specified by using the
  532. \kwd{method-combination} option of \macref{defgeneric} or
  533. of any of the other operators that specify generic function options. In
  534. this way this step of the procedure can be customized.
  535. New types of method combination can be defined by using
  536. \themacro{define-method-combination}.
  537. \endsubsubsubsection%{Applying method combination to the sorted list of applicable methods}
  538. \endsubsubsection%{Determining the Effective Method}
  539. \beginsubsubsection{Standard Method Combination}
  540. \DefineSection{StdMethComb}
  541. \idxref{standard}
  542. %!!! Barrett: "supported" ?
  543. Standard method combination is supported by \theclass{standard-generic-function}.
  544. It is used if no other type of method
  545. combination is specified or if the built-in method combination type
  546. \misc{standard} is specified.
  547. Primary methods define the main action of the effective method,
  548. while auxiliary methods modify that action in one of three ways.
  549. A primary method has no method \term{qualifiers}.
  550. An auxiliary method is a method whose
  551. %\term{method}
  552. \term{qualifier} is \kwd{before}, \kwd{after}, or \kwd{around}.
  553. Standard method combination
  554. allows no more than one \term{qualifier} per method; if a method definition
  555. specifies more than one \term{qualifier} per method, an error is signaled.
  556. \beginlist
  557. \itemitem{\bull}
  558. A \term{before method} has the keyword \kwd{before} as its only \term{qualifier}.
  559. A \term{before method} specifies \term{code} that is to be run before any
  560. \term{primary methods}.
  561. \itemitem{\bull}
  562. An \term{after method} has the keyword \kwd{after} as its only \term{qualifier}.
  563. An \term{after method} specifies \term{code} that is to be run after
  564. \term{primary methods}.
  565. \itemitem{\bull}
  566. An \term{around method} has the keyword \kwd{around} as its only \term{qualifier}.
  567. An \term{around method} specifies \term{code} that is to be run instead of other
  568. \term{applicable methods},
  569. %%I found this to be too vague. -kmp 9-Jan-91
  570. %but which is able to cause some of them to be run.
  571. but which might contain explicit \term{code}
  572. which calls some of those \term{shadowed} \term{methods}
  573. (via \funref{call-next-method}).
  574. \endlist
  575. The semantics of standard method combination is as follows:
  576. \beginlist
  577. \itemitem{\bull} If there are any \term{around methods}, the most specific
  578. \term{around method} is called. It supplies the value or values of the
  579. generic function.
  580. \itemitem{\bull} Inside the body of an \term{around method},
  581. \funref{call-next-method} can be used to call the \term{next method}. When the next
  582. method returns, the \term{around method} can execute more code,
  583. perhaps based on the returned value or values.
  584. % !!!
  585. % Moon: Can't happen, `next page' says signals an error if there are no primaries.
  586. % Barrett: This is a bone of contention. (e.g., no-next-method might -do- the signaling)
  587. \TheGF{no-next-method} is invoked if \funref{call-next-method} is used and
  588. there is no \term{applicable method} to call. \Thefunction{next-method-p}
  589. may be used to determine whether a \term{next method} exists.
  590. \itemitem{\bull}
  591. If an \term{around method} invokes \funref{call-next-method},
  592. the next most specific \term{around method}
  593. is called, if one is applicable. If there are no \term{around methods}
  594. or if \funref{call-next-method} is called by the least
  595. specific \term{around method}, the other methods are called as
  596. follows:
  597. \beginlist
  598. \itemitem{--} All the \term{before methods} are called, in
  599. most-specific-first order. Their values are ignored.
  600. An error is signaled if \funref{call-next-method} is used in a
  601. \term{before method}.
  602. \itemitem{--} The most specific primary method is called. Inside the
  603. body of a primary method, \funref{call-next-method} may be used to call
  604. the next most specific primary method. When that method returns, the
  605. previous primary method can execute more code, perhaps based on the
  606. returned value or values. The generic function \funref{no-next-method}
  607. is invoked if \funref{call-next-method} is used and there are no more
  608. applicable primary methods. \Thefunction{next-method-p} may be
  609. used to determine whether a \term{next method} exists. If \funref{call-next-method}
  610. is not used, only the most specific \term{primary method} is called.
  611. \itemitem{--} All the \term{after methods} are called in
  612. most-specific-last order. Their values are ignored.
  613. An error is signaled if \funref{call-next-method} is used in an
  614. \term{after method}.
  615. \endlist
  616. \itemitem{\bull} If no \term{around methods} were invoked, the most
  617. specific primary method supplies the value or values returned by the
  618. generic function. The value or values returned by the invocation of
  619. \funref{call-next-method} in the least specific \term{around method} are
  620. those returned by the most specific primary method.
  621. \endlist
  622. In standard method combination, if there is an applicable method
  623. but no applicable primary method, an error is signaled.
  624. The \term{before methods} are run in most-specific-first order while
  625. the \term{after methods} are run in least-specific-first order. The
  626. design rationale for this difference can be illustrated with an
  627. example. Suppose class $C\sub 1$ modifies the behavior of its
  628. superclass, $C\sub 2$, by adding \term{before methods} and \term{after methods}.
  629. Whether the behavior of the class $C\sub 2$ is defined
  630. directly by methods on $C\sub 2$ or is inherited from its superclasses
  631. does not affect the relative order of invocation of methods on
  632. instances of the class $C\sub 1$. Class $C\sub 1$'s
  633. \term{before method} runs before all of class $C\sub 2$'s methods.
  634. Class $C\sub 1$'s \term{after method} runs after all of class $C\sub 2$'s methods.
  635. By contrast, all \term{around methods} run before any other methods
  636. run. Thus a less specific \term{around method} runs before a more
  637. specific primary method.
  638. If only primary methods are used and if \funref{call-next-method} is not
  639. used, only the most specific method is invoked; that is, more specific
  640. methods shadow more general ones.
  641. \endsubsubsection%{Standard Method Combination}
  642. \beginsubsubsection{Declarative Method Combination}
  643. The macro \macref{define-method-combination} defines new forms of method
  644. combination. It provides a mechanism for customizing the production
  645. of the effective method. The default procedure for producing an
  646. effective method is described in \secref\DeterminingtheEffectiveMethod.
  647. There are two forms of
  648. \macref{define-method-combination}. The short form is a simple facility while
  649. the long form is more powerful and more verbose. The long form
  650. resembles \macref{defmacro} in that the body is an expression that
  651. computes a Lisp form; it provides mechanisms for implementing
  652. arbitrary control structures within method combination and for
  653. arbitrary processing of method \term{qualifiers}.
  654. \endsubsubsection%{Declarative Method Combination}
  655. \beginsubsubsection{Built-in Method Combination Types}
  656. \DefineSection{BuiltInMethCombTypes}
  657. The \CLOS\ provides a set of built-in method combination types. To
  658. specify that a generic function is to use one of these method
  659. combination types, the name of the method combination type is given as
  660. the argument to the \kwd{method-combination} option to
  661. \macref{defgeneric} or to the \kwd{method-combination} option to any of the
  662. other operators that specify generic function options.
  663. The names of the built-in method combination types are listed in \thenextfigure.
  664. \idxref{+}%
  665. \idxref{and}%
  666. \idxref{append}%
  667. \idxref{list}%
  668. \idxref{max}%
  669. \idxref{min}%
  670. \idxref{nconc}%
  671. \idxref{or}%
  672. \idxref{progn}%
  673. \idxref{standard}
  674. \displayfive{Built-in Method Combination Types}{
  675. +&append&max&nconc&progn\cr
  676. and&list&min&or&standard\cr
  677. }
  678. The semantics of the \misc{standard} built-in method combination type is
  679. described in \secref\StdMethComb. The other
  680. built-in method combination types are called simple built-in method
  681. combination types.
  682. The simple built-in method combination types act as though they were
  683. defined by the short form of \macref{define-method-combination}.
  684. They recognize two roles for \term{methods}:
  685. \beginlist
  686. \itemitem{\bull} An \term{around method} has the keyword symbol
  687. \kwd{around} as its sole \term{qualifier}. The meaning of
  688. \kwd{around} \term{methods} is the same as in standard method combination.
  689. Use of the functions \funref{call-next-method} and \funref{next-method-p}
  690. is supported in \term{around methods}.
  691. \itemitem{\bull} A primary method has the name of the method combination
  692. type as its sole \term{qualifier}. For example, the built-in method
  693. combination type {\tt and} recognizes methods whose sole \term{qualifier} is
  694. {\tt and}; these are primary methods. Use of the functions
  695. \funref{call-next-method} and \funref{next-method-p} is not supported
  696. in \term{primary methods}.
  697. \endlist
  698. The semantics of the simple built-in method combination types is as
  699. follows:
  700. \beginlist
  701. \itemitem{\bull}
  702. If there are any \term{around methods}, the most specific \term{around method}
  703. is called. It supplies the value or values of the \term{generic function}.
  704. \itemitem{\bull} Inside the body of an \term{around method}, the function
  705. \funref{call-next-method} can be used to call the \term{next method}.
  706. % !!!
  707. % Moon: Can't happen, `next page' says signals an error if there are no primaries.
  708. % Barrett: This is a bone of contention. (e.g., no-next-method might -do- the signaling)
  709. \TheGF{no-next-method} is invoked if
  710. \funref{call-next-method} is used and there is no applicable method to call.
  711. \Thefunction{next-method-p} may be used to determine whether a
  712. \term{next method} exists. When the \term{next method} returns,
  713. the \term{around method} can execute more code,
  714. perhaps based on the returned value or values.
  715. \itemitem{\bull} If an \term{around method} invokes \funref{call-next-method},
  716. the next most specific \term{around method} is
  717. called, if one is applicable. If there are no \term{around methods}
  718. or if \funref{call-next-method} is called by the least specific
  719. \term{around method}, a Lisp form derived from the name of the built-in
  720. method combination type and from the list of applicable primary
  721. methods is evaluated to produce the value of the generic function.
  722. Suppose the name of the method combination type is \i{operator}
  723. and the call to the generic function is of the form
  724. $$(\hbox{\i{generic-function}}\ a\sub 1\ldots a\sub n)$$
  725. \itemitem{} Let $M\sub 1,\ldots,M\sub k$ be the applicable primary methods
  726. in order; then the derived Lisp form is
  727. $$(\hbox{\i{operator}}\ \langle M\sub 1%
  728. \ a\sub 1\ldots a\sub n\rangle\ldots\langle%
  729. M\sub k\ a\sub 1\ldots a\sub n\rangle)$$
  730. \itemitem{} If the expression $\langle M\sub i \ a\sub 1\ldots a\sub
  731. n\rangle$ is
  732. evaluated, the method $M\sub i$ will be applied to the arguments
  733. $a\sub 1\ldots a\sub n$.
  734. For example,
  735. if \i{operator} is {\tt or},
  736. the expression $\langle M\sub i \ a\sub 1\ldots a\sub n\rangle$ is
  737. evaluated only if $\langle M\sub j \ a\sub 1\ldots a\sub n\rangle$,
  738. $1\leq j<i$, returned {\tt nil}.
  739. \itemitem{} The default order for the primary methods is
  740. \kwd{most-specific-first}. However, the order can be reversed by supplying
  741. \kwd{most-specific-last} as the second argument to the \kwd{method-combination} option.
  742. \endlist
  743. The simple built-in method combination types require exactly one
  744. \term{qualifier} per method. An error is signaled if there are applicable
  745. methods with no \term{qualifiers} or with \term{qualifiers} that are not supported
  746. by the method combination type. An error is signaled if there are
  747. applicable \term{around methods} and no applicable primary
  748. methods.
  749. \endsubsubsection%{Built-in Method Combination Types}
  750. \endSubsection%{Method Selection and Combination}
  751. \beginSubsection{Inheritance of Methods}
  752. \DefineSection{MethodInheritance}
  753. A subclass inherits methods in the sense that any method applicable to
  754. all instances of a class is also applicable to all instances of any
  755. subclass of that class.
  756. The inheritance of methods acts the same way regardless of
  757. which of the \term{method-defining operators} created the methods.
  758. % whether the
  759. % method was created by using one of the method-defining operators or by
  760. % using one of the \macref{defclass} options that causes methods to be
  761. % generated automatically.
  762. The inheritance of methods is described in detail in
  763. \secref\MethodSelectionAndCombination.
  764. \endSubsection%{Inheritance of Methods}