[Haskell] Compilation of big, computed tables

Chris Kuklewicz haskell at list.mightyreason.com
Wed Feb 22 16:09:45 EST 2006

Stefan Karrmann wrote:
> Dear all,
> can ghc compile huge tables into efficient code if they are constant at
> compile time?
> Two examples may clearify the question:
> big1 :: UArray Int Char
> big1 = array (0,1000) $! map (\i -> (i,toEnum i)) [0..1000]
> big2 = sum [0..10000]::Int -- == 50005000 == n*(n+1)/2 where n = 10000

GHC does not compute such values are compile time because
*) The computation may not finish in reasonable time (infinite/not halting)
*) The computation may generate a run-time error and crash the compilation
(divide by zero, pattern march failure, etc.)
*) Haskell is supposed to be lazy, things are computed when needed, not before

The big1 UArray is unboxed which prevents laziness, so big1 will be fully
computed the first time any of the values are referenced.

There is a way around this:

I have not needed to use it, but template haskell can compute things at compile
time. ( http://haskell.org/hawiki/TemplateHaskell and
http://haskell.org/hawiki/TemplateHaskellTutorial are good links)

[ Template haskell could even be abused to interactively allow you to
interactively type in a URL from which to download code to insert into the file. ]

More information about the Haskell mailing list