EDIT
I can’t reproduce what I reported below. Rather I have found the term_factorized/3 is robust for the stress test. I changed the number of hydra heads to is 2^10000 !!, and saw that it terminates without freezing, which is amazing.
% ?- time((hydra(10000, _X), hydra(10000, _Y, _Y),
% term_factorized(g(_X, _Y), _Skel, _Coa), length(_Coa, Len))),
% writeln(unifying),
% maplist(unify_pair, _Coa), writeln(unified), _Skel == g(_X, _Y),
% writeln(verified).
%@ % 2,680,468 inferences, 41.321 CPU in 55.699 seconds (74% CPU, 64869 Lips)
%@ unifying
%@ unified
%@ verified
%@ Len = 10000 .
END of edit
Is this a known bug on term_factorized/3, which freezes at the very exit of a stress test query ? If this is really a bug, it would be a great pity. It’s fifty times faster than the naïve code I came up with, though mine does not freeze, at least.
% ?- time((hydra(2000, _X), hydra(2000, _Y, _Y), term_factorized(g(_X, _Y), Is, _Coa), length(_Coa, Len))),
% writeln(unify), maplist(call, _Coa), writeln(check_equality),
% Is == g(_X, _Y), writeln(" verified.").
%@ % 532,865 inferences, 1.671 CPU in 1.716 seconds (97% CPU, 318799 Lips)
%@ unify
%@ check_equality
%@ verified. <=== freeze
% ?- hydra(2, X).
%@ X = h(h(_A, _A), h(_A, _A)) .
% hydra(0, _).
% hydra(N, h(X, X)):- N>0, N0 is N-1, hydra(N0, X).
% ?- hydra(2, X, X).
%@ X = h(_S1, _S1), % where
%@ _S1 = h(h(_S1, _S1), h(_S1, _S1))
% hydra(0, A, A).
% hydra(N, h(X, X), A):- N>0, N0 is N-1, hydra(N0, X, A).
```