feat(pbs): implement first-class functions and explicit captures

This commit is contained in:
2026-10-17 01:27:52 +01:00
parent 39df63581c
commit 2a418085d2
55 changed files with 996 additions and 459 deletions
@@ -67,7 +67,7 @@ This document integrates the following accepted decisions:
The following inputs are already fixed elsewhere and must not be contradicted here:
- PBS prioritizes determinism and compatibility over ergonomics when the two conflict.
- `FRAME_SYNC`-based execution semantics remain preserved.
- The existing synthetic frame lifecycle remains preserved; PBS exposes no sync or coroutine construct.
- No non-deterministic async execution model is part of PBS v1.
- Function application is defined on the canonical `apply` surface, with call sugar desugaring to `apply`.
- `optional T` function fallthrough yields `none`.
@@ -93,7 +93,7 @@ User-visible sequencing is defined by:
VM-internal frame layout, temporary storage, and lowering steps are not observable unless they change one of the semantic effects above.
`FRAME_SYNC` is the only normative safepoint in PBS v1.
PBS uses the existing synthetic frame lifecycle. Runtime GC opportunities, including successful root return, follow the runtime contract; there is no PBS sync statement.
## 6.1 Lifecycle bootstrap
@@ -161,7 +161,7 @@ The following expressions are evaluated exactly once where they appear:
- the payload expression of `some(expr)`,
- the operand of `expr!`,
- the operand of `handle expr { ... }`,
- the context expression of `bind(context, fn_name)`,
- explicit closure capture values,
- call receivers, callees, and argument expressions as required by the call rule above.
## 8. Blocks and Abrupt Completion
@@ -253,7 +253,7 @@ For the remap-only case, a short form such as `E1.case -> E2.case` is sugar for
`handle` does not intercept or convert traps.
## 11. `apply`, Call Sugar, and `bind`
## 11. Function application and closures
### 11.1 Canonical role of `apply`
@@ -279,22 +279,9 @@ If a callable returns:
- `optional<P>`, extraction remains explicit through `else`,
- `result<E> P`, propagation or processing remains explicit through `!` or `handle`.
### 11.3 `bind`
### 11.3 Closure creation and invocation
`bind(context, fn_name)` is the explicit callback-formation mechanism in PBS v1.
It:
- evaluates `context` exactly once,
- captures the same struct context identity without copying it,
- attaches that context to the resolved top-level function target,
- produces a callback value whose invocation behaves as if the target function were called with the captured context as its first argument.
At the language-semantics level, `bind` is trap-free.
Incompatible binding shape remains a compile-time matter rather than a runtime failure mode.
Retention and storage consequences of `bind` belong to `Memory and Lifetime Specification.md`.
Explicit capture lists snapshot values at creation. Object references retain identity. Calls complete within the current frame; objects may remain reachable between frames. See [First-class functions and explicit value captures](../../../compiler/pbs/specs/first-class-functions-and-captures.md).
## 12. Branch Selection