0% found this document useful (0 votes)
98 views6 pages

Lisp - Tears of Joy Part 8

The document provides an overview of Common Lisp Object System (CLOS), the object-oriented programming framework in Common Lisp. It discusses key concepts of CLOS such as: 1) It is a layered system designed for flexibility with multiple levels of abstraction. 2) It is based on generic functions rather than message passing. 3) It supports multiple inheritance through subclasses. 4) It provides powerful method combination facilities to define how methods are combined. 5) All primary CLOS entities like generic functions and classes are first-class objects that can be explicitly created and manipulated.

Uploaded by

ksenthil77
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
98 views6 pages

Lisp - Tears of Joy Part 8

The document provides an overview of Common Lisp Object System (CLOS), the object-oriented programming framework in Common Lisp. It discusses key concepts of CLOS such as: 1) It is a layered system designed for flexibility with multiple levels of abstraction. 2) It is based on generic functions rather than message passing. 3) It supports multiple inheritance through subclasses. 4) It provides powerful method combination facilities to define how methods are combined. 5) All primary CLOS entities like generic functions and classes are first-class objects that can be explicitly created and manipulated.

Uploaded by

ksenthil77
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Write For Us

Submit Tips

Subscribe to Print Edition

Search

HOME

REVIEWS

HOW-TOS

CODING

INTERVIEWS

FEATURES

OVERVIEW

BLOGS

SERIES

IT ADMIN

Lisp: Tears of Joy, Part 8


By Vivek Shangari on January 31, 2012 in Coding, Developers 0 Comments

Search for:

Search

Get Connected RSS Feed Twitter

Lisp has been hailed as the worlds most powerful programming language. But only the top percentile of programmers use it because of its cryptic syntax and academic reputation. This is rather unfortunate, since Lisp isnt that hard to grasp. If you want to be among the crme de la crme, this series is for you. This is the eighth article in the series that began in June 2011.
In rare moments of self-reflection, when I allow myself to doubt my skills as a Lisp evangelist, I sometimes wonder if I have left behind some of my fellow programmers who favour the objectoriented style of programming. Just because I have been focusing on Lisp as a functional programming paradigm, it doesnt mean we dont have a role for you in our plans of world domination. Read on to know where you fit in.

Functional vs object-oriented (OO) programming


With an OO approach, programmers write code that describes in exacting detail the steps that the computer must take to accomplish the goal. They focus on how to perform tasks, and how to track changes in state. They would use loops, conditions and method calls as their primary flow control, and instances of structures or classes as primary manipulation units. OO tries to control state behind object interfaces. In contrast, functional programming (FP) involves composing the problem as a set of functions to be executed. FP programmers focus on what information is desired and what transformations are required, by carefully defining the input to each function and what each function returns. They would use function calls, including recursion, as their primary flow control, functions as first-class objects and data collections as primary manipulation units. FP tries to minimise state by using pure functions as much as possible. According to Michael Feathers: OO makes code understandable by encapsulating moving parts. FP makes code understandable by minimising moving parts. Conard Barski points out that the critics of the OO programming style may complain that objectoriented techniques force data to be hidden away in a lot of disparate places by requiring them to live inside many different objects. Having data located in disparate places can make programs

LINUX For You on

Follow

+2,503

difficult to understand, especially if that data changes over time. Therefore, many Lispers prefer to use functional techniques over object-oriented techniques, though the two can often be used together with some care. Nonetheless, there are still many domains in which object-oriented techniques are invaluable, such as in user interface programming or simulation programming. On the other hand, James Hague, in his assessment of functional programming argues that, 100 per cent pure functional programming doesnt work. Even 98 per cent pure functional programming doesnt work. But if the slider between functional purity and 1980s BASIC-style imperative messiness is kicked down a few notches say to 85 per cent then it really does work. You get all the advantages of functional programming, but without the extreme mental effort and un-maintainability that increases as you get closer and closer to perfectly pure.

CLOS
If OO is what gets you going, Common Lisp offers the most sophisticated object-oriented programming framework of any major programming language. Its called Common Lisp Object System (CLOS). It is customisable at a fundamental level, using the Meta-Object Protocol (MOP). It has been claimed that theres really nothing like it anywhere else in programming. It lets you control incredibly complex software without losing control over the code. Let the tears of joy flow
Popular Comments Tag cloud
August 13, 2013 39 Comments Diksha P Gupta

India has immense under-utilised talent in the cloud security space


May 6, 2013 6 Comments Priyanka Sarkar

PHP Development: A Smart Career Move


