Effects (20a: design, and generators checked)
What an effect is in Trine
Trine already tracks the effects that touch the outside world, as capabilities: a function can only read files, input or the clock if it is handed a Sys, a Dir or a Py (see CAPABILITIES.md). What remains are control effects: a function that pauses and resumes. Trine has two, and both are written in the signature, so a reader (or an agent) sees them without reading the body:
| effect | written | type of f | handled by |
|---|---|---|---|
| generator | fn f(n) yields Int: + yield x | (Int) -> Gen[Int] | for x in f(n) |
| async | async fn f(...) + await e | a call gives Task[T] | scoped tasks (20d, PARALLEL.md) |
Design rules:
- Effects are visible in the type. A generator's type is
(...) -> Gen[T]; it is an ordinary value (pass it, store it, loop over it), so there is no hidden effect polymorphism:map-like code doesn't need effect variables, because a generator is just a value of typeGen[T]. - **
yieldonly where declared.** Usingyieldin a function withoutyields Tis an error with a certain fix (trine fix --applyaddsyields T, T inferred from the yielded value). - Lambdas don't yield. A lambda isn't a generator, so
yieldcan't hide inside a callback. - Generators return nothing.
-> Ton a generator is an error; values come fromyield. - Capabilities stay orthogonal. A generator that reads files takes a
Dirlike any other function; resuming it can only use what it was given.
Status
- 20a: syntax, types, errors and the fix.
trine check,caps,lsp,dochandle generators. - 20b: the interpreter runs them.
- 20c: the VM and native code run them too, with the same output, errors and step counts (
--max-stepsstops at the same place on every engine).
How the interpreter runs generators (20b)
A generator value is just the function and its arguments: nothing runs until a for loops over it, and every for starts it afresh (a generator is a value, like a list; looping twice runs the body twice, side effects included). for x in g: body runs g's body on the same Rust stack and treats yield v as "run the loop body now, with x = v": the loop's own variables are swapped back in for the body and out again after. The two interleave exactly as written (output from the generator and the loop alternate), generators can loop over other generators and over themselves recursively, and infinite generators are fine. Trine has no break, so the only early exits are errors (they just propagate) and return in the loop body: it unwinds through the generator's frames tagged with that loop's id, and becomes the enclosing function's return. Each yielded value is one step (--max-steps), like an element of a list loop.
How the VM and native code run them (20c)
Both engines run the ownership IR, so generators are a lowering (ir.rs, RetMode), in two forms:
- Inlined (the common case):
for x in g(args)wheregis a generator that can't leave early (noreturn, no?) and doesn't itself loop over a generator. Lowering does what the interpreter does at run time: it lowersg's body right there in its own scope, and at eachyield vlowers the loop body in the loop's scope withx = v. The result is an ordinary loop: no closure, no allocation. Summing 10M values from awhilegenerator takes 10 ms native, the same asfor x in range(...). - General (generator values in variables or parameters, recursive generators, early
return): calling the generator returns a closure over its arguments. The closure's function, the driver, takes the loop body#kand a state#st. The loop body becomes a function(captures, state, x) -> state, where the state is a list[done, return value, variables the body assigns...]. Values move in and out of it withtake, so they stay unique and in-place updates still happen. The list is updated in place, so ayieldallocates nothing. Areturnin the body setsdone: the driver stops at theyieldthat sent the value, and the loop's function returns. The same sum through aGen[Int]parameter takes 258 ms native: one indirect call per element.
Steps match the interpreter: the call that makes a generator is one step, each yielded value is one, and the end of the loop is one; the driver itself is uncounted. Writing this also fixed a long-standing difference: VM and native loop steps used to be reported at position 0:0. They are now reported where the interpreter reports them (the loop's test).
A function whose parameter is only looped over takes anything iterable: its type gets an Iter[T] requirement (total : (a) -> Int where a: Iter[Int]), met by List[T], Set[T], Gen[T] and Map[T, v] (its keys). Each call at a known type gets its own specialized copy, with an ordinary list loop, an inlined generator, or a map's keys. Where the type is only known at run time (a function value, for example), the loop checks once whether it was given a generator, and otherwise loops over the value as a list.
- 20c: VM and native (state-machine lowering: locals live across
yieldgo into a heap frame;for x in gen(...)with a non-escaping generator inlines into a loop with no allocation). - 20d:
async fn/await(done, M4): a call to an async function starts a scoped task (the arguments are evaluated first and follow the isolation rules),await twaits for it. No colouring: async bodies are ordinary code. See PARALLEL.md.