SWI Prolog陷入了奇怪的行为

时间:2015-01-10 23:51:06

标签: prolog

所以这就是事情。我正试图在prolog中制作一种“完美匹配”程序。 你给它一对,每个人都有一些“特征”。该程序是假设的 根据这些特点,在未来的婚姻中回归可能存在的问题。但是当我尝试这样做并且事情变得复杂时,我就会遇到这个问题:

1 ?- marriage_problems(Couple).
Wifename(sharon,marsh)
Husbandname(randy,marsh)
Possible problem:Wife is lazy while husband is demanding.Not matching.
true ;
Possible problem: Wife is agressive while husband is sensitive.Not matching.
true ;
Possible problem: Wife is sensitive while husband is agressive.Not matching.
true ;
Possible problem: Wife is sensitive while husband is agressive.Not matching.
true ;
Possible problem: Wife is sensitive while husband is agressive.Not matching.
true ;
Possible problem: Wife is sensitive while husband is agressive.Not matching.
true 

它永远不会阻止awsering妻子是敏感而丈夫是..不匹配。它也是错误的。这里是代码:

couple(wife(name(sharon,marsh),char(economical([stingy,lazy]),sexual([agressive]))),
   husband(name(randy,marsh),char(economical([demanding,wasteful]),sexual([sensitive])))).



marriage_problems(Couple):- couple(wife(Wname,char(economical(W_eco),sexual(W_sex))),
                                   husband(Hname,char(economical(H_eco),sexual(H_sex)))),
                                   write('Wife'),write(Wname),nl,
                                   write('Husband'),write(Hname),nl,
                                   marriage_economical_problems(W_eco,H_eco);
                                   marriage_sexual_problems(W_sex,H_sex).





marriage_economical_problems(W_eco,H_eco):-              couple(wife(_,char(economical(W_eco),_)),husband(_,char(economical(H_eco),_))),
                                                         member(demanding,W_eco),
                                                         member(lazy,H_eco),
                                                         W_pr=demanding,
                                                         H_pr=lazy,
                                                         write('Possible problem:Wife is '),write(W_pr),write(' while husband is '),
                                                         write(H_pr),write('.Not matching.'),nl;
                                                         member(lazy,W_eco),
                                                         member(demanding,H_eco),
                                                         W_pr=lazy,
                                                         H_pr=demanding,
                                                         write('Possible problem:Wife is '),write(W_pr),write(' while husband is '),
                                                         write(H_pr),write('.Not matching.'),nl.


marriage_sexual_problems(W_sex,H_sex):-             couple(wife(_,char(_,sexual(W_sex))),husband(_,char(_,sexual(H_sex)))),
                                                    member(agressive,W_sex),
                                                    member(sensitive,H_sex),
                                                    W_pr=agressive,
                                                    H_pr=sensitive,
                                                    write('Possible problem: Wife is '),write(W_pr),write(' while husband is '),
                                                    write(H_pr),write('.Not matching.'),nl;
                                                    member(sensitive,W_sex),
                                                    member(agressive,H_sex),
                                                    W_pr=sensitive,
                                                    H_pr=agressive,
                                                    write('Possible problem: Wife is '),write(W_pr),write(' while husband is '),
                                                    write(H_pr),write('.Not matching.'),nl.

这个奇怪的作品:

1 ?- marriage_problems(Couple).
Wifename(sharon,marsh)
Husbandname(randy,marsh)
Possible problem:Wife is lazy while husband is demanding.Not matching.
true ;
Possible problem: Wife is agressive while husband is sensitive.Not matching.
true.

代码:

couple(wife(name(sharon,marsh),char(economical([stingy,lazy]),sexual([agressive]))),
       husband(name(randy,marsh),char(economical([demanding,wasteful]),sexual([sensitive])))).



marriage_problems(Couple):- couple(wife(Wname,char(economical(W_eco),sexual(W_sex))),
                                   husband(Hname,char(economical(H_eco),sexual(H_sex)))),
                                   write('Wife'),write(Wname),nl,
                                   write('Husband'),write(Hname),nl,
                                   marriage_economical_problems(W_eco,H_eco);
                                   marriage_sexual_problems(W_sex,H_sex).





marriage_economical_problems(W_eco,H_eco):-              couple(wife(_,char(economical(W_eco),_,_,_)),husband(_,char(economical(H_eco),_,_,_))),
                                                         member(demanding,W_eco),
                                                         member(lazy,H_eco),
                                                         W_pr=demanding,
                                                         H_pr=lazy,
                                                         write('Possible problem:Wife is '),write(W_pr),write(' while husband is '),
                                                         write(H_pr),write('.Not matching.'),nl;
                                                         member(lazy,W_eco),
                                                         member(demanding,H_eco),
                                                         W_pr=lazy,
                                                         H_pr=demanding,
                                                         write('Possible problem:Wife is '),write(W_pr),write(' while husband is '),
                                                         write(H_pr),write('.Not matching.'),nl.


marriage_sexual_problems(W_sex,H_sex):-             couple(wife(_,char(_,sexual(W_sex))),husband(_,char(_,sexual(H_sex)))),
                                                    member(agressive,W_sex),
                                                    member(sensitive,H_sex),
                                                    W_pr=agressive,
                                                    H_pr=sensitive,
                                                    write('Possible problem: Wife is '),write(W_pr),write(' while husband is '),
                                                    write(H_pr),write('.Not matching.'),nl.

因此,当它变得更复杂时,由于某种原因它不起作用.. 我真的无法理解发生了什么。我只有几个月熟悉 prolog。几天来我一直在玩这个问题而没有运气。但是现在 我已经没时间了,我需要完成任务。任何帮助都将受到赞赏。 提前谢谢。

1 个答案:

答案 0 :(得分:1)

你滥用OR运算符(;)/ 2,你可能意味着

marriage_problems(Couple):- couple(wife(Wname,char(economical(W_eco),sexual(W_sex))),
                                   husband(Hname,char(economical(H_eco),sexual(H_sex)))),
                                   write('Wife'),write(Wname),nl,
                                   write('Husband'),write(Hname),nl,
                                   ( marriage_economical_problems(W_eco,H_eco);
                                     marriage_sexual_problems(W_sex,H_sex) ).

没有parens,W_sex和H_sex在调用marriage_sexual_problems / 2时未绑定。 SWI-Prolog能够发出症状信号:

Warning: /home/carlo/prolog/so.pl:81:
    Singleton variable in branch: W_sex
    Singleton variable in branch: H_sex
Warning: /home/carlo/prolog/so.pl:81:
    Singleton variables: [Couple]

还要注意情侣没用......