© Stefania Loredana Nita and Marius Mihailescu 2019
Stefania Loredana Nita and Marius MihailescuHaskell Quick Syntax Referencehttps://doi.org/10.1007/978-1-4842-4507-1_23

23. Haskell Stack

Stefania Loredana Nita1  and Marius Mihailescu1
(1)
Bucharest, Romania
 

Haskell Stack is a tool used to build Haskell projects and to handle its dependencies, including GHC, Cabal, a version of the Hackage repository, and a version of the Stackage package collection tool. In this chapter, you will learn the main uses of Haskell Stack.

The first step is to install Haskell Stack. On a Unix system, open a terminal and type the following:
curl -sSL https://get.haskellstack.org/ | sh
Or type the following:
wget -qO- https://get.haskellstack.org/ | sh

In Windows, go to https://get.haskellstack.org/stable/windows-x86_64-installer.exe , where you will be prompted to download the installer. Then follow the steps in the installer.

The following are the main commands for Haskell Stack:
stack new my-project
cd my-project
stack setup
stack build
stack exec my-project-exe
Let’s see what every command does.
  • stack new: Creates a new directory and all the necessary files to begin a project. The structure will look like Figure 23-1.

  • stack setup: Downloads the compiler if needed, putting it in a separate location. This means it won’t make changes outside its directory.

  • stack build: Builds a minimal project, designing reproducible builds. In this process, curated package sets are used, called snapshots. The main directory contains a file called stack.yaml representing a blueprint. It contains a reference called resolver that points to the snapshot used in the build process.

  • stack exec my-project-exe: Executes a command.

../images/475690_1_En_23_Chapter/475690_1_En_23_Fig1_HTML.jpg
Figure 23-1.

The structure of a new project

Another useful command is stack install <package_name> , which installs a desired package. And, of course, don’t forget about stack --help, which provides all the commands.

Now let’s see a concrete example, called hello-world, inspired from [1] (note that, in this example, we will work on the Windows operating system).

Open a terminal (if you use Windows, then right-click a command prompt and choose Run as Administrator). At the terminal, choose a location for the new project.
C:WindowsSystem32>cd C:HaskellStack
Then create the project.
C:HaskellStack>stack new hello-world new-template
This will create a new project called hello-world using the new-template template, applying an initial setup. Don’t worry if you get a lot of messages. Now, you will see file in the hello-world directory, as shown in Figure 23-2.
../images/475690_1_En_23_Chapter/475690_1_En_23_Fig2_HTML.jpg
Figure 23-2.

The files in the hello-world directory

The next step is to build the project. Before building, change the current directory to hello-world.
C:HaskellStack>cd hello-world
C:HaskellStackhello-world>stack build

In this step, Stack will check for GHC and will download and install it into the global Stack root directory. This will take a while, and you will get intermediary messages about the download progress. After the build process, a library called hello-world and an executable called hello-world-exe are created in the autocreated directory .stack-work in the hello-world directory.

Further, let’s run the executable.
C:HaskellStackhello-world>stack exec hello-world-exe

You will get a someFunc message.

In Haskell Stack, you can even test the project. To do so, use the following command:
C:HaskellStackhello-world>stack test
Now, let’s add other functionalities to your project. Find the Lib.hs file in the src folder and type the following:
module Lib
    ( someFunc
    ) where
import Acme.Missiles
someFunc :: IO ()
someFunc = launchMissiles
If you build the project, you will get an error because the acme-missiles package is not found as a dependency (Figure 23-3).
../images/475690_1_En_23_Chapter/475690_1_En_23_Fig3_HTML.jpg
Figure 23-3.

The result of build after modifying the Lib.hs file

To correct this, you need to modify the file package.yaml, adding the following line in the dependencies section (don’t forget to save the file after editing):
- acme-missiles
If you build again, you will get another error that says it failed to construct the plan. This error is caused by the fact that the acme-missile package is not included in the long-term support (LTS) package set. To correct this, in the file stack.yaml, add the following line to create a new section called extra-deps:
extra-deps:
- acme-missiles-0.3
Now build again the project, and it will finally succeed (Figure 23-4).
../images/475690_1_En_23_Chapter/475690_1_En_23_Fig4_HTML.jpg
Figure 23-4.

Successful build

Run the project.
C:HaskellStackhello-world>stack exec hello-world-exe
Nuclear launch detected.

You have created a simple project with the Haskell tool Stack.

The Haskell tool Stack is great for versioning control, focusing on reproducible build plans and multipackage projects. Of course, you can do a lot more things with it than what was presented in this chapter. For example, you can put your project into a Git repository, or you can include other projects from Git. You can find a comprehensive tutorial in [1].

Summary

In this chapter, you learned the following:
  • What the Haskell tool Stack is

  • What the main commands are and what they mean

  • How to create, build, run, and test a new project

  • How to add new dependencies to the project

References

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.184.29