为什么这不能在Prolog中定义"已婚"?
married(X,Y):-married(Y,X).
Run Code Online (Sandbox Code Playgroud)
这些循环谓词是不允许的?我该如何解决?
谢谢
我必须在prolog中模拟家谱.我有对称谓词的问题. 事实:
parent(x,y).
male(x).
female(y).
age(x, number).
Run Code Online (Sandbox Code Playgroud)
规则:
blood_relation让我头痛.这就是我所做的:
blood_relation(X,Y):-ancestor(X,Y).
blood_relation(X,Y):-uncle(X,Y);brother(X,Y);sister(X,Y);(mother(Z,Y),sister(X,Z));(father(Z,Y),sister(X,Z));(father(Z,Y),brother(X,Z)).
blood_relation(X,Y):-uncle(X,Z),blood_relation(Z,Y).
Run Code Online (Sandbox Code Playgroud)
我得到了令人满意的结果(我有双版画 - 我可以解决这个问题),问题是我希望这种关系是对称的.现在不是.
blood_relation(johns_father, joh):yes
blood_relation(john,johns_father): no
Run Code Online (Sandbox Code Playgroud)
所以..有办法解决这个问题.我需要查询:所有不在blood_relation中的对..
第一个声明应该满足什么样的关系?blood_relation(X,Y): - blood_relation(X,Y).
抱歉..这是一个糟糕的复制/粘贴.. ..
blood_relation(X,Y):-ancestor(X,Y).
Run Code Online (Sandbox Code Playgroud)
现在修复上面.
这是其他规则:
father(X,Y):-parent(X,Y),male(X).
mother(X,Y):-parent(X,Y),female(X).
brother(X,Y):-parent(Z,X),parent(Z,Y),male(X).
sister(X,Y):-parent(Z,X),parent(Z,Y),female(X).
grandFather(X,Y):-parent(Z,Y),parent(X,Z),male(X).
grandMother(X,Y):-parent(Z,Y),parent(X,Z),female(X).
uncle(X,Y):-mother(Z,Y),brother(X,Z).
ancestor(X,Y):-ancestor(X,Y).
ancestor(X,Y):-parent(X,Z),ancestor(Z,Y).
Run Code Online (Sandbox Code Playgroud)
母亲的兄弟在叔叔的定义.这有点奇怪.我已经制定了需要实施的规则,除此之外我不知道如何实施规则.我只是困惑.
知道如何制作blood_relation对称吗?这not_blood_relation是一个新规则.我需要查询.这个真的让我很头疼.也许是因为关系被写成废话.
并没有更多的事实.就这样.所有规则和所有事实.
查询.. not(blood_relation(X,Y))不起作用,我真的不知道为什么.例如查询:
age(X,Y), Y>18,
not(parent(X,Z)),write(X),nl,fail.
Run Code Online (Sandbox Code Playgroud)
工作得很好