Nice multiple dispatch details

View: New views
6 Messages — Rating Filter:   Alert me  

Nice multiple dispatch details

by a400 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

I have a few questions regarding the multiple dispatch mechanism in
Nice and how it compares to other languages (CLOS, Dylan, Cecil
etc.)

When calling a method, Nice, like other multiple dispatch languages,
has to chose the most appropriate method body from a set of
available definitions (based on the dynamic type of the supplied
arguments, of course).

What are the criteria of grouping method definitions into a "dispatch
set":
- the method's name (I assume)
- the package where the method is defined?
- the number of arguments?
- static types of arguments?

In other words....

* Example1:
        package Car;
        foo(Car a1, Motorcycle a2)
        package Motorcycle;
        foo(Motorcycle a1, Truck a2)
...will these methods be the same dispatch set?

* Example2:
        foo(Car a1, Motorcycle a2)
        foo(Car a1, Motorcycle a2, Truck a3)
...will these methods be in the same dispatch set?

* Example3:
        abstract class Vehicle;
        class Car extends Vehicle;
        abstract class Person;
        class Driver extends Person;

        toString(Car a1) = (...);
        toString(Driver a1) = (...);

        void main(String[] args) {
                let d = new Driver();
                toString(d);
        }
... will the 2 methods be in the same dispatch set? I.e. when calling
toString() with a Driver as an argument, will both method
definitions be considered at runtime? Or is the compiler able to
disambiguate these two, eliminating the need for dynamic dispatch
(in this particular case)?

...Thanks a lot!
Radu.

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Nice-info mailing list
Nice-info@...
https://lists.sourceforge.net/lists/listinfo/nice-info

Re: Nice multiple dispatch details

by Daniel Bonniot-5 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,


On Feb 7, 2008 8:09 AM,  <a400@...> wrote:

> Hi,
>
> I have a few questions regarding the multiple dispatch mechanism in
> Nice and how it compares to other languages (CLOS, Dylan, Cecil
> etc.)
>
> When calling a method, Nice, like other multiple dispatch languages,
> has to chose the most appropriate method body from a set of
> available definitions (based on the dynamic type of the supplied
> arguments, of course).
>
> What are the criteria of grouping method definitions into a "dispatch
> set":
> - the method's name (I assume)
yes

> - the package where the method is defined?
they don't have to be in the same package, as long as all packages are
visible by being imported

> - the number of arguments?
yes

> - static types of arguments?
yes. Precisely, a method will override all other methods when both
would be applicable for some same set of arguments, and the overriding
methods has more specific types than the overriden one.

>
> In other words....
>
> * Example1:
>         package Car;
>         foo(Car a1, Motorcycle a2)
>         package Motorcycle;
>         foo(Motorcycle a1, Truck a2)
> ...will these methods be the same dispatch set?

No, given I assume something cannot be both a motorcycle and a truck.

But if there was also a foo(Vehicule, Vehicule), then that one would
be overridden by both of yours.

> * Example2:
>         foo(Car a1, Motorcycle a2)
>         foo(Car a1, Motorcycle a2, Truck a3)
> ...will these methods be in the same dispatch set?

No.

> * Example3:
>         abstract class Vehicle;
>         class Car extends Vehicle;
>         abstract class Person;
>         class Driver extends Person;
>
>         toString(Car a1) = (...);
>         toString(Driver a1) = (...);
>
>         void main(String[] args) {
>                 let d = new Driver();
>                 toString(d);
>         }
> ... will the 2 methods be in the same dispatch set?
Yes.

> I.e. when calling
> toString() with a Driver as an argument, will both method
> definitions be considered at runtime? Or is the compiler able to
> disambiguate these two, eliminating the need for dynamic dispatch
> (in this particular case)?

I believe the compiler will avoid the dynamic dispatch in this
particular case, but that's an implementation detail (an
optimization).

You are welcome to write such programs yourself to check the behaviour
of the compiler.

Cheers,

Daniel

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Nice-info mailing list
Nice-info@...
https://lists.sourceforge.net/lists/listinfo/nice-info

Re: Nice multiple dispatch details

by a400 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Thanks Daniel for the clarification,

after playing a little with the nice compiler I realized that the
programmer is actually in control of which method bodies belong to a
method group. In this example:

        class Vehicle;
        class Car extends Vehicle;
        class Driver;

        String toString(Vehicle a1) = (...);
        String toString(Car a1) = (...);

the two toString() methods are not in the same method group (even
though they have the same name and number of arguments, and the
argument types are relate by inheritance) because they have
different declarations.

In contrast:
        String toString(Vehicle a1) = (...);
        toString(Car a1) = (...);

are in the same method group.

Conversely,
        String toString(Vehicle a1) = (...);
        toString(Car a1) = (...);
        toString(Driver a1) = (...);

