<div dir="ltr">Hello Ryan..<br><br><div class="gmail_quote">On Mon, Oct 13, 2008 at 3:33 PM, Ryan Ingram <span dir="ltr">&lt;<a href="mailto:ryani.spam@gmail.com">ryani.spam@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Step 1: Forget everything you know about OO classes, then try again :)</blockquote><div><br>you seem to have read my mind [:)].. i actaually hit upon this issue while trying to &quot;transcode&quot; some C++ to Haskell.. <br>
</div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><br>
<br>
2008/10/13 Arun Suresh &lt;<a href="mailto:arun.suresh@gmail.com">arun.suresh@gmail.com</a>&gt;:<br>
<div class="Ih2E3d">&gt; class Foo a where<br>
&gt; &nbsp; &nbsp; fooFunc :: a -&gt; Int<br>
&gt;<br>
&gt; data FooData = FData<br>
&gt;<br>
&gt; instance Foo FooData where<br>
&gt; &nbsp; &nbsp; fooFunc _ = 10<br>
<br>
</div>So far so good.<br>
<div class="Ih2E3d"><br>
&gt; class Bar a where<br>
&gt; &nbsp; &nbsp; barFunc :: (Foo b) =&gt; a -&gt; b -&gt; Int<br>
<br>
</div>So now, barFunc advertises &quot;for any type a which is in the Bar<br>
typeclass, and any type b which is in the Foo typeclass, I can give<br>
you a function from a to b to Int&quot;. &nbsp;You can make an implementation of<br>
this that has different functionality based on &quot;a&quot;, but it needs to be<br>
polymorphic in &quot;b&quot;, taking any type &quot;b&quot; which is a member of Foo.<br>
<div class="Ih2E3d"><br>
&gt; data BarData = BData<br>
&gt;<br>
&gt; instance Bar BarData where<br>
&gt; &nbsp; &nbsp; barFunc _ FData = 20<br>
<br>
</div>Uh oh! &nbsp;FData is of type FooData! &nbsp;But we just advertised that we<br>
could take *anything* which is in the typeclass Foo, which can include<br>
any number of other types. &nbsp;It doesn&#39;t matter that FooData is the only<br>
declared member at this point. &nbsp;What that means is that if you want to<br>
do anything with the second argument, you can only use functions that<br>
accept any type which is a member of the Foo class. &nbsp;Right now that<br>
just means generically polymorphic functions (id, const, etc.), and<br>
the fooFunc function inside the &quot;Foo&quot; typeclass.<br>
<br>
For example, this declaration would work:<br>
<br>
instance Bar BarData where<br>
 &nbsp; &nbsp;barFunc _ f = 10 + fooFunc f<br>
<div class="Ih2E3d"><br>
&gt; When I compile I get this :<br>
&gt; &nbsp;Couldn&#39;t match expected type `b&#39; against inferred type `FooData&#39;<br>
&gt; &nbsp; &nbsp; &nbsp; `b&#39; is a rigid type variable bound by<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; the type signature for `barFunc&#39; at Sample.hs:16:20<br>
&gt; &nbsp; &nbsp; In the pattern: FData<br>
&gt; &nbsp; &nbsp; In the definition of `barFunc&#39;: barFunc _ FData = 20<br>
&gt; &nbsp; &nbsp; In the definition for method `barFunc&#39;<br>
<br>
</div>The compiler is just telling you what I just told you: barFunc says<br>
that it should take any type &quot;b&quot;, but the pattern FData constrains the<br>
argument to be a FooData.<br>
<div class="Ih2E3d"><br>
&gt; Think Im missing something really big...<br>
&gt; Could somebody kindly help me out here...<br>
<br>
</div>I recommend reading <a href="http://www.haskell.org/haskellwiki/OOP_vs_type_classes" target="_blank">http://www.haskell.org/haskellwiki/OOP_vs_type_classes</a></blockquote><div>&nbsp;</div><div>Thanks... <br>Arun<br>&nbsp;</div>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><font color="#888888"><br>
 &nbsp;-- ryan<br>
</font></blockquote></div><br><br></div>