
Hello all, For school purposes, I would like to define something like this: module SetOverList( SetFromList , SetOL(..) ) where type SetFromList a = [a] addEq :: Eq a => a -> SetFromList a -> SetFromList a addEq v [] = [v] addEq v l@(x:xs) = if x==v then l else x : addEq v xs addOrd :: Ord a => a -> SetFromList a -> SetFromList a addOrd v [] = [v] addOrd v l@(x:xs) = case compare x v of GT -> v : l EQ -> l LT -> x : addOrd v xs class SetOL a where addElem :: a -> SetFromList a -> SetFromList a instance (Eq a) => SetOL a where addElem = addEq instance (Ord a) => SetOL a where addElem = addOrd 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... Thanks, Dušan

2010/7/28 Dušan Kolář
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.

Yes! Thanks. Dušan On 07/28/2010 01:03 PM, Felipe Lessa wrote:
2010/7/28 Dušan Kolář
: 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,
participants (2)
-
Dušan Kolář
-
Felipe Lessa