[Haskell-cafe] Re: static constants -- ideas?

Don Stewart dons at galois.com
Mon Mar 3 17:22:59 EST 2008


jason.dusek:
>   I have an awkward programming problem -- I need to take a
>   dictionary, parse it, build a bunch of intermediate lists and
>   then make maps and tries out of the list. A "programming
>   problem" because it's taken me a fair amount of effort to pull
>   together the parser and list generator -- and "awkward"
>   because a 69000 item list, [(String, [(String, String)])],
>   does not compile under GHC (stack overflow). (It's not likely
>   to compile under anything else, either!)

Here's an example of the approach Bryan outlined, which does seem to
work for files as large as gcc can handle:

    * generate your big Haskell Map
    * serialise it with Data.Binary, and Codec.Compression.GZip to a file
    * compile the data into a C const array, and link that into Haskell
    * decode it on startup, ressurecting the Haskell data.

The C source looks like:

    const uint8_t beowulf[] = { 
        31, 139,   8,   0,   0,   0,   0,   0,   0,   3, 124, 189,  75,
        150,  46,  54,  93, 193,  96, 144, 241, 168, 172, 238, 214,   0,
        ...

    http://code.haskell.org/~dons/code/compiled-constants/cbits/constants.c

which is the gzip, Data.Binary encoded version of a Map ByteString Int.
Then the Haskell code need only access this array as a Ptr Word8, wrap
that as a Bytestring, then run Data.Binary over the result to rebuild
the Map. As you can see here:

    http://code.haskell.org/~dons/code/compiled-constants/Constants.hs

I've put a couple of examples of how to access C-side serialised Haskell
values in a package here:

    http://code.haskell.org/~dons/code/compiled-constants/

Cheers,
  Don


More information about the Haskell-Cafe mailing list