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.
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.
Beware: You probably mean
and/3
, instead ofand/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 yieldfalse
for a given operation?" To get you started, one entry for the truth table ofand/3
, where I use the atomtrue
to denote the Boolean value 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: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
or universally quantified variables, denoted as atoms in
library(clpb)
:Thus, Boolean constraints can be very useful tools when working with binary values.