Hello,<br><br>> Is it possible in Haskell + GHC extensions to use reflection<br>> techniques to determine typeclass membership? I'm thinking of things<br>> like the following:<br>> <br>I think the short answer is not in general; i.e. I don't think there is any way to access the members of an arbitrary typeclass (but I'd love to be proved wrong).<br>
<br>However, you could always explicitly list the members of a typeclass you are interested in (this is similar to your Idea 2):<br><br><div style="margin-left: 40px;">{-# OPTIONS_GHC -fglasgow-exts -fallow-undecidable-instances -fallow-overlapping-instances #-}<br>
<br>class InEq a b | a -> b where inEq :: a -> Bool<br>instance TypeCast b HFalse => InEq a b where inEq _ = False<br>instance InEq Int HTrue where inEq _ = True<br>instance InEq a b => InEq [a] b where inEq _ = inEq (undefined :: a)<br>
<br>data HTrue<br>data HFalse<br><br>class TypeCast a b | a -> b, b-> a where typeCast :: a -> b<br>class TypeCast' t a b | t a -> b, t b -> a where typeCast' :: t -> a -> b<br>class TypeCast'' t a b | t a -> b, t b -> a where typeCast'' :: t -> a -> b<br>
instance TypeCast' () a b => TypeCast a b where typeCast x = typeCast' () x<br>instance TypeCast'' t a b => TypeCast' t a b where typeCast' = typeCast''<br>instance TypeCast'' () a a where typeCast'' _ x = x<br>
</div><br>You can also do an arguably nicer, more flexible version of the previous by recreating some of the typeclass machinery yourself; Oleg has several examples of this such as: //okmij.org/ftp/Haskell/poly2.txt<br><br>
-Jeff<br><br>