Description
Lab 5 Family Problem (Prolog)
17341015 Hongzheng Chen
Contents
1 About Cousin and Removed 2
2 Problem Description 2
3 Tasks 3
4 Codes 4
5 Results 6
1 About Cousin and Removed
What Is a First Cousin, Twice Removed?
If someone walked up to you and said, ”Howdy, I’m your third cousin, twice removed,” would you have any idea what they meant? Most people have a good understanding of basic relationship words such as ”mother,” ”father,” ”aunt,” ”uncle,” ”brother,” and ”sister.” But what about the relationship terms that we don’t use in everyday speech? Terms like ”second cousin” and ”first cousin, once removed”? We don’t tend to speak about our relationships in such exact terms (”cousin” seems good enough when you are introducing one person to another), so most of us aren’t familiar with what these words mean.
Relationship Terms
Sometimes, especially when working on your family history, it’s handy to know how to describe your family relationships more exactly. The definitions below should help you out.
Cousin (a.k.a ”first cousin”)
Your first cousins are the people in your family who have two of the same grandparents as you. In other words, they are the children of your aunts and uncles.
Second Cousin
Your second cousins are the people in your family who have the same great-grandparents as you., but not the same grandparents.
Third, Fourth, and Fifth Cousins
Your third cousins have the same great great grandparents, fourth cousins have the same greatgreat-great-grandparents, and so on.
Removed
When the word ”removed” is used to describe a relationship, it indicates that the two people are from different generations. You and your first cousins are in the same generation (two generations younger than your grandparents), so the word ”removed” is not used to describe your relationship.
The words ”once removed” mean that there is a difference of one generation. For example, your mother’s first cousin is your first cousin, once removed. This is because your mother’s first cousin is one generation younger than your grandparents and you are two generations younger than your grandparents. This one-generation difference equals ”once removed.”
Twice removed means that there is a two-generation difference. You are two generations younger than a first cousin of your grandmother, so you and your grandmother’s first cousin are first cousins, twice removed.
2 Problem Description
Please fulfill the following tasks by using Prolog:
1. Write sentences describing the predicates Grandchild, Greatgrandparent, Ancestor, Brother, Sister, Daughter, Son, FirstCousin, BrotherInLaw, SisterInLaw, Aunt, and Uncle. Hint: you can define these predicates by choosing child, sibling, male, female, father, mother, and so on.
2. Find out the proper definition of mth cousin n times removed, in other words, define the predicate mthCousinNremoved(X,Y,M,N). Hint: You’d better define the predicate distance(X,Y,N) by recursion ( please refer to hanoi.pl) to show there are N generations between X and Y in advance.
3. Write down the basic facts depicted in the family tree in Figure 2.
4. ASK it who are Elizabeth’s grandchildren, Diana’s brothers-in-law, Zara’s greatgrandparents, and Eugenie’s ancestors.
Figure 1: A typical family tree. The symbol ./ connects spouses and arrows point to children.
Figure 2: A typical family tree (Chinese version).
3 Tasks
1. Please complete the Prolog codes. There are several tutorials in the folder and I will explain the usage of Prolog in class.
2. Write the related codes and take a screenshot of the running results in the file named E05 YourNumber.pdf, and send it to ai 201901@foxmail.com.
4 Codes
The Prolog code is very intuitive and reads as what it declares, but there are several things to mention.
• firstCousin(X,Y) not only needs to guarantee the grandparent of X and Y are the same, but also needs to make sure they do not have the same parent.
• The hardest part of this experiment is the design of the cousin logic. I firstly design an axillary function mthAncestor(X,Y,M) for giving out the m-th ancestor Y of X. Notice the foundation part that M is equal to zero, and it should return an identity function.
• Then I implement the mthCousin(X,Y,M). Many people only make X and Y have the same (m+1)-th ancestor Z (the provided distance function), which is not correct. Based on the definition of the m-th cousin, we should make sure X and Y get the same ancestor following different paths. Thus, the child of Z on X’s path and the child of Z on Y’s path should not be the same. So we have the constraints in Line 122.
• Finally is the mthCousinNremoved(X,Y,M,N) rule which is similar to the implementation of the rule mthCousin. The same ancestor Z should have different children for X and Y to get up to there, as constrained in Line 126.
/* facts */
oneself(george,george).
oneself(mum,mum).
oneself(spencer,spencer). oneself(kydd,kydd). oneself(elizabeth,elizabeth).
oneself(philip,philip).
oneself(margaret,margaret).
oneself(diana,diana).
oneself(charles,charles).
oneself(anne,anne). oneself(mark,mark).
oneself(andrew,andrew). oneself(sarah,sarah). oneself(edward,edward). oneself(sophie,sophie). oneself(william,william).
oneself(harry,harry). oneself(peter,peter). oneself(zara,zara). oneself(beatrice,beatrice). oneself(eugenie,eugenie). oneself(louise,louise). oneself(james,james).
%% sex
% #1 gen
male(george). female(mum).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30% #2 gen
31female(elizabeth).
32male(philip).
33female(margaret).
34male(spencer). 35female(kydd).
36% #3 gen
37female(diana).
38male(charles).
39female(anne). 40male(mark).
41male(andrew).
42female(sarah).
43male(edward).
44female(sophie).
45% #4 gen
46male(william).
47male(harry).
48male(peter).
49female(zara).
50female(beatrice).
51female(eugenie).
52female(louise).
53male(james).
54
55%% parent 56parent(diana,spencer). 57parent(diana,kydd).
58parent(william,diana).
59parent(william,charles).
60parent(harry,diana).
61parent(harry,charles).
62parent(charles,elizabeth).
63parent(charles,philip).
64parent(anne,elizabeth).
65parent(anne,philip).
66parent(peter,anne).
67parent(peter,mark).
68parent(zara,anne).
69parent(zara,mark).
70parent(andrew,elizabeth).
71parent(andrew,philip).
72parent(beatrice,andrew).
73parent(beatrice,sarah).
74parent(eugenie,andrew).
75parent(eugenie,sarah).
76parent(louise,edward).
77parent(louise,sophie).
78parent(james,edward).
79parent(james,sophie).
80parent(edward,elizabeth). 81parent(edward,philip).
82parent(elizabeth,george).
83parent(elizabeth,mum).
84parent(margaret,george). 85parent(margaret,mum).
/* rules */ child(X,Y) :- parent(Y,X). father(X,Y) :- parent(X,Y), male(Y). mother(X,Y) :- parent(X,Y), female(Y). husband(X,Y) :- female(X), male(Y), father(Z,Y), mother(Z,X). wife(X,Y) :- male(X), female(Y), father(Z,X), mother(Z,Y). spouse(X,Y) :- husband(X,Y) ; wife(X,Y).
son(X,Y) :- child(X,Y), male(Y). daughter(X,Y) :- child(X,Y), female(Y).
sibling(X,Y) :- parent(X,Z), parent(Y,Z), X == Y. brother(X,Y) :- sibling(X,Y), male(Y). sister(X,Y) :- sibling(X,Y), female(Y). grandfather(X,Y) :- parent(X,Z), father(Z,Y). grandmother(X,Y) :- parent(X,Z), mother(Z,Y). grandchild(X,Y) :- child(X,Z), child(Z,Y). grandparent(X,Y) :- parent(X,Z), parent(Z,Y). greatGrandparent(X,Y) :- grandparent(X,Z), parent(Z,Y). ancestor(X,Y) :- parent(X,Y). % Base ancestor(X,Y) :- parent(X,Z), ancestor(Z,Y). % Recursion aunt(X,Y) :- grandparent(X,Z), parent(Y,Z), +(mother(X,Y)), female(Y). uncle(X,Y) :- grandparent(X,Z), parent(Y,Z), +(father(X,Y)), male(Y).
%% the husband of your sister or brother
%% or the brother of your husband or wife
%% or the man who is married to the sister %% or brother of your wife or husband brotherInLaw(X,Y) :- (spouse(X,Z), brother(Z,Y)) ; (sister(X,Z), husband(Z,Y)). sisterInLaw(X,Y) :- (spouse(X,Z), sister(Z,Y)) ; (brother(X,Z), wife(Z,Y)).
% cousins firstCousin(X,Y) :- grandparent(X,Z), grandparent(Y,Z), X == Y, +(sibling(X,Y)). mthAncestor(X,Y,0) :- oneself(X,Y). mthAncestor(X,Y,1) :- parent(X,Y).
mthAncestor(X,Y,M) :- parent(X,Z), M1 is M-1, mthAncestor(Z,Y,M1). mthCousin(X,Y,1) :- firstCousin(X,Y).
mthCousin(X,Y,M) :-
M1 is M+1, mthAncestor(X,Z,M1), mthAncestor(Y,Z,M1), X == Y, mthAncestor(X,A,M), mthAncestor(Y,B,M), A = B, parent(A,Z), parent(B,Z). %% mthAncestor(X,A,M), mthAncestor(Y,B,M), A = B, +(spouse(A,B)).
mthCousinNremoved(X,Y,M,N) :-
M1 is M+1, mthAncestor(X,Z,M1), M2 is M-N+1, mthAncestor(Y,Z,M2), X == Y, mthAncestor(X,A,M), M3 is M-N, mthAncestor(Y,B,M3), parent(A,Z), parent(B,Z), A == B.
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
5 Results
The results are shown below, where I test all the cases in the questions.




Reviews
There are no reviews yet.