June 20, 2013 3 Comments sophie-samuel

Object-oriented programming in Common Lisp


What is CLOS?
#1: It is a layered system designed for flexibility.
One of the design goals of CLOS is to provide a set of layers that separate different programming language concerns from one another. The first level of the Object System provides a programmatic interface to object-oriented programming. This level is designed to meet the needs of most serious users, and to provide a syntax that is crisp and understandable. The second level provides a functional interface into the heart of the Object System. This level is intended for programmers who are writing very complex software or a programming environment. The first level is written in terms of this second level. The third level provides the tools for programmers who are writing their own object-oriented language. It allows access to the primitive objects and operators of the Object System. It is this level at which the implementation of the Object System itself is based. The layered design of CLOS is founded on the meta-object protocol, a protocol that is used to define the characteristics of an object-oriented system. Using the meta-object protocol, other functional or programmatic interfaces to the Object System, as well as other object systems, can be written.

New and amazing features of Linux


June 20, 2013 3 Comments Priyanka Sarkar

What it Takes to be an Open Source Expert


May 6, 2013 1 Comments Deepti Sharma

A Simple guide to building your own Linux Kernel

#2: It is based on the concept of generic functions rather than on messagepassing.


This choice is made for two reasons: 1. there are some problems with message-passing in operations of more than one argument; 2. the concept of generic functions is a generalisation of the concept of ordinary Lisp functions. A key concept in object-oriented systems is that given an operation and a tuple of objects on which to apply the operation, the code that is most appropriate to perform the operation is selected, based on the classes of the objects. In most message-passing systems, operations are essentially properties of classes, and this selection is made by packaging a message that specifies the operation and the objects to which it applies, before sending that message to a suitable object. That object then takes responsibility for selecting the appropriate piece of code. These pieces of code are called methods.

#3: It is a multiple inheritance system.


Another key concept in object-oriented programming is the definition of structure and behaviour on the basis of the class of an object. Classes thus impose a type system the code that is

used to execute operations on objects depends on the classes of the objects. The sub-class mechanism allows classes to be defined that share the structure and the behaviour of other classes. This sub-classing is a tool for modularisation of programs.

#4: It provides a powerful method combination facility.


Method combination is used to define how the methods that are applicable to a set of arguments can be combined to provide the values of a generic function. In many object-oriented systems, the most specific applicable method is invoked, and that method may invoke other, less specific methods. When this happens, there is often a combination strategy at work, but that strategy is distributed throughout the methods as local control structure. Method combination brings the notion of a combination strategy to the surface, and provides a mechanism for expressing that strategy.

#5: The primary entities of the system are all first-class objects.
In the Common Lisp Object System, generic functions and classes are first-class objects with no intrinsic names. It is possible and useful to create and manipulate anonymous generic functions and classes. The concept of first-class is important in Lisp-like languages. A first-class object is one that can be explicitly made and manipulated; it can be stored in any location that can hold general objects.

What CLOS is not


It does not make for a great pickup conversation at the bar. I tried. It did not work! It also does not attempt to solve problems of encapsulation. The inherited structure of a class depends on the names of the internal parts of the classes from which it inherits. CLOS does not support subtractive inheritance. Within Common Lisp, there is a primitive module system that can be used to help create separate internal namespaces.

Classes
The d e f c l a s smacro is used to define a new class. The definition of a class consists of its name, a list of its direct super-classes, a set of slot specifiers and a set of class options. The direct super-classes of a class are those from which the new class inherits structure and behaviour. When a class is defined, the order in which its direct super-classes are mentioned in the d e f c l a s sform defines a local precedence order on the class and those super-classes. The local precedence order is represented as a list consisting of the class, followed by its direct super-classes, in the order mentioned in the d e f c l a s sform. The following two classes define a representation of a point in space. The x y p o s i t i o nclass is a sub-class of the p o s i t i o n class:
>( d e f c l a s sp o s i t i o n( )( ) ) >( d e f c l a s sx y p o s i t i o n( p o s i t i o n ) ( ( x: i n i t f o r m0 ) ( y: i n i t f o r m0 ) ) ( : a c c e s s o r p r e f i xp o s i t i o n ) )

The p o s i t i o nclass is useful if we want to create other sorts of representations for spatial positions. The xand ycoordinates are initialised to 0 in all instances, unless explicit values are supplied for them. To refer to the x coordinate of an instance of x y p o s i t i o n , you would write:
>( p o s i t i o n xp o s i t i o n )

To alter the x coordinate of that instance, you would write:


