[Haskell-cafe] Re: Newbie question about automatic memoization

Dave Bayer bayer at math.columbia.edu
Mon Jul 30 11:13:42 EDT 2007


peterv <bf3 <at> telenet.be> writes:

> Does Haskell support any form of automatic memorization?

I used a C compiler in the 1980's that would routinely put values into
registers that were already there, so I'm deeply suspicious in ways that
turn out to always be completely unwarranted for GHC. Nevertheless, there
is no better way to understand what a compiler does than to make
experiments.

One thing that profiling does with GHC is count entries into a function.
You can use this for example to determine which way case statements are
usually branching; just make dummy functions (f x = x, not f = id) to pass
each value through, and look at the counts.

In answer to your question, much depends on whether optimization is turned
on or off. If

    f x $ g y

is executed repeatedly for a fixed value of y, one sees equal counts for f
and g with optimization off. With optimization on, g gets called once.

Try to dissect your own situations this way (or better ways that others are
about to chime in with...) It's fun. The usual cycle is "Gee, they can't
have been clever enough to have provided for this obscure scenario...
Click, click, ... click, click. Oh. They were."



More information about the Haskell-Cafe mailing list