✦ som
Learn
Introduction A tour of som
Guides
Errors Compile-time code dyn Shaders The web
Elsewhere
GitHub Examples
use wgpu;

/// Light moving through a dark field: a few sine waves over the screen, folded into each
/// other, and coloured from deep blue through glow's purple to a cyan edge.
let mut gpu = wgpu::Gpu::open("som", 1280, 720);
let mut field = wgpu::Shader::empty();

while gpu.frame() {
    let t = gpu.time() * 0.25;
    field.fullscreen(&mut gpu, (frag) => {
        let p = (frag.uv - vec2(0.5, 0.5)) * vec2(frag.size.x / frag.size.y, 1.0) * 3.0;
        let a = (p.x * 1.4 + t).sin() + (p.y * 1.7 - t * 1.3).sin();
        let b = ((p.x + p.y) * 1.1 + a + t * 0.7).sin();
        let c = ((p.x - p.y) * 0.9 - b * 1.5 + t).cos();
        let glow = (0.5 + 0.5 * (a * 0.6 + b + c)).clamp(0.0, 1.0);
        let base = vec3(0.035, 0.04, 0.075);
        let purple = vec3(0.545, 0.427, 0.929);
        let cyan = vec3(0.30, 0.85, 0.95);
        let colour = base + purple * glow * glow * 0.55 + cyan * (glow * c).max(0.0) * 0.25;
        vec4(colour, 1.0)
    });
    gpu.present();
}

Compiles to C · shaders to WGSL · runs on the web

Code that's proven
before it runs.

som is a compiled language without a garbage collector. Borrows, array indexes and contracts are checked while compiling, and any function can run while compiling too.

Take the tour See som GitHub

Indexing is proven

An index compiles only where it is known to be in range. No bounds check is left to fail as the program runs.

fn third(numbers: &[i32]) -> i32 {
    if numbers.len < 3 { return 0; }
    numbers[2]    // proven: 2 < numbers.len
}

Contracts, checked while compiling

What a function needs and what it gives are proven at every call, not asserted at run time.

@require numbers.len > 0, "there's a last one"
fn last(numbers: &[i32]) -> i32 = numbers[numbers.len - 1];

Errors are values

A function that can fail says so in its type. A failure has to be passed on or handled; it can’t be ignored.

fn parse_age(text: string) -> u32! {
    let age = u32::parse(text) else fail "not a number";
    if age > 150 { fail "{age} is too old"; }
    age
}

Any function runs while compiling

No macros: ordinary som runs in the compiler, and @for writes code out per field, variant or number.

fn fib(n: i64) -> i64 = n if n <= 1 else fib(n - 1) + fib(n - 2);

@const FIB_30 = fib(30);   // the program holds 832040

Shaders are som

A closure becomes a GPU shader, compiled to WGSL. What it captures become its uniforms.

let time = gpu.time();
sky.fullscreen(&mut gpu, (frag) => vec4(frag.uv, time.sin(), 1.0));

C libraries from their headers

A header is read into som and linked, with nothing written by hand. Then it builds for Linux, macOS, Windows and the web.

use extern "raylib";

init_window(800, 450, "hello");

A look at som

The snippets leave out use som::io;, which println comes from.

Errors are values

u32! is a u32, or a failure saying why there isn't one. A failure can't be ignored: it has to be passed on with ? or handled with else.

fn parse_age(text: string) -> u32! {
    let age = u32::parse(text) else fail "`{text}` is not a number";
    if age > 150 { fail "{age} is too old"; }
    age
}

/// `?` passes a failure on to the caller.
fn total_age(a: string, b: string) -> u32! = parse_age(a)? + parse_age(b)?;

let age = parse_age("old") else 0;          // a fallback: 0

let total = total_age("30", "200") else e => {
    println("error: {e}");                  // error: 200 is too old
    0
};

Indexing is proven, not checked

numbers[i] compiles only where i is known to be in range. There's no bounds check left to fail as the program runs.

fn third(numbers: &[i32]) -> i32 {
    if numbers.len < 3 { return 0; }
    numbers[2]              // fine: the `if` above proves 2 < numbers.len
}

fn at(numbers: &[i32], i: usize) -> i32 = numbers[i];
error: index `i` is possibly out of bounds
   6 │ fn at(numbers: &[i32], i: usize) -> i32 = numbers[i];
     │                                                   ━ nothing is known about `i` here
     ▪ use `numbers.get(i)`, which gives an optional

