💻 Learn by Example

Code Examples

Production-quality AXIOM code. Copy, modify, ship.

All Basics Safety Concurrency I/O Patterns
🚀

Hello World

The simplest AXIOM program — requires the IO capability

AXIOM
module hello

-- Every side-effect requires an explicit capability.
-- 'requires IO' means this function can interact with the outside world.
fn main() -> Result<Unit, IOError> requires IO {
    io.print("Hello, AXIOM!")?
    Ok(())
}
Beginner Capabilities
🎯

Pattern Matching & Enums

Exhaustive match expressions with zero-cost enum dispatch

AXIOM
module shapes

enum Shape {
    Circle(radius: Float<64>),
    Rectangle(width: Float<64>, height: Float<64>),
    Triangle(base: Float<64>, height: Float<64>),
}

-- Pure function: no capabilities required, no side effects.
fn area(shape: Shape) -> Float<64> {
    match shape {
        Shape.Circle(r)        => 3.14159 * r * r,
        Shape.Rectangle(w, h)   => w * h,
        Shape.Triangle(b, h)    => 0.5 * b * h,
    }
}

-- The compiler guarantees all variants are handled.
-- Adding a new variant forces you to update every match.
Beginner Pure Functions Enums
🛡️

Error Handling

No exceptions, no null. Errors are explicit values you must handle.

AXIOM
module errors

enum ParseError {
    InvalidFormat(msg: String),
    OutOfRange(value: Int<64>, max: Int<64>),
}

fn parse_port(input: String) -> Result<Int<16>, ParseError> {
    let value = input.parse_int()
        .map_err(|e| ParseError.InvalidFormat(e.message))?

    if value < 1 || value > 65535 {
        Err(ParseError.OutOfRange(value, 65535))
    } else {
        Ok(value.to<Int<16>>())
    }
}

-- Usage: the ? operator propagates errors up the call stack.
-- You can never accidentally ignore an error in AXIOM.
fn setup(port_str: String) -> Result<Config, ParseError> {
    let port = parse_port(port_str)?
    Ok(Config { port, host: "0.0.0.0" })
}
Intermediate Safety Result Type

Structured Concurrency

Spawn tasks that are guaranteed to complete before the parent exits

AXIOM
module parallel

fn fetch_all(urls: List<String>) -> Result<List<Response>, NetError>
    requires Net, Async
{
    -- TaskGroup ensures ALL spawned tasks complete before this scope exits.
    -- No fire-and-forget. No leaked goroutines. No orphaned promises.
    let group = TaskGroup.new()

    for url in urls {
        group.spawn(|| {
            net.http.get(url)?
        })
    }

    -- All tasks run in parallel. Results collected in order.
    group.join_all()
}

-- Cancellation is cooperative and deterministic.
-- If any task fails, the group cancels siblings and returns the first error.
Intermediate Async TaskGroup
💰

Fixed-Point Financial Math

Native base-10 arithmetic — no floating-point surprises

AXIOM
module finance

struct Money {
    amount: Decimal<18, 4>,  -- 18 digits, 4 decimal places
    currency: Currency,
}

fn apply_tax(price: Money, rate: Decimal<8, 4>) -> Money {
    -- No floating-point rounding errors. Ever.
    -- 0.1 + 0.2 == 0.3 is ALWAYS true in AXIOM.
    Money {
        amount: price.amount * (1.0000 + rate),
        currency: price.currency,
    }
}

fn split_bill(total: Money, ways: Int<32>) -> List<Money> {
    let share = total.amount / ways.to_decimal()
    let remainder = total.amount - (share * ways.to_decimal())

    -- First person pays the remainder (deterministic, auditable)
    let shares = List.fill(ways, Money { amount: share, ..total })
    shares.update(0, |m| m { amount: m.amount + remainder })
}
Advanced Decimal Finance
🌐

HTTP Server

A type-safe server with automatic routing and middleware

AXIOM
module server

use std.net.http.{ Server, Request, Response, Status }
use std.json

struct User derives Serialize, Deserialize {
    name: String,
    age:  Int<32>,
}

fn main() -> Result<Unit, ServerError> requires Net, IO {
    let app = Server.new()
        .get("/", |_req| {
            Response.text("Welcome to AXIOM!")
        })
        .post("/users", |req| {
            let user = req.body.parse::<User>()?
            io.print("New user: {user.name}")?
            Response.json(user, Status.Created)
        })

    io.print("Server running at http://localhost:3000")?
    app.listen(3000)
}
Intermediate Networking JSON
🔧

Compile-Time Profiles

Write once, target firmware, WASM, server, or desktop

AXIOM
module driver

-- Profile constraints are checked at compile time.
-- This function can only be used in firmware or bare-metal targets.
fn init_gpio(pin: Int<8>, mode: PinMode) -> Result<GpioHandle, HwError>
    requires Hardware
    profile Firmware | BareKernel
{
    let reg = hardware.mmio.read(GPIO_BASE + pin.to<Addr>())?
    hardware.mmio.write(reg, mode.to_bits())?
    Ok(GpioHandle { pin, mode })
}

-- Trying to call init_gpio from a web or server profile
-- is a compile-time error, not a runtime failure.
Advanced Embedded Profiles

Try AXIOM Today

Get started in 60 seconds with our CLI installer.