0% found this document useful (0 votes)
53 views12 pages

Artificial Intelligence: Lecture 11: Prolog-I

The document discusses Prolog, a logic programming language. It explains how Prolog represents facts and rules, and how a Prolog program executes by matching questions to facts and rules through substitution and backtracking. It provides examples of representing family relationships like parents, siblings, and ancestors in Prolog through facts, rules, and recursion. Prolog finds solutions through recursively breaking problems into subproblems and integrating consistent subsolutions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views12 pages

Artificial Intelligence: Lecture 11: Prolog-I

The document discusses Prolog, a logic programming language. It explains how Prolog represents facts and rules, and how a Prolog program executes by matching questions to facts and rules through substitution and backtracking. It provides examples of representing family relationships like parents, siblings, and ancestors in Prolog through facts, rules, and recursion. Prolog finds solutions through recursively breaking problems into subproblems and integrating consistent subsolutions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 12

Artificial Intelligence

Lecture 11: Prolog-I

Represent Facts and Rules


Facts (propositions): e.g.
male(philip).

Rules (implications): e.g.


parent(X, Y) := father(X, Y).
parent(X, Y) := mother(X, Y).
parent(X, Y) := father(X, Y); mother(X, Y).

Questions (start point of execution):


?-parent(X, Y).

e.g.

Prolog as Logic
Logic formula:

Description A e.g. philip is male.


Logic OR A B : A; B.
Logic AND A B : A, B.
Logic NOT -- A : not A.
Logic implication: A B : B :- A.

Each expression terminates with a .

A Simple Rule
English description:
If
X is male,
F is the father of X,
M is the mother of X,
F is the father of Y,
M is the mother of Y
Then
X is a brother of Y

Logic formula:
male(X)
father(F, X)
mother(M, X)
father(F, Y)
mother(M, Y)

brother(X, Y)

Prolog:
brother(X, Y) :male(X),
father(F,X),
mother(M,X),
father(F,Y),
mother(M, Y).

A Simple Program
A Prolog program is a sequence of facts and rules
brother(X, Y) :- male(X), parent(P, X), parent(P, Y).
parent(X, Y):- father(X, Y); mother(X, Y).
male(philip).
father(bill, philip).
father(bill, jane).
?- brother(philip, jane).

Program Execution
Start from the question
Matching match the question with facts and rules by
substitution (try)
Unification looking for unified solution (consistent
solution)
Backtracking once fails, go back to try another case
Divide-and-conquer

divide problem into sub problems


solve all sub problems
integrate sub solutions to build the final solution
Solutions to all sub problems must be consistent

Program Trace Tree


brother(philip, jane)

male(philip)

parent(P, philip)

parent(P, jane)

X=philip

father(P,philip)

mother(P,philip)

P=bill
male(philip)

father(bill,philip)

father(P,jane)
P=bill
father(bill, jane)

mother(P,jane)

Another Example
Facts
mother(jane, george).
father(john, george).
brother(bill, john).

Rules:
parent(X, Y) :- mother(X, Y).
parent(X, Y) :- father(X, Y).
uncle(X, Y) :- parent(P, Y), brother(X, P).

Question:
?- uncle(X, george).

Program Execution Trace


uncle(X, george)
Y/george

parent(P, Y)

brother(X, P)
X/bill, P/john

P/X, Y/george

mother(X, Y)
X/jane

mother(jane, george)

father(X, Y)
X/john

father(john, george)

brother(bill, john)

Recursion
Base case a set of rules for direct solution
Normal case another set of rules for indirection
solution
E.g.
child(X, Y) :- mother(Y, X).
child(X, Y) :- father(Y, X).
descendant(X, Y) :- child(X, Y). base case
descendent(X, Y) :- child(X, Z), descendent(Z, Y).

Recursive Definition of Descendent

predecessor(X,Z) :parent(X,Y),
predecessor(Y,Z).

Family Tree
parent(pam,bob).
parent(tom,bob).
parent(tom,liz).
parent(bob,ann).
parent(bob,pat).
parent(pat,jim).
male(tom).
male(bob).
male(jim).
female(pam).
female(ann).
female(liz).
female(pat).
offspring(Y,X) :parent(X,Y).

mother(X,Y) :parent(X,Y),
female(X).
grandparent(X,Z) :parent(X,Y),
parent(Y,Z).
sister(X,Y) :parent(Z,X),
parent(Z,Y),
female(X),
different(X,Y).
predecessor(X,Z) :parent(X,Z).
predecessor(X,Z) :parent(X,Y),
predecessor(Y,Z).

You might also like