[mercury-users] Can mercury apply tail call optimization for this piece of code?

Vladimir Gubarkov xonixx at gmail.com
Wed Mar 30 06:47:07 AEDT 2011


Hi,

I'm experimenting with some optimization options. I know, that mercury
supports goal-reodering for optimization, so I thought, that it could
optimize next code to tail-recursive, but seems It's not.

%%% begin

:- module x_to_y.
:- interface.
:- import_module io.
:- pred main(io::di, io::uo) is det.
:- implementation.

:- import_module list, char, int, require.

:- pred replaceEvenElementList(list(char), list(char)).
:- mode replaceEvenElementList(in, out) is det.

replaceEvenElementList([A,E|T1],[A,E1|T2]):- replaceEvenElementList(T1,T2),
E1 = (E='x'->'y'; E).
%replaceEvenElementList([A,E|T1],[A,E1|T2]):- E1 = (E='x'->'y'; E),
replaceEvenElementList(T1,T2).
replaceEvenElementList([E],[E]).
replaceEvenElementList([],[]).

:- mode gen(in, in, out, in) is det.
gen(N, E) -->
( {N>0} ->
[E],
gen(N-1, E)
;
[]
).
l = L :- gen(10000000, 'x', L, []).

main --> {replaceEvenElementList(l, L1), (take(10,L1,L2) -> L = L2;
error("Not enough!"))}, print(L).

%%% end

I'm using command line options

set OPTS=%OPTS% --make --infer-all
set OPTS=%OPTS% -O6
set OPTS=%OPTS% --optimize-constructor-last-call
set OPTS=%OPTS% --grade=hlc.gc
set OPTS=%OPTS% --structure-reuse

But it shows stack-overflow this way. If uncomment second line of clause
definition and comment first one - works fine.
Maybe there is some additional optimization command line switch that can do
the job?


This is more theoretical than practical question and it appears as a result
of some holywar I'm participating in. :-)


Thank you
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mercurylang.org/archives/users/attachments/20110329/d6c9bb6e/attachment.html>


More information about the users mailing list