Syntax Reference
This page is a practical syntax reference for writing
.alef programs. It is public language documentation, not a
description of the private compiler implementation.
File Shape
import std.http { json_response }
fn main() {
println("hello")
}
Alef files usually contain imports, declarations, and statements
inside functions. The conventional program entrypoint is
main.
Comments
// A line comment explains the next statement.
println("ready")
Primitive Values
let name = "Ada"
let count = 3
let ratio = 0.75
let active = true
let missing = none
Strings
Strings use double quotes. Interpolation uses
{name}.
let id = "TD-101"
println("ticket {id} loaded")
Bindings
let name = "Ada"
name = "Grace"
Use reassignment for a real state transition. Use a new binding when the new value has a distinct meaning.
Function
fn add(a, b) {
return a + b
}
Typed Function
fn add(a: int, b: int) -> int {
return a + b
}
Decorator
Decorators attach supported metadata to the function that follows them.
@deprecated("use new_add instead")
fn old_add(a: int, b: int) -> int {
return a + b
}
@inline
fn new_add(a: int, b: int) -> int {
return a + b
}
Supported public decorators are @test,
@bench, @deprecated, @inline, and
@noinline.
Operators
let n = 1 + 2 * 3
let ok = n >= 3
let visible = ok and not hidden
let owner = plan == "enterprise" or priority == "P0"
Use == for equality. Use and,
or, and not for boolean logic.
Array
let xs = [1, 2, 3]
xs.push(4)
println(xs[0])
Map
let ticket = {
"id" => "TD-101",
"status" => "triage"
}
println(ticket["status"])
Struct
struct Ticket {
id: string,
title: string
}
let ticket = Ticket {
id: "TD-101",
title: "Checkout timeout"
}
println(ticket.title)
Enum
enum Status {
Triage,
Progress,
Done,
}
let status = Status.Triage
If
if ok {
return "yes"
}
return "no"
Use else when both branches produce useful work:
if priority == "P0" {
println("page incident lead")
} else {
println("queue normally")
}
For
for item in items {
println(item)
}
While
while retries < 3 {
retries = retries + 1
}
Match
match value {
Ok(v) => v,
Err(e) => 0,
}
Result
fn divide(a: f64, b: f64) -> Result<f64, string> {
if b == 0.0 {
return Err("division by zero")
}
return Ok(a / b)
}
Option
fn find_owner(ticket) -> Option<string> {
if ticket["owner"] == "" {
return None
}
return Some(ticket["owner"])
}
Import
import std.http { json_response }
Complete Small Program
fn priority(plan, blocked) {
if plan == "enterprise" and blocked {
return "P0"
}
return "P2"
}
fn main() {
let ticket = {
"id" => "TD-101",
"title" => "Checkout timeout",
"plan" => "enterprise",
"blocked" => true
}
let p = priority(ticket["plan"], ticket["blocked"])
let id = ticket["id"]
println("{id}: {p}")
}
Advanced AI-Native
Alef is designed for AI workloads with DeepMind/JAX-inspired vectorization, differentiation, macros, and data streams.
// 1. Vectorization (vmap) and Differentiation (grad)
fn f(x) { x * 2.0 }
let vec_f = vmap(f)
let grad_f = grad(f)
// 2. Metaprogramming (macro)
macro html(content) {
"<div class=\"ai-gen\">" + content + "</div>"
}
let ui = html!("Hello AI")
// 3. Async Streams (Generators)
fn stream_llm() {
yield "Hello"
yield "World"
}
for await chunk in stream_llm() {
print(chunk)
}