Send Close Add comments: (status displays here)
Got it!  This site uses cookies. You consent to this by clicking on "Got it!" or by continuing to use this website.nbsp; Note: This appears on each machine/browser from which this site is accessed.
Logical expressions using Prolog


1. Logical expressions using Prolog

2. True and false
True and false can be represented as Prolog facts using the b predicate.
b(0). b(1).

Note that facts are assumed to be true. Realize, though, that Prolog has no idea what the facts for b really mean.

3. Enumerate the values
The following Prolog program finds values for X and Y that will succeed, and then backtracks to find successive values until no more values can be found.

Here is the Prolog code.

Here is the output of the Prolog code.


4. Logical negation
The logical negation operator not can be expressed as the Prolog predicate not1 as the following facts.
not1(0,1). not1(1,0).

Note that not1 is used since not is a reserved word in Prolog, as is and and or.

5. Logical conjunction
The logical conjunction operator and can be expressed as the Prolog predicate and2 as the following facts.
and2(0,0,0). and2(0,1,0). and2(1,0,0). and2(1,1,1).


6. Logical disjunction
The logical disjunction operator or can be expressed as the Prolog predicate or2 as the following facts.
or2(0,0,0). or2(0,1,1). or2(1,0,1). or2(1,1,1).


7. Logical equivalence
Expression tree for X = YLet us start with the logical expression and extended truth table for logical equivalence.
X Y | X = Y ----------- 0 0 | 0 1 0 0 1 | 0 0 1 1 0 | 1 0 0 1 1 | 1 1 1


8. Logical equivalence
The equivalence operator eqv (equals) can be expressed as the Prolog predicate eqv2 as follows.
eqv2(0,0,1). eqv2(0,1,0). eqv2(1,0,0). eqv2(1,1,1).


9. Prolog query
Here is the Prolog code.

Here is the output of the Prolog code.

Note the following.

10. Logical expression
Expression tree for (X & Y) | ((! X) & (! Y))Let us use the running example of a logical expression. Here is the expression and extended truth table.
X Y | ( X & Y ) | ( ( ! X ) & ( ! Y ) ) --------------------------------------- 0 0 | ( 0 0 0 ) 1 ( ( 1 0 ) 1 ( 1 0 ) ) 0 1 | ( 0 0 1 ) 0 ( ( 1 0 ) 0 ( 0 1 ) ) 1 0 | ( 1 0 0 ) 0 ( ( 0 1 ) 0 ( 1 0 ) ) 1 1 | ( 1 1 1 ) 1 ( ( 0 1 ) 0 ( 0 1 ) )

Here is the Prolog code.

Here is the output of the Prolog code.


11. Declarative order
In an imperative (destructive assignment) programming language the order of the assignments to variables is important.

In a declarative programming logic programming system the order may impact efficiency (and issues due to the depth first search computation strategy) but the order is much less important.

Below, the order of the predicates are reversed (from the above). The result is the same. Here is the Prolog code.

Here is the output of the Prolog code.


12. Declarative order
Even the order of the values for X and Y can be changed, but note that this may change the order of X and Y in the resulting table. The calculated values are the same. Here is the Prolog code.

Here is the output of the Prolog code.


13. End of page