Module Management in Haskell
Table of Contents
1. Module Management and Interface
We can control what to expose in the brackets after module name. For example,
module ModuleName ( export1, export2 ) where
2. Module Architecture
Module names usually have their first letter capitalized, e.g., Data.List. The dots in the module names refer to folder architecture.
The root directory of modules can be configured in cabal as hs-source-dirs: src.
3. Importing Module
Suppose in Module, we have functions foo, bar and datatype Maybe with constrcutors Just, Nothing.
3.1. Import to Global Namespace
We can directly import, this will import all symbols to the global namespace, which may cause issues.
import Module
-- Then we can use directly
foo ...
3.2. Import Namespaced
We can import with namespace isolation, using qualified keyword.
import qualified Module
-- Then we can invoke with namespace
Module.foo
3.3. Import with Aliases
Even with qualified, we can use as keyword to give it an alias for convenience.
import qualified Module as M
-- Then we can invoke functions with alias
M.foo
3.4. Selective Import
We can import only part of the module.
import Module (foo)
-- we can only use foo, not bar
foo ...
Notably, if we want to selective import a type/typeclass, we have to import all constructors.
import Module (Maybe(..)) -- use .. to import all data constructors
Of course, we can choose not to expose any constructors or expose part of the constructors, so as to implement abstract data type to provide data security.