Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I suspect that <code>trees</code> is the evil guy. As John L said this is probably an instance of the Wadler Space Leak in which the compiler is unable to apply the optimization that prevents the leak. The problem is that you use a lazy pattern matching (the <code>let</code> expression) to deconstruct the pair and perform pattern matching via the application of <code>trees</code> on one of the components of the tuple. I had a quite similar problem once <a href="http://comments.gmane.org/gmane.comp.lang.haskell.glasgow.user/19129" rel="noreferrer">http://comments.gmane.org/gmane.comp.lang.haskell.glasgow.user/19129</a>. This thread also provides a more detailed explanation. To prevent the space leak you can simply use a <code>case</code> expression to deconstruct the tuple as follows.</p> <pre><code>trees [] = [] trees (Open x : es) = case splitStream es of (children, rest) -&gt; Tree (Node x) (trees children) : trees rest </code></pre> <p>With this implementation the maximum residency drops from 38MB to 28KB.</p> <p>But note that this new implementation of <code>trees</code> is more strict than the original one as it demands the application of <code>splitStream</code>. Therefore, in some cases this transformation might even cause a space leak. To regain a less strict implementation you might use a similar trick as the <code>lines</code> function in Data.List which causes a similar problem <a href="http://hackage.haskell.org/packages/archive/base/latest/doc/html/src/Data-List.html#lines" rel="noreferrer">http://hackage.haskell.org/packages/archive/base/latest/doc/html/src/Data-List.html#lines</a>. In this case <code>trees</code> would look as follows.</p> <pre><code>trees [] = [] trees (Open x : es) = context (case splitStream es of (children, rest) -&gt; (trees children, trees rest)) where context ~(children', rest') = Tree (Node x) children' : rest' </code></pre> <p>If we desugar the lazy pattern matching we get the following implementation. Here the compiler is able to detect the selector to the tuple component as we do not perform pattern matching on one of the components.</p> <pre><code>trees [] = [] trees (Open x : es) = Tree (Node x) children' : rest' where (children', rest') = case splitStream es of (children, rest) -&gt; (trees children, trees rest) </code></pre> <p>Does anybody know whether this transformation always does the trick?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload