From: High Energy Physics Group
Subject: Re: More on type checking in Common Lisp
Date: 
Message-ID: <12971@dime.cs.umass.edu>
Dear Lispers:

I was very interested to read the recent comments on declaring types
in Common Lisp.  I expected everybody to think that typing is a good
idea, but it seems that many people do not like it.  This surprised
me.

I recall that one reader pointed out that LispM's can ignore the type
declarations, because they have hardware that checks the types at run
time with no overhead.  However, this reader forgets that many people
must use Franz's or Lucid's Lisp on a Unix box -- take me for example.
So, while a LispM might not care about the typing, my DECstation 3100
does.  This same reader should also understand that if he wants to run
his Lisp fast, a LispM is *not* the way to go.  I suspect that any of
the new RISC uprocessors will eat up LispM's.  (But this isn't
comp.arch, so I will not write about the attack of the killer micros.)

Another reader pointed out that typing somehow breaks the pleasing
generic nature of Lisp.  However, in this case, one can also make very
generic declarations.  For example, if a function is expected to take
any kind of a number, and return anything, one should say:

(proclaim '(function foo (number) t))

This way, when I look at your generic piece of code, I will understand
that it is indeed very generic.  If you don't declare it, I will have
no idea about it's nature, and how wide a range of arguments it is
expected to accept.

It is true that declaring something as a number is in effect a waste
of (keyboard) typing, because I can't imagine how that might help the
compiler; but on the other hand, I imagine that other human readers
might be thankful.  I write a lot of Lisp, and when I forget to
declare the types of some function, I almost always have no idea about
what it does a few months later.

While I am by no means an expert in the CL specification, it seems to
me that the declarations are a "promise" to the compiler, and that any
specific implementation is not required to catch cases where the user
is in error.  For example, if I understand properly, the compiler
doesn't have to check (or emit code that checks) that I fed a given
function the right arguments.  This of course is a very serious
problem, and it often makes pieces of my code crash and burn when I
run after compiling at high optimization, since I am often in error.
Sometimes I just give up and compile at lower optimization, where the
compiler ignores my declarations!

However, what I would love to see in the serious commercial Lisp
compilers is a flag called

  compiler::*check-type-declarations*

If I would set this to ``t'' (or something), the compiler would check
at compile time (or add run-time code) that insures that

 (a)  the first reference to a variable returns of the proper type
 (b)  any destructive modification to a variable leaves it
      of the proper type

(To my knowledge nobody supports a feature like this.  If they do,
please send me that vendor's name and I'll be sure to get a copy of
their Lisp!)

This way, I would run one of my programs and I would see:

 ;;; A call to setf is in error inside the function ``goofup''
 ;;; You declared the variable ``x'' as a ``single-float''
 ;;; However, you tried to set it to ``32'', which is a ``fixnum''

If my program could pass this test for a while, I imagine that I could
then compile with high optimization and shred.

I try to do physics in Lisp, and I often get unhappy about the amazing
amount of floating point garbage that is made.  With proper
declarations of the type, I feel that there is little excuse in making
most of the garbage, and physics and engineering stuff should go much
faster.  :^)

It is depressing to note that whenever I rewrite a program in C, I get
an absolutely massive increase in performance.  I guess that I tend to
write (what I think is) tasteful Lisp, and what is without doubt
*nasty* C, but I hope that the difference in performance will close
and one day there will be no reason at all to use C.  ;^)

I think that using declarations in Lisp and providing an option for
automatic run-time type checking would be an important step in this
direction.  (After all, why should the major Lisp writers spend all of
their time on supporting optimization if few people are interested?)


              --kleanthes koniaris
                ···@thundercats.pse.umass.edu
                ···@smectos.gang.umass.edu
                413-549-3901

Note: Some readers will point out that a function or macro like
check-type will help document one's program, and this is true, but
this code has to be put in by hand, and I presume that it is not a
hint to the compiler.  I think that

 (setf compiler::*check-type-declarations* t)

should be exactly the same as adding check-type everywhere reasonable.

If you agree with me, please write to your Lisp vendor and ask for a type
checking option!

;;;

(require 'standard-disclaimer)