views:

468

answers:

3

I'm trying to work on a homework assignment based on the old song, I'm my own grandpa.

So, I've started out defining rules for who a son, daughter, father, father_in_law, etc was.

However, something must be wrong with the order of my rules/facts because every time I load it I get the following errors:

GNU Prolog 1.3.1
By Daniel Diaz Copyright (C) 1999-2009
Daniel Diaz | ?- [grandpa]. compiling /home/nfs/student/USER/cs4700/grandpa.pl
for byte code...
/home/nfs/student/USER/cs4700/grandpa.pl:119: warning: discontiguous predicate child/2 - clause ignored
/home/nfs/student/USER/cs4700/grandpa.pl:120: warning: discontiguous predicate child/2 - clause ignored
/home/nfs/student/USER/cs4700/grandpa.pl:121: warning: discontiguous predicate child/2 - clause ignored
/home/nfs/student/USER/cs4700/grandpa.pl:122: warning: discontiguous predicate child/2 - clause ignored
/home/nfs/student/USER/cs4700/grandpa.pl:123: warning: discontiguous predicate child/2 - clause ignored
/home/nfs/student/USER/cs4700/grandpa.pl:124: warning: discontiguous predicate child/2 - clause ignored
/home/nfs/student/USER/cs4700/grandpa.pl:125: warning: discontiguous predicate son/2 - clause ignored
/home/nfs/student/USER/cs4700/grandpa.pl:126: warning: discontiguous predicate son/2 - clause ignored
/home/nfs/student/USER/cs4700/grandpa.pl:127: warning: discontiguous predicate son/2 - clause ignored
/home/nfs/student/USER/cs4700/grandpa.pl:128: warning: discontiguous predicate son/2 - clause ignored
/home/nfs/student/USER/cs4700/grandpa.pl:129: warning: discontiguous predicate son/2 - clause ignored
/home/nfs/student/USER/cs4700/grandpa.pl:130: warning: discontiguous predicate daughter/2 - clause ignored
/home/nfs/student/USER/cs4700/grandpa.pl:131: warning: discontiguous predicate married/2 - clause ignored
/home/nfs/student/USER/cs4700/grandpa.pl:132: warning: discontiguous predicate married/2 - clause ignored
/home/nfs/student/USER/cs4700/grandpa.pl:133: warning: discontiguous predicate married/2 - clause ignored
/home/nfs/student/USER/cs4700/grandpa.pl:134: warning: discontiguous predicate married/2 - clause ignored
/home/nfs/student/USER/cs4700/grandpa.pl:135: warning: discontiguous predicate son_in_law/2 - clause ignored
/home/nfs/student/USER/cs4700/grandpa.pl:136: warning: discontiguous predicate father_in_law/2 - clause ignored
/home/nfs/student/USER/cs4700/grandpa.pl:137: warning: discontiguous predicate father/2 - clause ignored
/home/nfs/student/USER/cs4700/grandpa.pl:138: warning: discontiguous predicate father/2 - clause ignored
/home/nfs/student/USER/cs4700/grandpa.pl:139: warning: discontiguous predicate father/2 - clause ignored
/home/nfs/student/USER/cs4700/grandpa.pl:140: warning: discontiguous predicate mother/2 - clause ignored
/home/nfs/student/USER/cs4700/grandpa.pl:141: warning: discontiguous predicate mother/2 - clause ignored
/home/nfs/student/USER/cs4700/grandpa.pl:142: warning: discontiguous predicate mother/2 - clause ignored
/home/nfs/student/USER/cs4700/grandpa.pl:143: warning: discontiguous predicate step_mother/2 - clause ignored
/home/nfs/student/USER/cs4700/grandpa.pl:144: warning: discontiguous predicate brother_in_law/2 - clause ignored
/home/nfs/student/USER/cs4700/grandpa.pl:145: warning: discontiguous predicate brother_in_law/2 - clause ignored
/home/nfs/student/USER/cs4700/grandpa.pl:146: warning: discontiguous predicate uncle/2 - clause ignored
/home/nfs/student/USER/cs4700/grandpa.pl:147: warning: discontiguous predicate step_daughter/2 - clause ignored
/home/nfs/student/USER/cs4700/grandpa.pl compiled, 149 lines read - 8389 bytes written, 44 ms

