0% found this document useful (0 votes)
26 views5 pages

Prolog Family Tree Assignment 24BAI10706

Uploaded by

chirayujain600
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views5 pages

Prolog Family Tree Assignment 24BAI10706

Uploaded by

chirayujain600
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Name: Jain Chirayu

Reg No: 24BAI0706

Que: Create a family tree in prolog with the rule for following rela ons father,
mother, uncle, aunt, grandfather, grandmother, sibling, ancestor. The facts
should be relevant to your family informa on. Rules should be general.

James Sarah

| |

--------------------------

| |

Robert Catherine

/ \ |

Alice Tom Emily

Prolog Code

% Facts: Define the family tree structure

% Gender

male(james).

male(robert).

male(tom).

female(sarah).

female(catherine).

female(alice).

female(emily).

% Parent rela onships

parent (james, robert).

parent (sarah, robert).

parent (james, catherine).

parent (sarah, catherine).


parent (robert, alice).

parent (robert, tom).

parent (catherine, emily).

% Rules

% Father: A father is a male parent.

father (X, Y) :- parent(X, Y), male(X).

% Mother: A mother is a female parent.

mother (X, Y) :- parent(X, Y), female(X).

% Sibling: Two people are siblings if they share a parent and are not the same person.

sibling (X, Y) :- parent(Z, X), parent(Z, Y), X \= Y.

% Uncle: An uncle is a male sibling of a parent.

uncle (X, Y) :- sibling(X, Z), parent(Z, Y), male(X).

% Aunt: An aunt is a female sibling of a parent.

aunt (X, Y) :- sibling(X, Z), parent(Z, Y), female(X).

% Grandfather: A grandfather is a male parent of a parent.

grandfather (X, Y) :- parent(X, Z), parent(Z, Y), male(X).

% Grandmother: A grandmother is a female parent of a parent.

grandmother (X, Y) :- parent(X, Z), parent(Z, Y), female(X).

% Ancestor: An ancestor is a parent or an ancestor of a parent (recursive defini on).

ancestor (X, Y) :- parent(X, Y).

ancestor (X, Y) :- parent(X, Z), ancestor(Z, Y).

Family Tree Facts


Here’s how the family tree looks when translated into facts:

1. James and Sarah are the parents of:

o Robert

o Catherine

2. Robert is the father of:

o Alice

o Tom

3. Catherine is the mother of:

o Emily

Rules Explained

1. Father Rule:

Prolog code

father(X, Y) :- parent(X, Y), male(X).

o X is the father of Y if X is a parent of Y and X is male.

2. Mother Rule:

Prolog code

mother(X, Y) :- parent(X, Y), female(X).

o X is the mother of Y if X is a parent of Y and X is female.

3. Sibling Rule:

Prolog code

sibling(X, Y) :- parent(Z, X), parent(Z, Y), X \= Y.

o X and Y are siblings if they share a parent (Z) and are not the same person.

4. Uncle Rule:

Prolog code

uncle(X, Y) :- sibling(X, Z), parent(Z, Y), male(X).

o X is an uncle of Y if X is a male sibling of Y's parent.

5. Aunt Rule:

Prolog code

aunt(X, Y) :- sibling(X, Z), parent(Z, Y), female(X).

o X is an aunt of Y if X is a female sibling of Y's parent.


6. Grandfather Rule:

Prolog code

grandfather(X, Y) :- parent(X, Z), parent(Z, Y), male(X).

o X is a grandfather of Y if X is a parent of Y's parent and is male.

7. Grandmother Rule:

Prolog code

grandmother(X, Y) :- parent(X, Z), parent(Z, Y), female(X).

o X is a grandmother of Y if X is a parent of Y's parent and is female.

8. Ancestor Rule:

Prolog code

ancestor(X, Y) :- parent(X, Y).

ancestor(X, Y) :- parent(X, Z), ancestor(Z, Y).

o X is an ancestor of Y if X is a parent of Y or a parent of an ancestor of Y (recursive


rule).

Queries and Example Answers

1. Who is the father of Robert?

Prolog code

?- father(X, robert).

Output: X = james.

2. Who are the siblings of Robert?

Prolog code

?- sibling(X, robert).

Output: X = catherine.

3. Who is the grandfather of Alice?

Prolog code

?- grandfather(X, alice).

Output: X = james.

4. Is Sarah an ancestor of Emily?

Prolog code

?- ancestor(sarah, emily).
Output: true.

You might also like