CSE 341 Lecture Notes - Running CLP(R)

Unfortunately CLP(R) is not open source. We've got a license to run it on attu, though, so you can easily access it from anywhere with network connectivity. (See Using Linux and Emacs in CSE 341 for more information.) To run CLP(R) on attu type

clpr
Or you can run it on any of the personal linux machines in the undergrad labs (since the file system is cross-mounted).

The system is interactive, like Scheme and Haskell. The system prompt is
?-

You can type a query to it, for example X=[42,Y], Y=1. Here's a transcript of this:

CLP(R) Version 1.2
(c) Copyright International Business Machines Corporation
1989 (1991, 1992) All Rights Reserved

1 ?- X=[42,Y], Y=1.

X = [42, 1]
Y = 1

*** Yes

Like Haskell, CLP(R) has separate query and definition modes. To define some rules, put them in a file, say "mystuff.clpr". Then load them into CLP(R) using the goal consult("mystuff.clpr").

Type control-d to exit clpr.

CLP(R) isn't integrated with an editor -- just use emacs (or another editor if you prefer) to edit the file with the rules. If you are debugging away, our suggested approach is to open two terminal windows: one with CLP(R), the second with emacs and your file. When you are in CLP(R), you can reload all the definitions after you've made an edit using the goal reconsult("mystuff.clpr"). Caution: if you have CLP(R) already running, and want to reload your definitions after you've edited them, be sure and use reconsult("mystuff.clpr") rather than consult("mystuff.clpr"). ("consult" will just add the new definitions after the old ones rather than deleting the old ones.) Or just exit CLP(R) and restart.

There's a program on attu call rlwrap that acts a readline front end for console programs. So running CLP(R) using this command:

rlwrap -c clpr
lets you use conveniences like up-arrow for previous command, filename completion (e.g. consult("my[TAB] to autocomplete my-really-long-filename.clpr), home/end/C-K/C-Y/etc.

Emacs fans may prefer to run the whole thing under emacs. To do this, start up emacs on the file with your rules. Then split your window into two buffers using control-x 2. Continue editing your file in one of the buffers. In the other buffer type <esc>x shell to get a unix shell, then type clpr.

Shortcuts

For the consult command, the system will try the extension .clpr if you don't give one explicitly. You can omit the quotes around the file name if you don't include an extension. To read in a file mystuff.clpr, you can type
[mystuff].
rather than
consult("mystuff").
or
consult("mystuff.clpr").

Debugging

If you're trying to figure out what a program is doing, you can put write goals in your rule. For example, write("at start of rule") will print out "at start of rule", while write(N) will print out the current value of the variable N. Not surprisingly, backtracking over write can't actually undo the output, so this goal doesn't follow CLP(R)'s logical semantics.

A related goal is dump. This takes a list of variables, and prints out the constraints on them. (This can be more useful than write if the variables have some constraints but don't have specific values.)

Otherwise, the debugging facilities in CLP(R) are, in the words of the manual, 'rudimentary'. See Section 4.9 of the manual for a description.

For example, suppose you have a file append.clpr, and that you want to trace all goals. Do the following to set up debugging and to trace the goal append(A,B,[1,2]):

attu% clpr
?- codegen_debug.
?- [append].
?- spy.
?- trace.
?- append(A,B,[1,2]).