Hi all,

I discovered that the issue of trying to use the "dumpSTG" function from with ghci was occuring whenever I tried to run it on one particular Haskell input file. When I used it on a different file, it allowed me to get the list of STG bindings in ghci as desired. I'll be investigating why that first file is causing the ghci panic, but other than that, my question has been answered/resolved.

Thanks,
Michael

On 02/02/2016 10:30 PM, Michael Christensen wrote:
Hi all,

I'm currently trying to understand how STG works, and my goal right now is to be able to inspect StgBinding values. I've written a short program, based on the wiki article GHC/As a library, like below:

-- Code.hs --
module Lib (printSTG, dumpSTG) where

import           Control.Monad.Ghc  (lift, runGhcT)
import           CorePrep           (corePrepPgm)
import           CoreToStg          (coreToStg)
import           DynFlags           (defaultFatalMessager, defaultFlushOut)
import           GHC                hiding (runGhcT)
import           GHC.Paths          (libdir)
import           HscMain            (newHscEnv)
import           HscTypes           (hsc_dflags, typeEnvTyCons)
import           Outputable         (interppSP, showSDoc)
import           System.Environment (getArgs)
import           StgSyn             (StgBinding)

dumpSTG :: String -> IO [StgBinding]
dumpSTG fileName = defaultErrorHandler defaultFatalMessager defaultFlushOut $
    runGhcT (Just libdir) $ do
      sess <- getSession
      let dflags = hsc_dflags sess
      setSessionDynFlags dflags
      cm <- compileToCoreModule fileName
      let md = cm_module cm
      ml <- fmap ms_location $ getModSummary $ moduleName md
      lift $ do
        cp <- corePrepPgm sess ml <$> cm_binds <*> (typeEnvTyCons . cm_types) $ cm
        coreToStg dflags md cp

printSTG =
    getArgs >>= \x -> case x of
    [] -> putStrLn "usage: Main <file.hs>"
    (fileName:_) -> do
      bindings <- dumpSTG fileName
      str <- runGhcT (Just libdir) $ do
        dflags <- getSessionDynFlags
        return $ showSDoc dflags $ interppSP bindings
      putStrLn str

This works when I compile it to print out a Haskell file in STG format.

However, my question is, is there a way so that I can call dumpSTG to get back that list of StgBindings, from within Ghci? Whenever I do so within Ghci, i.e.

ghci> :l code.hs
ghci> bindings <- dumpSTG "fileToTest.hs"
<interactive>: panic! (the 'impossible' happened)
  (GHC version 7.10.3 for x86_64-unknown-linux):
  no package state yet: call GHC.setSessionDynFlags

I know that it's saying I need to set the session dyn flags, but I'm not sure how that can get done from within Ghci, especially because that needs to get done within the GhcMonad. Part of my problem may stem from having a inadequate understanding of monads. Any help or pointers would be greatly appreciated!

Thank you very much,
Mike