are not all 3 in the same method group (only the first 2).

cheers,
Radu


On Wed, 13 Feb 2008, Daniel Bonniot wrote:

> Hi,
>
> On Feb 7, 2008 8:09 AM,  <a400@...> wrote:
> > Hi,
> >
> > I have a few questions regarding the multiple dispatch mechanism
> > in Nice and how it compares to other languages (CLOS, Dylan,
> > Cecil etc.)
> >
> > When calling a method, Nice, like other multiple dispatch
> > languages, has to chose the most appropriate method body from a
> > set of available definitions (based on the dynamic type of the
> > supplied arguments, of course).
> >
> > What are the criteria of grouping method definitions into a
> > "dispatch set":
> > - the method's name (I assume)
>
> yes
>
> > - the package where the method is defined?
>
> they don't have to be in the same package, as long as all packages
> are visible by being imported
>
> > - the number of arguments?
>
> yes
>
> > - static types of arguments?
>
> yes. Precisely, a method will override all other methods when both
> would be applicable for some same set of arguments, and the
> overriding methods has more specific types than the overriden one.
>
> > In other words....
> >
> > * Example1:
> >         package Car;
> >         foo(Car a1, Motorcycle a2)
> >         package Motorcycle;
> >         foo(Motorcycle a1, Truck a2)
> > ...will these methods be the same dispatch set?
>
> No, given I assume something cannot be both a motorcycle and a
> truck.
>
> But if there was also a foo(Vehicule, Vehicule), then that one
> would be overridden by both of yours.
>
> > * Example2:
> >         foo(Car a1, Motorcycle a2)
> >         foo(Car a1, Motorcycle a2, Truck a3)
> > ...will these methods be in the same dispatch set?
>
> No.
>
> > * Example3:
> >         abstract class Vehicle;
> >         class Car extends Vehicle;
> >         abstract class Person;
> >         class Driver extends Person;
> >
> >         toString(Car a1) = (...);
> >         toString(Driver a1) = (...);
> >
> >         void main(String[] args) {
> >                 let d = new Driver();
> >                 toString(d);
> >         }
> > ... will the 2 methods be in the same dispatch set?
>
> Yes.
>
> > I.e. when calling
> > toString() with a Driver as an argument, will both method
> > definitions be considered at runtime? Or is the compiler able to
> > disambiguate these two, eliminating the need for dynamic
> > dispatch (in this particular case)?
>
> I believe the compiler will avoid the dynamic dispatch in this
> particular case, but that's an implementation detail (an
> optimization).
>
> You are welcome to write such programs yourself to check the
> behaviour of the compiler.
>
> Cheers,
>
> Daniel
>
> ------------------------------------------------------------------
>------- This SF.net email is sponsored by: Microsoft
> Defy all challenges. Microsoft(R) Visual Studio 2008.
> http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
> _______________________________________________
> Nice-info mailing list
> Nice-info@...
> https://lists.sourceforge.net/lists/listinfo/nice-info

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Nice-info mailing list
Nice-info@...
https://lists.sourceforge.net/lists/listinfo/nice-info

Re: Nice multiple dispatch details

by a400 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

There's one thing that I am a bit confused about.

The code:
        String toString(Vehicle a) = "a vehicle";
        toString(Car a) = "a car";

gives me this warning:

~/ca/corpus/nice/test1/main.nice: line 23, column 8:
warning: This method overrides <T> java.lang.String toString(!T)
You should make this explicit, either by omitting the return type
or by using the 'override' keyword.

What does 'overrides' mean here? If a method overrides another, does
it imply that it is part of the same method (dispatch) group?
So, in this case, is toString(test1.Vehicle a) part of toString(!T)?
Is toString(Car a) part of toString(!T)?

Additionally, the compiler seems to create a new method group
toString(test1.Vehicle a) with the two alternatives:
        toString(test1.Car a)
        toString(test1.Vehicle a)

>From this, it appears that a method body can be in multiple method
groups. Is this correct?

So for this call site:
        toString(new Vehicle());

the method group would be:
        toString(test1.Car a)
        toString(test1.Vehicle a)


while for this call site:
        toString(new java.lang.Object());

the method group would be:
        toString(!T)
        toString(test1.Car a)
        toString(test1.Vehicle a)

Is this correct?

Thanks :)
Radu


On Mon, 18 Feb 2008, a400@... wrote:

