Prolog Operators

Prolog Lists

Built-In Predicates

Miscellaneous

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
| ?-  
Advertisements