Is it possible to print out types of instances in scope

Hello, Is it possible to write a function that would display all the instances of a class currently in scope[1]? For example, for 'Show a' it should output something like: The instances of 'Show a' currently in scope are: Show Int Show Float Show Char Show ... Depending on what modules are imported at compile time, the list would grow or shrink. I am willing to accept all sorts of restrictions at this point in time such as: ~ Yes, but only if the classes are declared in this way (but not for ones declared in the standard library) ~ Yes, but it requires template haskell It is currently my belief that the compiler "knows" the answer, but there is no way to get the list from the compiler... I would be interested in any information, links, examples, papers, etc, that you can provide that would tell me: ~ Yes it can be done ~ It could be done with a compiler extension ~ It could never be done ~ It could be done, but it would be a really horrible idea, and here's why So don't hold back :) Thanks! Jeremy Shaw. [1] I suppose by 'currently in scope' I mean all the instances that type checker is considering when it type checks the function at compile time. But I am really not picky about what I mean right now as I can't get anything remotely ressembling this to happen.

On Sun, 16 Jan 2005 00:13:08 -0800, Jeremy Shaw
Hello,
Is it possible to write a function that would display all the instances of a class currently in scope[1]?
For example, for 'Show a' it should output something like:
The instances of 'Show a' currently in scope are: Show Int Show Float Show Char Show ...
In GHC 6.3: Prelude> :i Show class Show a where showsPrec :: Int -> a -> String -> String show :: a -> String showList :: [a] -> String -> String -- Imported from `GHC.Show' instance Show IOMode -- Imported from `GHC.IOBase' instance Show IOException -- Imported from `GHC.IOBase' instance Show IOErrorType -- Imported from `GHC.IOBase' instance Show HandleType -- Imported from `GHC.IOBase' [...] -- Friendly, Lemmih

At Sun, 16 Jan 2005 12:09:43 +0100, Lemmih wrote:
On Sun, 16 Jan 2005 00:13:08 -0800, Jeremy Shaw
wrote: Hello,
Is it possible to write a function that would display all the instances of a class currently in scope[1]?
For example, for 'Show a' it should output something like:
The instances of 'Show a' currently in scope are: Show Int Show Float Show Char Show ...
In GHC 6.3: Prelude> :i Show class Show a where showsPrec :: Int -> a -> String -> String show :: a -> String showList :: [a] -> String -> String -- Imported from `GHC.Show' instance Show IOMode -- Imported from `GHC.IOBase' instance Show IOException -- Imported from `GHC.IOBase' instance Show IOErrorType -- Imported from `GHC.IOBase' instance Show HandleType -- Imported from `GHC.IOBase' [...]
Neat! Is there some way I can get access to that information from within my program ? Maybe something like: import Language.Haskell.TH.Syntax instancesOf :: Name -> [Type] thanks! Jeremy Shaw.

Jeremy Shaw wrote:
Neat! Is there some way I can get access to that information from within my program ? Maybe something like:
import Language.Haskell.TH.Syntax instancesOf :: Name -> [Type]
You should be able to use the FFI to import the function from the RTS just like the run time loader library uses ghci's load and link functions. Keean.

On Sun, 16 Jan 2005 10:07:14 -0800, Jeremy Shaw
At Sun, 16 Jan 2005 12:09:43 +0100, Lemmih wrote:
On Sun, 16 Jan 2005 00:13:08 -0800, Jeremy Shaw
wrote: Hello,
Is it possible to write a function that would display all the instances of a class currently in scope[1]?
For example, for 'Show a' it should output something like:
The instances of 'Show a' currently in scope are: Show Int Show Float Show Char Show ...
In GHC 6.3: Prelude> :i Show class Show a where showsPrec :: Int -> a -> String -> String show :: a -> String showList :: [a] -> String -> String -- Imported from `GHC.Show' instance Show IOMode -- Imported from `GHC.IOBase' instance Show IOException -- Imported from `GHC.IOBase' instance Show IOErrorType -- Imported from `GHC.IOBase' instance Show HandleType -- Imported from `GHC.IOBase' [...]
Neat! Is there some way I can get access to that information from within my program ? Maybe something like:
import Language.Haskell.TH.Syntax instancesOf :: Name -> [Type]
There's no way you can access that information afaik. -- Friendly, Lemmih
participants (3)
-
Jeremy Shaw
-
Keean Schupke
-
Lemmih