Barry Margolin
11/9/2015 5:11:00 PM
In article <m1bnb3tbaf.fsf@scofield.lrde.epita.fr>,
Didier Verna <didier@lrde.epita.fr> wrote:
> Hey there,
>
> until a couple of minutes ago, I assumed that ftype declarations were
> just a more concise way of declaring argument and return value
> types. That is, instead of writing
>
> (defun plus (a b)
> (declare (type fixnum a b))
> (the fixnum (+ a b)))
>
> you could just
> (declaim (ftype (function (fixnum fixnum) fixnum) plus))
> (defun plus (a b) (+ a b))
>
>
> But apparently, this is wrong. According to CLtL2 section 4.5, ftype
> relates to function /calls/ and not function /definitions/. Indeed,
> let's do this (in SBCL):
>
> (declaim (optimize (safety 3) (debug 3) (speed 0)))
> (declaim (ftype (function (fixnum fixnum) fixnum) plus))
> (defun plus (a b) (+ a b))
>
> Disassembly of PLUS will still show the use of GENERIC-+, but with
> additional checks on the type of the arguments and the return value. My
> first reaction was that this is weird: if ftype is to specify how the
> function is used, as opposed to as it is implemented, it shouldn't
> affect the way the function is implemented! In other words, the checks
> should wrap the caller instead of being incorporated into the callee.
What difference does it make? Either way, you get the same safety checks.
>
> On top of that, at least in SBCL, changing the declamation later on
> doesn't affect the function, unless you re-issue the defun. Which sort
> of explains why the checks are embedded in the function: it's simpler to
> do it this way rather than wrapping every caller.
>
> So, in a safety context as above, I can't find a usefulness to ftype,
> apart from the fact that it's slightly more concise than a PLUS with
> type declarations for parameters and THE return value. It generates the
> same machine code. I think it would have been useful for, say, defining
> a generic PLUS somewhere, and then declaring that you're using it as
> fixnum -> fixnum -> fixnum in file1, and then float -> float -> float in
> file2. But that doesn't work.
>
>
> Now, the same experiment with
>
> (declaim (optimize (safety 0) (debug 0) (speed 3)))
>
> shows that PLUS would only be fully optimized if you provided parameter
> type declarations and THE return value within the function. Not with an
> ftype declaration. So ftype seems to be even less useful here.
>
> Thoughts ?
Sounds like SBCL is trying to be more fail-safe. High safety settings
apply to all callers, even those defined before the optimize
declamation, but speed settings don't.
--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***