Prolog Programming (Practical component for CSNB234) Contents: This manual provides code examples and exercises on the following: How to use the IDE to edit and build the KB How to use the “Listener” to consult & re-consult the Prolog program How to write clauses: facts and rules How to process lists through tail recursion How to control backtracking How to develop simple Knowledge-based systems PREPARED BY: Alicia Tang Systems and Networking Department Universiti Tenaga Nasional MALAYSIA Emails: aliciat@uniten.edu.my Lab No: Topic: Language: Engine: 1 Basic Facts and Rules Prolog AMZI! IDE 6.2.2 INSTRUCTIONS: Answer ALL the questions in the end of this question paper. Submit the solutions (in hardcopies) to me (Room: BW-3-C27) before 6:00pm. Check the exact date for the respective sections. This lab exercise carries 2% of the CSNB234 course. NOTE Download all the .txt or pro collection of files. Study and test each program logics. Enhance them if necessary. Do not copy answers from your friends. 1. Retrieve the following program (from LAB-1 link) and consult using Prolog Listener (via its IDE). This question is to test student ability to run/consult a simple knowledge base. /* Facts */ parent(mary,tom). parent(john,tom). parent(mary,alice). parent(john,alice). sex(mary, female). sex(john, male). sex(tom, male). sex(alice, female). /* Rules */ mother(X):sex(X,female), parent(X,_). father(X):sex(X, male), parent(X,_). sibling(X,Y):parent(M,X), sex(M,female), parent(F,X), sex(F,male), parent(M,Y), parent(F,Y). sibling1(X,Y):parent(M,X), sex(M,female), parent(F,X), sex(F,male), parent(M,Y), parent(F,Y), X \= Y. go:nl, nl, write('Hello there .....'), nl, write('Testing on Prolog2.'), nl, write('End Job'), nl. 2. This question is to test the student ability to structure query (i.e. to ask the “right” questions). parent( pam, bob). parent( tom, bob). parent( tom, liz). parent( bob, ann). parent( bob, pat). parent( pat, jim). % Pam is a parent of Bob female( pam). male( tom). male( bob). female( liz). female( ann). female( pat). male( jim). % Pam is female % Tom is male offspring( Y, X) :parent( X, Y). % Y is an offspring of X if % X is a parent of Y mother( X, Y) :parent( X, Y), female( X). % X is the mother of Y if % X is a parent of Y and % X is female grandparent( X, Z) :parent( X, Y), parent( Y, Z). % X is a grandparent of Z if % X is a parent of Y and % Y is a parent of Z sister( X, Y) :parent( Z, X), parent( Z, Y), female( X), % X is a sister of Y if % X and Y have the same parent and % X is female and different( X, Y). % X and Y are different predecessor( X, Z) :parent( X, Z). % Rule prl: X is a predecessor of Z predecessor( X, Z) :parent( X, Y), predecessor( Y, Z). % Rule pr2: X is a predecessor of Z 3. Suppose you are given the following facts: /****************************************/ /* Family tree example */ /****************************************/ parent(pam, bob). parent(tom, bob). parent(tom, liz). parent(bob, ann). parent(bob, pat). parent(tina, ann). parent(tina, pat). parent(pat, jim). parent(mary, jim). parent(ann, george). parent(michael, george). parent(ann, sophie). parent(ian, sophie). /* ------ */ parent(lou, kenny). parent(albert, kenny). parent(lou, pete). parent(lou, pauline). parent(albert, pauline). parent(pat, simon). parent(pete, simon). parent(kenny, simon). parent(kenny, elizabeth). parent(cathy, ian). parent(pete, ian). parent(pauline, michelle). parent(arthur, michelle). parent(michelle, vicky). parent(den, vicky). parent(cathy, donna). parent(kenny, elizabeth). /* ------ */ sex(pam, female). sex(tom, male). sex(bob, male). sex(tina, female). sex(liz, female). sex(ann, female). sex(pat, male). sex(mary, female). sex(michael, male). sex(george, male). sex(ian, male). sex(jim, male). sex(sophie, female). sex(lou, female). sex(pat, female). sex(elizabeth, female). sex(cathy, female). sex(donna, female). sex(pauline, female). sex(michelle, female). sex(vicky, female). sex(angie, female). sex(sharon, female). sex(kenny, male). sex(simon, male). sex(pete, male). sex(ian, male). sex(arthur, male). sex(den, male). sex(lofty, male). sex(albert, male). /* ------ */ married(cathy, pete). married(pauline, arthur). married(michelle, lofty). married(lou, albert). Lab Exercise #1 Instructions: Use Program no. 3 (above) to answer the following basic questions. Since program no. 3 contains no rules (at all), you are required to define a few rules that make use of the existing facts parent(Parent, Child). sex(Person, Sex). % where Sex is one of “male” or “female” married(Wife, Husband). Examples of rules needed may be: mother(), father(), etc. [Think of it!] You may need to use compound predicates when making queries (if there is no suitable rule to use). Basic questions: Construct queries at Prolog Listener to observe answers for each of the following: 1. 2. 3. 4. 5. 6. 7. 8. 9. Find all the married couples Find out all married females in the KB Find pat’s father Find arthur’s mother Find all of bob’s parents Find out if simon is a parent of jim Find out if donna is a parent of tina Find all of cathy’s parents Find michelle’s gender type