Term_factorized/3 error for compound_name_arity zero term

I happened to find a bug (?) on compound_name_arity zero terms in ‘term_facotized/3’.

% ?- term_factorized(f(q()), S, E).
%@ S = f(q()),
%@ E = [].

% ?- term_factorized(f(q(), q()), S, E).
%@ ERROR: Domain error: `compound_non_zero_arity' expected, found `q()'
%@ ERROR: In:
%@ ERROR:   [14] functor(q(),_7688,_7690)
%@ ERROR:   [13] terms:mk_subst([... - _7738],[_7748=_7750|_7744],t(black('',_7762,_7764,''),black(...,...,_7774,...))) at /Users/cantor/lib/swipl/library/terms.pl:309
%@ ERROR:   [11] toplevel_call(pt_mincoa:pt_mincoa: ...) at /Users/cantor/lib/swipl/boot/toplevel.pl:1678
%@ ERROR: 

I wanted to compare my version of ‘term_factoirzed’/3 via minimum coalgebra
representation, which shows statistics below ( imac intel 2020, macOS 26 Swiprolog latest ).
I hope it would be marginal compared with the builtin ‘term_foctorized/3’.

% ?-N=100000, time(forall(between(1, N, _), pt_term_factorize_test)).
%@ % 30,222,332 inferences, 2.885 CPU in 2.890 seconds (100% CPU, 10475896 Lips)
%@ N = 100000.

pt_term_factorize_test :-
	random_term(R),
	pt_term_coa([R], [I], Coa),
	pt_term_factorize(I, Coa, _, _).

Yes :slight_smile: Fixed. Curious to how your code compares!

Thanks for fix.

% ?- term_factorized(f(q()), S, E).
%@ S = f(q()),
%@ E = [].
% ?- term_factorized(f(q(), q()), S, E).
%@ S = f(_A, _A),
%@ E = [_A=q()].

However, it still ssems sometning wrong. Simply I use this predicate for test query below,
and got errors. So I hope it is reproducible for large N .

term_factorize_test :-
	random_term(R),
	term_factorized(R, _, _).
% ?-N=1, time(forall(between(1, N, _), term_factorize_test)).
%@ % 87 inferences, 0.000 CPU in 0.000 seconds (83% CPU, 2485714 Lips)
%@ N = 1.
% ?-N=2, time(forall(between(1, N, _), term_factorize_test)).
%@ % 869 inferences, 0.000 CPU in 0.000 seconds (94% CPU, 6437037 Lips)
%@ N = 2.
% ?-N=10, time(forall(between(1, N, _), term_factorize_test)).
%@ % 2,451 inferences, 0.000 CPU in 0.000 seconds (97% CPU, 9111524 Lips)
%@ N = 10.
% ?-N=100, time(forall(between(1, N, _), term_factorize_test)).
%@ % 5,356 inferences, 0.001 CPU in 0.001 seconds (97% CPU, 10260536 Lips)
%@ ERROR: Domain error: `compound_non_zero_arity' expected, found `h()'
%@ ERROR: In:
%@ ERROR:   [17] throw(error(domain_error(compound_non_zero_arity,...),context(...,_1322)))
%@ ERROR:   [15] catch('<garbage_collected>','<garbage_collected>','<garbage_collected>') at /Users/cantor/lib/swipl/boot/init.pl:565
%@ ERROR:   [14] call_cleanup(prolog_statistics:catch(...,...,...),prolog_statistics:(_1400=true)) at /Users/cantor/lib/swipl/boot/init.pl:685
%@ ERROR:   [13] prolog_statistics:time('<garbage_collected>') at /Users/cantor/lib/swipl/library/statistics.pl:287
%@ ERROR:   [11] toplevel_call('<garbage_collected>') at /Users/cantor/lib/swipl/boot/toplevel.pl:1678
%@ ERROR: 
%@ ERROR: Note: some frames are missing due to last-call optimization.
%@ ERROR: Re-run your program in debug mode (:- debug.) to get more detail.
%@    Call: (16) _192=true ? creep
%@    Exit: (16) true=true ? abort