Prolog First Order Logic - Printing the truth table

I need to write a program that prints a truth table of expressions. So, I wrote the following function:

bool(true). bool(fail). tableBody(A,B,E) :- bool(A), bool(B) , write(A) , write(' '), write(B), write(' '), write(E),nl, fail. 

My problem is that E (an expression containing A and B) is not evaluated, but printed as is. For instance:

 296 ?- table(A,B,and(A,B)). AB expr(A,B) true true and(true, true) true fail and(true, fail) fail true and(fail, true) fail fail and(fail, fail) false. 

I am interested in writing an estimated value and(true, true) (" and(X,Y) " is the functor I defined earlier) instead of the one currently displayed. I was thinking of writing an eval functor, but wouldn't it have the same effect? How can i solve this?

I am using SWI-Prolog 5.8. Thanks.

+4
source share
2 answers

Here is one way to do this:

 and(A, B) :- A, B. evaluate(E, true) :- E, !. evaluate(_, false). bool(true). bool(false). tableBody(A,B,E) :- bool(A), bool(B), write(A), write(' \t '), write(B), write(' \t '), evaluate(E, Result), write(Result),nl, fail. 

It produces:

 ?- tableBody(A,B,and(A,B)). true true true true false false false true false false false false false. 
+6
source

As usual, one slot here

 ?- forall((member(A,[true,false]),member(B,[true,false]),(A,B->C=true;C=false)),format('~w|~w|~w~n',[A,B,C])). true|true|true true|false|false false|true|false false|false|false 
+3
source

All Articles