Virtual Machines for Functional Languages by Erik Happi Stenman

May 25, 2022

In the Virtual Machines for Functional Languages presentation by Erik Happi Stenman, the CTO of Happy Hacking and an experienced Erlang developer, discusses the development of virtual machines for functional languages, specifically Erlang and Sophia. He shares personal experiences from working on the Erlang scheduling system, which led to the creation of the Erlang language and its virtual machine, BEAM. The second problem addressed was the need for safe contracts for blockchains, resulting in the development of the Sophia language and its virtual machine, FATE. The presentation covers the concurrency model, memory management, and message passing in Erlang and FATE, as well as the porting of the Ethereum Virtual Machine to the Eternity Blockchain’s Abstract Ethereum Virtual Machine. The speaker also mentions other functional languages like Sophia, Elixir, Lua, and a Lisp language that can run on the BEAM virtual machine.


Virtual Machines for Functional Languages: A comprehensive overview

Origins and evolution of virtual machines

Erik Happi Stenman highlights two distinct problems that drove the development of VMs for functional languages. The first emerged in the early 1980s at Ericsson, where the high software development cost for telephone exchanges led to the creation of Erlang. Erlang’s design focused on reliability, maintainability, and long-running performance, featuring a concurrency model based on shared-nothing semantics with message passing, robust error handling, and a philosophy of letting processes crash and handling the unhappy path.

The second problem arose with blockchains and the need for deterministic, reusable, and verifiable smart contracts. This led to the development of Sophia, a statically typed language designed for safe contract execution on blockchains, emphasizing deterministic results and parallel execution without concurrency.


Erlang and the BEAM virtual machine

Erlang, renowned for its concurrency features, uses gen servers and supervisors to manage concurrency, boasting symbolic and transparent data structures. The BEAM VM, Erlang’s runtime, is a garbage-collecting, reduction-counting, non-preemptive, directly threaded register VM. This design contrasts with Erlang’s preemptive scheduler, where reduction counting tracks function calls as reductions.

Erlang’s lightweight process model features individual stacks and heaps for each process, facilitating efficient resource use through message passing and signals. Optimized built-in functions yield or count reductions, ensuring smooth operation.


Message passing and dynamic typing

Erlang’s message passing involves copying messages to a buffer within the sender’s memory area before placing them in the receiver’s inbox. If the receiver isn’t ready, it tracks and eventually processes the message, maintaining dynamic but strong typing for error prevention and hot code loading.


Memory management and performance

Erlang employs a copying generation garbage collector with a single heap per process, enhancing calculation efficiency and memory management. This setup supports cache locality and minimizes pointers, though message passing can be costly due to heap copying and additional space requirements.


The FATE virtual machine and blockchain integration

FATE, designed for Sophia and smart contracts on the Æternity blockchain, is built on BEAM. It features no memory limit but is controlled by gas prices and costs, being statically typed and stack-based with well-defined serialization. FATE’s data types include integers, booleans, addresses, and first-class objects like contracts, oracles, and channels. Its compact bytecode contrasts with the more traditional Ethereum Virtual Machine (EVM), which relies on push and pop instructions.


Porting and efficiency improvements

Erik discusses porting the EVM to the Æternity Blockchain’s Abstract Ethereum Virtual Machine (AEVM), resulting in a more efficient and safer VM for smart contracts. FATE, a type-safe and deterministic VM, further improves efficiency and safety.


Other functional languages and conclusion

Erik also touches on other functional languages like Sophia for blockchain, Elixir for the BEAM platform, and languages like Lua and Lisp for BEAM. While he primarily uses Erlang, he occasionally resorts to other languages.

Erik concludes by thanking the audience and mentioning his free book on GitHub, which details writing a VM in both Erlang and C for small arithmetic expressions, offering a deep dive into the world of VMs for functional languages.


Additional resources

Check out more from the MeetUp Func Prog Sweden. Func Prog Sweden is the community for anyone interested in functional programming. At the MeetUps the community explore different functional languages like Erlang, Elixir, Haskell, Scala, Clojure, OCaml, F# and more.