Just to be sure, because I am not quite familiar with the dark hairy internals of GHC:<br><br>&gt; Of course, given a type signature that allows strictness to be inferred.<br><br>You mean a signature with no type variables and types that are know to GHC as being strict?<br>

(Like <span style="font-family: courier new,monospace;">Int -&gt; Int -&gt; Int</span> instead of <span style="font-family: courier new,monospace;">(Num a) =&gt; a -&gt; a -&gt; a</span>)<br><br>&gt; The difference is that the explicit recursion produces the better code even<br>


&gt; with optimisations turned off, except that the overload of (+) to use is<br>
&gt; not inlined, so the accumulator still builds a thunk, while with<br>
&gt; optimisations you get the specialised strict additions (+# resp.<br>
&gt; plusInteger, ...) so you have the strictness you need.<br><br>(+#) is then the GHC&#39;s strict equivalent of (+)?<br>But if you make an overlay to (+), like, say:<br><br>(?) :: (Num a) =&gt; a -&gt; a -&gt; a<br>a ? b = a + b<br>

<br>Then (?) will be lazy, won&#39;t it?<br>Then optimizations will not occur, <span style="font-family: courier new,monospace;">a ? b</span> will remain a thunk and not be replaced by <span style="font-family: courier new,monospace;">a +# b</span> and be strictly evaluated?<br>

<br>If so, then it means that you can always turn a strict function into a non strict one, am I right?<br><br><br><div class="gmail_quote">2011/3/31 Daniel Fischer <span dir="ltr">&lt;<a href="mailto:daniel.is.fischer@googlemail.com">daniel.is.fischer@googlemail.com</a>&gt;</span><br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"><div class="im">On Thursday 31 March 2011 11:45:00, Christian Maeder wrote:<br>
&gt; Since we don&#39;t have a function sum&#39; in the Prelude (should we have it?)<br>
<br>
</div>I think we should.<br>
<div class="im"><br>
&gt; I wonder what happens if you just use &quot;sum&quot;. Will the &quot;sum&quot; (based on<br>
&gt; sum&#39; so without -DUSE_REPORT_PRELUDE) be strict enough?<br>
<br>
</div>I don&#39;t know about other compiler&#39;s behaviour, but for GHC, it will be<br>
strict enough *if compiled with optimisations*, but not without (the<br>
strictness analyser runs only with optimisations turned on).<br>
- Of course, given a type signature that allows strictness to be inferred.<br>
<br>
However, the same holds for &#39;foldl (+) 0&#39;. In fact, in the presence of a<br>
suitable type signature, with optimisations turned on, both produce nearly<br>
identical code (the order of parameters in the recursive loop is changed,<br>
sometimes parameter order can make a surprisingly large difference, but<br>
whether it&#39;s better to have the list or the accumulator first depends).<br>
<br>
The difference is that the explicit recursion produces the better code even<br>
with optimisations turned off, except that the overload of (+) to use is<br>
not inlined, so the accumulator still builds a thunk, while with<br>
optimisations you get the specialised strict additions (+# resp.<br>
plusInteger, ...) so you have the strictness you need.<br>
<div class="im"><br>
&gt;<br>
&gt; #ifdef USE_REPORT_PRELUDE<br>
&gt; sum                     =  foldl (+) 0<br>
&gt; product                 =  foldl (*) 1<br>
&gt; #else<br>
&gt; sum     l       = sum&#39; l 0<br>
&gt;    where<br>
&gt;      sum&#39; []     a = a<br>
&gt;      sum&#39; (x:xs) a = sum&#39; xs (a+x)<br>
&gt; product l       = prod l 1<br>
&gt;    where<br>
&gt;      prod []     a = a<br>
&gt;      prod (x:xs) a = prod xs (a*x)<br>
&gt; #endif<br>
&gt;<br>
&gt; Cheers C.<br>
&gt;<br>
&gt; P.S.<br>
&gt;<br>
&gt; isMultiple a = any ((== 0) . mod a)<br>
<br>
</div>For Int (and most other types),<br>
<br>
isMultiples a = any ((== 0) . rem a)<br>
<br>
will be faster (mod is implemented using rem).<br>
However, for checks other than comparisons with 0, one needs to be aware of<br>
the differences of rem and mod, the latter does what one would expect, rem<br>
can badly surprise the unaware.<br>
<div><div></div><div class="h5"><br>
_______________________________________________<br>
Haskell-Cafe mailing list<br>
<a href="mailto:Haskell-Cafe@haskell.org">Haskell-Cafe@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/haskell-cafe" target="_blank">http://www.haskell.org/mailman/listinfo/haskell-cafe</a><br>
</div></div></blockquote></div><br>