My various dotfiles

chap-8.texi 42KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170
  1. @node Structures, Conditions, Objects, Top
  2. @chapter Structures
  3. @menu
  4. * Structures Dictionary::
  5. @end menu
  6. @node Structures Dictionary, , Structures, Structures
  7. @section Structures Dictionary
  8. @c including dict-structures
  9. @menu
  10. * defstruct::
  11. * copy-structure::
  12. @end menu
  13. @node defstruct, copy-structure, Structures Dictionary, Structures Dictionary
  14. @subsection defstruct [Macro]
  15. @code{defstruct} @i{name-and-options @r{[}documentation@r{]} @{!@i{slot-description}@}{*}}@*
  16. @result{} @i{structure-name}
  17. @w{@i{name-and-options} ::=structure-name | @r{(}structure-name [[!@i{options}]]@r{)}}
  18. @w{@i{options} ::=!@i{conc-name-option} |}
  19. @w{ @{!@i{constructor-option}@}{*} |}
  20. @w{ !@i{copier-option} |}
  21. @w{ !@i{include-option} |}
  22. @w{ !@i{initial-offset-option} |}
  23. @w{ !@i{named-option} |}
  24. @w{ !@i{predicate-option} |}
  25. @w{ !@i{printer-option} |}
  26. @w{ !@i{type-option}}
  27. @w{@i{conc-name-option} ::=@t{:conc-name} | @r{(}@t{:conc-name}@r{)} | @r{(}@t{:conc-name} @i{conc-name}@r{)}}
  28. @w{@i{constructor-option} ::=@t{:constructor} |}
  29. @w{ @r{(}@t{:constructor}@r{)} |}
  30. @w{ @r{(}@t{:constructor} @i{constructor-name}@r{)} |}
  31. @w{ @r{(}@t{:constructor} @i{constructor-name} @i{constructor-arglist}@r{)}}
  32. @w{@i{copier-option} ::=@t{:copier} | @r{(}@t{:copier}@r{)} | @r{(}@t{:copier} @i{copier-name}@r{)}}
  33. @w{@i{predicate-option} ::=@t{:predicate} | @r{(}@t{:predicate}@r{)} | @r{(}@t{:predicate} @i{predicate-name}@r{)}}
  34. @w{@i{include-option} ::=@r{(}@t{:include} @i{included-structure-name} @{!@i{slot-description}@}{*}@r{)}}
  35. @w{@i{printer-option} ::=!@i{print-object-option} | !@i{print-function-option}}
  36. @w{@i{print-object-option} ::=@r{(}@t{:print-object} @i{printer-name}@r{)} | @r{(}@t{:print-object}@r{)}}
  37. @w{@i{print-function-option} ::=@r{(}@t{:print-function} @i{printer-name}@r{)} | @r{(}@t{:print-function}@r{)}}
  38. @w{@i{type-option} ::=@r{(}@t{:type} @i{type}@r{)}}
  39. @w{@i{named-option} ::=@t{:named}}
  40. @w{@i{initial-offset-option} ::=@r{(}@t{:initial-offset} @i{initial-offset}@r{)}}
  41. @w{@i{slot-description} ::=@i{slot-name} | }
  42. @w{ @r{(}@i{slot-name} @r{[}@i{slot-initform} [[!@i{slot-option}]]@r{]}@r{)}}
  43. @w{@i{slot-option} ::=@t{:type} @i{slot-type} | }
  44. @w{ @t{:read-only} @i{slot-read-only-p}}
  45. @subsubheading Arguments and Values::
  46. @i{conc-name}---a @i{string designator}.
  47. @i{constructor-arglist}---a @i{boa lambda list}.
  48. @i{constructor-name}---a @i{symbol}.
  49. @i{copier-name}---a @i{symbol}.
  50. @i{included-structure-name}---an already-defined @i{structure name}.
  51. Note that a @i{derived type} is not permissible,
  52. even if it would expand into a @i{structure name}.
  53. @i{initial-offset}---a non-negative @i{integer}.
  54. @i{predicate-name}---a @i{symbol}.
  55. @i{printer-name}---a @i{function name} or a @i{lambda expression}.
  56. @i{slot-name}---a @i{symbol}.
  57. @i{slot-initform}---a @i{form}.
  58. @i{slot-read-only-p}---a @i{generalized boolean}.
  59. @i{structure-name}---a @i{symbol}.
  60. @i{type}---one of the @i{type specifiers}
  61. @b{list},
  62. @b{vector},
  63. or @t{(vector @i{size})},
  64. or some other @i{type specifier} defined
  65. by the @i{implementation} to be appropriate.
  66. @i{documentation}---a @i{string}; not evaluated.
  67. @subsubheading Description::
  68. @b{defstruct} defines a structured @i{type}, named @i{structure-type},
  69. with named slots as specified by the @i{slot-options}.
  70. @b{defstruct} defines @i{readers} for the slots and
  71. arranges for @b{setf} to work properly on such
  72. @i{reader} functions.
  73. Also, unless overridden, it
  74. defines a predicate named @t{@i{name}-p},
  75. defines a constructor function named @t{make-@i{constructor-name}},
  76. and defines a copier function named @t{copy-@i{constructor-name}}.
  77. All names of automatically created functions might automatically
  78. be declared @b{inline} (at the discretion of the @i{implementation}).
  79. If @i{documentation} is supplied, it is attached to @i{structure-name}
  80. as a @i{documentation string} of kind @b{structure},
  81. and unless @t{:type} is used, the @i{documentation} is also attached
  82. to @i{structure-name} as a @i{documentation string} of kind
  83. @b{type} and as a @i{documentation string} to the @i{class} @i{object}
  84. for the @i{class} named @i{structure-name}.
  85. @b{defstruct} defines a constructor function that is used to
  86. create instances of the structure created by @b{defstruct}.
  87. The default name is @t{make-@i{structure-name}}.
  88. A different name can be supplied
  89. by giving the name as the argument to the @i{constructor} option.
  90. @b{nil} indicates that no constructor function will be created.
  91. After a new structure type has been defined, instances of that type
  92. normally can be created by using the constructor function for the
  93. type.
  94. A call to a constructor function is of the following form:
  95. @w{ (@t{constructor-function-name}}@*
  96. @w{ @t{slot-keyword-1 form-1}}@*
  97. @w{ @t{slot-keyword-2 form-2}}@*
  98. @w{ ...)}@*
  99. The arguments to the constructor function are all keyword arguments. Each
  100. slot keyword argument must be
  101. a keyword whose name corresponds to the name of a structure slot.
  102. All the @i{keywords} and @i{forms}
  103. are evaluated.
  104. If a slot is not initialized in this way,
  105. it is initialized by evaluating @i{slot-initform} in the slot description
  106. at the time the constructor function is called.
  107. If no @i{slot-initform} is supplied,
  108. the consequences are undefined if an attempt is later made to read the slot's value
  109. before a value is explicitly assigned.
  110. Each @i{slot-initform} supplied for a @b{defstruct} component,
  111. when used by the constructor function for an otherwise unsupplied
  112. component, is re-evaluated on every call to the
  113. constructor function.
  114. The @i{slot-initform} is not evaluated
  115. unless it is needed in the creation of a particular structure
  116. instance. If it is never needed, there can be no type-mismatch
  117. error, even if the @i{type}
  118. of the slot is specified; no warning
  119. should be issued in this case.
  120. For example, in the following sequence, only the last call is an error.
  121. @example
  122. (defstruct person (name 007 :type string))
  123. (make-person :name "James")
  124. (make-person)
  125. @end example
  126. It is as if the @i{slot-initforms} were
  127. used as @i{initialization forms} for the @i{keyword parameters}
  128. of the constructor function.
  129. The @i{symbols} which name the slots must not be used by the
  130. @i{implementation} as the @i{names} for the @i{lambda variables}
  131. in the constructor function, since one or more of those @i{symbols}
  132. might have been proclaimed @b{special} or might be defined as
  133. the name of a @i{constant variable}.
  134. The slot default init forms are evaluated
  135. in the @i{lexical environment} in which the @b{defstruct} form itself appears and
  136. in the @i{dynamic environment} in which the call to the constructor function appears.
  137. For example, if the form @t{(gensym)} were used as an initialization form,
  138. either in the constructor-function call or as the default initialization form
  139. in @b{defstruct}, then every call to the constructor function would call
  140. @b{gensym} once to generate a new @i{symbol}.
  141. Each @i{slot-description} in @b{defstruct} can specify zero or more
  142. @i{slot-options}.
  143. A @i{slot-option} consists of a pair of a keyword and a value
  144. (which is not a form to be evaluated, but the value itself). For example:
  145. @example
  146. (defstruct ship
  147. (x-position 0.0 :type short-float)
  148. (y-position 0.0 :type short-float)
  149. (x-velocity 0.0 :type short-float)
  150. (y-velocity 0.0 :type short-float)
  151. (mass *default-ship-mass* :type short-float :read-only t))
  152. @end example
  153. This specifies that each slot always contains a @i{short float},
  154. and that the last slot cannot be altered once a ship is constructed.
  155. The available slot-options are:
  156. @table @asis
  157. @item @t{:type} @i{type}
  158. This specifies that the contents of the
  159. slot is always of type @i{type}. This is entirely
  160. analogous to the declaration of a variable or function; it
  161. effectively declares the result type of the @i{reader} function.
  162. It is @i{implementation-dependent} whether the @i{type} is checked
  163. when initializing a slot
  164. or when assigning to it.
  165. @i{Type} is not evaluated; it must be a valid @i{type specifier}.
  166. @item @t{:read-only} @i{x}
  167. When @i{x} is @i{true},
  168. this specifies that this slot cannot be
  169. altered; it will always contain the value supplied at construction time.
  170. @b{setf} will not accept the @i{reader} function for this slot.
  171. If @i{x} is @i{false}, this slot-option has no effect.
  172. @i{X} is not evaluated.
  173. When this option is @i{false} or unsupplied,
  174. it is @i{implementation-dependent} whether the ability to @i{write}
  175. the slot is implemented by a @i{setf function} or a @i{setf expander}.
  176. @end table
  177. The following keyword options are available for use with @b{defstruct}.
  178. A @b{defstruct} option can be either a keyword or a @i{list}
  179. of a keyword and arguments for that keyword;
  180. specifying the keyword by itself is equivalent to specifying a list consisting of
  181. the keyword and no arguments.
  182. The syntax for @b{defstruct} options differs from the pair syntax
  183. used for slot-options. No part of any of these options is evaluated.
  184. @table @asis
  185. @item @t{:conc-name}
  186. This provides for automatic prefixing of names of @i{reader} (or @i{access}) functions.
  187. The default behavior is to begin the names of all the @i{reader} functions of
  188. a structure with the name of the structure followed by a hyphen.
  189. @t{:conc-name} supplies an alternate
  190. prefix to be used. If a hyphen is to be used as a separator,
  191. it must be supplied as part of the prefix.
  192. If @t{:conc-name} is @b{nil} or no argument is supplied,
  193. then no prefix is used;
  194. then the names of the @i{reader} functions
  195. are the same as the slot names.
  196. If a @i{non-nil} prefix is given,
  197. the name of the @i{reader} @i{function} for each slot is constructed by
  198. concatenating that prefix and the name of the slot, and interning the resulting
  199. @i{symbol} in the @i{package} that is current at the time the
  200. @b{defstruct} form is expanded.
  201. Note that no matter what is supplied for @t{:conc-name},
  202. slot keywords that match the slot names with no prefix attached are used
  203. with a constructor function.
  204. The @i{reader} function name is used
  205. in conjunction with @b{setf}. Here is an example:
  206. @example
  207. (defstruct (door (:conc-name dr-)) knob-color width material) @result{} DOOR
  208. (setq my-door (make-door :knob-color 'red :width 5.0))
  209. @result{} #S(DOOR :KNOB-COLOR RED :WIDTH 5.0 :MATERIAL NIL)
  210. (dr-width my-door) @result{} 5.0
  211. (setf (dr-width my-door) 43.7) @result{} 43.7
  212. (dr-width my-door) @result{} 43.7
  213. @end example
  214. Whether or not the @t{:conc-name} option is explicitly supplied,
  215. the following rule governs name conflicts of generated @i{reader}
  216. (or @i{accessor}) names:
  217. For any @i{structure} @i{type} S_1
  218. having a @i{reader} function named R for a slot named X_1
  219. that is inherited by another @i{structure} @i{type} S_2
  220. that would have a @i{reader} function with the same name R for a slot named X_2,
  221. no definition for R is generated by the definition of S_2;
  222. instead, the definition of R is inherited from the definition of S_1.
  223. (In such a case, if X_1 and X_2 are different slots,
  224. the @i{implementation} might signal a style warning.)
  225. @item @t{:constructor}
  226. This option takes zero, one, or two arguments.
  227. If at least one argument is supplied and the first argument is not @b{nil}, then
  228. that argument is a @i{symbol} which specifies the name of the
  229. constructor function. If the argument is not supplied (or if the option itself is not
  230. supplied), the name of the constructor is produced by concatenating the
  231. string @t{"MAKE-"} and the name of the structure, interning the name
  232. in whatever @i{package} is current at the time @b{defstruct}
  233. is expanded. If the argument is provided and is @b{nil},
  234. no constructor function is defined.
  235. If @t{:constructor} is given as
  236. @t{(:constructor @i{name} @i{arglist})},
  237. then instead of making a keyword
  238. driven constructor function, @b{defstruct}
  239. defines a ``positional'' constructor function,
  240. taking arguments whose meaning is determined by the argument's position
  241. and possibly by keywords.
  242. @i{Arglist} is used to describe what the arguments to the
  243. constructor will be. In the simplest case something like
  244. @t{(:constructor make-foo (a b c))} defines @t{make-foo} to be
  245. a three-argument
  246. constructor function whose arguments are used to initialize the
  247. slots named @t{a}, @t{b}, and @t{c}.
  248. Because a constructor of this type operates ``By Order of Arguments,''
  249. it is sometimes known as a ``boa constructor.''
  250. For information on how the @i{arglist} for a ``boa constructor'' is
  251. processed, see @ref{Boa Lambda Lists}.
  252. It is permissible to use the
  253. @t{:constructor} option more than once, so that you can define several
  254. different constructor functions, each taking different parameters.
  255. [Reviewer Note by Barmar: What about (:constructor) and (:constructor nil).
  256. Should we worry about it?]
  257. @b{defstruct} creates the default-named keyword constructor function
  258. only if no explicit @t{:constructor} options are specified, or if the
  259. @t{:constructor} option is specified without a @i{name} argument.
  260. @t{(:constructor nil)} is meaningful only when there are no other
  261. @t{:constructor} options specified. It prevents @b{defstruct}
  262. from generating any constructors at all.
  263. Otherwise, @b{defstruct} creates a constructor function corresponding
  264. to each supplied @t{:constructor} option. It is permissible to specify
  265. multiple keyword constructor functions as well as multiple
  266. ``boa constructors''.
  267. @item @t{:copier}
  268. This option takes one argument, a @i{symbol},
  269. which specifies the name of the copier
  270. function. If the argument is not provided or if the option itself is not
  271. provided, the name of the copier is produced by concatenating the
  272. string @t{"COPY-"} and the name of the structure, interning the name
  273. in whatever @i{package} is current at the time @b{defstruct}
  274. is expanded.
  275. If the argument is provided and is @b{nil}, no copier function is defined.
  276. The automatically defined copier function is a function of
  277. one @i{argument},
  278. which must be of the structure type being defined.
  279. The copier function creates a @i{fresh}
  280. structure that has the same @i{type} as its @i{argument},
  281. and that has the @i{same} component values as the original
  282. structure; that is, the component values are not copied recursively.
  283. If the @b{defstruct} @t{:type} option was not used,
  284. the following equivalence applies:
  285. @example
  286. (@i{copier-name} x) = (copy-structure (the @i{structure-name} x))
  287. @end example
  288. @item @t{:include}
  289. This option is used for building a new structure definition as
  290. an extension of another structure definition. For example:
  291. @example
  292. (defstruct person name age sex)
  293. @end example
  294. To make a new structure to represent an astronaut
  295. that has the
  296. attributes of name, age, and sex, and @i{functions}
  297. that operate on @t{person} structures, @t{astronaut} is defined
  298. with @t{:include} as follows:
  299. @example
  300. (defstruct (astronaut (:include person)
  301. (:conc-name astro-))
  302. helmet-size
  303. (favorite-beverage 'tang))
  304. @end example
  305. @t{:include} causes the structure being defined
  306. to have the same slots as the included structure.
  307. This is done in such a way
  308. that the @i{reader} functions for the included
  309. structure also work on the structure being defined.
  310. In this example, an
  311. @t{astronaut} therefore has five slots: the three defined in
  312. @t{person} and the two defined in @t{astronaut}
  313. itself. The @i{reader} functions defined by the @t{person} structure
  314. can be applied to instances of the @t{astronaut} structure, and they
  315. work correctly.
  316. Moreover, @t{astronaut} has its own @i{reader} functions for
  317. components defined by the @t{person} structure.
  318. The following examples illustrate the
  319. use of @t{astronaut} structures:
  320. @example
  321. (setq x (make-astronaut :name 'buzz
  322. :age 45.
  323. :sex t
  324. :helmet-size 17.5))
  325. (person-name x) @result{} BUZZ
  326. (astro-name x) @result{} BUZZ
  327. (astro-favorite-beverage x) @result{} TANG
  328. @end example
  329. @example
  330. (reduce #'+ astros :key #'person-age) ; obtains the total of the ages
  331. ; of the possibly empty
  332. ; sequence of astros
  333. @end example
  334. The difference between the @i{reader} functions @t{person-name} and @t{astro-name}
  335. is that @t{person-name} can be correctly applied to any @t{person},
  336. including an @t{astronaut}, while @t{astro-name} can be correctly
  337. applied only to an @t{astronaut}. An implementation might
  338. check for incorrect use of @i{reader} functions.
  339. At most one @t{:include} can be supplied in a single @b{defstruct}.
  340. The argument to @t{:include} is required and must be the
  341. name of some previously defined structure. If the structure being
  342. defined has no @t{:type} option, then the included structure must
  343. also have had no @t{:type} option supplied for it.
  344. If the structure being defined has a @t{:type} option,
  345. then the included structure must have been declared with a @t{:type}
  346. option specifying the same representation @i{type}.
  347. If no @t{:type} option is involved, then
  348. the structure name of the including structure definition
  349. becomes the name of a @i{data type}, and therefore
  350. a valid @i{type specifier} recognizable by @b{typep}; it becomes
  351. a @i{subtype} of the included structure.
  352. In the above example,
  353. @t{astronaut} is a @i{subtype} of @t{person}; hence
  354. @example
  355. (typep (make-astronaut) 'person) @result{} @i{true}
  356. @end example
  357. indicating that all operations on persons also
  358. work on astronauts.
  359. The structure using @t{:include} can specify default values or
  360. slot-options for the included slots different from those the included
  361. structure specifies, by giving the @t{:include} option as:
  362. @example
  363. (:include @i{included-structure-name} @{@i{slot-description}@}{*})
  364. @end example
  365. Each @i{slot-description} must have a @i{slot-name}
  366. that is the same
  367. as that of some slot in the included structure.
  368. If a @i{slot-description} has no @i{slot-initform},
  369. then in the new structure the slot has no initial value.
  370. Otherwise its initial value form is replaced by
  371. the @i{slot-initform} in the @i{slot-description}.
  372. A normally writable slot can be made read-only.
  373. If a slot is read-only in the included structure, then it
  374. must also be so in the including structure.
  375. If a @i{type} is supplied for a slot, it must be
  376. a @i{subtype} of
  377. the
  378. @i{type} specified in the included structure.
  379. For example, if the
  380. default age for an astronaut is @t{45}, then
  381. @example
  382. (defstruct (astronaut (:include person (age 45)))
  383. helmet-size
  384. (favorite-beverage 'tang))
  385. @end example
  386. If @t{:include} is used with the @t{:type}
  387. option, then the effect is first to skip over as many representation
  388. elements as needed to represent the included structure, then to
  389. skip over any additional elements supplied by the @t{:initial-offset}
  390. option, and then to begin allocation of elements from that point.
  391. For example:
  392. @example
  393. (defstruct (binop (:type list) :named (:initial-offset 2))
  394. (operator '? :type symbol)
  395. operand-1
  396. operand-2) @result{} BINOP
  397. (defstruct (annotated-binop (:type list)
  398. (:initial-offset 3)
  399. (:include binop))
  400. commutative associative identity) @result{} ANNOTATED-BINOP
  401. (make-annotated-binop :operator '*
  402. :operand-1 'x
  403. :operand-2 5
  404. :commutative t
  405. :associative t
  406. :identity 1)
  407. @result{} (NIL NIL BINOP * X 5 NIL NIL NIL T T 1)
  408. @end example
  409. The first two @b{nil} elements stem from the @t{:initial-offset} of @t{2}
  410. in the definition of @t{binop}. The next four elements contain the
  411. structure name and three slots for @t{binop}. The next three @b{nil} elements
  412. stem from the @t{:initial-offset} of @t{3} in the definition of
  413. @t{annotated-binop}. The last three list elements contain the additional
  414. slots for an @t{annotated-binop}.
  415. @item @t{:initial-offset}
  416. @t{:initial-offset} instructs @b{defstruct} to skip over a certain
  417. number of slots before it starts allocating the slots described in the
  418. body. This option's argument is the number of slots @b{defstruct}
  419. should skip. @t{:initial-offset} can be used only if @t{:type} is also supplied.
  420. [Reviewer Note by Barmar: What are initial values of the skipped slots?]
  421. @t{:initial-offset} allows
  422. slots to be allocated beginning at a representational
  423. element other than the first. For example, the form
  424. @example
  425. (defstruct (binop (:type list) (:initial-offset 2))
  426. (operator '? :type symbol)
  427. operand-1
  428. operand-2) @result{} BINOP
  429. @end example
  430. would result in the following behavior for @t{make-binop}:
  431. @example
  432. (make-binop :operator '+ :operand-1 'x :operand-2 5)
  433. @result{} (NIL NIL + X 5)
  434. (make-binop :operand-2 4 :operator '*)
  435. @result{} (NIL NIL * NIL 4)
  436. @end example
  437. The selector functions
  438. @t{binop-operator}, @t{binop-operand-1},
  439. and @t{binop-operand-2} would be essentially equivalent to @b{third},
  440. @b{fourth}, and @b{fifth}, respectively.
  441. Similarly, the form
  442. @example
  443. (defstruct (binop (:type list) :named (:initial-offset 2))
  444. (operator '? :type symbol)
  445. operand-1
  446. operand-2) @result{} BINOP
  447. @end example
  448. would result in the following behavior for @t{make-binop}:
  449. @example
  450. (make-binop :operator '+ :operand-1 'x :operand-2 5) @result{} (NIL NIL BINOP + X 5)
  451. (make-binop :operand-2 4 :operator '*) @result{} (NIL NIL BINOP * NIL 4)
  452. @end example
  453. The first two @b{nil} elements stem from the @t{:initial-offset} of @t{2}
  454. in the definition of @t{binop}. The next four elements contain the
  455. structure name and three slots for @t{binop}.
  456. @item @t{:named}
  457. @t{:named} specifies that the structure is named.
  458. If no @t{:type} is supplied,
  459. then the structure is always named.
  460. For example:
  461. @example
  462. (defstruct (binop (:type list))
  463. (operator '? :type symbol)
  464. operand-1
  465. operand-2) @result{} BINOP
  466. @end example
  467. This defines a constructor function @t{make-binop} and three
  468. selector functions, namely @t{binop-operator}, @t{binop-operand-1},
  469. and @t{binop-operand-2}. (It does not, however, define a predicate
  470. @t{binop-p}, for reasons explained below.)
  471. The effect of @t{make-binop} is simply to construct a list of length three:
  472. @example
  473. (make-binop :operator '+ :operand-1 'x :operand-2 5) @result{} (+ X 5)
  474. (make-binop :operand-2 4 :operator '*) @result{} (* NIL 4)
  475. @end example
  476. It is just like the function @t{list} except that it takes
  477. keyword arguments and performs slot defaulting appropriate to the @t{binop}
  478. conceptual data type. Similarly, the selector functions
  479. @t{binop-operator}, @t{binop-operand-1},
  480. and @t{binop-operand-2} are essentially equivalent to @b{car},
  481. @b{cadr}, and @b{caddr}, respectively. They might not be
  482. completely equivalent because,
  483. for example, an implementation would be justified in adding error-checking
  484. code to ensure that the argument to each selector function is a length-3
  485. list.
  486. @t{binop} is a conceptual data type in that it is not made a part of
  487. the @r{Common Lisp} type system. @b{typep} does not recognize @t{binop} as
  488. a @i{type specifier}, and @b{type-of} returns @t{list} when
  489. given a @t{binop} structure. There is no way to distinguish a data
  490. structure constructed by @t{make-binop} from any other @i{list} that
  491. happens to have the correct structure.
  492. There is not any way to recover the structure name @t{binop} from
  493. a structure created by @t{make-binop}. This can only be done
  494. if the structure is named.
  495. A named structure has the property that, given an instance of the
  496. structure, the structure name (that names the type) can be reliably
  497. recovered. For structures defined
  498. with no @t{:type} option, the structure name actually becomes part
  499. of the @r{Common Lisp} data-type system. @b{type-of},
  500. when applied to such a structure, returns the structure name
  501. as the @i{type} of the @i{object};
  502. @b{typep} recognizes
  503. the structure name as a valid @i{type specifier}.
  504. For structures defined with a @t{:type} option, @b{type-of}
  505. returns a @i{type specifier} such as @t{list} or @t{(vector t)},
  506. depending on the type supplied to the @t{:type} option.
  507. The structure name does not become a valid @i{type specifier}.
  508. However,
  509. if the @t{:named} option is also supplied, then the first component
  510. of the structure (as created by a @b{defstruct} constructor function)
  511. always contains the structure name. This allows the structure name
  512. to be recovered from an instance of the structure and allows a reasonable
  513. predicate for the conceptual type to be defined:
  514. the automatically defined
  515. @i{name-p} predicate for the structure operates by first
  516. checking that its argument is of the proper type (@b{list},
  517. @t{(vector t)},
  518. or whatever) and then checking whether the first component contains
  519. the appropriate type name.
  520. Consider the @t{binop} example shown above, modified only to
  521. include the @t{:named} option:
  522. @example
  523. (defstruct (binop (:type list) :named)
  524. (operator '? :type symbol)
  525. operand-1
  526. operand-2) @result{} BINOP
  527. @end example
  528. As before, this defines a constructor function @t{make-binop} and three
  529. selector functions @t{binop-operator}, @t{binop-operand-1},
  530. and @t{binop-operand-2}. It also defines a predicate @t{binop-p}.
  531. The effect of @t{make-binop} is now to construct a list of length four:
  532. @example
  533. (make-binop :operator '+ :operand-1 'x :operand-2 5) @result{} (BINOP + X 5)
  534. (make-binop :operand-2 4 :operator '*) @result{} (BINOP * NIL 4)
  535. @end example
  536. The structure has the same layout as before except that the structure name
  537. @t{binop} is included as the first list element.
  538. The selector functions
  539. @t{binop-operator}, @t{binop-operand-1},
  540. and @t{binop-operand-2} are essentially equivalent to @b{cadr},
  541. @b{caddr}, and @b{cadddr}, respectively.
  542. The predicate @t{binop-p} is more or less equivalent to this
  543. definition:
  544. @example
  545. (defun binop-p (x)
  546. (and (consp x) (eq (car x) 'binop))) @result{} BINOP-P
  547. @end example
  548. The name @t{binop} is still not a valid @i{type specifier} recognizable
  549. to @b{typep}, but at least there is a way of distinguishing @t{binop}
  550. structures from other similarly defined structures.
  551. @item @t{:predicate}
  552. This option takes one argument, which specifies the name of the type predicate.
  553. If the argument is not supplied or if the option itself is not
  554. supplied, the name of the predicate is made by concatenating the
  555. name of the structure to the string @t{"-P"}, interning the name
  556. in whatever @i{package} is current at the time @b{defstruct}
  557. is expanded.
  558. If the argument is provided and is @b{nil}, no predicate is defined.
  559. A predicate can be defined only if the structure is named;
  560. if @t{:type} is supplied and @t{:named} is not supplied,
  561. then @t{:predicate} must either be unsupplied or have the value @b{nil}.
  562. @item @t{:print-function}, @t{:print-object}
  563. The @t{:print-function} and @t{:print-object}
  564. options
  565. specify that a @b{print-object}
  566. @i{method} for @i{structures} of type @i{structure-name} should be generated.
  567. These options are not synonyms, but do perform a similar service;
  568. the choice of which option (@t{:print-function} or @t{:print-object}) is used
  569. affects how the function named @i{printer-name} is called.
  570. Only one of these options may be used, and
  571. these options may be used only if @t{:type} is not supplied.
  572. If the @t{:print-function} option is used,
  573. then when a structure of type @i{structure-name} is to be printed,
  574. the designated printer function is called on three @i{arguments}:
  575. @table @asis
  576. @item --
  577. the structure to be printed
  578. (a @i{generalized instance} of @i{structure-name}).
  579. @item --
  580. a @i{stream} to print to.
  581. @item --
  582. an @i{integer} indicating the current depth.
  583. The magnitude of this integer may vary between @i{implementations};
  584. however, it can reliably be compared against @b{*print-level*}
  585. to determine whether depth abbreviation is appropriate.
  586. @end table
  587. Specifying @t{(:print-function @i{printer-name})}
  588. is approximately equivalent to specifying:
  589. @example
  590. (defmethod print-object ((object @i{structure-name}) stream)
  591. (funcall (function @i{printer-name}) object stream <<@i{current-print-depth}>>))
  592. @end example
  593. where the <<@i{current-print-depth}>> represents the printer's belief of
  594. how deep it is currently printing. It is @i{implementation-dependent}
  595. whether <<@i{current-print-depth}>> is always 0 and @i{*print-level*},
  596. if @i{non-nil}, is re-bound to successively smaller values as printing
  597. descends recursively, or whether @i{current-print-depth} varies in
  598. value as printing descends recursively and @i{*print-level*} remains
  599. constant during the same traversal.
  600. If the @t{:print-object} option is used, then
  601. when a structure of type @i{structure-name} is to be printed,
  602. the designated printer function is called on two arguments:
  603. @table @asis
  604. @item --
  605. the structure to be printed.
  606. @item --
  607. the stream to print to.
  608. @end table
  609. Specifying @t{(:print-object @i{printer-name})} is equivalent to specifying:
  610. @example
  611. (defmethod print-object ((object @i{structure-name}) stream)
  612. (funcall (function @i{printer-name}) object stream))
  613. @end example
  614. If no @t{:type} option is supplied,
  615. and if either a @t{:print-function} or a @t{:print-object} option is supplied,
  616. and if no @i{printer-name} is supplied,
  617. then a @b{print-object} @i{method} @i{specialized} for @i{structure-name}
  618. is generated that calls a function that implements the default printing behavior for
  619. structures using @t{#S} notation; see @ref{Printing Structures}.
  620. If neither a @t{:print-function}
  621. nor a @t{:print-object} option
  622. is supplied,
  623. then @b{defstruct} does not generate a @b{print-object} @i{method}
  624. @i{specialized} for @i{structure-name} and some default behavior is inherited
  625. either from a structure named in an @t{:include} option
  626. or from the default behavior for printing structures;
  627. see the @i{function} @b{print-object} and @ref{Printing Structures}.
  628. When @b{*print-circle*} is @i{true},
  629. a user-defined print function can print @i{objects}
  630. to the supplied @i{stream} using
  631. @b{write},
  632. @b{prin1},
  633. @b{princ},
  634. or @b{format}
  635. and expect circularities to be detected and printed using the @t{#@i{n}#} syntax.
  636. This applies to @i{methods} on @b{print-object} in addition to
  637. @t{:print-function} options.
  638. If a user-defined print function prints to a @i{stream} other than the one
  639. that was supplied, then circularity detection starts over for that @i{stream}.
  640. See the @i{variable} @b{*print-circle*}.
  641. @item @t{:type}
  642. @t{:type} explicitly specifies the representation to be used for
  643. the structure. Its argument must be one of these @i{types}:
  644. @table @asis
  645. @item @b{vector}
  646. This produces the same result as specifying @t{(vector t)}.
  647. The structure is represented
  648. as a general @i{vector}, storing components as vector elements.
  649. The first component is vector
  650. element 1 if the structure is @t{:named}, and element 0 otherwise.
  651. [Reviewer Note by Barmar: Do any implementations create non-simple vectors?]
  652. @item @t{(vector @i{element-type})}
  653. The structure is represented as a (possibly specialized) @i{vector}, storing
  654. components as vector elements. Every component must be of a @i{type}
  655. that can be stored in a @i{vector} of the @i{type} specified.
  656. The first component is vector
  657. element 1 if the structure is @t{:named}, and element 0 otherwise.
  658. The structure can be @t{:named} only if the @i{type} @b{symbol}
  659. is a @i{subtype} of the supplied @i{element-type}.
  660. @item @b{list}
  661. The structure is represented as a @i{list}.
  662. The first component is the @i{cadr} if the structure is @t{:named},
  663. and the @i{car} if it is not @t{:named}.
  664. @end table
  665. Specifying this option has the effect of forcing
  666. a specific representation and of forcing the components to be
  667. stored in the order specified in @b{defstruct}
  668. in corresponding successive elements of the specified representation.
  669. It also prevents the structure name from becoming a valid
  670. @i{type specifier} recognizable by @b{typep}.
  671. For example:
  672. @example
  673. (defstruct (quux (:type list) :named) x y)
  674. @end example
  675. should make a constructor that builds a @i{list} exactly like the one
  676. that @b{list} produces,
  677. with @t{quux} as its @i{car}.
  678. If this type is defined:
  679. @example
  680. (deftype quux () '(satisfies quux-p))
  681. @end example
  682. then this form
  683. @example
  684. (typep (make-quux) 'quux)
  685. @end example
  686. should return precisely what this one does
  687. @example
  688. (typep (list 'quux nil nil) 'quux)
  689. @end example
  690. If @t{:type} is not supplied,
  691. the structure is represented as an @i{object} of @i{type} @b{structure-object}.
  692. @b{defstruct} without a @t{:type} option defines a @i{class} with
  693. the structure name as its name. The @i{metaclass} of structure
  694. @i{instances} is @b{structure-class}.
  695. @end table
  696. The consequences of redefining a @b{defstruct} structure are undefined.
  697. In the case where no @b{defstruct} options have been supplied,
  698. the following functions are automatically defined to operate
  699. on instances of the new structure:
  700. @table @asis
  701. @item Predicate
  702. A predicate with the name @t{@i{structure-name}-p} is defined to
  703. test membership in the structure type. The predicate
  704. @t{(@i{structure-name}-p @i{object})} is @i{true} if an @i{object}
  705. is of this @i{type}; otherwise it is @i{false}. @b{typep} can also
  706. be used with the name of the new @i{type} to test whether an
  707. @i{object}
  708. belongs to the @i{type}.
  709. Such a function call has the form
  710. @t{(typep @i{object} '@i{structure-name})}.
  711. @item Component reader functions
  712. @i{Reader} functions are defined to @i{read} the components of the
  713. structure. For each slot name, there is a corresponding
  714. @i{reader} function with the name @t{@i{structure-name}-@i{slot-name}}.
  715. This function @i{reads} the contents of that slot.
  716. Each @i{reader} function takes one argument, which is
  717. an instance of the structure type.
  718. @b{setf} can be used with any of these @i{reader} functions
  719. to alter the slot contents.
  720. @item Constructor function
  721. A constructor function with the name @t{make-@i{structure-name}}
  722. is defined. This function creates and returns new
  723. instances of the structure type.
  724. @item Copier function
  725. A copier function with the name @t{copy-@i{structure-name}} is defined.
  726. The copier function takes an object of the structure type and creates a
  727. new object of the same type that is a copy of the first. The copier
  728. function creates a new structure with the same component entries
  729. as the original. Corresponding components of the two structure instances
  730. are @b{eql}.
  731. @end table
  732. If a @b{defstruct} @i{form} appears as a @i{top level form},
  733. the @i{compiler} must make the @i{structure} @i{type} name recognized
  734. as a valid @i{type} name in subsequent declarations (as for @b{deftype})
  735. and make the structure slot readers known to @b{setf}. In addition, the
  736. @i{compiler} must save enough information about the @i{structure} @i{type}
  737. so that further @b{defstruct} definitions can use @t{:include} in a subsequent
  738. @b{deftype} in the same @i{file} to refer to the @i{structure} @i{type} name.
  739. The functions which @b{defstruct} generates are not defined
  740. in the compile time environment, although the @i{compiler} may save enough
  741. information about the functions to code subsequent calls inline.
  742. The @t{#S} @i{reader macro} might or might not recognize the newly defined
  743. @i{structure} @i{type} name at compile time.
  744. @subsubheading Examples::
  745. An example of a structure definition follows:
  746. @example
  747. (defstruct ship
  748. x-position
  749. y-position
  750. x-velocity
  751. y-velocity
  752. mass)
  753. @end example
  754. This declares that every @t{ship} is an @i{object}
  755. with five named components.
  756. The evaluation of this form does the following:
  757. @table @asis
  758. @item 1.
  759. It defines @t{ship-x-position} to be a function
  760. of one argument, a ship, that returns the @t{x-position}
  761. of the ship; @t{ship-y-position}
  762. and the other components are given similar function definitions.
  763. These functions are called the @i{access} functions, as they
  764. are used to @i{access} elements of the structure.
  765. @item 2.
  766. @t{ship} becomes the name of a @i{type} of which instances
  767. of ships are elements. @t{ship} becomes acceptable to @b{typep},
  768. for example; @t{(typep x 'ship)} is @i{true} if @t{x} is a ship
  769. and false if @t{x} is any @i{object} other than a ship.
  770. @item 3.
  771. A function named @t{ship-p} of
  772. one argument is defined; it is a predicate
  773. that is @i{true} if its argument is a ship and is @i{false} otherwise.
  774. @item 4.
  775. A function called @t{make-ship} is defined that, when invoked,
  776. creates a data structure with five components, suitable for use with
  777. the @i{access} functions. Thus executing
  778. @example
  779. (setq ship2 (make-ship))
  780. @end example
  781. sets @t{ship2} to a newly created @t{ship} @i{object}.
  782. One can supply the initial values of any desired component in the call
  783. to @t{make-ship} by using keyword arguments in this way:
  784. @example
  785. (setq ship2 (make-ship :mass *default-ship-mass*
  786. :x-position 0
  787. :y-position 0))
  788. @end example
  789. This constructs a new ship and initializes three of its components.
  790. This function is called the ``constructor function''
  791. because it constructs a new structure.
  792. @item 5.
  793. A function called @t{copy-ship} of one argument
  794. is defined that, when given a @t{ship} @i{object},
  795. creates a new @t{ship} @i{object} that is a copy of the given one.
  796. This function is called the ``copier function.''
  797. @end table
  798. @b{setf} can be used to alter the components of a @t{ship}:
  799. @example
  800. (setf (ship-x-position ship2) 100)
  801. @end example
  802. This alters the @t{x-position} of @t{ship2} to be @t{100}.
  803. This works because @b{defstruct} behaves as if
  804. it generates an appropriate @b{defsetf}
  805. for each @i{access} function.
  806. @example
  807. ;;;
  808. ;;; Example 1
  809. ;;; define town structure type
  810. ;;; area, watertowers, firetrucks, population, elevation are its components
  811. ;;;
  812. (defstruct town
  813. area
  814. watertowers
  815. (firetrucks 1 :type fixnum) ;an initialized slot
  816. population
  817. (elevation 5128 :read-only t)) ;a slot that can't be changed
  818. @result{} TOWN
  819. ;create a town instance
  820. (setq town1 (make-town :area 0 :watertowers 0)) @result{} #S(TOWN...)
  821. ;town's predicate recognizes the new instance
  822. (town-p town1) @result{} @i{true}
  823. ;new town's area is as specified by make-town
  824. (town-area town1) @result{} 0
  825. ;new town's elevation has initial value
  826. (town-elevation town1) @result{} 5128
  827. ;setf recognizes reader function
  828. (setf (town-population town1) 99) @result{} 99
  829. (town-population town1) @result{} 99
  830. ;copier function makes a copy of town1
  831. (setq town2 (copy-town town1)) @result{} #S(TOWN...)
  832. (= (town-population town1) (town-population town2)) @result{} @i{true}
  833. ;since elevation is a read-only slot, its value can be set only
  834. ;when the structure is created
  835. (setq town3 (make-town :area 0 :watertowers 3 :elevation 1200))
  836. @result{} #S(TOWN...)
  837. ;;;
  838. ;;; Example 2
  839. ;;; define clown structure type
  840. ;;; this structure uses a nonstandard prefix
  841. ;;;
  842. (defstruct (clown (:conc-name bozo-))
  843. (nose-color 'red)
  844. frizzy-hair-p polkadots) @result{} CLOWN
  845. (setq funny-clown (make-clown)) @result{} #S(CLOWN)
  846. ;use non-default reader name
  847. (bozo-nose-color funny-clown) @result{} RED
  848. (defstruct (klown (:constructor make-up-klown) ;similar def using other
  849. (:copier clone-klown) ;customizing keywords
  850. (:predicate is-a-bozo-p))
  851. nose-color frizzy-hair-p polkadots) @result{} klown
  852. ;custom constructor now exists
  853. (fboundp 'make-up-klown) @result{} @i{true}
  854. ;;;
  855. ;;; Example 3
  856. ;;; define a vehicle structure type
  857. ;;; then define a truck structure type that includes
  858. ;;; the vehicle structure
  859. ;;;
  860. (defstruct vehicle name year (diesel t :read-only t)) @result{} VEHICLE
  861. (defstruct (truck (:include vehicle (year 79)))
  862. load-limit
  863. (axles 6)) @result{} TRUCK
  864. (setq x (make-truck :name 'mac :diesel t :load-limit 17))
  865. @result{} #S(TRUCK...)
  866. ;vehicle readers work on trucks
  867. (vehicle-name x)
  868. @result{} MAC
  869. ;default taken from :include clause
  870. (vehicle-year x)
  871. @result{} 79
  872. (defstruct (pickup (:include truck)) ;pickup type includes truck
  873. camper long-bed four-wheel-drive) @result{} PICKUP
  874. (setq x (make-pickup :name 'king :long-bed t)) @result{} #S(PICKUP...)
  875. ;:include default inherited
  876. (pickup-year x) @result{} 79
  877. ;;;
  878. ;;; Example 4
  879. ;;; use of BOA constructors
  880. ;;;
  881. (defstruct (dfs-boa ;BOA constructors
  882. (:constructor make-dfs-boa (a b c))
  883. (:constructor create-dfs-boa
  884. (a &optional b (c 'cc) &rest d &aux e (f 'ff))))
  885. a b c d e f) @result{} DFS-BOA
  886. ;a, b, and c set by position, and the rest are uninitialized
  887. (setq x (make-dfs-boa 1 2 3)) @result{} #(DFS-BOA...)
  888. (dfs-boa-a x) @result{} 1
  889. ;a and b set, c and f defaulted
  890. (setq x (create-dfs-boa 1 2)) @result{} #(DFS-BOA...)
  891. (dfs-boa-b x) @result{} 2
  892. (eq (dfs-boa-c x) 'cc) @result{} @i{true}
  893. ;a, b, and c set, and the rest are collected into d
  894. (setq x (create-dfs-boa 1 2 3 4 5 6)) @result{} #(DFS-BOA...)
  895. (dfs-boa-d x) @result{} (4 5 6)
  896. @end example
  897. @subsubheading Exceptional Situations::
  898. If any two slot names (whether present directly or inherited by the @t{:include} option)
  899. are the @i{same} under @b{string=},
  900. @b{defstruct} should signal an error of @i{type} @b{program-error}.
  901. The consequences are undefined if the @i{included-structure-name}
  902. does not name a @i{structure type}.
  903. @subsubheading See Also::
  904. @ref{documentation; (setf documentation)}
  905. ,
  906. @ref{print-object}
  907. ,
  908. @ref{setf; psetf}
  909. ,
  910. @ref{subtypep}
  911. ,
  912. @ref{type-of}
  913. ,
  914. @ref{typep}
  915. ,
  916. @ref{Compilation}
  917. @subsubheading Notes::
  918. The @i{printer-name} should observe the values of
  919. such printer-control variables as @b{*print-escape*}.
  920. The restriction against issuing a warning for type mismatches between
  921. a @i{slot-initform} and the corresponding slot's @t{:type} option is
  922. necessary because a @i{slot-initform} must be specified in order to
  923. specify slot options; in some cases, no suitable default may exist.
  924. The mechanism by which @b{defstruct} arranges for slot accessors to
  925. be usable with @b{setf} is @i{implementation-dependent};
  926. for example, it may use @i{setf functions}, @i{setf expanders}, or
  927. some other @i{implementation-dependent} mechanism known to that
  928. @i{implementation}'s @i{code} for @b{setf}.
  929. @node copy-structure, , defstruct, Structures Dictionary
  930. @subsection copy-structure [Function]
  931. @code{copy-structure} @i{structure} @result{} @i{copy}
  932. @subsubheading Arguments and Values::
  933. @i{structure}---a @i{structure}.
  934. @i{copy}---a copy of the @i{structure}.
  935. @subsubheading Description::
  936. Returns a @i{copy}_6 of the @i{structure}.
  937. Only the @i{structure} itself is copied; not the values of the slots.
  938. @subsubheading See Also::
  939. the @t{:copier} option to
  940. @ref{defstruct}
  941. @subsubheading Notes::
  942. The @i{copy} is the @i{same} as the given @i{structure}
  943. under @b{equalp}, but not under @b{equal}.
  944. @c end of including dict-structures
  945. @c %**end of chapter