
- Prolog - Home
- Prolog - Introduction
- Prolog - Environment Setup
- Prolog - Hello World
- Prolog - Basics
- Prolog - Relations
- Prolog - Data Objects
- Loop & Decision Making
- Conjunctions & Disjunctions
Prolog Operators
- Prolog - Type of Operators
- Prolog - Arithmetic Comparison Operators
- Prolog - Unification Operators
- Prolog - Term Comparision Operators
- Prolog - Arithmetic Operators
- Prolog - Logical Operators
- Prolog - List Operators
- Prolog - Custom Operators
Prolog Lists
- Prolog - Lists
- Prolog - Member of List
- Prolog - Length of List
- Prolog - Concatenating Lists
- Prolog - Appending to a List
- Prolog - Deleting from a List
- Prolog - Inserting into a List
- Prolog - Permutation Operation
- Prolog - Combination Operation
- Prolog - Reverse Items of a List
- Prolog - Shift Items of a List
- Prolog - Check Order of a List
- Prolog - SubSet of a Set
- Prolog - Union of Sets
- Prolog - Intersection of Sets
- Prolog - Even and Odd Length Finding
- Prolog - Divide a List
- Prolog - Find Maximum of a List
- Prolog - Find Minimum of a List
- Prolog - Find Sum of a List
- Prolog - Sorting List using MergeSort
Built-In Predicates
- Prolog - Built-In Predicates
- Prolog - Identifying Terms
- Prolog - Decomposing Structures
- Prolog - Collecting All
- Prolog - Mathematical Predicates
- Prolog - Scientific Predicates
Miscellaneous
- Recursion and Structures
- Prolog - Backtracking
- Prolog - Preventing Backtracking
- Prolog - Different and Not
- Prolog - Inputs and Outputs
- Tree Data Structure (Case Study)
- Prolog - Examples
- Prolog - Basic Programs
- Prolog - Practical Arithmetic Examples
- Prolog - Examples of Cuts
- Towers of Hanoi Problem
- Prolog - Linked Lists
- Monkey and Banana Problem
- Prolog Useful Resources
- Prolog - Quick Guide
- Prolog - Useful Resources
- Prolog - Discussion
Prolog - Tracing
Now let us see some more relationships that we can make from the previous relationships of a family. So if we want to make a grandparent relationship, that can be formed as follows −

We can also create some other relationships like wife, uncle, etc. We can write the relationships as given below −
grandparent(X,Y) :- parent(X,Z), parent(Z,Y). grandmother(X,Z) :- mother(X,Y), parent(Y,Z). grandfather(X,Z) :- father(X,Y), parent(Y,Z). wife(X,Y) :- parent(X,Z),parent(Y,Z), female(X),male(Y). uncle(X,Z) :- brother(X,Y), parent(Y,Z).
So let us write a prolog program to see this in action. Here we will also see the trace to trace-out the execution.
Knowledge Base (family_ext.pl)
female(pam). female(liz). female(pat). female(ann). male(jim). male(bob). male(tom). male(peter). parent(pam,bob). parent(tom,bob). parent(tom,liz). parent(bob,ann). parent(bob,pat). parent(pat,jim). parent(bob,peter). parent(peter,jim). mother(X,Y):- parent(X,Y),female(X). father(X,Y):- parent(X,Y),male(X). haschild(X):- parent(X,_). sister(X,Y):- parent(Z,X),parent(Z,Y),female(X),X\==Y. brother(X,Y):-parent(Z,X),parent(Z,Y),male(X),X\==Y. grandparent(X,Y):-parent(X,Z),parent(Z,Y). grandmother(X,Z):-mother(X,Y),parent(Y,Z). grandfather(X,Z):-father(X,Y),parent(Y,Z). wife(X,Y):-parent(X,Z),parent(Y,Z),female(X),male(Y). uncle(X,Z):-brother(X,Y),parent(Y,Z).
Output
| ?- consult('D:/TP Prolog/Sample Codes/family_ext.pl'). compiling D:/TP Prolog/Sample Codes/family_ext.pl for byte code... D:/TP Prolog/Sample Codes/family_ext.pl compiled, 25 lines read - 4789 bytes written, 4 ms yes | ?- uncle(X,Y). X = peter Y = jim ? ; (16 ms) no | ?- grandparent(X,Y). X = pam Y = ann ? ; X = pam Y = pat ? ; X = pam Y = peter ? ; X = tom Y = ann ? ; X = tom Y = pat ? ; X = tom Y = peter ? ; X = bob Y = jim ? ; X = bob Y = jim ? ; (78 ms) no | ?- wife(X,Y). X = pam Y = tom ? ; X = pat Y = peter ? ; no | ?-
Tracing the output
In Prolog we can trace the execution. To trace the output, you have to enter into the trace mode by typing trace.. Then from the output we can see that we are just tracing pam is mother of whom?. See the tracing output by taking X = pam, and Y as variable, there Y will be bob as answer. To come out from the tracing mode press notrace.
Program
| ?- consult('D:/TP Prolog/Sample Codes/family_ext.pl'). compiling D:/TP Prolog/Sample Codes/family_ext.pl for byte code... D:/TP Prolog/Sample Codes/family_ext.pl compiled, 25 lines read - 4789 bytes written, 4 ms yes | ?- mother(X,Y). X = pam Y = bob ? ; X = pat Y = jim ? ; (31 ms) no | ?- trace. The debugger will first creep -- showing everything (trace) yes {trace} | ?- mother(pam,Y). 1 1 Call: mother(pam,_23) ? 2 2 Call: parent(pam,_23) ? 2 2 Exit: parent(pam,bob) ? 3 2 Call: female(pam) ? 3 2 Exit: female(pam) ? 1 1 Exit: mother(pam,bob) ? Y = bob (47 ms) yes {trace} | ?- notrace. The debugger is switched off yes | ?-
Advertisements