GHC-api and typechecking class restrictions

I am trying to create a simple ghci-like console using ghc-api. I got to the point where I can extract Type expressions with exprType and evaluate them. Is there also an easy way to check if an expression type has an instance of a given class?

Edit: It seems that the functions I'm looking for will require InstEnv , but I don't know where to find it.

+4
source share
1 answer

The isInstance method can do what you need.

Example in ghci:

 > :set -XTemplateHaskell > import Language.Haskell.TH > $(stringE . show =<< (isInstance ''Functor . (: []) =<< [t| [] |])) "True" > $(stringE . show =<< (isInstance ''Show . (: []) =<< [t| Maybe Int |])) "True" $(stringE . show =<< (isInstance ''Show . (: []) =<< [t| (Int -> Bool) |])) "False" 

Its sig type is equal to

 isInstance :: Name -> [Type] -> Q Bool 

Those. you give the name (obtained with '' or using the mkName function) for the class, then you pass the types to check against the class (the class will use the multicast type class). Then it will return True or False in monad Q.

+3
source