Sunday, October 11, 2009

Weird Haskell import behavior

Say you have a module A:

module A where

class A a where
  a :: a -> a

Now you wish to write a module B that instantiates A.A.
Since you don't want to include the whole namespace of A, you try

module B where

import A(A)

instance A Int where
  a = id

This fails to compile because it says 'a' is not a visible method of class A.  The strange thing is it compiles if you instead write

module B where

import qualified A
import A(A)

instance A Int where
  a = id


I find this strange since you don't have to write

instance A Int where
  A.a = id

Importing with brackets brings only those names into scope, thus making a not
visible.  But I fail to see why importing all of A qualified allows you to figure
out that a (unqualified) is visible.

No comments:

Post a Comment