Professional Documents
Culture Documents
Two trees are equal when they share the same root
functor and the corresponding subtrees are equal.
A CLP(R) program consists of a finite number of rules:
where n, m 0 ,
where n, m 0 , n + m 1 ,
For example:
if A is resistor(X, Y, 5) and
B is resistor(2, 8, Z) ,
then A = B denotes {X = 2, Y = 8, Z = 5} .
• G1 is of the form
?- C1, A1, A2, ..., Am , where m 1
• G2 is of the form
?- C1, C2, Ai = B0, A1, A2, ..., Ai-1, Ai+1, ..., Am,
D1, D2, ..., Dk ,
where 1 i m
fact(0, 1).
fact(N, N*F) :-
N > 0,
fact(N-1,F).
?- fact(N0, 6).
The third and fourth cases are examples of partial evaluation. In the fifth case CLP() cannot determine whether the
answer constraint is solvable or not. Actually, the answer is not entirely useless, since the polynomial is solvable
and yields the correct solution for Rate.
kirchoff(L) :-
sum(L, 0). Voltage V2
R1
sum([ ], 0).
sum([H|T], N) :-
N = H + M,
sum(T, M).
V
availres(10).
availres(14). R2
availres(27).
availres(60).
availres(100).
availcell(10).
availcell(20).
The constraint in the first line is found to be solvable and the system proceeds to expand the atoms in the goal. Each
possible expansion of the first three atoms gives an instance a, b, c or R1, R2 and V. To the problem:
14.5 < V2 < 16.26, V1/a - V2/b = 0, V1 + V2 = c
A CLP(R) program for loan(P, I, S) which means that the series of (not necessarily uniform) payments S - expressed
as a list - exactly repays loan P, at rate of interest I.
loan(0, I, []).
loan(P, I, [X|Y]) :-
loan((1+I)*P - X, I, Y).
A program in CLP(R) for the predicate power(X, N, P) meaning that P is the value of number X raised to
the power of positive integer N:
power(X, 0, 1).
power(X, N, X*Y) :-
N > 0,
power(X, N-1, Y).
?- power(3, 2, P)
?- X1 = 3, N1 = 2, X1 * Y1 = P, N1 > 0,
power(X1, 1, Y1)
?- X1 = 3, N1 = 2, X1 * Y1 = P, N1 > 0,
X2 = X1, N2 = 1, X2 * Y2 = Y1, N2 > 0,
power(X2, 0, Y2)
?- X1 = 3, N1 = 2, X1 * Y1 = P, N1 > 0,
X2 = X1, N2 = 1, X2 * Y2 = Y1, N2 > 0,
X2 = X3, Y2 = 1
Newton’s method of finding a numerical solution to the equation xn = a , where n is a positive integer, involves
making an initial guess x0, and then iteratively calculating a series of values xi, where:
Here is a program in CLP(R) that uses this method to find the nth root of a real number, to any given precision:
root(Y, N, R, R, E) :-
power(R, N, P),
P < Y + E,
P > Y - E.
root(Y, N, R1, R2, E) :-
power(R1, N, P),
(P >= Y + E ; P <= Y - E),
power(R1, N-1, P1),
R3 = ((N-1)*R1 + Y/P1)/N,
root(Y, N, R3, R2, E).
where the equation is of the form X N = Y , E represents the required degree of precision (the largest permitted error), R1 is
the current value and R2 the next value.