Skip to content

Instantly share code, notes, and snippets.

@DanielG
Created September 18, 2015 03:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DanielG/1101b8273f945ba14184 to your computer and use it in GitHub Desktop.
Save DanielG/1101b8273f945ba14184 to your computer and use it in GitHub Desktop.
-- $ ghc -package ghc -package ghc-paths GhcTestcase.hs
module Main where
import GHC
import GHC.Paths (libdir)
import DynFlags
import CoreMonad
import Pretty
import PprTyThing
import Outputable
import Bag
import Data.List
main :: IO ()
main = do
defaultErrorHandler defaultFatalMessager defaultFlushOut $
runGhc (Just libdir) $
doStuff "Main.hs" "Main"
doStuff :: String -> String -> Ghc ()
doStuff targetFile targetModule = do
dflags' <- getSessionDynFlags
setSessionDynFlags dflags' {
ghcLink = LinkInMemory
, hscTarget = HscInterpreted
}
dflags <- getSessionDynFlags
target <- guessTarget targetFile Nothing
setTargets [target]
_ <- load LoadAllTargets
setContext [IIModule $ mkModuleName targetModule]
mss <- getModuleGraph
let Just ms = find (\m -> Just targetFile == ml_hs_file (ms_location m)) mss
p <- parseModule ms
tcm@TypecheckedModule{tm_typechecked_source = tcs} <- typecheckModule p
let AbsBinds _ _ _ _ bs = head $ map unLoc $ bagToList tcs
FunBind{ fun_matches = m } = head $ map unLoc $ bagToList bs
ty = mg_res_ty m
liftIO $ putStrLn $ "Type: " ++ showOneLine dflags (pprTypeForUser ty)
return ()
showGhc x df = showSDoc df $ ppr x
showOneLine :: DynFlags -> SDoc -> String
showOneLine dflag =
showDocWith dflag OneLineMode . withStyle dflag styleUnqualified
styleUnqualified :: PprStyle
styleUnqualified = mkUserStyle neverQualify AllTheWay
showDocWith dflags mode = Pretty.showDoc mode (pprCols dflags)
withStyle = withPprStyleDoc
module Main where
foo :: Num a => a
foo = 123
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment