QVR language reference¶
QVR is the modeling language of quivers, written in .qvr files, which the compiler parses,
checks, elaborates to QIEC, and then runs, fits, or transpiles. The Quivers Indexed Effect Core
(QIEC) is the typed core language that every executable QVR declaration elaborates to. It has
indexed families, effect rows, and handlers, and both the reference machine, the
library's interpreter for QIEC, and the transpilers read it. Its checker, serialization, and runtime interface are specified in the
QIEC developer note.
A computation is a typed procedure that returns a value and may perform
effects: requests, such as drawing a random value,
that a handler answers, possibly by resuming the suspended computation
(Plotkin and Power, 2003; Plotkin and Pretnar, 2009). Its
effect row lists the effect instances it may still request (Leijen, 2014). Elaboration is
the compiler step that rewrites a declaration into explicit QIEC terms. A define declaration
writes a computation directly, and a probabilistic program, a deduction, a parser(...)
built from rule schemata, and a structural encoder, decoder, or loss elaborate to
computations. Since every declaration reaches the same core, qvr run, Program.run, the REPL, the language server,
and the transpilers report the same types, effect rows,
and diagnostic codes.
Pages¶
The reference is organized by declaration kind: each page gives the surface of the declarations named in its row, with examples that compile.
Families, values, and types¶
| Declarations | Page |
|---|---|
index, family with its constructors, pattern; constructor values and case |
Types and indexed families |
record, record and variant values, row-polymorphic computations, function values, where |
Records, variants, and function values |
tensor types over finite types, for tabulations, selection by value, typed reductions, labeled data |
Tensors indexed by finite types |
map, fold, traverse, and normalization over tensors of fixed shape |
Finite collection expressions |
Computations, effects, and programs¶
| Declarations | Page |
|---|---|
define, calls, recursion, branching, pure and effectful binding |
Computations |
effect, instance, handler, effect_law, effect rows |
Effects and handlers |
program with draws, observations, calls, marginalization, groups, and scans |
Probabilistic programs |
| what parsers, deductions, and structural models elaborate to | Generated computation graphs |
Object languages¶
| Declarations | Page |
|---|---|
syntax with its equations, judgment |
Object languages: syntax, equations, and judgments |
notation, open notation, quotations [| ... |] |
Notation and quotation |
meta define, meta import, static assert, splices, deriving |
The meta phase and deriving |
Deductions¶
| Declarations | Page |
|---|---|
deduction signature, deduction ... extends, rule schemata and rules instantiations, generic code over extensions |
Composable deductions and rule schemata |
lexicon, categories, polymorphic entries, template, lexical rule, refines |
Lexicons: polymorphic entries, lexical rules, and refinements |
index map, named lexical entries, and interpret ... [reindex=..., mapped=...] |
Reindexed proof interpretations |
semiring, a deduction's semiring parameter [S = I] or [S : T] |
Semirings |
Modules and packages¶
| Declarations | Page |
|---|---|
import, open, export, qualified and generated names |
Modules |
signature, module, functors and their application, :, :>, with, module parameters, implicit module |
Signatures, functors, and module parameters |
hole, link, share, and sites across instances |
Holes, linking, and sites |
qvr.toml, lockfiles, versions, indexes, dependency lenses |
Packages, lockfiles, and versions |
qvr hash, qvr cache, declaration identities and incremental builds |
Content identity and reuse |
experiment with its data pins, inference method, seed, and sweeps |
Experiments |
.qvr.md documents, qvr example fences, prose references, woven results |
Literate modules |
Theories¶
| Declarations | Page |
|---|---|
theory, include, sums, renaming, hiding, instantiation, view, model, reduct, free |
Theories, views, and models |
Tests, holes, and tools¶
| Declarations | Page |
|---|---|
test, property, example, exact evaluation, mutation runs |
Tests, properties, and examples |
typed holes ?name, goals, runs around holes |
Typed holes |
qvr migrate, grammar revision paths, and migrations of Markdown fences |
Migrating QVR source |
| entry points, runtime providers, execution traces, the language server, targets | Execution and tooling |
The production rules of the grammar are summarized in the QIEC fragment of the grammar.
Parameter telescopes¶
A telescope is an ordered parameter list whose later types may mention earlier parameters (de Bruijn, 1991). QVR uses this structure to express dependencies between an input's type and the indices or objects chosen for a computation. The two stages are visible in both computation and program headers:
define f[n : Nat](x : Real[Fin n]) : Real !{} = ...
program p[Row : FinSet](x : Real[Fin Row]) : Row -> Out
...
Parameter lists may use hanging multiline layout, and an explicit empty runtime
list is written (). The same list layout applies to a computation's static
and value telescopes and to a family's index telescope. Line breaks inside
the delimiters do not begin a body or change a parameter's stage.
The brackets contain static parameters used in checking and specialization. The parentheses contain runtime values. A runtime value may vary between executions of one specialized program; a static argument selects that program's types and structure. Giving an argument implicitly is a separate question: binding can recover an omitted extent from a tensor's checked shape, but that extent still occupies the static context.
A program runtime parameter (count : Nat) is an integer value restricted to
nonnegative values. Its checked representation uses the arithmetic type Int
and retains the nonnegative restriction in the input contract. This differs
from [n : Nat], which binds a static index and can determine a tensor shape.
A static telescope follows the declaration's name, before its runtime
parameters and type. A declaration option block, such as [effects=[Sample]],
follows the type. Position and the binder form name : kind distinguish a
telescope from options, whose entries use names or name=value.
The table gives the corresponding interpretation for each declaration and application.
| Construct | Static information | Runtime information |
|---|---|---|
define f[A : Type](x : A) and f[Real](value) |
type, index, or effect arguments in brackets | value parameters and arguments in parentheses |
program p[Row : FinSet](x : Real[Fin Row]) |
shared QIEC static telescope, extended with object and morphism binders | covariates, responses, and scalar hyperparameters |
schema app[X, Y : Cat] : ... and rule app[X, Y] : ... |
category-pattern variables filled by static substitution | the resulting morphism's arguments, after matching |
C[A, n](field), or C(field) with the statics inferred |
family parameters and constructor indices | constructor fields |
| parameterized effects and handlers | interfaces, indices, and types selected by their static telescopes | operation arguments, return values, and resumed values |
family Vec[A : Type](n : Nat) : Type |
both the uniform parameter A and the refinable index n |
no value is given by declaring the family |
A[Fin n] |
element type and shape | tensor entries given by a value parameter |
contraction f(g : A -> B, ...) |
the declared types of its inputs | the morphisms given, composed under the contraction's wiring |
Parentheses thus do not universally mean runtime application. In a family type,
Vec[A](n) separates uniform parameters from indices that constructors and case
analysis refine; both belong to static type information. And a distribution
application such as Normal(loc=mean, scale=sigma) receives runtime distribution
parameters, though its named argument syntax differs from an ordinary positional call.
For instance, the concentration argument of a tail family is a runtime
real value, while [n : Nat] may fix the number of responses. A parameter
that changes a distribution's support is still a value parameter; it does
not become a static index merely because it is called a shape parameter.
Rule and schema variables use brackets because they are substituted in types and patterns. A contraction's morphism inputs remain parenthesized because the contraction combines the morphisms given. Likewise, names attached to an encoder or decoder identify given components, rather than quantified type variables. These forms retain their own typing rules while making the stage of a program or computation parameter explicit.
Input annotations have no probabilistic effect. An annotation says which values
are admissible; sample, observe, score, and explicit effect requests say
what execution does. This separation lets a response and a covariate share one
type while retaining different roles in fitting and prediction.
Shared syntax does not imply that every executor supports every specialization.
The symbolic checker accepts QIEC type, effect, and index binders alongside
program object and morphism binders. The tensor compiler specializes supported
finite objects and natural-number extents; a structured computation may remain
a CheckedProgram, with its checked interface intact. The
execution reference gives the separate capability boundary.
The shared binder sorts do not give every source application the same
argument grammar. A program call's [...] accepts names and
signed numeric literals, followed by kind checking. It does not accept
arbitrary structured type, effect, or index-constructor expressions there.
Typed define calls use the QIEC static-argument grammar. This is a
restriction of the program-call syntax, separate from what its
checked telescope can represent.
A minimal mixed module¶
The module below declares an indexed family Measurement, whose type records whether a value is
present, an effect Robust with a handler half_weight that halves the value it is asked to
shrink, a pure computation robustify that installs that handler, and a probabilistic program
observations that calls robustify. Every handler in it has a body written in QVR, so it runs
with the built-in core runtime provider alone.
index Availability = Observed | Missing
family Measurement(s : Availability) : Type
constructor Present : Real -> Measurement(Observed)
constructor Absent : Measurement(Missing)
effect Robust
shrink : Real -> Real
instance robust : Robust
handler half_weight for Robust : Real -> Real [coverage=total, forwards=none, implementation=authored]
return x =>
return x
shrink(x : Real) resumes 1 =>
resume(0.5 * x)
define robustify(x : Real) : Real !{} =
handle robust with half_weight in
let adjusted <- perform robust.shrink(x)
return adjusted
object Row : FinSet 8
program observations : Row -> Row
let location <- sample Normal(loc=0.0, scale=2.0)
let scale <- sample HalfNormal(scale=1.0)
let centre <- robustify(location)
observe y : Row <- Normal(loc=centre, scale=scale)
return y
export observations
robustify and observations are both entry points,
the named computations a module exposes to qvr run and Program.run. observations performs
effects through the two built-in effect instances
random and score, and each invocation installs a handler for both. The call to robustify is
pure because half_weight handles the only effect its body performs.
Compilation and execution map¶
The diagram follows a source file from parsing to its two consumers. The reference machine is the library's interpreter for QIEC, the closest standard notion being a definitional interpreter (Reynolds, 1972), and it fixes what a checked program means operationally (reference machine). The transpilers lower the same module to an intermediate representation (IR) shared by all targets and render it for each target.
flowchart TB
subgraph AUTHOR["Source boundary"]
direction LR
S["QVR source<br/><small>qvr-source/v0.22</small>"] -->|parse| P["Quivers parser"] --> A["Typed source AST"]
end
subgraph CORE["Checked interchange boundary"]
direction LR
A -->|elaborate + check| Q["QIEC module<br/><small>qiec-core/v1alpha2</small>"]
Q -->|serialize| J["qiec-json/v3"]
end
subgraph CONSUMERS["Consumers"]
direction LR
Q -->|evaluate| R["Reference machine"] --> C["qvr run · REPL · Program.run"]
Q -->|lower| T["Target-independent IR"] --> H["11 host-language targets"]
end
class S qv-input
class P,A,Q,J,T qv-checked
class R,C,H qv-output
The three identifiers in the diagram name the source grammar, the core, and the serialized form. They are listed under format identifiers.
Target support¶
The reference machine runs every well-typed computation and every distribution family. A computation
that calls a neural network, such as a structural
encoder, also
needs a runtime provider that holds the PyTorch module. A transpile target either emits the whole
reachable computation graph or refuses it with a stable qiec:capability:* diagnostic, and it never
drops an unsupported body. What each target accepts is listed under
Transpilation.
Tutorials and examples¶
The QVR tutorials on indexed data and total functions, effects and handlers, parsing as an effectful computation, and tree autoencoders build one feature each. The Bayesian Semantics Case Study combines indexed data, effects, probabilistic programs, and attachments in one model.
Cited sources¶
Gordon Plotkin and John Power. Algebraic Operations and Generic Effects. Applied Categorical Structures, 11:69–94, 2003. URL: https://link.springer.com/article/10.1023/A:1023064908962, doi:10.1023/A:1023064908962.
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.
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.
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.
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.