<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"><<a href="mailto:ryani.spam@gmail.com">ryani.spam@gmail.com</a>></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 "transcode" 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 <<a href="mailto:arun.suresh@gmail.com">arun.suresh@gmail.com</a>>:<br>
<div class="Ih2E3d">> class Foo a where<br>
> fooFunc :: a -> Int<br>
><br>
> data FooData = FData<br>
><br>
> instance Foo FooData where<br>
> fooFunc _ = 10<br>
<br>
</div>So far so good.<br>
<div class="Ih2E3d"><br>
> class Bar a where<br>
> barFunc :: (Foo b) => a -> b -> Int<br>
<br>
</div>So now, barFunc advertises "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". You can make an implementation of<br>
this that has different functionality based on "a", but it needs to be<br>
polymorphic in "b", taking any type "b" which is a member of Foo.<br>
<div class="Ih2E3d"><br>
> data BarData = BData<br>
><br>
> instance Bar BarData where<br>
> barFunc _ FData = 20<br>
<br>
</div>Uh oh! FData is of type FooData! 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. It doesn't matter that FooData is the only<br>
declared member at this point. 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. Right now that<br>
just means generically polymorphic functions (id, const, etc.), and<br>
the fooFunc function inside the "Foo" typeclass.<br>
<br>
For example, this declaration would work:<br>
<br>
instance Bar BarData where<br>
barFunc _ f = 10 + fooFunc f<br>
<div class="Ih2E3d"><br>
> When I compile I get this :<br>
> Couldn't match expected type `b' against inferred type `FooData'<br>
> `b' is a rigid type variable bound by<br>
> the type signature for `barFunc' at Sample.hs:16:20<br>
> In the pattern: FData<br>
> In the definition of `barFunc': barFunc _ FData = 20<br>
> In the definition for method `barFunc'<br>
<br>
</div>The compiler is just telling you what I just told you: barFunc says<br>
that it should take any type "b", but the pattern FData constrains the<br>
argument to be a FooData.<br>
<div class="Ih2E3d"><br>
> Think Im missing something really big...<br>
> 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> </div><div>Thanks... <br>Arun<br> </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>
-- ryan<br>
</font></blockquote></div><br><br></div>