Skip to content

[WIP] invisible kind arguments #164

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion src/Hint/Base.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module Hint.Base (
ImportList(..), ModuleQualification(..), ModuleImport(..),

ModuleName, PhantomModule(..),
findModule, moduleIsLoaded,
findModule, lookupTyCon, lookupDataCon, moduleIsLoaded,
withDynFlags,

ghcVersion,
Expand Down Expand Up @@ -175,6 +175,26 @@ findModule mn = mapGhcExceptions NotAllowed $
runGhc $ GHC.findModule mod_name Nothing
where mod_name = GHC.mkModuleName mn

lookupTyCon :: MonadInterpreter m => ModuleName -> String -> m GHC.TyCon
lookupTyCon moduleName tyConName = do
mapGhcExceptions NotAllowed $ do
runGhc $ do
GHC.lookupTyCon moduleName tyConName >>= \case
Right tyCon -> do
pure tyCon
Left err -> do
throwM $ UnknownError err

lookupDataCon :: MonadInterpreter m => ModuleName -> String -> m GHC.DataCon
lookupDataCon moduleName dataConName = do
mapGhcExceptions NotAllowed $ do
runGhc $ do
GHC.lookupDataCon moduleName dataConName >>= \case
Right dataCon -> do
pure dataCon
Left err -> do
throwM $ UnknownError err

moduleIsLoaded :: MonadInterpreter m => ModuleName -> m Bool
moduleIsLoaded mn = (True <$ findModule mn)
`catchIE` (\e -> case e of
Expand Down
50 changes: 50 additions & 0 deletions src/Hint/GHC.hs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ module Hint.GHC (
#if MIN_VERSION_ghc(9,6,0)
getPrintUnqual,
#endif
lookupTyCon,
lookupDataCon,
-- * Re-exports
module X,
) where
Expand Down Expand Up @@ -135,9 +137,15 @@ import ConLike as X (ConLike(RealDataCon))
{-------------------- Imports for Shims --------------------}

import Control.Monad.IO.Class (MonadIO)

-- guessTarget
import qualified GHC (guessTarget)

-- lookupTyCon, lookupDataCon
import Control.Monad.Trans.Class (lift)
import Control.Monad.Trans.Writer.CPS (execWriterT, tell)
import Data.Foldable (for_)

#if MIN_VERSION_ghc(9,6,0)
-- dynamicGhc
import GHC.Platform.Ways (hostIsDynamic)
Expand Down Expand Up @@ -653,3 +661,45 @@ guessTarget = GHC.guessTarget
getPrintUnqual :: GhcMonad m => m PrintUnqualified
getPrintUnqual = GHC.getNamePprCtx
#endif

lookupCandidates :: GhcMonad m => String -> String -> m [Name]
lookupCandidates moduleString name = do
parseName (moduleString ++ "." ++ name)

lookupTyCon :: GhcMonad m => String -> String -> m (Either String TyCon)
lookupTyCon moduleString tyConString = do
names <- lookupCandidates moduleString tyConString
tyCons <- execWriterT $ do
for_ names $ \name -> do
maybeTyThing <- lift $ lookupName name
case maybeTyThing of
Just (ATyCon tyCon) -> do
tell [tyCon]
_ -> do
pure ()
case tyCons of
[tyCon] -> do
pure $ Right tyCon
[] -> do
pure $ Left $ "Could not find type constructor " ++ tyConString
_ -> do
pure $ Left $ "Ambiguous type constructor " ++ tyConString

lookupDataCon :: GhcMonad m => String -> String -> m (Either String DataCon)
lookupDataCon moduleString dataConString = do
names <- lookupCandidates moduleString dataConString
dataCons <- execWriterT $ do
for_ names $ \name -> do
maybeTyThing <- lift $ lookupName name
case maybeTyThing of
Just (AConLike (RealDataCon dataCon)) -> do
tell [dataCon]
_ -> do
pure ()
case dataCons of
[dataCon] -> do
pure $ Right dataCon
[] -> do
pure $ Left $ "Could not find data constructor " ++ dataConString
_ -> do
pure $ Left $ "Ambiguous data constructor " ++ dataConString