Re: Interpreter loop - best approach in Prolog?
- From: bart demoen <bmd@xxxxxxxxxxxxxx>
- Date: Tue, 09 Oct 2007 20:42:42 +0200
On Mon, 08 Oct 2007 12:37:36 -0700, sbaker8688 wrote:
I see two basic approaches: 1)
either maintain the stack as a "global variable" using assert/retract,
or 2) pass the old stack into various predicates to obtain the new
stack.
I don't mind doing 2. My question is how to do this (if desirable, or
recommended) without creating issues for the Prolog execution stack
(not to be confused with the stack in the language I am
implementing). In other words, how do I run the loop (getting input,
processing it) without stacking up stack frames or activation records
or whatever you want to call them through recursion?
Determinism and tail-recursion.
In fact, I suppose this could still be an issue even with assert/
retract.
Indeed: without determinism, any assert/retract approach is bound to
cause brain damage during debugging (and stack overflows).
Either way you have an essentially endless loop reading and
processing input. How do Prologgers typically deal with these
issues? Through tail-recursion?
Yes. Basically:
loop(State) :-
read_input(I),
process(I,State,NewState),
loop(NewState).
will do fine, if process/1 is determinstic (leaves behind no choicepoints).
If in doubt about that (this) determinism (is recognized by your Prolog
system), put a once() around it.
A failure driven loop will also help you out often, but, you will start
relying on assert/retract and find yourself in trouble eventually. Stick
with determinism+tail-recursion and your programs will come out better,
and often also more efficient.
Cheers
Bart Demoen
.
- Follow-Ups:
- Re: Interpreter loop - best approach in Prolog?
- From: sbaker8688
- Re: Interpreter loop - best approach in Prolog?
- References:
- Interpreter loop - best approach in Prolog?
- From: sbaker8688
- Interpreter loop - best approach in Prolog?
- Prev by Date: Re: Interpreter loop - best approach in Prolog?
- Next by Date: Re: Interpreter loop - best approach in Prolog?
- Previous by thread: Re: Interpreter loop - best approach in Prolog?
- Next by thread: Re: Interpreter loop - best approach in Prolog?
- Index(es):
Relevant Pages
|
|