Repository navigation
Linear Types (Future)
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.
- Prevent misuse of single-use resources (files, sockets, capabilities).
- Preserve deterministic, verification-first execution.
- Make ownership transfer explicit and machine-checkable.
- Linear values must be used exactly once.
- Owned values can be moved but not copied.
- Borrowed references can be read without consuming ownership.
- 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).
- 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.
Capabilities are already opaque and explicit in Curlee. Linear types would refine them:
-
cap std.fscould be linear, forcing explicit transfer and preventing accidental reuse. - A function that consumes a capability must return a new capability (or end the effect).
This section proposes a concrete surface syntax that keeps Curlee’s “explicit delimiters” philosophy. It is a proposal, not an MVP commitment.
-
lin Tmarks a type as linear. -
&Tis 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;
- Passing a
linparameter consumes it. - Returning a
linvalue 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 {
/* ... */
}
The type checker tracks linear resources as obligations that must be discharged on every control-flow path.
- A
letbinding of a linear value introduces an obligation: the variable must be consumed exactly once. - Any use that transfers ownership (passing to a
linparameter, returning, assigning into a linear slot) consumes the source variable. - After consumption, the variable is unavailable.
- If
Tislin, then operations that duplicate values are rejected. - Assigning
xinto two places, or returningxwhile also using it, is rejected.
-
&xis allowed only whilexis not consumed. -
&Tdoes not discharge the linear obligation forx. - 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.
- 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.
- A block must not exit (by
}orreturn) with any unconsumed linear locals. - Linear values may be moved out of a block via
returnor by producing them as the block’s result (if/when Curlee adopts expression blocks).
- 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 are tricky for linear obligations.
Conservative future rule (keeps it decidable and easy to implement):
- Linear variables may not be consumed inside a
whileloop 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.
- 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;
}
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)”
- 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.
- Design-only milestone.
- Requires updates to wiki/Verification-Scope.md before implementation.