Prolog Operators

Prolog Lists

Built-In Predicates

Miscellaneous

Prolog - Decomposing Structures Predicates



Decompsing Structures Predicates are built-in predicates which 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.

The functor(T,F,N) Predicate

This returns true if F is the principal functor of T, and N is the arity of F.

Note − Arity means the number of attributes.

Example

| ?- functor(t(f(X),a,T),Func,N).

Func = t
N = 3

yes
| ?-

The arg(N,Term,A) Predicate

This returns true if A is the Nth argument in Term. Otherwise returns false.

Example

| ?- arg(1,t(t(X),[]),A).

A = t(X)

yes
| ?- arg(2,t(t(X),[]),A).

A = []

yes
| ?- 

Now, let us see another example. In this example, we are checking that the first argument of D will be 12, the second argument will be apr and the third argument will be 2020.

Example

| ?- functor(D,date,3), arg(1,D,12), arg(2,D,apr), arg(3,D,2020).

D = date(12,apr,2020)

yes
| ?- 

The ../2 Predicate

This is another predicate represented as double dot (..). This takes 2 arguments, so /2 is written. So Term = .. L, this is true if L is a list that contains the functor of Term, followed by its arguments.

Example

| ?- f(a,b) =.. L.

L = [f,a,b]

yes
| ?- T =.. [is_blue,sam,today].

T = is_blue(sam,today)

yes
| ?- 

By representing the component of a structure as a list, they can be recursively processed without knowing the functor name. Let us see another example −

Example

| ?- f(2,3)=..[F,N|Y], N1 is N*3, L=..[F,N1|Y].

F = f
L = f(6,3)
N = 2
N1 = 6
Y = [3]

yes
| ?- 
Advertisements