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:

effectwrittentype of fhandled by
generatorfn f(n) yields Int: + yield x(Int) -> Gen[Int]for x in f(n)
asyncasync fn f(...) + await ea call gives Task[T]scoped tasks (20d, PARALLEL.md)

Design rules:

  1. 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 type Gen[T].
  2. **yield only where declared.** Using yield in a function without yields T is an error with a certain fix (trine fix --apply adds yields T, T inferred from the yielded value).
  3. Lambdas don't yield. A lambda isn't a generator, so yield can't hide inside a callback.
  4. Generators return nothing. -> T on a generator is an error; values come from yield.
  5. Capabilities stay orthogonal. A generator that reads files takes a Dir like any other function; resuming it can only use what it was given.

Status

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:

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.