113cb1da2dacf22615f597c7b2012692e7a954b5
[sbcl.git] / src / compiler / fndb.lisp
1 ;;;; This file defines all the standard functions to be known
2 ;;;; functions. Each function has type and side-effect information,
3 ;;;; and may also have IR1 optimizers.
4
5 ;;;; This software is part of the SBCL system. See the README file for
6 ;;;; more information.
7 ;;;;
8 ;;;; This software is derived from the CMU CL system, which was
9 ;;;; written at Carnegie Mellon University and released into the
10 ;;;; public domain. The software is in the public domain and is
11 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
12 ;;;; files for more information.
13
14 (in-package "SB!C")
15 \f
16 ;;;; information for known functions:
17
18 (defknown coerce (t type-specifier) t
19   ;; Note:
20   ;; (1) This is not FLUSHABLE because it's defined to signal errors.
21   ;; (2) It's not worth trying to make this FOLDABLE in the
22   ;;     cross-compiler,because
23   ;;       (a) it would probably be really hard to make all the 
24   ;;           tricky issues (e.g. which specialized array types are
25   ;;           supported) match between cross-compiler and target
26   ;;           compiler, and besides
27   ;;       (b) leaving it not FOLDABLE lets us use the idiom
28   ;;               (COERCE FOO 'SOME-SPECIALIZED-ARRAY-TYPE-OR-ANOTHER)
29   ;;           as a way of delaying the generation of specialized
30   ;;           array types until runtime, which helps us keep the
31   ;;           cross-compiler's dumper relatively simple and which
32   ;;           lets us preserve distinctions which might not even exist
33   ;;           on the cross-compilation host (because ANSI doesn't
34   ;;           guarantee that specialized array types exist there).
35   ;; FIXME: It's actually not clear that COERCE on non-NUMBER types
36   ;; is FOLDABLE at all. Check this.
37   (movable #-sb-xc-host foldable)
38   ;; :DERIVE-TYPE RESULT-TYPE-SPEC-NTH-ARG 2 ? Nope... (COERCE 1 'COMPLEX)
39   ;; returns REAL/INTEGER, not COMPLEX.
40   )
41 (defknown list-to-vector* (list type-specifier) vector)
42 (defknown vector-to-vector* (vector type-specifier) vector)
43
44 (defknown type-of (t) t (foldable flushable))
45
46 ;;; These can be affected by type definitions, so they're not FOLDABLE.
47 (defknown (upgraded-complex-part-type upgraded-array-element-type)
48           (type-specifier &optional lexenv-designator) type-specifier
49   (unsafely-flushable))
50 \f
51 ;;;; from the "Predicates" chapter:
52
53 ;;; FIXME: Is it right to have TYPEP (and TYPE-OF, elsewhere; and
54 ;;; perhaps SPECIAL-OPERATOR-P and others) be FOLDABLE in the
55 ;;; cross-compilation host? After all, some type relationships (e.g.
56 ;;; FIXNUMness) might be different between host and target. Perhaps
57 ;;; this property should be protected by #-SB-XC-HOST? Perhaps we need
58 ;;; 3-stage bootstrapping after all? (Ugh! It's *so* slow already!)
59 (defknown typep (t type-specifier &optional lexenv-designator) t
60    ;; Unlike SUBTYPEP or UPGRADED-ARRAY-ELEMENT-TYPE and friends, this
61    ;; seems to be FOLDABLE. Like SUBTYPEP, it's affected by type
62    ;; definitions, but unlike SUBTYPEP, there should be no way to make
63    ;; a TYPEP expression with constant arguments which doesn't return
64    ;; an error before the type declaration (because of undefined
65    ;; type). E.g. you can do
66    ;;   (SUBTYPEP 'INTEGER 'FOO) => NIL, NIL
67    ;;   (DEFTYPE FOO () T)
68    ;;   (SUBTYPEP 'INTEGER 'FOO) => T, T
69    ;; but the analogous
70    ;;   (TYPEP 12 'FOO)
71    ;;   (DEFTYPE FOO () T)
72    ;;   (TYPEP 12 'FOO)
73    ;; doesn't work because the first call is an error.
74    ;;
75    ;; (UPGRADED-ARRAY-ELEMENT-TYPE and UPGRADED-COMPLEX-PART-TYPE have
76    ;; behavior like SUBTYPEP in this respect, not like TYPEP.)
77    (foldable))
78 (defknown subtypep (type-specifier type-specifier &optional lexenv-designator)
79   (values boolean boolean)
80   ;; This is not FOLDABLE because its value is affected by type
81   ;; definitions.
82   ;;
83   ;; FIXME: Is it OK to fold this when the types have already been
84   ;; defined? Does the code inherited from CMU CL already do this?
85   (unsafely-flushable))
86
87 (defknown (null symbolp atom consp listp numberp integerp rationalp floatp
88                 complexp characterp stringp bit-vector-p vectorp
89                 simple-vector-p simple-string-p simple-bit-vector-p arrayp
90                 sb!xc:packagep functionp compiled-function-p not)
91   (t) boolean (movable foldable flushable))
92
93 (defknown (eq eql) (t t) boolean (movable foldable flushable))
94 (defknown (equal equalp) (t t) boolean (foldable flushable recursive))
95 \f
96 ;;;; classes
97
98 (sb!xc:deftype name-for-class () t)
99 (defknown classoid-name (classoid) name-for-class (flushable))
100 (defknown find-classoid (name-for-class &optional t lexenv-designator)
101   (or classoid null) ())
102 (defknown classoid-of (t) classoid (flushable))
103 (defknown layout-of (t) layout (flushable))
104 (defknown copy-structure (structure-object) structure-object
105   (flushable unsafe))
106 \f
107 ;;;; from the "Control Structure" chapter:
108
109 ;;; This is not FLUSHABLE, since it's required to signal an error if
110 ;;; unbound.
111 (defknown (symbol-value) (symbol) t ())
112 ;;; From CLHS, "If the symbol is globally defined as a macro or a
113 ;;; special operator, an object of implementation-dependent nature and
114 ;;; identity is returned. If the symbol is not globally defined as
115 ;;; either a macro or a special operator, and if the symbol is fbound,
116 ;;; a function object is returned".  Our objects of
117 ;;; implementation-dependent nature happen to be functions.
118 (defknown (symbol-function) (symbol) function ())
119
120 (defknown boundp (symbol) boolean (flushable))
121 (defknown fboundp ((or symbol cons)) boolean (unsafely-flushable explicit-check))
122 (defknown special-operator-p (symbol) t
123   ;; The set of special operators never changes.
124   (movable foldable flushable))
125 (defknown set (symbol t) t (unsafe)
126   :derive-type #'result-type-last-arg)
127 (defknown fdefinition ((or symbol cons)) function (unsafe explicit-check))
128 (defknown %set-fdefinition ((or symbol cons) function) function
129   (unsafe explicit-check))
130 (defknown makunbound (symbol) symbol)
131 (defknown fmakunbound ((or symbol cons)) (or symbol cons)
132   (unsafe explicit-check))
133 (defknown (get-setf-method get-setf-method-multiple-value)
134   ((or list symbol) &optional lexenv-designator)
135   (values list list list form form)
136   (flushable))
137 (defknown apply (callable t &rest t) *) ; ### Last arg must be List...
138 (defknown funcall (callable &rest t) *)
139
140 (defknown (mapcar maplist) (callable list &rest list) list
141   (call))
142
143 ;;; According to CLHS the result must be a LIST, but we do not check
144 ;;; it.
145 (defknown (mapcan mapcon) (callable list &rest list) t
146   (call))
147
148 (defknown (mapc mapl) (callable list &rest list) list (foldable call))
149
150 ;;; We let VALUES-LIST be foldable, since constant-folding will turn
151 ;;; it into VALUES. VALUES is not foldable, since MV constants are
152 ;;; represented by a call to VALUES.
153 (defknown values (&rest t) * (movable flushable unsafe))
154 (defknown values-list (list) * (movable foldable unsafely-flushable))
155 \f
156 ;;;; from the "Macros" chapter:
157
158 (defknown macro-function (symbol &optional lexenv-designator)
159   (or function null)
160   (flushable))
161 (defknown (macroexpand macroexpand-1) (t &optional lexenv-designator)
162   (values form &optional boolean))
163
164 (defknown compiler-macro-function (t &optional lexenv-designator)
165   (or function null)
166   (flushable))
167 \f
168 ;;;; from the "Declarations" chapter:
169
170 (defknown proclaim (list) (values) (recursive))
171
172 ;;;; from the "Symbols" chapter:
173
174 (defknown get (symbol t &optional t) t (flushable))
175 (defknown remprop (symbol t) t)
176 (defknown symbol-plist (symbol) list (flushable))
177 (defknown getf (list t &optional t) t (foldable flushable))
178 (defknown get-properties (list list) (values t t list) (foldable flushable))
179 (defknown symbol-name (symbol) simple-string (movable foldable flushable))
180 (defknown make-symbol (string) symbol (flushable))
181 (defknown copy-symbol (symbol &optional t) symbol (flushable))
182 (defknown gensym (&optional (or string unsigned-byte)) symbol ())
183 (defknown symbol-package (symbol) (or sb!xc:package null) (flushable))
184 (defknown keywordp (t) boolean (flushable))       ; If someone uninterns it...
185 \f
186 ;;;; from the "Packages" chapter:
187
188 (sb!xc:deftype package-designator () '(or stringable sb!xc:package))
189 (sb!xc:deftype symbols () '(or list symbol))
190
191 (defknown gentemp (&optional string package-designator) symbol)
192
193 (defknown make-package (stringable &key
194                                    (:use list)
195                                    (:nicknames list)
196                                    ;; ### extensions...
197                                    (:internal-symbols index)
198                                    (:external-symbols index))
199   sb!xc:package)
200 (defknown find-package (package-designator) (or sb!xc:package null)
201   (flushable))
202 (defknown package-name (package-designator) (or simple-string null)
203   (unsafely-flushable))
204 (defknown package-nicknames (package-designator) list (unsafely-flushable))
205 (defknown rename-package (package-designator package-designator &optional list)
206   sb!xc:package)
207 (defknown package-use-list (package-designator) list (unsafely-flushable))
208 (defknown package-used-by-list (package-designator) list (unsafely-flushable))
209 (defknown package-shadowing-symbols (package-designator) list (unsafely-flushable))
210 (defknown list-all-packages () list (flushable))
211 (defknown intern (string &optional package-designator)
212   (values symbol (member :internal :external :inherited nil))
213   ())
214 (defknown find-symbol (string &optional package-designator)
215   (values symbol (member :internal :external :inherited nil))
216   (flushable))
217 (defknown (export import) (symbols &optional package-designator) (eql t))
218 (defknown unintern (symbol &optional package-designator) boolean)
219 (defknown unexport (symbols &optional package-designator) (eql t))
220 (defknown shadowing-import (symbols &optional package-designator) (eql t))
221 (defknown shadow ((or symbol string list) &optional package-designator)
222   (eql t))
223 (defknown (use-package unuse-package)
224   ((or list package-designator) &optional package-designator) (eql t))
225 (defknown find-all-symbols (stringable) list (flushable))
226 \f
227 ;;;; from the "Numbers" chapter:
228
229 (defknown zerop (number) boolean (movable foldable flushable explicit-check))
230 (defknown (plusp minusp) (real) boolean
231   (movable foldable flushable explicit-check))
232 (defknown (oddp evenp) (integer) boolean
233   (movable foldable flushable explicit-check))
234 (defknown (= /=) (number &rest number) boolean
235   (movable foldable flushable explicit-check))
236 (defknown (< > <= >=) (real &rest real) boolean
237   (movable foldable flushable explicit-check))
238 (defknown (max min) (real &rest real) real
239   (movable foldable flushable explicit-check))
240
241 (defknown + (&rest number) number
242   (movable foldable flushable explicit-check))
243 (defknown - (number &rest number) number
244   (movable foldable flushable explicit-check))
245 (defknown * (&rest number) number
246   (movable foldable flushable explicit-check))
247 (defknown / (number &rest number) number
248   (movable foldable flushable explicit-check))
249 (defknown (1+ 1-) (number) number
250   (movable foldable flushable explicit-check))
251
252 (defknown conjugate (number) number
253   (movable foldable flushable explicit-check))
254
255 (defknown gcd (&rest integer) unsigned-byte
256   (movable foldable flushable explicit-check)
257   #|:derive-type 'boolean-result-type|#)
258 (defknown lcm (&rest integer) unsigned-byte
259   (movable foldable flushable explicit-check))
260
261 #+sb-xc-host ; (See CROSS-FLOAT-INFINITY-KLUDGE.)
262 (defknown exp (number) irrational
263   (movable foldable flushable explicit-check recursive)
264   :derive-type #'result-type-float-contagion)
265
266 #-sb-xc-host ; (See CROSS-FLOAT-INFINITY-KLUDGE.)
267 (defknown exp (number) irrational
268   (movable foldable flushable explicit-check recursive))
269
270 (defknown expt (number number) number
271   (movable foldable flushable explicit-check recursive))
272 (defknown log (number &optional real) irrational
273   (movable foldable flushable explicit-check))
274 (defknown sqrt (number) irrational
275   (movable foldable flushable explicit-check))
276 (defknown isqrt (unsigned-byte) unsigned-byte
277   (movable foldable flushable explicit-check recursive))
278
279 (defknown (abs phase signum) (number) number
280   (movable foldable flushable explicit-check))
281 (defknown cis (real) (complex float)
282   (movable foldable flushable explicit-check))
283
284 #+sb-xc-host ; (See CROSS-FLOAT-INFINITY-KLUDGE.)
285 (progn
286 (defknown (sin cos) (number)
287   (or (float -1.0 1.0) (complex float))
288   (movable foldable flushable explicit-check recursive)
289   :derive-type #'result-type-float-contagion)
290
291 (defknown atan
292   (number &optional real) irrational
293   (movable foldable unsafely-flushable explicit-check recursive)
294   :derive-type #'result-type-float-contagion)
295
296 (defknown (tan sinh cosh tanh asinh)
297   (number) irrational (movable foldable flushable explicit-check recursive)
298   :derive-type #'result-type-float-contagion)
299 ) ; PROGN
300
301 #-sb-xc-host ; (See CROSS-FLOAT-INFINITY-KLUDGE.)
302 (progn
303 (defknown (sin cos) (number)
304   (or (float -1.0 1.0) (complex float))
305   (movable foldable flushable explicit-check recursive))
306
307 (defknown atan
308   (number &optional real) irrational
309   (movable foldable unsafely-flushable explicit-check recursive))
310
311 (defknown (tan sinh cosh tanh asinh)
312   (number) irrational (movable foldable flushable explicit-check recursive))
313 ) ; PROGN
314
315 (defknown (asin acos acosh atanh)
316   (number) irrational
317   (movable foldable flushable explicit-check recursive))
318
319 (defknown float (real &optional float) float
320   (movable foldable flushable explicit-check))
321
322 (defknown (rational) (real) rational
323   (movable foldable flushable explicit-check))
324
325 (defknown (rationalize) (real) rational
326   (movable foldable flushable explicit-check recursive))
327
328 (defknown (numerator denominator) (rational) integer
329   (movable foldable flushable))
330
331 (defknown (floor ceiling truncate round)
332   (real &optional real) (values integer real)
333   (movable foldable flushable explicit-check))
334
335 (defknown (mod rem) (real real) real
336   (movable foldable flushable explicit-check))
337
338 (defknown (ffloor fceiling fround ftruncate)
339   (real &optional real) (values float float)
340   (movable foldable flushable explicit-check))
341
342 (defknown decode-float (float) (values float float-exponent float)
343   (movable foldable flushable explicit-check))
344 (defknown scale-float (float float-exponent) float
345   (movable foldable flushable explicit-check))
346 (defknown float-radix (float) float-radix
347   (movable foldable flushable explicit-check))
348 (defknown float-sign (float &optional float) float
349   (movable foldable flushable explicit-check))
350 (defknown (float-digits float-precision) (float) float-digits
351   (movable foldable flushable explicit-check))
352 (defknown integer-decode-float (float)
353           (values integer float-exponent (member -1 1))
354           (movable foldable flushable explicit-check))
355
356 (defknown complex (real &optional real) number
357   (movable foldable flushable explicit-check))
358
359 (defknown (realpart imagpart) (number) real (movable foldable flushable))
360
361 (defknown (logior logxor logand logeqv) (&rest integer) integer
362   (movable foldable flushable explicit-check))
363
364 (defknown (lognand lognor logandc1 logandc2 logorc1 logorc2)
365           (integer integer) integer
366   (movable foldable flushable explicit-check))
367
368 (defknown boole (boole-code integer integer) integer
369   (movable foldable flushable))
370
371 (defknown lognot (integer) integer (movable foldable flushable explicit-check))
372 (defknown logtest (integer integer) boolean (movable foldable flushable))
373 (defknown logbitp (bit-index integer) boolean (movable foldable flushable))
374 (defknown ash (integer integer) integer
375   (movable foldable flushable explicit-check))
376 (defknown (logcount integer-length) (integer) bit-index
377   (movable foldable flushable explicit-check))
378 ;;; FIXME: According to the ANSI spec, it's legal to use any
379 ;;; nonnegative indices for BYTE arguments, not just BIT-INDEX. It's
380 ;;; hard to come up with useful ways to do this, but it is possible to
381 ;;; come up with *legal* ways to do this, so it would be nice
382 ;;; to fix this so we comply with the spec.
383 (defknown byte (bit-index bit-index) byte-specifier
384   (movable foldable flushable))
385 (defknown (byte-size byte-position) (byte-specifier) bit-index
386   (movable foldable flushable))
387 (defknown ldb (byte-specifier integer) integer (movable foldable flushable))
388 (defknown ldb-test (byte-specifier integer) boolean
389   (movable foldable flushable))
390 (defknown mask-field (byte-specifier integer) integer
391   (movable foldable flushable))
392 (defknown dpb (integer byte-specifier integer) integer
393   (movable foldable flushable))
394 (defknown deposit-field (integer byte-specifier integer) integer
395   (movable foldable flushable))
396 (defknown random ((real (0)) &optional random-state) (real 0) ())
397 (defknown make-random-state (&optional (or (member nil t) random-state))
398   random-state (flushable))
399 (defknown random-state-p (t) boolean (movable foldable flushable))
400 \f
401 ;;;; from the "Characters" chapter:
402 (defknown (standard-char-p graphic-char-p alpha-char-p
403                            upper-case-p lower-case-p both-case-p alphanumericp)
404   (character) boolean (movable foldable flushable))
405
406 (defknown digit-char-p (character &optional (integer 2 36))
407   (or (integer 0 35) null) (movable foldable flushable))
408
409 (defknown (char= char/= char< char> char<= char>= char-equal char-not-equal
410                  char-lessp char-greaterp char-not-greaterp char-not-lessp)
411   (character &rest character) boolean (movable foldable flushable))
412
413 (defknown character (t) character (movable foldable unsafely-flushable))
414 (defknown char-code (character) char-code (movable foldable flushable))
415 (defknown (char-upcase char-downcase) (character) character
416   (movable foldable flushable))
417 (defknown digit-char (unsigned-byte &optional (integer 2 36))
418   (or character null) (movable foldable flushable))
419 (defknown char-int (character) char-code (movable foldable flushable))
420 (defknown char-name (character) (or simple-string null)
421   (movable foldable flushable))
422 (defknown name-char (stringable) (or character null)
423   (movable foldable flushable))
424 (defknown code-char (char-code) base-char
425   ;; By suppressing constant folding on CODE-CHAR when the
426   ;; cross-compiler is running in the cross-compilation host vanilla
427   ;; ANSI Common Lisp, we can use CODE-CHAR expressions to delay until
428   ;; target Lisp run time the generation of CHARACTERs which aren't
429   ;; STANDARD-CHARACTERs. That way, we don't need to rely on the host
430   ;; Common Lisp being able to handle any characters other than those
431   ;; guaranteed by the ANSI spec.
432   (movable #-sb-xc-host foldable flushable))
433 \f
434 ;;;; from the "Sequences" chapter:
435
436 (defknown elt (sequence index) t (foldable unsafely-flushable))
437
438 (defknown subseq (sequence index &optional sequence-end) consed-sequence
439   (flushable)
440   :derive-type (sequence-result-nth-arg 1))
441
442 (defknown copy-seq (sequence) consed-sequence (flushable)
443   :derive-type (sequence-result-nth-arg 1))
444
445 (defknown length (sequence) index (foldable flushable))
446
447 (defknown reverse (sequence) consed-sequence (flushable)
448   :derive-type (sequence-result-nth-arg 1))
449
450 (defknown nreverse (sequence) sequence ()
451   :derive-type #'result-type-first-arg)
452
453 (defknown make-sequence (type-specifier index
454                                         &key
455                                         (:initial-element t))
456   consed-sequence
457   (movable unsafe)
458   :derive-type (result-type-specifier-nth-arg 1))
459
460 (defknown concatenate (type-specifier &rest sequence) consed-sequence
461   ()
462   :derive-type (result-type-specifier-nth-arg 1))
463
464 (defknown (map %map) (type-specifier callable sequence &rest sequence)
465   consed-sequence
466   (call)
467 ; :DERIVE-TYPE 'TYPE-SPEC-ARG1 ? Nope... (MAP NIL ...) returns NULL, not NIL.
468   )
469 (defknown %map-to-list-arity-1 (callable sequence) list (flushable call))
470 (defknown %map-to-simple-vector-arity-1 (callable sequence) simple-vector
471   (flushable call))
472 (defknown %map-to-nil-on-simple-vector (callable simple-vector) null
473   (flushable call))
474 (defknown %map-to-nil-on-vector (callable vector) null (flushable call))
475 (defknown %map-to-nil-on-sequence (callable sequence) null (flushable call))
476
477 (defknown map-into (sequence callable &rest sequence)
478   sequence
479   (call)
480   :derive-type #'result-type-first-arg)
481
482 ;;; returns the result from the predicate...
483 (defknown some (callable sequence &rest sequence) t
484   (foldable unsafely-flushable call))
485
486 (defknown (every notany notevery) (callable sequence &rest sequence) boolean
487   (foldable unsafely-flushable call))
488
489 ;;; unsafe for :INITIAL-VALUE...
490 (defknown reduce (callable
491                   sequence
492                   &key
493                   (:from-end t)
494                   (:start index)
495                   (:end sequence-end)
496                   (:initial-value t)
497                   (:key callable))
498   t
499   (foldable flushable call unsafe))
500
501 (defknown fill (sequence t &key (:start index) (:end sequence-end)) sequence
502   (unsafe)
503   :derive-type #'result-type-first-arg)
504
505 (defknown replace (sequence
506                    sequence
507                    &key
508                    (:start1 index)
509                    (:end1 sequence-end)
510                    (:start2 index)
511                    (:end2 sequence-end))
512   sequence ()
513   :derive-type #'result-type-first-arg)
514
515 (defknown remove
516   (t sequence &key (:from-end t) (:test callable)
517      (:test-not callable) (:start index) (:end sequence-end)
518      (:count sequence-count) (:key callable))
519   consed-sequence
520   (flushable call)
521   :derive-type (sequence-result-nth-arg 2))
522
523 (defknown substitute
524   (t t sequence &key (:from-end t) (:test callable)
525      (:test-not callable) (:start index) (:end sequence-end)
526      (:count sequence-count) (:key callable))
527   consed-sequence
528   (flushable call)
529   :derive-type (sequence-result-nth-arg 3))
530
531 (defknown (remove-if remove-if-not)
532   (callable sequence &key (:from-end t) (:start index) (:end sequence-end)
533             (:count sequence-count) (:key callable))
534   consed-sequence
535   (flushable call)
536   :derive-type (sequence-result-nth-arg 2))
537
538 (defknown (substitute-if substitute-if-not)
539   (t callable sequence &key (:from-end t) (:start index) (:end sequence-end)
540      (:count sequence-count) (:key callable))
541   consed-sequence
542   (flushable call)
543   :derive-type (sequence-result-nth-arg 3))
544
545 (defknown delete
546   (t sequence &key (:from-end t) (:test callable)
547      (:test-not callable) (:start index) (:end sequence-end)
548      (:count sequence-count) (:key callable))
549   sequence
550   (flushable call)
551   :derive-type (sequence-result-nth-arg 2))
552
553 (defknown nsubstitute
554   (t t sequence &key (:from-end t) (:test callable)
555      (:test-not callable) (:start index) (:end sequence-end)
556      (:count sequence-count) (:key callable))
557   sequence
558   (flushable call)
559   :derive-type (sequence-result-nth-arg 3))
560
561 (defknown (delete-if delete-if-not)
562   (callable sequence &key (:from-end t) (:start index) (:end sequence-end)
563             (:count sequence-count) (:key callable))
564   sequence
565   (flushable call)
566   :derive-type (sequence-result-nth-arg 2))
567
568 (defknown (nsubstitute-if nsubstitute-if-not)
569   (t callable sequence &key (:from-end t) (:start index) (:end sequence-end)
570      (:count sequence-count) (:key callable))
571   sequence
572   (flushable call)
573   :derive-type (sequence-result-nth-arg 3))
574
575 (defknown remove-duplicates
576   (sequence &key (:test callable) (:test-not callable) (:start index)
577             (:from-end t) (:end sequence-end) (:key callable))
578   consed-sequence
579   (unsafely-flushable call)
580   :derive-type (sequence-result-nth-arg 1))
581
582 (defknown delete-duplicates
583   (sequence &key (:test callable) (:test-not callable) (:start index)
584             (:from-end t) (:end sequence-end) (:key callable))
585   sequence
586   (unsafely-flushable call)
587   :derive-type (sequence-result-nth-arg 1))
588
589 (defknown find (t sequence &key (:test callable) (:test-not callable)
590                   (:start index) (:from-end t) (:end sequence-end)
591                   (:key callable))
592   t
593   (foldable flushable call))
594
595 (defknown (find-if find-if-not)
596   (callable sequence &key (:from-end t) (:start index) (:end sequence-end)
597             (:key callable))
598   t
599   (foldable flushable call))
600
601 (defknown position (t sequence &key (:test callable) (:test-not callable)
602                       (:start index) (:from-end t) (:end sequence-end)
603                       (:key callable))
604   (or index null)
605   (foldable flushable call))
606
607 (defknown (position-if position-if-not)
608   (callable sequence &key (:from-end t) (:start index) (:end sequence-end)
609             (:key callable))
610   (or index null)
611   (foldable flushable call))
612
613 (defknown count (t sequence &key (:test callable) (:test-not callable)
614                       (:start index) (:from-end t) (:end sequence-end)
615                       (:key callable))
616   index
617   (foldable flushable call))
618
619 (defknown (count-if count-if-not)
620   (callable sequence &key (:from-end t) (:start index) (:end sequence-end)
621             (:key callable))
622   index
623   (foldable flushable call))
624
625 (defknown (mismatch search)
626   (sequence sequence &key (:from-end t) (:test callable) (:test-not callable)
627             (:start1 index) (:end1 sequence-end)
628             (:start2 index) (:end2 sequence-end)
629             (:key callable))
630   (or index null)
631   (foldable flushable call))
632
633 ;;; not FLUSHABLE, since vector sort guaranteed in-place...
634 (defknown (stable-sort sort) (sequence callable &key (:key callable)) sequence
635   (call)
636   :derive-type (sequence-result-nth-arg 1))
637 (defknown sb!impl::sort-vector (vector index index function (or function null)) vector
638   (call))
639
640 (defknown merge (type-specifier sequence sequence callable
641                                 &key (:key callable))
642   sequence
643   (call)
644   :derive-type (result-type-specifier-nth-arg 1))
645
646 ;;; not FLUSHABLE, despite what CMU CL's DEFKNOWN said..
647 (defknown read-sequence (sequence stream
648                                   &key
649                                   (:start index)
650                                   (:end sequence-end))
651   (index)
652   ())
653
654 (defknown write-sequence (sequence stream
655                                    &key
656                                    (:start index)
657                                    (:end sequence-end))
658   sequence
659   ()
660   :derive-type (sequence-result-nth-arg 1))
661 \f
662 ;;;; from the "Manipulating List Structure" chapter:
663 (defknown (car cdr first rest)
664   (list)
665   t
666   (foldable flushable))
667
668 ;; Correct argument type restrictions for these functions are
669 ;; complicated, so we just declare them to accept LISTs and suppress
670 ;; flushing is safe code.
671 (defknown (caar cadr cdar cddr
672                 caaar caadr cadar caddr cdaar cdadr cddar cdddr
673                 caaaar caaadr caadar caaddr cadaar cadadr caddar cadddr
674                 cdaaar cdaadr cdadar cdaddr cddaar cddadr cdddar cddddr
675                 second third fourth fifth sixth seventh eighth ninth tenth)
676   (list)
677   t
678   (foldable unsafely-flushable))
679
680 (defknown cons (t t) cons (movable flushable unsafe))
681
682 (defknown tree-equal (t t &key (:test callable) (:test-not callable)) boolean
683   (foldable flushable call))
684 (defknown endp (list) boolean (foldable flushable movable))
685 (defknown list-length (list) (or index null) (foldable unsafely-flushable))
686 (defknown nth (index list) t (foldable flushable))
687 (defknown nthcdr (index list) t (foldable unsafely-flushable))
688 (defknown last (list &optional index) list (foldable flushable))
689 (defknown list (&rest t) list (movable flushable unsafe))
690 (defknown list* (t &rest t) t (movable flushable unsafe))
691 (defknown make-list (index &key (:initial-element t)) list
692   (movable flushable unsafe))
693
694 ;;; All but last must be of type LIST, but there seems to be no way to
695 ;;; express that in this syntax.
696 (defknown append (&rest t) t (flushable))
697
698 (defknown copy-list (list) list (flushable))
699 (defknown copy-alist (list) list (flushable))
700 (defknown copy-tree (t) t (flushable recursive))
701 (defknown revappend (list t) t (flushable))
702
703 ;;; All but last must be of type LIST, but there seems to be no way to
704 ;;; express that in this syntax. The result must be LIST, but we do
705 ;;; not check it now :-).
706 (defknown nconc (&rest t) t ())
707
708 (defknown nreconc (list t) list ())
709 (defknown butlast (list &optional index) list (flushable))
710 (defknown nbutlast (list &optional index) list ())
711 (defknown ldiff (list t) list (flushable))
712 (defknown (rplaca rplacd) (cons t) list (unsafe))
713
714 (defknown (nsubst subst) (t t t &key (:key callable) (:test callable)
715                             (:test-not callable))
716   list (flushable unsafe call))
717
718 (defknown (subst-if subst-if-not nsubst-if nsubst-if-not)
719           (t t t &key (:key callable))
720   list (flushable unsafe call))
721
722 (defknown (sublis nsublis) (list t &key (:key callable) (:test callable)
723                                  (:test-not callable))
724   list (flushable unsafe call))
725
726 (defknown member (t list &key (:key callable) (:test callable)
727                     (:test-not callable))
728   list (foldable flushable call))
729 (defknown (member-if member-if-not) (callable list &key (:key callable))
730   list (foldable flushable call))
731
732 (defknown tailp (t list) boolean (foldable flushable))
733
734 (defknown adjoin (t list &key (:key callable) (:test callable)
735                     (:test-not callable))
736   list (foldable flushable unsafe call))
737
738 (defknown (union intersection set-difference set-exclusive-or)
739   (list list &key (:key callable) (:test callable) (:test-not callable))
740   list
741   (foldable flushable call))
742
743 (defknown (nunion nintersection nset-difference nset-exclusive-or)
744   (list list &key (:key callable) (:test callable) (:test-not callable))
745   list
746   (foldable flushable call))
747
748 (defknown subsetp
749   (list list &key (:key callable) (:test callable) (:test-not callable))
750   boolean
751   (foldable flushable call))
752
753 (defknown acons (t t t) list (movable flushable unsafe))
754 (defknown pairlis (t t &optional t) list (flushable unsafe))
755
756 (defknown (rassoc assoc)
757           (t list &key (:key callable) (:test callable) (:test-not callable))
758   list (foldable flushable call))
759 (defknown (assoc-if-not assoc-if rassoc-if rassoc-if-not)
760           (callable list &key (:key callable)) list (foldable flushable call))
761
762 (defknown (memq assq) (t list) list (foldable flushable unsafe))
763 (defknown delq (t list) list (flushable unsafe))
764 \f
765 ;;;; from the "Hash Tables" chapter:
766
767 (defknown make-hash-table
768   (&key (:test callable) (:size unsigned-byte)
769         (:rehash-size (or (integer 1) (float (1.0))))
770         (:rehash-threshold (real 0 1))
771         (:weak-p t))
772   hash-table
773   (flushable unsafe))
774 (defknown hash-table-p (t) boolean (movable foldable flushable))
775 (defknown gethash (t hash-table &optional t) (values t boolean)
776   (flushable unsafe)) ; not FOLDABLE, since hash table contents can change
777 (defknown %puthash (t hash-table t) t (unsafe))
778 (defknown remhash (t hash-table) boolean ())
779 (defknown maphash (callable hash-table) null (flushable call))
780 (defknown clrhash (hash-table) hash-table ())
781 (defknown hash-table-count (hash-table) index (flushable))
782 (defknown hash-table-rehash-size (hash-table) (or (integer 1) (float (1.0)))
783   (foldable flushable))
784 (defknown hash-table-rehash-threshold (hash-table) (real 0 1)
785   (foldable flushable))
786 (defknown hash-table-size (hash-table) index (flushable))
787 (defknown hash-table-test (hash-table) symbol (foldable flushable))
788 (defknown sxhash (t) (integer 0 #.sb!xc:most-positive-fixnum)
789   (foldable flushable))
790 \f
791 ;;;; from the "Arrays" chapter
792
793 (defknown make-array ((or index list)
794                       &key
795                       (:element-type type-specifier)
796                       (:initial-element t)
797                       (:initial-contents t)
798                       (:adjustable t)
799                       (:fill-pointer t)
800                       (:displaced-to (or array null))
801                       (:displaced-index-offset index))
802   array (flushable unsafe))
803
804 (defknown vector (&rest t) simple-vector (flushable unsafe))
805
806 (defknown aref (array &rest index) t (foldable))
807 (defknown row-major-aref (array index) t (foldable))
808
809 (defknown array-element-type (array)
810   type-specifier
811   (foldable flushable recursive))
812 (defknown array-rank (array) array-rank (foldable flushable))
813 (defknown array-dimension (array array-rank) index (foldable flushable))
814 (defknown array-dimensions (array) list (foldable flushable))
815 (defknown array-in-bounds-p (array &rest integer) boolean (foldable flushable))
816 (defknown array-row-major-index (array &rest index) array-total-size
817   (foldable flushable))
818 (defknown array-total-size (array) array-total-size (foldable flushable))
819 (defknown adjustable-array-p (array) boolean (movable foldable flushable))
820
821 (defknown svref (simple-vector index) t (foldable flushable))
822 (defknown bit ((array bit) &rest index) bit (foldable flushable))
823 (defknown sbit ((simple-array bit) &rest index) bit (foldable flushable))
824
825 (defknown (bit-and bit-ior bit-xor bit-eqv bit-nand bit-nor bit-andc1 bit-andc2
826                    bit-orc1 bit-orc2)
827   ((array bit) (array bit) &optional (or (array bit) (member t nil)))
828   (array bit)
829   (foldable)
830   #|:derive-type #'result-type-last-arg|#)
831
832 (defknown bit-not ((array bit) &optional (or (array bit) (member t nil)))
833   (array bit)
834   (foldable)
835   #|:derive-type #'result-type-last-arg|#)
836
837 (defknown bit-vector-= (bit-vector bit-vector) boolean
838   (movable foldable flushable))
839
840 (defknown array-has-fill-pointer-p (array) boolean
841   (movable foldable flushable))
842 (defknown fill-pointer (vector) index (foldable unsafely-flushable))
843 (defknown vector-push (t vector) (or index null) ())
844 (defknown vector-push-extend (t vector &optional index) index ())
845 (defknown vector-pop (vector) t ())
846
847 (defknown adjust-array
848   (array (or index list) &key (:element-type type-specifier)
849          (:initial-element t) (:initial-contents t)
850          (:fill-pointer t) (:displaced-to (or array null))
851          (:displaced-index-offset index))
852   array (unsafe))
853 ;  :derive-type 'result-type-arg1) Not even close...
854 \f
855 ;;;; from the "Strings" chapter:
856
857 (defknown char (string index) character (foldable flushable))
858 (defknown schar (simple-string index) character (foldable flushable))
859
860 (sb!xc:deftype stringable () '(or character string symbol))
861
862 (defknown (string= string-equal)
863   (stringable stringable &key (:start1 index) (:end1 sequence-end)
864               (:start2 index) (:end2 sequence-end))
865   boolean
866   (foldable flushable))
867
868 (defknown (string< string> string<= string>= string/= string-lessp
869                    string-greaterp string-not-lessp string-not-greaterp
870                    string-not-equal)
871   (stringable stringable &key (:start1 index) (:end1 sequence-end)
872               (:start2 index) (:end2 sequence-end))
873   (or index null)
874   (foldable flushable))
875
876 (defknown make-string (index &key (:element-type type-specifier)
877                        (:initial-element character))
878   simple-string (flushable))
879
880 (defknown (string-trim string-left-trim string-right-trim)
881   (sequence stringable) simple-string (flushable))
882
883 (defknown (string-upcase string-downcase string-capitalize)
884   (stringable &key (:start index) (:end sequence-end))
885   simple-string (flushable))
886
887 (defknown (nstring-upcase nstring-downcase nstring-capitalize)
888   (string &key (:start index) (:end sequence-end))
889   string ())
890
891 (defknown string (stringable) string
892   (flushable explicit-check))
893 \f
894 ;;;; internal non-keyword versions of string predicates:
895
896 (defknown (string<* string>* string<=* string>=* string/=*)
897   (stringable stringable index sequence-end index sequence-end)
898   (or index null)
899   (foldable flushable))
900
901 (defknown string=*
902   (stringable stringable index sequence-end index sequence-end)
903   boolean
904   (foldable flushable))
905 \f
906 ;;;; from the "Eval" chapter:
907
908 (defknown eval (t) * (recursive))
909 (defknown constantp (t &optional lexenv-designator) boolean
910   (foldable flushable))
911 \f
912 ;;;; from the "Streams" chapter:
913
914 (defknown make-synonym-stream (symbol) stream (flushable))
915 (defknown make-broadcast-stream (&rest stream) stream (unsafely-flushable))
916 (defknown make-concatenated-stream (&rest stream) stream (unsafely-flushable))
917 (defknown make-two-way-stream (stream stream) stream (unsafely-flushable))
918 (defknown make-echo-stream (stream stream) stream (flushable))
919 (defknown make-string-input-stream (string &optional index index) stream
920   (flushable unsafe))
921 (defknown make-string-output-stream () stream (flushable))
922 (defknown get-output-stream-string (stream) simple-string ())
923 (defknown streamp (t) boolean (movable foldable flushable))
924 (defknown stream-element-type (stream) type-specifier
925   (movable foldable flushable))
926 (defknown (output-stream-p input-stream-p) (stream) boolean
927   (movable foldable flushable))
928 (defknown close (stream &key (:abort t)) (eql t) ())
929 \f
930 ;;;; from the "Input/Output" chapter:
931
932 ;;; (The I/O functions are given effects ANY under the theory that
933 ;;; code motion over I/O operations is particularly confusing and not
934 ;;; very important for efficiency.)
935
936 (defknown copy-readtable (&optional (or readtable null) (or readtable null))
937   readtable
938   ())
939 (defknown readtablep (t) boolean (movable foldable flushable))
940
941 (defknown set-syntax-from-char
942   (character character &optional (or readtable null) readtable) (eql t)
943   ())
944
945 (defknown set-macro-character (character callable &optional t readtable)
946   (eql t)
947   (unsafe))
948 (defknown get-macro-character (character &optional (or readtable null))
949   (values callable boolean) (flushable))
950
951 (defknown make-dispatch-macro-character (character &optional t readtable)
952   (eql t) ())
953 (defknown set-dispatch-macro-character
954   (character character callable &optional readtable) function
955   (unsafe))
956 (defknown get-dispatch-macro-character
957   (character character &optional (or readtable null)) (or callable null)
958   ())
959
960 ;;; may return any type due to eof-value...
961 (defknown (read read-preserving-whitespace read-char-no-hang read-char)
962   (&optional streamlike t t t) t (explicit-check))
963
964 (defknown read-delimited-list (character &optional streamlike t) list
965   (explicit-check))
966 (defknown read-line (&optional streamlike t t t) (values t boolean)
967   (explicit-check))
968 (defknown unread-char (character &optional streamlike) t
969   (explicit-check))
970 (defknown peek-char (&optional (or character (member nil t)) streamlike t t t)
971   t
972   (explicit-check))
973 (defknown listen (&optional streamlike) boolean (flushable explicit-check))
974
975 (defknown clear-input (&optional stream) null (explicit-check))
976
977 (defknown read-from-string
978   (string &optional t t
979           &key
980           (:start index)
981           (:end sequence-end)
982           (:preserve-whitespace t))
983   (values t index))
984 (defknown parse-integer
985   (string &key
986           (:start index)
987           (:end sequence-end)
988           (:radix (integer 2 36))
989           (:junk-allowed t))
990   (values (or integer null ()) index))
991
992 (defknown read-byte (stream &optional t t) t (explicit-check))
993
994 (defknown write
995   (t &key
996      (:stream streamlike)
997      (:escape t)
998      (:radix t)
999      (:base (integer 2 36))
1000      (:circle t)
1001      (:pretty t)
1002      (:level (or unsigned-byte null))
1003      (:readably t)
1004      (:length (or unsigned-byte null))
1005      (:case t)
1006      (:array t)
1007      (:gensym t)
1008      (:lines (or unsigned-byte null))
1009      (:right-margin (or unsigned-byte null))
1010      (:miser-width (or unsigned-byte null))
1011      (:pprint-dispatch t))
1012   t
1013   (any explicit-check)
1014   :derive-type #'result-type-first-arg)
1015
1016 (defknown (prin1 print princ) (t &optional streamlike) t (any explicit-check)
1017   :derive-type #'result-type-first-arg)
1018
1019 ;;; xxx-TO-STRING functions are not foldable because they depend on
1020 ;;; the dynamic environment.
1021 (defknown write-to-string
1022   (t &key (:escape t) (:radix t) (:base (integer 2 36)) (:readably t)
1023      (:circle t) (:pretty t) (:level (or unsigned-byte null))
1024      (:length (or unsigned-byte null)) (:case t) (:array t) (:gensym t)
1025      (:lines (or unsigned-byte null)) (:right-margin (or unsigned-byte null))
1026      (:miser-width (or unsigned-byte null)) (:pprint-dispatch t))
1027   simple-string
1028   (foldable flushable explicit-check))
1029
1030 (defknown (prin1-to-string princ-to-string) (t) simple-string (flushable))
1031
1032 (defknown write-char (character &optional streamlike) character
1033   (explicit-check))
1034 (defknown (write-string write-line)
1035   (string &optional streamlike &key (:start index) (:end sequence-end))
1036   string
1037   (explicit-check))
1038
1039 (defknown (terpri finish-output force-output clear-output)
1040   (&optional streamlike) null
1041   (explicit-check))
1042
1043 (defknown fresh-line (&optional streamlike) boolean
1044   (explicit-check))
1045
1046 (defknown write-byte (integer stream) integer
1047   (explicit-check))
1048
1049 (defknown format ((or streamlike string) (or string function) &rest t)
1050   (or string null)
1051   (explicit-check))
1052
1053 (defknown (y-or-n-p yes-or-no-p) (&optional string &rest t) boolean
1054   (explicit-check))
1055 \f
1056 ;;;; from the "File System Interface" chapter:
1057
1058 ;;; (No pathname functions are FOLDABLE because they all potentially
1059 ;;; depend on *DEFAULT-PATHNAME-DEFAULTS*, e.g. to provide a default
1060 ;;; host when parsing a namestring. They are not FLUSHABLE because
1061 ;;; parsing of a PATHNAME-DESIGNATOR might signal an error.)
1062
1063 (defknown wild-pathname-p (pathname-designator
1064                            &optional
1065                            (member nil :host :device
1066                                    :directory :name
1067                                    :type :version))
1068   boolean
1069   ())
1070 (defknown pathname-match-p (pathname-designator pathname-designator) boolean
1071   ())
1072 (defknown translate-pathname (pathname-designator
1073                               pathname-designator
1074                               pathname-designator &key)
1075   pathname
1076   ())
1077
1078 (defknown logical-pathname (pathname-designator) logical-pathname ())
1079 (defknown translate-logical-pathname (pathname-designator &key) pathname
1080   (recursive))
1081 (defknown load-logical-pathname-translations (string) t ())
1082 (defknown logical-pathname-translations (logical-host-designator) list ())
1083
1084 (defknown pathname (pathname-designator) pathname (unsafely-flushable))
1085 (defknown truename (pathname-designator) pathname ())
1086
1087 (defknown parse-namestring
1088   (pathname-designator &optional
1089                        (or list host string (member :unspecific))
1090                        pathname-designator
1091                        &key
1092                        (:start index)
1093                        (:end sequence-end)
1094                        (:junk-allowed t))
1095   (values (or pathname null) sequence-end)
1096   ())
1097
1098 (defknown merge-pathnames
1099   (pathname-designator &optional pathname-designator pathname-version)
1100   pathname
1101   (unsafely-flushable))
1102
1103 (defknown make-pathname
1104  (&key (:defaults pathname-designator)
1105        (:host (or string pathname-host))
1106        (:device (or string pathname-device))
1107        (:directory (or pathname-directory string (member :wild)))
1108        (:name (or pathname-name string (member :wild)))
1109        (:type (or pathname-type string (member :wild)))
1110        (:version pathname-version) (:case (member :local :common)))
1111   pathname (unsafely-flushable))
1112
1113 (defknown pathnamep (t) boolean (movable flushable))
1114
1115 (defknown pathname-host (pathname-designator
1116                          &key (:case (member :local :common)))
1117   pathname-host (flushable))
1118 (defknown pathname-device (pathname-designator
1119                            &key (:case (member :local :common)))
1120   pathname-device (flushable))
1121 (defknown pathname-directory (pathname-designator
1122                               &key (:case (member :local :common)))
1123   pathname-directory (flushable))
1124 (defknown pathname-name (pathname-designator
1125                          &key (:case (member :local :common)))
1126   pathname-name (flushable))
1127 (defknown pathname-type (pathname-designator
1128                          &key (:case (member :local :common)))
1129   pathname-type (flushable))
1130 (defknown pathname-version (pathname-designator)
1131   pathname-version (flushable))
1132
1133 (defknown (namestring file-namestring directory-namestring host-namestring)
1134   (pathname-designator) simple-string
1135   (unsafely-flushable))
1136
1137 (defknown enough-namestring (pathname-designator &optional pathname-designator)
1138   simple-string
1139   (unsafely-flushable))
1140
1141 (defknown user-homedir-pathname (&optional t) pathname (flushable))
1142
1143 (defknown open
1144   (pathname-designator &key
1145                        (:direction (member :input :output :io :probe))
1146                        (:element-type type-specifier)
1147                        (:if-exists (member :error :new-version :rename
1148                                            :rename-and-delete :overwrite
1149                                            :append :supersede nil))
1150                        (:if-does-not-exist (member :error :create nil))
1151                        (:external-format
1152                         ;; FIXME: This is logically (MEMBER :DEFAULT),
1153                         ;; but as a workaround for bug 244, we don't
1154                         ;; declare it (to keep the compiler from trusting
1155                         ;; the declaration unchecked).
1156                         t))
1157   (or stream null))
1158
1159 (defknown rename-file (pathname-designator filename)
1160   (values pathname pathname pathname))
1161 (defknown delete-file (pathname-designator) t)
1162 (defknown probe-file (pathname-designator) (or pathname null) ())
1163 (defknown file-write-date (pathname-designator) (or unsigned-byte null)
1164   ())
1165 (defknown file-author (pathname-designator) (or simple-string null)
1166   ())
1167
1168 (defknown file-position (stream &optional
1169                                 (or unsigned-byte (member :start :end)))
1170   (or unsigned-byte (member t nil)))
1171 (defknown file-length (stream) (or unsigned-byte null) (unsafely-flushable))
1172
1173 (defknown load
1174   ((or filename stream)
1175    &key
1176    (:verbose t)
1177    (:print t)
1178    (:if-does-not-exist (member :error :create nil))
1179    (:external-format
1180     ;; FIXME: This is logically (MEMBER :DEFAULT), but as a workaround
1181     ;; for bug 244, we don't declare it (to keep the compiler from
1182     ;; trusting the declaration unchecked).
1183     t))
1184   t)
1185
1186 (defknown directory (pathname-designator &key)
1187   list ())
1188 \f
1189 ;;;; from the "Errors" chapter:
1190
1191 (defknown error (t &rest t) nil) ; never returns
1192 (defknown cerror (string t &rest t) null)
1193 (defknown warn (t &rest t) null)
1194 (defknown break (&optional t &rest t) null)
1195
1196 ;;; and analogous SBCL extension:
1197 (defknown bug (t &rest t) nil) ; never returns
1198 \f
1199 ;;;; from the "Miscellaneous" Chapter:
1200
1201 (defknown compile ((or symbol cons) &optional (or list function null))
1202   (values (or function symbol cons) boolean boolean))
1203
1204 (defknown compile-file
1205   (filename
1206    &key
1207
1208    ;; ANSI options
1209    (:output-file (or filename
1210                      null
1211                      ;; FIXME: This last case is a non-ANSI hack.
1212                      (member t)))
1213    (:verbose t)
1214    (:print t)
1215    (:external-format t)
1216
1217    ;; extensions
1218    (:trace-file t)
1219    (:block-compile t))
1220   (values (or pathname null) boolean boolean))
1221
1222 ;; FIXME: consider making (OR CALLABLE CONS) something like
1223 ;; EXTENDED-FUNCTION-DESIGNATOR
1224 (defknown disassemble ((or callable cons) &key
1225                        (:stream stream) (:use-labels t))
1226   null)
1227
1228 (defknown fdocumentation (t symbol)
1229   (or string null)
1230   (flushable))
1231
1232 (defknown describe (t &optional (or stream (member t nil))) (values))
1233 (defknown inspect (t) (values))
1234 (defknown room (&optional (member t nil :default)) (values))
1235 (defknown ed (&optional (or symbol cons filename) &key (:init t) (:display t))
1236   t)
1237 (defknown dribble (&optional filename &key (:if-exists t)) (values))
1238
1239 (defknown apropos      (stringable &optional package-designator t) (values))
1240 (defknown apropos-list (stringable &optional package-designator t) list
1241   (flushable))
1242
1243 (defknown get-decoded-time ()
1244   (values (integer 0 59) (integer 0 59) (integer 0 23) (integer 1 31)
1245           (integer 1 12) unsigned-byte (integer 0 6) boolean (rational -24 24))
1246   (flushable))
1247
1248 (defknown get-universal-time () unsigned-byte (flushable))
1249
1250 (defknown decode-universal-time
1251           (unsigned-byte &optional (or null (rational -24 24)))
1252   (values (integer 0 59) (integer 0 59) (integer 0 23) (integer 1 31)
1253           (integer 1 12) unsigned-byte (integer 0 6) boolean (rational -24 24))
1254   (flushable))
1255
1256 (defknown encode-universal-time
1257   ((integer 0 59) (integer 0 59) (integer 0 23) (integer 1 31)
1258    (integer 1 12) unsigned-byte &optional (or null (rational -24 24)))
1259   unsigned-byte
1260   (flushable))
1261
1262 (defknown (get-internal-run-time get-internal-real-time)
1263   () internal-time (flushable))
1264
1265 (defknown sleep ((or (rational 0) (float 0.0))) null)
1266
1267 ;;; Even though ANSI defines LISP-IMPLEMENTATION-TYPE and
1268 ;;; LISP-IMPLEMENTATION-VERSION to possibly punt and return NIL, we
1269 ;;; know that there's no valid reason for our implementations to ever
1270 ;;; do so, so we can safely guarantee that they'll return strings.
1271 (defknown (lisp-implementation-type lisp-implementation-version)
1272   () simple-string (flushable))
1273
1274 ;;; For any of these functions, meaningful information might not be
1275 ;;; available, so -- unlike the related LISP-IMPLEMENTATION-FOO
1276 ;;; functions -- these really can return NIL.
1277 (defknown (machine-type machine-version machine-instance
1278            software-type software-version
1279            short-site-name long-site-name)
1280   () (or simple-string null) (flushable))
1281
1282 (defknown identity (t) t (movable foldable flushable unsafe)
1283   :derive-type #'result-type-first-arg)
1284
1285 (defknown constantly (t) function (movable flushable))
1286 (defknown complement (function) function (movable flushable))
1287 \f
1288 ;;;; miscellaneous extensions
1289
1290 (defknown get-bytes-consed () unsigned-byte (flushable))
1291
1292 ;;; PCOUNTERs
1293 (defknown incf-pcounter (pcounter unsigned-byte) pcounter)
1294 (defknown pcounter->integer (pcounter) unsigned-byte)
1295 (defknown %incf-pcounter-or-fixnum ((or pcounter fixnum) unsigned-byte)
1296   (or pcounter fixnum))
1297 (defknown pcounter-or-fixnum->integer ((or pcounter fixnum)) unsigned-byte)
1298 \f
1299 ;;;; magical compiler frobs
1300
1301 ;;; We can't fold this in general because of SATISFIES. There is a
1302 ;;; special optimizer anyway.
1303 (defknown %typep (t (or type-specifier ctype)) boolean
1304   (movable flushable explicit-check))
1305 (defknown %instance-typep (t (or type-specifier ctype)) boolean
1306   (movable flushable explicit-check))
1307
1308 (defknown %cleanup-point () t)
1309 (defknown %special-bind (t t) t)
1310 (defknown %special-unbind (t) t)
1311 (defknown %listify-rest-args (t index) list (flushable))
1312 (defknown %more-arg-context (t t) (values t index) (flushable))
1313 (defknown %more-arg (t index) t)
1314 (defknown %more-arg-values (t index index) * (flushable))
1315 (defknown %verify-arg-count (index index) (values))
1316 (defknown %arg-count-error (t) nil)
1317 (defknown %unknown-values () *)
1318 (defknown %catch (t t) t)
1319 (defknown %unwind-protect (t t) t)
1320 (defknown (%catch-breakup %unwind-protect-breakup) () t)
1321 (defknown %lexical-exit-breakup (t) t)
1322 (defknown %continue-unwind (t t t) nil)
1323 (defknown %throw (t &rest t) nil) ; This is MV-called.
1324 (defknown %nlx-entry (t) *)
1325 (defknown %%primitive (t t &rest t) *)
1326 (defknown %pop-values (t) t)
1327 (defknown %type-check-error (t t) nil)
1328 (defknown %odd-key-args-error () nil)
1329 (defknown %unknown-key-arg-error (t) nil)
1330 (defknown (%ldb %mask-field) (bit-index bit-index integer) unsigned-byte
1331   (movable foldable flushable explicit-check))
1332 (defknown (%dpb %deposit-field) (integer bit-index bit-index integer) integer
1333   (movable foldable flushable explicit-check))
1334 (defknown %negate (number) number (movable foldable flushable explicit-check))
1335 (defknown %check-bound (array index fixnum) index (movable foldable flushable))
1336 (defknown data-vector-ref (simple-array index) t
1337   (foldable explicit-check))
1338 (defknown data-vector-set (array index t) t (unsafe explicit-check))
1339 (defknown hairy-data-vector-ref (array index) t
1340   (foldable explicit-check))
1341 (defknown hairy-data-vector-set (array index t) t (unsafe explicit-check))
1342 (defknown %caller-frame-and-pc () (values t t) (flushable))
1343 (defknown %with-array-data (array index (or index null))
1344   (values (simple-array * (*)) index index index)
1345   (foldable flushable))
1346 (defknown %set-symbol-package (symbol t) t (unsafe))
1347 (defknown %coerce-name-to-fun ((or symbol cons)) function (flushable))
1348 (defknown %coerce-callable-to-fun (callable) function (flushable))
1349 (defknown failed-%with-array-data (t t t) nil)
1350 (defknown %find-position
1351   (t sequence t index sequence-end function function)
1352   (values t (or index null))
1353   (flushable call))
1354 (defknown (%find-position-if %find-position-if-not)
1355   (function sequence t index sequence-end function)
1356   (values t (or index null))
1357   (call))
1358 (defknown effective-find-position-test (callable callable)
1359   function
1360   (flushable foldable))
1361 (defknown effective-find-position-key (callable)
1362   function
1363   (flushable foldable))
1364
1365 (defknown %check-vector-sequence-bounds (vector index sequence-end)
1366   index
1367   (unwind))
1368 ;;; FIXME: including this information here is probably necessary to
1369 ;;; get efficient compilation of the inline expansion of
1370 ;;; %FIND-POSITION-IF, so it should maybe be in a more
1371 ;;; compiler-friendly package (SB-INT?)
1372 (defknown sb!impl::signal-bounding-indices-bad-error
1373     (sequence index sequence-end)
1374   nil) ; never returns
1375   
1376
1377 (defknown arg-count-error (t t t t t t) nil (unsafe))
1378 \f
1379 ;;;; SETF inverses
1380
1381 (defknown %aset (array &rest t) t (unsafe))
1382 (defknown %set-row-major-aref (array index t) t (unsafe))
1383 (defknown %rplaca (cons t) t (unsafe))
1384 (defknown %rplacd (cons t) t (unsafe))
1385 (defknown %put (symbol t t) t (unsafe))
1386 (defknown %setelt (sequence index t) t (unsafe))
1387 (defknown %svset (simple-vector index t) t (unsafe))
1388 (defknown %bitset ((array bit) &rest index) bit (unsafe))
1389 (defknown %sbitset ((simple-array bit) &rest index) bit (unsafe))
1390 (defknown %charset (string index character) character (unsafe))
1391 (defknown %scharset (simple-string index character) character (unsafe))
1392 (defknown %set-symbol-value (symbol t) t (unsafe))
1393 (defknown (setf symbol-function) (function symbol) function (unsafe))
1394 (defknown %set-symbol-plist (symbol t) t (unsafe))
1395 (defknown (setf fdocumentation) ((or string null) t symbol)
1396   (or string null)
1397   ())
1398 (defknown %setnth (index list t) t (unsafe))
1399 (defknown %set-fill-pointer (vector index) index (unsafe))
1400 \f
1401 ;;;; miscellaneous internal utilities
1402
1403 (defknown %fun-name (function) t (flushable))
1404 (defknown (setf %fun-name) (t function) t (unsafe))
1405
1406 (defknown policy-quality (policy symbol) policy-quality
1407           (flushable))