2
votes

I am a novice at programming in prolog.

I want to change the value returned by a prolog program such that it returns true / false instead of the standard yes or no.

Consider a very simple example : E.g. simple.P

node(1).

isNode(X) : node(X)

on the prolog command line if I type isNode(1) it returns with yes like:

isNode(1).

yes

My question is :

How do i change this from yes to true?

1
Strictly speaking, that is not a part that you control with your program, but the user interface of the Prolog system you are using. Such interface, often called REPL (Read,Eval,Print,Loop), or more often console, it's the simpler way to allow a programmer to control the 'inner working' of Prolog. As a programmer, you should define your own interface to your program, thus answering with true/false or whatever you think is better to appropriate user input.CapelliC
This would depend on the Prolog dialect you are using. For instance, SWI Prolog sometimes does not report anything, sometimes it says "true".Alexander Serebrenik
@AlexanderSerebrenik: When does SWI not report anything? To my understanding it always produces an answer.false
@false: it produces an answer but it does not necessarily add "yes" or :true" once the answer has been shown.Alexander Serebrenik

1 Answers

1
votes

Prolog attempts to find a proof of your query. If your query has variables, it prints a value that makes them true.

Q: Are there any prime numbers that are even? A: Yes - 2 is even and prime

It'll keep giving you more proofs as long as you type ; Eventually it'll run out, and respond false.

Q: Are there any prime numbers that are even? A: Yes - 2 is even and prime Q: Are there any more? A: false.

What you want is for your program to perform output. There's a number of library predicates to do this. The most flexible is format/2

myprogram :-
   my_old_program, !,
   format('yup, that sure is right!~n', []).
myprogram :-
   format('nope, nope, no way in heck!~n', []).