
louise
Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.
ADR 0008: Effect typing
Status: Accepted — 2026-10-08, reviewed and accepted by the project owner (issue #2).
Partly superseded by ADR 0010 (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.
maskwill 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). Handlers remove effects. Effect operations are resolved dynamically to the innermost handler for their effect (ADR 0003). The type system must keep that dynamic choice consistent with the static types.
Decision
Rows
- An effect row is
<l1, ..., ln | tail>.- Each label is an effect applied to its type arguments, e.g.
state<Int>. - The tail is empty (a closed, exact row) or a row variable (an open row).
- Each label is an effect applied to its type arguments, e.g.
- 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
handlebodies. 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, notfn(a) -> a / <e>. - Lambdas have their own row, taken from the expected function type if there is one.
- Handlers:
handle { body } with { clauses }handles exactly one effectL.- The body runs with
<L | outer>; the clauses run withouter. - A
fnclause produces the operation's result. - A
ctlclause produces the handler's result and may callresume: fn(op_result) -> R / outer. - A
final ctlclause produces the handler's result. - Every operation of
Lmust be handled.
- The body runs with
- Signatures: an omitted row means total (
<>).- In a row, a name like
eore1(one letter, optionally followed by digits) is the row variable and must come last:<io, e>means<io | e>. - 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.
- In a row, a name like
- Built-in effects
io,ffiandstare handled by the runtime aroundmain. Creating, reading or writing aRefperformsst.
Why equality, not inclusion
Take fn f(g: fn() -> () / <e>) -> () / <state<Int>, e> with a body that calls g() and then uses state<Int> itself.
If e is instantiated with state<String>, then g's get() finds the innermost state handler at runtime. That is the state<Int> 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() -> () / <state<Int>, 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<a>, fn(a) -> b / <e>) -> List<b> / <e>. - Adding
masklater only accepts more programs, so existing code keeps working.