equivalent of EXPLAIN PLAN with GHC?

Joshua Haberman joshua at reverberate.org
Sun Aug 2 02:50:40 EDT 2009


Hello, I'm quite new to Haskell, but experienced in other languages (C,
Python, Ruby, SQL, etc).  I am interested in Haskell because I've heard
that the language is capable of lots of optimizations based on laziness,
and I want to learn more about that.

I dug in with Project Euler problem #1, and wrote:

main = print (show (sum [x | x <- [3..999], x `mod` 3 == 0 || x `mod` 5 == 0]))

So far so good, but I want to have some way of observing what
optimizations GHC has performed.  Most notably, in this example I want
to know if the list was ever actually constructed in memory.  The "sum"
function only needs the elements one at a time, in order, so if Haskell
and GHC are everything I've heard about them, I would fully expect the
list construction to be optimized out.  :)

Unfortunately I was not able to see any way of examining ghc's output to
determine whether it had performed this optimization.  The C it produced
with '-fvia-C -C' was totally unreadable -- it looked like something
from the IOCCC. :(  I couldn't find any way to match up any of the code
it had generated with code I had written.  My attempts to objdump the
binaries was similarly unproductive.

Is there any kind of intermediate form that a person can examine to see
how their code is being optimized?  Anything like EXPLAIN PLAN in SQL?
It would make it much easier to understand the kinds of optimizations
Haskell can perform.

I'm not looking so much for profiling -- obvious this program is trivial
and takes no time.  I just want to better understand what kind of
optimizations are possible given Haskell's language model.

Thanks!
Josh



More information about the Glasgow-haskell-users mailing list