Hi all, I am pretty new to haskell, but while exploring the haskell module system, I came along some questions. As far as I found out, haskell supports separated namespaces, i.e. every module has its own symbol space. Thus, when defining a class in one module like module A(Foo(f)) where class Foo a where f :: a -> Integer one should be able to define two instances having the same signature, as long as they are in different namespaces module B(bar) where import A(Foo(f)) instance Foo Integer where f x = x bar x = f x module C(tango) import A(Foo(f)) instance Foo Integer where f x = x + 1 tango x = f x As the integer instances of Foo are in separate modules and thus namespaces, one should be able to just import bar and tango module Main(main) import B(bar) import C(tango) main = print( (bar 5) + (tango 4) ) But now, ghc complains about two instances of Foo Integer, although there should be none in the namespace main. I have not found any documentation on why ghc behaves like this and whether this conforms to the haskell language specification. Is there any haskell compiler out there that is able to compile the above example? Thanks for your help Stephan
Stephan Herhut <S.A.Herhut@herts.ac.uk> writes:
module B(bar) where instance Foo Integer where
module C(tango) instance Foo Integer where
import B(bar) import C(tango)
But now, ghc complains about two instances of Foo Integer, although there should be none in the namespace main.
I suspect the problem is that instances are always exported and imported, so that GHC sees both in Main, and complains. Perhaps this could be relaxed to allow your situation (where the class isn't used directly in Main anyway)? -kzm -- If I haven't seen further, it is by standing in the footprints of giants
one should be able to define two instances having the same signature, as long as they are in different namespaces [snip] But now, ghc complains about two instances of Foo Integer, although there should be none in the namespace main.
It's a Haskell problem, not a ghc one. Class instances are not constrained by module boundaries. Other people have found this to be a problem, e.g. in combination with tools like Strafunski - you just cannot encapsulate a class instance in a module. It's a design flaw in Haskell.
I have not found any documentation on why ghc behaves like this and whether this conforms to the haskell language specification. Is there any haskell compiler out there that is able to compile the above example?
I think Clean (a very similar language) permits to limit the scope of class instances. Stefan
participants (3)
-
Ketil Malde -
S.M.Kahrs -
Stephan Herhut