Michael Naunton <[EMAIL PROTECTED]> writes:
> Hi,
>
> I'm trying to convert a Greenspun's 10th language into Common Lisp. In
> cases such as the following, Python produces both a note (for the
> unreachable code,) and a warning (for the bad arg to truncate during
> constant folding.)
>
> (defun foo3 ()
> (let ((s "FOO"))
> (typecase s
> (double-float
> (format t "a double: ~A~%" (truncate s)))
> (t (format t "other: ~A~%" s)))))
>
> My questions:
>
> 1) Should the compiler really be emitting warnings/errors in code it can
> prove is unreachable? I think not, but am willing to revise my opinion.
I think it must only emit "unreachable code deleted"; e.g. SBCL does
so on your code.
> 2) Is there a way to locally suppress the warning?
For your private use this trick seems to work:
(handler-bind ((simple-warning (lambda (c)
(when (equal (first
(simple-condition-format-arguments c))
"constant folding")
(muffle-warning c)))))
(compile 'foo3 '(lambda ()
(let ((s "FOO"))
(declare (ignorable s))
(typecase s
(double-float
(format t "a double: ~A~%" (truncate s)))
(t (format t "other: ~A~%" s)))))))
Maybe it is worth to make a special condition class for this warning.
> 3) Is there a construct or hook/how hard would it be to hack the type
> inferencer to support something like:
>
> (defmacro op++ (x)
> "Give me inline code for doubles if x is definitely a double,
> else punt."
> (if (inferable-type x 'double-float)
> `(incf x)
> `(adhoc-incf x)))
1. You can ask for a type declaration on a variable. The interface
is described in "Syntactic Environment Access" X3J13 issue in
CLHS. Gerd Moellmann's implementation of it for CMUCL:
http://article.gmane.org/gmane.lisp.steel-bank.devel/1531
2. If you mean really "inferable", then for ordinary macro it is
impossible: type inference can be performed only after the whole
program is known, i.e. after all macros have been expanded.
3. In principle, it can be done for compiler macros, but CMUCL does
not expand them after type inference. If you know some good
interface for passing inferred information to compiler macros -- it
may be discussed.
4. CMUCL has DEFTRANSFORM macro, allowing to specialize functions
after type inference. See e.g. src/compiler/srctran.lisp for
examples.
5. CMUCL type inference engine does not like assignments :-( (and does
not like loops even more).
> 4) Section 5.3.5 of the CMUCL doc confuses me. I don't see how that
> ecase statement can work.
It probably meant ETYPECASE.
--
Regards,
Alexey Dejneka
"Alas, the spheres of truth are less transparent than those of
illusion." -- L.E.J. Brouwer