Already this paper had it, that many economical
representations of directed graphs do exist:
The Transitive Reduction of a Directed Graph
Aho, Garey & Ullman - 1972
https://dl.acm.org/doi/abs/10.1137/0201008
In particular I had high hopes that term_factorized/3,
can be used for a top-level, so that instead of this here:
/* SWI-Prolog 9.3.25 */
?- X = f(f(X)), Y = f(f(f(Y))).
X = Y, Y = f(f(f(Y))).
?- X = f(f(f(X))), Y = f(f(Y)).
X = Y, Y = f(f(Y)).
We would get this here:
?- X = f(f(X)), Y = f(f(f(Y))).
X = Y, Y = f(Y).
?- X = f(f(f(X))), Y = f(f(Y)).
X = Y, Y = f(Y).
But then I noticed that term_factorized/3
does a little bit too much:
/* SWI-Prolog 9.3.25 */
?- X=a(X), Y=b(X,X), term_factorized(Y,T,L).
T = b(_A, _A),
L = [_A=a(_A)].
?- X=a(Z), Y=b(X,X), term_factorized(Y,T,L).
T = b(_A, _A),
L = [_A=a(Z)].
But a top-level probably only wants:
/* SWI-Prolog 9.3.25 */
?- X=a(X), Y=b(X,X).
X = a(X),
Y = b(X, X).
?- X=a(Z), Y=b(X,X).
X = a(Z),
Y = b(a(Z), a(Z)).
The problem is, term_factorized/3 also decomposes
acyclic parts of the given term, if there is some sharing.
So term_factorized/3 has probably other niche applications
than the answer substitution display I had in mind.