本文介绍了Prolog -- 对称谓词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须在序言中模拟家谱.我有对称谓词的问题.事实:

I have to simulate family tree in prolog.And i have problem of symetrical predicates.Facts:

parent(x,y).
male(x).
female(y).
age(x, number).

规则:

blood_relation 让我头疼.这就是我所做的:

blood_relation is giving me headache. this is what i have done:

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).

而且我得到了我认为令人满意的结果(我有双重打印 - 我可以解决这个问题),问题是我希望这种关系是对称的.现在不是.

and I am getting i think satisfactory results(i have double prints - can i fix this), problem is that i want that this relation be symmetrical. It is not now.

blood_relation(johns_father, joh):yes
blood_relation(john,johns_father): no

所以..有没有办法解决这个问题.而且我需要查询:所有不在 Blood_relation 中的对..

so..is there a way to fix this.And i need query: All pairs that are not in blood_relation..

第一个语句应该满足什么样的关系?Blood_relation(X,Y):-blood_relation(X,Y).

对不起..这是一个错误的复制/粘贴..它

sorry..it is a bad copy/paste..it

blood_relation(X,Y):-ancestor(X,Y).

现在在上面修复了.

这里是其他规则:

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).

母亲的兄弟在叔叔的定义中.这有点奇怪.我有需要实施的规则,除此之外我不知道如何实施规则.我只是很困惑.

Mother's brother is in uncle definition. It's kind of strange. I've got rules that I need to implement, and I don't know how I can implement rules besides that. I'm just confused.

知道如何使 blood_relation 对称吗?not_blood_relation 是一个新规则.我需要查询.这个真让我头疼.也许是因为关系写得像废话.

Any idea how to make blood_relation symmetric? And not_blood_relation is a new rule. And I need query. This one is really giving me headache. Maybe because relation is written like crap.

而且没有更多的事实.就这样.所有规则和所有事实.

And there are no more facts. That's all. All rules, and all facts.

query.. not(blood_relation(X,Y)) 不起作用,我真的不知道为什么.例如查询:

query.. not(blood_relation(X,Y)) doesn't work, and I really don't know why.For example query:

age(X,Y), Y>18,
not(parent(X,Z)),write(X),nl,fail.

工作正常

推荐答案

有点像作业,是不是...

A bit looks like a homework, isn't it...

大多数 prolog 初学者没有想到的一个技巧是列表模式匹配.想想像 [a1,[[a2],[b2,[[e3],[f3]]],[c2]]] 这样的树,就像 <tree>=[root,[>,>,...]]:

One trick which most of beginners of prolog don't think of is list pattern matching. Think of a tree like [a1,[[a2],[b2,[[e3],[f3]]],[c2]]] as in <tree>=[root,[<tree1>,<tree2>,...]]:

%Y is immediate child of X?
child(X,Y,[X|S]) :- member([Y|_],S).

%pick one tree in S and check
child(X,Y,[X|S]) :- member([Z|SS],S),child(Z,Y,[Z|SS]).

%X and Y end up with same root?
sib(X,Y,[R|T]) :- child(R,X,[R|T]), child(R,Y,[R|T]).

我认为您可以改进这一点,例如使用对作为根,添加性别,为树成员的特定关系命名......

I think you can improve upon this like, using pairs as roots, adding genders, giving names to specific relations of members of the tree...

这篇关于Prolog -- 对称谓词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-17 16:25
查看更多