( s e t f( p o s i t i o n xp o s i t i o n )n e w x )

The macro d e f c l a s sis part of the Object System programmatic interface and, as such, is on the first of the three levels of the Object System.

Generic functions
The class-specific operations of the Common Lisp Object System are provided by generic functions and methods. A generic function is one whose behaviour depends on the classes or identities of the arguments supplied to it. The methods associated with the generic function define the class-specific operations of the generic function. Like an ordinary Lisp function, a generic function takes arguments, performs a series of operations and returns values. An ordinary function has a single body of code that is always executed when the function is called. A generic function is able to perform different series of

operations and to combine the results of the operations in different ways, depending on the class or identity of one or more of its arguments. Generic functions are defined by means of the d e f g e n e r i c o p t i o n sand d e f m e t h o dmacros. The d e f g e n e r i c o p t i o n smacro is designed to allow for the specification of properties that pertain to the generic function as a whole, and not just to individual methods. The d e f m e t h o d form is used to define a method. If there is no generic function of the given name, however, it automatically creates a generic function with default values for the argument precedence order (left-to-right, as defined by the lambda-list), the generic function class (the class s t a n d a r d g e n e r i c f u n c t i o n ), the method class (the class s t a n d a r d m e t h o d ) and the method

combination type (s t a n d a r d m e t h o dc o m b i n a t i o n ).

Methods
The class-specific operations provided by generic functions are themselves defined and implemented by methods. The class or identity of each argument to the generic function indicates which method or methods are eligible to be invoked. A method object contains a method function, an ordered set of parameter specialisers that specify when the given method is applicable, and an ordered set of qualifiers that are used by the method combination facility to distinguish between methods. The d e f m e t h o dmacro is used to create a method object. A d e f m e t h o dform contains the code that is to be run when the arguments to the generic function cause the method that it defines, to be selected. If a d e f m e t h o dform is evaluated, and a method object corresponding to the given generic function name, parameter specialisers and qualifiers already exists, then the new definition replaces the old. Generic functions can be used to implement a layer of abstraction on top of a set of classes. For example, the x y p o s i t i o nclass can be viewed as containing information in polar coordinates. Two methods have been defined p o s i t i o n r h oand p o s i t i o n t h e t a , that calculate the and coordinates given an instance of x y p o s i t i o n :
>( d e f m e t h o dp o s i t i o n r h o( ( p o sx y p o s i t i o n ) ) ( l e t( ( x( p o s i t i o n xp o s ) ) ( y( p o s i t i o n yp o s ) ) ) ( s q r t( +( *xx )( *yy ) ) ) ) ) >( d e f m e t h o dp o s i t i o n t h e t a( ( p o sx y p o s i t i o n ) ) ( a t a n( p o s i t i o n yp o s )( p o s i t i o n xp o s ) ) )

It is also possible to write methods that update the virtual slots p o s i t i o n r h oand p o s i t i o n t h e t a :
>( d e f m e t h o d s e t fp o s i t i o n r h o( ( p o sx y p o s i t i o n ) )( r h o ) ( l e t *( ( r( p o s i t i o n r h op o s ) ) ( r a t i o( /r h or ) ) ) ( s e t f( p o s i t i o n xp o s )( *r a t i o( p o s i t i o n xp o s ) ) ) ( s e t f( p o s i t i o n yp o s )( *r a t i o( p o s i t i o n yp o s ) ) ) ) ) >( d e f m e t h o d s e t fp o s i t i o n t h e t a( ( p o sx y p o s i t i o n ) )( t h e t a ) ( l e t( ( r h o( p o s i t i o n r h op o s ) ) ) ( s e t f( p o s i t i o n xp o s )( *r h o( c o st h e t a ) ) ) ( s e t f( p o s i t i o n yp o s )( *r h o( s i nt h e t a ) ) ) ) )

To update the -coordinate you may write:


>( s e t f( p o s i t i o n r h op o s )n e w r h o )

This is precisely the same syntax that would be used if the positions were explicitly stored as polar coordinates.

Class redefinition
The Common Lisp Object System provides a powerful class-redefinition facility. When a d e f c l a s sform is evaluated, and a class with the given name already exists, the existing class is redefined. Redefining a class modifies the existing class object to reflect the new class definition. You may define methods on the generic function c l a s s c h a n g e dto control the class redefinition process. This generic function is invoked automatically by the system after d e f c l a s shas been used to redefine an existing class; for example, suppose it becomes apparent that the application that requires representing positions uses polar coordinates more than it uses rectangular coordinates. It might make sense to define a sub-class of p o s i t i o nthat uses polar coordinates:
>( d e f c l a s sr h o t h e t a p o s i t i o n( p o s i t i o n ) ( ( r h o: i n i t f o r m0 )

( t h e t a: i n i t f o r m0 ) ) ( : a c c e s s o r p r e f i xp o s i t i o n ) )

