SWI Prolog 坚持奇怪的行为

SWI Prolog stuck with odd behavior

所以这里是 thing.I 我试图在序言中制作一种 "perfect match" 程序。 你给它一对,每个人都有一些"characteristics"。这个程序应该是 return 未来婚姻中可能出现的问题取决于那些 characteristics.But 当我尝试这样做并且事情变得复杂时,我遇到了这个问题:

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 

它永远不会停止回答妻子敏感而丈夫敏感..不是matching.And它也是wrong.And这里是代码:

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.

所以当它变得更复杂时,由于某种原因它不起作用.. 我真的不明白这是怎么回事on.I 我才几个月才熟悉 prolog.I我自己已经解决这个问题几天了,现在 luck.But 我 运行 没时间了,我需要得到东西 done.Any 帮助将不胜感激。 提前致谢。

您误用了 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) ).

没有括号,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]

还要注意Couple没用...