Term creation and decomposition

These predicates enable a term to be assembled from its component parts, or split into its component parts, or copied.

1. functor/3

functor(Term, Name, Arity) is true iff:

Templates and modes for the predicate are as follows:

functor(-nonvar, +atomic, +integer)
functor(+nonvar, ?atomic, ?integer)

1.1 Example tests

Let's start with some simple tests verifying success of failure of single goals.

Goal Theory success(String goal,String theory)
functor(foo(a, b, c), foo, 3). null true
functor([_|_], '.', 2). null true
functor([], [], 0). null true
functor(foo(a,b,c),X,Y). null true
functor(X,foo,3). null true
functor(X,foo,0). null true
functor(mats(A,B), A, B). null true
functor(1, X, Y). null true
functor(X, 1.1, 0). null true
Goal Theory success(String goal,String theory)
functor(foo(a), foo, 2). null false
functor(foo(a), fo, 1). null false
Goal Theory Variable Solution success(String goal,String theory,String variable,Strng solution)
functor(foo(a, b, c), X, Y). null X foo true
functor(foo(a, b, c), X, Y). null Y 3 true
functor(X, foo, 3). null X foo(_,_,_) true
functor(X, foo, 0). null X foo true
functor(mats(A, B), A, B). null A mats true
functor(mats(A, B), A, B). null B 2 true
functor(1, X, Y). null X 1 true
functor(1, X, Y). null Y 0 true
functor(X, 1.1, 0). null X 1.1 true

Tests With Exception

Goal Theory success(String goal) Type Of Error
functor(X, Y, 3). null true
functor(X, foo, N). null true
functor(X, foo, a). null true
functor(F, 1.5, 1). null true
functor(F, foo(a), 1). null true
current_prolog_flag(max_arity, A), X is A + 1, functor(T, foo, X). null true
Minus1 is 0 - 1, functor(F, foo, Minus1). null true

2. arg/3

arg(N, Term, Arg) is true iff the N th argument of Term is Arg .

Templates and modes for the predicate are as follows:

arg(+integer, +compound_term, ?term)

2.1 Example tests

Let's start with some simple tests verifying success of failure of single goals.

Goal Theory success(String goal,String theory)
arg(1, foo(a,b), a). null true
arg(1, foo(a,b), X). null true
arg(1, foo(a,b), X). null true
arg(1, foo(X,b), a). null true
arg(1, foo(X,b), Y). null true
Goal Theory success(String goal,String theory)
arg(1, foo(a, b), b). null false
arg(0, foo(a, b), foo). null false
arg(3, foo(3, 4), N). null false
arg(1, foo(X), u(X)). null false
Goal Theory Variable Solution success(String goal,String theory,String variable,Strng solution)
arg(1, foo(a, b), X). null X a true
arg(1, foo(X, b), a). null X a true
arg(1, foo(X, b), Y). null X X true

Tests With Exception

Goal Theory success(String goal) Type Of Error
arg(X, foo(a,b), a). null true
arg(1, X, a). null true
arg(0, atom, a). null true
arg(0, 3, a). null true

3. =../2 (univ)

'=..'(Term, List) is true iff:

Templates and modes for the predicate are as follows:

'=..'(+nonvar, ?list)
'=..'(-nonvar, +list)

Note that =.. is a predefined operator.

3.1 Example tests

Let's start with some simple tests verifying success of failure of single goals.

Goal Theory success(String goal,String theory)
'=..'(foo(a,b), [foo, a, b]). null true
'=..'(1, [1]). null true
Goal Theory success(String goal,String theory)
'=..'(foo(a,b), [foo, b, a]). null false
'=..'(f(X), [f, u(X)]). null false
Goal Theory Variable Solution success(String goal,String theory,String variable,Strng solution)
'=..'(X, [foo,a,b]). null X foo(a,b) true
'=..'(foo(a,b), L). null L [foo,a,b] true
'=..'(foo(X, b), [foo, a, Y]). null Y b true

Tests With Exception

Goal Theory success(String goal) Type Of Error
'=..'(X, Y). null true
'=..'(X, [foo, a | Y]). null true
'=..'(X, [foo | bar]). null true
'=..'(X, [Foo, bar]). null true
'=..'(X, [3, 1]). null true
'=..'(X, [1.1, foo]). null true
'=..'(X, [a(b), 1]). null true
'=..'(X, 4). null true

4. copy_term/2

copy_term(Term1, Term2) is true iff Term2 unifies with a term T which is a renamed copy of Term1 .

Templates and modes for the predicate are as follows:

copy_term(?term, ?term)

4.1 Example tests

Let's start with some simple tests verifying success of failure of single goals.

Goal Theory success(String goal,String theory)
copy_term(X, Y). null true
copy_term(X, 3). null true
copy_term(_, a). null true
copy_term(_, _). null true
Goal Theory success(String goal,String theory)
copy_term(a,b). null false
copy_term(a+X, X+b), copy_term(a+X, X+b). null false
copy_term(demoen(X, X), demoen(Y, f(Y))). null false
Goal Theory Variable Solution success(String goal,String theory,String variable,Strng solution)
copy_term(a+X, X+b). null X a true
copy_term(X+X+Y, A+B+B). null A A true