viernes, 12 de marzo de 2010

Finite automata in erlang... well, in perl

I've been hacking some erlang after the third class with Johan Montelius. Last lecture was about failure handling, exeptions, linking and monitoring processes.

I've been tempted to translate some classical problems of concurrent or parallel computing to erlang. First chapters in some books are about what they call LTSA. They are mostly state machines, that can be composed, concatenated, intersected, and some other operation.


At first, I started doing an erlang little program that has three states, and receives messages to travel from one state to other.

Here's the basic code for the hello->converse->goodbye LTSA


Theres's a couple of things to note here:
  • Erlang syntax is a bit tricky. so many different line endings (, ; . nothing).
  • It's an ad-hoc code, but it follows a clear pattern.

Then, I thought about going meta. Why can't we build a program builder, that with a simplifyed input of our desired behaviour, writes erlang code?

I've used perl to hack a simple script that builds the code structure (or the whole program depending on your needs). I won't have to tell you it's quite unstable, and you can make it produce failing codes. Of course, it's quite shaky, but well... Just for the fun of it.

Try it, play with it, break it, or fix it. I don't think it can be useful for anything in this state, but maybe implementing composing functions...

For the moment the program just gets information on states, transitions, and destination states.
Then it builds a function for every state, and builds a receive statement, with each possible transition, and and ending _ -> fun to discard unrecognized messages. Keep in mind it won't keep order, so non disjoint cases aren't guaranteed to work. When entering a function, it's name (and actual parameters) are printed on the screen.


Yeah, the perl syntax highlight problem... try here to see a hightlighted version.

Check out my other erlang posts

No hay comentarios: