Hierarchy of standard-object and standard-class in Common Lisp
Asked Answered
B

3

10

I'm studying Common Lisp (with Lispworks) and I'm trying to get into class system right now. There is a class called standard-object and it is defined as

The class standard-object is an instance of standard-class and is a superclass of every class that is an instance of standard-class except itself.

(taken from http://www.lispworks.com/documentation/HyperSpec/Body/t_std_ob.htm#standard-object) so it is an instance of standard-class

On the other hand standard-class is a subclass of standard-object

>(subtypep 'standard-class 'standard-object)
=>T, T

How can the standard-object be a superclass for the standard-class and be its instance at the same time? If we define standard-class as a subtype, we should define it after defenition of its supertype (e.g. standard-object), so how can it be, that the superclass becomes the instance? Or my logic is just wrong?

Beedon answered 10/10, 2012 at 8:29 Comment(0)
D
12

enter image description here

CLOS is an object system, where CLOS concepts itself are first-class objects. Classes themselves are instances - of a meta class. There is some circularity involved.

There is an instance standard-object. It's an instance of standard-class. It is a class itself. All standard CLOS objects will have it as a superclass. There are other types of objects, for example structures. So standard-object is there as a superclass for all typical CLOS objects.

standard-class is in instance of itself. It is the class of all class objects. Since standard-object is also a class, the instance for the class standard-object is an instance of the class standard-class. Since all standard classes are also CLOS objects, standard-class inherits from standard-object.

CL-USER 22 > (class-of (find-class 'standard-object))
#<STANDARD-CLASS STANDARD-CLASS 40F016A063>

The class of the standard-object class object is standard-class.

CL-USER 23 > (class-of (find-class 'standard-class))
#<STANDARD-CLASS STANDARD-CLASS 40F016A063>

The class of the standard-class class object is standard-class.

CL-USER 24 > (find-class 'standard-object)
#<STANDARD-CLASS STANDARD-OBJECT 40F017732B>

The class standard-object is itself an object and a class. It is a superclass of all CLOS objects.

CL-USER 25 > (find-class 'standard-class)
#<STANDARD-CLASS STANDARD-CLASS 40F016A063>

The class standard-class is itself an object and a class. It is a superclass of all CLOS classes.

Dogtooth answered 10/10, 2012 at 9:44 Comment(2)
Thank you! With these examples it seems I understand now.Beedon
starting at p. 306 in Appendix D. of The Art of the Metaobject Protocol, shows code to bootstrap a CLOS-like language; includes how all of the circularity is detangled; I found it informative, although I can't yet say that I completely understand it.Metallography
N
2

To understand this you will need to understand the concept of meta class. The instance of a meta class is a class and the instance of a class is an object, so basically we have 3 level hierarchy.

standard-class is a meta-class. standard-object is an instance of metaclass standard-class hence it is class. Every other user defined class by default inherits from standard-object class.

So when you are creating a class, you are basically instantiating standard-class metaclass and this new class is inherited by standard-object class.

Nightstick answered 10/10, 2012 at 9:14 Comment(3)
Ok, I think I understand what metaclass is, but do we define standard-object and standard-class simultaneously? In my mind everything happens like this: 0) we define class T; 1) we define class standard-object, it is a subtype of T; there is no standard-class! 2) we define standard-class as a subtype of standard-object and make it metaclass 3) standard-object becomes an instance of standard-class Is it right? It seems a bit strange to me.Beedon
standard-class is not a subtype of standard-object. standard-object is an instance of standard-classNightstick
Then why (subtypep 'standard-class 'standard-object) gives answer true?Beedon
A
1

I will try to give an answer to only one question that seem to be confusing you:

How can the standard-object be a superclass for the standard-class and be its instance at the same time?

I hope you are familiar with the concept of relations from mathematics. Relations that are defined on a set with an operation. Examples of relation would include "is divisible by", "is a", "is equal to" etc. So, "is instance of" is a relation, "is a subclass of" is a relation too. They are by no means the same! A sub-class must be a class, an instance may be a class, but commonly it's something else. If you take an example from the nature: primates are a subclass of mammals - this is the "is a subclass of" relation. Lassie (a dog from a movie) is a mammal - this is an example of "is instance of" relation.

Now, what was probably confusing you is that the function of something, which is "an instance of" something else is to be that something's class. This, indeed doesn't happen much in nature, but here's something I could think of:

Language and grammar. Grammar is a set of rules, which define a language, grammar is, itself a language too (i.e. it "is a subclass of" language), while a language instantiates grammar rules, so a language "is an instance of" grammar.

Ambassadoratlarge answered 10/10, 2012 at 11:42 Comment(1)
That example with grammar is definitely great, thank you so much, I get everything now! I felt there is something wrong like loop in definition, but it seems alright now.Beedon

© 2022 - 2024 — McMap. All rights reserved.