Re: THE to function as DECLARE?



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))

.



Relevant Pages

  • Re: "Sorting" assignment
    ... If the array is already sorted, this means that you end up ... less time than a bubble sort, ...     int intLeft, ... I don't declare anything inside of a loop is why. ...
    (comp.programming)
  • Re: Whats the difference between tactics, strategy, position, etc.?
    ... let me at least present a few classic examples of positional play, ...   With his last two moves Black has already accepted a more cramped ... White's rooks would swoop down on his king like the Mongol ... The square f5 is badly ...
    (rec.games.chess.misc)
  • Re: Irrationality of sqrt (n^2 - 1)
    ... *rational* if and only if n is a perfect square. ...   O.K; I did confuse things. ... than that of the irrationality of sqr2. ...
    (sci.math)
  • Re: Microsoft Office updates that are not backward compatable with Microsoft VB 6.5
    ... Dim rst, rstFill As ADODB.Recordset ... That is, in VB, any variable declared without "As" will be Variant, whether ... better readability and for the explicity, I never declare variable in one ...    Dim rst, rstFill As ADODB.Recordset ...
    (microsoft.public.office.developer.vba)
  • Re: Code style question
    ... It does matter a whole lot more with environment access as DEFTYPE ...     definitions can access the current compilation environment. ... (declare (notinline foo) ...
    (comp.lang.lisp)

Loading