13.7. Segmentation and cross-language calls

Realization leaves one tree per exported function with a language on every node (Realization). This section specifies how such a tree becomes code that runs in several pools: the callable units the compiler builds (manifolds), how the tree is cut into one segment per language change, what the calling side and the receiving side of a cut do at run time, and how recursion and tail calls are compiled so that their depth is bounded only by the target language. The packet format and the worker model that carry these calls are in Packet protocol and Worker model.

Manifolds. A manifold is the compiler’s unit of callable code: a function body with its parameters, identified by an integer index, its mid, that is unique across the whole program. Every exported command is a manifold, and the manifest records each command’s mid and pool (Compilation artifacts). A manifold takes one of three forms:

Form Meaning

Full

A saturated call that produces a value.

Partial

A closure: some arguments are captured from the enclosing scope (its context), the rest are supplied when it is called. A closure of no remaining arguments is a suspension (Effects).

Pass

An unapplied function handed to another function as a value; the receiver supplies every argument.

Parameterization gives each manifold exactly the enclosing arguments its body uses: an argument a subtree never reads is not passed into it. This is what lets a subtree be moved into another process with a small, explicit interface.

Expression. Each realized tree is then rewritten into a per-language intermediate form. Every node gets its concrete type in its own language (aliases resolved, type parameters instantiated), and every edge where a child’s language differs from its parent’s is marked as a call interface naming the target language, the result type as the caller sees it, and the arguments that cross. Two languages that share a pool, such as a guest language in its host’s pool, get no interface: their call is an ordinary in-process call.

Segmentation. The tree is cut at every call interface. The subtree below a cut becomes the body of a new top-level manifold in the target language, under the mid its caller will name; in the caller, the subtree is replaced by a call stub. Cutting recurses, so a subtree can itself contain cuts back into the caller’s language. A pool is then the set of segments of one language, and a program needs one pool per language that owns a segment.

For example, with inc sourced from Python and dbl from C++, foo x = inc (dbl (inc x)) realizes as below; the dashed edges are cuts:

Diagram
Diagram

The inner inc runs in the Python pool as a callback from C++: a segment is a subtree, so an argument whose producer lives in another language is reached by a call from the consumer, not computed beforehand by the caller.

The calling side. A stub serializes each argument to a data packet (Serialization), sends a call packet carrying the target mid to the target pool’s socket, and blocks until the single reply arrives. It then decodes the result into a native value and releases the reply’s shared memory. An argument that is already in serialized form, such as a command argument the nexus sent, is forwarded without being decoded. A call that the build configures to run as a cluster job is not sent to a local pool; it becomes a call packet that a pool on the cluster node reads from a file (Packet protocol), and such calls have a dispatch table of their own.

The receiving side. Every pool has a dispatch table from mid to the entry of each segment it holds. A worker that reads a call packet looks the mid up, decodes the arguments, runs the segment natively, serializes the result and replies with one data packet. A failure inside the segment becomes an error packet whose message carries the cross-language traceback (Error propagation). An unknown mid is answered with an error, never with silence. Because calls nest freely (Python calls C++, which calls back into Python), a pool must be able to serve a call while its other workers wait on another pool; Worker model gives the rule that guarantees it.

Function values. A function value that crosses a cut travels as its home language, the mid of its manifold, and its captured values in serialized form. The receiving language wraps it in a native callable. Calling that callable sends a call packet to the home pool with the captured values followed by the new arguments, so the function always runs in the language it was built in. A suspension crosses the same way, as a callable of no arguments that runs its computation each time it is forced.

Mutual recursion. Morloc-defined terms are expanded at their use, so a cycle of definitions such as isEven and isOdd becomes one recursive function, rooted at the term where the cycle is entered, whose only back-edges call itself. Realization places the whole cycle in one pool (Realization). A recursive helper defined in a where block is lifted into a manifold of its own, and the values it captures become extra parameters.

Tail calls become loops. When every back-edge of a recursive manifold is in tail position, the manifold is compiled to a loop in Python, R, C++ and Rust: its parameters become loop variables, each tail call reassigns them, and each base case returns. Tail position reaches through conditional branches, match arms, let bodies and do blocks. Such a recursion runs in constant stack space at any depth, even in languages that do not optimize tail calls. Three cases keep the recursion as real calls: a labeled tail call, whose hooks need a call of its own; a build with --debug, which keeps one frame per level for the trace; and an effectful recursion whose result is returned suspended rather than run.

Native entries. A recursive manifold is a pool entry, which takes and returns packets. A caller in the same pool would pay a serialization round trip on every level, quadratic in the size of a structure walked recursively. So a recursive manifold reached from its own pool gets a second, native entry that takes and returns native values, and same-pool calls use it. The serial entry keeps the original mid for callers in other pools and for the dispatch table. A labeled, cached or debug-traced manifold is not split, because its hooks belong to its mid; nor is one whose body calls another pool, unless it is a loop.

Depth. Recursion that is not in tail position costs one native frame per level, so it reaches the depth the same recursion written by hand reaches, less a constant. R is the exception: its generated code nests evaluation more deeply per level and reaches at least 80% of the hand-written depth. C++ and Rust pools run every call on a thread with the stack size a native main thread gets (the soft stack limit, or 8 MiB when that is unlimited), so recursion in sourced C++ or Rust also reaches its native depth. Past that depth the language’s own limit applies: Python and R raise their recursion error, and a C++ or Rust pool crashes as the native program would.