After having read the excellent book "Practical UML Statecharts in C/C++" by Miro Samek, I am eager to try them out sometime. More recently, I have started to teach myself Haskell and functional programming.
Only a few chapters into my book on Haskell, it struck me that statecharts might be difficult, and even against the grain of Haskell. After all, a large effort goes into creating state-free programs, or at least keeping all impure parts of the code well separated from the pure ones.
When I searched for "Haskell" combined with "statechart" on the net, I found almost nothing! This piqued my curiosity. Haskell is, after all, a reasonably old, general purpose programming language. How could it be that there seems to be virtually no activity related to statecharts?
Several possible explanations were suggested in the Haskell sub-Reddit thread "haskellers thoughts on statecharts", and I hope my short excerpts won't warp the views of any of the authors:
/…/ the Haskell community in general is not very active in UI development /…/
/…/ The downside of Statecharts is that all that flexibility that they provide make modelchecking (and therefor also model-based testing) harder. /…/
/…/ A state machine is a collection of states and transition rules. But for implementation states are redundant, you can work with transition rules alone, which are neatly represented as (a mutually recursive family of) functions. /…/ But Haskell is free from this restriction, so implementing state machine explicitly is usually redundant.
Hense (sic!), as a Haskeller I don't really need explicit state machine most of time. Functions as first-class citizens and TCO make them an implementation detail at best and unneeded at worst. /…/
a. /…/ You're probably more lucky with search-terms like "transition system", "actor model" or "state transducer" in the Haskell ecosystem. /…/
b. /…/ Functional reactive programming (FRP) and arrows are ways of implementing signal flow in Haskell. /…/
c. /…/ The State monad transformer can model that. If you have external signals in an intermediate step, you can embed it in a continuation monad. /…/
I allow myself to interpret, and comment this a bit:
- I find the notion that H. programmers don't use statecharts since they don't do that kind of programming a bit hard to believe. I may be wrong, but I would think the field of application is much broader than just UIs.
- This argument is slightly above my head. The author perhaps focuses on a certain type of testing, a certain tool. I mean, wouldn't a well-defined set of states, up front, make testing a lot easier, if anything?
- This is perhaps the most interesting argument, that explicit states would be superfluous in Haskell, and hence the need to code visible state machines. My objection is perhaps unfair, but wasn't the state machine abstraction (or rather, its visible incarnation in code) invented in order to make things clearer and easier to understand? The problem it solves — to make the unspecified, de facto state machine hidden in "ordinary" programs, making decisions based on the value of a large number of poorly organised variables, more visible — isn't this negated by getting rid of the state machine?
- Could it really be this simple, that statecharts are there, but are simply called something completely different? … or are these better solutions for the same problem, in the world of Haskell, effectively making statecharts genuinely redundant?
Will all this become embarrassingly obvious once I have finished reading the book on Haskell and FP?