Search
Clojure Protocols & expression problem
Apr 28, 2010
bearophile
Apr 28, 2010
BCS
Apr 28, 2010
bearophile
Apr 28, 2010
bearophile
Apr 28, 2010
bearophile
Apr 28, 2010
Lutger
Apr 28, 2010
bearophile
Apr 28, 2010
retard
Apr 28, 2010
retard
Apr 29, 2010
Walter Bright
Apr 29, 2010
retard
Apr 29, 2010
Ellery Newcomer
Apr 29, 2010
retard
Apr 29, 2010
Walter Bright
Apr 29, 2010
bearophile
Apr 29, 2010
bearophile
Apr 29, 2010
Michel Fortin
Apr 29, 2010
bearophile
Apr 28, 2010
retard
Apr 28, 2010
Lutger
Apr 28, 2010
Ellery Newcomer
Apr 29, 2010
Don
```A short video about Clojure 1.2 Protocols, created to solve the expression problem (that D doesn't solve well yet), they are like fake multimetods because they are like single dispatch :-) (So they need just a virtual table, that the JavaVM can optimize well):

http://vimeo.com/11236603

By the way, you can see an example of the expression problem here, this is D code that solves one case of it: http://codepad.org/RPw6Kxu6

This is the same in Python with the multimethod (module inlined at the top), it's similar to CLisp CLOS solution: http://codepad.org/tWsYomoZ

Bye,
bearophile
```
```Hello bearophile,

> By the way, you can see an example of the expression problem here,

Could you elaborate on what exactly the expression problem is?

--
... <IXOYE><

```
```BCS:

>Could you elaborate on what exactly the expression problem is?<

I am far from being an expert on such matters, I will do what I can to answer.
It's not an esoteric problem, if you program with an OO language you have probably faced it sometime.

I think it's one of the two most important OOP problems not yet solved by D. And I think it deserves some thinking. But it can be work for the design of the D3 language.

Curiously one of the original design goals of Scala was to "solve" the expression problem.

(The other basic OO problem not currently solved by D2 is the variance/covariance of collections that contain objects (like arrays). There is a well known bug report about this. C# has introduced only in its 4.0 release syntax to face this problem, and recently I've sent Andrei the URL of an article that shows how Scala faces and solves this problem in its collections.)

You can see a canonical example of this problem in the D and Python code I've shown in my original post. In OO languages simpler than Scala and CLisp+CLOS the expression problem is sometimes solved using this pattern, the same I've used in the D code:
http://en.wikipedia.org/wiki/Visitor_pattern

The have discussed it a little here: http://lambda-the-ultimate.org/node/2232

You can find some info relative to C++ solutions here too: http://www.mpi-inf.mpg.de/~kettner/courses/lib_design_03/notes/advanced.html#DoubleDispatch

More on the same in C++:
http://lambda-the-ultimate.org/node/2590
It contains a paper from Bjarne Stroustrup too:
http://www.research.att.com/~bs/multimethods.pdf

In the Python code I have used multiple (double) dispatch to solve it, hopefully better.

Bye,
bearophile
```
```Maybe it's better if I actually give a bit of answer too to your question.

If you take a look at my D code, you can see there are many kinds of car parts, and two different kind of "visits" of them, that perform different operations on them.

The problem is, what does it happen in the code if I want to define one (or more) new car parts? What does it happen if I want to add one (or more) more kind of visits? You can slice the design of all this program "vertically" or "horizontally". One design makes it easy to add more car parts, the other design makes it more easy to add "visits".

In the Python code you can see I have used double dispatch. I can define any combination of car part and "visit", they are separated (and in Python there is no need to recompile). CommonLisp has an OO system named CLOS that offers the same power of that python solution.

Bye,
bearophile
```
```Wed, 28 Apr 2010 16:44:07 +0000, BCS wrote:

> Hello bearophile,
>
>
>> By the way, you can see an example of the expression problem here,
>
> Could you elaborate on what exactly the expression problem is?

=> http://en.wikipedia.org/wiki/Expression_Problem

Some academic discussion can also be found:

=> http://lambda-the-ultimate.org/node/2232

But you've probably all (at least nick) blocked the domain since it contains too much academic nonsense coming from the ivory towers.
```
```retard wrote:

> Wed, 28 Apr 2010 16:44:07 +0000, BCS wrote:
>
>> Hello bearophile,
>>
>>
>>> By the way, you can see an example of the expression problem here,
>>
>> Could you elaborate on what exactly the expression problem is?
>
>
>
> => http://en.wikipedia.org/wiki/Expression_Problem
>
> Some academic discussion can also be found:
>
> => http://lambda-the-ultimate.org/node/2232
>
> But you've probably all (at least nick) blocked the domain since it contains too much academic nonsense coming from the ivory towers.

=> http://en.wikipedia.org/wiki/Persecution_complex
```
```According to the paper from Bjarne Stroustrup: http://www.research.att.com/~bs/multimethods.pdf

this is a possible syntax D3 can use for the double dispatch:
void foo(@virtual A a, @virtual B b) {}

Bye,
bearophile
```
```On 04/28/2010 01:15 PM, Lutger wrote:
> retard wrote:
>
>> Wed, 28 Apr 2010 16:44:07 +0000, BCS wrote:
>>
>>> Hello bearophile,
>>>
>>>
>>>> By the way, you can see an example of the expression problem here,
>>>
>>> Could you elaborate on what exactly the expression problem is?
>>
>>
>>
>> =>  http://en.wikipedia.org/wiki/Expression_Problem
>>
>> Some academic discussion can also be found:
>>
>> =>  http://lambda-the-ultimate.org/node/2232
>>
>> But you've probably all (at least nick) blocked the domain since it
>> contains too much academic nonsense coming from the ivory towers.
>
> =>  http://en.wikipedia.org/wiki/Persecution_complex

Good one
```
```bearophile wrote:

> According to the paper from Bjarne Stroustrup: http://www.research.att.com/~bs/multimethods.pdf
>
> this is a possible syntax D3 can use for the double dispatch:
> void foo(@virtual A a, @virtual B b) {}
>
> Bye,
> bearophile

I have studied this paper a bit once, it's good. It also shows that their multimethod implementation is faster than the visitor pattern. I would love to never see a visitor pattern again, it really sucks compared to multimethods.

I think we can first try to make a multimethods library, and we need only one addition to __traits ( to get the overloads of free / static functions) for *open* multimethods.

I tried it once but found it not that easy. It should be possible to have a (perhaps slow) proof of concept that rivals the python example you gave in conciseness.

```
```Lutger:

>I would love to never see a visitor pattern again, it really sucks compared to multimethods.<

You are probably more qualified than me to answer the original question by BCS :-) Multimethods are easy to use and understand (if you don't mix them with too many other things).

>I think we can first try to make a multimethods library,<

Seems a good idea.

>and we need only one addition to __traits ( to get the overloads of free / static functions) for *open* multimethods.<

So far I think DMD has progressed in mostly a linear way, but a temporary experimental dmd branch with this change can be useful to test, try and benchmark this feature. When possible I prefer true experiments.