It’s rare to write programs that are entirely self contained. Nearly all programs that we write will depend on some some code that’s been defined elsewhere in some other module. To make use of code defined in some other module, we have to import the code from that module. When we import a module, we can get access to all of the code that it has exported . In this section, we’ll focus on using libraries, and you’ll learn how to import code from modules defined inside of libraries that you depend on. Later on in this chapter, you’ll learn how to define your own modules and control what they export.

Importing a Module

You can import a module using the import keyword. You need to write an import declaration for each module you want to import, and all of your imports should be at the top of your module, immediately after the module declaration. Once you import a module, everything that module exports will be available to use.

As an example, let’s create a cabal project named import-demo . You can write the cabal file yourself or use cabal init to create an executable project:

​  cabal-version: 2.4
​  name: import-demo
​  version: 0.1.0.0
​  license: NONE
​  
​  executable import-demo
​  main-is: Main.hs
​  build-depends: base
​  hs-source-dirs: app
​  default-language: Haskell2010

Now that we have a new project, let’s write an application that can benefit from using some parts of the standard library that aren’t normally available to us without importing an extra module. For this example, we’ll write a program that will count the number of non-printable characters, like tab and newline characters, in a string.

In order to count the number of non-printable characters, we need to know whether a particular character is printable or not. The standard library includes a function named isPrint that returns True if a character is a printable Unicode character, and False otherwise.

Until now, the functions you’ve used from the standard library have always been available to you without needing to import anything. That’s because Haskell includes a special module named Prelude that’s imported by default into every module. The Prelude module exports a large, commonly used portion of the standard library. For things in the standard library that aren’t included with Prelude , we need to import the modules explicitly.

The isPrint function, along with several other functions for dealing with Char data, is exported by the module Data.Char module. Let’s start by importing it:

​  ​ module ​ ​ Main ​ ​ where ​
​  ​ import ​ ​ Data.Char ​

In this example, you can see the syntax of a basic import , and notice that we’ve included it immediately after the start of our module. You’re free to include empty lines or comments between the start of your module and your import list, but you can’t include any other code.

Now that we’ve imported Data.Char , we can use isPrint and implement the rest of our program:

​  ​ module ​ ​ Main ​ ​ where ​
​  ​ import ​ ​ Data.Char ​
​  
​  countNonPrintableCharacters :: ​ String ​ -> ​ Int ​
​  countNonPrintableCharacters =
​  length . filter (not . isPrint)
​  
​  main :: ​ IO ​ ()
​  main =
​  print $ countNonPrintableCharacters ​ " ​​ \v\t\a ​​ Hello ​​ \r\n ​​ " ​

Let’s use cabal to run our program and make sure it’s working:

​  ​ user@host$ ​​ cabal ​​ ​​ run ​
​  Up to date
​  5

We can also test our program out in ghci . Let’s use the cabal repl command to start an interactive session that will inherit all of the settings we’ve configured in our cabal file:

​  ​ user@host$ ​​ cabal ​​ ​​ repl ​

When we use cabal repl , our Main module will be loaded automatically into our session automatically, so we don’t need call :load explicitly. We can run main , or pass some strings directly to countNonPrintableCharacters :

​  ​ λ ​ main
​  5
​  ​ λ ​ countNonPrintableCharacters ​ "123" ​
​  0
​  ​ λ ​ countNonPrintableCharacters ​ "123 ​​ \t\t\t ​​ " ​
​  3

Let’s try calling isPrint directly from our ghci session to test whether a couple of characters count as being printable or not:

​  ​ λ ​ isPrint ​ ' ​​ \n ​​ ' ​
​  ​ F ​​ a ​lse

Although we haven’t imported Data.Char into our ghci session, the code that it exports is still available in our interactive session. When you load a file into ghci , all of the modules that it imports will be available in your session. To see this in action, let’s quit our current ghci session and start a new one using the ghci command rather than cabal repl so that Main isn’t loaded automatically:

