Prolog grandfather(i,i)

泄露秘密 提交于 2019-12-23 03:13:13

问题


A (very) strange story: I married a widow(W) who has a daughter(D). My father(F) married my stepdaughter (D). My wife gave birth to a son(s1). The wife of my father (the stepdaughter) also had a son (s2).

The goal of this project is to input:

grandfather(i,i).

and return yes in prolog.

Here is what I have so far:

%facts

father(f,i).

husband(i,w).

husband(f,d).

mother(w,d).

mother(w,s1).

father(i,s1).

mother(d,s2).

father(f,s2).

%rules

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

father(X,Y) :- husband(X,Z),mother(Z,Y).

f_in_law(X,Y) :- husband(Z,Y),father(X,Z).

b_in_law(X,Y) :- husband(Z,Y),brother(X,Z).

%brother(X,Y) :- b_in_law(X,Y).

uncle(X,Y) :- father(Z,Y),brother(X,Z).

grandfather(X,Y) :- father(Z,Y),father(X,Z).

I traced through it to see what went wrong. father(f,i) is true so that's good! But father(i,f) is thought of as false. Any suggestions/ideas on how to correct this? I appreciate any input as I am rather new to prolog.


回答1:


Should the predicate be

f_in_law(X,Y) :- husband(Y,Z),father(X,Z).

instead of

f_in_law(X,Y) :- husband(Z,Y),father(X,Z).



回答2:


I have reformulated the riddle

father(i, s1).
father(f, i).
father(f, s2).

fatlaw(X, Y) :- husband(X, Z), mother(Z, Y).

mother(w, d).
mother(w, s1).
mother(d, s2).

motlaw(X, Y) :- husband(Z, X), father(Z, Y).

husband(i, w).
husband(f, d).

grandfather(X, Y) :-
    ( father(X, Z) ; fatlaw(X, Z) )
    , ( father(Z, Y) ; fatlaw(Z, Y) ; mother(Z, Y) ; motlaw(Z, Y) )
    .

the point seems to be that grandfather must accept fake biological offsprings (I hope this is reasonable English).

with that

?- grandfather(X,X).
X = i ;
false.


来源:https://stackoverflow.com/questions/15063544/prolog-grandfatheri-i

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!