Skip to content

Typing and checking

QVR is checked twice. The source checker assigns objects, spaces, axes, and family shapes to the categorical and probabilistic declarations. Elaboration then translates every executable declaration into the Quivers Indexed Effect Core (QIEC), the typed core language of quivers, whose checker assigns kinds, indexed types, and effect rows to computations. The first check asks whether the pieces connect: whether a morphism returns what the next one expects, and whether a family receives suitable arguments. The second also asks what running a computation may request: draws, scores, state, or another declared effect.

A typing judgment is derived by inference rules: when the premises above the line hold, the conclusion below it holds. A context such as \(\Gamma\) lists the names in scope with their types.

1. The two judgments

The source judgment

\[ \Gamma \vdash e : A \rightsquigarrow B \]

states that in the context \(\Gamma\) of module declarations, the expression \(e\) is a morphism from \(A\) to \(B\), weighted, stochastic, or continuous (Morphisms). A program body also threads a context \(\Phi\) of the values bound so far, from left to right:

\[ \Gamma ; \Phi \vdash s \dashv \Phi', \]

which states that the step \(s\), checked with the bindings \(\Phi\), leaves the bindings \(\Phi'\): a let x <- sample F(...) adds \(x\), and an observe adds nothing. The semicolon separates the two contexts.

The QIEC judgment

\[ \Delta ; \Gamma_v \vdash c : T\ !\ \epsilon \]

states that the computation \(c\), with the static parameters \(\Delta\) and the run-time values \(\Gamma_v\), returns a value of type \(T\) and may make requests to the effect instances in the row \(\epsilon\). \(\Delta\) is a telescope, an ordered list of binders in which later types may mention earlier variables (de Bruijn, 1991). The QIEC judgment is the one that qvr run, the language server, serialization, and the analysis of what each transpilation target supports all rely on.

2. Source types and objects

An object declaration declares finite sets and spaces alike, and its value decides which:

object Row : FinSet 100
object State : {cold, warm, hot}
object Weight : Real 4
object Probability : Simplex 4

FinSet and enumerations denote finite sets, and Real, Simplex, Sphere, Ball, and the matrix constructors denote spaces (Types and spaces). Products use *. Coproducts and categorial types are allowed in the contexts given on Types and spaces and Schemas. There is no separate space declaration, and atom, sort, and binder do not declare objects: atomic syntactic categories come from category or a deduction, and sorts and binding constructors belong inside a syntax.

The type of a declared morphism is fixed by its domain and codomain:

\[ \frac{A, B\ \text{objects}} {\Gamma \vdash \mathsf{morphism}\ f : A \to B \ :\ A \rightsquigarrow B}. \]

Composition requires the codomain of the left operand to equal the domain of the right one, and the two operands to have the same composition rule. The tensor product combines domains and codomains componentwise. Expressions gives the equations, and Composition rules the laws they need.

3. Program contexts

A program declaration fixes a domain and a codomain and introduces its body:

object Row : FinSet 8

program regression(offset : Real, y : Real[Fin Row]) : Row -> Row
    let location <- sample Normal(loc=0.0, scale=2.0)
    let scale <- sample HalfNormal(scale=1.0)
    let mean = location + offset
    observe y : Row <- Normal(loc=mean, scale=scale)
    return y

The source checker classifies the names in a body:

A name that is Becomes
declared in the static telescope [...] a type, effect, index, object, or morphism parameter in \(\Delta\)
declared with a type in the runtime telescope (...) an external value parameter in \(\Gamma_v\)
declared as a bare domain parameter a projection of the declared domain
bound by sample, a call, or let a local binding in \(\Phi\)
the response of an observe an observation input
given by via an index map, an integer input (role index_map in the API)
read and bound nowhere a data input

Elaboration records offset and y above as typed parameters of the entry point. Their annotations are checked against every use. The value of offset is given when the program runs; observe y assigns y its observation role. The same checked parameter is inferred when its annotation is omitted and the body determines its type. An input whose type remains ambiguous requires an annotation.

The static and runtime telescopes have different judgments. If \(\Delta\) is well formed and \(\Delta\vdash T:\mathsf{Type}\), a runtime binder extends \(\Gamma_v\) with \(x:T\). It does not extend the effect row. The body is then checked under \(\Delta;\Gamma_v\), and specialization substitutes a static valuation into both its input types and result type. Thus declarations of external values and ordinary computation parameters use the same rule.

A static [n : Nat] may index Real[Fin n] and act as the finite ordinal in a domain or plate annotation. These occurrences share one binder, so specialization at \(n=k\) substitutes the same \(k\) into every shape and finite-object position. A module-level object cannot refer to a binder local to one program; the scope is the program signature and its body.

Natural-number addition has the static rule

\[ \frac{\Delta\vdash a:\mathsf{Nat}\qquad\Delta\vdash b:\mathsf{Nat}} {\Delta\vdash\mathsf{nat\_add}(a,b):\mathsf{Nat}}. \]

It introduces no term into \(\Gamma_v\) and no effect. Under valuation \(\delta\), its denotation is \(\llbracket a\rrbracket_\delta+ \llbracket b\rrbracket_\delta\). Canonical sums collect constants and nominal summands, including repeated variables. Substitution rebuilds that normal form while leaving rigid skolems untouched; a sum cannot hide an escaping skolem from the ordinary scope checks. Unproved Nat subtraction is absent. An exact affine solution must have nonnegative integer coefficients and constant before it becomes a symbolic Nat term.

Binding solves exact affine equations from checked shapes. It rejects conflicting evidence and forced negative or fractional extents, while underdetermined extents remain parameters requiring further evidence. A shape-only zero extent does not require an inhabitable categorical finite-set object. The index reference distinguishes this arithmetic from user-defined index constructors.

A distribution construction first selects one complete parameter schema. Its given parameter names must equal one declared alternative, with no repeated names. This check rules out missing parameters and simultaneous exclusive alternatives before the constructor's result is accepted. Named source draws and positional computation calls resolve to these same semantic parameter identities; a runtime library's optional defaults do not extend the checked constructor contract.

Distribution shape signatures use the same dependent context. Their local Nat telescope quantifies parameter and sample-event dimensions; each application freshens those binders before collecting exact shape equations. Known parameter shapes and any declared event shape are checked jointly; for instance, \(n+m=5\) and \(n+2m=8\) determine \(n=2,m=3\) without requiring either equation to determine one variable alone. Source programs, pure computation constructors, and kernel checking use the same signature. For instance, an additive log-ratio normal with Gaussian coordinate extent \(d\) takes a location tensor of shape \([d]\) and a Cholesky tensor of shape \([d,d]\), and returns an event of shape \([d+1]\). The optional reference component changes coordinate order without changing these extents.

These are dimensions of one parameter value and one sample event, before leading plate batch axes. Parameter-to-parameter relations apply even when the sample is scalar. Source inference may solve its own open extents, while the kernel solves only fresh signature variables; neither operation binds a caller's rigid skolem by its display name. Existing event-axis metadata is shorthand for this contract, rather than a separate typing rule.

A runtime (count : Nat) has a different judgment: it extends \(\Gamma_v\) with an integer-valued input subject to the restriction \(count\geq 0\). Its lowered parameter uses the arithmetic carrier Int and carries the checked constraint nonnegative. This constraint survives serialization and is validated at native and reference execution boundaries; it does not turn the value into a static index. At a program call, a nonnegative integer literal or an input carrying this constraint satisfies a runtime Nat formal. A computed or sampled Int does not establish the restriction; without a refinement proof mechanism, the checker conservatively refuses that actual argument.

The common rule across declarations concerns the stage of a binding, rather than one universal interpretation of parentheses. A define or program static telescope extends \(\Delta\); its value telescope extends \(\Gamma_v\). Rule and schema brackets instead bind static pattern variables, whose substitution fixes a morphism's types before its arguments are used. In family Vec[A : Type](n : Nat) : Type, both A and n belong to static information: n is an index that constructors and case analysis may refine. Constructor fields are values. Likewise, an effect interface's type and index parameters are static, while operation arguments and resumed values are runtime terms. Contraction inputs and encoder attachments provide components under their own elaboration rules; they do not quantify new index variables merely because their names occur in a header.

Distribution parameter names also bind no static indices. In Frechet(scale=s, concentration=nu), nu is a runtime real parameter, even though it changes the response's tail and moment domains. An annotation (nu : Real) checks its carrier; family validation checks the admissible parameter range. Inferring a carrier or axis from use does not infer whether a real parameter denotes a mean, a log-location, or a scale. The selected family and any explicit regression parameter map determine that interpretation. None of these declarations alone extends an effect row.

Checked scalar operations refine execution domains without introducing new static binders. positive_parameter(x) checks finite positivity; log_probability(p) checks a finite probability; log1mexp(x) checks a nonpositive logarithm; and log_ndtr(x) computes a Normal log CDF. Their checked signatures are Real -> Real; a carrier judgment alone does not prove their domains. An active invalid argument raises explicitly. A conditional that protects a partial operation must retain its branch boundary: eager evaluation of both branches would change its semantics. An executor that cannot preserve symbolic branching must refuse it.

3.1 Program steps

A mass query has a polymorphic structural rule, rather than a monomorphic scalar-primitive signature:

\[ \frac{\Gamma;\Phi\vdash d : \mathrm{Sampleable}[T]} {\Gamma;\Phi\vdash \operatorname{log\_mass}(d) : \mathrm{LogWeight}}. \]

For a measure \(\mu_d\), its denotation is \(\log \mu_d(\Omega)\), with \(\log 0=-\infty\). This operation introduces no effect. The LogMass core node records its source origin and any retained batch axes; with retained dimensions \(\bar b\), the result is \(\mathrm{Tensor}[\mathrm{LogWeight}](\bar b)\), summing the log masses of independent event factors within each batch. A nonempty retained batch must match the distribution construction's declared batch axes. Intrinsic family event axes are interpreted by the distribution itself. Restriction preserves raw measure mass; normalization changes that mass to one. Neither operation may infer a normalizer from the density at an arbitrary outcome.

The main rules are

\[ \frac{\Gamma ; \Phi \vdash F(\bar a) : \mathrm{Sampleable}[T]} {\Gamma ; \Phi \vdash \mathsf{sample}\ x \leftarrow F(\bar a) \dashv \Phi, x : T} \qquad \frac{\Gamma ; \Phi \vdash F(\bar a) : \mathrm{Sampleable}[T] \qquad y : T\ \text{is given}} {\Gamma ; \Phi \vdash \mathsf{observe}\ y \leftarrow F(\bar a) \dashv \Phi} \]
\[ \frac{\Gamma ; \Phi \vdash e : T} {\Gamma ; \Phi \vdash \mathsf{let}\ x = e \dashv \Phi, x : T}. \]

let x <- f(args) checks a call of a computation or a program. The callee's result type, with its arguments substituted, becomes the type of x, and the effects the callee leaves unhandled join the caller's row. It is a different form from the pure let x = e.

score name = e requires \(e\) to be a scalar log weight. It binds the value, which the trace records, and performs Score.add in the elaborated computation. return e is checked against the declared codomain. The elaborated entry point may also record data inputs that do not appear in the domain.

3.2 Plates, events, and index maps

An annotation after the bound name introduces a plate, a batch axis of independent draws:

object Group : FinSet 8

program hierarchical : Group -> Group
    let tau <- sample HalfNormal(scale=1.0)
    let group_effect : Group <- sample Normal(loc=0.0, scale=tau)
    observe y : Group <- Normal(loc=group_effect, scale=sigma)
    return y

The step options [over=Category, iid_over=Group] separate the event axes of a draw from its batch axes (Morphisms §6). The checker verifies the declared finite extents and records the result as a PlateShape, which a transpilation target receives as a typed value.

Inside a grouped marginalize, via=group_idx requires one index map for each factor of the group set (Programs as kernels §2.7). For a product of groups the positions are flattened in row major order, the last factor varying fastest. The values of an index map are data inputs and must be valid integer indices when the program runs.

Observation weight checking

For observe y : Row <- F(...) [weights=w], the weight has type Real or Real tensor over exactly the observation's batch axes. A declared scalar broadcasts across those axes. The observation's event coordinates are excluded from the weight shape: one multivariate observation receives one power after its joint log density is formed. An otherwise unbound weight name is inferred as a tensor over the batch axes, or a scalar Real when no batch axis is present.

The shape and element type are checked before execution; finiteness and nonnegativity are checked on the given or computed values when the program runs. Booleans are refused. The checked pure primitive observation_weight retains the value after validation, and scale_weight applies it to the observation's LogWeight before WeightSum or SegmentSum. scale_weight implements the zero-weight branch defined in the observation semantics.

A weight parameter is an ordinary input annotation and adds no effect. Its use by observe retains the observation's Score effect; weighting does not introduce another sampling request or modify the prior.

3.3 Effect annotations

The program option [effects=[...]] accepts Sample, Score, Marginal, and Pure. A sample contributes Sample, an observe or score contributes Score, a marginalize block contributes Marginal, and let and return contribute nothing. Pure requires the set of contributed effects to be empty. These four names summarize the body. The precise effect type is the QIEC row that elaboration computes (Programs as kernels §2.8).

4. QIEC kinds and types

A kind classifies static arguments as a type classifies run-time values. Static parameters, written in square brackets, are used in checking and specialization, and range over Type, an index sort, Nat, or Effect. Programs additionally abstract over objects and morphisms in their static telescopes. Run-time parameters, in parentheses on a program or computation, are values passed when it runs. Family declarations use parentheses for refinable static indices instead; punctuation is interpreted with its declaration form. In a telescope such as [A : Type, n : Nat](xs : Vec[A](n)), both the element type A and index n are in scope when xs is checked.

Kind Inhabitants
Type run-time value types and applications of indexed families
an index sort closed terms of its constructors
Effect effect interfaces and their applications
Nat static natural numbers, used as extents

The primitive run-time types are Unit, Bool, Int, Real, String, LogWeight, A[Fin d1, Fin ...], Sampleable[A], and Site[A], and products are written A * B. LogWeight is a nominal type, distinct from Real by its name.

An indexed family is a datatype whose type carries extra information, an index, that its constructors fix: an inductive family (Dybjer, 1994), called a generalized algebraic datatype (GADT) in programming (Xi et al., 2003). Vec[A](n) records an element type and a length:

index Nat = Z | S(Nat)

family Vec[A : Type](n : Nat) : Type
    constructor Nil : Vec[A](Z)
    constructor Cons[m : Nat] : A * Vec[A](m) -> Vec[A](S(m))

Nil has length zero, and Cons of a head and a tail of length m has length S(m), the successor of m. A is a parameter of the family, the same in every constructor's result, and n is an index, which each constructor fixes (Dybjer, 1994). Checking a constructor substitutes both telescopes and verifies the family application in its result.

5. Case analysis on indexed families

A case over an indexed family has a motive, the result type as a function of the scrutinee's indices (McBride, 2002). Matching a constructor introduces its static variables as rigid variables, fresh constants that stand for unknown indices inside the branch and may not escape it (Peyton Jones et al., 2006), together with equations between the scrutinee's indices and the constructor's result:

index Nat = Z | S(Nat)

family Vec[A : Type](n : Nat) : Type
    constructor Nil : Vec[A](Z)
    constructor Cons[m : Nat] : A * Vec[A](m) -> Vec[A](S(m))

define length[A : Type, n : Nat](xs : Vec[A](n)) : Int !{} =
    case xs motive (m : Nat) => Int
        Nil =>
            return 0
        Cons[k](head, tail) =>
            let rest <- length[A, k](tail)
            return 1 + rest

Here the motive is Int for every index. Every reachable branch must return the motive at its refined index. Matching a constructor establishes equations between the scrutinee's indices and the constructor's result indices, which hold in the branch and every branch nested in it. The checker solves them by first-order unification over index terms, in which distinct constructors are distinct and constructors are injective. A branch may be omitted exactly when its equations, together with the enclosing ones, have no solution: a vector of length zero cannot be a Cons, whose length is a successor, and inside a Cons[k] branch over Vec[A](S(S(Z))) the tail Vec[A](k) cannot be Nil, since S(S(Z)) = S(k) gives k = S(Z). Within a branch, types are compared under the most general unifier of its equations, so a branch whose index is known to be Z may return Vec[A](Z) where the motive states Vec[A](n). An equation the unifier cannot decide, one over a sum of index variables, remains an obligation, and the checker does not assume that a branch is unreachable.

5.1 Patterns, coverage, and compilation

A branch's pattern is checked against the scrutinee's type, never synthesized. The type selects the constructors a name may denote, so a name that two families declare is not ambiguous in a pattern; a bare name is a nullary constructor of that type when one exists and a variable otherwise; a literal is admitted only by Int, String, Bool, or Unit. A pattern synonym is expanded in the environment of its use before anything else reads the pattern.

Coverage follows Lower Your Guards (Graf et al., 2020). Each clause desugars to a sequence of guards x <- K(y1, ..., yn), one sequence per choice of or-pattern alternatives, with a Boolean guard as a test of unknown outcome. Two functions over refinement types, sets of constraints x ~ K(ys) and x ≁ K, follow the clauses in order: the uncovered set, the values no clause matches, and each clause's reaching set. A match is exhaustive when the uncovered set is uninhabited, and a clause is redundant when its reaching set is; both conditions are errors, since a computation is total and a redundant branch misstates the family. Inhabitation is decided per variable by the variable's type: an alternative whose result indices are distinct from the type's by distinct closed constructors does not inhabit it, the same rule the checker applies to a one-level case, and a field type is explored to a bounded depth beyond which it is assumed inhabited. Each refinement type also carries the index equations its assumed constructors establish, solved as the checker solves them, so a nested value's indices are read under the equations of every enclosing match, and a refinement type whose equations contradict is empty. Coverage is therefore sound: it never accepts a match that misses a value. A clause whose own equations contradict the scrutinee's indices is reported as unreachable, and a clause that earlier clauses cover as redundant; both are errors.

The clauses compile to a decision tree (Maranget, 2008). A clause matrix has a column per value still to examine and a row per clause; compilation tests the first column the first row needs, and continues with the matrix specialized to each alternative (the rows that name it or a wildcard there, with its fields as new columns) or with the default matrix of the wildcard rows, until the first row is all wildcards and selects its clause, behind its guard. Every value is tested at most once on a path. A family test lowers to the core's one-level case with the canonical skolems of each branch, a Bool test to if, a tuple to projections, and a literal test to a chain of equality tests. A default matrix reached from several alternatives is lowered once, through a generated family whose constructors carry the named alternatives' fields and whose last constructor stands for the default; when the tree still reaches a clause from several leaves, the leaves return a generated selector value carrying the clause's bindings and one flat case on it lowers each body once. A value crossing to the flat case loses the equations of the tests it passed, so its bindings travel at their types under the most general unifier of those equations, which the checker accepts because it compares the field types modulo the same equations; where the unifier would bind a static variable bound outside the match, which no generated value can carry, that part of the tree is lowered without sharing. A match whose clauses name distinct constructors with variable fields lowers to exactly the one-level case it states.

5.2 Bidirectional elaboration of values

Values are elaborated bidirectionally (Dunfield and Krishnaswami, 2013; Dunfield and Krishnaswami, 2021). Introduction forms, a constructor application, a tuple, a literal, are checked against an expected type when the position supplies one; elimination forms, a call, a projection, a case scrutinee, synthesize; and an annotation (e : T) checks e against T and synthesizes T. For a constructor c of a family F with telescope Δ = (params of F, own binders of c), elaboration introduces one metavariable per binder of Δ and generates equations (the expected type equals c's result type, each explicit static argument equals its metavariable, each field's declared type equals the field's type) solved by first-order unification over static terms (Pottier, 2014). A field is checked against its declared type once that type is solved, and synthesized otherwise. The solution instantiates the core constructor value, which states every static argument, so the core and every target see what an explicit application would have produced. An equation that fails is reported with both sides; a metavariable left unsolved is reported with a request for an annotation.

A name that several families declare is resolved by the expected type when it is one of those families, and is otherwise refused with every candidate listed. Qualification F.c and annotation (e : T) resolve it; the elaborator never chooses. A case checked against a known type T without a motive has the motive (i : S) => T that ignores the indices; a motive whose result depends on the indices is written out.

6. Computations and rows

An effect row lists the effect instances a computation may make requests to (Leijen, 2014; Hillerström and Lindley, 2016). A trace records the requests one run actually made, so a conditional that makes a request in one branch still needs it in its row. Two instances of State[Int], such as a counter and an accumulator, are different entries of a row, and a handler of the counter handles only the counter's requests (Effects and type classes §8). The effects guide shows rows in declarations.

A computation's signature separates its static telescope, its run-time parameters, its result, and its row:

define f[STATIC](VALUES) : RESULT !{INSTANCES | TAIL lacks INSTANCES} =
    COMPUTATION

Values and computations are checked separately. let x = value extends the value context and sequences nothing. let x <- computation checks the computation on the right, binds its result, and joins its row with that of the rest. perform i.op[...]() adds the instance i to the row, after its static arguments are substituted.

A row with a tail variable is open, so a computation may be polymorphic in the rest of its row (row polymorphism). A lacks constraint on the tail forbids it from containing an instance that an abstraction handles or adds. A conditional joins the rows of its branches, and a case joins the rows of every reachable branch. A call substitutes the callee's static arguments and then joins the effects it leaves unhandled. A recursive call is checked against the declared signature, so recursion needs no unrolling during checking.

7. Handlers and scoped instances

A handler interprets a request and may resume the computation that made it. The continuation is the rest of that computation with the request's answer filled in (continuation). Each operation clause declares a resumption grade, the number of times it may resume the continuation: 0 never, aff at most once, 1 exactly once, and omega any number of times. These are the multiplicities of substructural typing, as in quantitative type theory (Atkey, 2018). A handler for abort can use grade 0, and a handler for enumeration that tries several answers needs omega.

with instance introduces a fresh instance, which may not escape its scope, in the manner of lexically scoped handlers (Biernacki et al., 2020). handle i with h in c checks that the handler is defined for the exact applied interface of i, and then:

  • a handler with a clause for every operation removes i from the row of the body;
  • a handler without one keeps i in the row;
  • the effects of the handler's clauses join the remaining row;
  • a row variable keeps its lacks constraints.

A handler may have parameters, value binders \(\bar p : \bar P\) that its clauses read (Leijen, 2017; Bauer and Pretnar, 2015). Write \(h : E[\bar a];\ A \Rightarrow B;\ \bar P;\ \epsilon\) for a handler of \(E[\bar a]\) that takes a computation of type \(A\) to an answer of type \(B\) with parameters of types \(\bar P\) and introduced row \(\epsilon\). Its clauses are checked by

\[ \frac{\bar p : \bar P,\ x : A \vdash c_{\mathsf{ret}} : B \mathbin{!} \epsilon_0 \qquad \epsilon_0 \subseteq \epsilon} {\mathsf{return}\ x \Rightarrow c_{\mathsf{ret}}\ \text{ok}} \qquad \frac{\bar p : \bar P,\ \bar x : \bar X,\ \mathsf{resume} : Y \times \bar P \to B \mathbin{!} \epsilon \vdash c_{\mathsf{op}} : B \mathbin{!} \epsilon_0 \qquad \epsilon_0 \subseteq \epsilon} {\mathsf{op}(\bar x)\ \mathsf{resumes}\ g \Rightarrow c_{\mathsf{op}}\ \text{ok}} \]

for an operation \(\mathsf{op} : \bar X \to Y\), where \(c_{\mathsf{op}}\) also resumes as its grade \(g\) allows. A resume written without with carries the clause's own parameters. Installing the handler gives the parameters their first values:

\[ \frac{\Gamma \vdash c : A \mathbin{!} \{i : E[\bar a]\} \cup \epsilon' \qquad \Gamma \vdash \bar v : \bar P \qquad h : E[\bar a];\ A \Rightarrow B;\ \bar P;\ \epsilon} {\Gamma \vdash \mathsf{handle}\ i\ \mathsf{with}\ h(\bar v)\ \mathsf{in}\ c : B \mathbin{!} \epsilon' \cup \epsilon} \]

when \(h\) is total, and with \(i : E[\bar a]\) kept in the row when it is partial. A handler without parameters is the case of an empty \(\bar P\).

The checker records each clause's grade and rejects an authored clause that visibly resumes more often than its grade allows. The reference machine, the library's interpreter for QIEC, also counts resumptions at run time. Rigid indices, scoped instances, and the evidence for them may not occur in the type or row that leaves their scope. The separation of requests from their interpretation is that of algebraic effects and handlers (Plotkin and Pretnar, 2009). QIEC adds named instances, indexed types, and resumption grades.

8. Program elaboration

Every program elaborates to a named QIEC computation over the built-in instances random : Random and score : Score:

Step QIEC form
sample perform random.sample(...), then a score of the chosen value
observe perform score.add(log_prob(...)) at the given value
score perform score.add(...)
pure let pure binding
let x <- f(...) checked call and binding
marginalize over a finite family fresh choice and weight instances under foreign handlers that declare an enumeration and a log-semiring product (Weighted deduction §11)
marginalize over another family one ordinary draw, with no integral
scan a generated recursive helper over a sequence of open extent

Data inputs, observations, index maps, the program's parameters, and the free names found in the body become typed parameters of the entry point, each with its role. Generated helpers are ordinary checked computations, which the analysis of target support, serialization, and debugging all see.

Summing out a latent variable requires a registered family with finite support, and the QIEC path enumerates categorical and Bernoulli supports exactly. A reduction on a family without finite support is rejected. A sample inside a block that does not depend on the summed variable is moved before the block, and one that does depend on it is rejected, since it would be a different draw for each value of the variable.

9. Implementation

Stage Modules
parsing quivers.dsl.parser, grammars/qvr/grammar.js
source resolution and shapes quivers.dsl.compiler, quivers.dsl.step_resolution
elaboration of programs quivers.dsl.program_elaboration
QIEC checking quivers.qiec.checking, quivers.qiec.module
coverage of indexed cases quivers.qiec.coverage
reference execution quivers.qiec.evaluator, quivers.qiec.execution
analysis of target support quivers.transpile.qiec_ir

qvr check parses, checks the source constraints, builds the compiled runtime, the module's PyTorch implementation, where it applies, and checks the QIEC module. qvr check --target also analyzes which QIEC features the target supports. Transpilation runs the target's renderer as well, which can report further unsupported constructs.

10. Guarantees and limits

The checker guarantees that accepted terms have well-kinded types, that calls substitute their telescopes, that rows record every request, that handlers cover the operations they claim, and that generated computations form a valid module. The reference machine checks attachments and resumption grades when a program runs.

Some checks need concrete inputs: data inputs fix dynamic shapes, foreign handlers need a provider, and each target renderer accepts its own subset of QIEC. An attachment, a value or handler held only in the running Python process such as a PyTorch module, is outside the serialized core. The limits of the runtime are on Implementation correspondence and limits, and those of transpilation on Transpilation correctness.

Cited sources

N. G. de Bruijn. Telescopic Mappings in Typed Lambda Calculus. Information and Computation, 91(2):189–204, 1991. doi:10.1016/0890-5401(91)90066-B.

Peter Dybjer. Inductive Families. Formal Aspects of Computing, 6(4):440–465, 1994. doi:10.1007/BF01211308.

Hongwei Xi, Chiyan Chen, and Gang Chen. Guarded Recursive Datatype Constructors. In Proceedings of the 30th ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages (POPL), 224–235. 2003. doi:10.1145/604131.604150.

Conor McBride. Elimination with a Motive. In Types for Proofs and Programs (TYPES 2000), Lecture Notes in Computer Science, 197–216. Springer, 2002. doi:10.1007/3-540-45842-5_13.

Simon Peyton Jones, Dimitrios Vytiniotis, Stephanie Weirich, and Geoffrey Washburn. Simple Unification-Based Type Inference for GADTs. In Proceedings of the Eleventh ACM SIGPLAN International Conference on Functional Programming (ICFP), 50–61. 2006. doi:10.1145/1159803.1159811.

Sebastian Graf, Simon Peyton Jones, and Ryan G. Scott. Lower Your Guards: A Compositional Pattern-Match Coverage Checker. Proceedings of the ACM on Programming Languages, 4(ICFP):107:1–107:30, 2020. doi:10.1145/3408989.

Luc Maranget. Compiling Pattern Matching to Good Decision Trees. In Proceedings of the 2008 ACM SIGPLAN Workshop on ML, 35–46. ACM, 2008. doi:10.1145/1411304.1411311.

Jana Dunfield and Neelakantan R. Krishnaswami. Complete and Easy Bidirectional Typechecking for Higher-Rank Polymorphism. In Proceedings of the 18th ACM SIGPLAN International Conference on Functional Programming, 429–442. ACM, 2013. doi:10.1145/2500365.2500582.

Jana Dunfield and Neel Krishnaswami. Bidirectional Typing. ACM Computing Surveys, 54(5):98:1–98:38, 2021. doi:10.1145/3450952.

François Pottier. Hindley-Milner Elaboration in Applicative Style. In Proceedings of the 19th ACM SIGPLAN International Conference on Functional Programming, 203–212. ACM, 2014. doi:10.1145/2628136.2628145.

Daan Leijen. Koka: Programming with Row Polymorphic Effect Types. In Proceedings of the 5th Workshop on Mathematically Structured Functional Programming (MSFP), volume 153 of Electronic Proceedings in Theoretical Computer Science, 100–126. 2014. doi:10.4204/EPTCS.153.8.

Daniel Hillerström and Sam Lindley. Liberating Effects with Rows and Handlers. In Proceedings of the 1st International Workshop on Type-Driven Development (TyDe), 15–27. 2016. doi:10.1145/2976022.2976033.

Robert Atkey. Syntax and Semantics of Quantitative Type Theory. In Proceedings of the 33rd Annual ACM/IEEE Symposium on Logic in Computer Science (LICS), 56–65. 2018. doi:10.1145/3209108.3209189.

Dariusz Biernacki, Maciej Piróg, Piotr Polesiuk, and Filip Sieczkowski. Binders by Day, Labels by Night: Effect Instances via Lexically Scoped Handlers. Proceedings of the ACM on Programming Languages, 4(POPL):1–29, 2020. doi:10.1145/3371116.

Daan Leijen. Type Directed Compilation of Row-Typed Algebraic Effects. In Proceedings of the 44th ACM SIGPLAN Symposium on Principles of Programming Languages (POPL), 486–499. 2017. doi:10.1145/3009837.3009872.

Andrej Bauer and Matija Pretnar. Programming with Algebraic Effects and Handlers. Journal of Logical and Algebraic Methods in Programming, 84(1):108–123, 2015. URL: https://www.sciencedirect.com/science/article/pii/S2352220814000194, doi:10.1016/j.jlamp.2014.02.001.

Gordon Plotkin and Matija Pretnar. Handlers of Algebraic Effects. In Programming Languages and Systems, volume 5502 of Lecture Notes in Computer Science, 80–94. Springer, 2009. URL: https://link.springer.com/chapter/10.1007/978-3-642-00590-9_7, doi:10.1007/978-3-642-00590-9_7.