Skip to content

Unable to `reifyInstances` a type class with multiple parameters

Summary

I'd like to be able to find matching instances of a multiparameter type class, like class SqlSelect a b | a -> b, b -> a. But the naive reifyInstances ''SqlSelect [VarT (mkName "a")] is not working, and returns the empty list.

Steps to reproduce

{-# language MultiParamTypeClasses, TemplateHaskell #-}

import Language.Haskell.TH

class C a b

class D a

instance C Int Char
instance D Int

do
    instances <- reifyInstances ''C [VarT (mkName "a")]
    case instances of
        [] ->
            fail "should have at least one instance?"
        xs -> do
            runIO $ traverse print instances
            pure []

If you reifyInstances ''D instead, then it returns the sole instance.

Expected behavior

Well, that's a good question.

Obviously, AppT (ConT ''SqlSelect) (VarT (mkName "a")) doesn't make sense, which is the most 'obvious' way to consume the name and type given. What I really want to do is ask GHC whether there's an instance SqlSelect wanted Foo, and if so, use the concrete type of wanted in code generation.

Environment

  • GHC version used: 9.2.2

Optional:

  • Operating System:
  • System Architecture:
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information