14

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)
ely
  • 74,674
  • 34
  • 147
  • 228
  • 2
    From https://www.haskell.org/haskellwiki/Import it looks like this is impossible, but I'm not sure what good it would do anyway. Since you're allowed to specify your own prefix it's as short as you like, and there's no risk of conflict with any other modules. What's the harm in just `import qualified Data.List as List`? The only difference is that `List.intercalate` will be available: as long as you never type that, you won't know the difference, so to speak. – amalloy Jan 19 '15 at 03:44
  • 3
    You can also just create an auxiliary module which simply re-exports `sort` and `intersperse` and then do a qualified import of that module. – ErikR Jan 19 '15 at 03:58
  • 1
    @amalloy "There's no risk of conflict with any other modules" is not necessarily true. You can import as many modules with the same name as you like, and I could imagine wanting to have `A.map` from `Prelude` but `A.foldr` from `Data.Foldable`, for example, hence introducing many name clashes. – Daniel Wagner Jan 19 '15 at 15:39

3 Answers3

21
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.

Zeta
  • 103,620
  • 13
  • 194
  • 236
  • When I use the first method, such as `import qualified Data.Map as Map (findWithDefault)`, I am still able to see things like `Data.Map.notMember` in GHCi. Nothing is visible as `Map.*` except for `Map.findWithDefault` but it seems to also import all of Data.Map as well. – ely Jan 19 '15 at 12:40
  • 2
    @prpl.mnky.dshwshr, GHCi is weird about modules in general, because it's trying to be friendly to interactive development. If you want to know what you actually need, compile the code. – dfeuer Jan 19 '15 at 14:17
5

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?

genisage
  • 1,159
  • 7
  • 17
  • When I use the first method, such as `import qualified Data.Map as Map (findWithDefault)`, I am still able to see things like `Data.Map.notMember` in GHCi. Nothing is visible as `Map.*` except for `Map.findWithDefault` but it seems to also import all of Data.Map as well. – ely Jan 19 '15 at 12:42
  • @prpl.mnky.dshwshr The same thing happened with me. But I noticed that even with no imports, I could use functions with fully qualified names. Perhaps another question would be if there's a way to turn that off in GHCi. – genisage Jan 19 '15 at 21:24
5

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.

Community
  • 1
  • 1
dfeuer
  • 48,079
  • 5
  • 63
  • 167