Search code examples
haskellimportqualified-name

Haskell *qualified* import of a set of functions


In Haskell, I can import a module qualified by its name or a shortcut name, like so:

import qualified Data.List as List
import qualified Data.Map

I can also import only a select set of functions from a module, or import all functions other than a select set, like so:

import Data.List (sort, intersperse)
import Data.Map hiding (findWithDefault)

Is it possible to import a specific set of functions, like in the import Data.List (sort, intersperse) example above, but to ensure the functions are still identified in a qualified way, such as List.sort and List.intersperse?

Though this does not work, it is the spirit of what I am asking:

import qualified Data.List (sort, intersperse) as List

or perhaps

import qualified Data.List as List (sort, intersperse)

Solution

  • import qualified Data.List as List (sort, intersperse)
    

    This is actually fine and works. The grammar of an import declaration is as follows:

    5.3 Import Declarations

    impdecl   →   import [qualified] modid [as modid] [impspec]
    

    qualified and as do not prevent an import specification. This isn't a Haskell2010 addition, as it has been part of the Haskell 98 report.

    On the other hand your first example

    import qualified Data.List (sort, intersperse) as List
    --     qualified           impspec!            as modid
    --                            ^                    ^         
    --                            +--------------------+
    

    doesn't follow the grammar, as the impspec must be the last element in an import declaration if it's provided.