3.3 Using type-specific operations

3.3.3 Propagating type information

Liquid Common Lisp implements type propagation, which is the process of passing type information to all values assigned to a declared variable, returned by a special form, or returned by a function that contains type declarations. Type propagation is another means of passing the type information that allows the Compiler to generate more efficient code. Type information is propagated under the following circumstances:

The following example defines a function that adds the absolute values of a list of fixnum integers and produces a fixnum total:

(defun add-list (l)
  (let ((sum 0))
    (declare (fixnum sum))
    (dolist (i l sum)
      (declare (fixnum i))
      (setq sum
            (+ sum 
               (the fixnum
                    (if (> i 0) i (- i))))))))

In this definition, the type of the variablesum is propagated to the expression whose value is assigned tosum. The explicitly declared type of the second term ofsum is propagated to thei and (-i) components of theif expression. The Compiler can thus compile all of the operators as fixnum-specific operators.

For example, suppose you define a structurebird and an instancebird1 as follows:

(defstruct bird 
  (weight 0.0 :type float)
  (height 0.0 :type float)
  (age 0 :type fixnum))

(setf bird1 (make-bird :age 1))

Because theage field has been declared to be of type fixnum, in the following code the Compiler expects both the argument to1+ and the returned value of the expression to be fixnums:

(setf (bird-age bird1)
      (1+ (bird-age bird1)))

The Compiler can thus generate the fixnum-specific form of1+.

(defun use-square (z)
  (declare (fixnum z))
  (the fixnum (square z)))

Because the variablez and the result ofsquare are both declared to be of typefixnum, the type information propagates to the call to* in the body ofsquare. The Compiler can thus use a fixnum-specific form of the* operator.


The Advanced User's Guide - 9 SEP 1996

Generated with Harlequin WebMaker