# ADR 0008: Effect typing **Status:** Accepted — 2026-10-08, reviewed and accepted by the project owner (issue #2). > **Partly superseded by [ADR 0010](0010-lexical-handlers.md)** (lexically scoped handlers). Rows, opening, closing, handlers and signatures are unchanged. Calls now use *inclusion*, not equality, which is sound because operations are routed by handler instance. `mask` will not be added. The sections "Why equality, not inclusion" and "Idiom and limitation" describe the dynamically scoped design and are kept for the record. ## Context Every function type carries the effects a call may perform ([ADR 0001](0001-language-surface.md)). Handlers remove effects. Effect operations are resolved dynamically to the *innermost* handler for their effect ([ADR 0003](0003-effect-lowering.md)). The type system must keep that dynamic choice consistent with the static types. ## Decision ### Rows - An effect row is ``. - Each label is an effect applied to its type arguments, e.g. `state`. - The tail is empty (a *closed*, exact row) or a row variable (an *open* row). - **Labels are scoped** (Leijen 2005): duplicates are allowed and ordered, and the leftmost one is the innermost. - **Unification** rewrites one row to bring forward a label with the same effect, unifies the labels, then unifies the remaining rows. An open tail is extended as needed. The usual side condition on the tail prevents infinite rows. ### Rules - **Calls:** a call's effects must *equal* the current row, which is the enclosing function's row, extended inside `handle` bodies. Inclusion is not enough (see below). - **Opening:** at each use of a function value whose row is closed, the row gets a fresh tail (Koka's *open*). A function with fewer *known* effects can therefore be called anywhere. - **Closing:** when a function's type is generalized, a row variable that only occurs as the tail of its own row is removed (Koka's *close*). This keeps displayed types readable: `fn(a) -> a`, not `fn(a) -> a / `. - **Lambdas** have their own row, taken from the expected function type if there is one. - **Handlers:** `handle { body } with { clauses }` handles exactly one effect `L`. - The body runs with ``; the clauses run with `outer`. - A `fn` clause produces the operation's result. - A `ctl` clause produces the handler's result and may call `resume: fn(op_result) -> R / outer`. - A `final ctl` clause produces the handler's result. - Every operation of `L` must be handled. - **Signatures:** an omitted row means total (`<>`). - In a row, a name like `e` or `e1` (one letter, optionally followed by digits) is the row variable and must come last: `` means ``. - Any other unknown name is an error: it is almost always a missing effect import, so it must not silently become a variable. After `|`, any name is a variable. - This applies to externs too: declaring one total is the binding author's decision. - **Built-in effects** `io`, `ffi` and `st` are handled by the runtime around `main`. Creating, reading or writing a `Ref` performs `st`. ### Why equality, not inclusion Take `fn f(g: fn() -> () / ) -> () / , e>` with a body that calls `g()` and then uses `state` itself. If `e` is instantiated with `state`, then `g`'s `get()` finds the innermost `state` handler at runtime. That is the `state` one, so `g` receives an `Int` where its type says `String`. Requiring the callee's row to equal the current row rejects this program. Koka does the same. ### Idiom and limitation **The idiom:** a callback gets its caller's row, `g: fn() -> () / , e>`. Pure callbacks still fit, because of opening. **The limitation:** the case above needs `mask`, which makes an operation skip the innermost handler of its label. `mask` is **deferred** past v0: it needs syntax for naming a label, and the v0 demo does not need it. The diagnostic for this case tells users the idiom. ## Consequences - Effect inference needs no annotations for private code, and higher-order functions are effect-polymorphic automatically. - The type a user sees for a higher-order function includes its row variable, e.g. `map: fn(List, fn(a) -> b / ) -> List / `. - Adding `mask` later only *accepts more programs*, so existing code keeps working.