
Ragel State Charts - raju
http://www.zedshaw.com/essays/ragel_state_charts.html
======
frig
In my experience SMC ("state machine compiler", tres original no?) is better-
suited than ragel for working with state machines that're easily specified as
"state charts".

The difference is that ragel is oriented around consuming a stream-of-chars:
the generated state machine expects to be consuming chars as input.

You can pick up on this in the writeup even though Zed's not pointing it out:
he first has to define his messages as chars (eg: 'done' == 'D'), then he
specifies his states + transitions in terms of those definitions.

SMC instead generates methods for each 'message' and it's the enveloping
program's responsibility to call the correct method to update the state.

This is cleaner for simpler state machines.

None of this is intended as a knock on ragel, btw: it's a very solid piece of
software, and for more-complicated state machines should be the go-to choice.

~~~
thurston
Correction: ragel is not bound to chars. It can consume integral numbers of
any size up to long. The numbers do not need to be in arrays. They can be
embedded in structures.

~~~
frig
That's useful, thanks.

Also thanks for this: <http://news.ycombinator.com/item?id=134834>

It's a wonderfully useful statement, and gets at what I was trying to get at
much better than I could.

I stand by SMC being a better fit for simple finite state machines (eg: the
kind best specified using state charts); typical uses I've put SMC towards are
for ensuring consistency of internal state in complicated UI elements and
similar tasks...for which there's no real 'regular language' that needs
parsing (or, at least: no real gain to be had in setting up a regular language
of input-events when state charts will do).

I'm guessing you're _the_ thurston behind ragel: thank you for making ragel,
for making it available, and for making it so well; it is an extremely high-
quality piece of work.

------
preview
In the article, Zed says, "it’s just that nobody learns about state machines
unless they take an obscure compiler design class. Very few universities teach
state machines as a method of specifying the logic for a program."

My background is as a EE, and I work in hardware. I can't imagine not knowing
about state machine design. Is Zed's statement accurate for those with a pure
software focus?

~~~
sethg
I would think that anyone who gets a degree from a reputable CS program would
have to take a class in compiler design, so I wouldn't call such classes
"obscure". Of course

(a) just because you covered it in a class doesn't mean you remember it five
years later

(b) lots of people rise through the ranks of IT without formal CS training

(c) some CS programs are not so reputable

~~~
jerf
(d) a lot of people buy into the "school is worthless" line so hard that even
when something as practical as state machines are paraded in front of them
they don't notice

(Granted, the education system might have some responsibility there, but
personally _I_ recall examples being made. There's only so hard the profs can
make the point that this stuff _actually matters_.)

------
tsally
Zed seems to have a freaky ability not only to implement high quality code
himself, but to find high quality code written by others, absorb it, and then
explain it. Strikes me as the qualities that make a successful angel investor.

~~~
alex_c
I don't get it, although others seem to agree with you. What does one have to
do with the other?

~~~
tsally
The ability to recognize talented programmers and programs is pretty rare.
This is why software projects with founders that can write usually succeed,
because the responsibility of selling good code usually falls on the
programmer (think Linux, Arc, Rails, etc).

Having the ability to see good code allows an investor to find the hidden
gems-- excellent programs that no one knows about because they aren't
promoted. I'd be willing to bet that there is a goldmine of ideas implemented
in already existing code, just waiting to be discovered.

~~~
alex_c
Thanks, that clears it up a bit.

I'm not sure I agree though, for two reasons.

First, the projects you mentioned are great projects, but they're not
something an angel investor would be interested in.

Second, most startups - or at least web startups - do not fail for lack of
technical expertise. The product might be an unusable mess, or it might solve
entirely the wrong problem, or it might be marketed in completely the wrong
way, or there might simply not be enough demand for it. I'm sure there are
cases where a startup fails for technical reasons, but I'm willing to bet real
money that in the vast majority of cases investors and founders don't say
"man, if only the programmers were a tiny bit better, we could've made it".
(even when they do say it, chances are that wasn't the real problem)

~~~
tsally
You're correct, they're most definitely not projects that an investor would
invest in. However, if I am going to invest in someone, I want to know they
can _code_. I want to look at programs they wrote while they were in college,
or working a job. If I see a novel solution, my confidence in my investee goes
way up.

Tons of people have great ideas, but their success is going to be determined
by how well they can code. When you say that startups don't fail because of a
lack of technical expertise, you're right in a limited sense. Startups of
decent calibur will not fail because the founders don't know closures,
continuations, design patterns, etc; they'll still be able to build products.
They just wont have the vision to know the full range of products they are
capable of building. If that's the case, you'll only last as long as it takes
for a better hacker to enter the market. Experts Exchange being overtaken by
Stack Overflow is a perfect example. Same idea, but because actual hackers are
running Stack Overflow, it's trouncing Experts Exchange, with no signs of
slowing down.

~~~
staunch
What makes you think Stack Overflow has any technical advantage over Experts
Exchange? And, what makes you think their growth rates are at all correlated?

As far as I can tell Stack Overflow is a perfect example of a site where
technical talent is almost irrelevant. Just like this site. We're not here
because this site is written in Arc. We're here because of the community.

~~~
tsally
If not technical in terms of CPU cycles, technical in terms of design. For
one, I don't have to scroll all the way to the bottom of Stack Overflow to see
the answers to a question. Stack Overflow also loads far faster, and has
unobtrusive ads. These are things that hackers understand are important, and
the suits have a difficult time understanding.

------
biohacker42
_All of this sounds super fancy, but it’s really based on solid research done
on state machines over the years. Adrian simply took all the best research and
built a sweet little domain language for state machines._

This is why research and open source are important. And kudos to Adrian.

------
jerf
Erlang encourages this style through its OTP library; all of the default OTP
behaviors include state machines as part of their operations, or make it so
easy that they might as well. (gen_fsm is, as the name implies, a finite-
state-machine, and it's trivial to use pattern matching to use a piece of the
state in gen_event or gen_server to do the same thing). It doesn't sound like
it has the full range of awesome that Ragel state charts do, but it has a lot
of the value. It definitely gives you a lot of confidence that nobody can
trick your server into doing something unexpected with weird input unless you
do something very wrong yourself.

------
sofal
I'll just state here that this is also what powers _why's excellent Hpricot
HTML parser.

Back in my last job, I implemented a Java libconfig clone using Ragel. It was
the most fun I ever had at that company.

~~~
thurston
That's awesome.

------
grandalf
this is an awesome article... nice job, Zed. I want to try it.

