__Disjunctions in Prolog__

In this tutorial, we will discuss Disjunctions and how they are used in Prolog through a couple of examples.

A disjunction in logic programming is equivalent to an

more terms connected with the disjunction is true. A disjunction in Prolog is represented by the semicolon sign ' ; '.

A disjunction in logic programming is equivalent to an

**OR***,*where an expression is considered*true*if, one ormore terms connected with the disjunction is true. A disjunction in Prolog is represented by the semicolon sign ' ; '.

__Disjunction - Example 1 (Disjunction within a rule body).__

We will begin by writing a Prolog program that describes the quality "lazy". We say that something is lazy if it is a cat

or if it is sleepy. We can describe "lazy" by the following program.

or if it is sleepy. We can describe "lazy" by the following program.

cat(bounty). person(jack). dog(ginger). sleepy(jack). sleepy(bounty). lazy(X):- cat(X) ; sleepy(X).

Here, the rule says that "lazy(X)" is true if "cat(X) is true

(Note that, similar to conjunctions, disjunctions in Prolog can also be between two or more terms.). Lets observe the following query.

*or*sleepy(X) is true", i.e., only one of the two conditions should be true for the head to be true.(Note that, similar to conjunctions, disjunctions in Prolog can also be between two or more terms.). Lets observe the following query.

?- lazy(bounty). true . ?- lazy(jack). true. ?- lazy(ginger). false.

Here, the first query "lazy(bounty)" is true, since the constant "bounty" satisfies both "cat(X)" and "sleepy(X)" , where X is unified with "bounty". The

second query is also true, since "jack" satisfies "sleepy(X)" when X is unified with "jack". Note that even though "jack" doesn't satisfy "cat(X)", the rule

is still true since we used a disjunction, and only one of the conditions in the body has to be true. The third query "lazy(ginger)" returns false since

both conditions "cat(X)" and "sleepy(X)" are false (and a disjunction needs at least one condition to be true).

second query is also true, since "jack" satisfies "sleepy(X)" when X is unified with "jack". Note that even though "jack" doesn't satisfy "cat(X)", the rule

is still true since we used a disjunction, and only one of the conditions in the body has to be true. The third query "lazy(ginger)" returns false since

both conditions "cat(X)" and "sleepy(X)" are false (and a disjunction needs at least one condition to be true).

__Disjunction - Example 2 (Disjunctions within a query)__

Just like conjunctions, in Prolog, a query is also allowed to contain disjunctions. In this case, only one of the statements in the query

that are connected with disjunctions need to be true, in order to receive a

that are connected with disjunctions need to be true, in order to receive a

*true*response. Take the following query for example:?- cat(bounty) ; dog(bounty). true . ?- cat(bounty) ; dog(bounty). true ; false.

The queries above are the same query but with different responses. The first query returns

hence, the query terminated. In the second case, we pressed " ; " to get other alternative responses. The query first responds with "true" since the first

condition is true, when we press " ; ", it tries to satisfy the second condition "dog(bounty)" and fails, hence, we receive the

*true*since "cat(bounty)" is true. In the first query, we pressed "enter",hence, the query terminated. In the second case, we pressed " ; " to get other alternative responses. The query first responds with "true" since the first

condition is true, when we press " ; ", it tries to satisfy the second condition "dog(bounty)" and fails, hence, we receive the

*false*response.?- dog(X) ; cat(Y) ; sleepy(Z). X = ginger ; Y = bounty ; Z = jack ; Z = bounty.

In the query above, we used disjunctions between the three terms. Since we used disjunctions, Prolog has to satisfy only one of the three terms, hence

when finding a solutions, we get a solutions for X individually, then after running out of solutions for X, Prolog tries to unify Y, and later Z. If we had used

a conjunction, Prolog would have tried to unify all 3 together, since all three are required to satisfy the query.

when finding a solutions, we get a solutions for X individually, then after running out of solutions for X, Prolog tries to unify Y, and later Z. If we had used

a conjunction, Prolog would have tried to unify all 3 together, since all three are required to satisfy the query.

?- dog(X) , cat(Y) , sleepy(Z). X = ginger, Y = bounty, Z = jack ; X = ginger, Y = Z, Z = bounty