Prolog Tutorial
Prolog Tutorial
Example: human(X)
human: U → { true, false }
human(tree) = false
human(paul) = true
Example: mother(X,Y)
mother: U × U → { true, false }
mother(betty,paul) = true
mother(giraffe,peter) = false
Another way of looking at predicates is as properties of objects.
Note: if we do not make another assumptions on the universe then the
universe is usually taken as the set of all possible objects.
First-Order Logic
We can combine predicates and
quantified variables to make statements
on sets of objects
∃X[mother(X,paul)]
there exists an object X such that X is the
mother of Paul
∀Y[human(Y)]
for all objects Y such that Y is human
First-Order Logic
Logical Connectives: and, or, not
∃F ∀C[parent(F,C) and male(F)]
There exists an object F for all objects C such
that F is a parent of C and F is male.
∀X[day(X) and (rainy(X) or snowy(X))]
For all objects X such that X is a day and X is
either rainy or snowy.
First-Order Logic
If-then rules: A ⇒ B
∀X∀Y[parent(X,Y) and female(X) ⇒ mother(X,Y)]
For all objects X and for all objects Y such that if X is a
parent of Y and X is female then X is the mother of Y.
∀Q[human(Q) ⇒ mortal(Q)]
For all objects Q such that if Q is human then Q is
mortal.
We can combine quantified variables,
predicates, logical connectives, and
implication into WFF’s (well-formed formulas)
First-Order Logic
Modus Ponens
human(socrates)
∀Q[human(Q) ⇒ mortal(Q)]
--------------------------------------
∴ mortal(socrates)
Conjunctions only!
“Deduction is Computation”
J. Alan Robinson: A Machine-Oriented Logic Based on the Resolution Principle. J. ACM 12(1): 23-41 (1965)
Basic Programs
Prolog programs follow the FOL style: assert truth
and use the rules/implications to compute the
consequences of these assertions.
male(phil).
Facts, Prolog will treat these as true and enters
male(john).
them into its knowledgebase.
female(betty).
In other words, a query is a goal that Prolog is attempting to satisfy (prove true).
The interpretation of this query is: prove that there is at least one object X
that can be considered a parent of liz, or formally, prove that
∃x[parent(x,liz)]
holds.
NOTE: Prolog will return all objects for which a query evaluates to true.
A Prolog Program
% a simple prolog program
female(pam).
female(liz).
female(ann).
female(pat).
male(tom). Parent
male(bob). Relation
male(jim).
parent(pam,bob).
parent(tom,bob).
parent(tom,liz).
parent(bob,ann).
parent(bob,pat).
parent(pat,jim).
A Family Tree
Example Queries:
?- female(pam).
?- female(X). ∃X[female(X)]?
?- parent(tom,Z).
?- father(Y).
Compound Queries
A compound query is the conjunction of individual simple queries.
?- parent(X,Y) , parent(Y,ann).
∃X,Y[parent(X,Y) ∧ parent(Y,ann)]
When Prolog tries to satisfy this compound goal, it will make sure that the
two Y variables always have the same values.
Prolog uses unification and backtracking in order to find all the solutions
which satisfy the compound goal.
Prolog Rules
Prolog rules are Horn clauses, but they are written “backwards”, consider:
head body
You can think of a rule as introducing a new “fact” (the head), but the fact is
defined in terms of a compound goal (the body). That is, predicates defined as
rules are only true if the associated compound goal can be shown to be true.
Prolog Rules
% a simple prolog program
female(pam).
female(liz).
female(ann).
female(pat).
Queries:
male(tom).
?- mother(pam,bob).
male(bob).
male(jim). ?- mother(Z,jim).
?- mother(P,Q).
parent(pam,bob).
parent(tom,bob).
parent(tom,liz).
parent(bob,ann).
parent(bob,pat).
parent(pat,jim).
mother(X,Y) :- female(X),parent(X,Y).
Prolog Rules
The same predicate name can be defined by multiple rules:
sibling(X,Y) :- sister(X,Y) .
sibling(X,Y) :- brother(X,Y).
Socrates Revisited
Consider the program relating humans to mortality:
mortal(X) :- human(X).
human(socrates).
?- mortal(socrates).
True or false?
Declarative vs. Procedural
Meaning
When interpreting rules purely as Horn clause logic statement → declarative
Consider:
mother(X,Y) :- female(X),parent(X,Y).
Prolog Terms
A term in Prolog is anything that cannot
be considered a predicate
Simple object names, e.g. betty, john
Simple structures, e.g. couple(betty, john),
in this case the important part here is that
couple does not appear as a predicate
definition
Lists
Lists & Pattern Matching arity
?- a = a.
true
?- a = b.
false
?- a = X.
X=a
?- X = Y.
true
Lists & Pattern Matching
Lists – a convenient way to represent abstract
concepts
Prolog has a special notation for lists.
?- [a,b,c] = [X|Y];
X=a
Y = [b,c]
?- [a] = [Q|P];
Q=a
P=[]
Lists - the First Predicate
The predicate first/2: accept a list in the first argument and return
the first element of the list in second argument.
last([A],A).
last([A|L],E) :- last(L,E).
Lists - the Append Predicate
The append/3 predicate: accept two lists in the first two parameters, append
the second list to the first and return the resulting list in the third parameter.
Examples:
?- X is 10 + 5;
X = 15
Precedence and associativity
of operators are respected.
?- X is 10 + 5 * 6 / 3;
X = 20
Prolog – Arithmetic
Example: write a predicate definition for length/2 that takes a list
in its first argument and returns the length of the list in its second
argument.
length([ ], 0).
length(L, N) :- L = [H|T], length(T,NT), N is NT + 1.
Prolog – Arithmetic
Example: we can also use arithmetic in compound statements.
?- X is 5, Y is 2 * X.
X=5
Y = 10
Prolog – I/O
write(term)
is true if term is a Prolog term, writes term to the terminal.
read(X)
is true if the user types a term followed by a period, X
becomes unified to the term.
nl
is always true and writes a newline character on the
terminal.
?- write([1,2]).
[1, 2]
?- read(Q).
|: [1,2,3].
Q = [1, 2, 3]
Prolog – I/O
Example: write a predicate definition for fadd/1 that takes a list of
integers, adds 1 to each integer in the list, and prints each integer
onto the terminal screen.
fadd([ ]).
fadd([ H | T ]) :- I is H + 1, write(I), nl, fadd(T).
Member Predicate
Write a predicate member/2 that takes a list as its first argument and
an element as its second element. This predicate is to return true if
the element appears in the list.
member([E|_],E).
member([_|T],E) :- member(T,E).
Exercises
(1) Define a predicate max/3 that takes two numbers as its first two
arguments and unifies the last argument with the maximum
of the two.
(2) Define a predicate maxlist/2 takes a list of numbers as its first
argument and unifies the second argument with the maximum
number in the list. The predicate should fail if the list is empty.
(3) Define a predicate ordered/1 that takes a list of numbers as its
argument and succeeds if and only if the list is in non-decreasing
order.
The ‘Cut’ Predicate
The Cut predicate ‘!’ allows us to control
Prolog’s backtracking behavior
The Cut predicate forces Prolog to commit to
a set of choice points
Consider the following code:
different(A,B) :- A=B,!,fail.
different(_,_).
?- a.