announce: GHC internals library (version 0.1)

Bernard James POPE bjpop@cs.mu.OZ.AU
Wed, 1 May 2002 16:21:42 +1000 (EST)


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.