我有以下代码使用 GHC API 加载模块并获取表达式的类型:
typeObjects :: [String] -> [String] -> IO [Type]
typeObjects modules objects = do
defaultErrorHandler defaultDynFlags $ do
runGhc (Just libdir) $ do
dflags <- getSessionDynFlags
setSessionDynFlags dflags
targets <- mapM ((flip guessTarget) Nothing) modules
setTargets targets
result <- load LoadAllTargets
case result of
Failed -> error "Compilation failed"
Succeeded -> do
m <- mapM (((flip findModule) Nothing) . mkModuleName) modules
setContext m []
values <- mapM exprType objects
return values
如果表达式不进行类型检查,则整个程序会崩溃:
TestDynamicLoad: panic! (the 'impossible' happened)
(GHC version 7.0.3.20110330 for x86_64-unknown-linux):
Couldn't match expected type `GHC.Types.Int'
with actual type `[GHC.Types.Char]'
我怎样才能使它不会使程序崩溃?我只想知道哪些表达式类型检查成功,哪些没有。