On Thu, Jul 17, 2014 at 11:56 PM, Nils Bruin <nbr...@sfu.ca> wrote:
> On Thursday, July 17, 2014 10:35:32 PM UTC-7, Robert Bradshaw wrote:
>>
>> I'm not sure multimethods alone are enough to solve issues with Sage's
>> type system (e.g. coercion, where the result of a+b may happen in a
>> completely new domain) but they could certainly help.
>
>
> Indeed. If you want multiple dispatch in python you can have it:
>
>  class multi_dispatch(object):
>     def __init__(self):
>         self.dispatch = []
>     def register_function(self,signature,function):
>         self.dispatch.append( (signature,function) )
>     def register(self,*signature):
>         def register_decorator(function):
>             self.register_function(signature,function)
>             return function
>         return register_decorator
>     def __call__(self,*args):
>         for sig in self.dispatch:
>             if len(args) == len(sig) and all(isinstance(a,b) for a,b in
> zip(args,sig[0])):
>                 return sig[1](*args)
>         raise TypeError("Signature not implemented")
>
>
> With this it is now quite straightforward to write multiple dispatch at the
> cost of using a decorator:
> T = multi_dispatch()
>
> @T.register(int,int)
> def int_int(a,b):
>     return "called with integers (%d,%d)"%(a,b)
>
> @T.register(int,float)
> def int_float(a,b):
>     return "called with integer and float (%d,%f)"%(a,b)
>
> @T.register(float,float)
> def float_float(a,b):
>     return "called with floats (%f,%f)"%(a,b)
>
> You can then just call T:
>
>>>> T(1,2)
> 'called with integers (1,2)'
>>>> T(1,2.0)
> 'called with integer and float (1,2.000000)'
>>>> T(1.0,2.0)
> 'called with floats (1.000000,2.000000)'
>>>> T(1.0,2)
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
>   File "dispatch.py", line 15, in __call__
>     raise TypeError("Signature not implemented")
> TypeError: Signature not implemented
>
> The key is in optimizing the signature lookup (and the kind of lookup can
> definitely be refined a *lot* in the above implementation--linear search by
> registration order is probably not optimal) and finding a useful way of
> describing signatures and properly prioritizing which signature should get
> precedence (if you think C3 is complicated for multiple inheritance, brace
> yourself for multiple dispatch).

Yes, but here T must explicitly implement to multiple dispatch. One
can't take an existing object and "add a method" based on the type of
the second argument.

We also have @coerce_binop
https://github.com/sagemath/sage/blob/6.2/src/sage/structure/element.pyx#L3260
which is nice. Here we're conflating the issue of types and Parents
(where the latter can be viewed, somewhat, as dynamically created
parameterized types), and it's often the latter that affects the
choice of implementation (or even semantic meaning) of a method.

- Robert

-- 
You received this message because you are subscribed to the Google Groups 
"sage-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sage-devel+unsubscr...@googlegroups.com.
To post to this group, send email to sage-devel@googlegroups.com.
Visit this group at http://groups.google.com/group/sage-devel.
For more options, visit https://groups.google.com/d/optout.

Reply via email to