Saturday, 15 May 2010

Prolog keeps value when backtracking -



Prolog keeps value when backtracking -

hello i've problem homework question. problem concerns prolog backtracking feature, interpreted swipl.

as i've understood backtracking's purpose generate answers when asked. if there no possible solutions left homecoming false. if want lock, or commit, execution path can utilize cut.

my problem follows:the programme provide 1 right solution (out of 3 (ada,beda,calle)) when asking more solutions programme fails. when using trace programme appears have committed variable succeeded lastly time, fail since provides same answer.

when tested separately inputs result in true

what hope achieve: when asking solution seek other x:s , not committed first one. if code or database provide clarity i'll glad post it.

[trace] 4 ?- spider(x). call: (6) spider(_g4246) ? creep call: (7) persons_generate(_g4317) ? creep call: (8) bagof(_g4310, person(_g4310), _g4321) ? creep %^ call: (14) person(_g4310) ? creep exit: (14) person(ada) ? creep redo: (14) person(_g4310) ? creep exit: (14) person(beda) ? creep redo: (14) person(_g4310) ? creep exit: (14) person(calle) ? creep exit: (8) bagof(_g4310, user:person(_g4310), [ada, beda, calle]) ? creep%^ exit: (7) persons_generate([ada, beda, calle]) ? creep call: (7) lists:delete([ada, beda, calle], _g4246, _g4353) ? creep exit: (7) lists:delete([ada, beda, calle], _g4246, []) ? creep call: (7) conspirator(_g4246, [], [], [], _g4355, _g4356) ? creep exit: (7) conspirator(_g4246, [], [], [], [], []) ? creep call: (7) []==[] ? creep exit: (7) []==[] ? creep exit: (6) spider(_g4246) ? creep true ; redo: (7) conspirator(_g4246, [], [], [], _g4355, _g4356) ? creep fail: (7) conspirator(_g4246, [], [], [], _g4355, _g4356) ? creep fail: (6) spider(_g4246) ? creep false.

edit

%db person(ada). person(beda). person(calle). knows(ada,beda). knows(ada,calle). knows(beda,calle).

code

persons_generate(persons):- bagof(x,person(x),persons). spider(x):- persons_generate(persons), delete(persons,x,personsnospider), conspirator(x, [],personsnospider,[],_,listreturn), %when done listreturn should empty if have connections listreturn == []. conspirator(_,[],[],conslist,conslist,[]). conspirator(possiblespider,failedconspiratorlist,[possibleconspirator|list1],conspiratorlistin,conspiratorlistout,listreturn):- %conspiratorlist known conspirators friend(possibleconspirator,possiblespider), not(friend_with_list(possibleconspirator,conspiratorlistin)), friendlist(possibleconspirator,conspiratorsfriends), subtract(list1,conspiratorsfriends,listcut), subtract(failedconspiratorlist,conspiratorsfriends,newfailedconspiratorlist), conspirator(possiblespider,newfailedconspiratorlist,listcut, [possibleconspirator|conspiratorlistin],conspiratorlistout,listreturn). conspirator(possiblespider,failedconspirators, [notpossibleconspirator|list1], conspiratorlistin,conspiratorlistout,listreturn) :- %increase notconspirator list not(friend(possiblespider,notpossibleconspirator)), conspirator(possiblespider,[notpossibleconspirator|failedconspirators], list1, conspiratorlistin, conspiratorlistout, listreturn). %checks both ways friend(x,y):- (knows(x,y);knows(y,x)).%this how or supposed work right? friendlist(x,friendlist):- bagof(y,friend(x,y), friendlist). friend_with_list(_,[]):-%searching friend in empty world hard fail. %returns true if input knows b in list c friend_with_list(x,[y|_]):- friend(x,y). %x don't know head of list, let's seek again! friend_with_list(x,[_|tail]):- friend_with_list(x,tail).

explanation

so purpose of programme find spider in web. spider has contact conspirators of 1 can not have contact conspirator. if spider successful, i.e. homecoming true: conspirators , friends persons combined. i.e. no person should outside net.

prolog backtracking

No comments:

Post a Comment