3 ms·
But why would you ever want to run an FSM which doesn't return the new state as a result? You cannot test a single transition, because it will just keep running
by viraptor 17y ago
But why would you ever want to run an FSM which doesn't return the new state as a result? You cannot test a single transition, because it will just keep running. If you need callbacks, you have to pass them inside the machine. You cannot implement an external halting condition easily. FSMs are usually doing one step on demand (at least in all the network applications) or whole thing at once in regexes and parsing. Those are implementations that are either trivial, or can be solved with simple objects extending State class.
FSM is defined as `S * E->S` typically, not `S * E * (your implementation details and callbacks)->(result)`. Do you see any gains in the 2nd solution? What exactly are the existing problems TCO would solve?
- eru 17y agoI am not nearly as knowledgeable as Guy Steele, but I attempt an answer. (I will be using Haskell-like notation, because it's most natural for me at the moment. I hope currying does not confuse you.) I agree that in a lot of scenarios you can be happy with a step function step :: State -> Input -> (Output, State) and a loop that feeds inputs to the step function, then does something with the output (like displaying it) and repeats with the new state. Here I could argue that this loop is best described as a tail-recursive function; but I guess the loop is bearable. However what if you have more complex flow of control? Like a non-deterministic state machine. Think of a parser. There loops won't suffice and you will have to make up your own control structures out of function calls. (Or emulate them with an explicit stack. But why re-implement language features?) Composability also benefits from tail calls. I do not know if the above paragraph contains an argument that's good enough. What do you think? I do know that parsing with combinators is pleasant to program and benefits (at least in its natural expression in FP) from proper tail calls. I also toy with the idea of using the following definitions instead of separating the (deterministic) stepping function from the state in the interface: date StepState = State (Input -> (Output, StepState)) (It might be a good idea to then separate state and stepping function in most implementations. In Haskell I'd also --- probably --- use typeclasses. But that doesn't add to the point here.)
- eru 17y agoP.S. http://fare.livejournal.com/142410.html http://fare.livejournal.com/142410.html seems to have some good arguments, too. (Though not on the exact same issue as this thread.) Also the comment by gls (Guy Steele I guess) http://projectfortress.sun.com/Projects/Community/blog/ObjectOrientedTailRecursion#comment-7 http://projectfortress.sun.com/Projects/Community/blog/Objec... gives an interesting perspective on the union example.
- viraptor 17y agoInteresting. Parser combinators might actually be a "proper" example that can't be transformed into either a loop or a single trampoline chain... I've never tried to implemented one, but I can imagine some parts to be easier when done directly with closures (try one path, if it fails just drop it and try another one). On the other hand - which parser-combinator needs more than 50 or so levels of depth... Hopefully someone here did it / can comment :)
- eru 17y ago> On the other hand - which parser-combinator needs more than 50 or so levels of depth... Oh, that's easy: Just have a combinator that repeats somehow, but complicated enough that you don't want to do it as a loop.
- eru 17y ago> But why would you ever want to run an FSM which doesn't return the new state as a result? Or directly: Sometimes continuation passing style has advantages.