<div dir="ltr">Sylvain Henri's link did the trick, that's what i was looking for (they call it generic implementation).<div><br></div><div>in terms of terminology, is there an intrinsic difference between generic and polymorphic?</div></div><div class="gmail_extra"><br><div class="gmail_quote">2017-06-29 20:10 GMT+02:00 Silent Leaf <span dir="ltr"><<a href="mailto:silent.leaf0@gmail.com" target="_blank">silent.leaf0@gmail.com</a>></span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">typos in my previous message (including in the code), don't pay attention, any error is an error :P</div><div class="HOEnZb"><div class="h5"><div class="gmail_extra"><br><div class="gmail_quote">2017-06-29 20:08 GMT+02:00 Silent Leaf <span dir="ltr"><<a href="mailto:silent.leaf0@gmail.com" target="_blank">silent.leaf0@gmail.com</a>></span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">you're right my example is more like Show, it's because i change two times the order of input and output, and did not check at the end if it was what i wanted...<div><br></div><div>Actually i did not mean constraints on instances, especially thereafter ad-hoc polymorphic instances.</div><div>I rather meant what i said, as in default implementation (as in, right inside the body of the class).</div><div>like that:</div><span><div><br></div><div>class Foo a where</div><div> bar :: a -> String</div></span><div> bar :: Show a => a -> String</div><div> bar = show -- default implementation: in other terms, if you define an instance without defining this method</div><div><br></div><div>the idea would be then that if you have Foo (), you can either implement a special version, or leave it to show. mind you i'm not even sure we can define an instance without any specific implementation of method?</div><div><br></div><div>--that would be allowed:</div><div>instance Show => Foo () where</div><div> -- nothing, if it's even legal by default</div><div><br></div><div>-- and of course that would be allowed to if someone wanted a special function bar :: Foo () => () -> String</div><div>instance Foo () where</div><div> bar = ....</div><div><br></div><div>obviously, i do not mean *both* instances of Foo (), just, one or the other. it would merely be a way to implement ad-hoc polymorphism onto *default implementations of methods*, that is, those inside the body of the class.</div><div><br></div><div><br></div></div><div class="m_3194375983053602785HOEnZb"><div class="m_3194375983053602785h5"><div class="gmail_extra"><br><div class="gmail_quote">2017-06-29 19:45 GMT+02:00 David McBride <span dir="ltr"><<a href="mailto:toad3k@gmail.com" target="_blank">toad3k@gmail.com</a>></span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">This is a common mistake that people who try to use type classes run<br>
into. I remember banging my head against it pretty hard when I first<br>
started out.<br>
<br>
There's this temptation that you should be able to write the following:<br>
<span><br>
class Foo a where<br>
bar :: a -> String<br>
<br>
</span>instance Read a => Foo a where<br>
bar a = read a<br>
<br>
instance Foo () where<br>
bar _ = "bar"<br>
<br>
But the problem with that is that now () applies to two conflicting<br>
classes. It is both a Read and a Foo. So when you go bar (), which<br>
instance should fire? The Foo instance or the Read () => Foo<br>
instance?<br>
<br>
There are a multitude of ways you could try to resolve this. Let's<br>
say obviously the Read constrainted instance is more specific, we<br>
should use that. But then what if the user of your library happens to<br>
have a instance Ord a => Foo a in his library, now which one of those<br>
is more specific? Read or Ord?<br>
<br>
Because of all these ambiguities during type checking ghc doesn't even<br>
look at the constraint. It would see instance Foo a, and instance Foo<br>
(), and then say oh! those are overlapping instances because () could<br>
apply to either class before you consider what constraints apply.<br>
<br>
There's actually several very in depth answers on stackoverflow for<br>
this questions like this, such as this one:<br>
<a href="https://stackoverflow.com/a/3216937/1500583" rel="noreferrer" target="_blank">https://stackoverflow.com/a/32<wbr>16937/1500583</a> It might give you some<br>
ideas on what to do about this.<br>
<div class="m_3194375983053602785m_1431327136822582118HOEnZb"><div class="m_3194375983053602785m_1431327136822582118h5"><br>
On Thu, Jun 29, 2017 at 1:15 PM, Silent Leaf <<a href="mailto:silent.leaf0@gmail.com" target="_blank">silent.leaf0@gmail.com</a>> wrote:<br>
> hi,<br>
><br>
> say i have the following typeclass:<br>
><br>
> class Foo a where<br>
> bar :: a -> String<br>
><br>
> looks a lot like the Read typeclass, right? (at least i think it should?)<br>
> well say it's a different meaning (in other terms i can't or do not want to<br>
> use Read, but i'd like to implement a default version of bar for those<br>
> instances that also implement Read. is there a way to do so?<br>
><br>
</div></div><div class="m_3194375983053602785m_1431327136822582118HOEnZb"><div class="m_3194375983053602785m_1431327136822582118h5">> ______________________________<wbr>_________________<br>
> Beginners mailing list<br>
> <a href="mailto:Beginners@haskell.org" target="_blank">Beginners@haskell.org</a><br>
> <a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-bi<wbr>n/mailman/listinfo/beginners</a><br>
><br>
______________________________<wbr>_________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org" target="_blank">Beginners@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-bi<wbr>n/mailman/listinfo/beginners</a><br>
</div></div></blockquote></div><br></div>
</div></div></blockquote></div><br></div>
</div></div></blockquote></div><br></div>