3 ms·
Type declarations in Common Lisp support (safety <n>) and (speed <n>) syntax for configuring, you guessed it, safety and speed. The undefined behavior kicks in
by kazinator 9y ago
Type declarations in Common Lisp support (safety <n>) and (speed <n>) syntax for configuring, you guessed it, safety and speed.
The undefined behavior kicks in when you explicitly defeat safety. (safety 0) (speed 3) means "throw out the life jackets and assume that these declarations are true, making the code as fast as possible".
If you don't have the confidence for this over some piece of code, then just ... don't do that.
Safety is on by default. If some implementation changes that, that is bad.
The C language lacks any means to specify these preferences over a specific scope of code. Some compilers have certain #pragma directives, and beyond that, there are only module-level compiler flags. Those don't specifically give you safety. Usually there is just an optimization level. If optimization is omitted or perhaps specified as -O0 or whatever, that makes code sort of behave more safely in some handwaving manner in that certain "confusing, optimizy things" don't happen.
Also note that Lisp has a defined-order of evaluation. A function call like (f (inc i) (inc i)) is not comparable to the C f(++i, ++i) because it performs the left increment, obtains its value, then the right increment, and obtains its value.
Also note that C requires declarations everywhere. Lisp declarations are optional. Before Lisp declarations can be used in a wrong way that causes a malfunction, they have to be used, period.
We can use Lisp declarations to cause an integer overflow, like in C. If we don't use them, the integer type automatically prevents them by switching to bignums.
In C, even the function which is called once, main, is stuffed with declarations and compiled for speed, often with optimization on in actual real-world projects.
The SBCL people would have a long way to go before they screw up anything to the level of C.
- YouAreGreat 9y ago> Type declarations in Common Lisp support (safety <n>) and (speed <n>) syntax To be pedantic, those aren't type declarations. More importantly, the (un)safety of a type declaration isn't predicated on any specific safety/speed settings. The mere use of a type declaration implies that the programmer certifies their accuracy inside its scope; an additional optimize declaration is not required. It bears repeating: While Common Lisp has "type declarations", they don't behave as they do in statically typed languages. The are certificates, assurances by the programmer to be exploited at will by the implementation.
- lispm 9y ago> To be pedantic, those aren't type declarations. They are values of optimization qualities and guide the compiler strategy. > More importantly, the (un)safety of type declaration isn't predicated on any specific safety/speed settings. The mere use of a type declaration implies that the programmer certifies their accuracy inside its scope; With an optimize quality 3 for SAFETY a compiler will not reduce safety, even though there are type declarations. The Common Lisp standard REQUIRES that safety at 3 causes 'safe code': http://www.lispworks.com/documentation/HyperSpec/Body/03_eaa.htm http://www.lispworks.com/documentation/HyperSpec/Body/03_eaa... Which usually means full runtime checks in interpreted and compiled code, calls, system calls, ... > an additional optimize declaration is not required. Sure you need to have the right compiler qualities set to see those effects of your type declarations. LispWorks: CL-USER 4 > (proclaim '(optimize (safety 3))) NIL CL-USER 5 > (defun foo (x) (declare (string x)) (+ x 2)) FOO CL-USER 6 > (compile 'foo) FOO NIL NIL CL-USER 7 > (foo 'bar) Error: In + of (BAR 2) arguments should be of type NUMBER. 1 (continue) Return a value to use. 2 Supply a new first argument. 3 (abort) Return to top loop level 0. Type :b for backtrace or :c <option number> to proceed. Type :bug-form "<subject>" for a bug report template or :? for other options. CL-USER 8 : 1 > Oh, the LispWorks compiler has ignored the type declaration at safety 3. The runtime checks are still there. The code has the same safety as without a type declaration.
- YouAreGreat 9y agoViolation of a type declaration is UB in safe code. SBCL obviously makes use of that fact when inserting assertions.
- kazinator 9y agoUndefined behavior gives the implementation the freedom to insert any behavior whatsoever (like the proverbial demons flying out of the nose). An assertion isn't any behavior, it is a form of run-time error checking. Run-time error checking is what the "safety" optimization option means! From CLHS: Name Meaning compilation-speed speed of the compilation process debug ease of debugging safety run-time error checking space both code size and run-time space speed speed of the object code (optimize (safety 3)) means "greatest possible run-time error checking". Why wouldn't the implementation insert error checking based on the user's type declaration, when asked to provide the greatest possible run-time error checking?