Prologue and / 2, or / 2, nand / 2, nor / 2, xor / 2

I want to implement the following predicates in the 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 / 2, for example, so that I can do the rest myself and post them here.

+4
source share
2 answers

Beware: you probably mean and/3instead and/2. And it is a triple predicate that defines the relationship between truth values 3 , not 2. Of course, instead of reifying (= making things explicit), you can use the built-in Prolog mechanism, where the truth value is implicit. But for starters, I would start with a triple relationship, since this makes all the truth values ​​clear and also allows you to ask, for example: "What truth values ​​give falsefor this operation?" To get started, one entry in the truth table and/3, where I use the atom trueto denote the logical value true:

and(true, true, true).

, , , , SICStus Prolog GNU Prolog .

library(clpb) SICStus Prolog:

| ?- sat(X*Y =:= T), X = 0.
X = 0,
T = 0 ?

, , . , , , CLP (B) SICStus Prolog, , , taut/2

| ?- taut(A*B =:= B*A, T).
T = 1 ?

, library(clpb):

| ?- sat(a*b =:= b*a).
yes

, .

+3

/2 .

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).

A/B true/false. :

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

All Articles