Professional Documents
Culture Documents
PZ08B - Logic Programming and Prolog
PZ08B - Logic Programming and Prolog
PZ08B
Logic Programming
So far programming has been algorithmic.
Procedural: statements (C, C++, FORTRAN, Pascal)
Functional: expressions (Postscript, ML, LISP)
Now we will look at declarative languages - They state
what the solution should look like; not how to compute
such a solution.
You are already an expert in some of these languages!
Example, BNF. Give a description of all even length
palindromes over 0 and 1:
Can list them: 00, 11, 0000, 0110, 1001, 1111, ...
Can give a specification rule: S 0S0 | 1S1 | 00 | 11
The BNF rule does not say how to generate such strings or
how to recognize a given string, only a description of
what all such strings look like.
There is some other process that would build the parse
tree for any given string.
PZ08B
PZ08B
Prolog overview
Development by Alain Coulmerauer and Philippe Roussel,
who were interested in developing a language for
making deductions from text
Developed in Marseilles, France, in 1972
Based on the the principle of resolution by Kowalski
from the University of Edinburgh
Although there is no standard for Prolog, the version
developed at the University of Edinburgh has become a
widely used variant
A Prolog program consists of a series of facts (concrete
relationships) among data objects and a set of rules.
A process of unification on Horn clauses - as we shall
soon see - is the basis for Prolog execution
PZ08B
PZ08B
PZ08B
Example of unification
Can use a form of substitution called unification to
derive other relationships. (Will discuss unification more
formally later.)
inclass(susan, X). Prolog searches database and
responds X=cmsc420.
Hitting Enter key, Prolog says No since no other fact.
inclass(john, Y). Prolog has following responses:
Y=cmsc330.
Y=cmsc311.
no.
Can define more complex queries:
takingboth(X):-inclass(X, cmsc330),inclass(X, cmsc311).
takingboth(john)
yes
PZ08B
takingboth(Y)
Y=john.
no
Prolog execution
So Prolog is:
1. A database of facts.
2. A database of queries.
3. A sequential execution model: Search facts in
order looking for matches. If failure, back up to
last match and try next entry in database.
Because of this last item, Prolog is not truly
declarative. It does have an algorithmic execution
model buried in structure of database.
Also, unlike previous languages studied, Prolog is not
complete. Not every program function can be
specified as a Prolog program.
PZ08B
Simple queries
Other Examples. Multiple definitions:
registered(X) :- takingboth(X).
registered(X) :- inclass(X, cmsc330).
registered(X) :- inclass(X, cmsc311).
registered(X) :- inclass(X, cmsc420).
printclass(X) :- registered(X), write(X),
write(' is registered.'), nl.
printclass(X) :- write(X),
write(' is not registered.'),nl.
printclass(susan) prints 'susan is registered'
printclass(harold) prints 'harold is not registered'
Adding to database:
consult(user) or consult('filename') adds to data base
reconsult(user) modifies database
control-D (D) returns to query mode.
PZ08B
Prolog Data
Data:
Integers: 1, 2, 3, 4
Reals: 1.2, 3.4, 6.7
Strings: 'abc', '123'
Facts: lower case names
Variables: Upper case names
Lists: [a, b, c, d]
Like ML: [ head | tail ]
PZ08B
10
Append function
APPEND function: append([a,b,c], [d,e], X)
X = [a,b,c,d,e]
Definition:
append([ ], X, X).
append( [ H | T], Y, [ H | Z]) :- append(T, Y, Z).
(function design similar to applicative languages like
ML.)
PZ08B
11
Clauses
REVERSE:
reverse([ ],[ ]).
reverse( [H | T ], L) :- reverse(T,Z), append(Z, [H], L).
Prolog execution is via clauses: a,b,c,d.
If any clause fails, backup and try alternative for that
clause.
Cuts(!). Always fail on backup. Forces sequential
execution. Programs with many cuts are really an
imperative program.
PZ08B
12
Cuts
Logical predicates:
P :- a, b.
P :- c,d.
P is true if a and b are true or if c and d are
true, or (a b) (c d)
(This is where the term logic programming comes from.)
But:
P :- a, !, b.
P :- c,d.
P is true if (a and b) are true or if (a is false and
(c and d are true))
(a b) (not(a) c d) ( See difference?)
Cuts can be used for sequential execution:
P :- a, !, b, !, c, !, d.
PZ08B
13
PZ08B
14
PZ08B
15
Horn clauses
What if we only have one non-negated term? A :- B, E.
We call such expressions Horn clauses.
A:- B, E. means (B E) A
So if we show that B and E are true, then we have shown
that A must be true.
We call A :- B, E. a query.
Note: If we write a fact A., then we are simply saying
that we have the Horn clause:
true A.
PZ08B
16
Unification
Prolog unification is an extension to variable
substitution.
Macros are an example of substitution:
Generate quadratic formula: a x2 + b x + c = 0
root(A, B, C) = -B + sqrt(B*B-4*A*C)/(2*A)
If A=1, B=2, C=3+4, substitution gives us:
root(1, 2, 3+4) = -(2) + sqrt((2)*(2)-4*(1)*(3+4))/
(2*(1))
Consider following 2 sets of rules:
P(x,y) = x * y
Q(z) = z2
PZ08B
17
Unification - 2
Now consider:
P(c+d, Q(e+f)) = (c+d)*Q(e+f) = (c+d) * (e+f)2
P(c+d, Q(e+f)) = P(c+d, (e+f)2) = (c+d) * (e+f)2
Same result achieved two ways, substitute into P first or
into Q.
(c+d) * (e+f)2 represents both a substitution into P and
a substitution into Q.
The P substitution is c+d for x, Q(e+f) for y.
The Q substitution is e+f for z.
So we say c+d for x and Q(e+f) for y and e+f for z
unifies P and Q.
Example:
append([2,3],[4,5],L) L=[2,3,4,5] unifies clause.
append([1,2],L,M), reverse(M,[3,2,1])
L=[3], M=[1,2,3] unify these clauses.
Note in this last case, M came from reverse, L came from
append. Substitution works both ways and .
PZ08B
18