Should we add Namespaces to Haskell?

Have you seen the Local modules GHC proposal? It is an interesting idea, but there are a lot of small details that need to be worked out.

Also, namespaces wouldn’t fix orphans. Imagine this code:

data X = A | B deriving (Show, Eq)

namespace O1 where
  instance Ord X where
    A <= _ = True
    x <= y = x == y

namespace O2 where
  instance Ord X where
    B <= _ = True
    x <= y = x == y

main = do
   let myset = use O1::() in Set.fromList [A,B]
   let x = use O2::() in Set.findMin myset
   print x

Will that print A or B?

Being able to locally use different instances breaks type class coherence and means among other things that you cannot implement polymorphic sets using balanced trees.

2 Likes