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

AI Session 5 Slides

The document discusses using Prolog to represent family relationships and solve problems involving objects and their relations. It provides examples of coding family tree facts and rules to define relationships like parent, mother, sister, father, and brother in Prolog. It also shows executing sample queries against the family tree data.

Uploaded by

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

AI Session 5 Slides

The document discusses using Prolog to represent family relationships and solve problems involving objects and their relations. It provides examples of coding family tree facts and rules to define relationships like parent, mother, sister, father, and brother in Prolog. It also shows executing sample queries against the family tree data.

Uploaded by

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

PROLOG Part 2

Session 5 Slides
Case studies
 In this session we are going to look at a number of scenarios
which can be handled using Prolog
 We will start with the family tree
 Here we will define family relationships using Prolog
 Prolog here we solve problems that involve objects and their
relations
Case studies
 Family tree
Case studies
 Here Maria and John are parents of Bob
 John is also a parent of Elizabeth
 Bob is a parent of Anna and Peter
 Peter is a parent of Jim

 So we can write the following family tree as follows:


 parent(maria, bob).
 parent(john, bob).
 parent(john, elizabeth).
 parent(bob,anna).
 parent(bob, peter).
 parent(peter, jim).
 The above are the facts
Case studies
 Based on the facts from the family tree the user can query the
Prolog system about the relations in the family tree.
 The arguments of the relations are referred to as constants or
objects such as maria and john.
 These constants are called atoms.
 We may use general objects such as capital letter X and Y or
underscore
 These genaral objects are called variables.
Case studies
 Additional facts about the family tree
 Lets define the sex of the family members
 female(maria).
 male(john).
 male(bob).
 male(peter).
 female(elizabeth).
 female(anna).
 male(jim).
Case studies
 Family tree rules
 How to define a mother and sister relationship in Prolog

 How do we define a rule which shows that the variable X is a


mother of Y.
 mother(X,Y)
Case studies
 Family tree rules

 So here we are saying X is a mother of Y, if X is a parent of Y


and X is a female.
 mother(X,Y) :- parent(X,Y), female(X).
 So if these two clauses parent(X,Y) and female(X), then this
mean that X is a mother of Y.
 :- means if
Case studies
 Family tree rules

 How to define the sister relationship


 Here we are saying X is a sister of Y if they have the same
parent Z.
 So we can represent this in Prolog as follows:
 sister(X,Y) :- parent(Z,X), parent(Z,Y), female(X), X\==Y.
Case studies
 Family tree rules

 Here Z is the common parent for both sisters X and Y


 And X\==Y. means sister X is not equal to sister Y.
Case studies
 Family tree rules
 We can also define the father
 father(X,Y) :-parent(X,Y),male(X).
 Here we can define the brother relationship
 brother(X,Y) :- parent(Z,X), parent(Z,Y), male(X), X\==Y.
 We can also come up with another rule
 hasChild(X) :- parent(X,_).
 Here X has a child, if X is a parent of someone, and that
someone is unknown, so we denote the unknown variable in
Prolog using an underscore.
Execution in Prolog
 Here am using notepad++ to write my facts and rules
 The program has been divided into three sections namely the
domains, predicates and clauses
 the domains section is used to declare objects
 name, sex = symbol
 age = integer
 Symbol indicates that the object is of type text or string.
 The predicates section of the program simply lists each
predicate, showing the types (domains) of its arguments. Facts
and rules define predicates.
 The clauses section is where you put all the facts and rules that
make up your program
Execution in Prolog
 Here am using notepad++ to write my facts and rules
 However, the domains and the predicates section in our program
will not be executed, the idea was just to show you the structure
of the Prolog example
 So l will put the domains and the predicate sections in comment,
so the Prolog compiler will ignore that section
 /* and the section */
Execution in Prolog
Execution in Prolog
 So now l can run some queries
 Who is the parent of bob?
 ?- parent(X,bob).

 This is what l get and this shows that there is another variable
left with the ? For the other parent
 So l can press semi-colon and the other parent shows
Execution in Prolog

 So we can check from our facts if this quesry is true

 And we can see that Maria and John are parents of Bob.
Execution in Prolog

 Who has a child?


Execution in Prolog

 So the query is true


Homework 1
Homework

 Represent the following facts in Prolog


 Show the cousins in the family
 Who is married to who in the family?
Homework 2
Homework

 Represent the following facts in Prolog


 Do Mice eat Cheese?
 Is Bob a Builder?

You might also like