13.9. Ownership
A morloc variable may be read any number of times. In generated C++ and Rust, each read must instead borrow the value, copy it, or move it. This section specifies how the compiler chooses among the three, how parameters are passed, and how the choice is checked. Ownership planning runs on each pool’s tree after serialization is placed and before code is emitted (Code generation). Python and R share values by reference, so they never copy and need no plan.
An affine elaboration. The choice translates an unrestricted source
term into an affine target term: every place the source uses a value twice
becomes an explicit copy, and every place it stops using one becomes an
explicit release. This is the translation of intuitionistic terms into
linear ones, with contraction and weakening made explicit. It preserves
meaning only when copying and releasing have no observable effect. So every
native type a morloc type maps to must behave as a value. A copy is
independent and equal to the original. Copying, moving and releasing
perform no output and touch no resource. A value holds no reference into
data that something else owns, and sourced code never mutates an argument
(C++, Rust). Given that, any placement that keeps the target affine
is correct, and how many times a copy constructor, Clone, destructor or
Drop runs is unspecified. The compiler picks a placement that copies
little.
The rules.
| Rule | Statement |
|---|---|
No use after move |
Once a read moves a value, no later read on any path through the same evaluation reads it, unless a new binding of that name replaces it. A value that is never moved is released when it goes out of scope. |
Move only at a last read |
A read may move a value only if no later read on its path reads it and no other read in the same step does. A step is a conditional’s test, the value a body ends in, or the values one loop iteration passes to the next. Reads within a step have no order, so a value read twice in one step is moved by neither read. Reading through a local that refers into a value counts as reading that value. |
Repeated reads never move |
A read inside a lambda, a nested loop, a separate manifold or a |
Loop guards borrow |
A read in a condition that a loop iteration branches on, including a test of a sum type’s tag, borrows, because the chosen branch may still read the value. |
Projection at a last read takes the field |
Reading a constructor or record field at the value’s last read moves the field out and releases the rest. The value’s own destructor does not run. |
Walking a lent value borrows |
A loop-carried value borrows when it starts from a borrowed value and every iteration replaces it with a part of that value (a field, a list element, or a |
Argument passing. By default a native manifold takes each parameter by
reference: const T& in C++, and &T in Rust for any type that is not
Copy. Rust Copy types (scalars, field-free enums, and tuples and
optionals of them) are passed by value. A
fully applied manifold instead takes a parameter by value when its body
reads that parameter once on every evaluation and keeps it: as the start
of an owned loop value, as an element of a value it builds, or as a
by-value argument to another fully applied manifold. That last case makes
the decision propagate up call chains, so it is computed over the whole
pool until it reaches a fixed point. The caller then moves the argument at
its own last read and copies it otherwise, so a call copies at most once,
and only when the caller still needs the value. A parameter kept on only
one branch stays a reference, so the branch that drops it copies nothing.
A closure, or any manifold that is also used as a function value, takes
every parameter by reference, because one signature serves every caller.
Planning and checking. The plan is computed once per pool from the intermediate form, shared by both languages, and has two levels. At pool level it records which field projections take their payload and which parameters each manifold takes by value. At manifold level it records which indices hold references and which are read at more than one point. A language contributes only three facts: which of its types are free to copy, whether a tag test may read a value after a field was taken from it, and whether a list literal keeps an element without making its own copy. Rust’s own compiler rejects any read of a moved value, so a wrong Rust plan fails the build. C++ has no such check. For C++ the compiler has an independent checker that takes the plan’s decisions as given, recomputes evaluation order from the intermediate form, and reports every read that could see a taken value. Generated C++ moves at fewer positions than these rules allow, so it may make more copies than they require.