And my code so far is:

child(X,Y):-
   son(Y,X).

child(X,Y):-
   daughter(Y,X).

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

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

son(X,Y):-
    child(X,Y),
    male(X).

daughter(X,Y):-
    child(X,Y),
    female(X).

son_in_law(X,Y):-
    child(X,Z),
    not(child(X,Y)),
    married(Z,Y),
    male(X).

step_daughter(X,Y):-
    child(X,Z),
    married(Z,Y),
    not(child(X,Y)),
    female(X).

brother(X,Y):-
    sibling(X,Y),
    male(X).

brother_in_law(X,Y):-
    parent(Z,X),
    parent(Z,Y),
    not(sibling(X,Y)),
    male(X).

sibling(X,Y):-
    parent(Z,X),
    parent(Z,Y).

sister(X,Y):-
    sibling(X,Y),
    female(X).

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

father_in_law(X,Y):-
    child(X,Z),
    married(Y,Z),
    not(child(X,Y)),
    male(X).

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

step_parent(X,Y):-
    married(X,Z),
    parent(Z,Y),
    not(parent(X,Y)).

step_father(X,Y):-
    step_parent(X,Y),
    male(X).

step_mother(X,Y):-
    step_parent(X,Y),
    female(X).

grandparent(X,Y):-
    parent(X,Z),
    parent(Z,Y).

grandmother(X,Y):-
    grandparent(X,Y),
    female(X).

grandfather(X,Y):-
    grandparent(X,Y),
    male(X).

grandchild(X,Y):-
    child(X,Z),
    child(Z,Y).

married(X,Y):-
    wife(X,Y),
    female(X).

married(X,Y):-
    husband(X,Y),
    male(X).

uncle(X,Y):-
    sibling(X,Z),
    parent(Z,Y),
    male(X).

aunt(X,Y):-
    sibling(X,Z),
    parent(Z,Y),
    female(X).

male(i).
male(f).
male(s1).
male(s2).
female(w).
female(d).
child(i,f).
child(s1,w).
child(s1,i).
child(s2,d).
child(s2,f).
child(d,w).
son(i,f).
son(s1,w).
son(s1,i).
son(s2,d).
son(s2,f).
daughter(d,w).
married(i,w).
married(w,i).
married(f,d).
married(d,f).
son_in_law(f,i).
father_in_law(i,f).
father(f,i).
father(i,s1).
father(f,s2).
mother(w,s1).
mother(w,d).
mother(d,s2).
step_mother(d,i).
brother_in_law(f,s1).
brother_in_law(s1,f).
uncle(s1,i).
step_daughter(d,i).

I'm very new to prolog so I'm probably just making some fundamental mistake. Can someone help point me in the right direction concerning these errors?

+1  A: 

It was a long time since I used Prolog but I found this. And from that I conclude that you either need to use the discontiguous thingy first or you need to group all your rules together (i.e. having everything with child in one place, i.e. mixing your rules and facts). Example:

child(X,Y):- son(Y,X).

child(X,Y):- daughter(Y,X).

child(i,f).

child(s1,w).

child(s1,i).

child(s2,d).

child(s2,f).

child(d,w).

Cellfish
A: 

From http://www.gprolog.org/manual/gprolog.html#htoc50 put this at the top of your code:

discontiguous([child, son, daughter, married, etc])

So you can suppress this warning. You'll have a bit of fun digging your way out of recursion and stack overflow issues, but that's part of the fun of programming!

p.s.- 'directive' is a little more formal than 'thingy', but I like it :-)

Joel
A: 

You can reorganize the rules or use the discontiguous directive as hinted above.

Alternatively, and this would make sense with regards to the program use, you could declare the facts associated with the song narrative as dynamic. The production rules associated with a son, daugher etc. are established (even if the issue of married(X,Y) is hotly debated ;-) ), whereby the facts of the song need to be asserted.

Another thing: unless you are debugging/checking the sanity of your rules, you don't need to spell-out all the facts, only these from the lyrics. For example: "I had a baby son [from w]" gives you son(s1, i) and [implied by song] son(s1, w), but you can let prolog infer that male(s1) or child(s1, i), if that is even useful to its assertions.

Have fun !

mjv