Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Family Tree Out Of Local Stack Error

Status
Not open for further replies.

bsplitkids

Programmer
Dec 3, 2010
1
0
0
US
I am learning the prolog language and I'm starting with the family tree. When I try to ask different questions I get errors. For instance when I asked was sally the parent of rojai parent(sally,rojai). I got this error message ERROR: Out of local stack
Exception: (310,003) parent(sally, rojai) ? Iooked this error up online and I saw that it had to do with recursion, but I dont know how to fix it. I also got this error when I tried the following:
granddaughter(janae,sheri).
parent(sally,Y).
uncle(desmond,Y).
Does it have something to do with the order of my rules or the parent rule itsself?

The following code:
father(james,sheri).
father(ij,rojai).
father(michael,pam).
father(michael,desmond).
father(michael,tonya).
father(rojai,janae).
father(rojai,imani).
father(desmond,tyson).
father(desmond,miya).
father(desmond,demonica).
father(jason,taylor).
father(jason,bria).
father(X,Y) :- parent(X,Y), male(X).

mother(gwendolyn,sheri).
mother(sally,rojai).
mother(sheri,pam).
mother(sheri,desmond).
mother(sheri,tonya).
mother(pam,janae).
mother(pam,imani).
mother(cheri,tyson).
mother(cheri,miya).
mother(cheri,demonica).
mother(tonya,taylor).
mother(tonya,bria).

male(desmond).
male(james).
male(ij).
male(rojai).
male(tyson).
male(jason).

female(sheri).
female(pam).
female(tonya).
female(janae).
female(imani).
female(miya).
female(demonica).
female(taylor).
female(bria).
female(gwendolyn).
female(cheri).

parent(X,Y) :- father(X,Y).
parent(X,Y) :- mother(X,Y).
mother(X,Y) :- parent(X,Y),female(X).
grandparent(X,Y) :- parent(X,Z),parent(Z,Y).
granddaughter(X,Y) :- female(X),grandparent(Y,X).
sibling(X,Y) :- parent(Z,X),parent(Z,Y),X\==Y.
brother(X,Y) :- sibling(X,Y),male(X),X\==Y.
sister(X,Y) :- sibling(X,Y),female(X),X\==Y.
uncle(X,Y) :- male(X),sibling(X,Z),parent(Z,Y).
aunt(X,Y) :- female(X),sibling(X,Z),parent(Z,Y).
niece(X,Y) :- female(X),sibling(Z,Y),parent(Z,X).
 
I think I kinda fixed it.

father(james,sheri).
father(ij,rojai).
father(michael,pam).
father(michael,desmond).
father(michael,tonya).
father(rojai,janae).
father(rojai,imani).
father(desmond,tyson).
father(desmond,miya).
father(desmond,demonica).
father(jason,taylor).
father(jason,bria).
father(X,Y) :- male(X),parent(X,Y).

mother(gwendolyn,sheri).
mother(sally,rojai).
mother(sheri,pam).
mother(sheri,desmond).
mother(sheri,tonya).
mother(pam,janae).
mother(pam,imani).
mother(cheri,tyson).
mother(cheri,miya).
mother(cheri,demonica).
mother(tonya,taylor).
mother(tonya,bria).
mother(X,Y) :- female(X),parent(X,Y).

male(desmond).
male(james).
male(ij).
male(rojai).
male(tyson).
male(jason).

female(sheri).
female(pam).
female(tonya).
female(janae).
female(imani).
female(miya).
female(demonica).
female(taylor).
female(bria).
female(gwendolyn).
female(cheri).

parent(X,Y) :- father(X,Y).
parent(X,Y) :- mother(X,Y).
grandparent(X,Y) :- parent(X,Z),parent(Z,Y).
granddaughter(X,Y) :- female(X),grandparent(Y,X).
sibling(X,Y) :- parent(Z,X),parent(Z,Y),X\==Y.
brother(X,Y) :- sibling(X,Y),male(X),X\==Y.
sister(X,Y) :- sibling(X,Y),female(X),X\==Y.
uncle(X,Y) :- male(X),sibling(X,Z),parent(Z,Y).
aunt(X,Y) :- female(X),sibling(X,Z),parent(Z,Y).
niece(X,Y) :- female(X),sibling(Z,Y),parent(Z,X).
 
What you need to do is remove these 2 clauses:

Code:
father(X, Y) :- parent(X, Y), male(X).

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

You define some 'father' clauses, you define some 'mother' clauses, the 'father' clause uses the 'parent' clause and the 'parent' clause uses the 'father' clause again. While this is not a conceptual problem, it is a conceptual problem here in your code because you just make these calls with the same parameters, so Prolog will just go in a recursive loop without anything to get him out.

Code:
parent(X, Y) :- father(X, Y).
father(X, Y) :- parent(X, Y), ...

See the problem? When you call parent(sally, rojai) Prolog will call father(sally, rojai) which calls parent(sally, rojai) again and so on.

'father' and 'mother' are defined with concrete facts, so you don't need to define them further using 'parent', because 'parent' is defined in terms of 'father' and 'mother'.

There are still some minor problems in your code, but your main problem is solved by removing those 2 clauses
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top