Data Directed Programming with CLOS

Yeah, it’s been a while. I’ve mostly just been looking for jobs (I hate this town, I really do), working on a couple small projects (the distributed mud, an IRC library, a bot and client which use said library, etc…), lots of yard work type stuff, nothing really to post about.

Having no more luck as usual lately finding a job, I decided to take a break for a couple days and watch the UC Berkeley webcasts for CS61A, their SICP course. After watching the Fri 2/29 video on generic operators it occurred to me that (the general idea of) CLOS seems perhaps to be more like data directed programming than an object orientation system.

While CLOS’s brilliance has never been lost upon me, I was never quite comfortable thinking of it as being OO. I like things such as encapsulation and message passing in an OO system, it just feels more natural to me. Yes, I’ve read Graham’s Why Arc Isn’t Especially Object-Oriented, its response, all the OOP stuff at SchemeWiki, various papers/rants on why it’s a better OO than C++/Java, etc…. CLOS just doesn’t make sense as an OO system to me. I couldn’t quite “get” where CLOS fit in, and so I tended to shy away from its use. But now I have a way of looking at it which makes far more sense to me, and perhaps I’ll wind up grabbing a copy of The Art of the Metaobject Protocol sometime. More importantly, I can think of some *uses* for it now, which I’ll be exploring shortly.

Conversely, I wonder if DDP is a feasible (if, perhaps, naive) method to implement a simplistic CLOS-like system? Oh well, right now most of my interests in OO involve TinyTalk, which sadly is only available for Gambit (and that “other” Scheme-like language). I might try porting it to Snow, at some point.

Anyway, here’s the bit of code that I thought of while watching the video, to give an idea of what the hell I’m on about. It might make more sense if you’re looking at figure 2.2 in section 2.4.3 of SICP as well.

;;; Using a CLOS-ish "OOP" system for Data Directed Programming.
;;; Based on SICP 2.4.3 & a minimal psuedo-CLOS.
;;; matt.a.martin@gmail.com 
;;; Released to the public domain.
;;; Fri Jun 20 15:44:54 CDT 2008

;; Meroon or Swindle or ScmObj etc...
(load "clos-ish.scm")

;; The table's colums.
(def-class polar '() '(magnitude angle))
(def-class rectangle '() '(real imag))

;; The table's rows.
(def-generic real-part)
(def-generic imag-part)
(def-generic magnitude)
(def-generic angle)

;; Define real-part-polar &c... here, possibly involving a lexical scope for "encapsulation", or use lambdas instead, etc...

;; Fill in the table.

;; (put 'real-part 'polar real-part-polar)
(def-method (real-part (x polar))
        (real-part-polar x))

;; (put 'real-part 'rectangle real-part-rectanglular)
(def-method (real-part (x rectangular))
        (real-part-rectangular x))

;; (put 'imag-part 'polar imag-part-polar)
(def-method (imag-part (x polar))
        (imag-part-polar x))

;; (put 'imag-part 'rectangular imag-part-rectangular)
(def-method (imag-part (x rectangular))
        (imag-part-rectangular x))

;; (put 'magnitude 'polar magnitude-polar)
(def-method (magnitude (x polar))
        (magnitude-polar x))

;; (put 'magnitude 'rectangle magnitude-rectangle)
(def-method (magnitude (x rectangular))
        (magnitude-rectangular x))

;; (put 'angle 'polar 'angle-polar)
(def-method (angle (x polar))
        (angle-polar x))

;; (put 'angle 'rectangle angle-rectangle)
(def-method (angle (x rectangle))
        (angle-rectangle x))

June 20, 2008 - Posted by | Programming, Scheme, web

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: