13.6. Realization

A Morloc term may have several implementations: sourced functions in different languages, several in one language, and definitions written in Morloc itself. Realization chooses one implementation for every use of a term and one language for every node of each exported tree. It sits between typechecking and segmentation: its input is a typed tree in which each use of a term still carries all of its alternatives, and its output is a tree in which each node has exactly one implementation and one language, or no language at all when the nexus evaluates it. This section specifies the search space, the score being minimized, how ties are broken, and the placement rules that the score alone does not decide.

What is chosen. The user-facing view is in One term may have many definitions: one name, many definitions. The author’s claim in declaring them under one name is that they compute the same value, so realization treats them as interchangeable and chooses among them on cost alone. A term defined in Morloc has no language of its own. Its body is realized wherever its uses need it, so one definition can run in Python at one call and in C++ at another. A shared value or specialization (Term sharing and inlining) is scored once and then realized once for each language its uses land in, each copy under its own name. The result is the same every time the same program is built by the same compiler.

Search. Realization is a two-pass dynamic program over each tree.

  • Scoring, bottom up, gives every node a table from language to the best score of its subtree when that node runs in that language. A sourced call has one entry, its own language. An application combines its head’s table with, for each argument, the cheapest way to have that argument in the head’s language. A node with several alternatives takes, per language, the cheapest alternative. Literals, containers and other language-free nodes take the languages their context offers.

  • Collapse, top down, gives the root its cheapest language and then gives each child the language that minimizes its own score plus the cost of the edge from its parent’s language. At a node with alternatives, the cheapest alternative from the parent’s language is kept and the rest are discarded.

Before scoring, applications of a literal lambda whose parameter is used at most once are reduced, so that a long point-free composition does not make the scorer revisit the same function arguments again and again.

Cost. A score counts crossings between pools, grouped by depth, then changes of language within one pool, then the sourced calls placed in each language, grouped by the language’s rank in a fixed preference order, then the number of sourced calls. Scores are added along the tree and compared part by part in that order: crossings from the greatest depth down, so one crossing at a greater depth outweighs any number at a smaller one; calls from the least preferred language upward.

Part Meaning

Crossing

An edge between two languages in different pools is one crossing, whatever values cross it. A value made in one pool, passed through a second and used in a third crosses twice. An edge between languages that share a pool, such as a guest language in its host’s pool (Language onboarding), is a switch.

Depth

Code at the root of an exported function is at depth 0. A function or suspension passed to a sourced function, or to a shared specialization, is one deeper than the call, and the body of a recursive definition is one deeper than the call that enters it: either may run more than once per call. A function value passed to a definition in Morloc is counted where the body uses it.

Preference

The order is one table shipped with the compiler: compiled languages first, then interpreted ones, a language with no entry last. It decides only between placements that cross equally often at every depth.

Size

Fewer sourced calls wins, so a sourced implementation beats a Morloc definition that does the same work in the same languages by calling several.

Code the nexus evaluates costs nothing, and starting a pool costs nothing, since pools start once per run.

Ties. Before cost, alternatives whose types differ are separated by type: the most specific instance whose type matches the use wins, and an instance on a nearer alias beats one on a farther alias. Two instances that both match with neither more specific are rejected as ambiguous. A node with no implementation of its own, such as a let or a container, keeps its parent’s language when its options tie. Two sources of one term in one language are refused before realization; two sources still tied after every part of the score are refused at build, naming both. Definitions in Morloc that tie are interchangeable, and a fixed one is used, so the choice never varies between builds.

Checking the built placement. Collapse is meant to build the placement scoring found cheapest. With verbosity, morloc typecheck --realize scores the built tree again with every node fixed to the language and implementation collapse chose, and lists any difference beside the placement. A node built in a language its table does not offer, such as a lambda built in its parent’s language around a body placed elsewhere, is priced as reaching its table from there.

Placement rules. Some placements are fixed rather than scored:

  • A sourced function runs in the language it is sourced from.

  • A lambda is built in its parent’s language; only its body chooses, and a call the body makes elsewhere is an ordinary crossing there.

  • A function value stored in a container, or passed directly to a sourced function, is homed in that container’s or host’s language, so the host receives a native callable (Segmentation and cross-language calls).

  • A value bound by a let that holds a function and computes nothing to build, such as a lambda or a record of lambdas and suspensions, is built again in each language that uses it, so a use in another language calls its own copy rather than crossing back to the first. A bound function that is computed is built once, in its definition’s best language, and every use reaches it there.

  • A function that sourced code makes stays in its pool. The compiler follows where every function value was made through every construct and every call: a sourced call makes each function in its result, unless the source’s signature gives that position a type variable that one of its parameters also holds, in which case the result holds what the arguments held there; a function a source passes to a callback is made by that source; a lambda, a partial application or a suspension that Morloc builds is Morloc’s. A function inside a record field, a list element or a value of an alias type is followed like any other, and a function passed to a source returns its result where that source calls it, so a function another pool made cannot come back out of it. Every expression that holds or receives a function sourced code made, and the expression consuming it, is placed in that source’s language, and an argument computed from one is computed there so only its value crosses. A recursive function given one is built again for that language. A program that would carry one into another language, or serialize one, is refused at build.

  • When the call that makes such a function has sources in several languages, the source is chosen for the call alone; to have another make it, the program imports only the module that sources it in the language needed. A call given such a function may instead use its term’s source in the function’s language, when the term has one with the same call groups.

  • A language-free operation (building a list, tuple or record, applying a function value, a bracket access) runs in its context’s language only if that language can represent every type it works on. Otherwise it moves to the cheapest language that can.

  • A conversion between a newtype and its representation runs in a language where the newtype has no native form of its own, since only there is the conversion free.

  • Every node on a recursive cycle, from the recursive term down to each call that refers back to it, is placed in that term’s language or in a language that shares its pool. A cycle that crossed pools would nest one foreign call per level and park a worker in each pool per level, so its depth would be bounded by the number of workers rather than by memory. Kept in one pool, the cycle can become a loop (Segmentation and cross-language calls), and the foreign calls its body makes are a bounded number per step. The outermost recursive term wins, so nested cycles agree.

Nexus or pool. A tree that calls nothing sourced gets no language. The nexus evaluates it directly, and the manifest marks its command pure (Compilation artifacts). The nexus evaluator has no function values and cannot call a function by name, so two cases still need a pool. A term that calls itself is given one of the program’s pool languages, chosen by the same scoring; if the program imports no language at all, the build fails and asks for one. A shared specialization that takes a function argument is expanded at each of its nexus uses, while a first-order one is called by name. A language-free subtree used inside a realized tree runs in the pool of its context, exactly as if it had been written there.

Result. Every node of a realized tree carries one implementation and one language. The set of languages that appear is the set of pools the program needs, and every edge between two languages that do not share a pool is a cross-language call. Segmentation cuts the tree at exactly those edges (Segmentation and cross-language calls).