​  λ :t isPrint
​  
​  <interactive>:1:1: error: Variable not in scope: isPrint
​  λ :load app/Main.hs
​  [1 of 1] Compiling Main ( app/Main.hs, interpreted )
​  Ok, one module loaded.
​  λ :t isPrint
​  isPrint :: Char -> Bool

You can see in this example that until we load Main.hs we don’t have access to isPrint . As soon as we load Main , isPrint becomes available.

Loading Files in ghci

| images/aside-icons/info.png | Keep in mind that a module’s imports will only be available in ghci if you load it with :load . If you import a module, then you’ll only get access to whatever that module exports. This is one of the key differences between :load and import in ghci . |

If you don’t want to load a file, you can also import modules directly in-line in ghci . For example, let’s try using the permutations function from Data.List to look at all of the different permutations of the list [1,2,3] .

If we try to call permutations right now, it won’t be available because we haven’t imported Data.List :

​  λ permutations [1,2,3]
​  
​  <interactive>:6:1: error:
​  Variable not in scope: permutations :: [a0] -> t

Once we import Data.List , the function will be available:

​  ​ λ ​ ​ import ​ ​ Data.List ​
​  ​ λ ​ permutations [1,2,3]
​  [[1,2,3],[2,1,3],[3,2,1],[2,3,1],[3,1,2],[1,3,2]]

Local Module Aliases

As you’ve seen, when you import a module all of the code that it exports becomes available for you to use. At times this can present a problem, for example if a module exports something with a name that conflicts with something already defined in your program. Let’s continue with our character counting example by expanding our code to support another string type that’s commonly used in Haskell programs. The Text type comes from the text package, and it gives us a more efficient way to work with human-readable Unicode text. We’ll start by adding text as a dependency in our project:

​  cabal-version: 2.4
​  name: ​ import ​​ - ​​ demo ​
​  version: 0.1.0.0
​  license: ​ NONE ​
​  
​  executable ​ import ​​ - ​​ demo ​
​  main-is: ​ Main ​.hs
​  build-depends: base
​  , text
​  hs-source-dirs: app
​  ​ default ​-language: ​ Haskell2010 ​

Next, let’s return to Main.hs and work on updating our code to support counting the non-printable characters in a Text value. We’ll start by importing the Data.Text module, which exports the Text type along with some useful functions for dealing with Text values:

​  ​ module ​ ​ Main ​ ​ where ​
​  ​ import ​ ​ Data.Char ​
​  ​ import ​ ​ Data.Text ​

Unfortunately, it turns out that merely importing Data.Text is enough to cause our program to fail to compile. Let’s try running cabal build and looking at the error messages before going on:

​  app/Main.hs:7:3: error:
​  Ambiguous occurrence ‘length’
​  It could refer to
​  either ‘Prelude.length’,
​  imported from ‘Prelude’ at app/Main.hs:1:8-11
​  (and originally defined in ‘Data.Foldable’)
​  or ‘Data.Text.length’,
​  imported from ‘Data.Text’ at app/Main.hs:3:1-16
​  |
​  7 | length . filter (not . isPrint)
​  | ^^^^^^
​  
​  app/Main.hs:7:12: error:
​  Ambiguous occurrence ‘filter’
​  It could refer to
​  either ‘Prelude.filter’,
​  imported from ‘Prelude’ at app/Main.hs:1:8-11
​  (and originally defined in ‘GHC.List’)
​  or ‘Data.Text.filter’,
​  imported from ‘Data.Text’ at app/Main.hs:3:1-16
​  |
​  7 | length . filter (not . isPrint)
​  | ^^^^^^

It looks like the length and filter functions that we were using have now been defined twice. Both Prelude and Data.Text export functions with the same name, and the compiler has no way to know which one we intended to use.

