A simple finite state machine in Erlang and F#

Yan Cui

I help clients go faster for less using serverless technologies.

Considering a very simple finite state machine (FSM) such as a code lock which requires you to enter a 4-digit numeric code and upon which will open the lock for a brief few seconds.

imageSuch a FSM have two states: Locked and Open.

Every time a key is entered it checks the sequence of digits inputted so far and if the sequence does not match the secret code then it stays in the Locked state.

If the input sequence matches the length of the secret code then the user is judged to have entered an incorrect sequence and the input sequence is reset.

If, on the other hand, the user has entered the correct code then the FSM will change to the Open state for a number of seconds before reverting back to the Locked state.

This post is intended to show you how you can implement this FSM easily using F#’s agent and Erlang OTP’s gen_fsm behaviour.

 

 

 

F#

First, let’s define the different states that our FSM can be in:

image

and then the messages that our agent can receive:

image

The actual FSM implementation looks like this:

This implementation is pretty straight forward, I’ve used a similar technique to the one Tomas Petricek used in his BlockingQueueAgent implementation and represented the different states using recursive functions.

One caveat with this FSM is the need to revert back to the Locked state after being in the Open state for a certain amount of time. For simplicity sake, I’ve simply specified a timeout (3 seconds) when waiting for messages in the Open state and use the TimeoutException as cue to go back to the Locked state. The obvious downside to my implementation here is that if a GetState message is received it will reset the timer!

You can fix this easily enough in a number of ways, including using a mutable instance variable to keep track of the current state and let the async lambdas modify it during state changes and transitions, then the GetState() method would no longer need to trouble the agent to find out what the current state is.

 

Erlang

Erlang’s OTP framework provide a gen_fsm behaviour which you can use to implement a FSM, which will take care of most of the plumbing and orchestration for you leaving you free to focus on the important things – handling the different ‘events’ that can be triggered during different states.

This Erlang implementation might look much bigger than its F# counterpart but keep in mind that I’ve included plenty of comments here and not been as conservative with write spaces as I was with the F# code. All and all, both versions require roughly the same number of LOC, the main difference being the lack of plumbing in the Erlang version, which if I have to be honest, can be confusing if you don’t have a decent understanding of how the gen_fsm behaviour works!

You can download and play around with the full source code (with corresponding tests for each implementation) from Github here. Enjoy!


 

Whenever you’re ready, here are 4 ways I can help you:

  1. If you want a one-stop shop to help you quickly level up your serverless skills, you should check out my Production-Ready Serverless workshop. Over 20 AWS Heroes & Community Builders have passed through this workshop, plus 1000+ students from the likes of AWS, LEGO, Booking, HBO and Siemens.
  2. If you want to learn how to test serverless applications without all the pain and hassle, you should check out my latest course, Testing Serverless Architectures.
  3. If you’re a manager or founder and want to help your team move faster and build better software, then check out my consulting services.
  4. If you just want to hang out, talk serverless, or ask for help, then you should join my FREE Community.

 


1 thought on “A simple finite state machine in Erlang and F#”

  1. Nice article and a great way of presenting the implementation difference between two extremely potential languages.
    In the erlang version, it would have been better if a small change is made in the initialization values specified in gen_fsm:start_link({local, code_lock}, ?MODULE, Code, []).

    I believe the Code should be entered in the form of a list like gen_fsm:start_link({local, code_lock}, ?MODULE, [Code], []), which is the industry practice.

    Never mind, this version should still work if its entered like ?MODULE:start_link([1234[). in the erlang prompt. User himself has to enter the input as list.

Leave a Comment

Your email address will not be published. Required fields are marked *