## Getting started with CLP(R)

The first thing to know about CLP(R) is that it is a declarative programming language, rather than one of the imperative languages like C++ or Java to which you are used. This affects the way in which you program in tow ways.
1. In general your program specifies the solution you are looking for, rather than the way in which you find the solution.
2. Eventually you do need to understand how the CLP(R) engine works so that you write sensible programs.

### A nice declarative example

Consider the set of three linear equations:
3x + 4y - 2z = 8
x - 5y + z = 10
2x + 3y -z = 20
This is a specification for the three variables x,y, and z. It can be turned into a CLP(R) program (technically a goal) as:
3*X + 4*Y - 2*Z = 8,
X - 5*Y + Z = 10,
2*X + 3*Y -Z = 20.
Note the commas, which serve the role of and and the period at the end. Note also that the variables are now in upper case.

Given this goal CLP(R) responds:

Z = 35.75 Y = 8.25 X = 15.5 *** Yes Thus, at least for linear equations we have a direct connection (state the problem -- get the solution) no algorithm required.

### CLP(R) syntax

Before defining a CLP(R) program we need some definitions:
User defined constraint:
is of the form p(t1, ...,tn) where p is an n-ary predicate and each ti is an expression from the constraint domain
literal
A primitive constraint or a user defined constraint CLP(R) programs consist of a collection of rules.
A goal
is a sequence of literals.
A rule
is of the form H :- B where
1. H (the head of the rule) is a user-defined constraint,; and
2. B (the body) is a goal.
Lists
CLP(R) uses the standard Prolog syntax for lists
1. [] is the empty list
2. [a,b,...,k] is the list whose elements are a,b, ..., k
3. [A|B] is the list whose first element is A and whose tail is B. With this notation if we unify [a,b,c,d,e] and [H|T] then H = a and T = [b,c,d,e]
A constraint logic program is a sequence of rules.

You can put a goalto be executed at startup into a CLP(R) program by inserting code of the form:

::- the_start_goal. Into your file. Note that if you put more than one such into your file you can't be sure of the order in which they are tried. Note also that you can put in goals preceeded by :- or ?-. These affect when the goal is attempted. If you are using op/3 to define operators you almost certainly want to use ?- so that the operator is recognized both in the source file and at run time.

#### Examples

We can turn the above example into (a very dull) CLP(R) program as solve(X,Y,Z) :- 3*X + 4* Y - 2 *Z = 8, X - 5 * Y + Z = 10, 2 * X + 3 * Y - Z = 20. Note that , plays the role of the wedge (/\). If you want to try this you can get the code here!

As an exercise you could write a program where the coefficients can be chosen at run time. A suggestion for two variables

## Running the programs

CLP(R) is installed on maxwell. There are two ways to load a program into CLP(R).
1. At startup time: If your program is in a file my_prog just type clpr my_prog on the command line;
2. Or you can start clpr by typing clpr on the command line and then at the ?- prompt type [my_prog].-- The period is important, if you don't enter it the system will wait until you do!

If you type in the goal ls. you will see that the rules from your program have been entered.

### Dump

In some cases you may want to see the effect of projecting the constraints onto some of the variables. dump/1 and dup/2 do this for you. dump/1 takes as argument a list of the target variables and prints the projection of the constraints onto these variables. dump/2 is more flexible: the first argument is the variable names and the second is a list of new names to be used in the output. The Options example from the CLP(R) distribution uses dump/1.
Dr. J.P.E. Hodgson
Department of Mathematics and Computer Science
Saint Joseph's University
5600 City Avenue