Rows and functions¶
This page gives the static and dynamic semantics of records, variants, row predicates, and function values in the Quivers Indexed Effect Core (QIEC), and the translation that removes all of them before a program is compiled or transpiled. The surface forms are listed in Records, variants, and function values. The type system follows the qualified row types of Rose (Morris and McKinna, 2019), restricted so that every row the checker meets at export is closed; the inference of rows by unification with absence information follows Rémy (Rémy, 1989).
1. Rows¶
Fix a countable set \(\mathcal L\) of labels and the QIEC value types \(\tau\) of Typing and checking. A row is
with pairwise distinct labels \(\ell_i\), read up to permutation, and an optional tail \(r\), a static variable of kind \(\mathsf{Row}\). Write \(\mathrm{lab}(\rho)\) for the explicit labels of \(\rho\) and call \(\rho\) closed when it has no tail. Every row is stored with its labels sorted, so a closed row has a canonical position \(\mathrm{pos}_\rho(\ell) \in \{0, \dots, n-1\}\) for each of its labels, and two closed rows are equal exactly when they have the same entries.
A record type \(\{\rho\}\) and a variant type \(\langle \rho \rangle\) are formed from any row. A well-formed type proves absences: in \(\{\ell : \tau \mid r\}\) the tail \(r\) cannot hold \(\ell\), since the labels of a row are distinct. The checker collects these absences, for each declaration, into a lacks set \(\mathrm{lacks}(r) \subseteq \mathcal L\) for every row variable.
2. Predicates and entailment¶
A declaration's telescope may state predicates over its rows:
- containment \(\rho_1 \lesssim \rho_2\) (surface
{...} <= r): every entry of \(\rho_1\) is an entry of \(\rho_2\); - combination \(\rho_1 \odot \rho_2 \sim \rho_3\) (surface
r ++ s ~ t): \(\rho_1\) and \(\rho_2\) have disjoint labels and their union is \(\rho_3\).
For closed rows, each predicate has its set-theoretic meaning. For open rows, a predicate holds when it holds at every closed instantiation of its row variables that respects the lacks sets. Let \(\Theta\) be the predicates and lacks sets in scope in a body. The checker decides \(\Theta \vdash \pi\) syntactically:
- \(\Theta \vdash \rho_1 \lesssim \rho_2\) when \(\rho_1 \lesssim \rho_2 \in \Theta\), or when every explicit entry of \(\rho_1\) is an explicit entry of \(\rho_2\) (or lies in the tail of \(\rho_2\) by a stated containment) and the tail of \(\rho_1\) is absent, equal to the tail of \(\rho_2\), or contained in it by a stated containment;
- \(\Theta \vdash \rho_1 \odot \rho_2 \sim \rho_3\) when the predicate is in \(\Theta\), or when each explicit entry of either part is an explicit entry of \(\rho_3\) and provably absent from the other part, and the two tails combine into the rest of \(\rho_3\): both absent with nothing left; one absent and the other equal to the rest; or both present and related by a stated combination.
Proposition 1 (soundness and completeness). If \(\Theta \vdash \pi\) then \(\pi\) holds at every instantiation satisfying \(\Theta\). If every row of \(\pi\) is closed, then \(\Theta \vdash \pi\) exactly when \(\pi\) holds.
Proof sketch. Each clause either reads a hypothesis or compares explicit entries, which an instantiation leaves in place; a tail is equal to itself under every instantiation, and an absence in the lacks set holds under every instantiation by the well-formedness argument of §1. For closed rows, every clause reduces to a comparison of finite label sets with their types, which is the definition of the predicate. \(\square\)
Completeness for open rows is not claimed: a predicate implied by two stated predicates together, such as transitivity of containment through an intermediate variable, may need to be stated itself. The checker reports the predicate it could not derive, so the author can add it.
3. Typing¶
The rules extend the judgment \(\Gamma; \Theta \vdash v : \tau\) of Typing and checking for values and \(\Gamma; \Theta \vdash c : \tau \mathbin{!} \varepsilon\) for computations, where \(\varepsilon\) is an effect row. Records:
Extension \(\{v \;\mathtt{with}\; \ell = w\}\) is \(\{\ell = w\} \mathbin{+\!\!+} v\). Variants are dual: an injection \(\langle \ell = v \rangle\) has type \(\langle \rho \rangle\) whenever \(\Theta \vdash (\ell : \tau) \lesssim \rho\); a case function over the closed row \(\rho\) has type \(\langle \rho \rangle \to \sigma\) when it has one branch of result \(\sigma\) per label; and the join \(f \mathbin{+\!\!+} g\) of \(f : \langle \rho_1 \rangle \to \sigma\) and \(g : \langle \rho_2 \rangle \to \sigma\) has type \(\langle \rho_3 \rangle \to \sigma\) when \(\Theta \vdash \rho_1 \odot \rho_2 \sim \rho_3\).
A function type \(\tau_1 \to \tau_2 \mathbin{!} \varepsilon\) carries the effect row of its body. A lambda checks its body as a computation with the captured context and no resumption in scope, and an application performs the function's row:
Excluding the resumption from a lambda's context keeps a captured continuation from escaping the handler clause that owns it, so the one-shot and multi-shot discipline of Effects is unaffected by closures. A call of a computation whose telescope binds rows or open effect rows instantiates them, checks every instantiated predicate by entailment in the caller's \(\Theta\), and, when an argument is a function, unifies the open effect row of the parameter's arrow with the argument's row, respecting its lacks constraints.
Unannotated lambdas and case functions bound with let are typed by Hindley-Milner inference over
rows: each field access, extension, restriction, and injection introduces a fresh tail; tails unify
with Rémy's rule, which refuses to bind a tail to a row holding a label in its lacks set; and each
concatenation adds its combination predicate to the scheme. The scheme is principal: every type
the expression has is an instance of it. It is instantiated at each use.
4. Evidence¶
At a closed row, a predicate has evidence:
- the evidence of \(\rho_1 \lesssim \rho_2\) is the tuple \((\mathrm{pos}_{\rho_2}(\ell))_{\ell \in \mathrm{lab}(\rho_1)}\);
- the evidence of \(\rho_1 \odot \rho_2 \sim \rho_3\) is the pair of tuples \((\mathrm{pos}_{\rho_3}(\ell))_{\ell \in \mathrm{lab}(\rho_1)}\) and \((\mathrm{pos}_{\rho_3}(\ell))_{\ell \in \mathrm{lab}(\rho_2)}\).
Projection, restriction, and concatenation of records become tuple projections and tuple constructions at these offsets; an injection becomes the constructor at its label's position; and a join of case functions becomes a case whose branches are routed to the side whose evidence holds the alternative. This is the evidence translation of Rose, specialized: rather than passing evidence at run time, every computation with row binders is copied at the closed rows of its calls, so each offset is a constant and no evidence value exists in the translated program.
Proposition 2 (coherence). For closed rows, the evidence of a predicate is unique.
Proof. Labels within a row are distinct and rows are stored sorted, so \(\mathrm{pos}_\rho\) is a function of \(\rho\) alone, and each evidence tuple is determined by the predicate's rows. \(\square\)
Coherence is what makes the translation independent of how a predicate was derived: any two
derivations of \(\Theta \vdash \pi\) at closed rows give the same offsets, so the translated program
does not depend on the choices of the checker. This would fail with scoped labels, where a row may
repeat a label; QVR rows do not admit them, and a repeated label is refused with
qiec-row-overlap.
5. Iteration over labels¶
Generic programming over rows, as in Rω (Hubers and Morris, 2023), needs operations that range over the labels of a row: an analysis that eliminates a variant by one function of the label and payload, and a synthesis or fold that builds or consumes a record field by field. Let \(\rho = \ell_1 : \tau_1, \dots, \ell_n : \tau_n\) be closed, with \(\ell_1 < \dots < \ell_n\) in the canonical order of §1, and write \(e[\ell_k]\) for \(e\) with its label parameter replaced by the string literal \(\ell_k\). The rules are
together with \(\mathtt{get\_field}(x, \ell_k) : \tau_k\) and \(\mathtt{if\_alternative}(w, \ell_k, q \mapsto e, o) : \sigma\) when \(\Gamma, q : \tau_k \vdash e : \sigma\) and \(\Gamma \vdash o : \sigma\), for a label \(\ell_k\) of \(\rho\) known statically. Each operation denotes a finite term: the fold denotes \(e[\ell_n](\cdots e[\ell_1](i, x.\ell_1) \cdots, x.\ell_n)\), the map the record of the \(e[\ell_k]\) at \(v = x.\ell_k\), and the analysis the case over \(w\) with one branch per label, which is how the elaborator expands them.
The premises quantify over the labels of \(\rho\), so they are decidable only when \(\rho\) is closed. A
row that ends in a variable is refused (qiec-row-open), since the number of premises is not yet
known. A computation whose telescope binds rows and whose body iterates over them is therefore
typed by instance: each call fixes its static arguments, which must be closed, and the body is
checked once per distinct tuple of arguments, as an ordinary computation with no binders. This
departs from Rω, which types a generic body once, over an abstract label and payload type with a
containment predicate, and so needs a class constraint for every operation the body uses at the
payload type. Typing by instance lets the body use whatever its field types offer, such as == at
Real and at Int; the cost is that an error in a generic body appears at the first call whose
rows expose it, at that call's position.
Proposition 4 (finite instantiation). The instances of a module's generic computations are finite.
Proof. Instances are keyed by their closed static arguments, so a repeated key reuses an instance. A call that would begin an instance of \(g\) while an instance of \(g\) at arguments whose spelling is no longer is in progress is refused. Along any chain of nested instances, the instances of each one generic computation therefore have strictly decreasing argument lengths, so the chain is finite; each body makes finitely many calls, so the tree of instances that the module's calls begin is finite. \(\square\)
Because instances have no binders, §6 treats them like any other computation: their record and variant operations are rewritten by evidence, and the case functions the analysis builds are applied where they are built, so no closure family is generated for them.
6. Elimination¶
The reference machine evaluates records as label maps, variants as tagged values, and lambdas as closures pairing a lifted body with its captured environment, sharing the call frame discipline of named calls so that a closure's draws have stable addresses. Everything compiled or transpiled reads the module \(\mathcal E(M)\) produced from the checked module \(M\) by five passes:
- Template specialization. A computation with row binders, predicates, or an open effect row on an arrow parameter is a template; each call is replaced by a call of its copy at the call's closed static arguments. Specialization terminates because a template's copies are keyed by their closed arguments; a template that calls itself at a larger row (polymorphic recursion) has no finite set of copies, and the translation refuses it.
- Row desugaring. Every record and variant operation is rewritten by its evidence (§4).
- Closure conversion. Each lambda becomes a lifted computation taking its captured values and
its argument. For each function type, a family
__Closure_nhas one constructor per lambda of that type, whose fields are the lambda's captured values, and an application becomes acaseon the constructor followed by a call of the lifted computation (Reynolds, 1972). - Erasure. Records become tuples (a single field becomes the field itself), each variant type becomes a generated family with one constructor per label, and each function type becomes its closure family.
- Function specialization. A call whose function argument is a known constructor is replaced by
a call of a copy of the callee with that constructor fixed, so a function known at its call
leaves no
case. Copies are keyed by the constructor, and the keys of one module are finite.
The result is validated by the same checker as any module, and contains no record, variant, row,
predicate, lambda, or application; quivers.qiec.elimination.is_first_order decides this.
Proposition 3 (adequacy of elimination). For every closed computation \(c\) of \(M\), the reference machine gives \(c\) in \(M\) and its image in \(\mathcal E(M)\) the same result, the same sequence of requests, and the same addresses of sites, up to the renaming of generated computations.
Proof sketch. Specialization substitutes closed static arguments, which commutes with evaluation.
Row desugaring replaces a label lookup in a canonically ordered map by a positional lookup at the
label's canonical position (Proposition 2). Closure conversion is Reynolds' defunctionalization:
the case on a closure constructor selects exactly the lambda that built it, and the lifted body
receives exactly the captured values; the application allocates one call frame in both modules, so
addresses agree. Erasure is a bijection on values of each type. Function specialization unfolds one
case whose scrutinee is a known constructor. \(\square\)
The tests in tests/qiec/test_elimination.py check the proposition on programs covering each
construct: each is run on the reference machine as written and after translation with both
strategies, specialization and pure defunctionalization, and the values must agree exactly; a
program with draws is compiled, and its log joint is compared with the reference machine's and with
an independent closed form.
7. Limits¶
Three restrictions follow from requiring every exported program to be first order and finite.
First, a template, and every iteration over labels, must reach closed rows; a row variable that
reaches export or an iteration open is refused. Second, polymorphic recursion over rows is refused,
since it has no finite set of copies. Third, a target without a case on families accepts a
function value only when the function is known at its call.
Cited sources¶
J. Garrett Morris and James McKinna. Abstracting Extensible Data Types: Or, Rows by Any Other Name. Proceedings of the ACM on Programming Languages, 3(POPL):1–28, 2019. doi:10.1145/3290325.
Didier Rémy. Type Checking Records and Variants in a Natural Extension of ML. In Proceedings of the 16th ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages (POPL), 77–88. 1989. doi:10.1145/75277.75284.
Alex Hubers and J. Garrett Morris. Generic Programming with Extensible Data Types: Or, Making Ad Hoc Extensible Data Types Less Ad Hoc. Proceedings of the ACM on Programming Languages, 7(ICFP):356–384, 2023. doi:10.1145/3607843.
John C. Reynolds. Definitional Interpreters for Higher-Order Programming Languages. In Proceedings of the ACM Annual Conference, volume 2, 717–740. 1972. doi:10.1145/800194.805852.