Prolog Tutorials
Family Relations
%=============================================================================
%----Write a program that stores information about your family, and will
%----answer queries about relationships. Information about individual people
%----must be stored as facts containing the persons name, sex and parents'
%----names, e.g. person(geoff,male,bob,margaret).
%----Here are the queries you should be able to answer ...
%?-sibling(Person,Sibling). Algorithm : Siblings have the same parents.
%?-brother(Person,Brother). Algorithm : A brother is a male sibling.
%?-sister(Person,Sister). Algorithm : A sister is a female sibling.
%?-father(Person,Father).
%?-mother(Person,Mother).
%?-parent(Person,Parent). Algorithm : Either a mother or father
%?-offspring(Person,Offspring). Algorithm : The inverse of parent.
%?-son(Person,Son). Algorithm : A son is a male offspring.
%?-daughter(Person,Daughter). Algorithm : A daughter is a female offspring.
%?-aunt(Person,Aunt). Algorithm : An aunt is a parent's sister.
%?-uncle(Person,Uncle). Algorithm : An uncle is a parent's brother.
%?-nephew(Person,Nephew). Algorithm : A nephew is a sibling's son.
%?-niece(Person,Niece). Algorithm : A niece is a sibling's daughter.
%?-descendant(Person,Descendant). Algorithm : A descendant is an offspring or
% a descendant's offspring.
%?-ancestor(Person,Ancestor). Algorithm : The inverse of descendant.
%?-relation(Person,Relation). Algorithm : Descendants of most distant
% ancestor.
%=============================================================================
Solution for "cousin"
X and Y are cousins if a parent of X is a sibling of a parent of Y.
cousin(X,Y):- parent(X,XP), parent(Y,YP), sibling(XP,YP).
To write a program for "mother-in-law", (1) define mother-in-law in
English, (2) translate it into First-order-logic (FOL) and (3) translate
into a prolog program (CNF). Assume that parents of any person are married
to each other.
Tutorial 2
- Run programs given in the class room for (1) member, (2) append, (3) delete,
(4) insert (5) append-last, (6) quick-sort, (7) merge-sort, (8) list-length,
(9) list-product, (10) dot-product, (11) permutation, (12) reverse (13)
can-go, (13) sublist with queries that contain variables like
? member(X, [a,b,c,d]) and ? member(X, [a,b,X,d]) and
? member([maison,X], [[voiture,car] [pelouse,lawn] [maison,house]|Z]).
- Use "trace" and see how solutions are obtained/generated. Compare the
programs, whenever I gave you more than one program for any problem.
- Try to understand the backtracking mechanism of Prolog.
- Before you leave the tutorial, add the following 2 clauses to the append
program and execute the queries ?- everything_comes_withacost,
?- everyone_is_fooled and ?- append([], [a|L], L).
everything_comes_withacost :- append([], [a|L], L).
everyone_is_fooled :- append([], [a|L], L).
- I would like to introduce the following game which can be finished in
5 minutes by novices and it may not be finished in 5 hours when played
by experts. Please play the game with anyone you have around.
+---------------------+--------------------+
|\ | /|
| \ | / |
| \ | / |
| +-----------------+----------------+ |
| |\ | /| |
| | \ | / | |
| | \ | / | |
| | +-------------+------------+ | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
+---+---+ +---+---+
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | +-------------+------------+ | |
| | / | \ | |
| | / | \ | |
| |/ | \| |
| +-----------------+----------------+ |
| / | \ |
| / | \ |
|/ | \|
+---------------------+--------------------+
Rules:
-
Two payers X and Y have 9 coins (one uses red ones and
the other uses black ones) each.
-
X and Y place their coins alternatively (in turns) at a +
(plus in the above diagram).
-
If one has a line of 3 coins (of his colour), he can take one of
his opponent's coins.
-
If one does not have any coins left with him, he can move one of
his coin to an adjacent + if that + is empty.
-
Game ends when one has only 2 coins of his colour in the game
(there is no possibility of a line of 3 coins of his colour).
The other person wins the game.
Unification
%=============================================================================
%----Write a set of clauses with head predicate symbol "unify", which take two
%----arguments. If the arguments are both atoms, both integers or both
%----variables, attempt to unify them using the = predicate, otherwise unify
%----should fail.
%
%----Create an example where the lack of "occurs check" in unification causes
%----a problem.
%=============================================================================
%----An occurs check problem. Call with a variable argument.
Tutorial 3
Peano arithmetic
%=============================================================================
%----Integers may be represented by functions in Prolog. Zero is represented
%----by 0, one by s(0), two by s(s(0)), etc. Write Prolog clauses to add,
%----subtract, multiply and divide numbers in this representation.
%----Example
% ?-add(s(0),s(s(0)),X).
% yes
% X = s(s(s(0)))
%=============================================================================
Arithmetic with Variables
Write Prolog clauses to add, subtract, multiply and divide using Prolog's
built-in functions.
Mega append
Write a Prolog program for flattening a list of lists.
First argument is a list containing elements as well as lists of elements
and lists of lists of elements etc. Second argument is a list containing
all the basic elements of the first argument.
Library Facilities
Write a Prolog program for giving library users only basic facilities
like referencing and enquiries if he/she has an overdue book.
Otherwise, a user is allowed referencing, enquiries, borrowing and
inter-library-loans.
Append with cut
Run the following append program and tell me for what of queries this
program is (in)appropriate.
append([],X,X) :- !.
append([H|T], Y, [H|Z]) :- append(T, Y, Z).
Input/Output
Run programs given in the class room on 15.4.99.
Search
Run programs given in the class room on 21.4.99.