users-prolog
[Top][All Lists]

## Re: Iteratively calling Prolog from C

 From: Daniel Dudley Subject: Re: Iteratively calling Prolog from C Date: Thu, 17 Jul 2003 20:22:57 +0200

```Bartek Wilczynski wrote:

>
> <cut>
>
> > ?- factorial(18795, F).
>
> <cut>
>
> > Big enough for you?
> >
> > Daniel
> >
>
> I'm afraid You don't understand the problem with tail recursion
> optimization, and I think that other discussion participants
> find it to obvious to explain to You, so I'll try.

You assume far too much. :-(

> The problem of recursion optimization is usually caused not by
> the time overhead, but by the memory usage on the stack. 18000
> recursive calls is not much. The problem begins when you try to
> somehow create a list of let's say 100.000.000 atoms using a
> recursive call. The list itself doesn't take you too much
> memory, but the stack is overfilled with the recursive calls.
>
> unfortunately it's not implemented well in gprolog (to my best
> knowledge).

No, you are adding nothing to my knowledge base here. :-(

Generally, I would not recommend Prologers to create huge
lists via pure recursion -- in any Prolog implementation.

Whether it's advisable to create huge lists at all is an
entirely different matter. What a waste of resources it
would be to access, say, the 89346th element in the above
list, not to mention the question of inefficiency! I don't
think I am going too far in saying it borders on madness!

Which brings me back to my original post in this thread.
Ok, if you're feeling particularly mad (just today or even
long-term), then consult this (for_nd.pl) in gprolog:

% for_nd(Start,Stop,N) is true if N is the current value
% of the iteration ranging from Start to Stop. For each
% iteration, N is increased by one.

for_nd(N,_,N).
for_nd(Start,Stop,NumOut):-
Start < Stop,
Start1 is Start + 1,
for_nd(Start1,Stop,NumOut).

and run this query:

?- findall(N,for_nd(1,100000,N),L).

Bartek can add three 0's to the 100000 in the query if he
so wants.

Note that for_nd/3 is a non-deterministic predicate on the
same lines as factorial_nd/5.

I'll spare the mailing list the output!

> And please, stop giving examples using different prolog
> implementation, when discussion is about specific problems
> in gprolog implementation (or not-implementation ;).

I apologize, the meaning was merely to show erroneous
behaviour in gprolog (in comparison to other Prologs).
Clearly I assumed too much when I thought this would be

Daniel

```