[Haskell-cafe] Template haskell instance checking

heatsink kyz7tnt02 at sneakemail.com
Wed Mar 1 19:55:04 EST 2006

Is there a way, in template haskell, to find out what instance declarations 
exist?  I'm not trying to accomplish anything specific right now, but I can 
see two potential uses for this.  One is to emit compile-time error messages 
from TH code which would be more informative than the typechecker's output.  
The other is to produce TH functions that conditionally generate code 
depending on the instances satisfied by their arguments.

I offer the following as a motivational example.  Suppose you want to write 
an extensible library to handle files for, e.g., image data:

class FileFormat a where
formatName :: a -> String
read :: Handle -> IO a
write :: Handle -> a -> IO ()

instance FileFormat TheStandardFormat where ....

class (FileFormat a, FileFormat b) => Convert a b where
convert :: a -> b

Now it would be nice to generate instances of (Convert a TheStandardFormat, 
Convert TheStandardFormat b) => Convert a b _except_ where there is an 
explicit definition for (Convert a b).  AFAIK, there's no way to do this at 
present, but it seems like something that could be computed in TH.


More information about the Haskell-Cafe mailing list