One way we can address this is to include the name of the module along with the ambiguous functions. In this case, we want to continue using the versions from Prelude so we’ll replace length with Prelude.length and filter with Prelude.filter :

​  countNonPrintableCharacters :: ​ String ​ -> ​ Int ​
​  countNonPrintableCharacters =
​  ​ Prelude ​.length . ​ Prelude ​.filter (not . isPrint)

Now that our function calls aren’t ambiguous anymore, let’s go ahead and add a new version of our function to work with Text values. Our algorithm will remain the same, but we’ll work on Text values and use the versions of length and filter defined in Data.Text :

​  countNonPrintableCharactersInText :: ​ Text ​ -> ​ Int ​
​  countNonPrintableCharactersInText =
​  ​ Data ​.​ Text ​.length . ​ Data ​.​ Text ​.filter (not . isPrint)

Next, let’s add a function that will call both the String and Text versions of our function, just to make sure they always return the same value:

​  countNonPrintableCharactersStringAndText :: ​ String ​ -> (​ Int ​,​ Int ​)
​  countNonPrintableCharactersStringAndText input =
​  ( countNonPrintableCharacters input
​  , countNonPrintableCharactersInText $ pack input)

You’ll notice that we’re using the pack function in our call to countNonPrintableCharactersInText . This function comes from Data.Text and it’s how we can convert a String into a Text value. With this function in place, the last thing we need to do is to update main . Let’s look at our entire program as it currently stands:

​  ​ module ​ ​ Main ​ ​ where ​
​  ​ import ​ ​ Data.Char ​
​  ​ import ​ ​ Data.Text ​
​  
​  countNonPrintableCharactersInText :: ​ Text ​ -> ​ Int ​
​  countNonPrintableCharactersInText =
​  ​ Data ​.​ Text ​.length . ​ Data ​.​ Text ​.filter (not . isPrint)
​  
​  countNonPrintableCharacters :: ​ String ​ -> ​ Int ​
​  countNonPrintableCharacters =
​  ​ Prelude ​.length . ​ Prelude ​.filter (not . isPrint)
​  
​  countNonPrintableCharactersStringAndText :: ​ String ​ -> (​ Int ​,​ Int ​)
​  countNonPrintableCharactersStringAndText input =
​  ( countNonPrintableCharacters input
​  , countNonPrintableCharactersInText $ pack input)
​  
​  main :: ​ IO ​ ()
​  main =
​  print $ countNonPrintableCharactersStringAndText ​ " ​​ \v\t\a ​​ Hello ​​ \r\n ​​ " ​

At this point our program compiles, since we’ve included the full name of the functions that were previously ambiguous. One drawback to our current approach is that in a longer program it can start to get tiresome to type the full module name every time we want to use a function that would otherwise be ambiguous. This would be an even bigger problem if the module name were long.

Thankfully, we can give each module an alias when we import it using the as keyword. As an example, let’s import Data.Text but give it the alias T :

​  ​ import ​ ​ Data.Text ​ ​ as ​ T

Using an alias, we can still call any unambiguous functions without the module name, but when we need to include the module name we can use the shorter alias instead of the full module name. For example, with an alias our countNonPrintableCharactersInText will become:

​  ​ import ​ ​ Data.Text ​ ​ as ​ T
​  
​  countNonPrintableCharactersInText :: ​ Text ​ -> ​ Int ​
​  countNonPrintableCharactersInText =
​  ​ T ​.length . ​ T ​.filter (not . isPrint)

You can also import the same module several times with different aliases. This will let you use any of the aliases that you’ve assigned to the module interchangably. For example, we can assign Data.Text the aliases T and Text and use them in countNonPrintableCharactersInText :

​  ​ import ​ ​ Data.Text ​ ​ as ​ T
​  ​ import ​ ​ Data.Text ​ ​ as ​ Text
​  
​  countNonPrintableCharactersInText :: ​ Text ​ -> ​ Int ​
​  countNonPrintableCharactersInText =
​  ​ Text ​.length . ​ T ​.filter (not . isPrint)

