This commit was generated by cvs2svn to compensate for changes in r5670,
[chise/xemacs-chise.git.1] / info / cl.info-5
1 This is Info file ../info/cl.info, produced by Makeinfo version 1.68
2 from the input file cl.texi.
3
4 INFO-DIR-SECTION XEmacs Editor
5 START-INFO-DIR-ENTRY
6 * Common Lisp: (cl).            GNU Emacs Common Lisp emulation package.
7 END-INFO-DIR-ENTRY
8
9    This file documents the GNU Emacs Common Lisp emulation package.
10
11    Copyright (C) 1993 Free Software Foundation, Inc.
12
13    Permission is granted to make and distribute verbatim copies of this
14 manual provided the copyright notice and this permission notice are
15 preserved on all copies.
16
17    Permission is granted to copy and distribute modified versions of
18 this manual under the conditions for verbatim copying, provided also
19 that the section entitled "GNU General Public License" is included
20 exactly as in the original, and provided that the entire resulting
21 derived work is distributed under the terms of a permission notice
22 identical to this one.
23
24    Permission is granted to copy and distribute translations of this
25 manual into another language, under the above conditions for modified
26 versions, except that the section entitled "GNU General Public License"
27 may be included in a translation approved by the author instead of in
28 the original English.
29
30 \1f
31 File: cl.info,  Node: Structures,  Next: Assertions,  Prev: Hash Tables,  Up: Top
32
33 Structures
34 **********
35
36 The Common Lisp "structure" mechanism provides a general way to define
37 data types similar to C's `struct' types.  A structure is a Lisp object
38 containing some number of "slots", each of which can hold any Lisp data
39 object.  Functions are provided for accessing and setting the slots,
40 creating or copying structure objects, and recognizing objects of a
41 particular structure type.
42
43    In true Common Lisp, each structure type is a new type distinct from
44 all existing Lisp types.  Since the underlying Emacs Lisp system
45 provides no way to create new distinct types, this package implements
46 structures as vectors (or lists upon request) with a special "tag"
47 symbol to identify them.
48
49  - Special Form: defstruct NAME SLOTS...
50      The `defstruct' form defines a new structure type called NAME,
51      with the specified SLOTS.  (The SLOTS may begin with a string
52      which documents the structure type.)  In the simplest case, NAME
53      and each of the SLOTS are symbols.  For example,
54
55           (defstruct person name age sex)
56
57      defines a struct type called `person' which contains three slots.
58      Given a `person' object P, you can access those slots by calling
59      `(person-name P)', `(person-age P)', and `(person-sex P)'.  You
60      can also change these slots by using `setf' on any of these place
61      forms:
62
63           (incf (person-age birthday-boy))
64
65      You can create a new `person' by calling `make-person', which
66      takes keyword arguments `:name', `:age', and `:sex' to specify the
67      initial values of these slots in the new object.  (Omitting any of
68      these arguments leaves the corresponding slot "undefined,"
69      according to the Common Lisp standard; in Emacs Lisp, such
70      uninitialized slots are filled with `nil'.)
71
72      Given a `person', `(copy-person P)' makes a new object of the same
73      type whose slots are `eq' to those of P.
74
75      Given any Lisp object X, `(person-p X)' returns true if X looks
76      like a `person', false otherwise.  (Again, in Common Lisp this
77      predicate would be exact; in Emacs Lisp the best it can do is
78      verify that X is a vector of the correct length which starts with
79      the correct tag symbol.)
80
81      Accessors like `person-name' normally check their arguments
82      (effectively using `person-p') and signal an error if the argument
83      is the wrong type.  This check is affected by `(optimize (safety
84      ...))' declarations.  Safety level 1, the default, uses a somewhat
85      optimized check that will detect all incorrect arguments, but may
86      use an uninformative error message (e.g., "expected a vector"
87      instead of "expected a `person'").  Safety level 0 omits all
88      checks except as provided by the underlying `aref' call; safety
89      levels 2 and 3 do rigorous checking that will always print a
90      descriptive error message for incorrect inputs.  *Note
91      Declarations::.
92
93           (setq dave (make-person :name "Dave" :sex 'male))
94                => [cl-struct-person "Dave" nil male]
95           (setq other (copy-person dave))
96                => [cl-struct-person "Dave" nil male]
97           (eq dave other)
98                => nil
99           (eq (person-name dave) (person-name other))
100                => t
101           (person-p dave)
102                => t
103           (person-p [1 2 3 4])
104                => nil
105           (person-p "Bogus")
106                => nil
107           (person-p '[cl-struct-person counterfeit person object])
108                => t
109
110      In general, NAME is either a name symbol or a list of a name
111      symbol followed by any number of "struct options"; each SLOT is
112      either a slot symbol or a list of the form `(SLOT-NAME
113      DEFAULT-VALUE SLOT-OPTIONS...)'.  The DEFAULT-VALUE is a Lisp form
114      which is evaluated any time an instance of the structure type is
115      created without specifying that slot's value.
116
117      Common Lisp defines several slot options, but the only one
118      implemented in this package is `:read-only'.  A non-`nil' value
119      for this option means the slot should not be `setf'-able; the
120      slot's value is determined when the object is created and does not
121      change afterward.
122
123           (defstruct person
124             (name nil :read-only t)
125             age
126             (sex 'unknown))
127
128      Any slot options other than `:read-only' are ignored.
129
130      For obscure historical reasons, structure options take a different
131      form than slot options.  A structure option is either a keyword
132      symbol, or a list beginning with a keyword symbol possibly followed
133      by arguments.  (By contrast, slot options are key-value pairs not
134      enclosed in lists.)
135
136           (defstruct (person (:constructor create-person)
137                              (:type list)
138                              :named)
139             name age sex)
140
141      The following structure options are recognized.
142
143     `:conc-name'
144           The argument is a symbol whose print name is used as the
145           prefix for the names of slot accessor functions.  The default
146           is the name of the struct type followed by a hyphen.  The
147           option `(:conc-name p-)' would change this prefix to `p-'.
148           Specifying `nil' as an argument means no prefix, so that the
149           slot names themselves are used to name the accessor functions.
150
151     `:constructor'
152           In the simple case, this option takes one argument which is an
153           alternate name to use for the constructor function.  The
154           default is `make-NAME', e.g., `make-person'.  The above
155           example changes this to `create-person'.  Specifying `nil' as
156           an argument means that no standard constructor should be
157           generated at all.
158
159           In the full form of this option, the constructor name is
160           followed by an arbitrary argument list.  *Note Program
161           Structure::, for a description of the format of Common Lisp
162           argument lists.  All options, such as `&rest' and `&key', are
163           supported.  The argument names should match the slot names;
164           each slot is initialized from the corresponding argument.
165           Slots whose names do not appear in the argument list are
166           initialized based on the DEFAULT-VALUE in their slot
167           descriptor.  Also, `&optional' and `&key' arguments which
168           don't specify defaults take their defaults from the slot
169           descriptor.  It is legal to include arguments which don't
170           correspond to slot names; these are useful if they are
171           referred to in the defaults for optional, keyword, or `&aux'
172           arguments which *do* correspond to slots.
173
174           You can specify any number of full-format `:constructor'
175           options on a structure.  The default constructor is still
176           generated as well unless you disable it with a simple-format
177           `:constructor' option.
178
179                (defstruct
180                 (person
181                  (:constructor nil)   ; no default constructor
182                  (:constructor new-person (name sex &optional (age 0)))
183                  (:constructor new-hound (&key (name "Rover")
184                                                (dog-years 0)
185                                           &aux (age (* 7 dog-years))
186                                                (sex 'canine))))
187                 name age sex)
188
189           The first constructor here takes its arguments positionally
190           rather than by keyword.  (In official Common Lisp
191           terminology, constructors that work By Order of Arguments
192           instead of by keyword are called "BOA constructors."  No, I'm
193           not making this up.)  For example, `(new-person "Jane"
194           'female)' generates a person whose slots are `"Jane"', 0, and
195           `female', respectively.
196
197           The second constructor takes two keyword arguments, `:name',
198           which initializes the `name' slot and defaults to `"Rover"',
199           and `:dog-years', which does not itself correspond to a slot
200           but which is used to initialize the `age' slot.  The `sex'
201           slot is forced to the symbol `canine' with no syntax for
202           overriding it.
203
204     `:copier'
205           The argument is an alternate name for the copier function for
206           this type.  The default is `copy-NAME'.  `nil' means not to
207           generate a copier function.  (In this implementation, all
208           copier functions are simply synonyms for `copy-sequence'.)
209
210     `:predicate'
211           The argument is an alternate name for the predicate which
212           recognizes objects of this type.  The default is `NAME-p'.
213           `nil' means not to generate a predicate function.  (If the
214           `:type' option is used without the `:named' option, no
215           predicate is ever generated.)
216
217           In true Common Lisp, `typep' is always able to recognize a
218           structure object even if `:predicate' was used.  In this
219           package, `typep' simply looks for a function called
220           `TYPENAME-p', so it will work for structure types only if
221           they used the default predicate name.
222
223     `:include'
224           This option implements a very limited form of C++-style
225           inheritance.  The argument is the name of another structure
226           type previously created with `defstruct'.  The effect is to
227           cause the new structure type to inherit all of the included
228           structure's slots (plus, of course, any new slots described
229           by this struct's slot descriptors).  The new structure is
230           considered a "specialization" of the included one.  In fact,
231           the predicate and slot accessors for the included type will
232           also accept objects of the new type.
233
234           If there are extra arguments to the `:include' option after
235           the included-structure name, these options are treated as
236           replacement slot descriptors for slots in the included
237           structure, possibly with modified default values.  Borrowing
238           an example from Steele:
239
240                (defstruct person name (age 0) sex)
241                     => person
242                (defstruct (astronaut (:include person (age 45)))
243                  helmet-size
244                  (favorite-beverage 'tang))
245                     => astronaut
246                
247                (setq joe (make-person :name "Joe"))
248                     => [cl-struct-person "Joe" 0 nil]
249                (setq buzz (make-astronaut :name "Buzz"))
250                     => [cl-struct-astronaut "Buzz" 45 nil nil tang]
251                
252                (list (person-p joe) (person-p buzz))
253                     => (t t)
254                (list (astronaut-p joe) (astronaut-p buzz))
255                     => (nil t)
256                
257                (person-name buzz)
258                     => "Buzz"
259                (astronaut-name joe)
260                     => error: "astronaut-name accessing a non-astronaut"
261
262           Thus, if `astronaut' is a specialization of `person', then
263           every `astronaut' is also a `person' (but not the other way
264           around).  Every `astronaut' includes all the slots of a
265           `person', plus extra slots that are specific to astronauts.
266           Operations that work on people (like `person-name') work on
267           astronauts just like other people.
268
269     `:print-function'
270           In full Common Lisp, this option allows you to specify a
271           function which is called to print an instance of the
272           structure type.  The Emacs Lisp system offers no hooks into
273           the Lisp printer which would allow for such a feature, so
274           this package simply ignores `:print-function'.
275
276     `:type'
277           The argument should be one of the symbols `vector' or `list'.
278           This tells which underlying Lisp data type should be used to
279           implement the new structure type.  Vectors are used by
280           default, but `(:type list)' will cause structure objects to
281           be stored as lists instead.
282
283           The vector representation for structure objects has the
284           advantage that all structure slots can be accessed quickly,
285           although creating vectors is a bit slower in Emacs Lisp.
286           Lists are easier to create, but take a relatively long time
287           accessing the later slots.
288
289     `:named'
290           This option, which takes no arguments, causes a
291           characteristic "tag" symbol to be stored at the front of the
292           structure object.  Using `:type' without also using `:named'
293           will result in a structure type stored as plain vectors or
294           lists with no identifying features.
295
296           The default, if you don't specify `:type' explicitly, is to
297           use named vectors.  Therefore, `:named' is only useful in
298           conjunction with `:type'.
299
300                (defstruct (person1) name age sex)
301                (defstruct (person2 (:type list) :named) name age sex)
302                (defstruct (person3 (:type list)) name age sex)
303                
304                (setq p1 (make-person1))
305                     => [cl-struct-person1 nil nil nil]
306                (setq p2 (make-person2))
307                     => (person2 nil nil nil)
308                (setq p3 (make-person3))
309                     => (nil nil nil)
310                
311                (person1-p p1)
312                     => t
313                (person2-p p2)
314                     => t
315                (person3-p p3)
316                     => error: function person3-p undefined
317
318           Since unnamed structures don't have tags, `defstruct' is not
319           able to make a useful predicate for recognizing them.  Also,
320           accessors like `person3-name' will be generated but they will
321           not be able to do any type checking.  The `person3-name'
322           function, for example, will simply be a synonym for `car' in
323           this case.  By contrast, `person2-name' is able to verify
324           that its argument is indeed a `person2' object before
325           proceeding.
326
327     `:initial-offset'
328           The argument must be a nonnegative integer.  It specifies a
329           number of slots to be left "empty" at the front of the
330           structure.  If the structure is named, the tag appears at the
331           specified position in the list or vector; otherwise, the first
332           slot appears at that position.  Earlier positions are filled
333           with `nil' by the constructors and ignored otherwise.  If the
334           type `:include's another type, then `:initial-offset'
335           specifies a number of slots to be skipped between the last
336           slot of the included type and the first new slot.
337
338    Except as noted, the `defstruct' facility of this package is
339 entirely compatible with that of Common Lisp.
340
341 \1f
342 File: cl.info,  Node: Assertions,  Next: Efficiency Concerns,  Prev: Structures,  Up: Top
343
344 Assertions and Errors
345 *********************
346
347 This section describes two macros that test "assertions", i.e.,
348 conditions which must be true if the program is operating correctly.
349 Assertions never add to the behavior of a Lisp program; they simply
350 make "sanity checks" to make sure everything is as it should be.
351
352    If the optimization property `speed' has been set to 3, and `safety'
353 is less than 3, then the byte-compiler will optimize away the following
354 assertions.  Because assertions might be optimized away, it is a bad
355 idea for them to include side-effects.
356
357  - Special Form: assert TEST-FORM [SHOW-ARGS STRING ARGS...]
358      This form verifies that TEST-FORM is true (i.e., evaluates to a
359      non-`nil' value).  If so, it returns `nil'.  If the test is not
360      satisfied, `assert' signals an error.
361
362      A default error message will be supplied which includes TEST-FORM.
363      You can specify a different error message by including a STRING
364      argument plus optional extra arguments.  Those arguments are simply
365      passed to `error' to signal the error.
366
367      If the optional second argument SHOW-ARGS is `t' instead of `nil',
368      then the error message (with or without STRING) will also include
369      all non-constant arguments of the top-level FORM.  For example:
370
371           (assert (> x 10) t "x is too small: %d")
372
373      This usage of SHOW-ARGS is an extension to Common Lisp.  In true
374      Common Lisp, the second argument gives a list of PLACES which can
375      be `setf''d by the user before continuing from the error.  Since
376      Emacs Lisp does not support continuable errors, it makes no sense
377      to specify PLACES.
378
379  - Special Form: check-type FORM TYPE [STRING]
380      This form verifies that FORM evaluates to a value of type TYPE.
381      If so, it returns `nil'.  If not, `check-type' signals a
382      `wrong-type-argument' error.  The default error message lists the
383      erroneous value along with TYPE and FORM themselves.  If STRING is
384      specified, it is included in the error message in place of TYPE.
385      For example:
386
387           (check-type x (integer 1 *) "a positive integer")
388
389      *Note Type Predicates::, for a description of the type specifiers
390      that may be used for TYPE.
391
392      Note that in Common Lisp, the first argument to `check-type' must
393      be a PLACE suitable for use by `setf', because `check-type'
394      signals a continuable error that allows the user to modify PLACE.
395
396    The following error-related macro is also defined:
397
398  - Special Form: ignore-errors FORMS...
399      This executes FORMS exactly like a `progn', except that errors are
400      ignored during the FORMS.  More precisely, if an error is
401      signalled then `ignore-errors' immediately aborts execution of the
402      FORMS and returns `nil'.  If the FORMS complete successfully,
403      `ignore-errors' returns the result of the last FORM.
404
405 \1f
406 File: cl.info,  Node: Efficiency Concerns,  Next: Common Lisp Compatibility,  Prev: Assertions,  Up: Top
407
408 Efficiency Concerns
409 *******************
410
411 Macros
412 ======
413
414 Many of the advanced features of this package, such as `defun*',
415 `loop', and `setf', are implemented as Lisp macros.  In byte-compiled
416 code, these complex notations will be expanded into equivalent Lisp
417 code which is simple and efficient.  For example, the forms
418
419      (incf i n)
420      (push x (car p))
421
422 are expanded at compile-time to the Lisp forms
423
424      (setq i (+ i n))
425      (setcar p (cons x (car p)))
426
427 which are the most efficient ways of doing these respective operations
428 in Lisp.  Thus, there is no performance penalty for using the more
429 readable `incf' and `push' forms in your compiled code.
430
431    *Interpreted* code, on the other hand, must expand these macros
432 every time they are executed.  For this reason it is strongly
433 recommended that code making heavy use of macros be compiled.  (The
434 features labelled "Special Form" instead of "Function" in this manual
435 are macros.)  A loop using `incf' a hundred times will execute
436 considerably faster if compiled, and will also garbage-collect less
437 because the macro expansion will not have to be generated, used, and
438 thrown away a hundred times.
439
440    You can find out how a macro expands by using the `cl-prettyexpand'
441 function.
442
443  - Function: cl-prettyexpand FORM &optional FULL
444      This function takes a single Lisp form as an argument and inserts
445      a nicely formatted copy of it in the current buffer (which must be
446      in Lisp mode so that indentation works properly).  It also expands
447      all Lisp macros which appear in the form.  The easiest way to use
448      this function is to go to the `*scratch*' buffer and type, say,
449
450           (cl-prettyexpand '(loop for x below 10 collect x))
451
452      and type `C-x C-e' immediately after the closing parenthesis; the
453      expansion
454
455           (block nil
456             (let* ((x 0)
457                    (G1004 nil))
458               (while (< x 10)
459                 (setq G1004 (cons x G1004))
460                 (setq x (+ x 1)))
461               (nreverse G1004)))
462
463      will be inserted into the buffer.  (The `block' macro is expanded
464      differently in the interpreter and compiler, so `cl-prettyexpand'
465      just leaves it alone.  The temporary variable `G1004' was created
466      by `gensym'.)
467
468      If the optional argument FULL is true, then *all* macros are
469      expanded, including `block', `eval-when', and compiler macros.
470      Expansion is done as if FORM were a top-level form in a file being
471      compiled.  For example,
472
473           (cl-prettyexpand '(pushnew 'x list))
474                -| (setq list (adjoin 'x list))
475           (cl-prettyexpand '(pushnew 'x list) t)
476                -| (setq list (if (memq 'x list) list (cons 'x list)))
477           (cl-prettyexpand '(caddr (member* 'a list)) t)
478                -| (car (cdr (cdr (memq 'a list))))
479
480      Note that `adjoin', `caddr', and `member*' all have built-in
481      compiler macros to optimize them in common cases.
482
483
484 Error Checking
485 ==============
486
487 Common Lisp compliance has in general not been sacrificed for the sake
488 of efficiency.  A few exceptions have been made for cases where
489 substantial gains were possible at the expense of marginal
490 incompatibility.  One example is the use of `memq' (which is treated
491 very efficiently by the byte-compiler) to scan for keyword arguments;
492 this can become confused in rare cases when keyword symbols are used as
493 both keywords and data values at once.  This is extremely unlikely to
494 occur in practical code, and the use of `memq' allows functions with
495 keyword arguments to be nearly as fast as functions that use
496 `&optional' arguments.
497
498    The Common Lisp standard (as embodied in Steele's book) uses the
499 phrase "it is an error if" to indicate a situation which is not
500 supposed to arise in complying programs; implementations are strongly
501 encouraged but not required to signal an error in these situations.
502 This package sometimes omits such error checking in the interest of
503 compactness and efficiency.  For example, `do' variable specifiers are
504 supposed to be lists of one, two, or three forms; extra forms are
505 ignored by this package rather than signalling a syntax error.  The
506 `endp' function is simply a synonym for `null' in this package.
507 Functions taking keyword arguments will accept an odd number of
508 arguments, treating the trailing keyword as if it were followed by the
509 value `nil'.
510
511    Argument lists (as processed by `defun*' and friends) *are* checked
512 rigorously except for the minor point just mentioned; in particular,
513 keyword arguments are checked for validity, and `&allow-other-keys' and
514 `:allow-other-keys' are fully implemented.  Keyword validity checking
515 is slightly time consuming (though not too bad in byte-compiled code);
516 you can use `&allow-other-keys' to omit this check.  Functions defined
517 in this package such as `find' and `member*' do check their keyword
518 arguments for validity.
519
520
521 Optimizing Compiler
522 ===================
523
524 The byte-compiler that comes with Emacs 18 normally fails to expand
525 macros that appear in top-level positions in the file (i.e., outside of
526 `defun's or other enclosing forms).  This would have disastrous
527 consequences to programs that used such top-level macros as `defun*',
528 `eval-when', and `defstruct'.  To work around this problem, the "CL"
529 package patches the Emacs 18 compiler to expand top-level macros.  This
530 patch will apply to your own macros, too, if they are used in a
531 top-level context.  The patch will not harm versions of the Emacs 18
532 compiler which have already had a similar patch applied, nor will it
533 affect the optimizing Emacs 19 byte-compiler written by Jamie Zawinski
534 and Hallvard Furuseth.  The patch is applied to the byte compiler's
535 code in Emacs' memory, *not* to the `bytecomp.elc' file stored on disk.
536
537    The Emacs 19 compiler (for Emacs 18) is available from various Emacs
538 Lisp archive sites such as `archive.cis.ohio-state.edu'.  Its use is
539 highly recommended; many of the Common Lisp macros emit code which can
540 be improved by optimization.  In particular, `block's (whether explicit
541 or implicit in constructs like `defun*' and `loop') carry a fair
542 run-time penalty; the optimizing compiler removes `block's which are
543 not actually referenced by `return' or `return-from' inside the block.
544
545 \1f
546 File: cl.info,  Node: Common Lisp Compatibility,  Next: Old CL Compatibility,  Prev: Efficiency Concerns,  Up: Top
547
548 Common Lisp Compatibility
549 *************************
550
551 Following is a list of all known incompatibilities between this package
552 and Common Lisp as documented in Steele (2nd edition).
553
554    Certain function names, such as `member', `assoc', and `floor', were
555 already taken by (incompatible) Emacs Lisp functions; this package
556 appends `*' to the names of its Common Lisp versions of these functions.
557
558    The word `defun*' is required instead of `defun' in order to use
559 extended Common Lisp argument lists in a function.  Likewise,
560 `defmacro*' and `function*' are versions of those forms which
561 understand full-featured argument lists.  The `&whole' keyword does not
562 work in `defmacro' argument lists (except inside recursive argument
563 lists).
564
565    In order to allow an efficient implementation, keyword arguments use
566 a slightly cheesy parser which may be confused if a keyword symbol is
567 passed as the *value* of another keyword argument.  (Specifically,
568 `(memq :KEYWORD REST-OF-ARGUMENTS)' is used to scan for `:KEYWORD'
569 among the supplied keyword arguments.)
570
571    The `eql' and `equal' predicates do not distinguish between IEEE
572 floating-point plus and minus zero.  The `equalp' predicate has several
573 differences with Common Lisp; *note Predicates::..
574
575    The `setf' mechanism is entirely compatible, except that
576 setf-methods return a list of five values rather than five values
577 directly.  Also, the new "`setf' function" concept (typified by `(defun
578 (setf foo) ...)') is not implemented.
579
580    The `do-all-symbols' form is the same as `do-symbols' with no
581 OBARRAY argument.  In Common Lisp, this form would iterate over all
582 symbols in all packages.  Since Emacs obarrays are not a first-class
583 package mechanism, there is no way for `do-all-symbols' to locate any
584 but the default obarray.
585
586    The `loop' macro is complete except that `loop-finish' and type
587 specifiers are unimplemented.
588
589    The multiple-value return facility treats lists as multiple values,
590 since Emacs Lisp cannot support multiple return values directly.  The
591 macros will be compatible with Common Lisp if `values' or `values-list'
592 is always used to return to a `multiple-value-bind' or other
593 multiple-value receiver; if `values' is used without
594 `multiple-value-...'  or vice-versa the effect will be different from
595 Common Lisp.
596
597    Many Common Lisp declarations are ignored, and others match the
598 Common Lisp standard in concept but not in detail.  For example, local
599 `special' declarations, which are purely advisory in Emacs Lisp, do not
600 rigorously obey the scoping rules set down in Steele's book.
601
602    The variable `*gensym-counter*' starts out with a pseudo-random
603 value rather than with zero.  This is to cope with the fact that
604 generated symbols become interned when they are written to and loaded
605 back from a file.
606
607    The `defstruct' facility is compatible, except that structures are
608 of type `:type vector :named' by default rather than some special,
609 distinct type.  Also, the `:type' slot option is ignored.
610
611    The second argument of `check-type' is treated differently.
612
613 \1f
614 File: cl.info,  Node: Old CL Compatibility,  Next: Porting Common Lisp,  Prev: Common Lisp Compatibility,  Up: Top
615
616 Old CL Compatibility
617 ********************
618
619 Following is a list of all known incompatibilities between this package
620 and the older Quiroz `cl.el' package.
621
622    This package's emulation of multiple return values in functions is
623 incompatible with that of the older package.  That package attempted to
624 come as close as possible to true Common Lisp multiple return values;
625 unfortunately, it could not be 100% reliable and so was prone to
626 occasional surprises if used freely.  This package uses a simpler
627 method, namely replacing multiple values with lists of values, which is
628 more predictable though more noticeably different from Common Lisp.
629
630    The `defkeyword' form and `keywordp' function are not implemented in
631 this package.
632
633    The `member', `floor', `ceiling', `truncate', `round', `mod', and
634 `rem' functions are suffixed by `*' in this package to avoid collision
635 with existing functions in Emacs 18 or Emacs 19.  The older package
636 simply redefined these functions, overwriting the built-in meanings and
637 causing serious portability problems with Emacs 19.  (Some more recent
638 versions of the Quiroz package changed the names to `cl-member', etc.;
639 this package defines the latter names as aliases for `member*', etc.)
640
641    Certain functions in the old package which were buggy or inconsistent
642 with the Common Lisp standard are incompatible with the conforming
643 versions in this package.  For example, `eql' and `member' were
644 synonyms for `eq' and `memq' in that package, `setf' failed to preserve
645 correct order of evaluation of its arguments, etc.
646
647    Finally, unlike the older package, this package is careful to prefix
648 all of its internal names with `cl-'.  Except for a few functions which
649 are explicitly defined as additional features (such as `floatp-safe'
650 and `letf'), this package does not export any non-`cl-' symbols which
651 are not also part of Common Lisp.
652
653
654 The `cl-compat' package
655 =======================
656
657 The "CL" package includes emulations of some features of the old
658 `cl.el', in the form of a compatibility package `cl-compat'.  To use
659 it, put `(require 'cl-compat)' in your program.
660
661    The old package defined a number of internal routines without `cl-'
662 prefixes or other annotations.  Call to these routines may have crept
663 into existing Lisp code.  `cl-compat' provides emulations of the
664 following internal routines: `pair-with-newsyms', `zip-lists',
665 `unzip-lists', `reassemble-arglists', `duplicate-symbols-p',
666 `safe-idiv'.
667
668    Some `setf' forms translated into calls to internal functions that
669 user code might call directly.  The functions `setnth', `setnthcdr',
670 and `setelt' fall in this category; they are defined by `cl-compat',
671 but the best fix is to change to use `setf' properly.
672
673    The `cl-compat' file defines the keyword functions `keywordp',
674 `keyword-of', and `defkeyword', which are not defined by the new "CL"
675 package because the use of keywords as data is discouraged.
676
677    The `build-klist' mechanism for parsing keyword arguments is
678 emulated by `cl-compat'; the `with-keyword-args' macro is not, however,
679 and in any case it's best to change to use the more natural keyword
680 argument processing offered by `defun*'.
681
682    Multiple return values are treated differently by the two Common
683 Lisp packages.  The old package's method was more compatible with true
684 Common Lisp, though it used heuristics that caused it to report
685 spurious multiple return values in certain cases.  The `cl-compat'
686 package defines a set of multiple-value macros that are compatible with
687 the old CL package; again, they are heuristic in nature, but they are
688 guaranteed to work in any case where the old package's macros worked.
689 To avoid name collision with the "official" multiple-value facilities,
690 the ones in `cl-compat' have capitalized names:  `Values',
691 `Values-list', `Multiple-value-bind', etc.
692
693    The functions `cl-floor', `cl-ceiling', `cl-truncate', and
694 `cl-round' are defined by `cl-compat' to use the old-style
695 multiple-value mechanism, just as they did in the old package.  The
696 newer `floor*' and friends return their two results in a list rather
697 than as multiple values.  Note that older versions of the old package
698 used the unadorned names `floor', `ceiling', etc.; `cl-compat' cannot
699 use these names because they conflict with Emacs 19 built-ins.
700
701 \1f
702 File: cl.info,  Node: Porting Common Lisp,  Next: Function Index,  Prev: Old CL Compatibility,  Up: Top
703
704 Porting Common Lisp
705 *******************
706
707 This package is meant to be used as an extension to Emacs Lisp, not as
708 an Emacs implementation of true Common Lisp.  Some of the remaining
709 differences between Emacs Lisp and Common Lisp make it difficult to
710 port large Common Lisp applications to Emacs.  For one, some of the
711 features in this package are not fully compliant with ANSI or Steele;
712 *note Common Lisp Compatibility::..  But there are also quite a few
713 features that this package does not provide at all.  Here are some
714 major omissions that you will want watch out for when bringing Common
715 Lisp code into Emacs.
716
717    * Case-insensitivity.  Symbols in Common Lisp are case-insensitive
718      by default.  Some programs refer to a function or variable as
719      `foo' in one place and `Foo' or `FOO' in another.  Emacs Lisp will
720      treat these as three distinct symbols.
721
722      Some Common Lisp code is written in all upper-case.  While Emacs
723      is happy to let the program's own functions and variables use this
724      convention, calls to Lisp builtins like `if' and `defun' will have
725      to be changed to lower-case.
726
727    * Lexical scoping.  In Common Lisp, function arguments and `let'
728      bindings apply only to references physically within their bodies
729      (or within macro expansions in their bodies).  Emacs Lisp, by
730      contrast, uses "dynamic scoping" wherein a binding to a variable
731      is visible even inside functions called from the body.
732
733      Variables in Common Lisp can be made dynamically scoped by
734      declaring them `special' or using `defvar'.  In Emacs Lisp it is
735      as if all variables were declared `special'.
736
737      Often you can use code that was written for lexical scoping even
738      in a dynamically scoped Lisp, but not always.  Here is an example
739      of a Common Lisp code fragment that would fail in Emacs Lisp:
740
741           (defun map-odd-elements (func list)
742             (loop for x in list
743                   for flag = t then (not flag)
744                   collect (if flag x (funcall func x))))
745           
746           (defun add-odd-elements (list x)
747             (map-odd-elements (function (lambda (a) (+ a x))) list))
748
749      In Common Lisp, the two functions' usages of `x' are completely
750      independent.  In Emacs Lisp, the binding to `x' made by
751      `add-odd-elements' will have been hidden by the binding in
752      `map-odd-elements' by the time the `(+ a x)' function is called.
753
754      (This package avoids such problems in its own mapping functions by
755      using names like `cl-x' instead of `x' internally; as long as you
756      don't use the `cl-' prefix for your own variables no collision can
757      occur.)
758
759      *Note Lexical Bindings::, for a description of the `lexical-let'
760      form which establishes a Common Lisp-style lexical binding, and
761      some examples of how it differs from Emacs' regular `let'.
762
763    * Common Lisp allows the shorthand `#'x' to stand for `(function
764      x)', just as `'x' stands for `(quote x)'.  In Common Lisp, one
765      traditionally uses `#'' notation when referring to the name of a
766      function.  In Emacs Lisp, it works just as well to use a regular
767      quote:
768
769           (loop for x in y by #'cddr collect (mapcar #'plusp x))  ; Common Lisp
770           (loop for x in y by 'cddr collect (mapcar 'plusp x))    ; Emacs Lisp
771
772      When `#'' introduces a `lambda' form, it is best to write out
773      `(function ...)' longhand in Emacs Lisp.  You can use a regular
774      quote, but then the byte-compiler won't know that the `lambda'
775      expression is code that can be compiled.
776
777           (mapcar #'(lambda (x) (* x 2)) list)            ; Common Lisp
778           (mapcar (function (lambda (x) (* x 2))) list)   ; Emacs Lisp
779
780      XEmacs supports `#'' notation starting with version 19.8.
781
782    * Reader macros.  Common Lisp includes a second type of macro that
783      works at the level of individual characters.  For example, Common
784      Lisp implements the quote notation by a reader macro called `'',
785      whereas Emacs Lisp's parser just treats quote as a special case.
786      Some Lisp packages use reader macros to create special syntaxes
787      for themselves, which the Emacs parser is incapable of reading.
788
789    * Other syntactic features.  Common Lisp provides a number of
790      notations beginning with `#' that the Emacs Lisp parser won't
791      understand.  For example, `#| ... |#' is an alternate comment
792      notation, and `#+lucid (foo)' tells the parser to ignore the
793      `(foo)' except in Lucid Common Lisp.
794
795      The number prefixes `#b', `#o', and `#x', however, are supported
796      by the Emacs Lisp parser to represent numbers in binary, octal,
797      and hexadecimal notation (or radix), just like in Common Lisp.
798
799    * Packages.  In Common Lisp, symbols are divided into "packages".
800      Symbols that are Lisp built-ins are typically stored in one
801      package; symbols that are vendor extensions are put in another,
802      and each application program would have a package for its own
803      symbols.  Certain symbols are "exported" by a package and others
804      are internal; certain packages "use" or import the exported symbols
805      of other packages.  To access symbols that would not normally be
806      visible due to this importing and exporting, Common Lisp provides
807      a syntax like `package:symbol' or `package::symbol'.
808
809      Emacs Lisp has a single namespace for all interned symbols, and
810      then uses a naming convention of putting a prefix like `cl-' in
811      front of the name.  Some Emacs packages adopt the Common Lisp-like
812      convention of using `cl:' or `cl::' as the prefix.  However, the
813      Emacs parser does not understand colons and just treats them as
814      part of the symbol name.  Thus, while `mapcar' and `lisp:mapcar'
815      may refer to the same symbol in Common Lisp, they are totally
816      distinct in Emacs Lisp.  Common Lisp programs which refer to a
817      symbol by the full name sometimes and the short name other times
818      will not port cleanly to Emacs.
819
820      Emacs Lisp does have a concept of "obarrays," which are
821      package-like collections of symbols, but this feature is not
822      strong enough to be used as a true package mechanism.
823
824    * Keywords.  The notation `:test-not' in Common Lisp really is a
825      shorthand for `keyword:test-not'; keywords are just symbols in a
826      built-in `keyword' package with the special property that all its
827      symbols are automatically self-evaluating.  Common Lisp programs
828      often use keywords liberally to avoid having to use quotes.
829
830      In Emacs Lisp a keyword is just a symbol whose name begins with a
831      colon; since the Emacs parser does not treat them specially, they
832      have to be explicitly made self-evaluating by a statement like
833      `(setq :test-not ':test-not)'.  This package arranges to execute
834      such a statement whenever `defun*' or some other form sees a
835      keyword being used as an argument.  Common Lisp code that assumes
836      that a symbol `:mumble' will be self-evaluating even though it was
837      never introduced by a `defun*' will have to be fixed.
838
839    * The `format' function is quite different between Common Lisp and
840      Emacs Lisp.  It takes an additional "destination" argument before
841      the format string.  A destination of `nil' means to format to a
842      string as in Emacs Lisp; a destination of `t' means to write to
843      the terminal (similar to `message' in Emacs).  Also, format
844      control strings are utterly different; `~' is used instead of `%'
845      to introduce format codes, and the set of available codes is much
846      richer.  There are no notations like `\n' for string literals;
847      instead, `format' is used with the "newline" format code, `~%'.
848      More advanced formatting codes provide such features as paragraph
849      filling, case conversion, and even loops and conditionals.
850
851      While it would have been possible to implement most of Common Lisp
852      `format' in this package (under the name `format*', of course), it
853      was not deemed worthwhile.  It would have required a huge amount
854      of code to implement even a decent subset of `format*', yet the
855      functionality it would provide over Emacs Lisp's `format' would
856      rarely be useful.
857
858    * Vector constants use square brackets in Emacs Lisp, but `#(a b c)'
859      notation in Common Lisp.  To further complicate matters, Emacs 19
860      introduces its own `#(' notation for something entirely
861      different--strings with properties.
862
863    * Characters are distinct from integers in Common Lisp.  The
864      notation for character constants is also different:  `#\A' instead
865      of `?A'.  Also, `string=' and `string-equal' are synonyms in Emacs
866      Lisp whereas the latter is case-insensitive in Common Lisp.
867
868    * Data types.  Some Common Lisp data types do not exist in Emacs
869      Lisp.  Rational numbers and complex numbers are not present, nor
870      are large integers (all integers are "fixnums").  All arrays are
871      one-dimensional.  There are no readtables or pathnames; streams
872      are a set of existing data types rather than a new data type of
873      their own.  Hash tables, random-states, structures, and packages
874      (obarrays) are built from Lisp vectors or lists rather than being
875      distinct types.
876
877    * The Common Lisp Object System (CLOS) is not implemented, nor is
878      the Common Lisp Condition System.
879
880    * Common Lisp features that are completely redundant with Emacs Lisp
881      features of a different name generally have not been implemented.
882      For example, Common Lisp writes `defconstant' where Emacs Lisp
883      uses `defconst'.  Similarly, `make-list' takes its arguments in
884      different ways in the two Lisps but does exactly the same thing,
885      so this package has not bothered to implement a Common Lisp-style
886      `make-list'.
887
888    * A few more notable Common Lisp features not included in this
889      package:  `compiler-let', `tagbody', `prog', `ldb/dpb',
890      `parse-integer', `cerror'.
891
892    * Recursion.  While recursion works in Emacs Lisp just like it does
893      in Common Lisp, various details of the Emacs Lisp system and
894      compiler make recursion much less efficient than it is in most
895      Lisps.  Some schools of thought prefer to use recursion in Lisp
896      over other techniques; they would sum a list of numbers using
897      something like
898
899           (defun sum-list (list)
900             (if list
901                 (+ (car list) (sum-list (cdr list)))
902               0))
903
904      where a more iteratively-minded programmer might write one of
905      these forms:
906
907           (let ((total 0)) (dolist (x my-list) (incf total x)) total)
908           (loop for x in my-list sum x)
909
910      While this would be mainly a stylistic choice in most Common Lisps,
911      in Emacs Lisp you should be aware that the iterative forms are
912      much faster than recursion.  Also, Lisp programmers will want to
913      note that the current Emacs Lisp compiler does not optimize tail
914      recursion.
915