Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)
like image 750
ely Avatar asked Jan 19 '15 03:01

ely


People also ask

How do I import a function into Haskell?

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 qualified in Haskell?

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

What is Prelude in Ghci?

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


1 Answers

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.

like image 195
Zeta Avatar answered Oct 22 '22 23:10

Zeta