In practice, the only time you’re likely to import a module with multiple aliases is as an incremental step when refactoring your code, for example, when you are merging two previously separate modules, or splitting a module into several new smaller modules.

More commonly, you can also assign the same alias to several modules. For example, the module Data.Text.Encoding provides utility functions for dealing with text encoding, like encodeUtf8 and decodeUtf8 that will let you encode and decode raw UTF-8 encoded text. In these cases, it’s sometimes desirable to import several related modules under a single alias. Let’s import Data.Text. Encoding and then encode and re-decode our text before we count the characters:

​  ​ import ​ ​ Data.Text ​ ​ as ​ T
​  ​ import ​ ​ Data.Text.Encoding ​ ​ as ​ T
​  
​  countNonPrintableCharactersInText :: ​ Text ​ -> ​ Int ​
​  countNonPrintableCharactersInText =
​  ​ T ​.length . ​ T ​.filter (not . isPrint) . ​ T ​.decodeUtf8 . ​ T ​.encodeUtf8

In this example, we’ve combined all of the functions that have been exported by Data.Text and Data.Text.Encoding into a single alias, T .

Qualified Imports

So far, whenever we’ve imported a module the entire contents of the module have been made available to us. Even when we’ve assigned an alias to some modules, using the alias name is optional except when the name would otherwise be ambiguous. This is great for keeping our code short and easy to read, but it comes with a maintenance cost: code that works today might stop working in the future when we accidentally introduce some ambiguity into a call. This can happen in two different ways. First, you might add a new function without realizing that the name conflicts with something that’s already exported by a module that you are importing. This can be irritating, but at the very least you’ll quickly get feedback from the compiler and have the opportunity to rename your function. A worse outcome is that you have some code that has been working for some time, and then it starts to fail when you update your dependencies, and a module that you have been importing begins to export something with a conflicting name. In this scenario, you might find yourself having to make changes to code that you haven’t touched in some time, and perhaps making changes throughout your entire code base to address unexpected name conflicts.

Qualified imports give us one way to avoid this problem. When we use the qualified keyword in our import statement, we require that anything exported by the module be referenced using either the full name of the module, or the module alias if we’ve given one. Let’s look at an example of this by modifying our character counting application. We’ll change our import of Data.Char to be a qualified import:

​  ​ module ​ ​ Main ​ ​ where ​
​  ​ import ​ ​ qualified ​ ​ Data.Char ​
​  ​ import ​ ​ Data.Text ​ ​ as ​ T
​  ​ import ​ ​ Data.Text.Encoding ​ ​ as ​ T
​  
​  countNonPrintableCharactersInText :: ​ Text ​ -> ​ Int ​
​  countNonPrintableCharactersInText =
​  ​ T ​.length . ​ T ​.filter (not . isPrint) . ​ T ​.decodeUtf8 . ​ T ​.encodeUtf8
​  
​  countNonPrintableCharacters :: ​ String ​ -> ​ Int ​
​  countNonPrintableCharacters =
​  ​ Prelude ​.length . ​ Prelude ​.filter (not . isPrint)
​  
​  countNonPrintableCharactersStringAndText :: ​ String ​ -> (​ Int ​,​ Int ​)
​  countNonPrintableCharactersStringAndText input =
​  ( countNonPrintableCharacters input
​  , countNonPrintableCharactersInText $ pack input)
​  
​  main :: ​ IO ​ ()
​  main =
​  print $ countNonPrintableCharactersStringAndText ​ " ​​ \v\t\a ​​ Hello ​​ \r\n ​​ " ​

If we try to compile our program now, we’ll get an error. We’ve updated the import of Data.Char to be qualified, and so our references to isPrint are no longer valid:

