Examples

Short examples from real Chelis source

These examples are curated from source files rather than invented for the page. Complete listings are marked as complete; shorter excerpts are marked as source slices and should be read as idioms from a larger file.

To run a complete listing: get a prebuilt binary from the Chelis-Lang GitHub org, put chelis on your PATH, save the listing to a .ch or .dp file, then run chelis check file.ch or chelis surf file.dp.

Add two named-dimension tensors

A complete program with a polymorphic dimension. The two literal tensors share n, and the result keeps that dimension.

Surf Complete source: chelis/examples/hello_tensor.ch
module HelloTensor
def main() -> tensor[n, f32] = {
  a = to_tensor([1.0, 2.0, 3.0])
  b = to_tensor([4.0, 5.0, 6.0])
  out = add(a, b)
  out
}

Pipe activation stages left to right

The pipe operator keeps tensor flow readable: the value moves through relu, then sigmoid, without nesting the calls inside-out.

Surf Source slice: basics/pipeandmatch.ch
def pipeline(x: &tensor[n, f32]) -> tensor[n, f32] = x |> relu |> sigmoid

Vectorize over a batch axis

vmap lifts a feature-vector function over a batch dimension. The axis choice is explicit, and the dimension names remain part of the type.

Surf Complete source: chelis/examples/vmap_relu.ch
def process(x: tensor[features, f32]) -> tensor[features, f32] = relu(x)
def batch_process(xs: tensor[batch, features, f32]) -> tensor[batch, features, f32] = xs |> vmap(process, axis=0)

Linear layer with bias

A compact model step: rows pass through matmul, then a broadcast bias add. Shape mismatches are caught before backend code is generated.

Surf Source slice: capstone/linreg.ch
def predict(x: tensor[64, 64, f32], w: tensor[64, 1, f32], b: tensor[1, f32]) -> tensor[64, 1, f32] = x |> matmul(w) |> add(expand(b, 0, 64))

Transformer attention and feed-forward flow

The attention output is piped through two matrix multiplies. The sequence dimension stays named while the model and head widths are checked throughout.

Surf Source slice: capstone/transformerblock.ch
q = matmul(x, wq)
k = matmul(x, wk)
v = matmul(x, wv)
scores = matmul(q, permute(k, 1, 0))
probs = softmax(scores, 1)
attn_out = probs |> matmul(v) |> matmul(wo)
resid1 = add(x, attn_out)
ff_out = matmul(matmul(resid1, ff1), ff2)
add(resid1, ff_out)

Deep canonical form

Deep is the machine-facing s-expression for the same language. Tooling can inspect this stable tree after Surf has been parsed.

Deep Complete source: chelis-deep/tests/fixtures/pipeline.dp
(def {}
  process
  (fn {}
    (params {} x)
    (pipe {} (var {} x) (var {} normalize) (var {} relu) (var {} softmax))))

The language tour explains how Surf maps to Deep, and the docs cover the full surface.