announce: GHC internals library (version 0.1)

Hi all, Announcing the GHC internals library, version 0.1. Essentially this provides a means for polymorphically deconstructing values that reside on the GHC heap, all from the comfort of your Haskell program. For those who are familiar with the HugsInternals library that comes with Hugs, it offers similar support to getCell and classifyCell, in a GHC-centric way. What might you use this for? I don't know, but I'm using it for debugging, maybe you can too, or alternatively, you might find another use. You can find it here: http://www.cs.mu.oz.au/~bjpop/code.html For your curiosity, here's _one_ thing you can do with it:
import GhcInternals
main :: IO () main = do let val = zip [1..12] (iterate not True) print $ take 3 val metaVal <- reify False val putStrLn $ prettyPrintMeta metaVal []
Running this program you get: [(1,True),(2,False),(3,True)] (: ((,) 1 True) (: ((,) 2 False) (: ((,) 3 True) THUNK))) How does it work? Basically, using some odd looking FFI code it peaks at the heap representation of a value, and constructs a meta representation of it. The deconstruction is only done one constructor at a time, although provision is made for the whole structure to be inspected (more or less) at once. Cycles and sharing in general are not yet supported, but future versions will attempt to solve this, perhaps with some tinkering with Stable Pointers. You can choose to inspect the structure lazily (ie no further evaluation) or strictly (ie force the structure to evaluate one constructor at a time). Functions can be printed, but mostly you'll get some funny string that the compiler generated. This might be improved in later versions of the library. Currently it relies on compiling your program with -prof to make sure the names of constructors are retained in the heap. No modifications are required to your compiler, it should compile out of the box without any hackery to GHC itself. Works with version 5.03 of GHC, but you need a very current version to avoid exposing some bugs in the Stable Pointer implementation. Cheers, Bernie.

Bernard James POPE
Announcing the GHC internals library, version 0.1. Essentially this provides a means for polymorphically deconstructing values that reside on the GHC heap, all from the comfort of your Haskell program.
Very cool! As you say, it's a similar design to the HugsInternals library but there are some significant differences. For example, Hugs has: data CellKind = Apply Cell [Cell] | Fun Name | Con Name | Tuple Int | Int Int | Integer Integer | Float Float | Char Char | Prim String | Error Cell while GHC has: data CellKind = Apply String [Cell] -- constructor application (saturated) | Fun String -- a function (name may be mangled) | Thunk -- an expression not in WHNF | Int Int | Integer Integer | Float Float | Double Double | Char Char | Misc String -- any other value that we don't yet distinguish (ie Blackhole) Any chance we can work together to eliminate as many of these differences as possible? -- Alastair Reid
participants (2)
-
Alastair Reid
-
Bernard James POPE