Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is importing haskell module as qualified a good practice?

Tags:

haskell

I know import qualified names has benefit of avoiding name conflicts. I'm asking purely from readability point of view.

Not familiar with haskell standard libraries, one thing I found annoying when reading haskell code (mostly from books and tutorials online) is that when I come across a function, I don't know if it belongs to a imported module or will be defined by user later.

Coming from a C++ background, it's usually seen as a good practice to call standard library function with namespace, for example std::find. Is it the same for haskell? If not then how do you overcome the problem that I mentioned above?

like image 461
swang Avatar asked May 08 '14 10:05

swang


People also ask

What is qualified in Haskell?

The keyword qualified means that symbols in the imported modules are not imported into the unqualified (prefixless) namespace.

How do I import a Haskell module?

The syntax for importing modules in a Haskell script is import <module name>. This must be done before defining any functions, so imports are usually done at the top of the file. One script can, of course, import several modules. Just put each import statement into a separate line.

What is Haskell Prelude?

From HaskellWiki. Prelude is a module that contains a small set of standard definitions and is included automatically into all Haskell modules.


2 Answers

From Haskell style guide:

Always use explicit import lists or qualified imports for standard and third party libraries. This makes the code more robust against changes in these libraries. Exception: The Prelude.

So, the answer is yes. Using qualified import is considered a good practice for standard and third party libraries except the Prelude. But for infix function with symbols (something like <|*|>) you may want to import it explicitly as qualified import doesn't look nice on it.

like image 129
Sibi Avatar answered Oct 16 '22 09:10

Sibi


I'm not too fond of qualified names, IMO they rather clutter the code. The only modules that should always be imported qualified are those that use names clashing with prelude functions – these normally have an explicit recommendation for doing so, in the documentation.

For widespreadly used modules such as Control.Applicative, there's not much reason not to import unqualified; most programmers should know all that's in there. For modules from less well-known packages that do something very specific, or to avoid clashes of a single name, you can use an explicit import list, e.g. import Data.List (sortBy), import System.Random.Shuffle (shuffleM) – this way, you don't have to litter your code with qualifiers, yet looking up an identifier in the imports section tells you immediately where it comes from (this is analogous to using std::cout;). But honestly, I find it even more convenient to just load the module into ghci and use

*ClunkyModule> :i strangeFunction

to see where it's defined.

There's one good point to be made about qualified imports or explicit import lists that I tend to neglect: they make your packages more future-proof. If a new version of some module stops exporting an item you need, or another module introduces a clashing name, then an explicit import will immediately point you to the problem.

like image 6
leftaroundabout Avatar answered Oct 16 '22 08:10

leftaroundabout