J. Mini
J. Mini

Reputation: 1610

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.

Upvotes: 4

Views: 193

Answers (4)

Svante
Svante

Reputation: 51551

You give a definition for »first class«, but no definition for »type«.

A type is a set.

As such, it is quite impractical to directly reify types, in general. Instead, we describe or specify types, e. g. by giving a rule to test membership of a thing in them.

In Common Lisp, these descriptions are called type specifiers. Type specifiers can be symbols, classes, or lists (look at chapter 4.2, specifically 4.2.3 of the Spec, e. g. here). These are all values that trivially pass your definition of »first class«.

One interesting kind of type specifier, which can be seen as a sort of general fallback, is the ones using satisfies, which directly take a predicate to test type membership. However, the more general your type specifier, the less things you can do automatically and specifically. Nevertheless, how useful they are was not the question.

I think that Peter Norvig is a bit sloppy with the words type and class here, and most of his points can be made with first-class classes already.

Upvotes: 1

ignis volens
ignis volens

Reputation: 9282

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.

Upvotes: 5

Rainer Joswig
Rainer Joswig

Reputation: 139411

I'd think one of the basic things is to create objects from types/classes and to dispatch over the used type/class.

In Common Lisp one can say

(let ((class 'my-window-special-class))
  (make-instance class))

One can also dispatch over something like that

(defmethod foo (x y (class (eql 'my-window-special-class)))
  (make-instance class :x (+ x 5) :y (+ y 10)))

This is just ONE example there are more possibilities.

Upvotes: 1

Barmar
Barmar

Reputation: 782682

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.

Upvotes: 2

Related Questions