[Haskell-cafe] Type constraints for class instances
Luke Palmer
lrpalmer at gmail.com
Fri Mar 21 01:47:48 EDT 2008
First I'd like to say that this is a very clever idea. Thanks for the
exposition. :-)
2008/3/21 Krzysztof Skrzętnicki <gtener at gmail.com>:
> I'd like to write the following code:
>
> instance (Ord a) => YOrd a where
> ycmp x y = case x `compare` y of
> LT -> (x,y)
> GT -> (y,x)
> EQ -> (x,y)
>
>
> But i get an error "Undecidable instances" for any type [a].
> Does anyone know the way to solve this?
This type of superclassing is not supported by the typeclass system.
Finding systems where it
is supported is a point of much lost sleep on my part.
The typical way to solve this is rather disappointing: wrap it in a newtype:
newtype YOrdWrap a = YOrdWrap { yordUnwrap :: a }
instance (Ord a) => YOrd (YOrdWrap a) where
ycmp (YOrdWrap x) (YOrdWrap y) = ycmpWrap x y
And then apply the constructor whenever you want to use a "normal" type.
Luke
More information about the Haskell-Cafe
mailing list