Can I distinguish between typeclass instances at runtime?

Is it possible to implement a type class as follows:

class SomeClass e where isEq :: (SomeClass e') => e -> e' -> Bool 

where isEq xy will return true when x and y are the same instances of this type class?

Context. This is a finely veiled attempt to get runtime type tests. Although I first read that Haskell has type erasure, I also read that with recent extensions for GHC there is some runtime information.

edit: for those who are wondering about my usecase ... I used level-level programming to provide certain properties of some of my ADTs, namely resource utilization. These resources are represented by various types (and resource locks are respectively implemented at the type level).

I am trying to write an optimization procedure requiring identification of write / read resources. But since all my resources are represented by separate singletones, the only common factor (except for all values ​​that are the bottom) is the type that groups them together.

In a nutshell, I want to use the results of my programming at the level of the runtime level value.

+6
source share
1 answer

You can do this, as Karolis Juodelė hinted, with Data.Typeable :

 Prelude Data.Typeable> :{ Prelude Data.Typeable| let isEq :: (Typeable a , Typeable b) => a -> b -> Bool Prelude Data.Typeable| isEq xy = typeOf x == typeOf y Prelude Data.Typeable| :} Prelude Data.Typeable> isEq True () False Prelude Data.Typeable> isEq True False True 

Question: why don’t you know at work what types these are, and then why you care about whether they are equal - can you develop your own precedent?

+15
source

All Articles