Hi,<div><br></div><div>recently I found out that some recursive functions can be greatly optimized just by rewriting them using `let` so that they&#39;re not recursive themselves any more (only the inner let is). To give an example:</div>

<div><br></div><div><div>&gt; fix :: (a -&gt; a) -&gt; a</div><div>&gt; fix f = f (fix f)</div></div><div><br></div><div>isn&#39;t optimized, because it&#39;s a recursive function and so it isn&#39;t inlined or anything. However, defining it as</div>

<div><br></div><div>&gt; fix f = let x = f x in x</div><div><br></div><div>makes it much more efficient, since `fix` is not recursive any more, so it gets inlined and then optimized for a given argument `f`.</div><div>(See <a href="http://stackoverflow.com/questions/12999385/why-does-ghc-make-fix-so-confounding">http://stackoverflow.com/questions/12999385/why-does-ghc-make-fix-so-confounding</a>)</div>

<div><br></div><div>Another example is the well-known generic catamorphism function:</div><div><br></div><div>&gt; newtype Fix f = Fix { unfix :: f (Fix f) }</div><div>&gt;</div><div><div>&gt; catam :: (Functor f) =&gt; (f a -&gt; a) -&gt; (Fix f -&gt; a)</div>

<div>&gt; catam f = f . fmap (catam f) . unfix</div></div><div><br></div><div>is not optimized. When `catam` is used on a data structure such as [] or a tree, at each level `fmap` creates new constructors that are immediately consumed by `f`. But when written as</div>

<div><br></div><div><div>&gt; catam f = let u = f . fmap u . unfix in u</div></div><div><br></div><div>this gets inlined and then optimized, and constructor creation/consumption is fused into very effective code.</div><div>

(See <a href="http://stackoverflow.com/q/13099203/1333025">http://stackoverflow.com/q/13099203/1333025</a>)</div><div><br></div><div>As one comment asked, this seems like something GHC could do automatically. Would it be difficult? Is there a reason against it? I suppose in cases where it doesn&#39;t help, the additional `let` would get optimized away, so no harm would be done. And in cases like `fix` or `catam` the gain would be substantial.<br>

</div><div><br></div><div>  Best regards,</div><div>   Petr</div>