To be more specific, suppose I have some data constructor
data Foo = ... deriving Eq
And the following silly function
f :: Eq a => a -> Bool
In cases where the variable a is actually of type Foo, I want f to output True. In all other cases (i.e. for all other instances of Eq), I want f to output False.
At first I thought maybe I could define a new type class for this purpose
class IsFoo a where
isFoo :: a -> Bool
While it's easy to write an instance of IsFoo for Foo, obviously I don't want to do this for all types that are instances of Eq.
When answering, you can assume that Foo has A LOT of constructors and that I don't want to pattern match on all of them. I also don't want to use Data.Typeable (I've read that it's bad style). Is there a way to accomplish what I want in an elegant and natural (w.r.t. Haskell) way?
Data.Typeablewould do that. I concur on the fact thatData.Typeableis bad style, but this is because it allows to break parametricity, which is exactly what you require. Finding another way to do that withoutData.Typeablewould not make things better. - chi