COSC 8 - Problem Solving with CS

CS 8 Software

Throughout the term, we will be writing computer programs in a language called Haskell, a functional programming language.

The Haskell environment that we will be using is the Glasgow Haskell Compiler (GHC). We will be using it primarily in its interactive mode (GHCi). Our textbook uses a graphics library called SOEGraphics. The authors have since updated this library, and the new version is called simply SOE. Their implementation of this library depends on a graphics library called OpenGL. We will use their implementation on Windows and Linux machines. This library and its implementation instructions are described on the page: Software.

While they also supply an implementation of SOE for the Mac, it requires what they call the "Enable GUI trick". Some software has to be loaded into the interpreter every time you load a program. Unfortunately, it appears that this software disables the ability to reload a program, and you have to quit the interpreter and start over again if you make any changes to a program. Because the usual development cycle is to load a program and then to repeatedly run it, modify it, and reload it, this makes debugging graphics programs tedious and painful.

There is an alternate implementation of SOE that uses the graphics package Gtk instead of OpenGL. This is the software installed on the laboratory Macs and is the software I recommend installing if you have a Mac. The instructions are given below.

Unfortunately, the name of this Gtk-based graphics library is "Graphics.SOE.Gtk" rather than just "SOE". I will try to put statements to import both libraries in all graphics programs, with one commented out. Because I will be using my Mac as the demonstration computer in class the Mac version of the library will be he default version. To run one of these programs on Windows or Linux simply comment out the "Graphics.SOE.Gtk" import statements and remove the comment designators from the "SOE" import statements.

You will find GHC and the necessary graphics libraries pre-installed on the computers in the basement labs at Sudikoff, but I know many of you will also want to be able to work at home or in the libraries. To set this up, follow each of the directions below.

There are a lot of instructions, and some of the installations may take hours to complete. Installing this software may be the most confusing and frustrating part of the course! Feel free to ask for help from me or the course staff. (Fortunately, you only need to do it once, and it is not on the exam!)

Note:
This installation is not necessary when you are using a lab machine at Sudikoff. The lab machines already have the CS 8 course software installed.

Documentation Resources

Documentation on GHC and Haskell are available on the Course Materials web page.


Downloading Software that Appears in the SOE Book

I suggest creating a folder called cs8 at the top level of your personal login folder to hold all of your course-related materials. In what follows I will assume that you have done so. If you want to put your folder elsewhere that is fine, but you will have to do more navigation in some of the commands.

The first thing that you will do is to download all of the software related to the SOE book. A version is available on the book's web page. If you are using Windows or Linux, you should download that version here: SOE-20071003.zip. To get the version suitable for Macs, go to: SOE.zip. The major difference between the programs in this file and what appears in the book is that when Hudak writes:

import SOEGraphics

we replace it with

import Graphics.SOE.Gtk
for Macs and
import SOE
for Windows and Linux.

There are some other minor library changes made since the book was published, but they have been made in the files given above. Move the downloaded file to your cs8 folder and unzip it. You now will have an SOE folder, and the src subfolder contains all of the code from the book.


Installing Haskell on a Macintosh

This section describes how to install the Glasgow Haskell Compiler (GHC) under MacOS 10.4.x and 10.5, for use with the code from Paul Hudak's "The Haskell School of Expression". There are a couple of basic tests to verify that it works. These instructions will not work with older versions of MacOS, due to the broken versions of GCC they possess.

