ely
ely

Reputation: 77404

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)

Upvotes: 14

Views: 5760

Answers (3)

Zeta
Zeta

Reputation: 105876

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.

Upvotes: 21

genisage
genisage

Reputation: 1169

Despite the fact that it's not mentioned on https://www.haskell.org/haskellwiki/Import, import qualified Foo as Bar (x, y) seems to work fine for me. I'm running ghc 7.6.3. Maybe that particular wiki page is outdated. If it doesn't work for you, what version of ghc are you using?

Upvotes: 5

dfeuer
dfeuer

Reputation: 48580

This is allowed, at least according to the Haskell 2010 Report. First see the examples, which include this one:

import qualified A(x)

Then look up to the actual syntax spec, which indicates that the qualified, as, and "impspec" (the list of imported identifiers or the list of hidden identifiers) are all optional and independent. Thus the syntax genisage describes is actually standard.

Upvotes: 5

Related Questions