Created
September 18, 2015 03:37
-
-
Save DanielG/1101b8273f945ba14184 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- $ 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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