Re: THE to function as DECLARE?
- From: Lars Rune Nøstdal <larsnostdal@xxxxxxxxx>
- Date: Fri, 26 Jun 2009 06:11:39 -0700 (PDT)
On Jun 26, 6:04 am, Vassil Nikolov <vniko...@xxxxxxxxx> wrote:
In
(let ((x <expression>))
(declare (type <type> x))
...)
we have, of course, declared the type of the variable X; the
consequences of the value of <expression> not being of that type are
undefined. It may or may not be possible to determine that at compile
time.
On the other hand, in
(let ((x (the <type> <expression>)))
...)
we have not declared anything about the type of the variable X, but
only what the type of its initial value is. The language processor is
not obliged to infer anything about the type of X from that; and if it
does, the result will depend on what else is happening to X (as well
as on the implementation, of course).
Yeah, I'm trying to get to the type information the compiler is
finding. If I've understood how this works (or is supposed to work?),
it will "never" infer too optimistic or specific types -- at least not
"on its own". So if it can't be sure at all, it'll fall back to T. At
least I think that's what will and is supposed happen. :)
For example, in
(let ((x (the fixnum (g y))))
(h x)
(setq x (the double-float (u z)))
(v x)
(setq x (w s))
(mu x))
(OR FIXNUM DOUBLE-FLOAT) would have to be a subtype of the inferred
type of X, which will also depend on what is known about W's return
type and the type of MU's argument.
By the way, the THE form is redundant in (THE FIXNUM 2). Something
like (THE FIXNUM (+ I J)) would be more interesting.
Yeah, or perhaps:
AMX> (declaim (inline square))
; No value
AMX> (defun square (x)
(declare (fixnum x))
(the fixnum (* x x)))
SQUARE
AMX> (fun-info 'square)
(FUNCTION (FIXNUM) (VALUES (UNSIGNED-BYTE 29) &OPTIONAL))
AMX> (let* ((x 2)
(square-of-x (square x)))
(dbg-prin1 (lex-var-info 'x))
(dbg-prin1 (lex-var-info 'square-of-x))
(values x square-of-x))
(LEX-VAR-INFO 'X) => (INTEGER 2 2)
(LEX-VAR-INFO 'SQUARE-OF-X) => (INTEGER 4 4)
2
4
AMX>
...anyway, this is nice; it's great being able to dispatch at compile-
time even when I don't feel like adding (declare ..)'s "manually".
LEX-VAR-INFO is similar to the LEX-TYPE-INFO I posted above:
(defmacro lex-info (name kind &optional env)
`(when-let* ((sb-c:*lexenv* ,(or env sb-c:*lexenv*))
(lex-info (sb-c:lexenv-find ,name ,kind)))
(sb-kernel:type-specifier (sb-c::leaf-type lex-info))))
(defmacro lex-var-info (name &optional (env nil env-supplied-p))
(if env-supplied-p
`(lex-info ,name vars ,env)
`(lex-info ,name vars)))
(defun fun-info (name)
(sb-kernel:type-specifier (sb-c::info :function :type name)))
(defmacro dbg-prin1 (form &optional (stream t))
`(format ,stream "~S => ~S~%" ',form ,form))
.
- Follow-Ups:
- Re: THE to function as DECLARE?
- From: Vassil Nikolov
- Re: THE to function as DECLARE?
- References:
- THE to function as DECLARE?
- From: Lars Rune Nøstdal
- Re: THE to function as DECLARE?
- From: Vassil Nikolov
- THE to function as DECLARE?
- Prev by Date: Re: What makes different things lispy or unlispy?
- Next by Date: Re: read-from-string t nil :start
- Previous by thread: Re: THE to function as DECLARE?
- Next by thread: Re: THE to function as DECLARE?
- Index(es):
Relevant Pages
|
Loading