You are on page 1of 2

A Prolog Program

female(liz).
Prolog Reminder female(irene).
male(brendon).
male(emlyn).

ACSC 368 parent(liz,brendon).


parent(liz,emlyn).
parent(irene,liz).
Harris Papadopoulos
mother(X,Y) :- parent(X,Y), female(X).
grandparent(X,Y) :- parent(X,Z), parent(Z,Y).

Remember Remember
• Each statement, ending in a full stop, is • Variables start with an upper case letter or
called a clause. _. There are no global variables in Prolog,
• female, male are unary predicates. so the X and Y in rule for mother are
• parent, mother, grandparent are binary different from the X and Y in the rule for
predicates. grandparent.
• :- means IF and , means AND • All clauses are statements of what is
assumed to be true, and each clause is
• The clauses defining mother and logically independent of the others. Hence
grandparent predicates are rules, and the reordering clauses does not change the
rest are facts. meaning of the program.

Program Meaning Queries


Liz is female and Irene is female To solve a problem about a family, we use a query.
Brendon is male and Emlyn is male E.g. to discover who Liz is the mother of, enter query:
?- mother(liz,Child).
Liz is a parent of Brendon and Liz is a parent of Emlyn
Child = brendon;
and Irene is a parent of Liz
Child = emlyn;
For any X and Y, no
X is the mother of Y IF The query means ‘Find me a value of Child such that Liz
X is the parent of Y AND X is female
is the mother of Child.’
For any X and Y, First solution is Brendon.
X is the grandparent of Y IF there is some Z such that User enters ‘;’ to ask for a further solution, Emlyn.
X is the parent of Z AND Z is the parent of Y There are no more solutions.

1
Exercise Lists in Prolog
Write Prolog queries to solve the following • To represent a list of elements in a particular
problems, and state the solutions the Prolog order in Prolog, use a list structure, e.g.
interpreter would find: [a,b,c,d,e].
• We can represent a list using notation [H|T] to
1. Who are the males in the family? mean a head item H and a list T, the tail,
containing the rest of the elements in the list.
2. Who is Liz’s mother? • E.g. if we unify the list [a,b,c,d,e] with [H|T]
the result is H = a, T = [b,c,d,e] if we unify the
3. Who are the grandchildren of Irene?
list [a] with [H|T] the result is H = a, T = [].

A Route-Finding Problem Prolog for Route-Finding


We have a simple network: link(a,b).
link(b,d).
link(b,c).
link(c,e).
link(e,b).
route_list(X,Y,[X,Y]):- link(X,Y).
route_list(X,Y,[X|Rest]):-
The problem is to find a route between any link(X,Z), route_list(Z,Y,Rest).
two nodes and record it in a list.

The Meaning of Rules Exercise


The route from X to Y is [X,Y] if there is a Write a Prolog query to find all the routes
direct link from X to Y. starting at b, and state the solutions the Prolog
interpreter would find.
The route from X to Y starts with X, and the
rest of the route is the route from Z to Y,
where Z is a node you can reach by a single
direct link from X.

You might also like