Question

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.

Était-ce utile?

La solution 2

Beware: You probably mean and/3, instead of and/2. AND is a ternary predicate, defining a relation between 3 truth values, not 2. Of course, instead of reifying (= making things explicit), you could use Prolog's built-in mechanism, where the truth value is implicit. But to begin, I would start with the ternary relation, since this makes all truth values explicit and lets you also ask for example: "Which truth values yield false for a given operation?" To get you started, one entry for the truth table of and/3, where I use the atom true to denote the Boolean value true:

and(true, true, true).

Independently, also consider using Boolean constraints, which are for example available in SICStus Prolog and GNU Prolog, and let you declaratively express relations between truth values and Boolean expressions.

Example using library(clpb) in SICStus Prolog:

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

This shows that if the first argument of a conjunction is false, then the whole conjunction is also false. In addition, we can for example use the CLP(B) solver of SICStus Prolog to determine that conjunction is commutative, using either taut/2

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

or universally quantified variables, denoted as atoms in library(clpb):

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

Thus, Boolean constraints can be very useful tools when working with binary values.

Autres conseils

/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.
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top