%
% + - - - - - - - - - - - - - - - - - - - - - - - +
% | |
% | This is prolog.txt, a text file that contains |
% | the answers I, Savas Ali Tokmen, have |
% | prepared for the Prolog Assignment (CM20019) |
% | |
% + - - - - - - - - - - - - - - - - - - - - - - - +
%
%
% A copy of it normally should be found at the following address:
%
% http://people.bath.ac.uk/st221/CM20019/prolog.txt
%
% (Please make sure you directly download the text file,
% since the main folder is actually protected so you need
% a password to get a list of the files on that folder)
%
% It is organized in 4 parts: data, functions, tests and thinking
% - data uses 4 basic relations:
% - child(X,Y): X is a child of Y
% - male(X): X is male
% - female(X): X is female
% - married(X,Y): X and Y are married: at each time we
% define the marriage twice, therefore
% enter married(X,Y) and married(Y,X)
% - functions
% - testing includes tests for all these functions
% - the thinking part tells why all queries should terminate
% Also note that names presented in this relation table are
% completely random and fictious, and therefore do not have
% any link with any sort of physical reality.
% Family tree...
% First generation
male(jim).
female(elisabeth).
male(ricky).
married(elisabeth,ricky).
married(ricky,elisabeth).
% Second generation
female(melissa).
male(john).
male(elvis).
female(mary).
male(chris).
female(anne).
female(amanda).
male(bill).
child(john,jim).
child(john,elisabeth).
child(mary,jim).
child(mary,elisabeth).
child(chris,jim).
child(chris,elisabeth).
child(amanda,elisabeth).
child(amanda,ricky).
married(melissa,john).
married(john,melissa).
married(elvis,mary).
married(mary,elvis).
married(amanda,bill).
married(bill,amanda).
% Third generation
female(linda).
male(harold).
female(maryanne).
male(ronney).
female(jane).
male(edmond).
female(joanna).
female(jenna).
male(george).
child(harold,melissa).
child(harold,john).
child(maryanne,melissa).
child(maryanne,john).
child(ronney,elvis).
child(ronney,mary).
child(edmond,chris).
child(edmond,anne).
child(joanna,chris).
child(joanna,anne).
child(george,amanda).
child(george,bill).
married(linda,harold).
married(harold,linda).
married(ronney,jane).
married(jane,ronney).
married(jenna,george).
married(george,jenna).
% Fourth generation
male(edmund).
female(melinda).
male(david).
male(jimmy).
male(ken).
female(christina).
male(ben).
child(edmund,linda).
child(edmund,harold).
child(david,ronney).
child(david,jane).
child(jimmy,ronney).
child(jimmy,jane).
child(ken,ronney).
child(ken,jane).
child(christina,jenna).
child(christina,george).
married(edmund,melissa).
married(melissa,edmund).
married(ken,christina).
married(christina,ken).
% Fifth generation
male(henry).
female(lisa).
male(lee).
female(britney).
female(anna).
child(henry,edmund).
child(henry,melinda).
child(lisa,melinda).
child(lisa,jimmy).
child(lee,ken).
child(lee,christina).
child(britney,ken).
child(britney,christina).
child(anna,ben).
child(anna,christina).
% Functions...
% son(X,Y): X is a son of Y, therefore is a child of Y
% and is male
son(X,Y):-child(X,Y),male(X).
% daughter(X,Y): X is a daughter of Y, therefore is a
% child of Y and is female
daughter(X,Y):-child(X,Y),female(X).
% parent(X,Y): X is a parent of Y, therefore Y is a
% child of X
parent(X,Y):-child(Y,X).
% father(X,Y): X is the father of Y, therefore is a
% parent of Y and is male
father(X,Y):-parent(X,Y),male(X).
% mother(X,Y): X is the mother of Y, therefore is a
% parent of Y and is female
mother(X,Y):-parent(X,Y),female(X).
% husband(X,Y): X is the husband of Y, therefore X
% and Y are married and X is male
husband(X,Y):-married(X,Y),male(X).
% wife(X,Y): X is the wife of Y, therefore X and Y
% are married and X is female
wife(X,Y):-married(X,Y),female(X).
% bro_sis(X,Y): X and Y share parents, therefore the
% same mother and / or father... We need to use a series
% of "not" operators to remove duplicate responses for
% "real" brothers and sisters
% Note that bro_sis(X,Y) <=> bro_sis(Y,X)
bro_sis(X,Y):- mother(M1,X),mother(M2,Y),
father(F1,X),father(F2,Y),
( (M1=M2),not(F1=F2);
(F1=F2),not(M1=M2);
(M1=M2),(F1=F2)),X\=Y.
% brother(X,Y): X is a brother of Y, therefore is a
% bro_sis of Y and is male
brother(X,Y):-bro_sis(X,Y),male(X).
% sister(X,Y): X is a sister of Y, therefore is a
% bro_sis of Y and is female
sister(X,Y):-bro_sis(X,Y),female(X).
% bro_in_law(X,Y): X is a brother-in-law of Y, so is or:
% - a brother of Y's spouse
% - the husband of one of Y's sisters
% - the husband of a sister of Y's spouse
bro_in_law(X,Y):-married(Y,Z),brother(X,Z).
bro_in_law(X,Y):-sister(Z,Y),husband(X,Z).
bro_in_law(X,Y):-married(Y,Z),sister(S,Z),husband(X,S).
% sis_in_law(X,Y): X is a sister-in-law of Y, so is or:
% - a sister of X's spouse
% - the wife of one of X's brothers
% - the wife of a brother of X's spouse
sis_in_law(X,Y):-married(Y,Z),sister(X,Z).
sis_in_law(X,Y):-brother(Z,Y),wife(X,Z).
sis_in_law(X,Y):-married(Y,Z),brother(S,Z),wife(X,S).
% grandparent(X,Y): X is a grand parent of Y, therefore
% X is a parent of one of Y's parents
grandparent(X,Y):-parent(X,Z),parent(Z,Y).
% grandfather(X,Y): X is a grand father of Y, therefore
% is a grand parent of Y and is male
grandfather(X,Y):-grandparent(X,Y),male(X).
% grandmother(X,Y): X is a grand mother of Y, therefore
% is a grand parent of Y and is female
grandmother(X,Y):-grandparent(X,Y),female(X).
% aunt(X,Y): X is an aunt of Y, so is or:
% - the sister of one of Y's parents
% - the wife of one of Y's uncles
% Note that for the second part, we
% do not directly use "uncle" since
% a similar definition is also defined
% in the uncle/2 relation, so would make
% Prolog crash...
aunt(X,Y):-parent(Z,Y),sister(X,Z).
aunt(X,Y):-parent(Z,Y),brother(S,Z),wife(X,S).
% uncle(X,Y): X is an uncle of Y, so is or:
% - the brother of one of Y's parents
% - the husband of one of Y's aunts
% (similar note as for "aunt")
uncle(X,Y):-parent(Z,Y),brother(X,Z).
uncle(X,Y):-parent(Z,Y),sister(S,Z),husband(X,S).
% cousin(X,Y): X is a cousin of Y, therefore at least
% one of X and Y's parents are brothers or sisters
% (which also means that Y is a cousin of X)
% Some more constraints have been put in for
% the function to work well for married brother
% and sisters (there's none in this tree)
cousin(X,Y):- parent(Z,X),bro_sis(S,Z),child(Y,S),
X\=Y,not(bro_sis(X,Y)).
% deepcousin(X,Y): X is a deep cousin of Y, so X and Y are or:
% - cousins
% - children of cousin or deepcousin parents
% (which also means that Y is a deep cousin of X)
% Some more constraints have been put in for
% the function to work better for married deep
% cousins (there are some in this tree)
deepcousin(X,Y):-(cousin(X,Y);parent(M,X),parent(N,Y),deepcousin(M,N)),
X\=Y,not(bro_sis(X,Y)).
% cousinlist(X): X is a list of cousins
% The way it works is quite easy:
% 1- it takes a cousin of the first element
% 2- adds it to the list if it is not inside already
cousinlist([X,Y]):-cousin(X,Y).
cousinlist([X,Y|Z]):-cousin(X,Y),cousinlist([Y|Z]),not(member(X,[Y|Z])).
% Testing
% son(X,Y): X is a son of Y
% ?- son(X,mary).
% X = ronney ;
% No
% ?- son(X,jim).
% X = john ;
% X = chris ;
% No
% ?- son(X,elisabeth).
% X = john ;
% X = chris ;
% No
% ?- son(john,X).
% X = jim ;
% X = elisabeth ;
% No
% daughter(X,Y): X is a daughter of Y
% ?- daughter(X,jane).
% No
% ?- daughter(X,jenna).
% X = christina ;
% No
% ?- daughter(X,elisabeth).
% X = mary ;
% X = amanda ;
% No
% ?- daughter(X,jim).
% X = mary ;
% No
% ?- daughter(amanda,X).
% X = elisabeth ;
% X = ricky ;
% No
% parent(X,Y): X is a parent of Y
% ?- parent(X,jim).
% No
% ?- parent(jim,X).
% X = john ;
% X = mary ;
% X = chris ;
% No
% ?- parent(X,ronney).
% X = elvis ;
% X = mary ;
% No
% ?- parent(ronney,X).
% X = david ;
% X = jimmy ;
% X = ken ;
% No
% father(X,Y): X is the father of Y
% ?- father(X,jim).
% No
% ?- father(jim,X).
% X = john ;
% X = mary ;
% X = chris ;
% No
% ?- father(X,ronney).
% X = elvis ;
% No
% ?- father(ronney,X).
% X = david ;
% X = jimmy ;
% X = ken ;
% No
% mother(X,Y): X is the mother of Y
% ?- mother(X,elisabeth).
% No
% ?- mother(elisabeth,X).
% X = john ;
% X = mary ;
% X = chris ;
% X = amanda ;
% No
% ?- mother(X,ronney).
% X = mary ;
% No
% ?- mother(jane,X).
% X = david ;
% X = jimmy ;
% X = ken ;
% No
% husband(X,Y): X is the husband of Y
% ?- husband(X,elisabeth).
% X = ricky ;
% No
% ?- husband(ricky,X).
% X = elisabeth ;
% No
% ?- husband(X,anne).
% No
% wife(X,Y): X is the wife of Y
% ?- wife(X,ricky).
% X = elisabeth ;
% No
% ?- wife(elisabeth,X).
% X = ricky;
% No
% ?- wife(anne,X).
% No
% bro_sis(X,Y): X and Y share parents
% ?- bro_sis(X,john).
% X = mary ;
% X = chris ;
% X = amanda ;
% No
% ?- bro_sis(john,X).
% X = mary ;
% X = chris ;
% X = amanda ;
% No
% ?- bro_sis(X,henry).
% X = lisa ;
% No
% ?- bro_sis(X,ronney).
% No
% brother(X,Y): X is a brother of Y
% ?- brother(X,john).
% X = chris ;
% No
% ?- brother(john,X).
% X = mary ;
% X = chris ;
% X = amanda ;
% No
% sister(X,Y): X is a sister of Y
% ?- sister(X,john).
% X = mary ;
% X = amanda ;
% No
% ?- sister(mary,X).
% X = john ;
% X = chris ;
% X = amanda ;
% No
% bro_in_law(X,Y): X is a brother-in-law of Y
% % Brothers of elvis's spouse (john, chris)
% % The husband of one of elvis's sisters (bill)
% ?- bro_in_law(X,elvis).
% X = john ;
% X = chris ;
% X = bill ;
% No
% % The husbands of one of john's sisters
% ?- bro_in_law(john,X).
% X = elvis ;
% X = bill ;
% No
% % The husband of a sister of melissa's spouse (elvis)
% ?- bro_in_law(X,melissa).
% X = chris ;
% X = elvis ;
% X = bill ;
% No
% ?- bro_in_law(elvis,X).
% X = john ;
% X = chris ;
% X = amanda ;
% X = melissa ;
% X = bill ;
% No
% ?- bro_in_law(X,chris).
% X = elvis ;
% X = bill ;
% No
% sis_in_law(X,Y): X is a sister-in-law of Y
% % Sisters of melissa's spouse
% ?- sis_in_law(X,melissa).
% X = mary ;
% X = amanda ;
% No
% % The wife of one of mary's brothers
% ?- sis_in_law(X,mary).
% X = melissa ;
% No
% % The wife of a brother of elvis's spouse (amanda)
% ?- sis_in_law(X,elvis).
% X = amanda ;
% X = melissa ;
% No
% ?- sis_in_law(melissa,X).
% X = mary ;
% X = chris ;
% X = amanda ;
% X = elvis ;
% X = bill ;
% No
% grandparent(X,Y): X is a grandparent of Y
% ?- grandparent(X,ronney).
% X = jim ;
% X = elisabeth ;
% No
% ?- grandparent(X,britney).
% X = ronney ;
% X = jane ;
% X = jenna ;
% X = george ;
% No
% ?- grandparent(ronney,X).
% X = lisa ;
% X = lee ;
% X = britney ;
% No
% ?- grandparent(jane,X).
% X = lisa ;
% X = lee ;
% X = britney ;
% No
% grandfather(X,Y): X is a grandfather of Y
% ?- grandfather(X,ronney).
% X = jim ;
% No
% ?- grandfather(X,britney).
% X = ronney ;
% X = jenna ;
% No
% ?- grandfather(ronney,X).
% X = lisa ;
% X = lee ;
% X = britney ;
% No
% ?- grandfather(jane,X).
% No
% grandmother(X,Y): X is a grandmother of Y
% ?- grandmother(X,ronney).
% X = elisabeth ;
% No
% ?- grandmother(X,britney).
% X = jane ;
% X = jenna ;
% No
% ?- grandmother(ronney,X).
% No
% ?- grandmother(jane,X).
% X = lisa ;
% X = lee ;
% X = britney ;
% No
% aunt(X,Y): X is an aunt of Y
% % The sisters of one of harold's parents
% ?- aunt(X,harold).
% X = mary ;
% X = amanda ;
% No
% % The wives of ronney's uncles
% ?- aunt(X,ronney).
% X = amanda ;
% X = melissa ;
% No
% ?- aunt(mary,X).
% X = harold ;
% X = maryanne ;
% X = edmond ;
% X = joanna ;
% X = george ;
% No
% ?- aunt(melissa,X).
% X = ronney ;
% X = edmond ;
% X = joanna ;
% X = george ;
% No
% uncle(X,Y): X is an uncle of Y
% % The brothers of one of harold's parents (chris, bill)
% % The husband of one of ronney's aunts (elvis)
% ?- uncle(X,harold).
% X = chris ;
% X = elvis ;
% X = bill ;
% No
% % The brothers of one of ronney's parents
% ?- uncle(X,ronney).
% X = john ;
% X = chris ;
% X = bill ;
% No
% ?- uncle(john,X).
% X = ronney ;
% X = edmond ;
% X = joanna ;
% X = george ;
% No
% ?- aunt(melissa,X).
% X = ronney ;
% X = edmond ;
% X = joanna ;
% X = george ;
% No
% cousin(X,Y): X is a cousin of Y
% ?- cousin(X,ronney).
% X = harold ;
% X = maryanne ;
% X = edmond ;
% X = joanna ;
% X = george ;
% No
% ?- cousin(X,lee).
% X = lisa ;
% No
% ?- cousin(lee,X).
% X = lisa ;
% No
% ?- cousin(lisa,X).
% X = lee ;
% X = britney ;
% No
% ?- cousin(X,edmund).
% No
% ?- cousin(X,henry).
% No
% deepcousin(X,Y): X is a deepcousin of Y
% % Just normal cousins...
% ?- deepcousin(X,ronney).
% X = harold ;
% X = maryanne ;
% X = edmond ;
% X = joanna ;
% X = george ;
% No
% % Really deep cousins... (1)
% ?- deepcousin(X,edmund).
% X = david ;
% X = jimmy ;
% X = ken ;
% X = christina ;
% No
% % Really deep cousins... (2)
% ?- deepcousin(X,jimmy).
% X = edmund ;
% X = christina ;
% No
% % Really deep cousins, with some listing problems:
% % ken and christina, two deep cousins, got together
% % and therefore make things get listed twice...
% ?- deepcousin(X,henry).
% X = lee ;
% X = lee ;
% X = britney ;
% X = britney ;
% X = anna ;
% No
% ?- deepcousin(X,ronney).
% X = harold ;
% X = maryanne ;
% X = edmond ;
% X = joanna ;
% X = george ;
% No
% cousinlist(X): X is a list of cousins
% % lisa has 2 cousins, but they're bro_sis...
% % therefore lisa needs to be in the middle of
% % the list to make the list a cousin list
% ?- cousinlist([lisa,X]).
% X = lee ;
% X = britney ;
% No
% ?- cousinlist([lisa,X,Y]).
% No
% ?- cousinlist([X,lisa,Y]).
% Y = britney
% X = lee ;
% Y = lee
% X = britney ;
% No
% ?- cousinlist([george,X]).
% X = harold ;
% X = maryanne ;
% X = ronney ;
% X = edmond ;
% X = joanna ;
% No
% ?- cousinlist([george,X,Y,Z,W]).
%
% W = edmond
% Z = maryanne
% Y = ronney
% X = harold ;
%
% W = joanna
% Z = maryanne
% Y = ronney
% X = harold ;
%
% W = maryanne
% Z = edmond
% Y = ronney
% X = harold
%
% % [ etc ]
%
% W = harold
% Z = edmond
% Y = ronney
% X = joanna ;
%
% W = maryanne
% Z = edmond
% Y = ronney
% X = joanna ;
%
% No
% Thinking: why this actually works...
% We start with 4 basic relations:
% - male(X)
% - female(X)
% - child(X,Y)
% - married(X,Y)
% which are all defined with constants.
% Our family tree does not loop from bottom to up, therefore growth
% of the family tree is always uni-directional (which in real world
% is not always true, since the age of all members of the family is
% not equal and therefore some members may have the same age as
% other members that are multiple generations beyond or behind)
% "son" and "daughter" are simply some combinations between "child"
% and the sex, and should always terminate.
% "parent" is simply the inverse of "child", which also terminates.
% "father" and "mother" are simply some combinations between "parent"
% and the sex, and should always terminate.
% "husband" and "wife" are simply some combinations between "married"
% and the sex, and should always terminate.
% "bro_sis" uses some "parent" relations put together with "and", "or"
% and "not" operators. It therefore should also terminate.
% "brother" and "sister" are simply some combinations between "bro_sis"
% and the sex, and should always terminate.
% "bro_in_law" and "sis_in_law" use combinations of "brother", "sister"
% and "married"; which all are independent. It therefore terminates.
% "grandparent" uses the "parent" relation twice, so also terminates.
% "grandfather" and "grandmother" are simply some combinations between
% "grandparent" and the sex, and should always terminate.
% "aunt" and "uncle" are simply some combinations between "parent",
% "bro_sis", "married" and the sex, and should always terminate.
% "cousin" is based on "parent" and "bro_sis", therefore terminates.
% "deepcousin" uses "cousin" and "parent" combined; and when browsing
% *always* goes up in the family tree. It therefore terminates.
% "cousinlist" just uses "cousin" with a given (=not infinite) length
% of list... It therefore always terminates.
%
%
%
% + - - - - - - - - - - - - - - - - - - - - - - - +
% | |
% | To contact me, please see |
% | http://people.bath.ac.uk/st221/ |
% | |
% | This point marks the end of this document |
% | |
% + - - - - - - - - - - - - - - - - - - - - - - - +
%