Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

printing the steps of a derivation
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.

Report this thread to moderator Post Follow-up to this message
Old Post
j0hndoe.12435@gmail.com
03-30-08 09:59 AM


Re: printing the steps of a derivation
I'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).


Report this thread to moderator Post Follow-up to this message
Old Post
j0hndoe
03-31-08 02:04 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

Prolog archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 06:11 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.