Skip to content

Stdlib and Capabilities

w4ffl35 edited this page Aug 24, 2026 · 9 revisions

Curlee stdlib + builtins (MVP design)

This document defines the MVP boundary between builtins and stdlib modules, and shows how capability-bearing APIs work without ambient authority (see Glossary).

Goals

  • Keep the runtime minimal and deterministic.
  • Preserve verification-first guarantees: no hidden effects.
  • Require explicit capabilities for any I/O or system effects.

Builtins vs stdlib

Builtins (compiler/VM level)

Builtins are minimal, hard-coded operations needed to bootstrap the language:

  • print(expr) (MVP builtin; see capability gating below)
  • string concatenation via String + String
  • arithmetic ops (+, -, *, comparisons)

Rules:

  • Builtins must not have ambient authority. If a builtin triggers an effect (like stdout), the VM/runner enforces an explicit capability check.
  • Builtins should be deterministic and side-effect free by default.

Stdlib modules (imported)

Stdlib modules are regular Curlee modules that expose APIs gated by capabilities.

Minimal v1 modules (defined):

  • std.math
    • fn add1(x: Int) -> Int
    • fn sub1(x: Int) -> Int
  • std.io
    • fn identity_int(value: Int, out: cap io.stdout) -> Int

Reference implementation lives under stdlib/v1/std/ in the repository.

Stdlib modules are loaded using the same deterministic import rules as user modules.

Capability-bearing APIs

Any effectful API requires a capability value (opaque token) passed explicitly.

Example (v1 syntax):

fn main(out: cap io.stdout) -> Unit {
  print("hello");
  return;
}

Notes:

  • cap <name> is a capability type, where <name> is a dotted identifier path like io.stdout.
  • Capability values are not constructible in user code.
  • The host/runner supplies authority via CLI flags (curlee run --cap ...).

V1 capability model (implemented)

Curlee uses explicit capability tokens to prevent ambient authority.

V1 capability surface decision (issue #257)

The v1-forbidden list contains exactly the Python-interop capabilities:

  • python.ffi (forbidden)
  • python.sandbox (forbidden)

phys.mem is not v1-forbidden: it is a real freestanding capability granting Phys<T> read()/write() MMIO access (used by the freestanding-c build target). It cannot be executed by the VM (Phys is freestanding-only, so curlee run rejects such programs), but that restriction is enforced by the front-end/emitter, not by the capability gate. Declaring cap phys.mem in a bundle manifest is therefore allowed.

Syntax

  • Capability types are written as cap <qualified-name>.
  • Qualified names are dotted identifiers (for example io.stdout).
  • Whitespace is not allowed around dots in qualified names (e.g. cap io. stdout is rejected).

Typing rules

  • Capability values are opaque and only flow via variables/parameters/returns.
  • Effectful operations require a capability-typed value to be in scope:
    • print(expr) requires a value of type cap io.stdout.
  • If a required capability value is not in scope, type checking fails with a precise-span error:
    • missing capability value '<cap>' (add a parameter of type cap <cap>)

Runtime representation

  • Capability values are represented as Unit at runtime (they carry no data).
  • Runtime authority is enforced by the VM using the host capability set passed in from the CLI.

Entry points:

  • The runnable entry point may take capability parameters.
  • main parameters are restricted to capability types in runnable code; the runtime synthesizes default capability values for those parameters.

CLI grants (curlee run --cap)

  • curlee run --cap <name> grants a host capability string exactly matching the capability type name (e.g. io.stdout).
  • curlee run checks required vs granted capabilities before execution and emits an error at the use site when a capability is not granted.

Bundling

  • Bundles declare required capabilities in the manifest (see Bundles).
  • Bundle execution is rejected deterministically if a required capability is not granted.
  • v1 guardrail: bundles that declare python.ffi or python.sandbox are rejected.

Current MVP behavior (implemented)

  • The VM Print instruction is gated by the io.stdout capability.
  • The CLI/runner grants capabilities explicitly (e.g. curlee run --cap io.stdout ...).
  • print(expr) compiles to Print and returns Unit.

Minimal stdlib signatures (v1)

These signatures are part of the v1 baseline surface and should remain stable unless explicitly revised:

// std/math.curlee
fn add1(x: Int) -> Int
fn sub1(x: Int) -> Int

// std/io.curlee
fn identity_int(value: Int, out: cap io.stdout) -> Int

Safety constraints for these modules:

  • std.io.identity_int requires explicit cap io.stdout in its signature.
  • No ambient capabilities are introduced by stdlib imports.

Python interop is de-scoped from v1 surface and documented as future work in Python Interop (Future).

Module format + loading (MVP)

  • Modules are resolved using import (see Modules-and-Imports).
  • Deterministic, local-only resolution: imports resolve relative to the entry source file / entry directory (no implicit stdlib search path yet).
  • Import cycles are rejected.
  • No network access or dynamic resolution.

String concatenation policy (MVP)

  • String + String is allowed and deterministic.
  • String + Int is not allowed (no implicit conversion).
  • Formatting should go through explicit stdlib APIs (future).

Status

  • Implemented:
    • print(expr) as a builtin that emits Print (capability-gated by io.stdout).
    • string literals in the emitter (minimal escape decoding).
    • String + String concatenation.

Clone this wiki locally