
Is there any way in Haskell to have the correct function selected based on the types of two different types? For example, let's say I'm writing intersection tests: aABBandAABB :: AABB -> AABB -> Bool oBBandOBB :: OBB -> OBB -> Bool oBBandPoint :: OBB -> Point -> Bool Is there some way (such as with Type Families) that I can write some sort of generic method for this: intersects :: Intersectable -> Intersectable -> Bool which automatically selects the right function to call based on the types of the two intersectables? Regards, - clark

{-# LANGUAGE MultiParamTypeClasses #-} class Intersectable a b where intersectsWith :: a -> b -> Bool -- Felipe.

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
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

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
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
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@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
participants (3)
-
Clark Gaebel
-
Felipe Almeida Lessa
-
wren ng thornton