Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In what sense are types in Common Lisp first-class?

Peter Norvig famously claimed that many OOP design patterns are trivial or redundant in Lisp. Slide #10 here claims that first-class types replace many of them.

In what sense are types first-class in Common Lisp? For reference, Structure and Interpretation of Computer Programs gives the following as the conditions that an element of a programming language must satisfy to be considered first-class:

  1. They may be named by variables.

  2. They may be passed as arguments to procedures.

  3. They may be returned as the results of procedures.

  4. They may be included in data structures

a demonstration that types in Common Lisp satisfy the above conditions would be a wonderful answer.

like image 675
J. Mini Avatar asked Sep 16 '25 12:09

J. Mini


2 Answers

Types are not first-class in Common Lisp: the language exposes no object which represents a type and thus trivially fails all the requirements you give.

Classes are first-class and classes correspond to a subset of types.

Type specifiers are also first-class: a type specifier like integer or (integer 0 (10)) is obviously first-class because symbols and lists are first-class. But type specifiers are not types, any more than the symbol cons or the list (lambda (x) x) is a function.

So Norvig is wrong about this.

like image 107
ignis volens Avatar answered Sep 19 '25 06:09

ignis volens


Types aren't a distinct data type in Common Lisp, they're represented using type specifiers. These are symbols (e.g. FIXNUM or RANDOM-STATE), lists (e.g. (INTEGER 3 5) or (AND LIST (NOT NULL)), and class objects (e.g. the value of (FIND-CLASS 'STANDARD-CLASS)), which are all first-class objects.

You can pass these around and then use them when calling something like TYPEP:

(let ((some-class 'FIXNUM))
  (print (typep 3 some-class)))

You can also create new type specifier names using the DEFTYPE macro. However, there's no standard functional interface to this. In this respect types are somewhat second-class.

like image 37
Barmar Avatar answered Sep 19 '25 04:09

Barmar