Would anyone have an example of implementing a state machine in continuation passing style (CPS)? I'm looking for a simple example although it does not have to be.
I'd like to implement a state machine for a game lobby where on receipt of event-1 I would send game summaries, on event-2 player summaries, etc. I will then build on this example to implement a poker logic state machine.
In the game logic fsm there are events like player-joined and player-left and where the game would end when there are not enough players or begin when enough players joined and where it would otherwise state in the same state.
Thanks, Joel
Afficher les réponses par date
Would anyone have an example of implementing a state machine in continuation passing style (CPS)? I'm looking for a simple example although it does not have to be.
Why do you want to do this? In Scheme you can code in direct style, and use call/cc when you want to access the continuation. The overall structure of your code should be much clearer than CPS'ed code.
Howdy!
Is it possible to return continuations to C in Gambit? This is a quote from the documentation:
Yes you can. The Scheme continuation (call it a stack if you want) and the C stack are managed independently. Scheme continuations are managed in a heap and you can jump from one continuation to another one at will. In C, once you return from a function call (i.e. you pop a stack frame), you can't return to the stack frame that was popped. The problem is when you mix both worlds, i.e. you call C from Scheme or Scheme from C. In this case, you can still jump from one Scheme continuation to another at will. However, if you return from a C to Scheme function call X, this may remove from the C stack some frames related to other Scheme continuations (the frames for the C to Scheme calls that have been created since the call X and that have not returned yet). The constraint is that you can't return from these (otherwise unrelated) C to Scheme calls.
Marc
Marc Feeley wrote:
Why do you want to do this? In Scheme you can code in direct style, and use call/cc when you want to access the continuation. The overall structure of your code should be much clearer than CPS'ed code.
I realized after a couple more days of reasearch that I'm confused. What I really want to do is code state machine with call/cc since I have an event-driven application. I couldn't figure out how to do this, though.
I'm thinking that I should have a single "dispatch-event" function as entry into scheme. This function would deserialize the event and figure out which state machine it belongs to. This part I have no trouble with.
I cannot figure out, though, how to use call/cc to move the state machine from one state to the next upon event. When an event arrives I need to pass it to the state machine and have it advance itself and return the outgoing packet or #f to signal that the exit state has been reached.
One thing that confuses me is that it seems that I need to return the continuation (not sure if I actually need to) but then I also need to return the outgoing event/packet from my "entry-into-scheme-dispatch- function".
Thanks, Joel
On Wed, 8 Dec 2004 09:16:14 +0000, Joel Reymont joelr@well.com wrote:
I realized after a couple more days of reasearch that I'm confused. What I really want to do is code state machine with call/cc since I have an event-driven application. I couldn't figure out how to do this, though.
Yes, you are confused. This is the wrong way to do it. You want to use tail-called closures, I expect. A lot depends on just how much you need dynamic extent versus what you can arrange to have in lexical scope. If you're building the software from scratch, you will have a much easier task without call/cc. If you have a significant piece of 3rd-party software that isn't event driven in the first place, *then* it can make sense to use call/cc to convert it to an event-driven model.
david rush