I want to define a function, <-? to check whether an element is in a list/set/map.
module Test where
import qualified Data.Map as Map
import qualified Data.Set as Set
class Memberable a where
(<-?) :: b -> a -> Bool
instance Memberable [x] where
(<-?) = elem
instance Memberable (Map.Map k v) where
(<-?) = Map.member
instance Memberable (Set.Set x) where
(<-?) = Set.member
The type variable b in the class declaration should be the type of element I want to check. However, this doesn't work for Haskell.
Test.hs:8:13:
Couldn't match type 'b' with 'x'
'b' is a rigid type variable bound by
the type signature for (<-?) :: b -> [x] -> Bool at Test.hs:8:5
'x' is a rigid type variable bound by
the instance declaration at Test.hs:7:10
Expected type: b -> [x] -> Bool
Actual type: b -> [b] -> Bool
Relevant bindings include
(<-?) :: b -> [x] -> Bool (bound at Test.hs:8:5)
In the expression: elem
In an equation for '<-?': (<-?) = elem
How can I use b in the class declaration, but still make the types coincider?