[Haskell-cafe] How can we detect and fix memory leak due to lazyness?

Spencer Janssen spencerjanssen at gmail.com
Mon Aug 7 21:14:04 EDT 2006


On 8/7/06, Ahn, Ki Yung <kyagrd at gmail.com> wrote:
> I have posted an wiki article including one example of adding
> a counter to count the number of basic operations in sorting algorithm.
>
> http://www.haskell.org/haskellwiki/Physical_equality
>
> This was a rather simple situation and we figured out how to
> cure this by self equality check ( x==x ) forcing evaluation.

Forcing evaluation using (==) is a bit of a hack.  Luckily, we have a
better function to force evaluation: seq (which has type a -> b -> b).
 "seq x y" evaluates "x" to weak head normal form before returning
"y".

Let's try another feature of Haskell to force evaluation: strict data
fields.  A ! in front of a field in a data declaration signifies
strictness.  In the example below, whenever we construct a value with
TT, the second argument is evaluated.

\begin{code}
data TT a b = TT a !b
\end{code}

Perhaps your instances will work correctly with this data declaration?


Cheers,
Spencer Janssen


More information about the Haskell-Cafe mailing list