Installing Haskell
Getting started is the hardest part
Table of Contents
Objectives
When you are done with this chapter, you will…
- be able to describe the difference between Haskell tools such as
ghc
,ghci
, andstack
, and - install these things on your computer.
This kind of information tends to become out of date quickly, but the advantage of publishing to html is that we can also update this quickly. Let us know if something is out of date.
Installation
Recommended Method: ghcup
The Haskell site has an installer called ghcup
that is multi-platform. If you
are not generally comfortable installing things on your computer (and even if you are) then this is one of the better
choices. If you are running windows, then use this with the Windows Subsystem for Linux (usually
just called WSL
).
Linux options
If you are running Linux, then your package manager might also be a good option. This is particularly true of nixos.
Mac Options
For Apple computers, you might want to use the homebrew system.
$ brew install haskell-stack
Windows Options
First take a Linux installation disk and … no? Okay, fine. Using ghcup
as described above is probably your best
bet.
What should I install?
You will need ghc
and stack
, and we also recommend cabal
and HLS
. We’ll describe these next.
The Tools
Now that you’ve install these things, here is what they do.
ghc
The main compiler is called ghc
, standing for Glasgow Haskell Compiler. You can compile a program with it into a
standalone executable. If you create a file hello.hs
with this content:
main = do putStrLn "Hello, world!"
This creates a main
function that does the traditional thing. You can compile and run it from the command line
We will use Linux for our command line examples, but we try to be OS agnostic in the course. The $
will be the
command line prompt. Whatever system you use, as CS students you should absolutely be comfortable using the command
line.
with:
$ gch foo.hs
$ ./foo
Hello, world!
ghci
You will not be using the standalone compiler much in this course. You will be using the interactive version called
ghci
, where the i
stands for interactive. This tool gives you an interactive shell that lets you prototype and
test your code. If you have used Python, Ruby, or any Lisp dialect, this will
be familiar to you. We call this kind of thing a REPL, abbreviating Read Eval Print Loop.
The simplest way to start it is to run ghci
with the file name. The prompt will be something like Prelude>
if you
have not loaded a file or Main>
, depending on the module name. Use control-D to exit. In this example you can see
us run the main
function.
$ ghci foo.hs
GHCi, version 8.10.7: https://www.haskell.org/ghc/ :? for help
[1 of 1] Compiling Main ( foo.hs, interpreted )
Ok, one module loaded.
*Main> main
Hello, world.
*Main>
Leaving GHCi.
You can also start the REPL by itself. In addition to running Haskell code, you can use :l
to load a
file and :r
to reload a file for example after you have changed something.
$ ghci
GHCi, version 8.10.7: https://www.haskell.org/ghc/ :? for help
Prelude> :load foo
[1 of 1] Compiling Main ( foo.hs, interpreted )
Ok, one module loaded.
*Main> main
Hello, world.
*Main> :r
Ok, one module loaded.
*Main> main
Hello, world.
*Main>
A common workflow is to have your favorite editor running in one window and the REPL running in another. Note that
running :r
will erase anything you defined in the REPL earlier. Another useful command is :t
, which will tell you
the type of any expression.
$ ghci
GHCi, version 8.10.7: https://www.haskell.org/ghc/ :? for help
Prelude> :t 12
12 :: Num p => p
Prelude> :t "hi"
"hi" :: [Char]
We will explain the types in a different section.
stack
The stack
program is a combination of a build system and a package manager. You will use this most of the time.
stack repl
will startghci
for you, and automatically load any files that are in the project in the current directory. This is how you will interact with the code for your machine problems.stack test
will run the provided test cases against your code. Some of the tests are unit tests, but we are slowly replacing them with randomized specification-based tests.- There are many other commands; run
stack help
to look at them and see what kinds of things are possible.