[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Iteratively calling Prolog from C

From: Daniele Peri
Subject: Re: Iteratively calling Prolog from C
Date: Wed, 16 Jul 2003 17:19:16 +0200
User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.4) Gecko/20030624

Lindsey Spratt wrote:

On Wednesday, July 2, 2003, at 03:30  AM, Daniele Peri wrote:

I made some tests about iteratively calling prolog from a foreign C
function. The idea is to improve either performance or memory footprint
of very deep recursion calls. This is also needed as gprolog doesn't
seem to optimize tail recursion. I didn't delve into gprolog source but
I have not find any reference to such optimization in the documentation.

gprolog *does* "optimize tail recursion". It uses the Last Call Optimization (LCO). I looked at the generated WAM code, and gprolog appears to follow the LCO as described by Hassan Aït-Kaci in "Warren's Abstract Machine: A Tutorial Reconstruction" (MIT Press, 1991). Specifically, the "call;deallocate" at the end of the body is replaced by "deallocate;execute". This can work well for a tail recursive predicate, leading to constant stack size. Unfortunately, there is nothing to optimize the use of heap storage, so a simple recursive predicate may exhaust the heap. For instance, consider the following predicate to calculate the sum of the series of integers from 1 to N

Subtle, doubtful, distinction that, unfortunately, does not solve the problem. My little hack would do what I need until the main problem is solved if it only worked.
I would really appreciate any useful comments.

Thank you.

reply via email to

[Prev in Thread] Current Thread [Next in Thread]