​  app/Main.hs:8:30: error:
​  • Variable not in scope: isPrint :: Char -> Bool
​  • Perhaps you meant ‘Data.Char.isPrint’ (imported from Data.Char)
​  |
​  8 | T.length . T.filter (not . isPrint) . T.decodeUtf8 . T.encodeUtf8
​  | ^^^^^^^
​  
​  app/Main.hs:12:42: error:
​  • Variable not in scope: isPrint :: Char -> Bool
​  • Perhaps you meant ‘Data.Char.isPrint’ (imported from Data.Char)
​  |
​  12 | Prelude.length . Prelude.filter (not . isPrint)
​  | ^^^^^^^

You’ll notice here that, thankfully, the compiler has pointed out not only what function is no longer valid, but also found that the version we want is now called Data.Char.isPrint . Typing Data.Char everywhere we call isPrint might be a little annoying, but we can combine qualified imports with aliases. This will allow us to require that we use a qualified name every time we reference something imported from a module, but still allow us to use a shorter and more readable module alias. Let’s change our qualified import to give it the alias Char and then update our code to use the qualified name when we call isPrint :

​  ​ module ​ ​ Main ​ ​ where ​
​  ​ import ​ ​ qualified ​ ​ Data.Char ​ ​ as ​ Char
​  ​ import ​ ​ Data.Text ​ ​ as ​ T
​  ​ import ​ ​ Data.Text.Encoding ​ ​ as ​ T
​  
​  countNonPrintableCharactersInText :: ​ Text ​ -> ​ Int ​
​  countNonPrintableCharactersInText =
​  ​ T ​.length . ​ T ​.filter (not . ​ Char ​.isPrint) . ​ T ​.decodeUtf8 . ​ T ​.encodeUtf8
​  
​  countNonPrintableCharacters :: ​ String ​ -> ​ Int ​
​  countNonPrintableCharacters =
​  ​ Prelude ​.length . ​ Prelude ​.filter (not . ​ Char ​.isPrint)
​  
​  countNonPrintableCharactersStringAndText :: ​ String ​ -> (​ Int ​,​ Int ​)
​  countNonPrintableCharactersStringAndText input =
​  ( countNonPrintableCharacters input
​  , countNonPrintableCharactersInText $ pack input)
​  
​  main :: ​ IO ​ ()
​  main =
​  print $ countNonPrintableCharactersStringAndText ​ " ​​ \v\t\a ​​ Hello ​​ \r\n ​​ " ​

Once again, our program compiles. Now, thanks to our use of qualified imports, we’re also protected against any accidental name collisions with Data.Char .

Choosing What to Import

Normally, when you import a module you are importing everything that the module makes available. Sometimes this is a useful feature, like when you want to use most of what a module exports. Other times, you might want to only import a subset of the things that a module exports. When you want more fine-grained control over what you import, you can use import lists.

There are two types of import lists. Most commonly, import lists allow you to list explicitly the things that you want to import from a particular module. To add an import list, you need to list everything that you want to import inside of parentheses after the import statement. Let’s give it a try by modifying our character counting function. Instead of a qualified imports of Data.Text. Encoding and Data.Char we’ll import only the particular functions that we want to use from those modules:

​  ​ import ​ ​ Data.Char ​ (​ isPrint ​)
​  ​ import ​ ​ Data.Text ​ ​ as ​ T
​  ​ import ​ ​ Data.Text.Encoding ​ (​ decodeUtf8 ​, ​ encodeUtf8 ​)
​  
​  countNonPrintableCharactersInText :: ​ Text ​ -> ​ Int ​
​  countNonPrintableCharactersInText =
​  ​ T ​.length . ​ T ​.filter (not . isPrint) . decodeUtf8 . encodeUtf8

In this example, you can see how we’ve specified the exact functions that we want to import from each of the two modules. We don’t have to qualify the modules, but we can still be assured that we’re not going to have any accidental name collisions, since nothing except what we’ve listed in the import list will be available. We can see this in practice in ghci . Let’s import isPrint from Data.Char into a fresh ghci session:

