[Haskell-cafe] Overlapping instances: are functions and typeclass methods treated differently?
norc foobar
norc.foobar at gmail.com
Tue Oct 13 00:27:31 UTC 2020
Hi Daniel,
The constraint on an instance declaration is, perhaps counterintuitively,
discharged at the usage site rather than the definition site.
foo :: Show a => Wrapper a -> String
foo (Wrapper a) = show a
will produce the aforementioned diagnostic.
---
Victor
On Fri, Oct 9, 2020 at 2:51 PM Daniel Díaz <diaz.carrete at gmail.com> wrote:
> On Thu, Oct 8, 2020 at 2:12 PM Henning Thielemann <
> lemming at henning-thielemann.de> wrote:
>
>>
>> On Thu, 8 Oct 2020, Daniel Díaz wrote:
>>
>> > But now consider this instance:
>> >
>> > > newtype Wrapper a = Wrapper (Maybe a)
>> > > instance Show a => Show (Wrapper a) where
>> > > show (Wrapper x) = show x
>> >
>> > This compiles just fine, despite—to my mind—suffering from the exact
>> same problem that the "foo" function had.
>> > Why does it work?
>>
>> There is an instance Show (Maybe a) in Prelude, but no instance Show
>> (Wrapper a), so no overlapping.
>
>
> Sorry, I wasn't very clear there.
>
> The problem is not with the "Show (Wrapper a)" instance itself, but with
> its implementation of "show". It depends (just as the function "foo" did)
> on "Show (Maybe a)", for which there are overlapping instances.
>
> And yet "foo" compiles without problem, while the typeclass method
> doesn't.
>
> _______________________________________________
> Haskell-Cafe mailing list
> To (un)subscribe, modify options or view archives go to:
> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
> Only members subscribed via the mailman list are allowed to post.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20201013/3b8ced44/attachment.html>
More information about the Haskell-Cafe
mailing list