PROLOG AS AI PROGRAMMING
LANGUAGE
PRINCIPLE OF LOGICAL
PROGRAMMING
Logic programming is a programming paradigm based
on mathematical logic.
In this paradigm the programmer specifies relationships
among data values (this constitutes a logic program) and
then poses queries to the execution environment (usually
an interactive interpreter) in order to see whether certain
relationships hold.
2
PRINCIPLE…
The Prolog execution environment, on the other hand,
doesn’t so much “compute” an answer, it “deduces” an
answer from the relation definitions at hand.
Though the basic elements of Prolog are type-less, most
implementations have been enhanced to include
character, integer values and operations.
3
FACTS AND RULES
Prolog is referred to as a declarative language because
all program statements are definitional.
In particular, a Prolog program consists of facts and rules
which serve to define relations (in the mathematical
sense) on sets of values.
4
FACTS…
Everything in Prolog is defined in terms of two
constructs: the fact and the rule.
Fact: is a Prolog statement consisting simply of an identifier
followed by an n-tuple of constants.
The identifier is interpreted as the name of a (mathematical)
relation and the fact states that the specified n-tuple is in the
relation.
5
FACTS…
A fact states that a certain tuple of values satisfies a
predicate unconditionally .
A rule , on the other hand, is a Prolog statement which
gives conditions under which tuples satisfy a predicate.
Hence, a fact is just a special case of a rule, where the
condition is always satisfied
6
THE SWI-PROLOG ENVIRONMENT
SWI-Prolog, developed at the Swedish Institute of
Computer Science.
It can be executed in two different forms: from the
command line or via an Emacs interface
Prolog file has the extension .pl
7
THE SWI-PROLOG…
SWI-Prolog interpreter reads a program and then allows
the user to compute a query based on the definitions
(facts and rules).
A query “asks” the system if there are any data values
which satisfy a particular predicate (or combination).
8
STARTING PROLOG AND LOADING A
PROGRAM
This file can be opened in Prolog from the Start Menu,
by opening .pl files in the Windows explorer or using the
following command in the Prolog application.
?- [swi('demo/likes')].
Or if you have the absolute address we can use the
following
?- ['C:/Users/Documents/Prolog/parents'].
9
EXECUTING A QUERY
After loading a program, one can ask Prolog queries
about the program. The system responds with X =
<value> if it can prove the goal for a certain X.
The user can type the semi-colon (;) if (s)he wants
another solution, or RETURN if (s)he is satisfied, after
which Prolog will say True. If Prolog answers False, it
indicates it cannot find any (more) answers to the query.
Finally, Prolog can answer using an error message to
indicate the query or program contains an error.
10
SYNTAX OF PROLOG
What exactly are facts, rules and queries built out of?
Prolog Terms
11
PROLOG TERMS
Terms
Simple Terms Complex Terms
Constants Variables
Atoms Numbers
12
ATOMS
A sequence of characters of upper-case letters, lower-case
letters, digits, or underscore, starting with a lowercase
letter
Examples: butch, big_bang_theory, playGuitar
An arbitrary sequence of characters enclosed in single
quotes
Examples: 'Vincent', 'Five dollar shake', '@$%'
A sequence of special characters
Examples: : , ; . :-
13
NUMBERS
Integers:
12, -34, 22342
Floats:
34573.3234, 0.3435
14
VARIABLES
A sequence of characters of uppercase letters,
lower-case letters, digits, or underscore, starting
with either an uppercase letter or an underscore
Examples: X, Y, Variable, Vincent, _tag
15
COMPLEX TERMS
Atoms, numbers and variables are building blocks
for complex terms
Complex terms are built out of a functor
(identifier) directly followed by a sequence of
arguments (tuple).
Arguments are put in round brackets, separated by
commas
The functor must be an atom 16
EXAMPLES OF COMPLEX TERMS
Examples re
parent (pam, bob).
female(pam).
male(tom).
Complex terms inside complex terms:
hide(X,father(father(father(jon))))
17
ARITY
The number of arguments a complex term has is
called its arity.
Examples:
female(pam). is a term with arity 1
parent (pam, bob). has arity 2
father(father(joy)) arity 1
In prolog documentation the arity can be described as
female/1
parent/2
father/1
18
PROLOG
Defining relations by facts
It is especially well suited for solving problems that
involves objects and relations between objects.
19
Example : The Family Tree
Pam Tom
Bob Liz
Ann
Pat
Jim 20
The fact that Tom is a parent of Bob can be written in Prolog as:
parents (tom, bob).
21
The whole family tree of the above figure is defined by the
following Prolog program:
parent (pam, bob).
parent (tom, bob).
parent (tom, liz).
parent (bob, ann).
parent (bob, pat).
parent (pat, jim).
22
When this program has been communicated to the Prolog system,
prolog can be posed some questions about the parent relation.
For example: is Bob a parent of Pat? This questions can be
communicated to the Prolog system by typing into the terminal:
?-parent (bob, pat).
Having found this as an asserted fact in the program, Prolog will
answer: yes/true.
If it doesn’t find a clause that satisfies the relation, Prolog will
answer: no/false.
23
Who is Liz’s parent?
?-parent (X, liz).
The Prolog will tell us what the value of X is such that the above
statement is true. So the answer is: X=tom.
The question who are Bob’s children? Can be communicated to
Prolog as:
?-parent (bob, X).
24
We can ask other complicated questions like who is a grandparent
of jim? Our program don’t have a grandparent relation so the
query has to be broken down into two steps,
Who is a parent of jim? Assume that this is some Y.
Who is a parent of Y? Assume that this is some X.
Such a composed query is written in Prolog as a sequence of two
simple ones:
?-parent (Y, jim), parent (X,Y).
The answer will be:
X=bob
25
Y=pat
Defining relations by rules
We could define offspring in a similar way as the parent relation;
that is, by simply providing a list of simple facts about the
offspring relations
offspring (liz, tom).
However, the offspring relation can be defined much more
elegantly by making use of the fact that it is the inverse of
parent, and that parent has already been defined. This alternative
way can be based on the following logical statement:
26
For all X and Y,
Y is an offspring of X if
X is a parent of Y.
This formulation is already close to the formalism of Prolog. The
corresponding Prolog clause which has the same meaning is:
27
Before defining this relationship we define the female relation
female(pam).
male(tom).
male(bob).
female(liz).
female(pat).
female(ann).
male(jim).
Let us now add more family relation to our example program. The
specification of the mother relation can be based on the following logical
statement:
For all X and Y,
X is the mother of Y if
X is a parent of Y and
X is a female.
This is translated into Prolog as the following rule:
mother (X,Y):- parent (X,Y), female(X).
28
A comma between two conditions indicates the conjunction of the
conditions meaning that both conditions have to be true.
Exercise
1. Who are Tom’s grandchildren?
2. Do Ann and Pat have a common parent?
3. Who is pat’s grandparents?
4. Write a father relation in prolog ?
5. Write a grandparent relation in prolog ?
6. Write a sister relation in prolog?
7. Write a brother relation in prolog?
8. Write a aunt relation in prolog?
9. Write a married relation in prolog? 29
ANSWER
THE ANSWER IS IN THE PARENT.PL
FILE WHICH ACCOMPANIES THIS SLIDE.
30
WRITING FACTS ABOUT DIRECTED
GRAPH
b e
c g
a
d f
31
ANSWER
THE ANSWER IS IN THE GRAPH.PL
FILE WHICH ACCOMPANIES THIS SLIDE.
32
NEGATION AS FAILURE
No equivalent of logical not in Prolog:
Prolog can only assert that something is true.
Prolog cannot assert that something is false.
Prolog can assert that the given facts and rules do not allow
something to be proven true
33
PROPER USE OF NEGATION AS FAILURE
loves(bill,X) :- pretty(X), female(X), not(loves(tom,X)).
loves(tom,X):- famous(X), female(X), not(dead(X)).
female(marilym_monroe).
female(cindy_crawford).
female(martha_stewart). ?-loves(tom,X).
female(girl_next_door). X=cindy_crawford;
X=martha_stewart;
famous(marilym_monroe). ?-loves(bill,X).
famous(cindy_crawford). X=marilyn_monroe;
X=girl_next_door;
famous(martha_stewart).
pretty(marilym_monroe).
pretty(cindy_crawford).
pretty(girl_next_door).
34
dead(marilym_monroe).
PROLOG: FAIL & TRUE PREDICATES
How to represent “Mary likes all animals but snakes”?
If X is a snake then Mary likes X is not true
otherwise if X is an animal then Mary likes X
snake(cobra).
cat(persian).
animal(X):-snake(X);cat(X).
likes(mary,X):-snake(X),!,fail.
likes(mary,X):-animal(X).
?- likes(mary,cobra).
no
rule 1 failed because of fail and the ! told the interpreter not to try
rule 2 because since we have reached there(aka to the !), it must
35
be a snake
UNIFICATION
Working definition – two terms unify:
if they are the same term, or
if they contain variables that can be uniformly
instantiated with terms in such a way that the
resulting terms are equal
When Prolog unifies two terms, it performs
all the necessary instantiations, so that the
36
terms are equal afterwards
UNIFICATION
If T1 and T2 are constants, then T1 and T2 unify if
they are the same atom, or the same number
If T1 is a variable and T2 is any type of term, then T1
and T2 unify, and T1 is instantiated to T2 (and vice
versa)
If T1 and T2 are complex terms then they unify if:
They have the same functor and arity, and
all their corresponding arguments unify, and
37
the variable instantiations are compatible
EXERCISE ON UNIFICATION
?- mia=mia. ?-K(s(g),Y)=K(X,t(k)).
True X=s(g)
Y=t(k)
?-mia=joy. ?- k(s(g),t(k)) = k(X,t(Y)).
X=s(g)
False Y=k
?- loves(X,X) = loves(marsellus,mia).
?- mia=X. no
X=mia
?- X=mia,X=joy.
38
False