FW: GHC as a library
C.M.Brown
cmb21 at kent.ac.uk
Wed May 30 12:21:44 EDT 2007
Hi Kenny,
>
> \begin{code}
>
> main = do
> session <- GHC.newSession GHC.JustTypecheck (Just "Test.hs")
>
> dflags1 <- GHC.getSessionDynFlags session
> (dflags2, packageIds) <- Packages.initPackages dflags1
> GHC.setSessionDynFlags session dflags2{GHC.hscTarget=GHC.HscNothing}
>
> let name = "Test.hs"
> target <- GHC.guessTarget name Nothing
> GHC.addTarget session target
> GHC.load session GHC.LoadAllTargets
>
> let preludeModule = GHC.mkModule (PackageConfig.stringToPackageId
> "base") (GHC.mkModuleName "Prelude")
> GHC.setContext session [] [preludeModule]
>
> m_checkedModule <- GHC.checkModule session (GHC.mkModuleName "Test")
> ...
>
> \end{code}
>
> I am hoping to retrieve the inferred types of the declared Ids in Test.hs
> At this stage, what I obtain is a value of type Maybe CheckedModule.
You can certainly get the type of an arbritrary expression in your project
module using something like the following:
ghcTypeCheck session expr modName =
unsafePerformIO(
do
Just ty <- exprType session (modName ++ "." ++ expr)
ty' <- cleanType ty
return $ showSDoc $ ppr ty'
)
cleanType ty = do
return $! GHC.dropForAlls ty
That may be of some help...
Cheers,
Chris.
More information about the Glasgow-haskell-users
mailing list