Code Comments
Programming Forum and web based access to our favorite programming groups.I'm trying to write a program that prints formatted text for the
various steps in a derivation. The output of the program is used as
the input of another (non-Prolog) program that uses the derivation for
other purposes.
Here's a simplified example:
-------------------
happy(alice).
friend(carol, alice).
enemy(bob, carol).
enemy(bob, dan).
happy(X) :- friend(X,Y), happy(Y),
write(happy(Y)), write(' => '), writeln(X).
happy(X) :- enemy(X,Y), not(happy(Y)), enemy(X,Y),
write(unhappy(Y)), write(' => '), writeln(X).
-------------------
If I query:
?- happy(bob).
it outputs:
happy(alice) => carol
unhappy(dan) => bob
This is correct, but what I really want is only the line
"unhappy(dan)=>bob" because "happy(alice) => carol" was ultimately
useless in the derivation. Basically, I want to suppress all the
output that eventually gets backtracked over just leaving the steps of
the final derivation.
Post Follow-up to this messageI've made some progress. I can solve my toy example problem in SWI-
prolog using their global variable extensions, as listed below.
My real program however is using XSB prolog because I need tabling,
and so far I haven't found an equivalent solution in that system.
---------------
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), fail_if(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).
Post Follow-up to this messagePowered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.