The instances of x y p o s i t i o ncan be automatically updated by defining a class-changed method:


>( d e f m e t h o dc l a s s c h a n g e d( ( o l dx y p o s i t i o n ) ( n e wr h o t h e t a p o s i t i o n ) ) ; ;C o p yt h ep o s i t i o ni n f o r m a t i o nf r o mo l dt on e wt om a k en e w ; ;b ear h o t h e t a p o s i t i o na tt h es a m ep o s i t i o na so l d . ( l e t( ( x( p o s i t i o n xo l d ) ) ( y( p o s i t i o n yo l d ) ) ) ( s e t f( p o s i t i o n r h on e w )( s q r t( +( *xx )( *yy ) ) ) ( p o s i t i o n t h e t an e w )( a t a nyx ) ) ) )

At this point, we can change an instance of the class x y p o s i t i o n ,p 1 , to be an instance of


r h o t h e t a p o s i t i o nby using change-class:
>( c h a n g e c l a s sp 1' r h o t h e t a p o s i t i o n )

Inheritance
Inheritance is the key to program modularity within CLOS. A typical object-oriented program consists of several classes, each of which defines some aspect of behaviour. New classes are defined by including the appropriate classes as super-classes, thus gathering the desired aspects of behaviour into one class. In general, slot descriptions are inherited by sub-classes. That is, slots defined by a class are usually slots implicitly defined by any sub-class of that class, unless the sub-class explicitly shadows the slot definition. A class can also shadow some of the slot options declared in the
d e f c l a s sform of one of its super-classes by providing its own description for that slot.

A sub-class inherits methods in the sense that any method applicable to an instance of a class is also applicable to instances of any sub-class of that class (all other arguments to the method being the same). The inheritance of methods acts the same way regardless of whether the method was created by using d e f m e t h o dor by using one of the d e f c l a s soptions that cause methods to be generated automatically. I hope with this article I have managed to convince OO programmers that Lisp is generous enough to cater to your style of thinking. Stick with me, and I promise that you wont be disappointed. So far weve seen how to fit the nuts and bolts into the engine. Next month, well learn how to paint it a nice shiny red I am referring to Graphical Programming in Lisp!

References
Let Over Lambda, Doug Hoyte CLOS: Integrating Object-Oriented and Functional programming, Richard P. Gabriel, Jon L White, Daniel G. Bobrow

Related Posts:
Lisp: Tears of Joy, Part 4 Lisp: Tears of Joy, Part 10 Lisp: Tears of Joy, Part 7 Lisp: Tears of Joy, Part 5 Loading Library Files in C++
Tags: CLOS, Common Lisp, Common Lisp Object System, Conard Barski, control functions, Daniel G. Bobrow, Doug Hoyte, flow control, functional programming, James Hague, Jon L White, LFY January 2012, Lisp, Lisp: Tears of Joy series, Michael Feathers, object interfaces, object-oriented programming, oo approach, oo programming, programming, programming style, recursion, Richard P Gabriel

Article written by:


Vivek Shangari
The author is a hard-core hacker, if there ever was one. He always thinks programs, and holds a piece of code in his head all the time. His favourite past-time is to stop random strangers on the street and start talking about the benefits of open source over proprietary software, till they agree to switch sides, or threaten to jump off the nearest building. Connect with him: Website

Previous Post

Next Post

Developing Apps on Qt, Part 1

Device Drivers, Part 14: A Dive Inside the Hard Disk for Understanding Partitions

Reviews

How-Tos

Coding

Interviews

Features

Overview

Blogs

Search
Popular tags
Linux , ubuntu, Java, MySQL, Google, python, Fedora, Android, PHP, C, html, w eb applications , India, Microsoft, unix , Window s , Red Hat, Oracle, Security , Apache, xml, LFY April 2012, FOSS, GNOME, http, JavaScript, LFY June 2011, open source, RAM, operating systems

For You & Me Developers Sysadmins Open Gurus CXOs Columns

All published articles are released under Creative Commons Attribution-NonCommercial 3.0 Unported License, unless otherw ise noted. LINUX For You is pow ered by WordPress, w hich gladly sits on top of a CentOS-based LEMP stack.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy