Is it possible to rename current module ? Let's say I want to introduce data constructors Left and Right, but these would clash with Prelude.Left and Right. I don't want to write everywhere MyModule.Left and MyModule.Right, I would prefer to rename it somehow to MyModule as M and write M.Left. Is it possible ? What are the alternatives ?
Dario's answer gives the easiest way to do this, but since you ask for alternatives as well, you might want to look at the description of import
declarations in the Haskell report, or at the "Loading modules" section of Learn You a Haskell.
One common alternative to local aliases (i.e. import qualified SomeModule as X
) is just to hide the Prelude
functions (or constructors) if you know you'll not need them:
import Prelude hiding (Left, Right)
import MyModule
You can also import different parts of a module in different ways: one common trick is to import a module's types directly but alias its functions:
import Data.Map (Map)
import qualified Data.Map as M
import Data.Set (Set)
import qualified Data.Set as S
simpleMap :: Map String Int
simpleMap = M.singleton "one" 1
simpleSet :: Set String
simpleSet = S.singleton "one"
This allows us to use both singleton
functions, but we also don't have to type M.Map
and S.Set
in every type signature.
You just have one module. In this case, it's the very simplest solution to just hide the data-constructors (Left, Right) from the Prelude (These are defined there):
module MyModule where
import Prelude hiding (Left,Right)
Alternativly, you can simply import the Prelude qualified. Then you have to stick some letter before any Prelude functions / stuff / etc:
module MyModule where
import qualified Prelude as P