0% found this document useful (0 votes)
32 views

Defining Relations by Facts, by Rules and Recursive Rules

This document describes a Prolog practical on defining relationships using facts, rules, and recursive rules. It provides an example family tree program that defines relationships like parent, offspring, mother, and grandparent using facts. It also shows how to define these relationships using rules. Recursive rules are introduced to define predecessors in a family tree. The document concludes by explaining how to ask questions by querying the program to find relationships like sisters and mothers.

Uploaded by

beenish
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

Defining Relations by Facts, by Rules and Recursive Rules

This document describes a Prolog practical on defining relationships using facts, rules, and recursive rules. It provides an example family tree program that defines relationships like parent, offspring, mother, and grandparent using facts. It also shows how to define these relationships using rules. Recursive rules are introduced to define predecessors in a family tree. The document concludes by explaining how to ask questions by querying the program to find relationships like sisters and mothers.

Uploaded by

beenish
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

DEPARTMENT OF COMPUTER SCIENCE

QUAID-E-AWAM UNIVERSITY OF ENGG., SCIENCE & TECHNOLOGY, NAWABSHAH


ARTIFICIAL INTELLIGENCE ( 7th TERM, FOURTH YEAR)

Practical

Object:

Defining relations by facts, by Rules and Recursive rules.

Theory:
This Prolog practical offers you the opportunity to get familiar yourself with facts and
rules for defining relationships, as an example we write a program for a whole
familytree. In this practical you will learn following:
 Define relationship by facts.
 Define relationship by Rules
 Recursive rules.
 Ask questions about family tree.

An Example of a Procedure:

Defining relationship by facts:


% Pam is a parent ofBob

parent( pam, bob).


parent( tom, bob).
parent( tom, liz).
parent( bob, ann).
parent( bob, pat).
parent( pat, jim).
female( pam). % Pam is female
male( tom). % Tom is male
male( bob).
female(liz).
female(ann).
female( pat).
male(jim).

Defining relationship by Rules Meaning


Rules have:
Defining offspring rule. A condition part (body)
the right-hand side of the rule
offspring( Y, X) :- parent( X, Y).
DEPARTMENT OF COMPUTER SCIENCE
QUAID-E-AWAM UNIVERSITY OF ENGG., SCIENCE & TECHNOLOGY, NAWABSHAH
ARTIFICIAL INTELLIGENCE ( 7th TERM, FOURTH YEAR)

A conclusion part (head)


Defining Mother relationship rule. the left-hand side of the rule

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


For all X and Y,
Y is an offspring of X if X is a parent of Y.
Mother rule.
For all X and Y,
X is the mother of Y if
X is a parent of Y and
X is a female.

Define the grandparent relation: Grandparent Relationshio


grandparent( X, Z) :- For all X and Z,
parent( X, Y), parent( Y, Z). X is grandparent of Z if
X is Parent of Y and Y is parent of Z.

Recursive Rules. For all X and Z,


Defining the predecessor Rule.
X is a predecessor of Z if
there is a Y such that
predecessor( X, Z):- parent( X, Z). (1) X is a parent of Y and
predecessor( X, Z):- parent( X, Y), predecessor( Y, Z). (2) Y is a predecessor of Z.
DEPARTMENT OF COMPUTER SCIENCE
QUAID-E-AWAM UNIVERSITY OF ENGG., SCIENCE & TECHNOLOGY, NAWABSHAH
ARTIFICIAL INTELLIGENCE ( 7th TERM, FOURTH YEAR)

The family program.


% Defining Rules:
% DefiningFacts: offspring( Y, X):- parent( X, Y).
parent( pam, bob).
parent( tom, bob). mother( X, Y) :- parent( X, Y),female( X).
parent( tom, liz).
parent( bob, ann). grandparent( X, Z):-parent( X, Y),parent( Y, Z).
parent( bob, pat).
parent( pat, jim). sister( X, Y):-parent( Z, X),parent( Z, Y),female( X).
female( pam).
female(liz). predecessor( X, Z):- parent( X, Z).
female(ann).
female( pat). predecessor( X, Z):- parent( X, Y),predecessor( Y, Z).
male( tom).
male( bob).
male(jim).

Asking Questions
To answer a question, Prolog tries to satisfy all the goals.
To satisfy a goal means to demonstrate that the goal istrue, assuming that the relations in
the program is true.
Example.
sister(X,Y). % who is sister of whom.
mother(X,Y). % X is mother of Y.

Conclusion:
DEPARTMENT OF COMPUTER SCIENCE
QUAID-E-AWAM UNIVERSITY OF ENGG., SCIENCE & TECHNOLOGY, NAWABSHAH
ARTIFICIAL INTELLIGENCE ( 7th TERM, FOURTH YEAR)

________________

Review Questions/ Exercise:

1. find all the sisters relations in the above programs?

________________

2. Write a rule for uncle relationship.

________________

3.Make a family tree of own and find relationships for brother, sister, mother,
father, uncle, grandparent.

________________

You might also like