Prolog Operators

Prolog Lists

Built-In Predicates

Miscellaneous

Prolog - Built-In Predicates



In Prolog, we have seen the user defined predicates in most of the cases, but there are many built-in-predicates as well. There are mainly following types of built-in predicates as given below −

  • Identifying terms

  • Decomposing structures

  • Collecting all solutions

  • Mathematical

  • Scientific

Identifying Terms Predicates Group

So this is the list of some 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.

Decomposing Structures

Now we will see, another group of built-in predicates, that is Decomposing structures. We have seen the identifying terms before. So when we are using compound structures we cannot use a variable to check or make a functor. It will return error. So functor name cannot be represented by a variable.

Error

| ?- X = tree, Y = X(maple).
uncaught exception: error(syntax_error('user_input:6 (char:16) . or operator expected after expression'),read_term/3)
| ?- 

Decomposing structure predicates are used to check Functors of terms. Following is the list of important decomposing structures predictates.

Predicate Description
functor(T,F,N) returns true if F is the principal functor of T, and N is the arity of F.
arg(N,Term,A) returns true if A is the Nth argument in Term. Otherwise returns false.
..L returns true if L is a list that contains the functor of Term, followed by its arguments.

Collecting All Solutions

Now let us see the third category called the collecting all solutions, that falls under built-in predicates in Prolog.

We have seen that to generate all of the given solutions of a given goal using the semicolon in the prompt. So here is an example of it.

Example

| ?- member(X, [1,2,3,4]).

X = 1 ? ;

X = 2 ? ;

X = 3 ? ;

X = 4

(16 ms) yes
| ?- 

Sometimes, we need to generate all of the solutions to some goal within a program in some AI related applications. So there are three built-in predicates that will help us to get the results. These predicates are as follows −

Predicate Description
findall/3 This predicate is used to make a list of all solutions X, from the predicate P.
setoff/3 Similar to findall/3, but it removes all of the duplicate outputs, and the returns the sorted answers.
bagof/3 Similar to setoff/3, but duplicates are retained, and the answers may not be sorted.

These three predicates take three arguments, so we have written /3 after the name of the predicates.

These are also known as meta-predicates. These are used to manipulate Prolog's Proof strategy.

Mathematical Predicates

Following are the mathematical predicates −

Predicates Description
random(L,H,X). Get random value between L and H
between(L,H,X). Get all values between L and H
succ(X,Y). Add 1 and assign it to X
abs(X). Get absolute value of X
max(X,Y). Get largest value between X and Y
min(X,Y). Get smallest value between X and Y
round(X). Round a value near to X
truncate(X). Convert float to integer, delete the fractional part
loor(X). Round down
ceiling(X). Round up
sqrt(X). Square root

Scientific Predicates

Following are the scientific predicates −

Predicates Description
sin(X). Sine of X where X is in radians.
cos(X). Cosine of X where X is in radians.
tan(X). Tangent of X where X is in radians.
asin(X). Arcsine of X where X is in radians.
acos(X). Arccosine of X where X is in radians.
atan(X) Arctangent of X where X is in radians.
sinh(X) Hyperbolic Sine of X where X is in radians.
cosh(X). Hyperbolic Cosine of X where X is in radians.
tanh(X) Hyperbolic Tangent of X where X is in radians.
asinh(X). Hyperbolic Arcsine of X where X is in radians.
acosh(X) Hyperbolic Arccosine of X where X is in radians.
atanh(X) Hyperbolic Arctangent of X where X is in radians.
log(X) Log of X
log10(X) Log10 of X
exp(X) Exponential of X
Advertisements