> Thanks Daniel for the clarification,
>
> after playing a little with the nice compiler I realized that the
> programmer is actually in control of which method bodies belong to
> a method group. In this example:
>
> class Vehicle;
> class Car extends Vehicle;
> class Driver;
>
> String toString(Vehicle a1) = (...);
> String toString(Car a1) = (...);
>
> the two toString() methods are not in the same method group (even
> though they have the same name and number of arguments, and the
> argument types are relate by inheritance) because they have
> different declarations.
>
> In contrast:
> String toString(Vehicle a1) = (...);
> toString(Car a1) = (...);
>
> are in the same method group.
>
> Conversely,
> String toString(Vehicle a1) = (...);
> toString(Car a1) = (...);
> toString(Driver a1) = (...);
>
> are not all 3 in the same method group (only the first 2).
>
> cheers,
> Radu
>
> On Wed, 13 Feb 2008, Daniel Bonniot wrote:
> > Hi,
> >
> > On Feb 7, 2008 8:09 AM,  <a400@...> wrote:
> > > Hi,
> > >
> > > I have a few questions regarding the multiple dispatch
> > > mechanism in Nice and how it compares to other languages
> > > (CLOS, Dylan, Cecil etc.)
> > >
> > > When calling a method, Nice, like other multiple dispatch
> > > languages, has to chose the most appropriate method body from
> > > a set of available definitions (based on the dynamic type of
> > > the supplied arguments, of course).
> > >
> > > What are the criteria of grouping method definitions into a
> > > "dispatch set":
> > > - the method's name (I assume)
> >
> > yes
> >
> > > - the package where the method is defined?
> >
> > they don't have to be in the same package, as long as all
> > packages are visible by being imported
> >
> > > - the number of arguments?
> >
> > yes
> >
> > > - static types of arguments?
> >
> > yes. Precisely, a method will override all other methods when
> > both would be applicable for some same set of arguments, and the
> > overriding methods has more specific types than the overriden
> > one.
> >
> > > In other words....
> > >
> > > * Example1:
> > >         package Car;
> > >         foo(Car a1, Motorcycle a2)
> > >         package Motorcycle;
> > >         foo(Motorcycle a1, Truck a2)
> > > ...will these methods be the same dispatch set?
> >
> > No, given I assume something cannot be both a motorcycle and a
> > truck.
> >
> > But if there was also a foo(Vehicule, Vehicule), then that one
> > would be overridden by both of yours.
> >
> > > * Example2:
> > >         foo(Car a1, Motorcycle a2)
> > >         foo(Car a1, Motorcycle a2, Truck a3)
> > > ...will these methods be in the same dispatch set?
> >
> > No.
> >
> > > * Example3:
> > >         abstract class Vehicle;
> > >         class Car extends Vehicle;
> > >         abstract class Person;
> > >         class Driver extends Person;
> > >
> > >         toString(Car a1) = (...);
> > >         toString(Driver a1) = (...);
> > >
> > >         void main(String[] args) {
> > >                 let d = new Driver();
> > >                 toString(d);
> > >         }
> > > ... will the 2 methods be in the same dispatch set?
> >
> > Yes.
> >
> > > I.e. when calling
> > > toString() with a Driver as an argument, will both method
> > > definitions be considered at runtime? Or is the compiler able
> > > to disambiguate these two, eliminating the need for dynamic
> > > dispatch (in this particular case)?
> >
> > I believe the compiler will avoid the dynamic dispatch in this
> > particular case, but that's an implementation detail (an
> > optimization).
> >
> > You are welcome to write such programs yourself to check the
> > behaviour of the compiler.
> >
> > Cheers,
> >
> > Daniel
> >
> > ----------------------------------------------------------------
> >-- ------- This SF.net email is sponsored by: Microsoft
> > Defy all challenges. Microsoft(R) Visual Studio 2008.
> > http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
> > _______________________________________________
> > Nice-info mailing list
> > Nice-info@...
> > https://lists.sourceforge.net/lists/listinfo/nice-info
>
> ------------------------------------------------------------------
>------- This SF.net email is sponsored by: Microsoft
> Defy all challenges. Microsoft(R) Visual Studio 2008.
> http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
> _______________________________________________
> Nice-info mailing list
> Nice-info@...
> https://lists.sourceforge.net/lists/listinfo/nice-info

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Nice-info mailing list
Nice-info@...
https://lists.sourceforge.net/lists/listinfo/nice-info

Re: Nice multiple dispatch details

by Daniel Bonniot-5 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

For clarification: there are two concepts:

1) method declaration, for instance:
<T> String toString(!T);
and
String toString(Vehicle);

It has a return type, possible a contract, no code.

2) method implementation (or "method body" or "alternative"), for instance
toString(Car a) = "...";

It has no return type nor contract, but some code.

A method implementation belongs to one (or several, see below) method
declaration, for which it represents the code to execute in a certain
context.

A method declaration m2 can override another method declaration m1.
This happens when m2 is more specific than m1. It can in particular
have a more specific return type. In this case, all implementations of
m2 will also be implementations of m1.



On Feb 19, 2008 11:29 PM,  <a400@...> wrote:

