Qualified names

From HaskellWiki
Jump to navigation Jump to search

http://www.dcs.gla.ac.uk/mail-www/haskell/msg02004.html

Please make more use of the qualified ID syntax! Haskell has had this feature for quite some time now, but I hardly ever see it used ...

Especially in modules that import a lot of other modules, it's

  • easier to recognize an identifier if it's prefixed by a module ID rather than look through all the imported modules, and
  • easier to modify a module if all you need to do to pull in a new value is to use it, rather than scrolling up, and adding it to the list of identifiers in something like import M (...).

This is standard practice in both SML and Ocaml. (In fact, I would rather not have to declare things like import qualified M at all ...)

If you have a long module name, declare an alias:

import qualified LongModuleName as LMN

BTW, another advantage of this syntax is that identifiers within their own defining module get shorter, and consequently it gets easier to read. For example, don't define addToFM; define addTo and then do import FiniteMap as FM. Sometimes this means having to hide Prelude identifiers and qualify them at use (as it would be with lookupFM in GHC's FiniteMap module) but is that such a great price to pay...?

Please. Pretty please? Pretty please with sugar on top?

--FC

P.S. Except infix combinators. These are ugly when you qualify them.


One good example of qualified names, one of the Base64 modules has functions named encode and decode. Those are the most sensible names, but are very likely to clash or shadow other functions if directly imported. If you import qualified Base64 then you can write Base64.decode and everyone's happy.


I use this extensively in an even more rigorous way. In the style of Modula-3 I define one data type or one type class per module. The module is named after the implemented type or class. Then a type is named T, and a type class C. I use them qualified, e.g. Music.T or Collection.C. Similarly, if a type has only one constructor then I call it Cons and use it qualified MidiFile.Cons. This style also answers the annoying question whether the module name should be in singular or plural form: Always choose singular form!

Cons is going to confuse the heck out of those like myself who still think in Lisp sometimes. Would Con be ok instead? -- BartMassey
I thought the similarity to LISP is good. -- HenningThielemann

Many functions can be considered as conversions. So I define functions like Music.toMIDI and Music.fromMIDI and use it in this qualified form. This way I can import all modules qualified, avoiding name clashes and I can provide a consistent naming through all modules. Since Haskell's direction of processing in functions (except monad notation) is from right-to-left, the "from" names are preferable. The expression a = A.fromB b is certainly nicer than a = B.toA b. However, the order of functions and their arguments is subject of criticism.

The big question remains: In which module shall the conversions reside? Translated to our example: Shall it be Music.toMIDI or MIDI.fromMusic? I suggest the following: Find out which module is the general one, and which is the more specific one. I consider the more specific module as an extension to the general module. When you write a general module you cannot predict all possible extensions. That's why you should put all conversions into the extension modules. How to find out which module is more specific? Imagine module A and module B still don't contain any conversion routine between A.T and B.T. If module B imports A, then B is the more specific one. Put all conversions between A and B into B. If module A and B are mutually recursive or don't import each other, then rethink if one or the other is the more specific one. If they are on the same level of generality you may add a new module dedicated to conversion between A.T and B.T.

-- HenningThielemann