[Haskell-cafe] Double-dispatch

Clark Gaebel cgaebel at csclub.uwaterloo.ca
Tue Mar 6 06:40:31 CET 2012


Wow, that's a lot of freedom in the type system. Haskell never fails
to amaze me how it can remove features and increase expressiveness in
one fell sweep.

I also like how the user will get type errors if attempting
intersection between two geometries which do not have intersection
defined. It makes the API really intuitive.

In terms of the extra features, in my case (geometric intersection
tests), MultiParamTypeClasses seem to be the perfect fit. However,
thanks for giving me a much more comprehensive arsenal of type system
hacks!

Regards,
  - clark

On Tue, Mar 6, 2012 at 12:28 AM, wren ng thornton <wren at freegeek.org> wrote:
> On 3/5/12 4:24 PM, Clark Gaebel wrote:
>>
>> Well look at that.
>>
>> Thanks!
>>
>> On Mon, Mar 5, 2012 at 4:07 PM, Felipe Almeida Lessa
>> <felipe.lessa at gmail.com>  wrote:
>>>
>>> {-# LANGUAGE MultiParamTypeClasses #-}
>>>
>>> class Intersectable a b where
>>>  intersectsWith :: a ->  b ->  Bool
>
>
> Assuming that intersectsWith is something like unification, equality, or
> similar operations:
>
> Do note that this can lead to needing quadratically many instances, about
> half of which will be redundant if intersectsWith is supposed to be
> symmetric.
>
> Often times we know that the vast majority of these quadratically many
> instances should be vacuous (i.e., always return False), and it'd be nice to
> avoid writing them out. This can be achieved via -XOverlappingInstances
> where you give a default instance:
>
>    instance Intersectable a b where intersectsWith _ _ = False
>
> and then override it for more specific choices of a and b. Beware that if
> you want to have other polymorphic instances you may be forced to use
> -XIncoherentInstances, or else resolve the incoherence by filling out the
> lattice of instances.
>
> The other notable complication is if you want your collection of types to
> have more than just a set structure (e.g., if you want some kind of
> subtyping hierarchy). It's doable, but things get complicated quickly.
>
>
> Other than those caveats, have at it! The ability to do this sort of thing
> is part of what makes Haskell great. Few languages have multiple-dispatch
> this powerful.
>
> --
> Live well,
> ~wren
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>



More information about the Haskell-Cafe mailing list