How does method sharing interact with the ability of the rules engine to &quot;look through&quot; lets?&nbsp; Wouldn&#39;t an f rule kick in when fint is seen, by looking through the fint binding?<br><br>I&#39;ve been wondering: will pattern matching look through a let even when the let-bound variable is used more than once?&nbsp; I chose &quot;yes&quot; in Pan, though somewhat nervously, since all but one of the uses are free anyway.<br>
<br>Cheers,&nbsp; - Conal<br><br><div class="gmail_quote">On Mon, Jun 9, 2008 at 2:38 AM, Simon Peyton-Jones &lt;<a href="mailto:simonpj@microsoft.com" target="_blank">simonpj@microsoft.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;">










<div link="blue" vlink="purple" lang="EN-GB">

<div>

<p><span style="font-size: 11pt; color: rgb(31, 73, 125);">The -fno-method-sharing flag was supposed to be a bit
experimental, which is why it takes the cheap-and-cheerful route of being a
static flag.&nbsp; (Only dynamic flags can go in OPTIONS_GHC.)</span></p>

<p><span style="font-size: 11pt; color: rgb(31, 73, 125);">&nbsp;</span></p>

<p><span style="font-size: 11pt; color: rgb(31, 73, 125);">What it does is this. When you call an overloaded function f ::
C a =&gt; a -&gt; a, in a function</span></p>

<p style="text-indent: 36pt;"><span style="font-size: 11pt; color: rgb(31, 73, 125);">g = ...f...f...</span></p>

<p style="text-indent: 36pt;"><span style="font-size: 11pt; color: rgb(31, 73, 125);">&nbsp;</span></p>

<p><span style="font-size: 11pt; color: rgb(31, 73, 125);">you normally get something like this</span></p>

<p><span style="font-size: 11pt; color: rgb(31, 73, 125);">&nbsp;</span></p>

<p style="text-indent: 36pt;"><span style="font-size: 11pt; color: rgb(31, 73, 125);">fint :: Int -&gt; Int</span></p>

<p style="text-indent: 36pt;"><span style="font-size: 11pt; color: rgb(31, 73, 125);">fint = f Int dCInt</span></p>

<p><span style="font-size: 11pt; color: rgb(31, 73, 125);">&nbsp;</span></p>

<p style="text-indent: 36pt;"><span style="font-size: 11pt; color: rgb(31, 73, 125);">g = ...fint...fint...</span></p>

<p><span style="font-size: 11pt; color: rgb(31, 73, 125);">&nbsp;</span></p>

<p><span style="font-size: 11pt; color: rgb(31, 73, 125);">That is, &#39;fint&#39; extracts the &#39;f&#39; method
from dCInt::C Int, and it&#39;s then used repeatedly.&nbsp; </span></p>

<p><span style="font-size: 11pt; color: rgb(31, 73, 125);">&nbsp;</span></p>

<p><span style="font-size: 11pt; color: rgb(31, 73, 125);">With -fno-method-sharing you get</span></p>

<p><span style="font-size: 11pt; color: rgb(31, 73, 125);">&nbsp;</span></p>

<p style="text-indent: 36pt;"><span style="font-size: 11pt; color: rgb(31, 73, 125);">g =&nbsp; ...(f Int dCInt)
... (f Int dCInt)...</span></p>

<p><span style="font-size: 11pt; color: rgb(31, 73, 125);">&nbsp;</span></p>

<p><span style="font-size: 11pt; color: rgb(31, 73, 125);">So the record selection is duplicated.&nbsp; It shouldn&#39;t
make much difference, but of course it *<b>does</b>* when rules are involved,
because there are no rules for fint (it&#39;s a fresh, local function).&nbsp;
</span></p>

<p><span style="font-size: 11pt; color: rgb(31, 73, 125);">&nbsp;</span></p>

<p><span style="font-size: 11pt; color: rgb(31, 73, 125);">Simon</span></p>

<p><span style="font-size: 11pt; color: rgb(31, 73, 125);">&nbsp;</span></p>

<div style="border-style: none none none solid; border-color: -moz-use-text-color -moz-use-text-color -moz-use-text-color blue; border-width: medium medium medium 1.5pt; padding: 0cm 0cm 0cm 4pt;">

<div>

<div style="border-style: solid none none; border-color: rgb(181, 196, 223) -moz-use-text-color -moz-use-text-color; border-width: 1pt medium medium; padding: 3pt 0cm 0cm;">

<p><b><span style="font-size: 10pt;" lang="EN-US">From:</span></b><span style="font-size: 10pt;" lang="EN-US"> <a href="mailto:glasgow-haskell-users-bounces@haskell.org" target="_blank">glasgow-haskell-users-bounces@haskell.org</a>
[mailto:<a href="mailto:glasgow-haskell-users-bounces@haskell.org" target="_blank">glasgow-haskell-users-bounces@haskell.org</a>] <b>On Behalf Of </b>Conal
Elliott<br>
<b>Sent:</b> 07 June 2008 17:26<br>
<b>To:</b> <a href="mailto:glasgow-haskell-users@haskell.org" target="_blank">glasgow-haskell-users@haskell.org</a><br>
<b>Subject:</b> Re: desperately seeking RULES help</span></p>

</div>

</div><div><div></div><div>

<p>&nbsp;</p>

<p style="margin-bottom: 12pt;">Is it by intention that
-fno-method-sharing works only from the command line, not in an OPTIONS_GHC
pragma?&nbsp; </p>

<div>

<p>On Sat, Jun 7, 2008 at 9:23 AM, Conal Elliott &lt;<a href="mailto:conal@conal.net" target="_blank">conal@conal.net</a>&gt; wrote:</p>

<p>Thanks a million, Lennart!&nbsp; -fno-method-sharing was the
missing piece.&nbsp; - Conal</p>

<div>

<div>

<p style="margin-bottom: 12pt;">&nbsp;</p>

<div>

<p>On Sat, Jun 7, 2008 at 5:07 AM, Lennart Augustsson &lt;<a href="mailto:lennart@augustsson.net" target="_blank">lennart@augustsson.net</a>&gt;
wrote:</p>

<p>Here&#39;s something that actually works. &nbsp;You need to pass<br>
-fno-method-sharing on the command line.<br>
Instead of using rules on methods it uses rules on global functions,<br>
and these global functions don&#39;t get inlined until late (after the<br>
rule has fired).<br>
<br>
&nbsp;-- Lennart</p>

