[Haskell] Annoying naming clashes

André Pang ozone at algorithm.com.au
Mon Jun 14 21:21:47 EDT 2004

On 12/06/2004, at 9:52 AM, Ben.Yu at combined.com wrote:

> Hi, when writing haskell code. It is so annoying that name clashes keep
> happening.
> I have to be careful about the data constructor names, about the class
> names, about the class member names.
> I understand that we can use class to achieve some overloading effect.
> However, it does not help with data constructors. And, what's more
> important, it looks like Haskell does not allow two classes sharing 
> methods
> of the same name.

The standard response to this is "use the module system to help you".  
Don't export your data constructors because you then expose the 
internals of your module, which is bad for abstraction; export function 
names which serve as the data constructors instead.

Use qualified module names.  e.g. in the case of the Data.FiniteMap 
module, it has function names such as "emptyFM", "unitFM", "addToFM", 
etc.  Don't do this, instead call those functions "empty", "unit", 
"add", and then a user of a module can use the module by using 
qualified module names:

     module MyModule where

     import Data.FiniteMap as FM

     foo = FM.add key elt FM.empty

You can 'wrap' the current Data.FiniteMap module with your own 
MyFiniteMap module to achieve this affect.

I think this is a far from ideal solution, but it's adequate.  Google 
for "per-type function namespaces", which was an idea I sent to the 
list a few months ago, for something which I think better solves the 
naming issue.  Quite a lot of people disagreed with it, but I still 
think it's a good idea.  (FWIW, I nearly managed to implement this with 
Template Haskell, but I couldn't work around the restriction that you 
couldn't splice in a TH function declared in the same module.)

% Andre Pang : trust.in.love.to.save

More information about the Haskell mailing list