Lesson 2 / 25
Installing GHC, Cabal, Stack and GHCi
Install the toolchain with GHCup and work in GHCi and Cabal projects.
The Haskell toolchain
The recommended installer is GHCup, which installs and switches between versions of GHC (the compiler), cabal-install (the build tool and package manager), Stack (an alternative build tool using curated package sets) and HLS (the Haskell Language Server, which powers editor features in VS Code and others). GHCi is the interactive REPL: evaluate expressions, :t expr shows a type, :i name shows information about a type or class, :l File.hs loads a file and :r reloads it. Libraries are published on Hackage; Stackage provides snapshots of package versions known to build together. A Cabal project has a .cabal file describing the package, its library, executables and test suites, their build-depends with version bounds, and the default-language (such as GHC2021, a bundle of widely used extensions). Common commands: cabal init, cabal build, cabal run, cabal repl and cabal test. Searching by type signature with Hoogle is a Haskell superpower: searching [a] -> Int finds length.
Creating and exploring a project
GHCup, a Cabal package and GHCi commands.
ghcup tui # interactive installer: choose GHC, cabal, HLS versions
ghc --version
cabal --version
mkdir shop && cd shop
cabal init --non-interactive --exe # creates shop.cabal and app/Main.hs
cabal run # build and run
cabal repl # GHCi with the project loaded
# shop.cabal (excerpt)
# executable shop
# main-is: Main.hs
# hs-source-dirs: app
# build-depends: base >=4.17 && <5, containers, text
# default-language: GHC2021
# ghc-options: -Wall
# Inside GHCi:
# ghci> :t map
# map :: (a -> b) -> [a] -> [b]
# ghci> :i Maybe
# ghci> :r -- reload after editingA workshop with a catalogue
GHCup sets up the workshop, Cabal builds your projects, Hackage is the parts catalogue, and Hoogle is a catalogue you can search by the shape of the part you need rather than its name.
Quick check: What does the GHCi command :t do?
- Runs tests
- Terminates GHCi
- Toggles laziness
- Shows the type of an expression
Answer
Shows the type of an expression — :t (or :type) prints the inferred type of an expression.