Dark44
Dark44

Reputation: 69

SWI Prolog stuck with odd behavior

So here is the thing.I am trying to make a sort of "perfect match" program in prolog. You give it a couple and each individual has some "characteristics".The program is supposed to return possible problems in future marriage depending on those characteristics.But when I try to do that and things get complicated I get this problem:

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 

It never stops anwsering wife is sensitive while husband is..Not matching.And it's also wrong.And here is the code:

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.

And this oddly enough works:

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.

code:

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.

So when it gets more complicated it does not work for some reason.. I really can't understand what's going on.I am only a couple of months familiar with prolog.I've been playing with this problem a few days myself with no luck.But now I've run out of time and I need to get things done.Any help will be appreciated. Thanks in advance.

Upvotes: 1

Views: 198

Answers (1)

CapelliC
CapelliC

Reputation: 60034

you are misusing the OR operator (;)/2, you probably meant

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

without the parens, W_sex and H_sex are unbound when calling marriage_sexual_problems/2. SWI-Prolog is able to signal the symptom:

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]

Also note that Couple is useless...

Upvotes: 1

Related Questions