0 XP
CH1·L1Guided DemoBeginner

Welcome to Erlang, The BEAM VM & Expression Syntax

Why This Matters in Production

In 1986, Ericsson computer scientists Joe Armstrong, Robert Virding, and Mike Williams designed Erlang to power telephone switches that could never go down. Their benchmark was nine nines (99.9999999%) availability—less than one second of downtime every 31 years.

Today, that exact runtime—the BEAM Virtual Machine—powers the core infrastructure of WhatsApp (handling billions of daily messages with fewer than 100 engineers), Discord's global guild clustering, Bet365's real-time betting engines, and RabbitMQ messaging brokers.

Erlang achieved this not through complex defensive programming, but by rethinking computing from first principles: isolated lightweight processes, immutable data, and shared-nothing message passing.

Deep Concept Breakdown & Under the Hood

Unlike operating system threads that allocate 1 MB to 8 MB of stack memory upfront, a BEAM process begins with only 309 words of memory (approximately 2.5 KB on a 64-bit operating system). A standard developer laptop can comfortably run hundreds of thousands of concurrent BEAM processes simultaneously.

Every BEAM node runs an asynchronous preemptive scheduler per available CPU core. Schedulers count reductions—roughly one function call or Built-In Function (BIF) invocation. After 4,000 reductions, the active process is preempted and yields CPU time to the next runnable process in the queue. This reduction-counting mechanism guarantees that no single calculation can monopolize the CPU or cause latency spikes in network I/O.

1%% Inspecting the live virtual machine
2Schedulers = erlang:system_info(schedulers_online),
3MaxProcs = erlang:system_info(process_limit),
4OtpRel = erlang:system_info(otp_release).
WarningCommon Beginner Syntax Trap: The Period Terminator

In Erlang, standalone expressions and function definitions must terminate with a period (.).

Forgetting the final period or placing it in the middle of a function will result in a compiler error such as syntax error before: ....

Comments in Erlang start with a single percent symbol (%) and run to the end of the line.

Parallel Worked Example

Consider a module designed to probe basic operating system environment metrics without using complex data structures:

1-module(os_probe).
2-export([check_env/0]).
3 
4check_env() ->
5 NumSchedulers = erlang:system_info(schedulers_online),
6 io:format("Operating with ~p online schedulers.~n", [NumSchedulers]),
7 {env_info, NumSchedulers}.

Notice the anatomy of an Erlang module:

  1. -module(os_probe). declares the module name, which must match the filename os_probe.erl.
  2. -export([check_env/0]). exports the function check_env with arity 0 (taking 0 arguments).
  3. The function body separates statements with commas (,) and ends with a period (.).
  4. The return value is the final evaluated expression: a 2-tuple {env_info, NumSchedulers}.

Official Erlang Docs & Function Reference

1%% Official OTP Signature (erts / erlang)
2erlang:system_info(Item) -> Info
3 Types:
4 Item = schedulers_online | process_limit | atom_limit | otp_release | term()
5 Info = term()

How to Read This Spec: erlang:system_info/1 is a C-implemented Built-In Function (BIF) taking one Item selector atom and returning the corresponding emulator property (Info) in O(1) time. Open the attached erlang:system_info/1 Reference Card below for official OTP documentation and ERTS C source links.

Assignment Specification

This is a Guided Runnable Demo. The module src/beam_intro.erl is pre-written and ready to execute:

  1. Inspect -module(beam_intro). and -export([inspect_vm/0]). at the top of src/beam_intro.erl.
  2. Trace how each call to erlang:system_info/1 queries live emulator telemetry before logging a diagnostic summary and returning the final tagged tuple.
  3. Click Run to execute the live probe against the BEAM engine, then click Submit to verify AST and runtime contracts.

beam_intro:inspect_vm/0

  • Input: None (0-arity function).
  • Behavior: Queries the live BEAM emulator for its OTP release string, online scheduler count, process limit, and atom table limit, prints a diagnostic summary to standard output, and returns a 5-element tagged telemetry tuple whose payload fields are a non-empty string (OtpRelease) followed by three positive integers (Schedulers, ProcessLimit, AtomLimit) in O(1) time.
  • Examples:
  • beam_intro:inspect_vm() returns {vm_info, OtpRelease, Schedulers, ProcessLimit, AtomLimit} (for example, {vm_info, "29", 8, 1048576, 1048576} depending on host hardware and OTP release).

Rules & Verification Constraints

  • No code edits are required for this guided introductory demo (the pre-written starter module passes 100% out of the box).
  • Exports: beam_intro must export inspect_vm/0.
  • AST Rule: Must call erlang:system_info/1 directly (hardcoded telemetry tuples are rejected by the AST verifier).
  • Runtime Contract: beam_intro:inspect_vm/0 must return a 5-tuple tagged with vm_info where OtpRelease is a non-empty list/string and Schedulers, ProcessLimit, and AtomLimit are positive integers.
Official Erlang Docs & Function Reference
OTP Docserlang:system_info
erlang:system_info(Item) -> Info
Quick Mental Model CheckPredict Before Coding

Why does the BEAM virtual machine preempt processes after 4,000 reductions rather than relying on cooperative multitasking?

Strict Rules & Verification Constraints

These rules are verified by bf_ast_verifier:verify_rules/3 on every Submit.