
Hi, I have a test file: module Main where class A a where doSomething :: a -> IO () class B b where doMore :: b -> IO () instance B b => A b where doSomething = doMore instance A String where doSomething = putStrLn main = doSomething "Hello, World! So there is a class A, and a class B. If something is part of B it is automatically part of A (so A is kind of a superclass to B). But String is just part of A. I try to compile it with: ghc Test.hs -XFlexibleInstances -XUndecidableInstances I get: Test.hs:14:8: Overlapping instances for A [Char] arising from a use of `doSomething' Matching instances: instance B b => A b -- Defined at Test.hs:8:10 instance A String -- Defined at Test.hs:11:10 In the expression: doSomething "Hello, World!" In an equation for `main': main = doSomething "Hello, World!" Why are the instances overlapping? String is not part of B??? How can I do this? Thanks! Nathan

On Tue, Dec 25, 2012 at 10:52 AM, Nathan Hüsken
instance B b => A b where doSomething = doMore
This doesn't quite do what you think; it matches *all* types, then afterward applies the context. Your terminology suggests you're trying to do OOP with typeclasses. Don't; they're not OOP, and treating them like they are leads only to grief. -- brandon s allbery kf8nh sine nomine associates allbery.b@gmail.com ballbery@sinenomine.net unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net

On 12/25/2012 05:05 PM, Brandon Allbery wrote:
On Tue, Dec 25, 2012 at 10:52 AM, Nathan Hüsken
mailto:nathan.huesken@posteo.de> wrote: instance B b => A b where doSomething = doMore
This doesn't quite do what you think; it matches *all* types, then afterward applies the context.
Your terminology suggests you're trying to do OOP with typeclasses. Don't; they're not OOP, and treating them like they are leads only to grief.
Well, you are completely right. The reason is, that I am trying to write FFI for a OOP library.

On Tue, Dec 25, 2012 at 11:46 PM, Nathan Hüsken
The reason is, that I am trying to write FFI for a OOP library.
You might find this useful then: http://stackoverflow.com/questions/4029455/is-there-a-haskell-equivalent-of-... -- Kim-Ee

On Dienstag, 25. Dezember 2012, 16:52:00, Nathan Hüsken wrote:
Hi,
I have a test file:
module Main where
class A a where doSomething :: a -> IO () class B b where doMore :: b -> IO ()
instance B b => A b where doSomething = doMore
instance A String where doSomething = putStrLn
main = doSomething "Hello, World!
So there is a class A, and a class B. If something is part of B it is automatically part of A (so A is kind of a superclass to B). But String is just part of A. I try to compile it with:
ghc Test.hs -XFlexibleInstances -XUndecidableInstances
I get:
Test.hs:14:8: Overlapping instances for A [Char] arising from a use of `doSomething' Matching instances: instance B b => A b -- Defined at Test.hs:8:10 instance A String -- Defined at Test.hs:11:10 In the expression: doSomething "Hello, World!" In an equation for `main': main = doSomething "Hello, World!"
Why are the instances overlapping? String is not part of B???
For instance resolution, only the instance head is taken into account, that does not include the constraints. So the instance head of instance B b => A b where is only the `b' type variable, and that matches every type, in particular String, so the instances overlap (with an instance of the form instance context => Class a where any further instance declaration overlaps, since that instance effectively says every type is an instance of Class, but if `context' isn't satisfied, that is a static error).
How can I do this?
Allow overlapping instances if you must. But maybe you can restructure your code to not use overlapping instances.
participants (4)
-
Brandon Allbery
-
Daniel Fischer
-
Kim-Ee Yeoh
-
Nathan Hüsken