13.4. Effects

An effectful type <E> T denotes a suspended computation: a value that, when run, may perform the effects in E and yields a T. This section specifies what the type checker requires of suspensions, when one is run, and how code generation turns them into closures of no arguments. It also covers how they are reconciled at every point where a value meets a calling convention that has no suspensions, so that each computation runs exactly as often, and in the order, the program says, in every language. The user-facing description is in Effects and delayed evaluation.

A suspension is a value. <E> T and T are different types, and no pass erases, strips or reinterprets the effect layer. The model is call-by-push-value: a suspension is a thunk, and the only way from <E> T to T is to run it. Holding, passing, returning, projecting or storing a suspension in a record, list or optional runs nothing. Every run runs the computation again. The compiler never shares, hoists, memoizes, duplicates or drops a run. This is the one place where morloc’s freedom to reorder and share work stops (Term sharing and inlining).

Effect labels are tags. The compiler gives a label no operational meaning. It propagates labels and checks that they are covered. What IO or Rand permits is decided by the library that declares it. Effects come from the signatures of sourced functions. Such a row is the author’s claim, which the compiler takes as given because it cannot look inside foreign code. Every other row is inferred by composition and checked against the signature that declares it.

Typing. Rows enter the bidirectional checker (Type system) through subtyping:

Rule Meaning

Inclusion

<E1> T1 <: <E2> T2 when E1 is a subset of E2 and T1 <: T2. Label order is irrelevant and duplicates collapse. Widening is accepted and narrowing is rejected.

No coercion

A T never fills an <E> T slot, and an <E> T never fills a T slot. do v is the only lift, and it has type <> T, which fits any row.

Body fits signature

The effects a definition’s body runs must be a subset of the row in its signature. Declaring more than the body runs is legal.

Layers stay apart

<E> (<E'> T) is two suspensions, never <E,E'> T.

Row variables

A row may end in a variable standing for unknown further labels, so combinators can thread a callback’s effects into their result. An unconstrained row variable is <> at ground.

Building and running. A do block builds one suspension from its statements. Only a bind runs a suspension: x ← t, or a bare statement t. Application, let, projection, passing and returning run nothing. !e is sugar for a bind inserted above the statement that contains it, with a fresh name in its place. The front end hoists every ! into an explicit bind and desugars each block into nested binds, so later passes see only two operations: suspend (build a thunk around an expression) and force (run a thunk once). A force applied directly to a suspend cancels.

Evaluation order. Pure work has no order: the compiler may move, parallelize or reorder it, including the arguments of one call. The only order the compiler must keep is the order of effect statements in a do block. A pure let in a block is never computed before an effect statement written above it. A pure argument is evaluated once, at the application. When a suspension is built from a call whose arguments still need work, the arguments are computed outside it and the suspension captures their values, so a second run repeats the call and nothing else.

Failure is not an effect. A fallible operation returns Try e a, an ordinary data type. @throw abandons the computation and carries no row. @try :: <e> a → <e> (Try Str a) catches every recoverable failure that escapes its argument, whichever language raised it, and passes the row through unchanged. Internal errors and interrupts bypass it. See Failure is not an effect.

Effect boundaries. After realization and segmentation (Realization, Segmentation and cross-language calls), every position in a pool’s tree sits under a boundary that dictates a calling convention: a plain value or the suspension itself. A boundary pass compares each position’s declared type with its boundary and inserts a force or a suspend wherever they differ by a layer:

Boundary Expects Reconciliation

Return of a command

plain

The root of a command whose result is <E> T is forced once, so the wire carries the T the caller asked for.

Argument of a command

suspension

The caller of a program holds values, not thunks. An argument declared <E> T arrives as a T, and each reference to it becomes a suspension with a constant result.

Sourced call

suspension

Foreign functions are eager. When a source is declared to return <E> T, the host call becomes the body of a suspension, so running the suspension calls the host function.

Callback into a host

plain

A morloc function handed to a host at an A → <E> C slot is wrapped as \a → force (g a), because the host calls it and expects the effect to happen.

Host function into morloc

suspension

A host function received at that type is wrapped to build the suspension the type promises.

Cross-pool call, callee side

plain

The callee’s entry point runs one layer, and the result travels over the wire.

Cross-pool call, caller side

suspension

The caller holds the remote call as a suspension, so forcing it makes the call.

Wire serializer

either

A suspension passed as data crosses as a closure that calls back to its home pool once per run (Serialization).

Host conversion follows the declared type to any depth: function arguments (contravariantly), results, record fields, tuple and list elements, optionals, and packable types. Under a type variable of the source’s signature the host holds the value opaquely, so nothing there is converted. A sourced map :: (a → b) → f a → f b used at b = <IO> C builds suspensions and does not run them. A value that cannot be rebuilt at its type is refused at build time with an error. After boundaries are placed, no later pass adds or removes a force. Passes may move and rename code, but the number of runs on every path is fixed.

Lowering. A suspend becomes a closure of no arguments. Its captured variables are the expression’s free variables, copied into the closure when it is built. A force becomes a call of that closure with no arguments. A suspension therefore shares every code path a closure already has. It can be stored in an aggregate, captured, passed to a foreign function as a callable, and sent across pools (Code generation). An intrinsic whose result is declared as a suspension (@save, @try and the like) is the eager runtime call. Forcing it calls it, and holding it builds a closure that makes the call.

Diagram
Diagram