In Haskell Data.Set
implements a concrete set type. Normal []
lists also implement all operations of a set (in Data.List
). But there seems to be no predefined Set
typeclass that they both implement. You can implement one yourself:
{-# LANGUAGE MultiParamTypeClasses, FlexibleInstances, FunctionalDependencies #-}
module Set where
import qualified Data.List as ConcreteList
import qualified Data.Set as ConcreteSet
class Set set a | set -> a where
empty :: set
singleton :: a -> set
insert :: a -> set -> set
delete :: a -> set -> set
union :: set -> set -> set
intersection :: set -> set -> set
member :: a -> set -> Bool
filter :: (a -> Bool) -> set -> set
instance (Ord a) => Set (ConcreteSet.Set a) a where
empty = ConcreteSet.empty
singleton = ConcreteSet.singleton
insert = ConcreteSet.insert
delete = ConcreteSet.delete
union = ConcreteSet.union
intersection = ConcreteSet.intersection
member = ConcreteSet.member
filter = ConcreteSet.filter
instance (Eq a) => Set [a] a where
empty = []
singleton e = [e]
insert = (:)
delete = ConcreteList.delete
union = ConcreteList.union
intersection = ConcreteList.intersect
member = ConcreteList.elem
filter = ConcreteList.filter
but it seems that this would already have been done if this was the way to go. So my question is: Where is the Set
typeclass or what alternative solution has the Haskell community come up with?
My question is admittedly quite similar to Why is Haskell missing “obvious” Typeclasses, but by being more concrete (focusing on a specific example with sample implementation), I'm hoping to get some more concrete answers as well.