Common Lisp class hierarchy

让人想犯罪 __ 提交于 2019-12-04 17:41:00

Types and classes are two different things.

Don't confuse them.

Some types have corresponding classes. Most have not.

atom is the name of a type, but not of a class.

CL-USER 18 > (find-class 'atom nil)
NIL

Since atom is not a class, it can't be in any class precedence list. Thus atom is not in the class precedence list of structure-object.

The type structure is non-standard and not defined by ANSI CL.

Types are not in a class precedence list, classes are.

The interface for types:

  • create a type -> DEFTYPE
  • is something of a type? -> TYPEP
  • is a type a subtype of another type? -> SUBTYPEP
  • what is a type for something? -> TYPE-OF

That's basically all you can do with types.

CLOS classes have corresponding types

CLOS functions and class precedence lists don't work with types, but classes have corresponding types.

CL-USER 23 > (defclass bar () ())
#<STANDARD-CLASS BAR 40200A2413>

CL-USER 24 > (typep (make-instance 'bar) 'bar)
T

CL-USER 25 > (type-of (make-instance 'bar))
BAR

CL-USER 26 > (class-of (make-instance 'bar))
#<STANDARD-CLASS BAR 40200A2413>

CLOS works with classes. Thus in an extended CLOS you can ask for subclasses and superclasses. But not for subtypes or supertypes.

History: Types and CLOS

Common Lisp started in CLtL1 with types and no CLOS.

CLOS and CLOS classes have been added years later. They have been added in such a way that some types got corresponding classes and such that classes have corresponding types.

Common Lisp allows to define types using type specifiers like AND, OR, SATISFIES, MEMBER, NOT, ... For those no corresponding CLOS classes exist.

There are also compound type specifiers like (integer 0 100). There are also no corresponding CLOS classes for those types.

CL-USER 31 > (deftype integer100 () '(integer 0 100))
INTEGER100

CL-USER 32 > (find-class 'integer100)

Error: INTEGER100 is not the name of a class

All classes are types, but not all types are classes. Some types are defined in terms of other types. An atom is anything that's not a cons. Since an instance of a structure isn't a cons, it's an atom. From the HyperSpec:

Type ATOM

Supertypes:

atom, t

Description:

It is equivalent to (not cons).

As another common example, consider the type list, which is equivalent to (or null cons). NIL (of type null) is a list, and a cons is a list. That's it.

Neither atom nor list are classes, but they are types.

Because we can have complement types and union types and intersection types, the concept of type hierarchies gets a bit more complicated, even if there are still proper class hierarchies.

The Hyperspec says the precedence list for structure-object is only itself and t. Are atom and structure not types in the hierarchy?

That's not quite what the HyperSpec says. The HyperSpec says that t is a supertype of structure-type. In Common Lisp, you can define arbitrary new types. E.g., with a simple (deftype my-new-type (or structure-object list)), you'd have (typep *p1* 'my-new-type) return true as well. That doesn't suddenly invalidate what the HyperSpec says about the class precedence of structure-object.

By analogy with set theory, it would seem that all Common Lisp types/classes could theoretically be subdivided into two subclasses of t

That would be one way of doing it, but because of the ability to define new types in terms of union of types, intersection of types, and complements of types, there are many ways that you could partition the objects of Common Lisp by type.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!