GHC is bad at CSE.  Of course, in general CSE might not be a good idea,
but with strict computations it is.&nbsp; So someone needs to add a CSE pass.<br><br><div class="gmail_quote">On Sat, Mar 29, 2008 at 2:23 AM, Don Stewart &lt;<a href="mailto:dons@galois.com">dons@galois.com</a>&gt; wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">conal:<br>
<div><div></div><div class="Wj3C7c">&gt; &nbsp; &nbsp;I&#39;d like to know if it&#39;s possible to get GHC to perform some simple CSE<br>
&gt; &nbsp; &nbsp;for function-level programming. &nbsp;Here&#39;s a simple example:<br>
&gt;<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp;liftA2 (*) sin sin :: Double -&gt; Double<br>
&gt;<br>
&gt; &nbsp; &nbsp;which inlines and simplifies to<br>
&gt;<br>
&gt; &nbsp; &nbsp; &nbsp;\ t -&gt; sin t * sin t<br>
&gt;<br>
&gt; &nbsp; &nbsp;A more realistic, equivalent, example:<br>
&gt;<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp;let b = sin &lt;$&gt; id in liftA2 (*) b b<br>
&gt;<br>
&gt; &nbsp; &nbsp;Can GHC be nudged into computing &#39;sin t&#39; once rather than twice?<br>
&gt;<br>
<br>
</div></div>So GHC does do some light CSE, but not in this case, as far as I could<br>
see.<br>
<br>
I had a go with a rewrite rule that I felt should have matched, but it<br>
didn&#39;t seem to want to fire. Possibly the dictionaries were getting in<br>
the way.<br>
<br>
<br>
 &nbsp; &nbsp;import System.Environment<br>
 &nbsp; &nbsp;import Prelude hiding (sin)<br>
 &nbsp; &nbsp;import qualified Prelude<br>
<br>
 &nbsp; &nbsp;sin :: Double -&gt; Double<br>
 &nbsp; &nbsp;sin x = Prelude.sin x<br>
 &nbsp; &nbsp;{-# NOINLINE sin #-}<br>
<br>
 &nbsp; &nbsp;times :: Double -&gt; Double -&gt; Double<br>
 &nbsp; &nbsp;times x y = x * y<br>
 &nbsp; &nbsp;{-# NOINLINE times #-}<br>
<br>
 &nbsp; &nbsp;{-# RULES<br>
<br>
 &nbsp; &nbsp;&quot;sin/cse&quot; forall x.<br>
 &nbsp; &nbsp; &nbsp; &nbsp;times (sin x) (sin x) = case Prelude.sin x of y -&gt; y * y<br>
<br>
 &nbsp; &nbsp; &nbsp;#-}<br>
<br>
<br>
 &nbsp; &nbsp;main = do<br>
 &nbsp; &nbsp; &nbsp; &nbsp;[x] &lt;- getArgs<br>
 &nbsp; &nbsp; &nbsp; &nbsp;let n = read x<br>
 &nbsp; &nbsp; &nbsp; &nbsp;print $ sin n `times` sin n<br>
<br>
<br>
_______________________________________________<br>
Glasgow-haskell-users mailing list<br>
<a href="mailto:Glasgow-haskell-users@haskell.org">Glasgow-haskell-users@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/glasgow-haskell-users" target="_blank">http://www.haskell.org/mailman/listinfo/glasgow-haskell-users</a><br>
</blockquote></div><br>