[Haskell-cafe] Class instances on class constraints

Dušan Kolář kolar at fit.vutbr.cz
Wed Jul 28 07:17:29 EDT 2010


  Yes! Thanks.

Dušan

On 07/28/2010 01:03 PM, Felipe Lessa wrote:
> 2010/7/28 Dušan Kolář<kolar at fit.vutbr.cz>:
>> which does not work, of course (Flexible or Undecidable instances won't
>> help). The aim is to have addElem function that works differently according
>> to situation whether a type, which is base of the list/set, is a member of
>> class Eq or Ord. Could you point me or hint me how to get as close as
>> possible to the required solution? Maybe I'm not able to see an obvious
>> way...
> Use the good'ol trick:
>
> newtype ByEq a = ByEq {unByEq :: a}
> newtype ByOrd a = ByOrd {unByOrd :: a}
>
> instance (Eq a) =>  SetOL (ByEq a) where
>    addElem = addEq . unByEq
>
> instance (Ord a) =>  SetOL (ByOrd a) where
>    addElem = addOrd . unByOrd
>
> Cheers,
>


More information about the Haskell-Cafe mailing list