Multiple dispatch

Help requests on developing Logtalk applications

Moderator: Paulo Moura

Post Reply
Jaetan
Posts: 13
Joined: Thu Apr 09, 2009 11:35 am
Location: Tōkyō, Japan

Multiple dispatch

Post by Jaetan » Thu Apr 09, 2009 12:08 pm

Hello,

I would like to know if Logtalk already supports multiple dispatch. I believe I can parametrize the calls to methods with arbitrary terms (for example, something like the identifiers of objects representing the types of the arguments) to select the method to use from the types of the actual arguments, at once, when the method is called. I would like to know if somebody has tried this kind of approach, and what they found to be working.

Thanks in advance for any pointers or suggestions.

Paulo Moura
Logtalk developer
Posts: 475
Joined: Sat May 05, 2007 8:35 am
Location: Portugal
Contact:

Re: Multiple dispatch

Post by Paulo Moura » Thu Apr 09, 2009 12:33 pm

Logtalk only supports single dispatch for methods (based on the message receiver). I'm familiar with the idea of multiple dispatch methods but I have not designed Logtalk with that functionality in mind. Where you would store such methods? In the pseudo-object user? Conceptually, they don't belong to any single object. What would distinguish them from Prolog predicates defined in the pseudo-object user? Which problem are you trying to solve?
Paulo Moura
Logtalk developer

Jaetan
Posts: 13
Joined: Thu Apr 09, 2009 11:35 am
Location: Tōkyō, Japan

Re: Multiple dispatch

Post by Jaetan » Thu Apr 09, 2009 3:20 pm

I agree these multi-methods do not belong to any of the objects which take part in its execution. On the other hand, we can choose to resolve a call like

Code: Select all

object1 doSomethingWith: object2 with: object3
to something like

Code: Select all

Dispatcher callMethod: #doSomethingWith:with:
                 fromTypes: { object1 type . object2 type . object3 type }
                 on: { object1 . object2 . object3 }
and hide the multi-method resolution algorithm inside the Dispatcher object. And the Dispatcher object is stored along with the other objects of the current genealogy (resp. the Dispatcher class is stored with the other classes of the hierarchy). The fact that they take part in the description of the behaviour of a method call for the genealogy/hierarchy, they are more than Prolog predicates in the pseudo-object user, who would be linked a posteriori to the genealogies/hierarchies that use them. Or so do I believe.

Now for my story: I am investigating how to model the sort of behaviour found in role-playing games. The type of equipment available, the effects of using it, and for what purpose, and in which circumstances can give very different results in situations where little changes. This leads to an explosive complexity; all the source code that I have read until now, even when it used an object-oriented paradigm (it was C++), evolved over time from a rather manageable class hierarchy into a very complex and fragile and tangled system of classes, templates and mere functions. I believe part of the problem comes from the burden that using a class system such as that of C++ puts on the designer: much boilerplate, no distinction between structure inheritance and behaviour inheritance... I think a prototype-based object system, armed with multiple dispatch, could do much better and am interested in investigating further this idea.

Hence my question :wink:

Thank you for your response.

Paulo Moura
Logtalk developer
Posts: 475
Joined: Sat May 05, 2007 8:35 am
Location: Portugal
Contact:

Re: Multiple dispatch

Post by Paulo Moura » Thu Apr 09, 2009 8:05 pm

Just a quick note (as I'm quite busy at the moment releasing a new Logtalk version). Working with prototypes instead of classes is often simpler. Logtalk categories may also help you in keep code manageable by providing a more fine-grained units of code reuse.
Paulo Moura
Logtalk developer

Paulo Moura
Logtalk developer
Posts: 475
Joined: Sat May 05, 2007 8:35 am
Location: Portugal
Contact:

Re: Multiple dispatch

Post by Paulo Moura » Sat Apr 11, 2009 4:31 pm

Take a look at the "prototypes" and the "viewpoints" prototype examples in the current Logtalk distribution. The flexibility of prototype relations, combined with easy to define per-predicate property sharing and value sharing behavior, combined with category's fine-grained code reuse can give you a manageable implementation solution for the kind of applications you describe. Parametric object, by allowing easy code parameterization, may also be helpful in your case. See e.g. the "parametric" and "proxies" examples.
Paulo Moura
Logtalk developer

Jaetan
Posts: 13
Joined: Thu Apr 09, 2009 11:35 am
Location: Tōkyō, Japan

Re: Multiple dispatch

Post by Jaetan » Mon Apr 27, 2009 10:04 am

The "viewpoints" and "parametric" examples demonstrate attractive features, that I will indeed need :) . I also gather from the Reference Manual that an object's parameter can be any Prolog term. However, I did not see anything like an object identifier... If I want to parametrize an object with another object, will I need to label the object playing the role of the parameter with, say, an atom ?

Thanks for your help and suggestions.

Paulo Moura
Logtalk developer
Posts: 475
Joined: Sat May 05, 2007 8:35 am
Location: Portugal
Contact:

Re: Multiple dispatch

Post by Paulo Moura » Mon Apr 27, 2009 10:58 am

Jaetan wrote: The "viewpoints" and "parametric" examples demonstrate attractive features, that I will indeed need :) . I also gather from the Reference Manual that an object's parameter can be any Prolog term.
Correct. Object parameters can be any Prolog term.
Jaetan wrote: However, I did not see anything like an object identifier... If I want to parametrize an object with another object, will I need to label the object playing the role of the parameter with, say, an atom ?
Either an atom or a compound term. I.e. nothing prevents an object parameter to be the identifier of another parametric object. You may also share parameters between them.
Jaetan wrote: Thanks for your help and suggestions.
You're welcome.
Paulo Moura
Logtalk developer

Jaetan
Posts: 13
Joined: Thu Apr 09, 2009 11:35 am
Location: Tōkyō, Japan

Re: Multiple dispatch

Post by Jaetan » Mon Apr 27, 2009 3:18 pm

Paulo Moura wrote:Either an atom or a compound term. I.e. nothing prevents an object parameter to be the identifier of another parametric object. You may also share parameters between them.
I see. That is how I can reintroduce the flexibility that I would have got from a multiple dispatch system. And, as a bonus, the dispatch algorithm is... unification ! Wow ! That is beautiful !

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests