<p>I was wondering the same thing a while ago and created a prototypical implementation of such a thunk-duplication function. You can read about the result in my preprint „<a href="http://arxiv.org/abs/1207.2017" rel="noreferrer">dup – Explicit un-sharing in haskell</a>” and see the code at <a href="http://darcs.nomeata.de/ghc-dup" rel="noreferrer">http://darcs.nomeata.de/ghc-dup</a>. Unfortunately, the paper was neither accepted for the Haskell Symposium nor the Haskell Implementors Workshop this year.</p>

<p>To my knowledge, there is no real-world-ready solution to the problem; only fragile work-arounds as the unit parameter trick that might break due to one or the other compiler optimizations.</p>
Can a thunk be duplicated to improve memory performance?
<p>One of my struggles with lazy evaluation in Haskell is the difficulty of reasoning about memory usage.  I think the ability to duplicate a thunk would make this much easier for me.  Here's an example.</p>

<p>Let's create a really big list:</p>

<pre><code>let xs = [1..10000000]
</code></pre>

<p>Now, let's create a bad function:</p>

<pre><code>bad = do
    print $ foldl1' (+) xs
    print $ length xs
</code></pre>

<p>With no optimizations, this eats up a few dozen MB of ram.  The garbage collector can't deallocate xs during the fold because it will be needed for calculating the length later.</p>

<p>Is it possible to reimplement this function something like this:</p>

<pre><code>good = do
    (xs1,xs2) &lt;- copyThunk xs
    print $ foldl1' (+) xs1
    print $ length xs2
</code></pre>

<p>Now, xs1 and xs2 would represent the same value, but also be independent of each other in memory so the garbage collector can deallocate during the fold preventing memory wasting.  (I think this would slightly increase the computational cost though?)</p>

<p>Obviously in this trivial example, refactoring the code could easily solve this problem, but It seems like it's not always obvious how to refactor.  Or sometimes refactoring would greatly reduce code clarity.</p>