> There's one thing that I am a bit confused about.
>
> The code:
>         String toString(Vehicle a) = "a vehicle";
>         toString(Car a) = "a car";
>
> gives me this warning:
>
> ~/ca/corpus/nice/test1/main.nice: line 23, column 8:
> warning: This method overrides <T> java.lang.String toString(!T)
> You should make this explicit, either by omitting the return type
> or by using the 'override' keyword.
>
> What does 'overrides' mean here? If a method overrides another, does
> it imply that it is part of the same method (dispatch) group?
> So, in this case, is toString(test1.Vehicle a) part of toString(!T)?
> Is toString(Car a) part of toString(!T)?
>
> Additionally, the compiler seems to create a new method group
> toString(test1.Vehicle a) with the two alternatives:
>         toString(test1.Car a)
>         toString(test1.Vehicle a)
>
> >From this, it appears that a method body can be in multiple method
> groups. Is this correct?
>
> So for this call site:
>         toString(new Vehicle());
>
> the method group would be:
>         toString(test1.Car a)
>         toString(test1.Vehicle a)
>
>
> while for this call site:
>         toString(new java.lang.Object());
>
> the method group would be:
>         toString(!T)
>         toString(test1.Car a)
>         toString(test1.Vehicle a)
>
> Is this correct?

Yes, this is correct :-)

Daniel

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Nice-info mailing list
Nice-info@...
https://lists.sourceforge.net/lists/listinfo/nice-info

Re: Nice multiple dispatch details

by a400 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Thanks Daniel, that makes it a lot clearer!

Now, in terms of dynamic (multiple or single) dispatch:
The concept is that at compile time there is a known set of
alternative method bodies for each call site. At runtime, the most
appropriate body is chosen from this set, depending on the dynamic
types of the arguments involved in the call. In some languages, this
set is called a generic function. In Nice, it is a method
declaration that has a set of alternative method
implementations/bodies.

In some languages (at least CLOS, Dylan, Cecil), a method body
belongs to one generic function. In Nice it can belong to one or
multiple declarations.

I hope I got that right, correct me if I'm wrong please ;-)

cheers,
Radu


On Wed, 20 Feb 2008, Daniel Bonniot wrote:

> For clarification: there are two concepts:
>
> 1) method declaration, for instance:
> <T> String toString(!T);
> and
> String toString(Vehicle);
>
> It has a return type, possible a contract, no code.
>
> 2) method implementation (or "method body" or "alternative"), for
> instance toString(Car a) = "...";
>
> It has no return type nor contract, but some code.
>
> A method implementation belongs to one (or several, see below)
> method declaration, for which it represents the code to execute in
> a certain context.
>
> A method declaration m2 can override another method declaration
> m1. This happens when m2 is more specific than m1. It can in
> particular have a more specific return type. In this case, all
> implementations of m2 will also be implementations of m1.
>
> On Feb 19, 2008 11:29 PM,  <a400@...> wrote:
> > There's one thing that I am a bit confused about.
> >
> > The code:
> >         String toString(Vehicle a) = "a vehicle";
> >         toString(Car a) = "a car";
> >
> > gives me this warning:
> >
> > ~/ca/corpus/nice/test1/main.nice: line 23, column 8:
> > warning: This method overrides <T> java.lang.String toString(!T)
> > You should make this explicit, either by omitting the return
> > type or by using the 'override' keyword.
> >
> > What does 'overrides' mean here? If a method overrides another,
> > does it imply that it is part of the same method (dispatch)
> > group? So, in this case, is toString(test1.Vehicle a) part of
> > toString(!T)? Is toString(Car a) part of toString(!T)?
> >
> > Additionally, the compiler seems to create a new method group
> > toString(test1.Vehicle a) with the two alternatives:
> >         toString(test1.Car a)
> >         toString(test1.Vehicle a)
> >
> > >From this, it appears that a method body can be in multiple
> > > method
> >
> > groups. Is this correct?
> >
> > So for this call site:
> >         toString(new Vehicle());
> >
> > the method group would be:
> >         toString(test1.Car a)
> >         toString(test1.Vehicle a)
> >
> >
> > while for this call site:
> >         toString(new java.lang.Object());
> >
> > the method group would be:
> >         toString(!T)
> >         toString(test1.Car a)
> >         toString(test1.Vehicle a)
> >
> > Is this correct?
>
> Yes, this is correct :-)
>
> Daniel
>
> ------------------------------------------------------------------
>------- This SF.net email is sponsored by: Microsoft
> Defy all challenges. Microsoft(R) Visual Studio 2008.
> http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
> _______________________________________________
> Nice-info mailing list
> Nice-info@...
> https://lists.sourceforge.net/lists/listinfo/nice-info

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Nice-info mailing list
Nice-info@...
https://lists.sourceforge.net/lists/listinfo/nice-info
LightInTheBox - Buy quality products at wholesale price