Re: Interpreter loop - best approach in Prolog?



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
.



Relevant Pages

  • Re: switch between functions
    ... 'A' can't be called and use a separate stack... ... prolog - allocates a new stack frame ... The total space used by allocation of variables on the stack depends on ... The prolog and epilog that the compiler generates are dependent on the ...
    (alt.lang.asm)
  • Re: ARMCODE object
    ... (the latter would avoid potential problems brought up below) ... If new prolog, can a Saturn-based calc ... A sysRPL program may expect a result on the stack, ...
    (comp.sys.hp48)
  • Re: Interpreter loop - best approach in Prolog?
    ... written in another language (not Prolog). ... The stack is the "global state" of the program that must be maintained ... creating an endless loop in Prolog is through the ... you might have to define repeat for yourself. ...
    (comp.lang.prolog)
  • Re: global stack (swi-prolog)
    ... I was running into local stack problems ... >> but I cleared that up, there is no local stack usage from pred. ... Determinism though is the key. ... graphical debugger to check for and analyse choicepoints. ...
    (comp.lang.prolog)
  • Re: Prolog to WAM Register Allocation?
    ... > I'm currently studying how Prolog compiles its source into Warren ... > Abstract Machine code and I was wondering what kind of algorithm does ... > Prolog use to assign variables and constants to X and Y registers? ... The model is highly optimised, as you probably gather, so stack frames ...
    (comp.compilers)