Skip to content

Transitively imported instances aren't found

I have three modules:

  • A.hs defines a typeclass TC and an instance TC Int
  • B.hs imports A and uses the TC Int instance
  • C.hs imports B and uses the TC Int instance

The problem I am encountering is that B typechecks, but C doesn't. I assume it must mean that something from B is not correctly registered by my usage of the GHC API. If I change C.hs to directly import A, then it typechecks.

My full code is in Main.hs. The function that registers a module after compilation is implemented as follows:

registerModule :: (GhcMonad m) => ModDetails -> ModIface -> m ()
registerModule details@ModDetails{..} iface = do
    liftIO . putStrLn $ unwords ["Registering module", moduleNameString . moduleName $ mod]
    env <- getSession
    env <- return $ extendHpt . addModule $ env
    setSession env
  where
    hmi = HomeModInfo iface details Nothing

    mod = mi_module iface
    modOrig = ModOrigin (Just True) [] [] True

    addModule = modifyUnitState $ \us -> us
        { moduleNameProvidersMap = M.insert (moduleName mod) (M.singleton mod modOrig) $ moduleNameProvidersMap us
        }

    extendHpt env = env
        { hsc_unit_env = let ue = hsc_unit_env env in ue
            { ue_hpt = addHomeModInfoToHpt hmi (hsc_HPT env)
            }
        }
Edited by Gergő Érdi
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information