Logic-Programming (1)
Logic-Programming (1)
❑What is a predicate?
d
Question: What are the...
❑ atoms
❑ facts
❑ rules
Clausal Form
❑Problem: There are too many ways to express
propositions.
■difficult for a machine to parse or understand
❑Clausal form: standard form for expressing
propositions
Consequent Antecedent
Example:
path(X, Y) ⊂ edge(X, N) ∩ path(N, Y).
Clausal Form Example
path(X, Y) ⊂ edge(X, N) ∩ path(N, Y).
Meaning:
if there is an edge from X to N and there a path from N
to Y,
then there is a path from X to Y.
The above is also called a "headed Horn clause".
In Prolog this is written as a proposition or rule:
gfo(dd,bb)?
CUTS
1. The Process of query evaluation often requires
considerable backtracking.
2. To save time, CUTS was introduced
3. CUT(!) indicates that if backtracking must be applied,
the pattern fails
4. It limits the search space for a solution
5. e.g gfo(aa, cc), female(cc)
- Yes
gfo(aa,cc), !, female(cc)
-No
Cut has not effect in forward direction
PROLOG: PROgramming in LOGic
ancestor(X,Y) :- parent(X,Y).
ancestor(X,Y) :-
ancestor(X,Z), ancestor(Z,Y).
parent(X,Y) :- mother(X,Y).
parent(X,Y) :- father(X,Y).
father(bill, jill).
mother(jill, sam).
mother(jill, sally).
Query the Ancestors
?- consult('/pathname/ancestors.pl').
ancestor(bill,sam).
Yes
?- ancestor(bill,X).
X = jill ;
X = sam ;
ERROR: Out of local stack
?- ancestor(X,bob).
ERROR: Out of local stack
Understanding the Problem
❑You need to understand how Prolog finds a solution.
ancestor(X,Y) :- parent(X,Y).
ancestor(X,Y) :-
Depth-first search
ancestor(X,Z), ancestor(Z,Y). causes immediate
parent(X,Y) :- mother(X,Y). recursion
parent(X,Y) :- father(X,Y).
father(bill,jill).
mother(jill,sam).
father(bob,sam).
Factorial
File: factorial1.pl
factorial(0,1).
factorial(N,N*M) :- factorial(N-1,M).
❑ The factorial of 0 is 1.
❑ The factorial of N is N*M if the the factorial of N-1 is M
?- consult('/path/factorial1.pl').
?- factorial(0,X).
X = 1
Yes
?- factorial(1,Y).
ERROR: Out of global stack
Query Factorial
?- consult('/path/factorial1.pl').
?- factorial(2,2).
No
?- factorial(1,X).
ERROR: Out of global stack
?- 2*3 = 6. ?- 6 is 2*3.
No Yes is(6,2*3).
?- 2*3 = 2*3. ?- 2*3 is 6.
Yes No
l-value = r-value ?
Arithmetic via Instantiation: is
❑"=" simply means comparison for identity.
factorial(N, 1) :- N=0.
?- sumto(0, Sum).
Sum = 0.
Yes
?- sumto(1, Sum).
No
is : how to fix?
❑How would you fix this problem?
?= sumto(5, X).
Factorial revised
File: factorial2.pl
factorial(0,1).
factorial(N,P) :- N1 is N-1,
factorial(N1,M), P is M*N.
Meaning:
❑ The factorial of 0 is 1.
❑ factorial of N is P
if N1 = N-1
and factorial of N1 is M
and P is M*N.
Query Revised Factorial
?- consult('/path/factorial2.pl').
?- factorial(2,2).
Yes
?- factorial(5,X).
X = 120
Yes
but still has some problems...
?- factorial(5,X).
X = 120 ; request another solution
factorial(0,1).
factorial(N,P) :- not(N=0), N1 is N-1,
factorial(N1,M), P is M*N.
?- factorial(5,X).
X = 120 ;
No
?-
Readability: one clause per line
factorial(0,1).
factorial(N,P) :- not(N=0), N1 is N-1,
factorial(N1,M), P is M*N.
factorial(0,1).
factorial(N,P) :- Better
not(N=0),
N1 is N-1,
factorial(N1,M),
P is M*N.
Finding a Path through a Graph
edge(a, b). a
edge(b, c).
edge(b, d). b
edge(d, e). edge(d, f).
path(X, X). c d
path(X, Y) :- edge(X, Z), path(Z, Y).
e f
?- edge(a, b).
Yes
?- path(a, a).
Yes
?- path(a, e).
Yes
?- path(e, a).
No
How To Define an Undirected Graph?
edge(a, b). a
edge(b, c).
edge(b, d). b
edge(d, e).
edge(d, f). c d
edge(X, Y) := not(X=Y), edge(Y, X).
path(X, X). e f
path(X, Y) :- edge(X, Z), path(Z, Y).
?- edge(b, a).
Yes
?- path(a, b).
Yes
?- path(b, e).
No
Queries and Answers
❑When you issue a query in Prolog, what are the
possible responses from Prolog?
% Suppose "likes" is already in the database
:- likes(jomzaap, 219212). % Programming Languages.
Yes.
:- likes(papon, 403111). % Chemistry.
No.
:- likes(Who, 204219). % Theory of Computing?
Who = pattarin
?- append([],[a,b,c],L).
L = [a,b,c]