About Lectures Research Software Blog
Musical Site

Moods Blog

Dojo Shin Kaï

RSS Feed
Thank you!

XHTML 1.0 conformant
CSS 2.0 conformant
Didier Verna's scientific blog: Lisp, Emacs, LaTeX and random stuff.

Friday, August 16 2013

Lisp Corner Cases: Method Combinations

In the process of writing Declt, I had to deepen my knowledge of some Lisp corner cases, notably in the area of introspection. As you know, Lisp has in fact more than 2 namespaces. Sometimes, introspecting a potential symbol definition in one namespace is trivial. COMPILER-MACRO-FUNCTION is one example. The "functional" namespace is heterogeneous but you can still make your way out of macros, regular or generic functions very easily. In the same vein, distinguishing constants, special variables and symbol macros in the "variables" namespace is more complicated because there is no standard way to access that information, but with the help of some vendor-specific machinery (e.g. SB-INT:INFO), it's still doable.

At some point, I tackled the case of method combinations, and to my greatest surprise, found out that introspecting a method combination by name is not simple. In fact, it's not even doable. The reason is that method combinations don't have a namespace proper. Let me explain why.

You define a new method combination of some NAME with DEFINE-METHOD-COMBINATION, and you use it with the :METHOD-COMBINATION option to DEFGENERIC. But how do you introspect a NAME for a potential method combination definition? Well, you can't do exactly that. First, there is no standard way to do so. Next, let's look at what the MOP provides. One would expect something along the lines of FIND-CLASS...

There is indeed something called FIND-METHOD-COMBINATION, but either I'm missing something, or it is really, and I mean really badly designed. The arguments to this function are: a generic function meta-object, a method combination name and some method combination options. So if this function is supposed to be the equivalent of FIND-CLASS for method combinations, what in the hell are the 1st and 3rd arguments for? In fact, it's not supposed to do what its name suggests. According to the AMOP, p.191, the purpose of this function is to "determine the method combination object used by a generic function". In practice however (at least in SBCL), it's not doing that either (see below), and anyway, determining the method combination object used by a generic function is better achieved by using the GENERIC-FUNCTION-METHOD-COMBINATION accessor.

So this protocol doesn't seem to make any sense. In order to understand what to make of all this, I looked at how SBCL handles method combinations (I don't know what other vendors do) and here is what I found. When you call DEFINE-METHOD-COMBINATION for some NAME, SBCL creates a new method for FIND-METHOD-COMBINATION, eql-specialized on that NAME. This method is encapsulated in a closure containing the method combination's definition, ignores its first argument (the generic function meta-object; that's why I said that it's not really doing what the AMOP says it does), recreates and returns a new method combination object on the fly every time it is called.

This has several consequences. First, the notion of "method combination named NAME" doesn't really make sense. Method combinations don't have a proper namespace. Every time you create a new generic function, the method combination essentially becomes local to that function. Redefining a method combination has no effect on existing generic functions using a method combination of the same NAME. To put it differently, you can end up with several generic functions using different yet equally named method combinations.

In Declt, I'm now assuming that programmers behave and only define method combinations once. Which brings us to the second consequence. With that assumption in mind, the "proper" way to introspect a NAME for a method combination definition (at least in SBCL) is to first look for the existence of a FIND-METHOD-COMBINATION method eql-specialized on that NAME, and then call it to retrieve the actual definition.

I haven't thought about it a lot yet, but it would be interesting to investigate the idea of cleaning this up a bit. I mean, establishing a real global namespace for method combinations, creating an alternate FIND-METHOD-COMBINATION protocol more along the lines of FIND-CLASS. And then, it could also be interesting to investigate on the potential applications of allowing method combination redefinition to affect live generic functions, just like class redefinition affects live instances...

Monday, May 10 2010

ELS 2010 paper now available

My paper entitled "CLoX: Common Lisp Objects for XEmacs", presented at the 3rd European Lisp Symposium last week, is now available for download on my website.

You can find it here.

Tuesday, March 9 2010

Paper accepted at ELS 2010

I'm happy to announce that I will be presenting a paper at ELS 2010, the next European Lisp Symposium, in Lisbon. The abstract is given below:

CloX: Common Lisp Objects for XEmacs

CloX is an ongoing attempt to provide a full Emacs Lisp implementation of the Common Lisp Object System, including its underlying meta-object protocol, for XEmacs. This paper describes the early development stages of this project. CloX currently consists in a port of Closette to Emacs Lisp, with some additional features, most notably, a deeper integration between types and classes and a comprehensive test suite. All these aspects are described in the paper, and we also provide a feature comparison with an alternative project called EIEIO.

Wednesday, April 15 2009

Concepts of Programming Languages

These days, I'm reading "Concepts of Programming languages", 8th edition, by Robert W. Sebesta (Addison Wesley). In the category of programming languages comparison books, this is all in all a fair one, especially after having read the crappy "Comparative Programming Languages", 3rd edition, by Clark (Addison Wesley).

As in all those similar books, the room for Lisp is obviously miserable, but that is not very surprising. At least Sebesta seems to know something about it... but wait ! That was until yesterday.

Yesterday, I read chapter 12 (Support for Object Oriented Programming), and this reading pretty much ruined my fun, my evening, and the little consideration I had for the author. On page 508 you can find this:

CLOS, an object-oriented version of Lisp, also supports functional programming.

And there's even a reference to the CLOS specification reference document.

Since the book is suddenly turning the Big Circus way, let's not stop just here. A couple of pages later, there's an interview of Bjarne Stroustrup, for whom having invented C++ is obviously not enough, and so claims that

Currently, C++ is the best language for multi-paradigm programming.

That's it boys. These guys have no clue whatsoever.

Tuesday, March 31 2009

Binary Methods Programming: the CLOS Perspective

The Journal version of this paper of mine is finally available. This is an extended version of the paper I presented at the first European Lisp Symposium last year. It's about twice as long and contains much more details.

Although it's a journal article, the PDF is freely available at the Journal's website.
French Flag English Flag
Copyright (C) 2008 -- 2013 Didier Verna didier@lrde.epita.fr