4

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.

J. Mini
  • 1,868
  • 1
  • 9
  • 38
  • "a demonstration that types in Common Lisp satisfy the above conditions would be a wonderful answer" — I think that is the most explicitly declared declaration of expectations from an answer I've ever seen on this site. I like it. – M. Justin Jun 20 '23 at 21:31
  • I thought that slide #11 explains, what Norvig means... – Rainer Joswig Jun 21 '23 at 06:57
  • @RainerJoswig It *explains* but it doesn't *demonstrate*. I'm asking for examples. – J. Mini Jun 21 '23 at 16:05

4 Answers4

5

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.

ignis volens
  • 7,040
  • 2
  • 12
  • 1
    I think you answer is on-spot except for the last sentence - Peter Norvig explains just on next slide (11) of referred presentation what he means, and that is not wrong, So I would say he used the term as simplified reference to the next slide, not as formal "types are first class as defined by SICP". – Tomas Zellerin Jun 21 '23 at 06:31
  • and I am not sure if it makes sense to demonstrate the four requirements above to show that classes and type specifiers are first-class to have "wonderful answer", it is trivial. – Tomas Zellerin Jun 21 '23 at 06:37
  • @TomasZellerin On slide 11 Norvig says 'Types and classes are objects at run-time (not just at compile-time)'. Given the view (which I have) that type specifiers are not types that statement, and most of the statements on slide 11, is wrong. So, in my view, Norvig is wrong: he should be using the term 'type specifier'. What he's doing is like saying the list `(lambda (x) x)` is a function: it's not, it's a function specifier (or, if you like, the name of a function). – ignis volens Jun 21 '23 at 07:47
  • @ignisvolens That seems like a very important clarification. You may want to add it to your answer, perhaps with a demonstration that the specifiers are first-class in (at least) the sense he means. – J. Mini Jun 21 '23 at 16:07
  • @ignisvolens I would read it as he speaks about classes primarily, and the question is whether the "classes map to types" can be simplified as "classes are types" – Tomas Zellerin Jun 22 '23 at 06:23
  • @TomasZellerin then he should say what he means, not something else. Classes are not types, they are at best an infinitesimal subset of types (there are an infinite number of types in any CL implementation but only a finite number of classes). Sorry, Norvig doesn't get a pass on this just because he's famous. – ignis volens Jun 22 '23 at 10:50
  • @J.Mini I have added that. I haven't added some laborious demonstration that lists and symbols are first-class in Lisp: that would serve no useful purpose. – ignis volens Jun 22 '23 at 10:51
2

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.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • the same slide also talks about "Method Combination" so maybe the "types" intended there are more in the CLOS sense? – Will Ness Jun 20 '23 at 22:18
  • @WillNess I think they would have said "class" if they were being specific to that. – Barmar Jun 21 '23 at 15:02
1

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.

Rainer Joswig
  • 136,269
  • 10
  • 221
  • 346
1

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.

Svante
  • 50,694
  • 11
  • 78
  • 122