Note that the results of this predicate are independent from the actual sharing and cycles that appear in the term. For example:
?- term_factorized(a(x(1),x(1)), T, S). T = a(_G1475, _G1475), S = [_G1475=x(1)].
Factorized cycles are guaranteed to have the minimal cycle length. For example:
?- A = [1,2,1,2|A], term_factorized(A, T, S). A = [1, 2, 1, 2|A], S = [T=[1, 2|T]].
The complexity of this predicate is O(N*log(N)
), where N is the size of the term.