Skip to content

Linear Types (Future)

Joe Curlee (w4ffl35) edited this page Jan 31, 2026 · 3 revisions

Linear/ownership types (future design)

This document sketches a future linear/ownership type system for Curlee.

It is not implemented and is out of scope for the MVP.

This page is a design target only. Until an explicit feature gate is defined and implemented, any linear-type syntax described here should be treated as rejected by the compiler.

Goals

  • Prevent misuse of single-use resources (files, sockets, capabilities).
  • Preserve deterministic, verification-first execution.
  • Make ownership transfer explicit and machine-checkable.

Core ideas

  • Linear values must be used exactly once.
  • Owned values can be moved but not copied.
  • Borrowed references can be read without consuming ownership.

Non-goals (initially)

  • Borrow checking / aliasing rules beyond a minimal, read-only borrow.
  • Region/lifetime inference.
  • Linear reasoning inside the verifier/solver (this is a type-system check).

Terminology

  • Linear: must be consumed exactly once.
  • Owned (move-only): may be moved (transferred), but not copied.
  • Borrowed: an alias that does not transfer ownership.

Note: some languages use “affine” (used at most once) vs “linear” (used exactly once). This design intentionally targets linear semantics for resources where leaks and double-use should be rejected.

Interaction with capabilities

Capabilities are already opaque and explicit in Curlee. Linear types would refine them:

  • cap std.fs could be linear, forcing explicit transfer and preventing accidental reuse.
  • A function that consumes a capability must return a new capability (or end the effect).

Proposed surface syntax (future)

This section proposes a concrete surface syntax that keeps Curlee’s “explicit delimiters” philosophy. It is a proposal, not an MVP commitment.

Type modifiers

  • lin T marks a type as linear.
  • &T is a read-only borrowed reference.

Examples:

let fs: lin cap std.fs = /* host-supplied */;
let file: lin File = open("/tmp/x", fs).0;
let file_ref: &File = &file;

Function signatures

  • Passing a lin parameter consumes it.
  • Returning a lin value transfers ownership to the caller.

When an operation uses a linear capability, it should return the updated capability:

fn open(path: String, fs: lin cap std.fs) -> (lin File, lin cap std.fs) {
  /* ... */
}

fn close(file: lin File, fs: lin cap std.fs) -> lin cap std.fs {
  /* ... */
}

Typing rules (draft)

The type checker tracks linear resources as obligations that must be discharged on every control-flow path.

Bindings and moves

  • A let binding of a linear value introduces an obligation: the variable must be consumed exactly once.
  • Any use that transfers ownership (passing to a lin parameter, returning, assigning into a linear slot) consumes the source variable.
  • After consumption, the variable is unavailable.

Copy is forbidden

  • If T is lin, then operations that duplicate values are rejected.
  • Assigning x into two places, or returning x while also using it, is rejected.

Borrowing (read-only, minimal)

  • &x is allowed only while x is not consumed.
  • &T does not discharge the linear obligation for x.
  • The MVP-compatible restriction for a first implementation would be to keep borrows strictly within an expression / statement (no escaping borrows), and reject cases requiring lifetime inference.

Function calls

  • If a function parameter is lin T, providing an argument consumes the argument variable.
  • If a function parameter is &T, providing an argument does not consume it.

Blocks and scope

  • A block must not exit (by } or return) with any unconsumed linear locals.
  • Linear values may be moved out of a block via return or by producing them as the block’s result (if/when Curlee adopts expression blocks).

Conditionals

  • For if/else, linear obligations must match across both branches:
    • Any linear variable consumed in one branch must be consumed in the other.
    • Any linear value produced/returned from one branch must be produced/returned from the other with the same type.

This prevents “maybe-consumed” states.

Loops

Loops are tricky for linear obligations.

Conservative future rule (keeps it decidable and easy to implement):

  • Linear variables may not be consumed inside a while loop unless the loop body also re-establishes the obligation on every iteration (e.g., by re-acquiring a fresh linear capability and assigning it back).

An initial implementation can simply reject consumption of linear variables inside loops with a deterministic diagnostic.

Interaction with returns

  • Returning a linear value consumes it (ownership transfers to the caller).
  • A function that takes a linear parameter must either:
    • return it (possibly updated), or
    • consume it via another linear-consuming operation (e.g., close).

Example:

fn use_then_close(file: lin File, fs: lin cap std.fs) -> lin cap std.fs {
  // use(file, fs) would consume and return updated values
  let fs2: lin cap std.fs = close(file, fs);
  return fs2;
}

Diagnostics (expectations)

If/when implemented, linear-type errors should be deterministic and span-precise. Common cases:

  • “linear value not consumed on all paths”
  • “linear value consumed more than once”
  • “cannot copy linear value”
  • “linear value consumed in loop (not supported yet)”

Ownership rules (draft)

  • Move-by-default for linear values.
  • Copy is disallowed for linear values.
  • Borrowing requires explicit & and starts as read-only in an initial implementation.
  • Dropping linear values without consuming them is an error.

Status

  • Design-only milestone.
  • Requires updates to wiki/Verification-Scope.md before implementation.

Clone this wiki locally