<div>

<p style="margin-bottom: 12pt;"><br>
module F where<br>
<br>
-- | Domain of a linear map.<br>
class AsInt a where<br>
&nbsp;toInt&#39; &nbsp; :: a -&gt; Int<br>
&nbsp;fromInt&#39; :: Int -&gt; a</p>

</div>

<p>{-# INLINE[1] toInt #-}<br>
toInt :: (AsInt a) =&gt; a -&gt; Int<br>
toInt = toInt&#39;<br>
<br>
{-# INLINE[1] fromInt #-}<br>
fromInt :: (AsInt a) =&gt; Int -&gt; a<br>
fromInt = fromInt&#39;</p>

<div>

<p><br>
{-# RULES<br>
&quot;toInt/fromInt&quot; &nbsp; forall m . toInt (fromInt m) = m<br>
&nbsp;#-}<br>
<br>
{-# INLINE onInt #-}<br>
onInt :: AsInt a =&gt; (Int -&gt; Int) -&gt; (a -&gt; a)</p>

</div>

<p>onInt f x = fromInt (f (toInt x))</p>

<div>

<p style="margin-bottom: 12pt;"><br>
test :: AsInt a =&gt; (Int -&gt; Int) -&gt; (Int -&gt; Int) -&gt; (a -&gt; a)<br>
test h g = onInt h . onInt g<br>
<br>
<br>
</p>

</div>

<div>

<p>2008/6/7 Conal Elliott &lt;<a href="mailto:conal@conal.net" target="_blank">conal@conal.net</a>&gt;:</p>

</div>

<div>

<div>

<p>&gt; I&#39;m trying to do some fusion in ghc, and I&#39;d greatly
appreciate help with<br>
&gt; the code below (which is simplified from fusion on linear maps).
&nbsp;I&#39;ve tried<br>
&gt; every variation I can think of, and always something prevents the fusion.<br>
&gt;<br>
&gt; Help, please! &nbsp;Thanks, - Conal<br>
&gt;<br>
&gt;<br>
&gt; {-# OPTIONS_GHC -O2 -Wall -fglasgow-exts -ddump-simpl -ddump-simpl-stats
#-}<br>
&gt; -- {-# OPTIONS_GHC -ddump-simpl-iterations #-}<br>
&gt;<br>
&gt; module F where<br>
&gt;<br>
&gt; -- | Domain of a linear map.<br>
&gt; class AsInt a where<br>
&gt; &nbsp; toInt &nbsp; :: a -&gt; Int<br>
&gt; &nbsp; fromInt :: Int -&gt; a<br>
&gt;<br>
&gt; {-# RULES<br>
&gt; &quot;toInt/fromInt&quot; &nbsp; forall m. toInt (fromInt m) = m<br>
&gt; &nbsp;#-}<br>
&gt;<br>
&gt; {-# INLINE onInt #-}<br>
&gt; onInt :: AsInt a =&gt; (Int -&gt; Int) -&gt; (a -&gt; a)<br>
&gt; onInt f = fromInt . f . toInt<br>
&gt;<br>
&gt; test :: AsInt a =&gt; (Int -&gt; Int) -&gt; (Int -&gt; Int) -&gt; (a -&gt;
a)<br>
&gt; test h g = onInt h . onInt g<br>
&gt;<br>
&gt; -- The desired result:<br>
&gt; --<br>
&gt; -- &nbsp; test h g<br>
&gt; -- &nbsp; &nbsp; == onInt h . onInt g<br>
&gt; -- &nbsp; &nbsp; == (fromInt . h . toInt) . (fromInt . g . toInt)<br>
&gt; -- &nbsp; &nbsp; == \ a -&gt; (fromInt . h . toInt) ((fromInt . g . toInt)
a)<br>
&gt; -- &nbsp; &nbsp; == \ a -&gt; (fromInt . h . toInt) (fromInt (g (toInt
a)))<br>
&gt; -- &nbsp; &nbsp; == \ a -&gt; fromInt (h (toInt (fromInt (g (toInt a)))))<br>
&gt; -- &nbsp; &nbsp; == \ a -&gt; fromInt (h (g (toInt a)))<br>
&gt;<br>
&gt;<br>
&gt;</p>

</div>

</div>

<div>

<div>

<p>&gt; _______________________________________________<br>
&gt; Glasgow-haskell-users mailing list<br>
&gt; <a href="mailto:Glasgow-haskell-users@haskell.org" target="_blank">Glasgow-haskell-users@haskell.org</a><br>
&gt; <a href="http://www.haskell.org/mailman/listinfo/glasgow-haskell-users" target="_blank">http://www.haskell.org/mailman/listinfo/glasgow-haskell-users</a><br>
&gt;<br>
&gt;</p>

</div>

</div>

</div>

<p>&nbsp;</p>

</div>

</div>

</div>

<p>&nbsp;</p>

</div></div></div>

</div>

</div>


</blockquote></div><br>