<p dir="ltr"><br>
On Aug 3, 2012 11:13 PM, &quot;Brandon Simmons&quot; &lt;<a href="mailto:brandon.m.simmons@gmail.com">brandon.m.simmons@gmail.com</a>&gt; wrote:<br>
&gt; In particular I don&#39;t fully understand why these sorts of contortions...<br>
&gt;<br>
&gt;     <a href="http://hackage.haskell.org/packages/archive/base/latest/doc/html/src/GHC-List.html#foldl">http://hackage.haskell.org/packages/archive/base/latest/doc/html/src/GHC-List.html#foldl</a><br>
&gt;<br>
&gt; ...are required. It seems like a programmer has to throw &quot;equational<br>
&gt; reasoning&quot;, separation of concerns, and all the little elegant bits<br>
&gt; about the language out the window just to indicate something boring to<br>
&gt; the compiler.<br>
&gt;<br>
&gt; Disclaimer: The following is less a proposal meant to be taken<br>
&gt; seriously, and more me trying to better understand things.<br>
&gt;<br>
&gt; Could the following be used as syntax for indicating inlining? Rather<br>
&gt; than relying on the syntactic LHS, instead let that be specified in<br>
&gt; the type signature...<br>
&gt;<br>
&gt;     foldl        :: (a -&gt; b -&gt; a) -&gt; a -&gt; [b] -&gt; {-# INLINE #-} a<br>
&gt;     foldl f z []     =  z<br>
&gt;     foldl f z (x:xs) = foldl f (f z x) xs<br>
&gt;<br>
&gt; ...indicating, in this case, that foldl should be inlined when<br>
&gt; &quot;fully-applied&quot; means its first three arguments (I guess that&#39;s the<br>
&gt; intent of the original version linked above?). Then (waves hands) the<br>
&gt; compiler could do the necessary transformations that the programmer<br>
&gt; had to do to foldl above. Maybe what I&#39;m proposing is actually a<br>
&gt; separate NORECURSIVE_TRANSFORM pragma or something</p>
<p dir="ltr">That&#39;s not quite the effect. What has been done to foldl there is known as the static argument transform. It avoids passing constant arguments along in recursion. f is the only static argument to foldl (foldr by contrast has two).</p>

<p dir="ltr">This can be important for multiple reasons. Sometimes it frees up registers. Here, we may inline foldl and possibly specialize the loop to a statically known f. That is often a big win. For instance, if you write sum with foldl, you can inline, do a worker wrapper transform, and work on unboxed integers with raw adds (probably) instead of going through multiple layers of indirection.</p>

<p dir="ltr">There was some work on making GHC automatically SAT, but of it&#39;s a bit tricky with regard to when it&#39;s worth it, so I don&#39;t think it&#39;s been put in.</p>
<p dir="ltr">I have code that relies on this sort of thing a lot, so if someone comes up with a good way to automate it, I wouldn&#39;t complain.</p>
<p dir="ltr">Dan</p>