​  ​ λ ​ ​ import ​ ​ Data.Char ​ (​ isPrint ​)

Now we can call isPrint as you’d expect, but we’ll get an error if we try to call a different function exported by Data.Char that we haven’t listed in our import list. For example, we can try to call isSpace :

​  λ isPrint ' '
​  True
​  λ isSpace ' '
​  
​  <interactive>:3:1: error:
​  Variable not in scope: isSpace :: Char -> t

In ghci , if you’ve imported a particular function from a module and decide that you want some other functions, you can add more import statements with import lists to get the extra functions. Let’s add isSpace to our session to see for ourselves:

​  ​ λ ​ ​ import ​ ​ Data.Char ​ (​ isSpace ​)
​  ​ λ ​ isPrint ​ ' ' ​
​  ​ True ​
​  ​ λ ​ isSpace ​ ' ' ​
​  ​ True ​

You’ll notice here that when we write another import statement it adds the new function to our environment, but we still have access to the isPrint function that we imported earlier. If you decide you want everything from the module, you can still write an import statement without an import list. For example, if we try to call isHexDigit we’ll get an error, but when we import all of Data.Char we’ll have access to it, along with everything else exported by the module:

​  λ isHexDigit '0'
​  
​  <interactive>:7:1: error:
​  Variable not in scope: isHexDigit :: Char -> t
​  λ import Data.Char
​  λ isHexDigit '0'
​  True

You can use import lists alongside qualified imports and module aliases too. Returning to our character counting example, let’s update our import of Data.Text . We’ll make it a qualified import, keep our module alias, and also use an import list to limit ourselves to only importing the pack , length , and filter functions that we’re intending to use:

​  ​ module ​ ​ Main ​ ​ where ​
​  ​ import ​ ​ Data.Char ​ (​ isPrint ​)
​  ​ import ​ ​ Data.Char ​ (​ isSpace ​)
​  ​ import ​ ​ qualified ​ ​ Data.Text ​ ​ as ​ T (pack, length, filter)
​  ​ import ​ ​ Data.Text.Encoding ​ (​ decodeUtf8 ​, ​ encodeUtf8 ​)
​  
​  countNonPrintableCharactersInText :: ​ Text ​ -> ​ Int ​
​  countNonPrintableCharactersInText =
​  ​ T ​.length . ​ T ​.filter (not . isPrint) . decodeUtf8 . encodeUtf8
​  
​  countNonPrintableCharacters :: ​ String ​ -> ​ Int ​
​  countNonPrintableCharacters =
​  ​ Prelude ​.length . ​ Prelude ​.filter (not . isPrint)
​  
​  countNonPrintableCharactersStringAndText :: ​ String ​ -> (​ Int ​,​ Int ​)
​  countNonPrintableCharactersStringAndText input =
​  ( countNonPrintableCharacters input
​  , countNonPrintableCharactersInText $ ​ T ​.pack input)
​  
​  main :: ​ IO ​ ()
​  main =
​  print $ countNonPrintableCharactersStringAndText ​ " ​​ \v\t\a ​​ Hello ​​ \r\n ​​ " ​

If you build this, you’ll notice that we have an error:

​  app/Main.hs:7:38: error:
​  Not in scope: type constructor or class ‘Text’
​  |
​  7 | countNonPrintableCharactersInText :: Text -> Int
​  | ^^^^

Functions aren’t the only thing that are imported, or not, when we use import lists. It looks like we’ve forgotten to import the Text type. One option that we have is to add it to the import list for our qualified import of Text :

