projog

3.51. copy_term(X,Y) - makes a copy of a term.

copy_term(X,Y) makes a copy of X and attempts to unify it with Y. Any variables in term X will be replaced with new variables in the copied version of the term.

Examples

?- copy_term(X, Y), X \== Y.
X = UNINSTANTIATED VARIABLE
Y = UNINSTANTIATED VARIABLE

yes

?- copy_term(X, Y), X=a, Y=b.
X = a
Y = b

yes

?- X=a, copy_term(X, Y).
X = a
Y = a

yes

?- X=p(A,B,p(C)), copy_term(X, Y), A=1, B=2, C=3.
A = 1
B = 2
C = 3
X = p(1, 2, p(3))
Y = p(A, B, p(C))

yes

?- X=p(A,B,p(3)), copy_term(X, Y), Y=p(1,2,p(C)).
A = UNINSTANTIATED VARIABLE
B = UNINSTANTIATED VARIABLE
C = 3
X = p(A, B, p(3))
Y = p(1, 2, p(3))

yes

?- X=[A,B,C], copy_term(X, Y), A=1, B=2, C=3.
A = 1
B = 2
C = 3
X = [1,2,3]
Y = [A,B,C]

yes

?- copy_term(a, a).

yes

?- copy_term(a, b).

no

?- copy_term(p(1,2,p(3)), p(1,2,p(3))).

yes

?- copy_term(p(1,2,p(3)), p(1,2,p(4))).

no

?- X=p(A,B,3), copy_term(X, p(1,E,F)), B=b, E=e.
A = UNINSTANTIATED VARIABLE
B = b
E = e
F = 3
X = p(A, b, 3)

yes