forked from filonenko-mikhail/cltl2-doc
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpreds.tex
1943 lines (1670 loc) · 87.6 KB
/
preds.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
%Part{Preds, Root = "CLM.MSS"}
%Chapter of Common Lisp Manual. Copyright 1984, 1988, 1989 Guy L. Steele Jr.
\clearpage\def\pagestatus{FINAL PROOF}
\ifx \rulang\Undef
\chapter{Predicates}
\label{PREDS}
A \emph{predicate} is a function that tests for some condition involving
its arguments and returns {\false} if the condition is false, or some
non-{\false} value if the condition is true. One may think of a predicate as
producing a Boolean value, where {\false} stands for \emph{false} and anything
else stands for \emph{true}. Conditional control structures such as
\cdf{cond},
\cdf{if}, \cdf{when}, and \cdf{unless} test such Boolean values.
We say that a predicate \emph{is true} when it returns a non-{\false} value,
and \emph{is false} when it returns {\false}; that is, it is true or false
according to whether the condition being tested is true or false.
By convention, the names of predicates usually end in the letter
\cd{p} (which stands for ``predicate'').
Common Lisp uses a uniform convention in hyphenating names of predicates.
If the name of the predicate is formed by adding a \cd{p} to
an existing name, such as the name of a data type,
a hyphen is placed before the final \cd{p} if and only if there is
a hyphen in the existing name. For example, \cd{number} begets \cd{numberp}
but \cd{standard-char} begets \cd{standard-char-p}.
On the other hand, if the name of a predicate is formed by adding
a prefixing qualifier to the front of an existing predicate name,
the two names are joined with a hyphen and the presence or absence
of a hyphen before the final \cd{p} is not changed. For example,
the predicate \cd{string-lessp} has no hyphen before the \cd{p}
because it is the string version of \cdf{lessp} (a MacLisp function
that has been renamed \cdf{<} in Common Lisp). The name \cd{string-less-p}
would incorrectly imply that it is a predicate that tests for a kind
of object called a \cdf{string-less}, and the name \cd{stringlessp}
would connote a predicate that tests whether something has no strings
(is ``stringless'')!
The control structures that test Boolean values only test for
whether or not the value is {\false}, which is considered to be false. Any
other value is considered to be true. Often a predicate will return {\false} if
it ``fails'' and some \emph{useful} value if it ``succeeds'';
such a function can be used not only as a test but
also for the useful value provided in case of success. An example
is \cdf{member}.
If no better non-{\nil} value is available for the purpose of indicating
success, by convention the symbol \cdf{t} is used as the ``standard''
true value.
\section{Logical Values}
The names \cdf{nil} and \cdf{t} are constants in Common Lisp. Although they
are symbols like any other symbols, and appear to be treated
as variables when evaluated, it is not permitted to modify their
values. See \cdf{defconstant}.
\begin{defun}[Constant]
nil
The value of {\nil} is always {\nil}. This object represents the logical
\emph{false} value and also the empty list. It can also be written \cd{()}.
\end{defun}
\begin{defun}[Constant]
t
The value of \cdf{t} is always \cdf{t}.
\end{defun}
\section{Data Type Predicates}
Perhaps the most important predicates in Lisp are those that deal
with data types; that is, given a data object one can determine whether
or not it belongs to a given type, or one can compare two type specifiers.
\subsection{General Type Predicates}
If a data type is viewed as the set of all objects belonging to the type,
then the \cdf{typep} function is a set membership test, while \cdf{subtypep}
is a subset test.
\begin{defun}[Function]
typep object type
\cdf{typep} is a predicate that
is true if \emph{object} is of type \emph{type}, and is false otherwise.
Note that an object can be ``of'' more than one type, since one type can
include another. The \emph{type} may be any of the type specifiers
mentioned in chapter~\ref{DTSPEC} \emph{except} that it may not
be or contain a type specifier list whose first element is \cdf{function}
or \cdf{values}.
A specifier of the form \cd{(satisfies \emph{fn})} is handled simply
by applying the function \emph{fn} to \emph{object}
(see \cdf{funcall}); the \emph{object} is considered
to be of the specified type if the result is not {\false}.
\begin{new}
X3J13 voted in January 1989
\issue{ARRAY-TYPE-ELEMENT-TYPE-SEMANTICS}
to change \cdf{typep} to give specialized
\cdf{array} and \cdf{complex} type specifiers the same meaning for
purposes of type discrimination as they have for declaration purposes.
Of course, this also applies to such type specifiers as \cdf{vector}
and \cdf{simple-array}
(see section~\ref{SPECIALIZED-TYPE-SPECIFIER-SECTION}).
Thus
\begin{lisp}
(typep foo '(array bignum))
\end{lisp}
in the first edition asked the question, Is \cdf{foo} an array
specialized to hold bignums? but under the new interpretation
asks the question, Could the array \cdf{foo} have resulted from
giving \cdf{bignum} as the \cd{:element-type} argument
to \cdf{make-array}?
\end{new}
\end{defun}
\begin{defun}[Function]
subtypep type1 type2
The arguments must be type specifiers that are acceptable to \cdf{typep}.
The two type specifiers are compared; this predicate is true
if \emph{type1} is definitely a (not necessarily proper) subtype of \emph{type2}.
If the result is {\false}, however, then \emph{type1} may or may not be a subtype of
\emph{type2} (sometimes it is impossible to tell, especially when
\cdf{satisfies} type specifiers are involved).
A second returned value indicates the certainty of the result;
if it is true, then the first value is an accurate indication
of the subtype relationship. Thus there are three possible
result combinations:
\begin{tabbing}
~~~~~~~~\=~~~~~~~~\=\kill
{\true}\>{\true}\>\emph{type1} is definitely a subtype of \emph{type2} \\
{\false}\>{\true}\>\emph{type1} is definitely not a subtype of \emph{type2} \\
{\false}\>{\false}\>\cdf{subtypep} could not determine the relationship
\end{tabbing}
\begin{new}
X3J13 voted in January 1989
\issue{SUBTYPEP-TOO-VAGUE}
to place certain requirements upon the implementation of \cdf{subtypep},
for it noted that implementations in many cases simply ``give up''
and return the two values \cdf{nil} and \cdf{nil} when in fact it would have been
possible to determine the relationship between the given types.
The requirements are as follows, where it is understood that a type specifier \emph{s}
\emph{involves} a type specifier \emph{u} if either \emph{s} contains an occurrence of \emph{u}
directly or \emph{s} contains a type specifier \emph{w} defined by \cdf{deftype} whose
expansion involves \emph{u}.
\begin{itemize}
\item \cdf{subtypep} is not permitted to return a second value of \cdf{nil}
unless one or both of its arguments involves \cdf{satisfies},
\cdf{and}, \cdf{or}, \cdf{not}, or \cdf{member}.
\item \cdf{subtypep} should signal an error when one or both of its arguments
involves \cdf{values} or the list form of the \cdf{function} type specifier.
\item \cdf{subtypep} must always return the two values \cdf{t} and \cdf{t}
in the case where its arguments, after expansion of specifiers
defined by \cdf{deftype}, are \cdf{equal}.
\end{itemize}
In addition, X3J13 voted to clarify that in some cases
the relationships between types
as reflected by \cdf{subtypep} may be implementation-specific.
For example, in an implementation supporting only one type of
floating-point number, \cd{(subtypep 'float 'long-float)} would return
\cdf{t} and \cdf{t}, since the two types would be identical.
Note that \cdf{satisfies} is an exception because relationships between
types involving \cdf{satisfies} are undecidable in general, but (as X3J13 noted)
\cdf{and}, \cdf{or}, \cdf{not}, and \cdf{member} are merely very messy to deal
with. In all likelihood these will not be addressed unless and
until someone is willing to write a careful specification that covers
all the cases for the processing of these type
specifiers by \cdf{subtypep}. The requirements stated above were easy
to state and probably suffice for most cases of interest.
\end{new}
\begin{new}
X3J13 voted in January 1989
\issue{ARRAY-TYPE-ELEMENT-TYPE-SEMANTICS}
to change \cdf{subtypep} to give specialized
\cdf{array} and \cdf{complex} type specifiers the same meaning for
purposes of type discrimination as they have for declaration purposes.
Of course, this also applies to such type specifiers as \cdf{vector}
and \cdf{simple-array}
(see section~\ref{SPECIALIZED-TYPE-SPECIFIER-SECTION}).
If \emph{A} and \emph{B} are type specifiers (other than \cdf{*}, which technically
is not a type specifier anyway), then \cd{(array~\emph{A})}
and \cd{(array~\emph{B})} represent the same type in a given implementation
if and only if they denote arrays
of the same specialized representation in that implementation;
otherwise they are disjoint.
To put it another way, they represent the same type
%(and otherwise are disjoint)
if and only if
\cd{(upgraded-array-element-type~'\emph{A})} and
\cd{(upgraded-array-element-type~'\emph{B})} are the same type.
Therefore
\begin{lisp}
(subtypep '(array \emph{A}) '(array \emph{B}))
\end{lisp}
is true if and only if
\cd{(upgraded-array-element-type~'\emph{A})}
is the same type as
\cd{(upgraded-array-element-type~'\emph{B})}.
The \cdf{complex} type specifier is treated in a similar but subtly different
manner.
If \emph{A} and \emph{B} are two type specifiers (but not \cdf{*}, which technically
is not a type specifier anyway), then \cd{(complex~\emph{A})}
and \cd{(complex~\emph{B})} represent the same type in a given implementation
if and only if they refer to complex numbers
of the same specialized representation in that implementation;
otherwise they are disjoint.
Note, however, that there is no function called \cdf{make-complex} that
allows one to specify a particular element type (then to be upgraded);
instead, one must describe specialized complex numbers in terms of
the actual types of the parts from which they were constructed.
There is no number of type (or rather, \emph{representation\/})
\cdf{float} as such; there are only numbers of type \cdf{single-float},
numbers of type \cdf{double-float},
and so on. Therefore we want \cd{(complex single-float)} to
be a subtype of \cd{(complex float)}.
The rule, then, is that \cd{(complex~\emph{A})}
and \cd{(complex~\emph{B})} represent the same type (and otherwise are disjoint)
in a given implementation
if and only if \emph{either} the type \emph{A} is a subtype of \emph{B}, \emph{or}
\cd{(upgraded-complex-part-type~'\emph{A})} and
\cd{(upgraded-complex-part-type~'\emph{B})} are the same type.
In the latter case \cd{(complex~\emph{A})}
and \cd{(complex~\emph{B})} in fact refer to the same specialized representation.
Therefore
\begin{lisp}
(subtypep '(complex \emph{A}) '(complex \emph{B}))
\end{lisp}
is true if and only if the results of
\cd{(upgraded-complex-part-type~'\emph{A})} and
\cd{(upgraded-complex-part-type~'\emph{B})} are the same type.
Under this interpretation
\begin{lisp}
(subtypep '(complex single-float) '(complex float))
\end{lisp}
must be true in all implementations; but
\begin{lisp}
(subtypep '(array single-float) '(array float))
\end{lisp}
is true only in implementations that do not have a specialized array representation
for \cdf{single-float} elements distinct from that for \cdf{float} elements in
general.
\end{new}
\end{defun}
\subsection{Specific Data Type Predicates}
The following predicates test for individual data types.
\begin{defun}[Function]
null object
\cdf{null} is true if its argument is {\emptylist},
and otherwise is false.
This is the same operation performed by the function \cdf{not};
however, \cdf{not} is normally used to invert a Boolean value,
whereas \cdf{null} is normally used to test for an empty list. The programmer
can therefore express \emph{intent} by the choice of function name.
\begin{lisp}
(null x) \EQ\ (typep x 'null) \EQ\ (eq x '{\emptylist})
\end{lisp}
\end{defun}
\begin{defun}[Function]
symbolp object
\cdf{symbolp} is true if its argument is a symbol,
and otherwise is false.
\begin{lisp}
(symbolp x) \EQ\ (typep x 'symbol)
\end{lisp}
\end{defun}
\begin{defun}[Function]
atom object
The predicate \cdf{atom} is true if its argument is not a cons,
and otherwise is false.
Note that \cd{(atom '{\emptylist})} is true, because {\emptylist}$\;\equiv\;${\nil}.
\begin{lisp}
(atom x) \EQ\ (typep x 'atom) \EQ\ (not (typep x 'cons))
\end{lisp}
\end{defun}
\begin{defun}[Function]
consp object
The predicate \cdf{consp} is true if its argument is a cons,
and otherwise is false.
Note that the empty list is not a cons, so
\cd{(consp '{\emptylist})} \EQ\ \cd{(consp '{\nil})} \EV\ {\nil}.
\begin{lisp}
(consp x) \EQ\ (typep x 'cons) \EQ\ (not (typep x 'atom))
\end{lisp}
\end{defun}
\begin{defun}[Function]
listp object
\cdf{listp} is true if its argument is a cons or the empty list {\emptylist},
and otherwise is false. It does not check for whether the list
is a ``true list'' (one terminated by {\nil}) or a ``dotted list''
(one terminated by a non-null atom).
\begin{lisp}
(listp x) \EQ\ (typep x 'list) \EQ\ (typep x '(or cons null))
\end{lisp}
\end{defun}
\begin{defun}[Function]
numberp object
\cdf{numberp} is true if its argument is any kind of number,
and otherwise is false.
\begin{lisp}
(numberp x) \EQ\ (typep x 'number)
\end{lisp}
\end{defun}
\begin{defun}[Function]
integerp object
\cdf{integerp} is true if its argument is an integer, and otherwise
is false.
\begin{lisp}
(integerp x) \EQ\ (typep x 'integer)
\end{lisp}
\end{defun}
\begin{defun}[Function]
rationalp object
\cdf{rationalp} is true if its argument is a rational number (a ratio or
an integer), and otherwise is false.
\begin{lisp}
(rationalp x) \EQ\ (typep x 'rational)
\end{lisp}
\end{defun}
\begin{defun}[Function]
floatp object
\cdf{floatp} is true if its argument is a floating-point number,
and otherwise is false.
\begin{lisp}
(floatp x) \EQ\ (typep x 'float)
\end{lisp}
\end{defun}
\begin{defun}[Function]
realp object
\cdf{realp} is true if its argument is a real number,
and otherwise is false.
\begin{lisp}
(realp x) \EQ\ (typep x 'real)
\end{lisp}
\end{defun}
\begin{defun}[Function]
complexp object
\cdf{complexp} is true if its argument is a complex number,
and otherwise is false.
\begin{lisp}
(complexp x) \EQ\ (typep x 'complex)
\end{lisp}
\end{defun}
\begin{defun}[Function]
characterp object
\cdf{characterp} is true if its argument is a character,
and otherwise is false.
\begin{lisp}
(characterp x) \EQ\ (typep x 'character)
\end{lisp}
\end{defun}
\begin{defun}[Function]
stringp object
\cdf{stringp} is true if its argument is a string,
and otherwise is false.
\begin{lisp}
(stringp x) \EQ\ (typep x 'string)
\end{lisp}
\end{defun}
\begin{defun}[Function]
bit-vector-p object
\cdf{bit-vector-p} is true if its argument is a bit-vector,
and otherwise is false.
\begin{lisp}
(bit-vector-p x) \EQ\ (typep x 'bit-vector)
\end{lisp}
\end{defun}
\begin{defun}[Function]
vectorp object
\cdf{vectorp} is true if its argument is a vector,
and otherwise is false.
\begin{lisp}
(vectorp x) \EQ\ (typep x 'vector)
\end{lisp}
\end{defun}
\begin{defun}[Function]
simple-vector-p object
\cdf{vectorp} is true if its argument is a simple general vector,
and otherwise is false.
\begin{lisp}
(simple-vector-p x) \EQ\ (typep x 'simple-vector)
\end{lisp}
\end{defun}
\begin{defun}[Function]
simple-string-p object
\cdf{simple-string-p} is true if its argument is a simple string,
and otherwise is false.
\begin{lisp}
(simple-string-p x) \EQ\ (typep x 'simple-string)
\end{lisp}
\end{defun}
\begin{defun}[Function]
simple-bit-vector-p object
\cdf{simple-bit-vector-p} is true if its argument is a simple bit-vector,
and otherwise is false.
\begin{lisp}
(simple-bit-vector-p x) \EQ\ (typep x 'simple-bit-vector)
\end{lisp}
\end{defun}
\begin{defun}[Function]
arrayp object
\cdf{arrayp} is true if its argument is an array,
and otherwise is false.
\begin{lisp}
(arrayp x) \EQ\ (typep x 'array)
\end{lisp}
\end{defun}
\begin{defun}[Function]
packagep object
\cdf{packagep} is true if its argument is a package,
and otherwise is false.
\begin{lisp}
(packagep x) \EQ\ (typep x 'package)
\end{lisp}
\end{defun}
\begin{defun}[Function]
functionp object
\begin{obsolete}
\cdf{functionp} is true if its argument is suitable for applying
to arguments, using for example the \cdf{funcall} or \cdf{apply} function.
Otherwise \cdf{functionp} is false.
\cdf{functionp} is always true of symbols, lists whose \emph{car}
is the symbol \cdf{lambda}, any value returned by the \cdf{function}
special operator, and any values returned by the function \cdf{compile}
when the first argument is {\nil}.
\end{obsolete}
\begin{newer}
X3J13 voted in June 1988 \issue{FUNCTION-TYPE}
to define
\begin{lisp}
(functionp x) \EQ\ (typep x 'function)
\end{lisp}
Because the vote also specifies that types \cdf{cons} and \cdf{symbol} are disjoint
from the type \cdf{function}, this is an incompatible change;
now \cdf{functionp} is in fact always false of symbols and lists.
\end{newer}
\end{defun}
\begin{defun}[Function]
compiled-function-p object
\cdf{compiled-function-p} is true if its argument is any compiled code object,
and otherwise is false.
\begin{lisp}
(compiled-function-p x) \EQ\ (typep x 'compiled-function)
\end{lisp}
\end{defun}
\medskip
See also \cdf{standard-char-p}, \cdf{string-char-p},
\cdf{streamp}, \cdf{random-state-p},
\cdf{readtablep},
\cdf{hash-table-p}, and \cdf{pathnamep}.
\section{Equality Predicates}
Common Lisp provides a spectrum of predicates for testing for equality of
two objects: \cdf{eq} (the most specific), \cdf{eql}, \cdf{equal}, and \cdf{equalp}
(the most general). \cdf{eq} and \cdf{equal} have the meanings traditional
in Lisp. \cdf{eql} was added because it is frequently needed, and
\cdf{equalp} was added primarily in order to have a version of \cdf{equal}
that would ignore type differences when comparing numbers
and case differences when comparing characters.
If two objects satisfy any one of these equality predicates,
then they also satisfy all those that are more general.
\begin{defun}[Function]
eq x y
\cd{(eq \emph{x} \emph{y})} is true
if and only if \emph{x} and \emph{y} are the same identical object.
(Implementationally, \emph{x} and \emph{y} are usually
\cdf{eq} if and only if they address the same identical memory location.)
It should be noted that things that print the same are not necessarily \cdf{eq}
to each other. Symbols with the same print name usually are \cdf{eq} to
each other because of the use of the \cdf{intern} function.
However, numbers with the same value
need not be \cdf{eq}, and two similar lists are usually not \cdf{eq}.
For example:
\begin{lisp}
(eq 'a 'b) \textrm{is false.} \\
(eq 'a 'a) \textrm{is true.} \\
(eq 3 3) \textrm{might be true or false, depending on the implementation.} \\
(eq 3 3.0) \textrm{is false.} \\
(eq 3.0 3.0) \textrm{might be true or false, depending on the implementation.} \\
(eq \#c(3 -4) \#c(3 -4)) \\
~~\textrm{might be true or false, depending on the implementation.} \\
(eq \#c(3 -4.0) \#c(3 -4)) \textrm{is false.} \\
(eq (cons 'a 'b) (cons 'a 'c)) \textrm{is false.} \\
(eq (cons 'a 'b) (cons 'a 'b)) \textrm{is false.} \\
(eq '(a . b) '(a . b)) \textrm{might be true or false.} \\
(progn (setq x (cons 'a 'b)) (eq x x)) \textrm{is true.} \\
(progn (setq x '(a . b)) (eq x x)) \textrm{is true.} \\
(eq \#{\Xbackslash}A \#{\Xbackslash}A) \textrm{might be true or false, depending on the implementation.} \\
(eq "Foo" "Foo") \textrm{might be true or false.} \\
(eq "Foo" (copy-seq "Foo")) \textrm{is false.} \\
(eq "FOO" "foo") \textrm{is false.}
\end{lisp}
In Common Lisp, unlike some other Lisp dialects, the implementation
is permitted to make ``copies'' of
characters and numbers at any time. (This permission is granted
because it allows tremendous performance improvements in many
common situations.) The net effect is that
Common Lisp makes no guarantee that \cdf{eq} will be true even when both
its arguments are ``the same thing'' if that thing is a character or number.
For example:
\begin{lisp}
(let ((x 5)) (eq x x)) \textrm{might be true or false.}
\end{lisp}
The predicate \cdf{eql} is the same as \cdf{eq}, except that if the
arguments are characters or numbers of the same type then their
values are compared. Thus \cdf{eql} tells whether two objects
are \emph{conceptually} the same, whereas \cdf{eq} tells whether two
objects are \emph{implementationally} identical. It is for this reason
that \cdf{eql}, not \cdf{eq}, is the default comparison predicate
for the sequence functions defined in chapter~\ref{KSEQUE}.
\beforenoterule
\begin{implementation}
\cdf{eq} simply compares the two given pointers,
so any kind of object that is represented in an ``immediate'' fashion
will indeed have like-valued instances satisfy \cdf{eq}.
In some implementations, for example,
fixnums and characters happen to ``work.''
However, no program should depend on this, as other implementations
of Common Lisp might not use an immediate representation for these data types.
\end{implementation}
\afternoterule
\begin{obsolete}
An additional problem with \cdf{eq} is that the implementation is permitted
to ``collapse'' constants (or portions thereof)
appearing in code to be compiled if they are
\cdf{equal}. An object is considered to be a constant in code to be compiled
if it is a self-evaluating form or is contained in a \cdf{quote} form.
This is why \cd{(eq "Foo" "Foo")} might be true or false; in interpreted
code it would normally be false, because reading in the
form \cd{(eq "Foo" "Foo")} would construct distinct strings for the two
arguments to \cdf{eq}, but the compiler might choose to use the same
identical string or two distinct copies as the two arguments in the
call to \cdf{eq}. Similarly, \cd{(eq '(a . b) '(a . b))} might be true
or false, depending on whether the constant conses appearing in the
\cdf{quote} forms were collapsed by the compiler. However,
\cd{(eq (cons 'a 'b) (cons 'a 'b))} is always false, because every distinct
call to the \cdf{cons} function necessarily produces a new and distinct cons.
\end{obsolete}
\begin{newer}
X3J13 voted in March 1989 \issue{QUOTE-SEMANTICS} to clarify that
\cdf{eval} and \cdf{compile} are not permitted either to copy or
to coalesce (``collapse'') constants (see \cdf{eq})
appearing in the code they process; the resulting
program behavior must refer to objects that are \cdf{eql} to the
corresponding objects in the source code.
Only the \cdf{compile-file}/\cdf{load} process is permitted
to copy or coalesce constants (see section~\ref{COMPILER-SECTION}).
\end{newer}
\end{defun}
\begin{defun}[Function]
eql x y
The \cdf{eql} predicate is true if its arguments are \cdf{eq},
or if they are numbers of the same type with the same value,
or if they are character objects
that represent the same character.
For example:
\begin{lisp}
(eql 'a 'b) \textrm{is false.} \\
(eql 'a 'a) \textrm{is true.} \\
(eql 3 3) \textrm{is true.} \\
(eql 3 3.0) \textrm{is false.} \\
(eql 3.0 3.0) \textrm{is true.} \\
(eql \#c(3 -4) \#c(3 -4)) \textrm{is true.} \\
(eql \#c(3 -4.0) \#c(3 -4)) \textrm{is false.} \\
(eql (cons 'a 'b) (cons 'a 'c)) \textrm{is false.} \\
(eql (cons 'a 'b) (cons 'a 'b)) \textrm{is false.} \\
(eql '(a . b) '(a . b)) \textrm{might be true or false.} \\
(progn (setq x (cons 'a 'b)) (eql x x)) \textrm{is true.} \\
(progn (setq x '(a . b)) (eql x x)) \textrm{is true.} \\
(eql \#{\Xbackslash}A \#{\Xbackslash}A) \textrm{is true.} \\
(eql "Foo" "Foo") \textrm{might be true or false.} \\
(eql "Foo" (copy-seq "Foo")) \textrm{is false.} \\
(eql "FOO" "foo") \textrm{is false.}
\end{lisp}
Normally \cd{(eql 1.0s0 1.0d0)} would be false, under the assumption
that \cd{1.0s0} and \cd{1.0d0} are of distinct data types.
However, implementations that do not provide four distinct floating-point
formats are permitted to ``collapse'' the four formats into some
smaller number of them; in such an implementation \cd{(eql 1.0s0 1.0d0)}
might be true. The predicate \cdf{=} will compare
the values of two numbers even if the numbers are of different types.
If an implementation supports positive and negative zeros as distinct
values (as in the IEEE proposed standard floating-point format),
then \cd{(eql 0.0 -0.0)} will be false. Otherwise, when the syntax
\cd{-0.0} is read it will be interpreted as the value \cd{0.0},
and so \cd{(eql 0.0 -0.0)} will be true. The predicate \cdf{=}
differs from \cdf{eql} in that \cd{(= 0.0 -0.0)} will always be true,
because \cdf{=} compares the mathematical values of its operands,
whereas \cdf{eql} compares the representational values, so to speak.
Two complex numbers are considered to be \cdf{eql}
if their real parts are \cdf{eql} and their imaginary parts are \cdf{eql}.
For example, \cd{(eql \#C(4 5) \#C(4 5))} is true and
\cd{(eql \#C(4 5) \#C(4.0 5.0))} is false.
Note that while \cd{(eql \#C(5.0 0.0) 5.0)} is false,
\cd{(eql \#C(5 0) 5)} is true.
In the case of \cd{(eql \#C(5.0 0.0) 5.0)} the
two arguments are of different types
and so cannot satisfy \cdf{eql}; that's all there is to it.
In the case of \cd{(eql \#C(5 0) 5)}, however,
\cd{\#C(5 0)} is not a complex number but
is always automatically reduced by the rule of complex
canonicalization to the integer \cd{5},
just as the apparent ratio \cd{20/4} is always simplified to \cd{5}.
The case of \cd{(eql "Foo" "Foo")} is discussed above in the description
of \cdf{eq}. While \cdf{eql} compares the values of numbers and
characters, it does not compare the contents of strings. To compare
the characters of two strings, one should use \cdf{equal}, \cdf{equalp},
\cdf{string=}, or \cdf{string-equal}.
\end{defun}
\begin{defun}[Function]
equal x y
The \cdf{equal} predicate is true if its arguments are structurally similar
(isomorphic) objects. A rough rule of thumb is that two objects
are \cdf{equal} if and only if their printed representations are the same.
Numbers and characters are compared as for \cdf{eql}.
Symbols are compared as for \cdf{eq}. This method
of comparing symbols can violate the rule
of thumb for \cdf{equal} and printed representations,
but only in the infrequently occurring case of two distinct
symbols with the same print name.
Certain objects that have components are \cdf{equal} if they are of the same
type and corresponding components are \cdf{equal}.
This test is implemented in a recursive manner and may fail to
terminate for circular structures.
For conses, \cdf{equal} is defined recursively as
the two \emph{car}'s being \cdf{equal} and the two \emph{cdr}'s being
\cdf{equal}.
Two arrays are \cdf{equal} only if they are \cdf{eq},
with one exception:
strings and bit-vectors are compared element-by-element.
If either argument has a fill pointer, the fill pointer limits
the number of elements examined by \cdf{equal}.
Uppercase and lowercase letters in strings are considered by
\cdf{equal} to be distinct. (In contrast, \cdf{equalp} ignores
case distinctions in strings.)
Two pathname objects are \cdf{equal} if and only if
all the corresponding components
(host, device, and so on) are equivalent. (Whether or not
uppercase and lowercase letters are considered equivalent
in strings appearing in components depends on the file
name conventions of the file system.) Pathnames
that are \cdf{equal} should be functionally equivalent.
\cdf{equal} never recursively
descends any structure or data type other than the ones explicitly
described above: conses, bit-vectors, strings, and pathnames.
Numbers and characters are compared as if by \cdf{eql}, and all other
data objects are compared as if by \cdf{eq}.
\begin{lisp}
(equal 'a 'b) \textrm{is false.} \\
(equal 'a 'a) \textrm{is true.} \\
(equal 3 3) \textrm{is true.} \\
(equal 3 3.0) \textrm{is false.} \\
(equal 3.0 3.0) \textrm{is true.} \\
(equal \#c(3 -4) \#c(3 -4)) \textrm{is true.} \\
(equal \#c(3 -4.0) \#c(3 -4)) \textrm{is false.} \\
(equal (cons 'a 'b) (cons 'a 'c)) \textrm{is false.} \\
(equal (cons 'a 'b) (cons 'a 'b)) \textrm{is true.} \\
(equal '(a . b) '(a . b)) \textrm{is true.} \\
(progn (setq x (cons 'a 'b)) (equal x x)) \textrm{is true.} \\
(progn (setq x '(a . b)) (equal x x)) \textrm{is true.} \\
(equal \#{\Xbackslash}A \#{\Xbackslash}A) \textrm{is true.} \\
(equal "Foo" "Foo") \textrm{is true.} \\
(equal "Foo" (copy-seq "Foo")) \textrm{is true.} \\
(equal "FOO" "foo") \textrm{is false.}
\end{lisp}
To compare a tree of conses using \cdf{eql}
(or any other desired predicate) on the leaves, use \cdf{tree-equal}.
\end{defun}
\begin{defun}[Function]
equalp x y
Two objects are \cdf{equalp} if they are \cdf{equal};
if they are characters and satisfy \cdf{char-equal},
which ignores alphabetic case and certain other attributes of characters;
if they are numbers and have the same numerical value,
even if they are of different types;
or if they have components that are all \cdf{equalp}.
Objects that have components are \cdf{equalp} if they are of the same
type and corresponding components are \cdf{equalp}.
This test is implemented in a recursive manner and may fail to
terminate for circular structures.
For conses, \cdf{equalp} is defined recursively as
the two \emph{car}'s being \cdf{equalp} and the two \emph{cdr}'s being
\cdf{equalp}.
Two arrays are \cdf{equalp} if and only if they have the same
number of dimensions, the dimensions match,
and the corresponding components are \cdf{equalp}.
The specializations need not match; for example,
a string and a general array that happens to contain the same characters
will be \cdf{equalp} (though definitely not \cdf{equal}).
If either argument has a fill pointer, the fill pointer limits
the number of elements examined by \cdf{equalp}.
Because \cdf{equalp} performs element-by-element comparisons
of strings and ignores the alphabetic case of characters,
case distinctions are therefore also ignored when \cdf{equalp} compares
strings.
Two symbols can be \cdf{equalp} only if they are \cdf{eq}, that is, the same
identical object.
\begin{new}
X3J13 voted in June 1989
\issue{EQUAL-STRUCTURE}
to specify that \cdf{equalp} compares components
of hash tables (see below), and to
clarify that otherwise \cdf{equalp} never recursively
descends any structure or data type other than the ones explicitly
described above: conses, arrays (including bit-vectors and strings), and pathnames.
Numbers are compared for numerical equality (see \cdf{=}),
characters are compared as if by \cdf{char-equal}, and all other
data objects are compared as if by \cdf{eq}.
Two hash tables are considered the same by \cdf{equalp} if and only if
they satisfy a four-part test:
\begin{itemize}
\item They must be
of the same kind; that is, equivalent \cd{:test} arguments were given to
\cdf{make-hash-table} when the two hash tables were created.
\item They must have the same number of entries (see \cdf{hash-table-count}).
\item For every entry (\emph{key1}, \emph{value1\/}) in one hash table
there must be a corresponding entry (\emph{key2}, \emph{value2\/}) in the
other, such that \emph{key1} and \emph{key2} are considered to be the same
by the \cd{:test} function associated with the hash tables.
\item For every entry (\emph{key1}, \emph{value1\/}) in one hash table
and its corresponding entry (\emph{key2}, \emph{value2\/}) in the
other, such that \emph{key1} and \emph{key2} are the same,
\cdf{equalp} must be true of \emph{value1} and \emph{value2}.
\end{itemize}
The four parts of this test are carried out in the order shown, and
if some part of the test fails, \cdf{equalp} returns \cdf{nil} and
the other parts of the test are not attempted.
If \cdf{equalp} must compare two structures and the \cdf{defstruct}
definition for one used the \cd{:type} option and the other did not,
then \cdf{equalp} returns \cdf{nil}.
If \cdf{equalp} must compare two structures and neither \cdf{defstruct}
definition used the \cd{:type} option,
then \cdf{equalp} returns \cdf{t} if and only if the structures have the
same type (that is, the same \cdf{defstruct} name) and the values
of all corresponding slots (slots having the same name) are \cdf{equalp}.
As part of the X3J13 discussion of this issue
the following observations were made.
Object equality is not a concept for which there is a uniquely
determined correct algorithm. The appropriateness of an equality
predicate can be judged only in the context of the needs of some
particular program. Although these functions take any type of
argument and their names sound very generic, \cdf{equal} and \cdf{equalp} are
not appropriate for every application. Any decision to use or not
use them should be determined by what they are documented to do
rather than by any abstract characterization of their function. If
neither \cdf{equal} nor \cdf{equalp} is found to be appropriate in a particular
situation, programmers are encouraged to create another operator
that is appropriate rather than blame \cdf{equal} or \cdf{equalp} for ``doing
the wrong thing.''
\end{new}
\begin{new}
Note that one consequence
of the vote to change the rules of
floating-point contagion
\issue{CONTAGION-ON-NUMERICAL-COMPARISONS}
(described in section~\ref{PRECISION-CONTAGION-COERCION-SECTION})
is to make \cdf{equalp}
a true equivalence relation on numbers.
\end{new}
\begin{lisp}
(equalp 'a 'b) \textrm{is false.} \\
(equalp 'a 'a) \textrm{is true.} \\
(equalp 3 3) \textrm{is true.} \\
(equalp 3 3.0) \textrm{is true.} \\
(equalp 3.0 3.0) \textrm{is true.} \\
(equalp \#c(3 -4) \#c(3 -4)) \textrm{is true.} \\
(equalp \#c(3 -4.0) \#c(3 -4)) \textrm{is true.} \\
(equalp (cons 'a 'b) (cons 'a 'c)) \textrm{is false.} \\
(equalp (cons 'a 'b) (cons 'a 'b)) \textrm{is true.} \\
(equalp '(a . b) '(a . b)) \textrm{is true.} \\
(progn (setq x (cons 'a 'b)) (equalp x x)) \textrm{is true.} \\
(progn (setq x '(a . b)) (equalp x x)) \textrm{is true.} \\
(equalp \#{\Xbackslash}A \#{\Xbackslash}A) \textrm{is true.} \\
(equalp "Foo" "Foo") \textrm{is true.} \\
(equalp "Foo" (copy-seq "Foo")) \textrm{is true.} \\
(equalp "FOO" "foo") \textrm{is true.}
\end{lisp}
\end{defun}
\section{Logical operators}
Common Lisp provides three operators on Boolean values: \cdf{and}, \cdf{or},
and \cdf{not}. Of these, \cdf{and} and \cdf{or}
are also control structures because their arguments are evaluated
conditionally.
The function \cdf{not} necessarily examines its single argument, and so
is a simple function.
\begin{defun}[Function]
not x
\cdf{not} returns {\true} if \emph{x} is {\false}, and otherwise returns {\false}.
It therefore inverts its argument considered as a Boolean value.
\cdf{null} is the same as \cdf{not}; both functions are included for the sake
of clarity. As a matter of style,
it is customary to use \cdf{null} to check whether something is the empty list
and to use \cdf{not} to invert the sense of a logical value.
\end{defun}
\begin{defmac}
and {form}*
\cd{(and \emph{form1} \emph{form2} ... )} evaluates each \emph{form}, one at a time,
from left to right. If any \emph{form} evaluates to {\false}, the value {\nil}
is immediately returned without evaluating the remaining
\emph{form\/}s. If every \emph{form} but the last evaluates to a non-{\false} value,
\cdf{and} returns whatever the last \emph{form} returns.
Therefore in general \cdf{and} can be used both for logical operations,
where {\false} stands for \emph{false} and non-{\false} values stand for \emph{true},
and as a conditional expression.
An example follows.
\begin{lisp}
(if (and (>= n 0) \\
~~~~~~~~~(< n (length a-simple-vector)) \\
~~~~~~~~~(eq (elt a-simple-vector n) 'foo)) \\
~~~~(princ "Foo!"))
\end{lisp}
The above expression prints \cd{Foo!} if element \cd{n} of \cd{a-simple-vector}
is the symbol \cd{foo}, provided also that \cdf{n} is indeed a valid index
for \cdf{a-simple-vector}. Because \cdf{and} guarantees left-to-right testing
of its parts, \cdf{elt} is not called if \cd{n} is out of range.
To put it another way,
the \cdf{and} special operator does \emph{short-circuit} Boolean evaluation,
like the \textbf{and then} operator in Ada
and what in some Pascal-like languages is called \textbf{cand} (for ``conditional
and''); the Lisp \cdf{and} special operator is
unlike the Pascal or Ada \textbf{and} operator,
which always evaluates both arguments.
In the previous example writing
\begin{lisp}
(and (>= n 0) \\
~~~~~(< n (length a-simple-vector)) \\
~~~~~(eq (elt a-simple-vector n) 'foo) \\
~~~~~(princ "Foo!"))
\end{lisp}
would accomplish the same thing. The difference is purely stylistic.
Some programmers never use expressions containing side effects
within \cdf{and}, preferring to use \cdf{if} or \cdf{when} for that purpose.
From the general definition, one can deduce that
\cd{(and \emph{x})} \EQ\ \emph{x}. Also,
\cd{(and)} evaluates to {\true}, which is an identity for this operation.
One can define \cdf{and} in terms of \cdf{cond} in this way:
\begin{lisp}
(and \emph{x} \emph{y} \emph{z} ... \emph{w}) \EQ\ (cond \=((not \emph{x}) {\false}) \\
\>((not \emph{y}) {\false}) \\
\>((not \emph{z}) {\false}) \\
\>$\ldots$ \\
\>({\true} \emph{w}))
\end{lisp}
See \cdf{if} and \cdf{when}, which are sometimes stylistically
more appropriate than \cdf{and} for conditional purposes.
If it is necessary to test whether a predicate is true
of all elements of a list or vector (element 0 \emph{and} element 1 \emph{and}
element 2 \emph{and} $\ldots$), then the function \cdf{every} may be useful.
\end{defmac}
\begin{defmac}
or {form}*
\cd{(or \emph{form1} \emph{form2} ... )} evaluates each \emph{form}, one at a time,
from left to right. If any \emph{form} other than the last
evaluates to something other than {\false},
\cdf{or}
immediately returns that non-{\false} value without evaluating the remaining
\emph{form\/}s. If every \emph{form} but the last evaluates to {\false},
\cdf{or} returns whatever evaluation of the last of the \emph{form\/}s returns.
Therefore in general \cdf{or} can be used both for logical operations,
where {\false} stands for \emph{false} and non-{\false} values stand for \emph{true},
and as a conditional expression.
To put it another way,
the \cdf{or} special operator does \emph{short-circuit} Boolean evaluation,
like the \textbf{or else} operator in Ada
and what in some Pascal-like languages is called \textbf{cor} (for ``conditional
or''); the Lisp \cdf{or} special operator is
unlike the Pascal or Ada \textbf{or} operator,
which always evaluates both arguments.
From the general definition, one can deduce that
\cd{(or \emph{x})} \EQ\ \emph{x}. Also,
\cd{(or)} evaluates to {\nil}, which is the identity for this operation.
One can define \cdf{or} in terms of \cdf{cond} in this way:
\begin{lisp}
(or \emph{x} \emph{y} \emph{z} ... \emph{w}) \EQ\ (cond (\emph{x}) (\emph{y}) (\emph{z}) ... ({\true} \emph{w}))
\end{lisp}
See \cdf{if} and \cdf{unless}, which are sometimes
stylistically more appropriate than \cdf{or} for conditional purposes.
If it is necessary to test whether a predicate is true of
one or more elements of a list or vector (element 0 \emph{or} element 1 \emph{or}
element 2 \emph{or} $\ldots$), then the function \cdf{some} may be useful.