Search code examples
dynamictypesstaticlispcommon-lisp

Is Common Lisp static or dynamically typed? If both how is it done?


I m doing a paper on python vs lisp in functional programming. I was seeing the typing system in Common Lisp. I have read that it is dynamic, lexical, strong. But my professor says it is static...can anyone clear this for me? its maddening!


Solution

  • According to the seminal paper on types by Luca Cardelli and Peter Wegner: On understanding types, data abstraction, and polymorphism, ACM Computing Surveys, 17(4):471-522, 1985,

    Programming languages in which the type of every expression can be determined by static program analysis are said to be statically typed.

    This is not true for Common Lisp. Consider, for instance, the following, legal, function definition:

    (defun f(g x)
      (funcall g x))
    

    The type of the expression (funcall g x) inside the body of the function cannot be inferred or determined statically in any way.

    In Common Lisp however you can, if you want, specify the types of the parameters of a function. For instance:

    (defun f (g x)
      (declare (type integer x)
               (type (function (integer) float) g))
      (funcall g x))
    

    and in this case the compiler can infer that the type of (funcall g x) is float.

    So, I think we could say that Common Lisp is not a statically typed language as normally intended, but it can be optionally used as such, if one provides appropriate type information.