Modules, Packages, and Building Complete Programs
The hard part about learning to program in a new languages is to understand all the different abstractions the language offers. This part is behind you now. The last topic we need to cover is how to split larger projects across multiple source code files and how to package source code files into, well, packages that can be imported into other programs. This is the key to encouraging code reuse.
Haskell's method to organize source code distirbuted across multiple source code files is similar to those in many modern languages, only the terminology differs a little. In this chapter, we will discuss
-
Modules: A module is a program component. Its distinguishing feature is that it has private data types and functions that are visible only to other data types and functions within the same module. Private data types and functions are implementation details intentionally hidden from the user of the module. Public data types and functions constitute the API through which the user can interact with the module.
In Haskell, a module corresponds to a source code file.
-
Packages: These are similar to packages in Python and many other languages. In C or C++, we would call them third-party libraries. A package contains multiple modules that together provide some functionality, such as support for parallel programming, manipulating XML files, parser libraries, web server libraries, and many more. In Python, installing a package using
pip
copies the modules of the package into a standard location where the Python interpreter knows to look for modules we try to import into our code. The same happens when installing a package in Haskell.
We will start our discussion by having a look at modules, at how to import
modules from other packages or from our own project, and at how to define which
types and functions of a module are public and which ones are private. We will
then have a look at packages the way they are managed by the stack
build
system.