BRAIDGROUP
RESEARCH & DEV
47. Documentation

Keyword Reference

Complete reference of all Braid reserved keywords with syntax and examples.

Core Keywords

fn

Grammar: fn identifier ('[params]') ['->' type] block

Declares a function. Parameters are comma-separated name: type pairs. Return type is optional with ->. The body is a {} block.

fn add(x: int, y: int) -> int {
    return x + y
}

fn greet() {
    print("Hello")
}

let

Grammar: let identifier [':' type] '=' expression

Declares an immutable binding with optional type annotation and required initializer.

let x = 10
let name: string = "Alice"
let pi = 3.14159

if / else

Grammar: if expression block ['else' (if expression block | block)]

Conditional branching. The condition must evaluate to bool. else if chains are supported.

if x > 0 {
    print("positive")
} else if x < 0 {
    print("negative")
} else {
    print("zero")
}

while

Grammar: while expression block

Loops while the condition is true. Condition must be bool type.

let i = 0
while i < 10 {
    print(i)
    i = i + 1
}

return

Grammar: return [expression]

Returns from a function. The expression is optional for functions with no return value (nil is returned).

fn max(a: int, b: int) -&gt; int {
    if a > b { return a }
    return b
}

fn log(msg: string) {
    print(msg)
    return
}

Data Structure Keywords

struct

Grammar: struct identifier '{' {identifier ':'; type ';'} '}'

Defines a named struct type with typed fields. Max 16 fields per struct.

struct Point {
    x: int;
    y: int;
}

struct User {
    name: string;
    age: int;
    active: bool;
}

enum

Grammar: enum identifier ' {identifier} '

Defines a named enum type with member variants. Members are accessed with dot notation: Color.Red.

enum Color { Red, Green, Blue }
enum Status { OK, NotFound, Error }

let c = Color.Red
let s = Status.OK

match

Grammar: match expression '{' {pattern '=>' (expression | block) '}'}

Pattern matching on integers, strings, booleans, enums, and nil. The wildcard _ matches any value.

match code {
    200 => print("OK"),
    404 => print("Not Found"),
    _ => print("Unknown")
}

Module Keywords

import

Grammar: import module_path where module_path is dot-separated identifiers

Imports a module. Resolves to .br or .bx files matching the path.

import std.io
import std.math
import std.time
import ui.widget

extern

Grammar: extern fn identifier '(' [params] ')' ['->' type]

Declares an external C function for FFI linking at compile time.

extern fn sqrt(x: float) -&gt; float
extern fn srand(seed: int)
extern fn rand() -> int

impl

Grammar: impl identifier ' {fn_decl} '

Implements methods for a struct type. Methods receive self as the first parameter.

impl Point {
    fn distance(self, other: Point) -&gt; float {
        // compute distance
    }
}

Diameter Keywords

diameter

Grammar: diameter identifier ':' ' pole pole_body pole pole_body '

Defines a dialectical reasoning construct with two opposing poles.

diameter temperature: {
    pole hot: { return 80.0 }
    pole cold: { return 10.0 }
}

pole

Grammar: pole identifier ':' block

Declares one pole of a Diameter. Must appear exactly twice inside a Diameter definition.

observe

Grammar: observe '(' expression ')' or expression '.' observe '(' string ')'

Reads the current state of a Diameter. Supported observations: "tension", "state", "pole_a", "pole_b".

let tension = diameter.observe("tension")
let state = observe(diameter)

evolve

Grammar: evolve '(' expression ')' or expression '.' evolve '(' ')'

Advances a Diameter's state by one step, resolving tension between poles.

diameter.evolve()
evolve(diameter)

FFI and Runtime Keywords

native

Grammar: native fn identifier '(' [params] ')'

Declares a runtime-linked FFI function. Implemented in C within the BraidVM binary.

native fn print_int(val: int)
native fn print_float(val: float)
native fn print_string(val: string)

ML Keywords

model

Grammar: model identifier '=' identifier '{' {identifier ':' expression ';'} '}'

Defines an ML model configuration by instantiating a base model with hyperparameters.

model my_llm = Transformer {
    vocab_size: 50257;
    d_model: 512;
    num_layers: 6;
    num_heads: 8;
    d_ff: 2048;
    dropout: 0.1;
    activation: "gelu";
    learning_rate: 0.001;
    batch_size: 8;
    seq_len: 512;
    num_epochs: 5;
    seed: 42;
}

Literal Keywords

true / false

The two boolean literal values. Type is bool.

let is_active = true
let is_complete = false

Decorator Keywords

@autograd

Grammar: '@autograd' fn_decl

Decorator that marks a function for automatic differentiation. Builds a computation graph during forward pass.

@autograd fn loss_fn(x: float, y: float) -&gt; float {
    return (x - y) * (x - y)
}

@layer

Grammar: '@layer' struct_decl

Decorator that marks a struct as a neural network layer with trainable parameters.

@layer struct Linear {
    @param weight = tensor_init(128, 64);
    @param bias = tensor_init(128);
    fn forward(self, x: tensor) -&gt; tensor {
        return x @ self.weight + self.bias
    }
}

@param

Grammar: '@param' identifier '=' expression

Declares a trainable parameter inside a @layer struct. Automatically tracked for gradient computation.

Concurrency and Device Keywords

async

Declares an asynchronous function. Planned feature via std.async. Syntax is reserved.

await

Pauses execution of an async function until the awaited value resolves. Planned feature.

spawn

Creates a new green thread for concurrent execution. Syntax is reserved for std.async.

device

Grammar: with device '(' string ')' block

Sets the compute device context for tensor operations within the block.

with device("cuda") {
    let result = matrix * matrix
    let gpu_tensor = tensor.to("cuda")
}

with

Grammar: with device '(' string ')' block

Context manager for device scope. Currently only supports with device(...).

Related Pages