Is there a way to programmatically get a list of instances of a type class?
It strikes me that the compiler must know this information in order to type check and compile the code, so is there some way to tell the compiler: hey, you know those instances of that class, please put a list of them right here (as strings or whatever some representation of them).
You can generate the instances in scope for a given type class using Template Haskell.
Running this in GHCi:
Another useful technique is showing all instances in scope for a given type class using GHCi.
Edit: The important thing to know is that the compiler is only aware of type classes in scope in any given module (or at the ghci prompt, etc.). So if you call the
showInstancesTH function with no imports, you’ll only get instances from the Prelude. If you have other modules in scope, e.g. Data.Word, then you’ll see all those instances too.