CSE 341 - Programming Languages

Running Haskell

We're using the Glasgow Haskell Compiler, which is the de facto standard version of Haskell. The latest version is 8.2.2. There is an older version on attu and the CSE undergrad linux machines, namely 7.6.3; and 8.0.2-a on Windows. All of these should work (or any other recent version) -- there is a lot of activity in the Haskell community making new packages and such, but the basic language is stable.

On the CSE Undergrad Windows Machines

Caution: we will be using the HUnit unit testing package, which is unfortunately not installed on the windows machines in the lab. So if you usually work on a Windows machine in the lab, just for Haskell you might want to use attu instead -- see Software for CSE 341 for how to access attu.

Ok ... despite the warning, if you still want to run Haskell on Windows in the Lab, if you'll be using unit tests first install HUnit using cabal, which is Haskell's package manager. Do this from a cmd terminal:

cabal update
cabal install HUnit
This will take a minute or so. Unfortunately, HUnit will disappear after you log out, and you'll need to re-install it the next time you log in.

Then, from the command window, navigate to the directory where you want to keep your Haskell source files. Run Haskell by typing ghci or ghci MyFile.hs. (The "i" in "GHCi" stands for "interactive", as opposed to compiling and producing an executable file.)

Alternately, if you're just experimenting and don't have a source file, you can run Haskell via the "Start" menu. It is at Haskell Platform 8.0.2-a / ghci

You can also run Haskell on a .hs file by double-clicking on the file name (but this may not work correctly if you are importing another module that you defined, since Haskell will look in the current working directory for this file).

There are also local copies of documentation under
Haskell Platform 8.0.2-a / GHC Documentation

On CSE Undergrad Linux Machines and attu

At the command prompt type ghci to start it, or ghci MyFile.hs to start it and load MyFile.hs. Fortunately these machines do have HUnit installed.

On a Personal Machine

Haskell is available for Windows, Mac, and Linux. Get the Haskell Platform. Here's the download page: http://www.haskell.org/platform/. Core should be sufficient.

If you are getting an "xcrun: error" when running GHCi from the terminal (on a Mac), refer to this post for a possible fix: https://apple.stackexchange.com/questions/254380/macos-sierra-invalid-active-developer-path

We will be using the HUnit unit testing package in 341. You can check whether it is there by starting Haskell and typing:

import Test.HUnit
If it's missing on an install on a personal machine, first try Haskell's package system. At the command line type:
cabal update
cabal install HUnit
If that doesn't work, you can try installing HUnit from the source https://hackage.haskell.org/package/HUnit (download the .tar.gz file). Run the following commands within the unzipped folder:
runhaskell Setup.hs configure
runhaskell Setup.hs build
runhaskell Setup.hs install
If you get an error that says "call-stack --any" dependency is missing (or something to that effect), you have to install the "call-stack" dependency directly from this source https://hackage.haskell.org/package/call-stack. Repeat the runhaskell commands within the unzipped folder, and retry the previous steps.

If that doesn't work post a note on the discussion board and we'll try to help.

Finally, if this is more than you want to deal with, even if you want to work someplace other than the undergrad labs, as long as you have an internet connection you can log in remotely to attu.cs.washington.edu and run Haskell there -- see "Working Elsewhere" section of Software for CSE 341 for how to ssh.

Haskell Mode for emacs

Haskell Mode for emacs will help with indentation, do syntax highlighting, and so forth. See the Quick Installation section of the Haskell Mode for Emacs github page and Haskell Mode Manual. If you want Haskell mode for emacs on attu, you'll need to do the installation as well. This version requires GNU Emacs version 24.3 or later. The emacs version on attu is ok, but the version that ships with the Macintosh operating system will be too old -- get a newer version if you're running on a Mac.

For the Haskell project (HW 4), you may want to also tell emacs to use haskell-mode for the parser file, which has a .y extension. While you're at it, tell emacs about prolog-mode as well, to get ready later for that language, by adding the following to your .emacs file in your home directory:

; use haskell-mode for Happy parser files (.y) and prolog-mode for .pl files
(setq auto-mode-alist
  (append (list (cons "\\.y" 'haskell-mode) (cons "\\.pl" 'prolog-mode))
          auto-mode-alist))

Basic Haskell Commands

A few essential commands:

If you just want to try Haskell without loading any files, just start it up. For anything interesting, though, you'll want to have a file with defintions in it. A simple way to do this is to use your favorite editor to create a file (with the extension .hs) in your home directory or a subdirectory. Save the file but leave the editor open on it. Then start up Haskell in that same directory on that file. Try things out. If you want to edit your file, do so and save it, and then back in Haskell reload the file using :reload.

If you have some favorite setting (for example you always want the prompt to be ghci>) you can put that in a .ghci file, and it will be loaded when Haskell starts up.

The above directions may be all you want for 341; and in any case I'd suggest starting out that way. However, you can also run Haskell from within emacs as part of the Haskell Mode package.

Beware the Evil Tab!

Unlike most programming languages, whitespace is significant in Haskell. So that you can see whether things are lining up properly use a fixed-pitch font when editing. Haskell has some rule about how tabs are processed, but I recommend that you avoid tabs in Haskell code and always use spaces instead.

For emacs, if you use the Haskell mode for Emacs, it will avoid using tabs in files. Otherwise, by default, Emacs inserts tabs in place of multiple spaces when it formats a region. Putting the following in your .emacs file turns this off:

     (setq-default indent-tabs-mode nil)