I have (roughly) the following code:<br><br><div style="margin-left: 40px;">data Foo e<br>type MFoo e = Maybe (Foo e)<br><br>instance Ord e =&gt; Monoid (Foo e) where<br><div style="margin-left: 40px;">f1 `mappend` f2 = &lt;code invoking the mappend instance from Maybe (Foo e)&gt;<br>
</div></div><div style="margin-left: 40px;"><br></div>I&#39;d expect this to optimize to the same thing as if I had implemented:<br><div style="margin-left: 40px;">meld :: Ord e =&gt; Foo e -&gt; Foo e -&gt; Foo e<br>f1 `meld` f2 = -- code invoking meld&#39;<br>
<br>meld&#39; :: Ord e =&gt; Maybe (Foo e) -&gt; Maybe (Foo e) -&gt; Maybe (Foo e)<br>meld&#39; (Just f1) (Just f2) = meld f1 f2<br>meld&#39; m1 Nothing = m1<br>meld&#39; Nothing m2 = m2<br><br>instance Ord e =&gt; Monoid (Foo e) where<br>
</div><div style="margin-left: 80px;">&nbsp;mappend = meld<br></div><br>However, GHC&#39;s Core output tells me that the first piece of code reexamines the polymorphism in every recursion, so that mappend, which is used in the Monoid instance of Foo, looks up the Monoid instance of Foo <i>again</i> (for the sole purpose of looking itself up) and recurses with that.&nbsp; Why is this, and is there a way to fix that?<br>
<br><br clear="all">Louis Wasserman<br><a href="mailto:wasserman.louis@gmail.com">wasserman.louis@gmail.com</a><br>