Contracts

@require says what a function needs from its callers, and every call is proven to meet it.

@require numbers.len > 0, "there's a last one"
fn last(numbers: &[i32]) -> i32 = numbers[numbers.len - 1];

let empty: [i32; 0] = [];
let x = last(&empty);
error: `(&empty).len > 0` is false here
   5 │ let x = last(&empty);
     │         ━━━━━━━━━━━━ `last` needs it: there's a last one

Ownership without a garbage collector

Each value has one owner. Passing it moves it, and & lends it instead.

fn total(numbers: List<i32>) -> i32 { … }

let numbers: List<i32> = [1, 2, 3];
let sum = total(numbers);
let again = total(numbers);
error: `numbers` is used after it was consumed
   3 │ let sum = total(numbers);
     │                 ─────── consumed here, gone after this point
   4 │ let again = total(numbers);
     │                   ━━━━━━━ tried to use here, but it's gone

Enums and match

A match has to cover every variant, and gives a value.

enum Shape {
    Circle(f64),
    Rect { w: f64, h: f64 },
}

fn area(shape: Shape) -> f64 = match shape {
    Circle(r) => 3.14 * r * r,
    Rect { w, h } => w * h,
};

println(area(Shape::Rect { w: 2.0, h: 3.0 }));   // 6

Code that runs while compiling

Any function can run while compiling. @for writes code out once per field, variant or number, which covers what other languages use macros for.

fn fib(n: i64) -> i64 = n if n <= 1 else fib(n - 1) + fib(n - 2);

@const FIB_30 = fib(30);   // worked out while compiling: the program only holds 832040

struct Point { x: i32, y: i32 }

/// Works for any struct.
fn describe<T>(value: &T) -> string {
    let mut text = "";
    @for f in T { text = text + "{@name(f)}: {value.f}\n"; }
    text
}

print(describe(&Point { x: 1, y: 2 }));   // x: 1
                                          // y: 2

Shaders in som

Shaders are written in som and compiled to WGSL while compiling. A closure can be a shader: what it captures becomes its uniforms.

use wgpu;

let mut gpu = wgpu::Gpu::open("waves", 800, 450);
let mut background = wgpu::Shader::empty();
while gpu.frame() {
    let time = gpu.time();
    // Runs on the GPU, once per pixel; `time` is sent along each frame.
    background.fullscreen(&mut gpu, (frag) => vec4(frag.uv.x, 0.5 + 0.5 * time.sin(), frag.uv.y, 1.0));
    gpu.present();
}

A @fragment function is compiled for the GPU too, and is still an ordinary function that can run and be tested on the CPU. som build --emit wgsl writes out the WGSL.

struct Waves { pub time: f32 }

@fragment
fn stripes(frag: Fragment, u: &Waves) -> Vec4 {
    let k = 0.5 + 0.5 * ((frag.uv.x + frag.uv.y) * 10.0 + u.time).sin();
    vec4(k, k * 0.5, 1.0 - k, 1.0)
}

C libraries, straight from their headers

use extern "raylib"; reads raylib.h, and links the library. Nothing is written by hand.

use extern "raylib";

init_window(800, 450, "hello");
while !window_should_close() {
    begin_drawing();
    clear_background(Color::RAYWHITE);
    draw_text("Hello from som", 300, 200, 20, Color::DARKGRAY);
    end_drawing();
}
close_window();

Getting started

Build the compiler with cargo build --release. That gives target/release/som, which needs a C compiler. It'll use zig cc if Zig is installed, or the system's cc ($SOM_CC overrides it).

som run hello          # a folder with a main.som; the current one if left out
som test hello         # runs its @test functions
som build hello        # an executable, in hello/.build
som run --target web hello

som lsp is the language server, and editors/vscode has an extension that uses it.

Learning som

  • A tour of som: the whole language, one feature at a time, from hello world to compile-time code
  • Errors: T!, fail, ? and else
  • Compile-time code: @const, @for, @if and reflection
  • dyn: values whose type is known only as the program runs
  • The web: building for the browser
  • Shaders: vertex and fragment shaders and compute kernels, written in som
  • example/: bigger programs, from tetris to a path tracer

Edit this page on GitHub