Logic Programming
Declarative Programming
Imperative (Conventional) Languages Properties
- State-oriented: each statement execution changes the abstract machine state
- Destructive assignment: update the value
- Side effects
- Difficult to read, write, and verify programs
Declarative languages
- Higher program understandability and verifiability
- Referential transparency: no side effect
Prolog
Divided into two stages:
- Building a program (asserting what is true)
- Running a program (asking for consequences of what has been asserted)
A Prolog program is a collection of assertions
- Facts
- Rules
They are used to express relationships amongst some objects
Facts
a statement that asserts a relationship between objects
Facts can be of various arities
1
2father(edwyn, caroline). /* "Edwyn is the father of Caroline." */
give(tom, apple, teacher). /* "Tom gives an apple to the teacher." */Unary facts denote properties
1
2red(apple). /* "Apple is red." */
number(three). /* "Three is a number." */
Query
Prolog is executed by posting a question (query), which makes a request for information based on the facts and rules defined in a program.
- provided facts
1
2
3
4father(george, randy).
mother(kari, peter).
father(randy, kari).
mother(kari, mary). - query
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20?- father(george, randy). /* yes */
?- mother(kari, june). /* no */
/* Is there an X such that X is the father of randy ? */
?- father(X, randy).
/* X = george */
/* Is there an X such that Kari is the mother of X ? */
?- mother(kari, X).
/* X = mary;
X = peter */
/* Are there X and Y such that X is the father of Y ? */
?- father(X, Y).
/* X = randy X = george
Y = kari; Y = randy */
/* Is there an X such that X is the mother of him/herself ? */
?- mother(X, X).
/* no */
- provided facts
In general, query can carry many subqueries:
- use
,to conjunct the subqueries1
2
3
4/* Are there X & Z such that X is the father of Z and Z is the father of Kari? */
?- father(X, Z), father(Z, kari).
/* X = george
Z = randy */ - When $m=0$, it is an empty query
- use
Terms
- Prolog programs are constructed from terms which can be constants, variables or structures
Constants
- represent a specific object
- must start with a lower-case letter
karl - they can be number (but we just focus on the logical part so we seldom use number)
Variables
- must start with a upper-case letter
X, Y, Z
- must start with a upper-case letter
Structures
- They consist of a functor and a number of arguments
bonks(big_doge, small_doge)- functor:
bonks - arguments:
big_doge,small_doge
- Advanced usage:
- Suppose we want to represent a location on a map, we can use a structure with two components a latitude (
p) and a longitude (q)loc(p, q)
- Suppose we want to represent a location on a map, we can use a structure with two components a latitude (
- They consist of a functor and a number of arguments
Functor
- glue some objects into a composite object
- A functor may have only one argument (for labeling an argument)
south(32)loc(north(45), east(72))
Rules
Some concepts are based on the others
e.g., To find a parent, we have to ask two questions to obtain a single piece of information
1
2?- father(X,kari).
?- mother(X,kari).- either of the above queries succeeds, a parent of Kari is found
We can translate the “parent” concept into the “father” and “mother” concepts using Rules
Form of Rules:
Variables in a rule are universally quantified
1
2
3
4
5/* Parent: For all X & Y, X is a parent of Y if X is a father or mother of Y. */
parent(X, Y) :- father(X, Y).
parent(X, Y) :- mother(X, Y).
?- parent(X, kari). /* X = randy */More exmaples:
1
2
3
4
5
6
7/* Grandfather: For all X, Y & Z, X is a grandfather of Z
if X is a father of Y and Y is a parent of Z. */
grandfather(X, Z) :- father(X, Y), parent(Y, Z).
?- grandfather(X, Y).
/* X = george X = randy X = randy
Y = kari; Y = mary; Y = peter */Rule for ancestor
- A parent is an ancestor
ancestor(X, Y) :- parent(X, Y) - An ancestor of
X: a parent of an ancestor of an individualX
ancestor(X, Z) :- parent(X, Y), ancestor(Y, Z)
- A parent is an ancestor
- Unconditional Rules
- the rules with no body $n=0$ is an unconditional rule
1
2loves(X, doge). /* Everyone loves doge */
loves(doge, X). /* doge loves Everyone */
- the rules with no body $n=0$ is an unconditional rule
Answer and Response
If the query has variables
- the answer subsitution may assign a constant to some of the variables in the query
1
2
3
4?- father(X, randy).
X = george /* Answer subsitution */
father(george, randy) /* Answer */
yes /* Response */
- the answer subsitution may assign a constant to some of the variables in the query
If the query has no variables
- the answer subsitution is vacuous (i.e., the query itself is the Answer)
1
2?- father(george, randy).
yes /* Response */
- the answer subsitution is vacuous (i.e., the query itself is the Answer)
When a program consists of facts only
- for the correct answer, the answer must literally appear as a fact in the program
1
2?- grandfather(george, X).
X = kari
- for the correct answer, the answer must literally appear as a fact in the program
Deriving Answers
- Steps for deriving answers in Prolog
Reduction
- A query can be replaced to another by matching the head of the rule.
- If a sub-query matched a fact, then that sub-query can be reduced and applying the matching substitution to the entire resulting query.
Derivation
a sequence of queries and substitutions
Rules:
1
2
3
4grandfather(X,Z) :- father(X,Y), parent(Y,Z). /* R1 */
parent(X,Y) :- mother(X,Y). /* R2 */
mother(caroline, nina). /* R3 */
father(edwyn, caroline). /* R4 */Derivation:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15Q1: ?- grandfather(U, nina).
/* Replace Q1 with R1 and Subsitute X = U & Z = nina */
Q2: ?- father(U, Y), parent(Y, nina). /* By R1 */
/* Reduce "father" subquery and Subsitute U = edwyn & Y = caroline */
Q3: ?- parent(caroline, nina) /* By R4 */
/* Replace Q3 with R2 */
Q4: ?- mother(caroline, nina). /* By R2 */
/* Reduce "mother" subquery */
Q5: {} /* By R3 */
/* Successful: grandfather(edwyn, nina) */
- Successful Derivation
- it ends in the empty query
- variables in rules serve only as place holders
1
2
3parent(A,B) :- father(A,B).
/* Is equivalent to */
parent(X,Y) :- father(X,Y). - the matching is two-way (unification)
1
2
3father(X,caroline) /* matches */ father(edwyn,Y)
X = edwyn
Y = caroline
Axiomatization of Natural Numbers
0: the number 0s(X): the successor ofX(orX+1)s(0) = 1, s(s(0)) = 2, ...- Rules: summation
1
2sum(0,X,X). /* R1 */
sum(s(X),Y,s(Z)) :- sum(X,Y,Z). /* R2: If X+Y=Z, then s(X)+Y=s(Z) */ Derivation
Example 1:
1
2
3
4
5
6
7
8
9
10
11Q1: ?- sum(s(0),s(0),X).
/* Unifying sum(s(0),s(0),X) with sum(s(A),B,s(C)) */
/* Using A = 0, B = s(0), X = s(C) */
Q2: ?- sum(0, s(0), C).
/* Unifying sum(0, s(0), C) with sum(0,D,D) */
/* Using D = s(0), D = C = s(0) */
Q3: ?- {}
Answer: X = s(C) = s(s(0))Example 2:
1
2
3
4
5
6
7
8
9
10
11Q1: ?- sum(X, s(0), s(s(0))).
/* Unifying sum(X, s(0), s(s(0))) with sum(s(A),B,s(C)) */
/* Using X = s(A), Y = s(0), s(C) = s(s(0)) */
Q2: ?- sum(A, s(0), s(0)).
/* Unifying sum(A, s(0), s(0)) with sum(0,D,D) */
/* Using A = 0, D = s(0) */
Q3: ?- {}
Answer: X = s(A) = s(0)Example 3:
1
2
3
4
5
6
7Q1: ?- sum(X,Y,s(s(0))).
/* Unifying sum(X,Y,s(s(0))) with sum(0,X,X) */
/* Using X = 0, Y = A = s((0)) */
Q2: ?- {}
Ans: X = 0, Y = s((0))1
2
3
4
5
6
7
8
9
10
11Q1: ?- sum(X,Y,s(s(0))).
/* Unifying sum(X,Y,s(s(0))) with sum(s(A),B,s(C)) */
/* Using X = s(A), Y = B, s(C) = s(s(0)) */
Q2: ?- sum(A, B, s(0)).
/* Unifying sum(A, B, s(0)) with sum(0,D,D) */
/* Using A = 0, B = D = s(0) */
Q3: ?- {}
Ans: X = s(0), Y = s(0)1
2Answer: X = 0 X = s(0)
Y = s((0)); Y = s(0);
Representing Binary Trees
Binary Trees in Prolog

- Rules:
1
2
3btMember(E,bt(L,E,R)). /* Rule 1 */
btMember(E,bt(L,Rt,R)) :- btMember(E,L). /* Rule 2 */
btMember(E,bt(L,Rt,R)) :- btMember(E,R). /* Rule 3 */
- Rules:
Derivation
Example 1:
1
2
3
4
5
6
7Q1: ?- btMember(4,bt(bt(nil,2,bt(nil,4,nil)),1,bt(bt(nil,5,nil),3,nil))).
/* Unifying with btMember(E,bt(L,E,R)) */
/* E=4, L=bt(nil,2,bt(nil,4,nil)), E=1, R=bt(bt(nil,5,nil),3,nil) */
Q2: ?- {}
yesExample 2:
1
?- btMember(E,bt(bt(nil,2,bt(nil,4,nil)),1,bt(bt(nil,5,nil),3,nil))).
E = 1:
Q1: ?- btMember(E,bt(bt(nil,2,bt(nil,4,nil)),1,bt(bt(nil,5,nil),3,nil))).1
2
3
4/* Unifying with Rule 1 */
/* E=1, L=bt(nil,2,bt(nil,4,nil)), R=bt(bt(nil,5,nil),3,nil) */
Q2: ?- {}
Ans: E = 1E = 2:1
2
3
4
5
6Q1: ?- btMember(E,bt(bt(nil,2,bt(nil,4,nil)),1,bt(bt(nil,5,nil),3,nil))).
/* Unifying with Rule 2 */
Q2: ?- btMember(E,bt(nil,2,bt(nil,4,nil))).
/* E=2, L=nil, R=bt(nil,4,nil) */
Q3: ?- {}
Ans: E = 2E = 3:1
2
3
4
5
6Q1: ?- btMember(E,bt(bt(nil,2,bt(nil,4,nil)),1,bt(bt(nil,5,nil),3,nil))).
/* Unifying with Rule 2 */
Q2: ?- btMember(E,bt(bt(nil,5,nil),3,nil)).
/* E=3, L=bt(nil,5,nil), R=nil */
Q3: ?- {}
Ans: E = 3E = 4:1
2
3
4
5
6
7
8Q1: ?- btMember(E,bt(bt(nil,2,bt(nil,4,nil)),1,bt(bt(nil,5,nil),3,nil))).
/* Unifying with Rule 2 */
Q2: ?- btMember(E,bt(nil,2,bt(nil,4,nil))).
/* Unifying with Rule 3 */
Q3: ?- btMember(E, bt(nil,4,nil))
/* E=4, L=nil, R=nil */
Q4: ?- {}
Ans: E = 4E = 5:1
2
3
4
5
6
7
8Q1: ?- btMember(E,bt(bt(nil,2,bt(nil,4,nil)),1,bt(bt(nil,5,nil),3,nil))).
/* Unifying with Rule 3 */
Q2: ?- btMember(E,bt(bt(nil,5,nil),3,nil)).
/* Unifying with Rule 2 */
Q3: ?- btMember(E, bt(nil,5,nil))
/* E=5, L=nil, R=nil */
Q4: ?- {}
Ans: E = 5Answer:
1
E = 1 E = 2 E = 3 E = 4 E = 5