​  ​ module ​ ​ Main ​ ​ where ​
​  ​ import ​ ​ Data.Char ​ (​ isPrint ​)
​  ​ import ​ ​ qualified ​ ​ Data.Text ​ ​ as ​ T (​ Text ​, length, filter, pack)
​  ​ import ​ ​ Data.Text.Encoding ​ (​ decodeUtf8 ​, ​ encodeUtf8 ​)
​  
​  countNonPrintableCharactersInText :: ​ T ​.​ Text ​ -> ​ Int ​
​  countNonPrintableCharactersInText =
​  ​ T ​.length . ​ T ​.filter (not . isPrint) . decodeUtf8 . encodeUtf8
​  
​  countNonPrintableCharacters :: ​ String ​ -> ​ Int ​
​  countNonPrintableCharacters =
​  ​ Prelude ​.length . ​ Prelude ​.filter (not . isPrint)
​  
​  countNonPrintableCharactersStringAndText :: ​ String ​ -> (​ Int ​,​ Int ​)
​  countNonPrintableCharactersStringAndText input =
​  ( countNonPrintableCharacters input
​  , countNonPrintableCharactersInText $ ​ T ​.pack input)
​  
​  main :: ​ IO ​ ()
​  main =
​  print $ countNonPrintableCharactersStringAndText ​ " ​​ \v\t\a ​​ Hello ​​ \r\n ​​ " ​

In this example, we’ve added Text to our import list, and we’ve also changed the reference to Text in the type of countNonPrintableCharactersInText . Since Data.Text is a qualified import, we need to reference it with the alias we’ve given to the module, in this case T .

You’ve already seen that you can import a module more than once with different import lists. You can also import a module more than once using other combinations of features. One pattern that you’ll see somewhat frequently is to use an unqualified import with an import list to import the types defined in a module, and to use a qualified import for everything else. We can apply this pattern in our example by adding an unqualified import of Data.Text to import just the Text type, but keeping everything else in Data.Text under a qualified import:

​  ​ module ​ ​ Main ​ ​ where ​
​  ​ import ​ ​ Data.Char ​ (​ isPrint ​)
​  ​ import ​ ​ Data.Text ​ (​ Text ​)
​  ​ import ​ ​ qualified ​ ​ Data.Text ​ ​ as ​ T
​  ​ import ​ ​ Data.Text.Encoding ​ (​ decodeUtf8 ​, ​ encodeUtf8 ​)
​  
​  countNonPrintableCharactersInText :: ​ Text ​ -> ​ Int ​
​  countNonPrintableCharactersInText =
​  ​ T ​.length . ​ T ​.filter (not . isPrint) . decodeUtf8 . encodeUtf8
​  
​  countNonPrintableCharacters :: ​ String ​ -> ​ Int ​
​  countNonPrintableCharacters =
​  ​ Prelude ​.length . ​ Prelude ​.filter (not . isPrint)
​  
​  countNonPrintableCharactersStringAndText :: ​ String ​ -> (​ Int ​,​ Int ​)
​  countNonPrintableCharactersStringAndText input =
​  ( countNonPrintableCharacters input
​  , countNonPrintableCharactersInText $ ​ T ​.pack input)
​  
​  main :: ​ IO ​ ()
​  main =
​  print $ countNonPrintableCharactersStringAndText ​ " ​​ \v\t\a ​​ Hello ​​ \r\n ​​ " ​

Choosing What Not to Import

Import lists are a great way to include just a few items from a module, but the syntax you’ve used so far can be inconvenient if you want all but a few things from a module. In cases where you want to import all but a few things, you can use the hiding keyword before your import list. As you might expect, this inverts the import list, causing you to import everything from the module except what you’ve listed. As you might expect, you can combine imports that hide certain elements with qualified imports and aliases. Let’s take another look at our character counter, but this time instead of using a qualified import for all of Data.Text , we’ll do an unqualified import, but we’ll hide the length and filter functions that were conflicting with Prelude . For those two functions specifically, we’ll do a qualified import so that we still have access to them:

