Prolog Tutorial 1
Prolog Tutorial 1
Computational Intelligence
Basics of
PROLOG
Suchitra Agrawal
Research Scholar
•
Introduction Descriptive Language
• Describe using facts
• Knowledge Database consists of facts
• Define rules
• SWISH: SWI-Prolog for Sharing
Prolog Compiler
• GNU Prolog Compiler
Syntax
Terms Clauses
Atoms Facts
Numbers Rules
Variables Programs
Compound Terms Queries
Execution • Create Knowledge Base/Facts
Prolog
Examples
1.
Facts:
bigger(elephant, horse).
bigger(horse, donkey).
bigger(donkey, dog).
bigger(donkey, monkey).
Query:
a. ?- bigger(donkey, dog). Answer: Yes
b. ?- bigger(monkey, elephant). Answer: No
c. ?- bigger(elephant, monkey). Answer: No Why?
Examples
2.
Rules for previous data:
is_bigger(X, Y) :- bigger(X, Y).
is_bigger(X, Y) :- bigger(X, Z), is_bigger(Z, Y).
Query:
?- bigger(elephant, monkey). Answer: Yes
Examples
3.
Query:
?- is_bigger(X, donkey).
Examples
Answer: X = horse
4.
Query:
?- is_bigger(donkey, X), is_bigger(X, monkey).
Answer:
No
Example:
Query:
?- write(‘Hello World’), n1.
Print Output
Output:
Hello World
Yes
Query:
?- X = elephant, write(X), n1.
Matching Query:
?- f(a, g(X, Y)) = f(X, Y), Z = g(W, h(X)).
Answer:
X=a
Y = h(a)
Z = g(a, h(a))
W=a
Yes
All men are mortal. Socrates is a man.
-> Hence, Socrates is mortal.
Implement in Prolog:
Goal
Execution mortal(X) :- man(X).
man(socrates).
?- mortal(socrates).
Yes
• Comment: /* Comment. */ Or
% Comment
• Spaces after every comma
Writing Style • Separate clauses by one or more blank
lines
• One predicate per line with indentation
THANK YOU
QUERIES??