JMBR (superadditive.com) added several informed notes on type useage in common lisp after Aleteoryx and I just muddled around in the Mastodon thread for that article.
I hope everyone will forgive me for reproducing your notes directly.
check-type
is the portable one.This is the last patch on this patchwork article!
optimize
declarationsA LITTLE ADVANCED CONTEXT
To paraphrase the common lisp hyperspec, (compiler) optimization declarations include the optimize quality
s {compilation-speed
, debug
, safety
, space
, speed
} each having one the values from <0, 1, 2, 3>
. 0
meaning none and 3 meaning as much as possible. A conforming common lisp may have other qualities and values but it must support these ones. However, what they do is left to the compiler.
In practice, the meanings of different conjunctions of optimize
values come from CMUCL e.g. from here. Often used are (speed 3)
(sacrifice safety for speed), space
which whatever its original intent, controls inline expansion and safety
controlling what lisp does with error checking and type checking.
Note that sbcl is the successor to cmucl’s “python” lisp compiler.
satisfy
ing defun
sJMBR uses the behaviour of type declarations under the optimize condition, (safety 1). These meanings of lisp’s optimize declarations
(defun zero-to-20-only (x)
(declare (type (integer 0 20) x)
(optimize (safety 1)))
:yes)
JMBR’s code uses declarations as intended, resulting in the error here that x
is-not-of-type (mod 21)
at runtime. However, changing the safety
to 0
before recompiling the defun
disables the type check, and it will always appear to be true.
JMBR reserves THE for sbcl optimizations only.
Actually, I had not been using these.
(defun zero-to-20-only (x)
(declare (type (integer 0 20) x)
(values keyword)
(optimize (safety 1)))
:yes)
values
for multiple returnsEdit contributed by JMBR again: CMUCL’s values declaration documentation
(values 1 2 3)
yields multiple returns and is fairly ubiquitous. Occasionally a values
expression is used to ascribe types to multiple returns, as is the case above.
JMBR suggests those interested in lisp-but-ML-like-typing to look at Coalton, though I am quite a common lisp loyalist!
As JMBR mentioned (I think I got it in here), the declaration behaviours are from CMUCL historically / SBCL specifically.
The compiler may conformingly ignore type declarations. SBCL by accident has a policy to add check-type on type declaration, but in light of the spec it means “I hereby promise to always pass an argument of that type to this function, at a risk of nasal daemons if I do not abide”
Well, there is infinitely more to say but I just wanted to get JMBR’s note out there as well.
I intend to get into the details of both lisp’s types and condition system with a mixture of personal discovery and learning and sharing basically the cultural context lisp carries with it.
See you back on the Mastodon.
Sorry again for the me-relaying-peoples’-points post, but these lisp threads on the Mastodon are basically noteworthy for the history of computing and cutting edge programming language useage!
screwlisp proposes kittens