[Haskell-cafe] Class instances on class constraints

Felipe Lessa felipe.lessa at gmail.com
Wed Jul 28 07:03:50 EDT 2010


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,

-- 
Felipe.


More information about the Haskell-Cafe mailing list