​  ​ module ​ ​ Main ​ ​ where ​
​  ​ import ​ ​ Data.Char ​ (​ isPrint ​)
​  ​ import ​ ​ Data.Text ​ ​ hiding ​ (​ length ​, ​ filter ​)
​  ​ import ​ ​ qualified ​ ​ Data.Text ​ ​ as ​ T (length, filter)
​  ​ import ​ ​ Data.Text.Encoding ​ (​ decodeUtf8 ​, ​ encodeUtf8 ​)
​  
​  countNonPrintableCharactersInText :: ​ Text ​ -> ​ Int ​
​  countNonPrintableCharactersInText =
​  ​ T ​.length . ​ T ​.filter (not . isPrint) . decodeUtf8 . encodeUtf8
​  
​  countNonPrintableCharacters :: ​ String ​ -> ​ Int ​
​  countNonPrintableCharacters =
​  ​ Prelude ​.length . ​ Prelude ​.filter (not . isPrint)
​  
​  countNonPrintableCharactersStringAndText :: ​ String ​ -> (​ Int ​,​ Int ​)
​  countNonPrintableCharactersStringAndText input =
​  ( countNonPrintableCharacters input
​  , countNonPrintableCharactersInText $ pack input)
​  
​  main :: ​ IO ​ ()
​  main =
​  print $ countNonPrintableCharactersStringAndText ​ " ​​ \v\t\a ​​ Hello ​​ \r\n ​​ " ​

Hiding imports can be particularly useful if you want to define your own implementation of a function from a module. For example, imagine that we wanted to define our own length function. Since length is defined in Prelude we’d get a conflict, but we can explicitly import Prelude and hide the length function:

​  ​ module ​ ​ Main ​ ​ where ​
​  ​ import ​ ​ Prelude ​ ​ hiding ​ (​ length ​)
​  
​  length :: [a] -> ​ Int ​
​  length ​ [] ​ = 0
​  length (​ _ ​:xs) = 1 + length xs
​  
​  main :: ​ IO ​ ()
​  main =
​  print $ ​ "the length of 'hello' is: " ​ <> show (length ​ "hello" ​)

As you might expect, you can also use an exclusion list with qualified imports, and with local module aliases.

Common Practices for Importing Modules

With so much flexibility in how you import things, you might find yourself wondering how to approach imports in your applications. Each style of import offers its own benefits and drawbacks.

Standard imports are often the easiest place to start. When you import a module without any import list, aliases, or qualification, you simply get access to everything exported from that module and you can start writing code right away. Name collisions are the biggest drawback of standard imports. In some cases, libraries use standard names by design and most imports will cause collisions. In other cases, you may simply want to avoid accidental name collisions. In either case, the next step is often to use qualified imports with an alias.

Qualified imports without an alias can be extremely verbose. From time to time you’ll come across libraries with short module names, but long module names are common in Haskell, and repeatedly typing a long module name can become tiresome. Aliases, without qualification, give you a way to disambiguate name collisions, but they invite inconsistency. You may have some code that uses the alias and other code that doesn’t. For these reasons, you’ll see that qualified imports and local aliases are frequently used together. Qualified imports with a short readable module alias provide some additional documentation by letting readers know exactly where a given function was imported from, avoid the problem of accidental name collisions, and with well-chosen module aliases they minimize the impact of repeatedly typing the module name.

You’ll see import lists being used with both qualified and unqualified imports. In some cases, import lists can serve as an additional form of documentation. When you list everything that you want to import from a particular module, it’s easier to understand why the module has been imported. In some larger projects, you’ll also notice the use of import lists as a way to improve compile times. Since GHC attempts to only recompile code that has changed, using an import list may allow you to avoid some extra work. That’s because your module will need to be recompiled if a function you import has changed. If you import everything from a module, then your module will need to be recompiled every time that dependency changes. If you only import a particular function, then the compiler might be able to avoid recompiling your module if that function didn’t change in your dependency.