next up previous
Next: Copyright notice Up: Functional and Logic Programming Previous: Reference Material




Hugs (a simple Haskell-98 interpreter) is lightweight and perfectly suited for this course, but produces cryptic error messages. I therefore recommend the ghci interpreter that comes with the Glasgow Haskell Compiler (GHC) (a complete Haskell toolset). This is also installed in Johnson 118. GHC is a big package so you may want to consider Hugs for use on your own system. In what follows I will use ghci but hugs is a perfectly acceptable drop-in replacement.

SWI-Prolog is also installed on the workstations in Johnson 118.


Programs may be composed and edited using any text editor, such as notepad on Windows. In any case, make sure that you have a good text editor. A programmer's editor is a very different thing from a word processor. Most importantly, it saves your work in plain text files and it doesn't insert extra carriage returns beyond the ones you actually type (so, Microsoft Word does not qualify). A good programmer's text editor will do a lot more than this, including:

There are many text editors that have some or all of these features. The most widely used editors on UNIX systems are vi and Emacs (or a variant called XEmacs). They are also subjects of a long standing holy war. Various ``modes,'' supporting various programming languages, exist for both of them.

You are of course free to program in any editor you like (including ed). For this course though, I recommend Kate or XEmacs.

Kate presents a familiar user interface, plus a good file selector and access to a terminal from within the editor. In addition to everything mentioned above, XEmacs is also able to run programs as ``inferior processes'' (their term), allowing you to communicate with them from within the editor. The Prolog mode we will use takes advantage of this feature and offers an seamless integration with Prolog interpreters and compilers.

The disadvantage of using XEmacs (there is no such thing as a free lunch, you know) is that XEmacs interface is quite different from anything you have probably seen. While most commands you are likely to use are accessible from menus or the toolbar, keyboard shortcuts do come handy, and XEmacs' are quite different from a usual Windows environment. Here is a list of the most used commands and their shortcuts (in Emacs-speak, C stands for Control, and M stands for Meta; the Meta key is usually bound to Alt, but Esc can be used as well: M-x stands thus for pressing (and holding) Meta/Alt and then pressing x, or pressing (and releasing) Esc and then pressing x):

Command XEmacs-speak Shortcut
Open find-file C-x C-f
Save save-buffer C-x C-s
Save as write-file C-x C-w
Copy kill-ring-save M-w
Cut kill-region C-w
Paste yank C-y
  XEmacs function call M-x function-name

Session Listings

In addition to the usual copy and paste, to get a listing of a ghci session on UNIX, start it up as follows:

    ghci | tee <file>
A log of the session will be output to the <file>, which can later be edited and printed. Don't use a terminal editor such as nano or vi during the session being logged.

Differences between the Textbook and GHC

The ghci user interface is different in various ways from the interface used in examples in the Davie text. Some language changes have been performed as well.

The ghci prompt has the form

where Module is the name of the current module (by default, the standard prelude Prelude). There is no what next? prompt after showing a result.

An "Enter" keystroke (rather than a ?) terminates an expression entered at the prompt; such an expression must fit on a single line.

let definitions at the prompt must be written on one line, using { ...} brackets and ; as a separator, as in the following:

    Prelude> let { u = 5; v = 2 } in (u + v) / (u - v)
A more practical alternative is to enter the definitions into a module using a text editor, and :load (or :reload) the module; for example,
    { u = 5; v = 2 }
or, using the layout conventions, just
    u = 5
    v = 2
would be a (simple) module that could be loaded to get the effect of the let definitions.

The ghci interpreter does not support the :def command described on page 30. Load a module with suitable code instead.

The divRem operator has been replaced by two operators, quotRem and divMod.

The list of reserved words on page 265 is out of date. Here is the list of words that currently cannot be used as identifiers:

case class data default deriving do else if import in infix infixl infixr instance let module newtype of then type where _

On interacting with SWI-Prolog

From within XEmacs

Under the standard configuration of the lab machines, once a file with the right extension (.pl for Prolog programs) is opened, XEmacs will automatically switch to the appropriate mode (called prolog-mode; if for some reason the right mode does not fire up automatically, you can switch to it by hand by calling its name as an XEmacs function).

Once you are in the right mode, explore the mode menus ( ``Prolog,'' ``Code,'' ``Help'') for useful commands.

Outside XEmacs

Fire up your favourite editor and write your Prolog program. Save it somewhere. Let's say for the sake of discussion that you save it as ~/prolog/

Then fire up SWI Prolog, change to the directory holding your program, and issue the loading command. In Prolog, the ``change directory'' command is the predicate cd/1, and the ``load'' command is the predicate consult/1 (or its operator counterpart []/1). The standard extension .pl is appended automatically to the argument by consult/1 if necessary. If the argment to consult/1 contains non-alphanumeric characters (such as periods) surround the argument in single quotes. Your interaction looks like this:

    ?- cd("~/prolog").
    ?- [foo].
    % foo compiled 0.00 sec, 1,960 bytes

Note that, outside XEmacs, SWI Prolog features a graphical interface (under XEmacs we rely completely on the XEmacs GUI).


next up previous
Next: Copyright notice Up: Functional and Logic Programming Previous: Reference Material
Stefan Bruda 2013-04-22