trealla-prolog / trealla

A compact, efficient Prolog interpreter written in plain-old C.
MIT License
268 stars 13 forks source link

TCO examples #543

Closed infradig closed 4 months ago

infradig commented 4 months ago
$ cat w.pl
main(N) :- N > 0, N1 is N-1, main(N1).

$ cat v.pl
% No local vars & no structures, yet not recovered...

zx81(X, Y) :- Y is (X*75+74) mod 65537.
main(0) :- !.
main(I) :- zx81(I, _), I2 is I-1, main(I2).

$ cat y.pl
f(0) :- !, fail.
f(_).
main(N) :- f(N), N1 is N-1, main(N1).

$ cat z.pl
f(X) :- X > 0, X=Y.
main(N) :- f(N), N1 is N-1, main(N1).

All these examples run in constant memory with SWI Prolog:

$ time swipl -g 'main(100000000);halt' w.pl

With Trealla only v.pl does not.

flexoron commented 4 months ago
zx81(X,Y) :- Y is X*1.
main(0) :- !.
% main(I) :- zx81(I,I), I2 is I-1, main(I2). % Good
% main(I) :- zx81(I,M), I2 is I-1, main(I2). % Bad, Var M eats memory
% main(I) :- zx81(I,_), I2 is I-1, main(I2). % Bad, Var _ eats memory
infradig commented 4 months ago

Yes, it's the call to xz81/2 (when you get var/var unification) that causes main/1 not to be TCO. I know why just haven't worked out a solution that doesn't break another test, there may be 2 things to fix.

On Sat, May 25, 2024 at 12:21 PM flexoron @.***> wrote:

zx81(X,Y) :- Y is X*1. main(0) :- !. % main(I) :- zx81(I,I), I2 is I-1, main(I2). % Good % main(I) :- zx81(I,M), I2 is I-1, main(I2). % Bad, Var M eats memory % main(I) :- zx81(I,), I2 is I-1, main(I2). % Bad, Var eats memory

— Reply to this email directly, view it on GitHub https://github.com/trealla-prolog/trealla/issues/543#issuecomment-2130672067, or unsubscribe https://github.com/notifications/unsubscribe-auth/AFNKSEVO3GUMGHW5SOZUNU3ZD7YTTAVCNFSM6AAAAABIHBE6YWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCMZQGY3TEMBWG4 . You are receiving this because you authored the thread.Message ID: @.***>