Mostrando entradas con la etiqueta erlang. Mostrar todas las entradas
Mostrando entradas con la etiqueta erlang. Mostrar todas las entradas

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

lunes, 8 de marzo de 2010

My name is erlang

I've been introduced to erlang by Prof.Johan Montelius.

I'm enjoying the lectures a lot (the two so far). He's a great speaker (is's quite fun to attend classes) and he managed to explain the basic concepts of concurrent erlang programming in 2 hours (provided you already knew the basic erlang syntax).

I'm taking some notes at class, and I'll eventually clean them and upload them in a blogpost format.

For the moment, here is a silly code I wrote the other day while I was in the bus.

It's interesting how:

  • Concepts learnt previously in scheme and perl (Higher Order Perl ftw!) become useful and appliable here.
  • Pattern matching is stressed to become the basis of method dispatch (with or without guards), variable binding, if/case/any_conditional and message receiving.
  • The Message Passing system is GREAT. Not just the "forget about low level sockets" but the mailbox
    system of each process, and the transparency that frees you from thinking low level.
  • Being a functional language, it does tail call optimization, and in most cases you better take advantage of it, because processes (if you think of them as DFA's) have a long life, and they just travel from one state to other by calling themselves recursively with an updated argument (you can
    pass the state along through the function arguments).


Here's the code. Just a Proof of concept. Two processes sending pings and pongs one to the other. Pretty simple, but there's pretty much a synopsis of the erlang (ugly) syntax and expressions.