--- layout: ../Site.layout.js --- # JMBR's short addendum on Common Lisp types (+ a note on cmucl) [JMBR](https://mathstodon.xyz/@rwxrwxrwx) ([superadditive.com](https://superadditive.com)) added several informed notes on type useage in common lisp after [Aleteoryx and I just muddled around](../common-lisp-strong-typing-example) in [the Mastodon thread for that article](https://gamerplus.org/@screwlisp/114577201539912869). I hope everyone will forgive me for reproducing *your* notes directly. ## [JMBR Notes](https://gamerplus.org/@rwxrwxrwx@mathstodon.xyz/114582527029571029) that [Vassil Nikolov's use of `check-type`](https://ieji.de/@vnikolov/114578211301813713) is the portable one. This is the last patch on this patchwork article! ## common lisp `optimize` declarations A LITTLE ADVANCED CONTEXT To paraphrase [the common lisp hyperspec](https://www.lispworks.com/documentation/HyperSpec/Body/d_optimi.htm), (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](https://cmucl.org/doc/index.html) e.g. [from here](https://cmucl.org/docs/cmu-user/html/The-Optimize-Declaration.html). Often used are `(speed 3)` (sacrifice safety for speed), `space` which whatever its original intent, controls [inline expansion](https://cmucl.org/docs/cmu-user/html/Inline-Expansion.html#inline_002dexpansion) and `safety` controlling what lisp does with error checking and type checking. Note that [sbcl](https://sbcl.org) is the successor to [cmucl](https://cmucl.org)'s "python" lisp compiler. ## `satisfy`ing `defun`s JMBR 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](https://www.lispworks.com/documentation/HyperSpec/Body/s_the.htm) for sbcl optimizations only. # Common lisp Return value type declaration in sbcl and cmucl 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 returns Edit contributed by JMBR again: [CMUCL's values declaration documentation](https://cmucl.org/docs/cmu-user/cmu-user.html#The-Values-Declaration) `(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! ### Note on declarations by [Jack Daniel](https://turtleware.eu) (embeddable common lisp / McCLIM) 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" # Fin. 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](https://gamerplus.org/@screwlisp). 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!