
- Prolog - Home
- Prolog - Introduction
- Prolog - Environment Setup
- Prolog - Hello World
- Prolog - Basics
- Prolog - Relations
- Prolog - Data Objects
- Prolog - Operators
- Loop & Decision Making
- Conjunctions & Disjunctions
- Prolog - Lists
- Recursion and Structures
- Prolog - Backtracking
- Prolog - Different and Not
- Prolog - Inputs and Outputs
- Prolog - Built-In Predicates
- Tree Data Structure (Case Study)
- Prolog - Examples
- Prolog - Basic Programs
- Prolog - Examples of Cuts
- Towers of Hanoi Problem
- Prolog - Linked Lists
- Monkey and Banana Problem
- Prolog Useful Resources
- Prolog - Quick Guide
- Prolog - Useful Resources
- Prolog - Discussion
Prolog - Conjunctions & Disjunctions
In this chapter, we shall discuss Conjunction and Disjunction properties. These properties are used in other programming languages using AND and OR logics. Prolog also uses the same logic in its syntax.
Conjunction
Conjunction (AND logic) can be implemented using the comma (,) operator. So two predicates separated by comma are joined with AND statement. Suppose we have a predicate, parent(jhon, bob), which means âJhon is parent of Bobâ, and another predicate, male(jhon), which means âJhon is maleâ. So we can make another predicate that father(jhon,bob), which means âJhon is father of Bobâ. We can define predicate father, when he is parent AND he is male.
Disjunction
Disjunction (OR logic) can be implemented using the semi-colon (;) operator. So two predicates separated by semi-colon are joined with OR statement. Suppose we have a predicate, father(jhon, bob). This tells that âJhon is father of Bobâ, and another predicate, mother(lili,bob), this tells that âlili is mother of bobâ. If we create another predicate as child(), this will be true when father(jhon, bob) is true OR mother(lili,bob) is true.
Program
parent(jhon,bob).parent(lili,bob).male(jhon).female(lili).% Conjunction Logicfather(X,Y) :- parent(X,Y),male(X).mother(X,Y) :- parent(X,Y),female(X).% Disjunction Logicchild_of(X,Y) :- father(X,Y);mother(X,Y).
Output
| ?- [conj_disj].compiling D:/TP Prolog/Sample_Codes/conj_disj.pl for byte code...D:/TP Prolog/Sample_Codes/conj_disj.pl compiled, 11 lines read - 1513 bytes written, 24 msyes| ?- father(jhon,bob).yes| ?- child_of(jhon,bob).true ?yes| ?- child_of(lili,bob).yes| ?-