Is this a good implementation of a loop in Prolog?

Posted by Carles Araguz on Programmers See other posts from Programmers or by Carles Araguz
Published on 2012-12-03T17:05:55Z Indexed on 2012/12/03 17:18 UTC
Read the original article Hit count: 157

First of all, let me tell you that this happens to be the first time I ask something here, so if it's not the right place to do so, please forgive me.

I'm developing a rather complex software that has a Prolog core implementing a FSM. Since I don't want it to stop (ever), I'm trying to write a good loop-like predicate that would work using Prolog's recursion. After a few unsuccessful tries (mainly because of stack problems) I ended up having something similar to this:

/* Finite State Transition Network */
transition(st0,evnt0,st1).
transition(st1,evnt1,st2).
transition(st2,evnt2,st0).

fsm_state(state(st0),system(Energy,ActivePayloads),[]) :-
    /* ... */
    transition(st0,evnt0,NextState),
    !,
    fsm_state(state(NextState),system(Energy,ActivePayloads),[]).

fsm_state(state(st1),system(Energy,ActivePayloads),[]) :-
    /* ... */
    transition(st1,evnt1,NextState),
    !,
    fsm_state(state(NextState),system(Energy,ActivePayloads),[0,1,2]).

fsm_state(state(st2),system(Energy,ActivePayloads),[P|Params]) :-
    /* ... */
    transition(st2,evnt2,NextState),
    !,
    fsm_state(state(NextState),system(Energy,ActivePayloads),[]).

start :- 
    Sys = system(10,[]),
    fsm_state(state(s0),Sys,[]).

Is this a good approach?

© Programmers or respective owner

Related posts about finite-state-machine

Related posts about prolog