Re: [Haskell-cafe] Overlapping instances: are functions and typeclass methods treated differently?

On Thu, Oct 8, 2020 at 2:12 PM Henning Thielemann < lemming@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.

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
On Thu, Oct 8, 2020 at 2:12 PM Henning Thielemann < lemming@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.

That's not the same, because you show what's under the wrapper, rather than
the wrapped value.
On Mon, Oct 12, 2020, 8:28 PM norc foobar
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
wrote: On Thu, Oct 8, 2020 at 2:12 PM Henning Thielemann < lemming@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.
_______________________________________________ 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.
participants (3)
-
Daniel Díaz
-
David Feuer
-
norc foobar