Prolog Operators

Prolog Lists

Built-In Predicates

Miscellaneous

Prolog - Identifying Terms Predicates



Identifying Terms Predicates are built-in predicates which helps in checking the type of a variable. Following are some of the important predicates that are falls under the identifying terms group −

Predicate Description
var(X) succeeds if X is currently an un-instantiated variable.
novar(X) succeeds if X is not a variable, or already instantiated
atom(X) is true if X currently stands for an atom
number(X) is true if X currently stands for a number
integer(X) is true if X currently stands for an integer
float(X) is true if X currently stands for a real number.
atomic(X) is true if X currently stands for a number or an atom.
compound(X) is true if X currently stands for a structure.

The var(X) Predicate

When X is not initialized, then, it will show true, otherwise false. So let us see an example.

Example

| ?- var(X).

yes
| ?- X = 5, var(X).

no
| ?- var([X]).

no
| ?- 

The novar(X) Predicate

When X is not initialized, the, it will show false, otherwise true. So let us see an example.

Example

| ?- nonvar(X).

no
| ?- X = 5, nonvar(X).

X = 5

(16 ms) yes
| ?- nonvar([X]).

yes
| ?- 

The atom(X) Predicate

This will return true, when a non-variable term with 0 argument and a not numeric term is passed as X, otherwise false.

Example

| ?- atom(paul).

yes
| ?- X = paul,atom(X).

X = paul

yes
| ?- atom([]).

yes
| ?- atom([a,b]).

no
| ?- 

The number(X) Predicate

This will return true, X stands for any number, otherwise false.

Example

| ?- number(X).

no
| ?- X=5,number(X).

X = 5

yes
| ?- number(5.46).

yes
| ?- 

The integer(X) Predicate

This will return true, when X is a positive or negative integer value, otherwise false.

Example

| ?- integer(5).

yes
| ?- integer(5.46).

no
| ?-

The float(X) Predicate

This will return true, X is a floating point number, otherwise false.

Example

| ?- float(5).

no
| ?- float(5.46).

yes
| ?- 

The atomic(X) Predicate

We have atom(X), that is too specific, it returns false for numeric data, the atomic(X) is like atom(X) but it accepts number.

Example

| ?- atom(5).

no
| ?- atomic(5).

yes
| ?- 

The compound(X) Predicate

If atomic(X) fails, then the terms are either one non-instantiated variable (that can be tested with var(X)) or a compound term. Compound will be true when we pass some compound structure.

Example

| ?- compound([]).

no
| ?- compound([a]).

yes
| ?- compound(b(a)).

yes
| ?- 
Advertisements