Prolog implement and/2, or/2, nand/2, nor/2, xor/2

PROLOGik picture PROLOGik · Oct 28, 2013 · Viewed 9k times · Source

I want to implement the following predicates in prolog and use them for truth-tables: and/2, or/2, nand/2, nor/2, xor/2

Maybe someone can show me how to implement and/2 for example so I can do the others myself and post them here.

Answer

Cameron White picture Cameron White · Oct 31, 2013

/2 is possible and actually very elegent.

and(A,B) :- A,B.
or(A,B) :- A;B.
nand(A,B) :- not(and(A,B)).
nor(A,B) :- not(or(A,B)).
xor(A,B) :- or(A,B), nand(A,B).

To use just replace A/B with true/false. For example:

?- and(true,true).
true.
?- and(false, true).
false.