I am trying to extract the STG representation of a Haskell source as a String
via Outputable
, but it looks like coreToStgArgs
is panicing with the following dump:
user@machine ~/Desktop/hue $ runhaskell test.hs
[foo :: forall a. Num a => a -> a
[GblId, Arity=2, Caf=NoCafRefs, Str=DmdType] =
\r srt:SRT:[] [$dNum a1] + $dNum a1 a1;,
bar :: Int -> Int
[GblId,test.hs: test.hs: panic! (the 'impossible' happened)
(GHC version 7.10.3 for x86_64-unknown-linux):
coreToStgArgs I# 3
Please report this as a GHC bug: http://www.haskell.org/ghc/reportabug
Here is the file FooBar.hs
that I want to extract:
module FooBar where
foo a = a + a
bar :: Int -> Int
bar b = b + 3
Here is the source of test.hs
that I used:
import CoreToStg
import GHC
import GHC.Paths
import Outputable
import StgSyn
mkDynFlags :: IO DynFlags
mkDynFlags = runGhc (Just libdir) getSessionDynFlags
mkSTG :: FilePath -> FilePath -> IO [StgBinding]
mkSTG proj src = do
dflags <- mkDynFlags
ghc_core <- runGhc (Just libdir) $ do
setSessionDynFlags (dflags {importPaths = [proj]})
compileToCoreSimplified src
-- compileToCoreModule src
coreToStg dflags (cm_module ghc_core) (cm_binds ghc_core)
mkIOStr :: (Outputable a) => a -> IO String
mkIOStr obj = do
dflags <- mkDynFlags
let ppr_str = showPpr dflags obj
return ppr_str
main :: IO ()
main = do
let proj = "/home/user/Desktop/hue"
let src = proj ++ "/FooBar.hs"
res <- mkIOStr =<< mkSTG proj src
putStrLn res
It looks like someone several years before me has run into a similar problem:
https://ghc.haskell.org/trac/ghc/ticket/7159
However, I have no idea what has happened since. I am also not sure if this is the correct way to go about extracting the STG of an arbitrary Haskell source, so if there are better alternatives that work, I would like to hear about them.
EDIT:
STG translation appears successful for the following program where bar b = b + 3
is changed to bar b = 3
:
module FooBar where
foo a = a + a
bar :: Int -> Int
bar b = 3
In fact, at first glance, things appear to work if the induced Core Haskell does not force primitive operations to be performed. For instance bar b = 3 + 9
fails.
CorePrep.corePrepPgm
before trying to useCoreToStg.coreToStg
.". – melpomene