Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to turn off "true" and "false" outputs in Prolog?

I would like to write a small text-based adventure game using Prolog (this might be a dumb idea but I am not here to discuss that).

The only problem is that I can't manage to print text on screen without the "true" and "false" values to appear as well.

For instance if I try something like:

take(desk) :- write('This thing is way too heavy for me to carry!').

where take is a one place predicate and desk a name I get as an output:

?- take(desk).
   This thing is way too heavy for me to carry!
   true.

How can I get rid of this "true" or "false" outputs?

Just to mention that I also tried with the format/1 one place predicate for simple text output and also the format/2 two place predicate (when I want to output the name of a variable) but it gives exactly the same problem.

I have also seen this answer but first it is not detailed enough (at least not for someone like me) and second, I hope deep inside that there is a simpler manner to do it.

And finally, I am using SWI-Prolog.

Thank you.

like image 367
gatsu Avatar asked Jun 23 '14 14:06

gatsu


People also ask

How do I stop a query in Prolog?

If you want to exit SWI-Prolog, issue the command halt., or simply type CTRL-d at the SWI-Prolog prompt.

How do you negate a fact in Prolog?

not(X) is the way to implement negation in Prolog; however not(X) does not mean that X is false, it means that X can't be proven true.

What does #= mean in Prolog?

I looked this up on the SWI prolog website where they defined it as. The arithmetic expression X equals Y.

How do you use output in Prolog?

output in Prologwrite(X) which writes the term X to the current output stream (which means the window on your workstation unless you have done something fancy). print(X, …) which writes a variable number of arguments to the current output stream.


1 Answers

A simplistic method would be to create a little REPL (read, evaluate, print loop) of your own. Something like this:

game :-
    repeat,
    write('> '),
    read(X),
    call(X),
    fail.

This will just prompt and execute whatever you enter at the prompt. In conjunction with your take fact (and another I added for illustration):

take(desk) :- write('This thing is way too heavy for me to carry!'), nl.
take(chair) :- write('This is easier to carry.'), nl.

You would get:

?- game.
> take(desk).
This thing is way too heavy for me to carry!
> take(chair).
This is easier to carry.
>

You don't get the true or false because the game goal doesn't resolve until you exit the loop somehow. You could add checks for a quit or bye or whatever to exit the game loop. Ctrl-C or Ctrl-D can be used as well to abort the loop. You might need to add some other "features" to make it work to your needs or liking.

like image 145
lurker Avatar answered Sep 23 '22 23:09

lurker