UMBC CMSC331-01 Programming Languages, Fall 2024
Running Haskell Programs
Updated: September 6, 2024
We will be using the Glasgow
Haskell Compiler (GHC) using the command line interface on
Unix/Linux.
The general format for the command to compile Haskell source code is:
ghc file1.hs file2.hs ... -o run.out
This is assuming that one of the files has the main function defined.
For example, if the file Main.hs has the main function and
Qsort.hs defines the quicksort function, then you can
compile the whole program and create an exectuable file
dothis.out with:
% ghc Main.hs Qsort.hs -o dothis.out
[1 of 2] Compiling Qsort ( Qsort.hs, Qsort.o )
[2 of 2] Compiling Main ( Main.hs, Main.o )
Linking dothis.out ...
Then you can run the program dothis.out:
% ./dothis.out
[1,2,3,5,7,9]
For this class, we will often write functions in Haskell without
bothering with a main fuunction. In that case, you can test your code
using the interactive console ghci. For example, to test out
the quicksort function in ghci, you can:
% ghci Qsort.hs
GHCi, version 9.0.2: https://www.haskell.org/ghc/ :? for help
[1 of 1] Compiling Qsort ( Qsort.hs, interpreted )
Ok, one module loaded.
ghci> qsort [7,2,1,9]
[1,2,7,9]
ghci> :quit
Leaving GHCi.
%
Type :help for many more ghci commands.
Installing Haskell
Here are some ways you can optionally install and run Haskell.
On GL (default option)
The default option is to run GHC on GL. The Unix command to invoke the
compiler is ghc. To run GHC in interactive mode, use
ghci. These tools are already installed on GL, so nothing is
required on your part. Use your favorite terminal emulator to log into
GL and your favorite text editor to compose your Haskell source code.
On Windows 10 and 11
The recommended procedure for running GHC on Windows is to use the
Windows Subsystem for Linux (WSL). This will essentially give you Linux
running under Microsoft Windows. Many things that you want to do
with Linux can be accomplished in WSL. The exceptions are tasks
that involve the Linux kernel. Also, WSL doesn't come with a GUI desktop,
but you have Windows for that. WSL is provided by Microsoft and
will not mess up your Windows installation. You can run Linux and Windows
side-by-side. For most tasks, this is much easier than dual-booting
Linux.
Steps:
- First make sure that WSL is enabled on your Windows system.
From the Start Menu, search for "Turn Windows features on or off".
Click on the Control Panel result from that search.
This brings up a window with a bunch of checkboxes.
Scroll to the bottom and see if the box for "Windows Subsystem for Linux"
is checked. If not, check that box, click "OK" and reboot your machine.
- Go to the Microsoft Store and search for "WSL". Choose one of
the Linux distributions available. Either "Ubuntu" or "Debian" is
recommended.
- After Debian or Ubuntu is installed, launch the app. This
will bring up a terminal window. Allow the system to do some
housekeeping after the first launch. This could take a few minutes.
You will be asked to create a super-user account and password.
Remember this password! You will need the password to install packages.
- When the Linux distribution is ready, first update the
packages:
sudo apt update
- Next, install GHC and the PCRE module
sudo apt install ghc libghc-regex-pcre-dev
- You will also want to install some Linux tools that might not be
already installed, such as emacs, vim, openssh-client, rsync, ...
These can be easily installed using the sudo apt install
command.
If maintaining a Linux distribution is not your cup of tea, then just
use GHC on GL. You can easily remove WSL the Linux distribution by
uninstalling the app (Debian or Ubuntu) that you downloaded from the
Microsoft App Store. If you want to keep WSL (quite useful for many
computer science classes), you can uninstall ghc:
sudo apt remove ghc
Linux
If you are running Linux on your main machine, presumably you know how
to install packages. If you use Debian or Ubuntu, the instructions above
for Windows WSL should also work. For other Linux distributions, search
for ghc and regex-pcre (on Fedora the PCRE package is
ghc-regex-pcre).
If your Linux distribution does not have a package for
Text.Regex.PCRE, you would have to install it using
cabal, the native Haskell package manager. Check if
cabal is already installed with ghc, if not it is
likely in a separate package called cabal-install. After
installing cabal:
cabal update
cabal v1-install regex-pcre
Then, check whether the library was installed correctly:
ghc-pkg list
You should see some version of regex-pcre in the list. If not,
you can try instead:
cabal install --lib regex-pcre
If that also doesn't work, things get pretty squirmy. Check the internet
if there are reports of problems with cabal installing a
global library for your particular flavor of Linux.
Note: cabal takes about 1GB of storage in the .cabal
directory in your home directory. When you are done with Haskell, you
can safely trash .cabal.
On MacOS
MacOS is Unix underneath. So, you just need to install the GHC package.
Since MaCOS does not come with its own package manager, you have to
first install a package manager. The recommended package manager is
Homebrew.
If you already have another package manager installed (e.g.,
Macports
or
Fink), you can also
install GHC using those package managers.
MacOS installation is complicated by three things:
- The steps vary a bit depending on which shell you use.
The Unix command to install Homebrew on their website works
for sh, bash and zsh. If you are using csh or tcsh, you should
fire up zsh (the default MacOS shell these days) to run
the Homebrew installation command. Also, for csh and tcsh,
each time you install something using Homebrew, you should
issue a rehash command to make the new command
available to you.
- The steps vary a bit depending on which version
of MacOS you have. The following has been tested on
Ventura (MacOS 13.5), Monterey (MacOS 12.5) and Big Sur (MacOS 11.6).
Mojave (MacOS 10.14) is not supported by Homebrew.
I have not checked Catalina (MacOS 10.15).
- Neither Homebrew nor MacPorts has the
PCRE package for Haskell as a library install.
We have to use cabal, the native Haskell package
manager to get the Text.Regex.PCRE module.
OTOH, Fink appears to have a
PCRE
package, but I have not tested this. If you are running Fink,
try installing it and check if you have the Haskell PCRE module (see
below.)
Here are the steps:
- Go to the Homebrew web page
and copy the Unix command under the heading "Install Homebrew".
The command starts with
/bin/bash -c "$(curl -fsSL ...
- Launch the MacOS Terminal app.
(Drop into zsh if you are using csh or tcsh.)
Paste the command at the Unix prompt.
Wait for Homebrew to finish installing.
- After Homebrew is done with its installation,
check if the brew command is available. If not,
follow the instructions to add the path to the commands
installed by Homebrew to your PATH environment
variable.
The actual path might vary depending on versions. It might be
/opt/homebrew/bin or /usr/local/bin.
If you are using csh or tcsh, manually add the path
in your .cshrc or .tcshrc file
instead of using the instructions provided by
the Homebrew installer.
Logout and log back in and check if the brew
command is available.
- Next, you can use Homebrew to install GHC:
brew install ghc
In addition to ghc, you need to install the PCRE module (Perl
Compatible Regular Expressions) for Haskell. This is somewhat more
complicated on MacOS since Homebrew does not supply the binaries and we
need to use cabal to download and install the package. This
means we need to install cabal and some system tools:
brew install cabal-install
brew install pkg-config
brew install pcre
Depending on which shell you use, you may need to issue the
rehash command to see the newly installed utilities. Then, we
can use cabal to install Text.Regex.PCRE:
cabal update
cabal v1-install regex-pcre
You can check if the Text.Regex.PCRE package was installed correctly by
running ghci and typing:
import Text.Regex.PCRE
Note: cabal takes about 1GB of storage in the .cabal
directory in your home directory. When you are done with Haskell, you
can safely trash ~/.cabal.
On Android (not recommended)
Yes, you can run GHC on Android, but this is not for the faint of heart.
You first install the Termux app which exposes the Linux system underneath
Android. This allows you to use Linux shell commands.
The hiccup is that you do not want to use the version of Termux
in the Google Play Store. So, the better version of Termux is on F-Droid.
This is a small security risk, since you have to allow F-Droid to
install apps on your device. Proceed at your own risk.
-
First, on your Android device, go to the
F-Droid
site and download the F-Droid .apk file.
Then, using your favorite Android file manager, install the
.apk file. You may need to change your system
settings to allow your file manager to install apps.
(You can change it back after installing F-Droid.)
- Run the F-Droid app. Go to the updates panel. Update
F-Droid itself, if available.
You may need to change your system settings to allow F-Droid to
install and update apps.
- In the F-Droid app, search for "Termux". Install the Termux app.
- Launch Termux. There are some housekeeping commands you need to
issue:
- First, update Termux itself
pkg update
- Second, allow Termux to access the Android file system
termux-setup-storage
Your Android files are linked under storage or you can go
directly to /sdcard.
- Next, install any Linux utilities that you like using the
pkg install command. Termux has many but not all Linux
packages in its repository.
- Now you can install ghc and cabal in Termux:
pkg install ghc
pkg install cabal-install
- You can optionally install dnsutils. If you don't,
cabal will complain that it can't run nslookup, but
will carry on and work anyway:
pkg install dnsutils
- Next, we initialize cabal and install the PCRE
module:
cabal update
cabal v1-install regex-pcre
- Finally, check whether the library was installed correctly:
ghc-pkg list
You should see some version of regex-pcre in the list. You
can also check if the Text.Regex.PCRE package was installed correctly
by running ghci and typing:
import Text.Regex.PCRE
Note: cabal takes about 1GB of storage in the .cabal
directory in your Termux home directory. When you are done with Haskell, you
can safely trash ~/.cabal.
Last Modified:
6 Sep 2024 10:43:46 EDT
by
Richard Chang
Back to Fall 2024 CMSC 331-01 Homepage