In this course we will be using the Terminal window, both in the installation process described below and to run your programs. The command language of the terminal window is Unix. If you do not know Unix, you should look at this Basic Unix Guide before reading the directions below.

  1. Install X11.

    This can be installed using the "Optional Installs" installer package on "Mac OS X Install Disc 1" for 10.4 and Disc 2 for 10.5. If you don't have your installer disk I have one to loan to you.

  2. Install and Update Xcode Tools.

    Use the "XcodeTools.mpkg" installer package on Mac OS X Install Disc (1 or 2 as appropriate). You actually don't need everything; at a minimum, you should install:

    If you don't have a recent enought version of Xcode tools (2.4.1 or later), you need to download a more recent version. For OS 10.4, download xcode_2.4.1_8m1910_6936315.dmg of the Xcode tools (double-click on the .dmg file you downloaded, double click on the installer icon in the Finder window that opens, and follow the directions in the dialog boxes). For OS 10.5 you can install xcode tools by downloading: xcodetools_leopard.zip, unzipping it, and running the installer in the folder that is created by unzipping. This is important - may people have run into problems when they did not do this update. If you get error messages when you do "sudo port selfupdate" or it complains about problems with the C compiler when you do "sudo port install ghc" an old version of xcode tools is probably the problem.

    To find what version of Xcode you have, open a Terminal window and do the following:

    cd /developer/Applications
    open Xcode.app

    Cancel the Setup Assistant that appears and go up to the Xcode menu in the menu bar and select "About Xcode".

  3. Update your system paths

    You may need to add some things to your command-line path so that your computer will find this new code. This is done by creating or modifying a .profile file in your home directory. This is complicated by the fact that the Finder will hide any file whose name starts with a period. We will get around this as follows.

    First, open the TextEdit application and select Preferences in the TextEdit menu. Click on New Document, select the "Plain Text" radio button, and close the window.

    Open the Terminal application and type the following (without the "%"sign - that is to indicate the Terminal's prompt character or characters):

    % cd
    % touch .profile
    % open .profile

    This will open the file named ".profile" in the TextEdit application. It will probably be empty. If so, type the following (easiest to copy and paste):

    export PATH=.:/opt/local/bin:/opt/local/sbin:$PATH
    export MANPATH=/opt/local/man:$MANPATH
    export DISPLAY=:0.0

    Then save and close the file.

    If the .profile file is not empty, I assume that you will know how to add /opt/local/bin and /opt/local/sbin to the PATH variable and /opt/local/man to the MANPATH variable and to set the DISPLAY variable as above. If not see a member of the course staff. (If you changed your Mac to run csh or tcsh instead of bash you need to update .cshrc in a similar way, but instead of export you type setenv and leave replace the "=" by a space.)

If you have an Intel processor and Leopard (OS 10.5)

In this case installation is straightforward. In a Terminal window do the following. (You may want to copy and paste.) The following instructions assume that you have never used MacPorts (except possibly a failed attempt to install this course's software). If this is not the case talk with your instructor before you follow these instructions.
cd /tmp
curl http://www.cs.dartmouth.edu/~tim/opt.tar.gz -o opt.tar.gz
sudo rm -rf /opt/
sudo tar -C / -zxvf opt.tar.gz
rm /tmp/opt.tar.gz
At this point, you are ready to test your installation. Go to step 5 in the next section.

If you don't have an Intel processor or don't have Leopard (OS 10.5)

In this case things are more complicated. You should do the following steps:
  1. Get and install MacPorts.

    Go to: http://www.macports.org/

    1. Follow the link "Downloads are available at ...". Or see: http://svn.macosforge.org/repository/macports/downloads/
    2. Grab the latest .dmg file for your system (1.6.0 as I write).
    3. Mount the .dmg file, and run the installer (.pkg) file.

    This should set up MacPorts in /opt/local.

    Caveats: The rest of this section will not apply to most of you. If it makes no sense, it is a good indication that you do not have to worry about it. This problem only affects people who have installed open-source or other software through Terminal rather than through drag-and-drop or a clickable installer program.

    If you have other packages installed in /usr/local, such as those installed by other package managers like Fink, or simply because that is the default location for most open-source tools built and installed by autoconf and libtool, MacPorts will see them while it is installing, and may link against them. For this reason, you should make sure that either (a) anything you have in /usr/local is a complete installation, and not missing components, or (b) you hide /usr/local while you are first installing MacPorts.

    To do the latter, assuming you have a /usr/local on your system:

    % cd /usr
    % sudo mv local x-local

    ... install MacPorts, ghc, and gtk2hs, but not soegtk ...

    % cd /usr
    % sudo mv x-local local

    ... then install soegtk as described below.

  2. Run Terminal and make sure you can run the "port" command, e.g.,

    % port installed
    You should get a response of something like "no ports found". (You haven't used the port command to install anything yet.)

    To insure you have the latest version of MacPorts and the current package lists, you must now run:

    % sudo port selfupdate
  3. Install GHC:
    % sudo port install ghc

    This will also install the following dependencies:

       readline   -- GNU readline library (for command-line processing).
       gmp        -- GNU arbitrary precision math library.
       perl5.8    -- Perl interpreter (newer than the default).
       ncurses    -- terminal-handling library (update of curses).
       ncursesw   -- wide-character support for ncurses.
    

    The whole process will take a while, most of it for building GHC. On some systems, the build may take up to four hours.

    When you're done, you should be able to run a simple Haskell program. First open a terminal and navigate to the SOE folder that you downloaded. If you have a top-level cs8 folder and SOE is inside it, you can do this by typing:

    % cd cs8/SOE

    We will now run a simple "Hello, world" program by typing:

    % ghci hello.hs
    *Main> main
    You can type additional commands (e.g. 2 + 3) to see what happens. Quit by typing :q.

  4. Install graphics libraries.
    % sudo port install gtk2hs

    Note: This port has a lot of dependencies, so it might take a long time to build and install.

  5. Test your Installation

    To test that your graphics setup is working, run the X11 program that you installed in the first step. (It can be found in /Applications/Utilities.) An xterm window should appear. (If it does not, choose "Terminal" under the Applications window of X11.) This time cd to the cs8/SOE/src folder and type:

    % ghci Picture.lhs
    *Picture>> main

    This should create a window that looks like:

    Output from Picture.lhs

    To exit the program, click anywhere on the black background. But try clicking on the shapes first.

Making graphics work

You will need to have X11 running in order to run graphics programs. You can do this by running X11 directly, as you did in the graphics test above. You can alternately display graphics from the Terminal program. In this case, you must both have X11 running and your operating system DISPLAY variable must be set appropriately. That is the reason that we had you set the DISPLAY variable above. If you get the message that ghci is unable to initialize graphics, it is probably because you forgot to run x11.


Installing Haskell on Windows or Vista

Follow these instructions.


Installing Haskell on Linux

  1. Install GHC

    Go to http://www.haskell.org/ghc/distribution_packages.html and download and install the GHC version for your Linux variety and processor. If you use an automatic update tool like yum or apt-get you should try installing ghc with that tool before trying to install a package from www.haskell.org. You would say "yum install ghc"

  2. Install GLFW

    Go to http://www.haskell.org/soe/software1.htm/ and follow the instructions there to install GLFW.

    If on linux you have an error Loading package GLFW-0.1 ......GLFW-0.1.o: unknown symbol `XRRQueryExtension' add this line to your GLFW.cabal file extra-libraries: Xrandr Then you'll need to compile and install the Haskell GLFW module again.
  3. Test your installation

    Test your installation as in step 7 of the Macintosh instructions above.


Text Editors

You will edit programs using an editor that can handle plain text. Word can be used, but it is easier to use something that is designed to edit plaintext. On Macintoshes that could be TextEdit, on Windows it could be notepad, on Linux it could be emacs or vi. The Macintosh program that I will be using during class is the freeware program TextWrangler. I recommend downloading it and using it if you are on Macs. It will be on the laboratory Macintoshes.


Department of Computer Science, Dartmouth College, Hanover, New Hampshire, USA

Last modified: 08-January-2009