__Conjunctions in Prolog__

In this tutorial, we will discuss Conjunctions in logic programming, and how they are used in Prolog.

To start with, lets present the definition of a Conjunction. A

between the two terms will result in the whole expression to evaluate to

evaluate to false, the whole expression evaluates to false. (The word "conjunction" is used mainly in the context of logic and logic programming, it

is equivalent to an "AND" in Java, C++, etc..)

To start with, lets present the definition of a Conjunction. A

*Conjunction*is a logical operator that connects two logical terms. A conjunctionbetween the two terms will result in the whole expression to evaluate to

*true*if both terms evaluate to true. If either or both terms in the expressionevaluate to false, the whole expression evaluates to false. (The word "conjunction" is used mainly in the context of logic and logic programming, it

is equivalent to an "AND" in Java, C++, etc..)

__Conjunction - Example 1 (conjunction within a rule body)__

In Prolog, we are allowed to write rule bodies that contain conjunctions. Remember when we said that a rule head is true if the body is true;

if we use a conjunction in the body, the head of the rule is considered true, if and only if

example:

if we use a conjunction in the body, the head of the rule is considered true, if and only if

*all*terms in the body are also true. Lets consider the followingexample:

pet(maggie). pet(bounty). pet(ginger). fast(maggie). fast(ginger). lazy(bounty). big(maggie). big(ginger). friendly(ginger). cat(X):- pet(X), lazy(X). dog(X):- pet(X), fast(X), big(X), friendly(X).

The example above contains 2 rules. The first describes a cat, and the second describes a dog. (Regardless of how true the following statements are)

The first rule says that X is a cat, if X is a pet

says that X is a dog, if X is a pet

Note that if any of the four conditions fail, the whole condition fails, and hence dog(X) fails.

The first rule says that X is a cat, if X is a pet

*and*X is lazy. A conjunction in Prolog is represented by the comma symbol ' , '. The second rulesays that X is a dog, if X is a pet

*and*X is fast*and*X is big*and*X is friendly, which shows that we can use conjunctions with any number of terms.Note that if any of the four conditions fail, the whole condition fails, and hence dog(X) fails.

?- cat(X). X = bounty ; false. ?- dog(ginger). true. ?- dog(maggie). false.

The first query, "cat(X)" lets prolog attempt to unify X with something that satisfies both "pet(X)" and "lazy(X)". Since "bounty" is the only constant

that satisfies both conditions, Prolog replies with the answer "X = bounty". The second query asks the knowledge base if "ginger" is a dog.

Since "ginger" satisfies all 4 conditions of dog(X), Prolog replies with

the conditions "pet(X)", "fast(X)" and "big(X)", it doesn't satisfy "friendly(X)", since there is no information in the knowledge base to indicate that, hence

the whole expression fails, and Prolog replies with

One final note about conjunctions within rules,

smart(X),dog(X) :- pet(X), ............ % WRONG

that satisfies both conditions, Prolog replies with the answer "X = bounty". The second query asks the knowledge base if "ginger" is a dog.

Since "ginger" satisfies all 4 conditions of dog(X), Prolog replies with

*true.*The last query asks if "maggie" is a dog. Even though maggie satisfiesthe conditions "pet(X)", "fast(X)" and "big(X)", it doesn't satisfy "friendly(X)", since there is no information in the knowledge base to indicate that, hence

the whole expression fails, and Prolog replies with

*false.*One final note about conjunctions within rules,

__conjunctions can only occur at the__**body**of the rule. No conjunctions are allowed at the__For example, we cannot have a rule as follows:__*head of the rule*.smart(X),dog(X) :- pet(X), ............ % WRONG

__Conjunction - Example 2 (conjunction within a query)__

Conjunctions are not just used within rule bodies, they are also used within queries. For example, we can ask Prolog to find both a Cat and a Dog

from the knowledge base at the same time.

from the knowledge base at the same time.

?- dog(X),cat(Y). X = ginger, Y = bounty .

In the above query, we asked Prolog to unify X with a variable such that dog(X) is true

The knowledge base was successful, hence, it returned "X = ginger, Y = bounty". We can also ask for certain facts in conjunction, for example:

*and*to unify Y with a constant such that cat(Y) is true.The knowledge base was successful, hence, it returned "X = ginger, Y = bounty". We can also ask for certain facts in conjunction, for example:

?- big(maggie),big(ginger). true. ?- big(maggie),lazy(ginger). false. ?- fast(maggie), pet(bounty), cat(X). X = bounty .

For the first query, we asked the knowledge base if "maggie is big

*and*ginger is big". Since both conditions are true, we received

*true.*

For the second query, we asked if "maggie is big

*and*ginger is lazy". Since there is no information in the knowledge base that says ginger is lazy,

Prolog replies with

*false.*

**The last query asks if "maggie is fast**

*and*bounty is a bet

*and*if there exists a constant in the knowledge base that could

unify with X such that cat(X) is true". Since all three conditions are true, Prolog replies with "X = bounty.". If one of the conditions were false, we

X would not be assigned any value, and we would get a

*false*right away.