ARTIFICIAL
INTELLIGENCE
Introduction to Prolog
Prolog
◦ PROgramming LOGic
◦ It is most widely used programming language
◦ Main applications:
◦ Artificial Intelligence: expert systems, natural language
processing, …
◦ Mathematics: theorem proving, …
Prolog
◦ Prolog is a declarative language
◦ A program is a collection of axioms from which theorems
can be proven.
◦ Rather than describing how to compute a solution, a program
consists of a database of facts and logical relationships (rules)
◦ Rather than running a program to obtain a solution, the user
asks a question. When asked a question, the run time system
searches through the database of facts and rules to determine
the answer by logical deduction
Prolog Program
◦ Set of specifications in the first-order predicate calculus
describing the objects and relations in a problem domain.
◦ Set of specifications – database
◦ Prolog interpreter responds to questions about this database
◦ Uses pattern directed search
◦ Process queries, searching the database in left to right depth-first
order
◦ It basically checks if the query is a logical sequence of the
database of the specifications
Syntax for Predicate Calculus
Programming
ENGLISH PREDICATE PROLOG
CALCULUS
and ,
or ;
implication :-
not not
Prolog as Logic
◦ Logic formula:
◦ Logic OR – AB : A;B.
◦ Logic AND – AB : A,B.
◦ Logic NOT - A : not A.
◦ Logic implication: A→B : B:-A.
◦ Each expression terminates with a period ‘.’
Simple Example
◦ Describing George’s, Kate’s and Susie’s likes and dislikes
using following predicates:
◦ likes(george, kate).
◦ likes(george, susie).
◦ likes(george, chocolate).
◦ likes(susie, chocolate).
◦ likes(kate, sushi).
◦ likes(kate, susie).
Queries given about the
database
◦ ?- likes(george, kate).
◦ yes
◦ ?- likes(kate, susie).
◦ yes
◦ ?- likes(george, X).
◦ X = kate
◦ ;
◦ X = susie
◦ ;
◦ X = chocolate
◦ ;
◦ no
Defining Rules
◦ Only one predicate is permitted on the left hand side
◦ This must be a positive literal (negation cannot be applied
to it)
◦ All predicate calculus expressions that contain implication
or equivalence must be reduced to this form – horn clause
logic
◦ Left side of the implication must be a single positive literal
Adding a rule in likes/dislikes
database
◦ In order to check if two people are friends based on their
likes/dislikes
◦ A rule can be constructed as follows:
◦ friends(X, Y) :- likes(X,Z), likes(Y,Z).
◦ ?- friends(george, susie).
◦ yes
A Simple Rule
◦ English description:
◦ If X is male, F is a father of X, M is a mother of X, F is a father of Y, M is a
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).
Another example
◦ Facts
◦ bigger(elephant, horse).
◦ bigger(horse, donkey).
◦ bigger(donkey, dog).
◦ bigger(donkey, monkey).
◦ Queries
◦ ?- bigger(donkey, dog).
◦ Yes
◦ ?- bigger(monkey, elephant).
◦ No
Another example
◦ Facts
◦ bigger(elephant, horse).
◦ bigger(horse, donkey).
◦ bigger(donkey, dog).
◦ bigger(donkey, monkey).
◦ Queries
◦ ?- bigger(elephant, monkey).
◦ No
◦ To incorporate this, we will have to add a new relation – isbigger
Another example
◦ is_bigger(X, Y) :- bigger(X, Y).
◦ is_bigger(X, Y) :- bigger(X, Z), is_bigger(Z, Y).