DOC PREVIEW
UMBC CMSC 331 - Object-Oriented Lisp

This preview shows page 1-2-15-16-17-32-33 out of 33 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

25Object-Oriented LispThis chapter discusses object-oriented programming in Lisp. Common Lispincludes a set of operators for writing object-orientedprograms. Collectively theyare called the Common Lisp Object System, orCLOS. Here we consider CLOS notjust as a way of writing object-oriented programs, but as a Lisp program itself.SeeingCLOS in this light is the key to understanding the relation between Lisp andobject-oriented programming.25.1 Plus c¸a ChangeObject-orientedprogramming means a change in the way programs are organized.This change is analogous to the one that has taken place in the distribution ofprocessor power. In 1970, a multi-user computer system meant one or two bigmainframes connectedto a large number of dumb terminals. Now it is more likelyto mean a large number of workstations connected to one another by a network.The processing power of the system is now distributed among individual usersinstead of centralized in one big computer.Object-oriented programming breaks up traditional programs in much thesame way: instead of having a single program which operates on an inert massof data, the data itself is told how to behave, and the program is implicit in theinteractions of these new data “objects.”For example, suppose we want to write a program to find the areas of two-dimensional shapes. One way to do this would be to write a single function whichlooked at the type of its argument and behaved accordingly:34825.2 OBJECTS IN PLAIN LISP 349(defun area (x)(cond ((rectangle-p x) (* (height x) (width x)))((circle-p x) (* pi (expt (radius x) 2)))))The object-orientedapproach is to make each object able to calculate its own area.The area function is broken apart and each clause distributed to the appropriateclass of object; the area method of the rectangle class might be#’(lambda (x) (* (height x) (width x)))and for the circle class,#’(lambda (x) (* pi (expt (radius x) 2)))In this model, we ask an object what its area is, and it responds according to themethod provided for its class.The arrival ofCLOS might seem a sign that Lisp is changing to embrace theobject-oriented paradigm. Actually, it would be more accurate to say that Lispis staying the same to embrace the object-oriented paradigm. But the principlesunderlying Lisp don’t have a name, and object-oriented programming does, so ◦there is a tendency now to describe Lisp as an object-oriented language. It wouldbe closer to the truth to say that Lisp is an extensible language in which constructsfor object-oriented programming can easily be written.SinceCLOS comes pre-written, it is not false advertising to describe Lisp asan object-oriented language. However, it would be limiting to see Lisp as merelythat. Lisp is an object-oriented language, yes, but not because it has adoptedthe object-oriented model. Rather, that model turns out to be just one morepermutation of the abstractions underlying Lisp. And to prove it we haveCLOS,aprogram written in Lisp, which makes Lisp an object-oriented language.The aim of this chapter is to bring out the connection between Lisp andobject-oriented programming by studyingCLOS as an example of an embeddedlanguage. This is also a good way to understandCLOS itself: in the end, nothingexplains a language feature more effectively than a sketch of its implementation.In Section 7.6, macros were explained this way. The next section gives a similarsketch of how to build object-oriented abstractions on top of Lisp. This programprovides a reference point from which to describeCLOS in Sections 25.3–25.6.25.2 Objects in Plain LispWe can mold Lisp into many different kinds of languages. There is a particularlydirect mapping between the concepts of object-oriented programming and thefundamental abstractions of Lisp. The size ofCLOS tends to obscure this fact. So350 OBJECT-ORIENTED LISPbefore looking at what we can do with CLOS, let’s see what we can do with plainLisp.Much of what we want from object-oriented programming, we have alreadyin Lisp. We can get the rest with surprisingly little code. In this section, we willdefine an object system sufficient for many real applications in two pages of code.Object-oriented programming, at a minimum, implies1. objects which have properties2. and respond to messages,3. and which inherit properties and methods from their parents.In Lisp, there are already several ways to store collections of properties.One way would be to represent objects as hash-tables, and store their propertiesas entries within them. We then have access to individual properties throughgethash:(gethash ’color obj)Since functions are data objects, we can store them as properties too. This meansthat we can also have methods; to invoke a given method of an object is to funcallthe property of that name:(funcall (gethash ’move obj) obj 10)We can define a Smalltalk style message-passing syntax upon this idea:(defun tell (obj message &rest args)(apply (gethash message obj) obj args))so that to tell obj to move 10 we can say(tell obj ’move 10)In fact, the only ingredient plain Lisp lacks is inheritance, and we can providea rudimentary version of that in six lines of code, by defining a recursive versionof gethash:(defun rget (obj prop)(multiple-value-bind (val win) (gethash prop obj)(if win(values val win)(let ((par (gethash ’parent obj)))(and par (rget par prop))))))25.2 OBJECTS IN PLAIN LISP 351(defun rget (obj prop)(some2 #’(lambda (a) (gethash prop a))(get-ancestors obj)))(defun get-ancestors (obj)(labels ((getall (x)(append (list x)(mapcan #’getall(gethash ’parents x)))))(stable-sort (delete-duplicates (getall obj))#’(lambda (x y)(member y (gethash ’parents x))))))(defun some2 (fn lst)(if (atom lst)nil(multiple-value-bind (val win) (funcall fn (car lst))(if (or val win)(values val win)(some2 fn (cdr lst))))))Figure 25.1: Multiple inheritance.If we just use rget in place of gethash, we will get inherited properties andmethods. We specify an object’s parent thus:(setf (gethash ’parent obj) obj2)So far we have only single inheritance—an object can only have one parent.But we can have multiple inheritance by making the parent property a list, anddefining rget as in Figure 25.1.With single inheritance, when we wanted to retrieve some property of anobject, we just searched recursively up its ancestors. If the object itself had noinformation about the property we wanted, we looked at its parent, and so on.With multiple inheritance we want to perform the same


View Full Document

UMBC CMSC 331 - Object-Oriented Lisp

Documents in this Course
Semantics

Semantics

14 pages

Java

Java

12 pages

Java

Java

31 pages

V

V

46 pages

Semantics

Semantics

11 pages

Load more
Download Object-Oriented Lisp
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Object-Oriented Lisp and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Object-Oriented Lisp 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?