What does 'qualified' mean in 'import qualified Data.List' statement?

Haskell

Haskell Problem Overview


I understand import Data.List.

But what does qualified mean in the statement import qualified Data.List?

Haskell Solutions


Solution 1 - Haskell

A qualified import makes the imported entities available only in qualified form, e.g.

import qualified Data.List

result :: [Int]
result = Data.List.sort [3,1,2,4]

With just import Data.List, the entities are available in qualified form and in unqualified form. Usually, just doing a qualified import leads to too long names, so you

import qualified Data.List as L

result :: [Int]
result = L.sort [3,1,2,4]

A qualified import allows using functions with the same name imported from several modules, e.g. map from the Prelude and map from Data.Map.

Solution 2 - Haskell

If you do an unqualified import (the default), you can refer to anything imported just by its name.

If you do a qualified import, you have to prefix the name with the module it's imported from.

E.g.,

import Data.List (sort)

This is an unqualified import. You can now say either sort or Data.List.sort.

import qualified Data.List (sort)

This is a qualified import. Now sort by itself doesn't work, and you have to say Data.List.sort.

Because that's quite lengthy, normally you say something like

import qualified Data.List (sort) as LS

and now you can write LS.sort, which is shorter.

Solution 3 - Haskell

The keyword qualified means that symbols in the imported modules are not imported into the unqualified (prefixless) namespace. They are only available with their full qualified name. For instance, foldr' has the unqualified name foldr' and the qualified name Data.List.foldr'.

One uses qualified imports to prevent namespace pollution. It is also possible to use import qualified Foo as Bar, which imports from Foo but with names as if the import stems from Bar. For instance, if you type import qualified Data.List as L, you can use foldr' as L.foldr'.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionOptimightView Question on Stackoverflow
Solution 1 - HaskellDaniel FischerView Answer on Stackoverflow
Solution 2 - HaskellMathematicalOrchidView Answer on Stackoverflow
Solution 3 - HaskellfuzView Answer on Stackoverflow