From: john D. <j0h...@gm...> - 2008-04-01 16:13:40
|
I'm trying to use global variables with backtracking to capture and print the steps of the derivation. The example program at the end of this message demonstrates the issue I'm having. If I load the program in SWI Prolog, then the output is as follows. [SWI transcript] ?- init. Yes ?- happy(bob). Yes ?- output. unhappy(dan) => happy(bob) Yes During execution, step(happy(alice, carol)) is also stored in the global variable, but it is removed during backtracking, as per the semantics of the SWI-Prolog b_setval/2 function. Now, I try to use the storage module to accomplish the same effect in XSB Prolog. In the documentation, it appears to have the same backtracking/nonbacktracking options, but the backtracking isn't working the same way. I define compatibility functions to map the SWI getval/setval functions to XSB storage, but I get the following transcript, which indicates that the irrelevant step(happy(alice,carol)) was not removed during backtracking. Is this a bug in the module implementation or a flaw in my expectation, and if it's the latter, how I can I accomplish what I want? [XSB transcript] | ?- init. yes | ?- happy(bob). yes | ?- output. happy(alice) => happy(carol) unhappy(dan) => happy(bob) yes ------------------------- % for XSB-Prolog only :- import storage_delete_keypair/3, storage_insert_keypair/4, storage_delete_keypair_bt/3, storage_insert_keypair_bt/4, storage_find_keypair/3 from storage. nb_setval(K, V) :- storage_delete_keypair(global, K, _), storage_insert_keypair(global, K, V, _). b_setval(K, V) :- storage_delete_keypair_bt(global, K, _), storage_insert_keypair_bt(global, K, V, _). nb_getval(K, V) :- storage_find_keypair(global, K, V). b_getval(K, V) :- storage_find_keypair(global, K, V). % end of XSB-Prolog functions happy(alice). friend(carol, alice). enemy(bob, carol). enemy(bob, dan). happy(X) :- friend(X,Y), happy(Y), b_getval(out, R), b_setval(out, [step(happy(Y),happy(X)) | R]), b_getval(out, Q), nb_setval(out2, Q). happy(X) :- enemy(X,Y), \+(happy(Y)), b_getval(out, R), b_setval(out, [step(unhappy(Y),happy(X)) | R]), b_getval(out, Q), nb_setval(out2, Q). init :- nb_setval(out, []). output([]). output([step(happy(Y),happy(X)) | R]) :- output(R), write(happy(Y)), write(' => '), writeln(happy(X)). output([step(unhappy(Y),happy(X)) | R]) :- output(R), write(unhappy(Y)), write(' => '), writeln(happy(X)). output :- nb_getval(out2, R), output(R). |