user5616531
user5616531

Reputation: 59

How to print variable value from a question?

I´m making a one bit addition:

sumbit(CIN,A,B,CO,R):- ... ?- sumbit(0 ,1 ,1 ,CO ,R) ,write(CIN),nl ,write(A),nl ,write("+"),nl ,write(B),nl ,write("--"),nl ,write(CO),write(R),nl.

What I want to do is to print the variable values of CIN,A,B,CO and R. It should come out something like this:

0
1
+
1
--
10

Instead it comes out as this:

_40
_73
+
_149
--
10
Yes.

Also is there a way to not print the "Yes"? I´m using strawberry prolog if it helps. Thank you in advance

Upvotes: 3

Views: 1718

Answers (2)

Will Ness
Will Ness

Reputation: 71065

One way to achieve that without altering your predicate definition is to tweak the query, like so:

?- [CIN, A, B] = [0, 1, 1]
    ,sumbit(CIN
        ,A
        ,B
        ,CO
        ,R)
    ,write(CIN),nl
    ,write(A),nl
    ,write("+"),nl
    ,write(B),nl
    ,write("--"),nl
    ,write(CO),write(R),nl.

Now all variables are instantiated, either by the call itself, or prior to the call.

When a variable is not instantiated, there's no value to print, so its "name" is printed instead. But since non-used name has no meaning in itself, it can be freely renamed by the system to anything. In SWI Prolog:

1 ?- write(A).
_G1338
true.

The renaming is usually done, as part of the Prolog problem solving process, to ensure that any two separate invocations of the same predicate do not interfere with each other.

So where SWI Prolog uses names like _G1338, the Prolog implementation you're using evidently uses names with the numbers only, after the underscore, like _40.

Upvotes: 4

user5616531
user5616531

Reputation: 59

I found an answer by putting the write() inside the sumbit(...) predicate:

sumbit(CIN,A,B,CO,R):-
    xor_(A,B,R1)
    ,and(A,B,R2)
    ,xor_(R1,CIN,R)
    ,and(R1,CIN,R4)
    ,or(R2,R4,CO)
    ,write(CIN),nl
    ,write(A),nl
    ,write("+"),nl
    ,write(B),nl
    ,write("--"),nl
    ,write(R),nl.

There are still some unanswered questions though:

is there a way to not print the "Yes"?

what was the _number that came out before?

Upvotes: 0

Related Questions