GHC/QualifiedModuleExport
Motivation[edit]
There are a number of libraries in the Haskell ecosystem which encourage qualified imports to avoid name clashes with other modules. For example:
-- Import the ByteString and Text types un-qualified.
import Data.ByteString (ByteString)
import Data.Map.Strict (Map)
import Data.Text (Text)
-- Import ByteString and Text modules qualified to avoid name collisions.
import qualified Data.ByteString.Char8 as BS
import qualified Data.Map.Strict as Map
import qualified Data.Text as T
In larger projects the above lines will need to be added to every file that uses ByteString
, Map
and Text
.
The problem for the types can easily be fixed by creating a Prelude module which does the following:
module MyPrelude
( Text
, Map
, ByteString
) where
import Data.ByteString (ByteString)
import Data.Map.Strict (Map)
import Data.Text (Text)
and then importing MyPrelude to access the types. Unfortunately there is currently no way to add qualified module exports to MyPrelude
and have those qualified names available to modules that import MyPrelude
.
Background[edit]
The following is a list of previous documents and discussions about Haskell's module system:
- Iavor S. Diatchki et al's formal specification of the Haskell 98 module system: http://purely-functional.net/yav/publications/modules98.pdf
- Module system in the Haskell 98 report: http://www.haskell.org/onlinereport/modules.html (code at: https://github.com/yav/module-98 )
- Module system in the Haskell 2010 report: http://www.haskell.org/onlinereport/haskell2010/haskellch5.html
- A thread from the haskell-cafe mailing list: http://www.haskell.org/pipermail/haskell-cafe/2005-March/thread.html
- A related proposal for nested modules: http://osdir.com/ml/glasgow-haskell-users@haskell.org/2012-01/msg00171.html
The Proposal[edit]
This proposal extends the export mechanism so that the following is possible:
module MyPrelude
( Text
, Map
, ByteString
, qualified module BS
, qualified module Map
, qualified module T
) where
import Data.ByteString (ByteString)
import Data.Map.Strict (Map)
import Data.Text (Text)
import qualified Data.ByteString.Char8 as BS
import qualified Data.Map.Strict as Map
import qualified Data.Text as T
Files which originally had 6 lines of imports can now replace those 6 lines with a single import of MyPrelude
.