Skip to content

Typed holes

A typed hole is a term the author has not written yet. QVR gives holes the treatment of the Hazelnut calculus (Omar et al., 2017) and of live programming with typed holes (Omar et al., 2019): every incomplete module that is otherwise well formed has a type, checking proceeds around its holes, and evaluation proceeds around them too, producing results that record which holes they depend on. Holes are a surface and core feature only; nothing compiled or exported ever contains one. The surface forms are in Typed holes.

The core term

The core has one hole form, the value HoleValue ?x : T, carrying its name, its goal T, and its origin. Its typing rule has no premise but the well-formedness of T in the static context:

\[ \frac{\Delta \vdash T \ \mathsf{type}}{\Delta;\Gamma \vdash\ ?x : T \;\Rightarrow\; T} \]

The rule synthesizes T and only T, so a hole at Int in a position that needs Bool is a type error like any other. Because the rule ignores Γ, replacing the hole by any value v with Δ; Γ ⊢ v ⇒ T preserves the typing of everything around it (the substitution lemma); this is what it means for a module to check around its holes.

The checker reports each hole it meets, with the context Γ (and the branch equations) it is checked in, to an observer (HOLE_OBSERVER); a hole's goal is that pair of type and context (hole_goals).

Elaboration

Elaboration is bidirectional. A surface hole ?x in a position that checks against an expected type U elaborates to ?x : U; a hole ?x : T elaborates to ?x : T and requires T = U when the position expects U. A hole in a synthesizing position with no annotation has no goal and is refused (qiec-hole-goal): the elaborator does not invent unification variables for value holes, so every goal is determined by the program as written.

A type hole ?A elaborates to an opaque family ?A with no parameters, indices, or constructors, declared once per name per module. An opaque family is equal only to itself, so the unknown type is rigid: a body that checks with ?A is parametric in it and checks for every type ?A may be filled with. A body that needs ?A to be some particular type is refused at the point that needs it, naming ?A. Since ? cannot begin a declared name, only a type hole declares such a family.

A declaration hole ?f(x : A) : B !{e} elaborates to the computation f whose body is return (?f : B), so its parameters are the hole's bindings and calls of f check against the header.

In the flat program that static interpretation builds, the holes of an imported module lib are named lib:x and lib:?A, as its declarations are, so two modules' holes are never filled or identified by a coincidence of names.

Evaluation around holes

On the reference machine a hole evaluates to a hole closure: the hole paired with the values of the locals in scope where it was reached. A closure is a value that may flow wherever a value flows without being inspected: into a binding, a constructor or record field, a call argument, or a result. A step that inspects a value is strict in it:

  • an operation (arithmetic, a primitive, a projection, a distribution's parameters, a tensor operation) needs every hole its operands contain, at any depth;
  • a case, a conditional, a variant elimination, and an application need only the head of their scrutinee or function;
  • a request needs its arguments, and a value returned into a handler needs the value, because a handler validates it or passes it to host code.

When the next step is strict in an open hole, the machine stops before the step. The result is an Indeterminate that records the closure of the hole the step needs and a Suspension: the step, its bindings, and the frame stack beneath it. When the run finishes but its value holds closures, the result is an indeterminate partial value listing them. A run that never reaches a hole, or binds one and never uses it, is determinate and has exactly the value of the module with that hole filled by anything.

Fill and resume

Filling a hole ?x with a value v that checks against the goal of every occurrence of ?x resolves each closure of ?x by evaluating v under that closure's bindings. A suspended run resumes by replaying the step it stopped before; nothing before that step runs again, so effects already performed (draws, requests, trace events) are not repeated. A partial value is settled by resolving its closures. This is the fill-and-resume property of (Omar et al., 2019): resuming after a filling produces the result of running the filled program, without rerunning it. A filling may itself contain holes, so a run may be refined step by step.

A suspension holds live handler state, so it is a one-shot continuation: it resumes once. A hole needed inside a foreign handler clause, whose frames are host code, stops the run without a suspension; such a run is run again with the filling.

Export

A hole has no compiled form, so every path that leaves the reference machine refuses a module containing one, listing every hole with its goal and position: compilation to PyTorch, transpilation to every target, the stable wire form (which neither encodes nor decodes a hole), fitting, and program runs, whose draws and log joint feed inference. These refusals follow from one declared property of the core, that holes are stuck terms, and go through one function, refuse_holes.

Content identity

A declaration with holes is hashed like any other (Content identity). A hole's name is a name: it becomes an ordinal of the declaration's name space, so renaming a hole consistently keeps the hash, while two distinct holes stay distinct. A hole term is never equal to a value, so a declaration with a hole never shares a hash with a complete one.

Hazelnut (Omar et al., 2017) gives every edit state of a structure editor a type, with holes for missing terms and for terms whose type is inconsistent. Hazel's live evaluation (Omar et al., 2019) proceeds around holes, producing hole closures and supporting fill-and-resume. The marked lambda calculus (Zhao et al., 2024) localizes type errors so that a program with errors still has a type; QVR reports a type error as an error rather than marking it. Live GUIs fill holes with values constructed interactively (Omar et al., 2021); QVR's language server offers fills from the bindings and constructors in scope.

Cited sources

Cyrus Omar, Ian Voysey, Michael Hilton, Jonathan Aldrich, and Matthew A. Hammer. Hazelnut: A Bidirectionally Typed Structure Editor Calculus. In Proceedings of the 44th ACM SIGPLAN Symposium on Principles of Programming Languages (POPL), 86–99. 2017. doi:10.1145/3009837.3009900.

Cyrus Omar, Ian Voysey, Ravi Chugh, and Matthew A. Hammer. Live Functional Programming with Typed Holes. Proceedings of the ACM on Programming Languages, 3(POPL):14:1–14:32, 2019. doi:10.1145/3290327.

Eric Zhao, Raef Maroof, Anand Dukkipati, Andrew Blinn, Zhiyi Pan, and Cyrus Omar. Total Type Error Localization and Recovery with Holes. Proceedings of the ACM on Programming Languages, 8(POPL):68:1–68:28, 2024. doi:10.1145/3632910.

Cyrus Omar, David Moon, Andrew Blinn, Ian Voysey, Nick Collins, and Ravi Chugh. Filling Typed Holes with Live GUIs. In Proceedings of the 42nd ACM SIGPLAN International Conference on Programming Language Design and Implementation (PLDI), 511–525. 2021. doi:10.1145/3453483.3454059.