
- Prolog - Home
- Prolog - Introduction
- Prolog - Environment Setup
- Prolog - Hello World
- Prolog - Basics
- Prolog - Relations
- Prolog - Data Objects
- Loop & Decision Making
- Conjunctions & Disjunctions
Prolog Operators
- Prolog - Type of Operators
- Prolog - Arithmetic Comparison Operators
- Prolog - Unification Operators
- Prolog - Term Comparision Operators
- Prolog - Arithmetic Operators
- Prolog - Logical Operators
- Prolog - List Operators
- Prolog - Custom Operators
Prolog Lists
- Prolog - Lists
- Prolog - Member of List
- Prolog - Length of List
- Prolog - Concatenating Lists
- Prolog - Appending to a List
- Prolog - Deleting from a List
- Prolog - Inserting into a List
- Prolog - Permutation Operation
- Prolog - Combination Operation
- Prolog - Reverse Items of a List
- Prolog - Shift Items of a List
- Prolog - Check Order of a List
- Prolog - SubSet of a Set
- Prolog - Union of Sets
- Prolog - Intersection of Sets
- Prolog - Even and Odd Length Finding
- Prolog - Divide a List
- Prolog - Find Maximum of a List
- Prolog - Find Minimum of a List
- Prolog - Find Sum of a List
- Prolog - Sorting List using MergeSort
Built-In Predicates
- Prolog - Built-In Predicates
- Prolog - Identifying Terms
- Prolog - Decomposing Structures
- Prolog - Collecting All
- Prolog - Mathematical Predicates
- Prolog - Scientific Predicates
Miscellaneous
- Recursion and Structures
- Prolog - Backtracking
- Prolog - Preventing Backtracking
- Prolog - Different and Not
- Prolog - Inputs and Outputs
- Tree Data Structure (Case Study)
- Prolog - Examples
- Prolog - Basic Programs
- Prolog - Practical Arithmetic Examples
- 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 - Logical Operators
Logical Operators are for various logical operations in Prolog. Logical operators are used to create complex queries or rules. Logical operator determines the flow of expression as well. Following is the list of logical operators −
Operator | Meaning |
---|---|
, | Conjunction, represents "and" |
; | Disjunction, represents "or" |
:- | Implication, represents "if" or "is defined as" |
\+ | Represents negation as failure. |
Conjunction , (Comma)
The comma , acts as logical AND operator in Prolog.
Syntax
Goal1, Goal2, Goal3
Above syntax can be interpreted as Goal1 AND Goal2 AND Goal3 where each goal is to be satisfied for entire conjuction to succeed.
Example (family.pl)
parent(sudip, piyush). parent(sudip, raj). male(piyush). male(raj). brother(X,Y) :- parent(Z,X), parent(Z,Y),male(X), male(Y)
Output
| ?- consult('D:/TP Prolog/Sample Codes/family.pl'). compiling D:/TP Prolog/Sample Codes/family.pl for byte code... D:/TP Prolog/Sample Codes/family.pl compiled, 4 lines read - 1002 bytes written, 13 ms yes | ?- brother(piyush,raj). true ? yes | ?- brother(piyush,sudip). no | ?-
Disjunction ; (Semicolon)
The semicolon ; acts as logical OR operator in Prolog.
Syntax
Goal1; Goal2; Goal3
Above syntax can be interpreted as Goal1 OR Goal2 OR Goal3 where at least one of the goal is to be satisfied for entire clause to succeed.
Example (family.pl)
parent(sudip, piyush). parent(sudip, raj). parent(susan, raj). parent(susan, piyush).
Output
| ?- parent(sudip,X); parent(susan, X). X = piyush ? yes | ?- parent(sudip,X); parent(susan, X). X = piyush ? a X = raj X = raj X = piyush (15 ms) yes | ?-
Here we're having clause, where X is to be determined whose parent is either sudip or susan.
Implication, :-
The implication :- is used to define rules. It means "if" or "is defined as".
Syntax
Head :- Body
Above syntax can be interpreted as Goal1 AND Goal2 AND Goal3 where each goal is to be satisfied for entire conjuction to succeed.
Example (family.pl)
parent(sudip, piyush). parent(sudip, raj). male(piyush). male(raj). brother(X,Y) :- parent(Z,X), parent(Z,Y),male(X), male(Y)
Here brother(X,Y) is true if parent of X and Y is same and both are males.
Output
| ?- consult('D:/TP Prolog/Sample Codes/family.pl'). compiling D:/TP Prolog/Sample Codes/family.pl for byte code... D:/TP Prolog/Sample Codes/family.pl compiled, 4 lines read - 1002 bytes written, 13 ms yes | ?- brother(piyush,raj). true ? yes | ?- brother(piyush,sudip). no | ?-
Negation as Failure, \+
The Negation operator \+ represents negation as failure. A goal \+ Goal succeeds if Goal cannot be proven as true. In case Goal is proven true, negation fails.
Syntax
\+ Goal
It represents NOT. If Goal can be proven true than it fails else succeeds.
Example (family.pl)
male(piyush). male(raj).
As there is no entry for susan, if we try to check susan for male, it will return false and negation will return true.
Output
| ?- consult('D:/TP Prolog/Sample Codes/family.pl'). compiling D:/TP Prolog/Sample Codes/family.pl for byte code... D:/TP Prolog/Sample Codes/family.pl compiled, 1 lines read - 305 bytes written, 3 ms yes | ?- male(susan). no | ?- \+ male(susan). yes | ?-