Presentation is loading. Please wait.

Presentation is loading. Please wait.

Prolog programming Introduction to Prolog (part3)

Similar presentations


Presentation on theme: "Prolog programming Introduction to Prolog (part3)"— Presentation transcript:

1 Prolog programming Introduction to Prolog (part3)
CS 370

2 How to solve this problem?
Consider the following facts: parent(pam,bob). parent(tom,bob). parent(tom,liz). parent(bob,ann). parent(bob,pat). parent(pat,jim). Is Liz a child of Tom? ?- child(liz, tom). The answer will be “error “ WHY ? Because there are no facts in the program about a child.

3 Defining relations by rules.
To solve the previous child(liz, tom) relation problem we have to either to : Use the parent facts ( which were defined previously ) ,considering the parent fact as the inverse of child relation. Ex : ?- parent (tom ,liz) . OR Use the alternative way based of the following logical statements: For all X and Y, Y is a child of X if X is a parent of Y.

4 Defining relations by rules.
In prolog , we can define the offspring relation using the alternative logical statement in the following way: child(Y,X) :- parent(X, Y) This clause is called a rule Each rule has two parts: Condition. (The right hand side) Conclusion. (The left hand side) So, the child rule consist of : child(Y,X) -> as a conclusion part. parent(X,Y) > as a condition part.

5 Defining relations by rules.
The differences between rule and fact: Fact: is a something that always, unconditionally true. Rule: specify things that are true if some condition is satisfied. It has two parts : condition and conclusion separated by “ :- “.

6 Defining relations by rules.
How rules are actually used by prolog program? ?- child(liz, tom). X= tom, Y= liz. The compiler will search for the initial goal which is : chlid(liz,tom). If the rule was defined , the condition part will be executed: parent(tom,liz). The condition can be found using the facts about parent relation. After finding that the condition part is true, the conclusion part will be also true.

7 Anonymous Variables Do somebody have a child? Who is a parent ?
Example : Does Bob have a child ? -> hasachild( X ) :- parent( X, _). Do somebody have a child? somebody_has_child :- parent( _, _). somebody_has_child :- parent( X, X). Who is a parent ? ?- parent( X, _). In this query we are interested in people who have children, but not in the names of the children.

8 Lexical Scope The lexical scope of A variable is one clause. If X occurs in two clauses, then it signifies two different variables. hasachild(X) :- parent( X, Y). isapoint(X) :- point( X, Y, Z). But each occurrence of X with in the same clause means the same variables. hasachild( X) :- parent( X, Y). An atom is always the same object in any clause throughout the whole program.

9 Class exercise(2) Write a rule to describe the grandparent relation?

10 Home exercise(1) Write a rule to describe the sister relation?


Download ppt "Prolog programming Introduction to Prolog (part3)"

Similar presentations


Ads by Google