Extracting Haskell STG source

I am trying to extract the STG representation of a Haskell source as Stringthrough Outputable, but it looks like it coreToStgArgsis panning 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.hsI want to extract:

module FooBar where

foo a = a + a

bar :: Int -> Int
bar b = b + 3

Here is the source test.hsI 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 seems like someone a few years before me ran into a similar problem:

https://ghc.haskell.org/trac/ghc/ticket/7159

However, I have no idea what has happened since then. I'm also not sure if this is the right way to extract the STG of an arbitrary Haskell source, so if there are better alternatives that work, I'd love to hear about them.

EDIT: The STG translation seems successful for the next program, where it is bar b = b + 3changed to bar b = 3:

module FooBar where

foo a = a + a

bar :: Int -> Int
bar b = 3

, , , , Core Haskell . , bar b = 3 + 9 .

+6
1

melpomene , .

test.hs, :

import CorePrep
import CoreToStg
import GHC
import GHC.Paths
import GhcMonad
import HscTypes
import Outputable
import StgSyn
import System.IO

mkSTG :: FilePath -> FilePath -> IO [StgBinding]
mkSTG proj src = runGhc (Just libdir) $ do
        env    <- getSession
        dflags <- getSessionDynFlags
        setSessionDynFlags (dflags {importPaths = [proj]})
        target <- guessTarget src Nothing
        setTargets [target]
        load LoadAllTargets

        mod_graph <- getModuleGraph
        let mod_sum = head mod_graph  -- This is bad practice
        pmod <- parseModule mod_sum
        tmod <- typecheckModule pmod
        dmod <- desugarModule tmod
        let guts  = coreModule dmod
        let loc   = ms_location mod_sum
        let binds = mg_binds guts
        let tcs   = mg_tcs guts
        prep <- liftIO $ corePrepPgm env loc binds tcs
        liftIO $ coreToStg dflags (mg_module guts) prep

mkIOStr :: (Outputable a) => a -> IO String
mkIOStr obj = do
    dflags <- runGhc (Just libdir) getSessionDynFlags
    let ppr_str = showPpr dflags obj
    return ppr_str

main :: IO ()
main = do
    let proj = "/home/celery/Desktop/hue"
    let src  = proj ++ "/FooBar.hs"
    res <- mkIOStr =<< mkSTG proj src
    putStrLn res

, ModSummary (, , ModuleName) Target , , ModuleGraph, type ModuleGraph = [ModSummary].

corePrepPgm GHC 7 8:

https://downloads.haskell.org/~ghc/7.10.1/docs/html/libraries/ghc-7.10.1/CorePrep.html

https://downloads.haskell.org/~ghc/latest/docs/html/libraries/ghc-8.0.1/CorePrep.html

:)

. - head ModuleGraph . , , - ModSummary ModuleGraph , .

+1

All Articles