betaFeedback

WorksComputer Science

Compiling Environment Classifiers to Contextual Modal Types

Abstract

Type systems for metaprogramming take one of two approaches to safeguarding the scopes of variables in generated code. They either use Environment Classifiers (EC) to identify scopes by name (Taha and Nielsen, 2003), or they use Contextual Modal Type Theory (CMTT) to match scopes by structure (Nanevski, Pfenning, and Pientka, 2008). In this paper, we connect these two approaches by compiling a calculus with polymorphic environment classifiers, subtyping, and mutable references to a CMTT-based calculus with polymorphic contexts, mutable references, and first-class substitutions. The key idea is to map each environment classifier to a context. We prove that the compilation preserves types and that compilation preserves the behavior of metaprograms. Both proofs are mechanized in Agda. The compilation shows that CMTT is at least as expressive as environment classifiers. We conjecture that the converse compilation is difficult, because environment classifiers impose a stricter regime on the metaprogram.

Introduction

Metaprogramming is the paradigm in which a program in a metalanguage generates a program in a code language. In this paper, we study the relationship between two typing disciplines for metaprogramming with open code, that is, code fragments with free variables: environment classifiers [25], which name the scope in which the code is meaningful, and contextual modal types [20], which spell out that scope as a typing context. Both guarantee that generated programs are well typed and well scoped, but they have been developed largely independently. We show that every scope named by a classifier can be translated into a context, define a compiler from a calculus with environment classifiers to one with contextual modal types, and prove that compilation preserves types and that source and target programs generate the same code.

In the tradition of MetaML [26, 27], the type system of the metalanguage tracks the type of quoted code, so that every generated program is well typed.

The difficulty lies with open code: the free variables of a fragment are bound by binders of the code language outside the fragment, so the type system must also record the scope in which the fragment is meaningful. Otherwise the fragment may leave that scope, through run or through a mutable reference, and the generated program ends up with unbound variables, a failure known as scope extrusion [14, 15, 16].

Two lines of research (Figure 1) have developed type systems that track open code fragments and keep generated code well scoped in the presence of run or mutable references. Both lines descend from the Curry–Howard-style analyses of staged computation. The calculus λ□\lambda^{\Box} of Davies and Pfenning [8, 9] is based on the necessity operator □\Box of the intuitionistic modal logic S4: a term of type □A\Box A is closed code of type AA, which can safely be evaluated, but the calculus manipulates closed code only. On the other hand, the calculus λ∘\lambda^{\circ} of Davies [6, 7] is based on the “next” operator ∘\circ of linear-time temporal logic: a term of type ∘A\circ A is code that may contain free variables. Thus λ∘\lambda^{\circ} enables the manipulation of open code, which makes it a foundation for the partial evaluation of functional languages [7]. However, λ∘\lambda^{\circ} does not include run because its type system would not be able to prevent scope extrusion in that case [7, 25].

Two approaches to the scopes of generated code: environment classifiers and contextual modal types

Figure 1. Two approaches to the scopes of generated code: environment classifiers (left) and contextual modal types (right). Both lines descend from the modal and temporal analyses of staged computation and are connected through compilation in this paper.

Practical metaprogramming needs both the manipulation of open code and run. Environment classifiers start from λ◯\lambda^{\bigcirc} and add run; contextual modal types start from λ□\lambda^{\square} and add support for open code.

Environment classifiers. The research on environment classifiers, the left arrow of Figure 1, grows out of λ◯\lambda^{\bigcirc} and MetaML, the latter of which combines open code with run [17, 26, 27]. As an early solution, Calcagno et al. [3, 4] propose closed types, which single out the code types whose values are known to be closed. They restrict run to code of closed type, and similarly for constructing mutable references to code. Taha and Nielsen [25] then introduce Environment Classifiers (EC). An environment classifier is a name of a scope, and a code type ⟨T⟩α\langle T\rangle^{\alpha} records the classifier α\alpha of the scope in which its code, possibly open, is meaningful. The type system checks by name that a code fragment only mentions variables, and only splices code, of the same classifier. It permits run only on code whose classifier is universally quantified, which guarantees that the code is closed. Early versions of MetaOCaml adopted this discipline [5, 24], and Tsukada and Igarashi [28] give it a logical foundation through a Curry–Howard correspondence with a multi-modal logic.

Refined environment classifiers. Mutable references reintroduce the scope extrusion problem, because open code can be smuggled out of its scope by storing it in a reference. Kiselyov, Kameyama, and Sudo [15] rule out such scope extrusions statically with refined environment classifiers, which mark every code-level binder with its own classifier, ordered by the nesting of the binders, so that storing code in a reference cell with an incompatible classifier is a type error. Isoda et al. [11] add algebraic effects and handlers to refined environment classifiers, and Murase and Igarashi [18] extend them to multi-level staging with run, cross-stage persistence, and polymorphism over classifiers.

Contextual modal types. The research on contextual modal types, the right arrow of Figure 1, grows out of λ□\lambda^{\square}. Building on the judgmental reconstruction of modal logic by Pfenning and Davies [21], Nanevski, Pfenning, and Pientka [20] restrict the necessity modality to an explicit context and obtain Contextual Modal Type Theory (CMTT). A contextual code type [Ψ]A[\Psi]A lists the context Ψ\Psi in which its code is well typed, so open code becomes expressible, and using the code in another context requires an explicit substitution [1] that provides a term for every variable in Ψ\Psi. Contextual modal types thus give a uniform account of code with free variables and of the substitutions that instantiate those variables. CMTT has been extended in directions relevant to our paper: Moebius [12] combines it with System F and pattern matching on code. λ∀[]\lambda_{\forall[]} [19] adds polymorphic contexts. Contextual MetaML [29] adds higher-order references that may hold open code and establishes full abstraction via operational game semantics. Closely related to our work, Murase et al. [19] translate λ◯\lambda^{\bigcirc}, restricted to two levels, into λ∀[]⋅\lambda_{\forall[]}^{\mathord{\cdot}}. There are two insights from their translation that we use in our compilation from EC to CMTT. First, quote and splice are translated by constructing code contexts and explicit substitutions, respectively. Second, code contexts are made explicit by following the binding structure of generated code.

Two other type systems index code types by the typing context, independently of CMTT. Kim, Yi, and Calcagno [13] let the surrounding context capture variables by name, so code may be spliced into any context that binds at least its free variables, whereas the λ[ ]\lambda^{[\,]} calculus of Rhiger [23] binds them lexically.

Both EC and CMTT record information about the scope of open code in the code type and support manipulating open code in the presence of mutable references. However, they differ in how code variables are bound, as observed by Murase and Igarashi [18]. We elaborate on the difference using examples.

Variable binding in environment classifiers. Consider the following program in a metalanguage with environment classifiers. It assembles two pieces of code, c1 and c2, and splices them into a third. We write ``...''α_\alpha for a quotation with classifier α\alpha and a tilde (∼\sim) for a splice. Variable bindings are highlighted.

1  ``(λ (x:Int) α. λ (y:Int) β.
2      ~(let c1 = `` x * 2''α in /* the scope of α contains x */
3       let c2 = `` x + y''β in /* the scope of β contains x and y */
4       ``~c1 - ~c2''β))
5  43 1''ε

In a calculus with environment classifiers, a code variable is bound by a λ\lambda-abstraction in its lexical scope, even though quotations and splices intervene. The occurrences of x in c1 and c2 are bound by λ\lambda (x:Int) α\alpha (red arrow), and the y in c2 by λ\lambda (y:Int) β\beta (blue arrow). This binding structure is inherited from λ\lambda⃝ [6, 7].

Scopes of binders in code are identified by names such as α\alpha and β\beta. The abstraction λ\lambda (x:Int) α\alpha binds both the code variable x and the classifier α\alpha that names its scope (line 1), and the inner abstraction λ\lambda (y:Int) β\beta binds y and β\beta. The scope named by β\beta lies within the scope named by α\alpha, so the type system introduces the subtyping α<:β\alpha<: \beta. Both quotes and their code types are annotated with classifiers, so c1 (line 2) has type Int''$_\alpha$ and c2 has type Int''β_\beta. Due to the subtyping in this example, a piece of code with classifier α\alpha may be spliced wherever classifier α\alpha or β\beta is expected such as the splice of c1 (line 4), and β\beta may be spliced wherever β\beta is expected, such as c2 (line 3). A calculus based on EC thus relates scopes through their names and the subtyping between those names. We therefore refer to environment classifiers as a nominal approach to ensuring scope safety. The type system ensures that metaevaluation ultimately produces closed code, in this example ``(λ\lambda (x:Int) α\alpha. λ\lambda (y:Int) β\beta. (x * 2) - (x + y)) 43 1''ε_\varepsilon, where ε\varepsilon is the classifier of the empty scope.

Variable binding in contextual modal types. Here is the same program in a metalanguage with contextual modal types. Variable bindings are highlighted.

1  ``∅ ⊢ (λ (x:Int). λ (y:Int).
2       ~(let c1 = `` x:Int ⊢ x * 2'' in
3          let c2 = `` x:Int, y:Int ⊢ x + y '' in
4          ``x:Int, y:Int ⊢ ~c1{x=x} - ~c2{x=x,y=y}''){x=x,y=y})
5       43 1''
Contextual modal types (CMTT)

In a calculus with contextual modal types, each piece of code carries a typing context, such as x:Intx:\mathrm{Int} and (x:Int,y:Int)(x:\mathrm{Int}, y:\mathrm{Int}), which identifies the scope of the code by spelling out the variables that the code may mention together with their types. In CMTT, it is this context annotation on a quotation that binds the variables of the quoted code, not an enclosing λ\lambda-abstraction. The annotation x:Intx:\mathrm{Int} of c1 binds the x in x * 2 (red arrow, line 2), and the annotation (x:Int,y:Int)(x:\mathrm{Int}, y:\mathrm{Int}) of c2 binds both x (yellow arrow) and y (blue arrow) in x + y (line 3). The x in c1, the x in c2, and the x bound by λ\lambda (x:Int)(x:\mathrm{Int}) (line 1) are therefore three different variables, so every splice must say how the variables of the spliced code relate to those of the surrounding context. The splice ~c1{x=x} (line 4) applies to c1 an explicit substitution of type (x:Int)⇒(x:Int,y:Int)(x:\mathrm{Int}) \Rightarrow(x:\mathrm{Int}, y:\mathrm{Int}), which maps the x of c1, bound by its context annotation, to the x of the surrounding context (x:Int,y:Int)(x:\mathrm{Int}, y:\mathrm{Int}). Similarly, the splice ~c2{x=x,y=y} substitutes the two variables of c2, and the outermost splice applies {x=x,y=y}, which maps the variables of the annotation on line 4 to the ones bound by the two λ\lambda-abstractions. The program makes each substitution explicit, and the type system checks that it provides a term for every variable of the context. Whether code of one context can be used in another thus depends only on the variable names and their types. We therefore call CMTT the structural approach to scopes. Code types mention contexts, so c1 has type x:Int ⊢ Int'' and c2 has type x:Int, y:Int ⊢ Int''. Metaevaluation performs the substitutions and produces the closed code ``∅ ⊢ (λ (x:Int). λ (y:Int). (x * 2) - (x + y)) 43 1'', where ∅ is the empty context.

In CMTT, code fragments can be assembled even if their scopes are not nested, which EC cannot express. In the following program, c is constructed outside any λ\lambda-abstraction in code and carries the context y:Inty:\mathrm{Int}, which binds y (line 1, highlighted), and is then spliced under the abstraction λ\lambda (x:Int)(x:\mathrm{Int}).

1  let c = `` y:Int ⊢ y * 2'' in
2  ``∅ ⊢ (λ (x:Int). ~c{y=x}) 21''
Reconnecting binders in CMTT

The substitution {y=x} reconnects the variable y of c to the variable x bound by the abstraction (line 2, highlighted), and metaevaluation produces the closed code ``∅ ⊢ (λ (x:Int). x * 2) 21''.

This program has no counterpart in EC, where the code y * 2 can only be written within the scope of a λ\lambda that binds y and can only be spliced into code within that scope. It can never be constructed outside any λ\lambda and rewired to a binder of an unrelated scope. The difference between the two approaches mirrors the one between nominal and structural type systems [22]: a nominal type system relates two types only when the program declares their names related, whereas a structural one relates them whenever their types match.

Environment classifiers identify the scope of code by a name, whereas contextual modal types identify it by the typing context. We observe that a classifier can be compiled into a code-level context that includes the variables and types in scope at the classifier. This intuition leads to the main contribution of our paper: compiling EC into CMTT, and thereby connecting the nominal and structural approaches to scopes.

This paper: compiling from EC to CMTT. Our compilation extends the translation of Murase et al. [19], discussed above, to a source language with environment classifiers and mutable references. The source language, λRefα\lambda^{\alpha}_{\mathrm{Ref}}, is a metaprogramming calculus with refined environment classifiers and mutable references, extended with unrestricted subtyping and subtype-bounded classifier polymorphism. The target language, λRefCtx\lambda^{\mathrm{Ctx}}_{\mathrm{Ref}}, combines features of recent extensions of contextual modal type theory. The λRefCtx\lambda^{\mathrm{Ctx}}_{\mathrm{Ref}} calculus is a two-level metaprogramming language that supports mutable references and polymorphic contexts. To these, λRefCtx\lambda^{\mathrm{Ctx}}_{\mathrm{Ref}} adds first-class substitutions in support of the compilation. Both λRefα\lambda^{\alpha}_{\mathrm{Ref}} and λRefCtx\lambda^{\mathrm{Ctx}}_{\mathrm{Ref}} are restricted to two levels, because going beyond two levels with mutable references in CMTT is an open problem [29]. We do not include run, because mutable references already make scope safety nontrivial, and the mechanism that tracks open code fragments for typing run is analogous to the one for references. We define a type-directed compilation from λRefα\lambda^{\alpha}_{\mathrm{Ref}} to λRefCtx\lambda^{\mathrm{Ctx}}_{\mathrm{Ref}} and prove that it preserves types and that source and target programs generate the same code. This connects the nominal scopes of EC to the structural contexts of CMTT, and it is the main contribution of this paper.

The key to our compilation is identifying the code-level context that an EC corresponds to. The following program and its compilation illustrate this idea.

``(λ (x:Int) α. λ (y:Int) β.
   ~(let c = ``x + y'' β
    in c))
41 1'' ε

compiles to ⇝

`` ∅ ⊢
   (λ (x:Int). λ (y:Int).
      ~(let c = `` x:Int, y:Int ⊢ ~`` x:Int ⊢ x''{x=x} + y''
       in c){x=x,y=y})
41 1''

For each code-level binder annotated with a classifier, such as β\beta, the compilation recovers the context Γβ\Gamma_{\beta} of the scope that the binder introduces by following EC subtyping outwards. The binder lies in the scope of an enclosing classifier α\alpha, so Γβ\Gamma_{\beta} is Γα\Gamma_{\alpha} extended with the variable that the binder binds, and the chain ends at the empty classifier ε\varepsilon, whose context is empty. The compiler calculates the context for each classifier: Γα=x:Int\Gamma_{\alpha} = x\mathord{:}\mathrm{Int}, Γβ=(x:Int,y:Int)\Gamma_{\beta} = (x\mathord{:}\mathrm{Int}, y\mathord{:}\mathrm{Int}), and Γε=∅\Gamma_{\varepsilon} = \emptyset. Every classifier annotation of the source is then replaced by the context that it corresponds to, shown in matching colors: α\alpha corresponds to x:Intx\mathord{:}\mathrm{Int} and β\beta to x:Int,y:Intx\mathord{:}\mathrm{Int}, y\mathord{:}\mathrm{Int}. The quotation ``x+yx + y'' β\beta becomes a quotation annotated with x:Int,y:Intx\mathord{:}\mathrm{Int}, y\mathord{:}\mathrm{Int}, and the outer quotation, of the empty classifier ε\varepsilon, is annotated with the empty context ∅\emptyset. The code-level abstractions lose their classifiers, since the contexts now carry that information. The interesting part is the framed occurrence of xx, which is bound in the scope named α\alpha but used in code of classifier β\beta. This is allowed because the scope named by β\beta lies within the scope named by α\alpha. In the target, this occurrence compiles to a quotation of xx in its own context x:Intx\mathord{:}\mathrm{Int}, spliced into the larger context (x:Int,y:Int)(x\mathord{:}\mathrm{Int}, y\mathord{:}\mathrm{Int}) under the substitution {x=x}\{x=x\}. Scope nesting therefore becomes context weakening, witnessed by a substitution. Finally, the splice around the let-expression gains the substitution {x=x,y=y}\{x=x,y=y\}, which maps the two variables of the quotation’s context to the ones bound by the λ\lambda-abstractions.

Metatheoretical results. We prove in Agda that the compilation preserves types and is semantically correct by a bisimulation between configurations of the source and the target. Contextual modal types are thus at least as expressive as environment classifiers, in that any scope described by a name can also be described by a context. We conjecture that the converse direction is difficult, because a classifier can only name a scope that arises from nested binders in code, whereas a contextual modal type may mention any context.

Technical Contributions. This paper makes the following contributions.

  1. We present λRefCtx\lambda_{\mathrm{Ref}}^{\mathrm{Ctx}}, a two-level CMTT calculus that adds first-class substitutions (to support the compilation) and combines support for mutable references [29] and polymorphic contexts [19].

  1. We define a type-directed compilation from λRefα\lambda_{\mathrm{Ref}}^{\alpha} to λRefCtx\lambda_{\mathrm{Ref}}^{\mathrm{Ctx}}, thereby establishing the connection between the nominal scopes of environment classifiers and the structural contexts of contextual modal types.

  1. We prove that the compilation preserves types and that it is semantically correct: source and target programs generate the same code and agree on divergence. Both proofs are fully mechanized in Agda.

Section 2 introduces the compilation through examples, Sections 3 and 4 present λRefα\lambda_{\mathrm{Ref}}^{\alpha} and λRefCtx\lambda_{\mathrm{Ref}}^{\mathrm{Ctx}}, Section 5 defines the compilation, Section 6 proves it type preserving and semantically correct, and Section 7 concludes. The Appendix, the Agda proof scripts, and the Haskell code for a prototype implementation of the compilation are all in the supplementary material of this paper.

From EC to CMTT, by Examples

This section demonstrates the compilation from λRefα\lambda_{\mathrm{Ref}}^{\alpha} to λRefCtx\lambda_{\mathrm{Ref}}^{\mathrm{Ctx}} through examples. We start with the basic translation of classifiers into contexts (§ 2.1). We then compile the two forms of subtyping in λRefα\lambda_{\mathrm{Ref}}^{\alpha}, between classifiers and between types (§ 2.2), into substitutions and coercion functions in λRefCtx\lambda^{\mathrm{Ctx}}_{\mathrm{Ref}}. Finally, we compile classifier polymorphism and subtype constraints into context polymorphism and first-class substitutions (§ 0.2.3).

Compiling Environment Classifiers to Contexts

In this section, we illustrate the basic pattern of our compilation, which has two parts. First, classifiers become contexts, so code types and quotations mention contexts instead of classifiers. Second, splices become splices under substitutions. We also preview the semantic correctness of the compilation.

Consider the following metaprogram, which generates the code of x4x^4 by squaring twice.

1  ``λ (x:Int) α.
2  ~(let square = λ (c:``Int''α). ``~c * ~c''α
3  in square (square ``x''α))''ε
                                      Source program (EC)

The program uses a single classifier. The λ\lambda-abstraction on line 1 binds x and names its scope α\alpha. The metafunction square (line 2) takes code of classifier α\alpha and returns code of the same classifier that multiplies the argument of the function by itself. Applying square twice to x''$\alpha$ (line 3) produces the code λ\lambda (x:Int) α\alpha. (x * x) * (x * x)''ϵ\epsilon.

The compilation first identifies the context of each classifier. The abstraction on line 1 binds x in the empty scope, so Γα=x\Gamma_\alpha= x:Int and Γϵ=∅\Gamma_\epsilon= \varnothing. The compilation then replaces every classifier by its context and equips every splice with a substitution. In the listing below, contexts are highlighted in the color of their classifier, and substitutions are underlined.

1  ``∅ ⊢ λ (x:Int).
2  ~(let square = λ (c:``x:Int ⊢ Int''). ``x:Int ⊢ ~c{x=x} * ~c{x=x}''
3  in square (square ``x:Int ⊢ x'')){x=x}''
                                      Target program (CMTT)

Code types and quotations now mention contexts instead of classifiers. The type Int''$\alpha$ of the parameter c becomes x:Int ⊢\vdash Int'', and each quotation ...''$\alpha$ becomes x:Int ⊢\vdash ...''. Every splice applies a substitution (underlined). Here every quotation is spliced into code of the same classifier α\alpha, so every substitution is the identity on Γα\Gamma_\alpha, written {x=x}\{x=x\}. The substitutions connect different binders. On line 2, they map the x of the spliced code, bound by its context annotation x:Int, to the x bound by the context annotation of the surrounding quotation; on line 3, to the x bound by the λ\lambda-abstraction of line 1.

The target program metaevaluates to ``∅⊢λ\varnothing\vdash\lambda (x:Int). (x * x) * (x * x)''. This example shows the basic pattern of the compilation: classifiers become contexts, and splices become splices with substitutions. Both the source metaprogram and the target produce λ\lambda (x:Int). (x * x) * (x * x). In general, we prove that the target always generates the same code as the source does with the semantic correctness theorem in § 2.1.1.

Compiling Subtyping

Code of an outer scope should be usable wherever code of an inner scope is expected, and λRefα\lambda_{\mathrm{Ref}}^{\alpha} allows this through subtyping. From λRefα\lambda_{\mathrm{Ref}}^{\alpha} to λRefCtx\lambda_{\mathrm{Ref}}^{\mathrm{Ctx}}, we compile subtyping away through a coercion semantics [2, 22]. In the following subsections, we show that subtyping between classifiers compiles to a substitution, and subtyping between types compiles to a coercion function that applies the substitution through a splice.

Subtyping between classifiers. In the example of § 0.1, a variable of an outer scope occurs in code of an inner scope, and the compilation moves it there by splicing it under a substitution and re-quoting it in the inner context. In general, a derivation of α<:β\alpha<: \beta compiles to a substitution of type Γα⇒Γβ\Gamma_{\alpha} \Rightarrow\Gamma_{\beta}, where Γα\Gamma_{\alpha} and Γβ\Gamma_{\beta} are the contexts that α\alpha and β\beta compile to. The following example demonstrates the compilation of subtyping between classifiers.

1  ``(λ (x:Int) α. λ (y:Int) β. λ (z:Int) γ.
2     ~``x + y''γ)
3  1 41 0''ε                         Source program (EC): subtyping

We have α<:β<:γ\alpha<: \beta<: \gamma from the three nested λ\lambda-abstractions. The quotation ``x + y''γ (line 2) mentions two variables of outer scopes. The variable y is bound in the scope named by β\beta, so its occurrence requires β<:γ\beta<: \gamma. The variable x is bound in the scope named by α\alpha, so its occurrence requires α<:γ\alpha<: \gamma, which holds by transitivity through β\beta. Here is the compilation of the program.

1  ``∅ ⊢ (λ (x:Int). λ (y:Int). λ (z:Int).
2     ~`` x:Int, y:Int, z:Int ⊢
3       ~`` x:Int ⊢ x'' ({x=x} ∘ {x=x,y=y}) +
4       ~`` x:Int, y:Int ⊢ y'' {x=x,y=y}''
5       {x=x,y=y,z=z})
6  1 41 0''                         Target program (CMTT)

The contexts are Γα=x:Int\Gamma_{\alpha} = x:\mathrm{Int}, Γβ=(x:Int,y:Int)\Gamma_{\beta} = (x:\mathrm{Int}, y:\mathrm{Int}), and Γγ=(x:Int,y:Int,z:Int)\Gamma_{\gamma} = (x:\mathrm{Int}, y:\mathrm{Int}, z:\mathrm{Int}). The occurrence of y is spliced under the substitution {x=x,y=y}\{x=x,y=y\}, which is of type Γβ⇒Γγ\Gamma_{\beta} \Rightarrow\Gamma_{\gamma} (line 4, underlined). Transitivity of subtyping compiles to the composition of substitutions. The occurrence of x is spliced under the substitution {x=x}∘{x=x,y=y}\{x=x\} \circ\{x=x,y=y\} (line 3, underlined), which composes the substitution for α<:β\alpha<: \beta with the one for β<:γ\beta<: \gamma and thus has type Γα⇒Γγ\Gamma_{\alpha} \Rightarrow\Gamma_{\gamma}.

Subtyping between types. The following example demonstrates subtyping between types by storing code of classifier α\alpha into a reference cell that expects code of classifier β\beta. We have α<:β\alpha<: \beta from the nesting of λ\lambda-abstractions.

1  ``(λ (x:Int) α. λ (y:Int) β.       Source program (EC): subtyping between types
2     ~(let r = ref ``0''β in          /* r : Ref ``Int''β */
3       r := ``x''α;                    /* ``Int''α <: ``Int''β */
4       !r))
5  42 24''ε

The reference r holds code of classifier β\beta and has type Ref Int''$\beta$ (line 2). Line 3 assigns to it the code x''α\alpha, which the type system accepts by subsumption. Line 4 reads the code back and splices it into the inner abstraction. Here is the compilation of the program.

1  ``∅ ⊢ (λ (x:Int). λ (y:Int).
2     ~(let r = ref ``x:Int, y:Int ⊢ 0'' in
3        r := (λ (c:``x:Int ⊢ Int''). ``x:Int, y:Int ⊢ ~c{x=x}'')
4             ``x:Int ⊢ x'';
5        !r){x=x,y=y})
6  42 24''

The reference holds code of type x:Int, y:Int ⊢ Int'' (line 2), whereas the code assigned to it, x:Int ⊢ x'' (line 4), has type x:Int ⊢ Int''. The highlighted contexts correspond to $\beta$ and $\alpha$. To convert between the two types, the compilation inserts the framed function on line 3, which is the coercion that Int''α\alpha <: ``Int''β\beta compiles to. It splices its argument under the substitution {x=x}\{x=x\} and re-quotes the result in the context (x:Int, y:Int). In general, the coercion between two code types applies the substitution between their classifiers through a splice and re-quotes the result, and the compilation inserts it wherever the typing derivation of the source uses subsumption.

Compiling Bounded Quantification for Classifiers

ΛRefα\Lambda^\alpha_{\mathrm{Ref}} expresses bounded quantification for classifiers with two features, classifier polymorphism and subtype constraints. We compile these language features separately. Classifier abstraction and application compile to context abstraction and application. Subtype constraint introduction and elimination compile to abstraction over and application to a first-class substitution, respectively.

A metafunction that manipulates code of a fixed classifier, such as square in § 0.2.1, can only be used in one scope. For reuse across scopes, ΛRefα\Lambda^\alpha_{\mathrm{Ref}} supports classifier polymorphism, an idea that Kiselyov et al. [15] sketch informally and Murase and Igarashi [18] formalize. (Λ α. M)(\Lambda\ \alpha.\ M) abstracts over a classifier, and M[β]M[\beta] instantiates the abstraction with β\beta. Subtype constraints let a polymorphic metafunction mention the variables of a specific scope. For example, in the term β<:γ⇒M\beta<: \gamma\Rightarrow M, where γ\gamma is a polymorphic classifier and β\beta the name of a scope, the constraint β<:γ\beta<: \gamma lets code of classifier γ\gamma in MM use the variables of the scope named by β\beta. The elimination form M∙M \mathbin{\bullet} discharges the subtype constraint in the type of MM by checking that the subtyping holds.

The following example uses bounded quantification for classifiers, expressed as a classifier abstraction followed by a subtype constraint.

1  ``(λ (x:Int) α. λ (y:Int) β.
2  ~(let l = Λ γ. β <: γ ⇒ ``x + y''γ in
3      l [β] • ))
4  42 0''ε

The function l (line 2) generates the code x + y at the polymorphic classifier γ. Its body mentions x and y, bound in the scopes named by α and β, and both are visible in code of classifier γ thanks to the constraint β <: γ. Line 3 instantiates l at β and discharges the constraint, which reads β <: β after instantiation and holds by reflexivity. The result has type ``Int''β and is spliced into the inner abstraction of the same classification.

Here is the compilation. The frames of matching colors relate the constructs of the source to those of the target. We explain the compilation below.

1  ``∅ ⊢ (λ (x:Int). λ (y:Int).
2  ~(let l = Λ γ. λ (s:(x:Int, y:Int) ⇛ γ ).
3      `` γ ⊢ ~``x:Int ⊢ x'' ({x=x} ◦ s) +
4                  ~``x:Int, y:Int ⊢ y'' s'' in
5          l [x:Int, y:Int] {x=x,y=y}){x=x,y=y})
6  42 0''

Compiling classifier polymorphism. Classifier abstraction compiles to context abstraction (line 2, yellow box), and classifier application to context application (line 5, green box). To support the compilation, λRef supports context polymorphism. The polymorphic classifier γ compiles to a context variable of the same name, so the quotation ``x + y''γ is now in the context γ (line 3, highlighted). The instantiation at β becomes an instantiation at the context of β, which is (x:Int, y:Int).

Compiling subtype constraints. Recall from § 2.2 that the coercion for subtyping between classifiers is a substitution. Consequently, a subtype constraint introduction compiles to a function that takes a substitution (line 2, red box). The function takes the substitution for the subtyping β <: γ, so the argument is of type (x:Int, y:Int) ⇛ γ. A constraint elimination compiles to a function application. In the example, the application passes the substitution for the subtyping that the elimination checks, which is β <: β after instantiation, so the argument is the identity substitution {x=x,y=y} (line 5, blue box).

In summary, bounded quantification for classifiers compiles to context polymorphism and to abstraction over and application to first-class substitutions. To support compilation, substitutions must be first-class citizens in λRef.

Source Language: LambdaAlphaRef with Environment Classifiers

In this section, we define λαRef, the source language of the compilation. λαRef is a two-level calculus with refined environment classifiers [15] and mutable references, so a code type records the classifier that names the scope in which

base typesι::=Int∣Unitcode typesS,T::=ι∣S→TEC variablesα,β,γECse::=α∣εmeta typesA,B::=ι∣A→B∣Ref⁡A∣“T”e∣∀α. A∣e1<:e2⇒AcontextsΓ::=∅∣Γ,α∣Γ,e1<:e2∣Γ,x:A∣Γ,(x:T)subtype contextsΘ::=∅∣Θ,e1<:e2constantskterm variablesx,y,z,…code termsL∘,M∘,N∘::=k∣x∣λ(x:T)α.M∘∣M∘ N∘∣∼Mmeta termsL,M,N::=k∣x∣λx:A.M∣M NL,M,N::=∣Λα.M∣M[e]∣e1<:e2⇒M∣M∙L,M,N::=∣“M∘”e∣ref⁡M∣M:=N∣!M\begin{aligned} &\text{base types} && \iota::= \mathrm{Int} \mid\mathrm{Unit}\\ &\text{code types} && S,T ::= \iota\mid S \to T\\ &\text{EC variables} && \alpha,\beta,\gamma\\ &\text{ECs} && e ::= \alpha\mid\varepsilon\\ &\text{meta types} && A,B ::= \iota\mid A \to B \mid\operatorname{Ref} A \mid\text{``}T\text{''}_{e} \mid\forall\alpha.\,A \mid e_{1}<:e_{2}\Rightarrow A\\ &\text{contexts} && \Gamma::= \varnothing\mid\Gamma,\alpha\mid\Gamma,e_{1}<:e_{2} \mid\Gamma,x:A \mid\Gamma,(x:T)\\ &\text{subtype contexts} && \Theta::= \varnothing\mid\Theta,e_{1}<:e_{2}\\ &\text{constants} && k\\ &\text{term variables} && x,y,z,\ldots\\ &\text{code terms} && L^{\circ},M^{\circ},N^{\circ} ::= k \mid x \mid\lambda(x:T)^{\alpha}.M^{\circ} \mid M^{\circ}\ N^{\circ} \mid\sim M\\ &\text{meta terms} && L,M,N ::= k \mid x \mid\lambda x:A.M \mid M\ N\\ &&&\phantom{L,M,N ::=}\mid\Lambda\alpha.M \mid M[e] \mid e_{1}<:e_{2}\Rightarrow M \mid M_{\bullet}\\ &&&\phantom{L,M,N ::=}\mid\text{``}M^{\circ}\text{''}_{e} \mid\operatorname{ref} M \mid M:=N \mid{!}M \end{aligned}

the code is meaningful, and a reference may hold open code. To these, λRef⁡α\lambda_{\operatorname{Ref}}^{\alpha} adds unrestricted subtyping between types, and classifier polymorphism with bounded quantification in the style of R2EC [18]. We define the syntax in § 0.3.1, the type system in § 0.3.2, and the operational semantics in § 3.3.

Syntax of LambdaAlphaRef

Figure 2 defines the syntax of λRef⁡α\lambda_{\operatorname{Ref}}^{\alpha}. It generates code in STLC, and the metalanguage uses classifiers to ensure that the generated code is well scoped in the presence of mutable references. We describe the EC-specific features below.

Syntax of lambda Ref

Figure 2. Syntax of λRef⁡α\lambda_{\operatorname{Ref}}^{\alpha}.

Code and environment classifiers. Following Taha and Nielsen [25], a quotation “M∘”e\text{``}M^{\circ}\text{''}_{e} and a code type “T”e\text{``}T\text{''}_{e} carry an environment classifier ee, which represents the scope in which the code is meaningful. A classifier is either ε\varepsilon, which is the name of the empty scope, or a classifier variable α\alpha. Following the refined environment classifiers of Kiselyov et al. [15], a code-level abstraction λ(x:T)α.M∘\lambda(x:T)^{\alpha}.M^{\circ} binds both its parameter xx and the classifier α\alpha that gives the scope of its body a name. A splice ∼M\sim M integrates the code that the meta term MM produces into the surrounding code.

Bounded quantification for classifiers. Following R2EC [18], λRef⁡α\lambda_{\operatorname{Ref}}^{\alpha} supports bounded quantification over classifiers, which it expresses with two features, classifier polymorphism and subtype constraints. The universal type ∀α. A\forall\alpha.\,A quantifies over classifiers. Classifier abstraction Λα.M\Lambda\alpha.M binds a classifier variable (α\alpha), whereas classifier application M[e]M[e] instantiates it with a classifier (ee). A subtype-constrained type e1<:e2⇒Ae_{1}<:e_{2}\Rightarrow A introduces the constraint e1<:e2e_{1}<:e_{2} in the body type AA. The subtype constraint introduction form e1<:e2⇒Me_{1}<:e_{2}\Rightarrow M assumes the constraint while typing MM, and its elimination form M∙M_{\bullet} discharges the constraint where the subtyping holds.

Type System of λRefα\lambda_{\mathrm{Ref}}^{\alpha}

Figure 3 shows selected typing rules of λRefα\lambda_{\mathrm{Ref}}^{\alpha}; the complete rules are in Figure 13 of the Appendix. All judgments carry a typing context Γ\Gamma, which binds classifiers, code and meta variables, and records subtype constraints. The typing judgment for meta terms takes the form Γ⊢M:A\Gamma\vdash M : A. The typing judgment for code terms, Γ;e⊢M∘:T\Gamma; e \vdash M^{\circ} : T, also carries the classifier ee of the nearest enclosing scope. The two judgments are mutually recursive through quotation and splice. Well-formedness and subtyping of types are straightforward. They are defined in Figure 11 and Figure 12 of the Appendix, respectively.

Selected typing rules of lambda Ref

Figure 3. Selected typing rules of λRefα\lambda_{\mathrm{Ref}}^{\alpha} (excerpt of Figure 13 of the Appendix).

Well-formedness rules for ECs and meta types
Γ⊢εα∈ΓΓ⊢αΓ⊢ιΓ⊢AΓ⊢BΓ⊢A→BΓ⊢AΓ⊢Ref⁡AΓ⊢eΓ⊢“T”eΓ,α⊢Aα∉ΓΓ⊢∀α.AΓ⊢e1Γ⊢e2Γ⊢AΓ⊢e1<:e2⇒A\begin{aligned} &\Gamma\vdash\varepsilon && \frac{\alpha\in\Gamma}{\Gamma\vdash\alpha} && \Gamma\vdash\iota \\[1ex] &\frac{\Gamma\vdash A \qquad\Gamma\vdash B}{\Gamma\vdash A \to B} && \frac{\Gamma\vdash A}{\Gamma\vdash\operatorname{Ref} A} \\[1ex] &\frac{\Gamma\vdash e}{\Gamma\vdash\text{``T''}e} && \frac{\Gamma,\alpha\vdash A \qquad\alpha\notin\Gamma}{\Gamma\vdash\forall\alpha.A} && \frac{\Gamma\vdash e_1 \qquad\Gamma\vdash e_2 \qquad\Gamma\vdash A}{\Gamma\vdash e_1 <: e_2 \Rightarrow A} \end{aligned}

Figure 11. Well-formedness for ECs and meta types of λRefα\lambda_{\mathrm{Ref}}^\alpha

Subtyping rules
Θ⊢ι<:ι  (<:base)Θ⊢A2<:A1Θ⊢B1<:B2Θ⊢A1→B1<:A2→B2  (<:fun)Θ⊢A<:BΘ⊢B<:AΘ⊢Ref⁡A<:Ref⁡B  (<:ref)Θ⊢e1<:e2Θ⊢“T”e1<:“T”e2  (<:code)Θ⊢A<:BΘ⊢∀α.A<:∀α.B  (<:all)Θ,e1<:e2⊢A<:BΘ⊢e1<:e2⇒A<:e1<:e2⇒B  (<:sub)\begin{aligned} &\frac{}{ \Theta\vdash\iota<: \iota}\;(<:\mathrm{base}) && \frac{\Theta\vdash A_2 <: A_1 \qquad\Theta\vdash B_1 <: B_2}{\Theta\vdash A_1 \to B_1 <: A_2 \to B_2}\;(<:\mathrm{fun}) \\[1ex] &\frac{\Theta\vdash A <: B \qquad\Theta\vdash B <: A}{\Theta\vdash\operatorname{Ref} A <: \operatorname{Ref} B}\;(<:\mathrm{ref}) && \frac{\Theta\vdash e_1 <: e_2}{\Theta\vdash\text{``T''}e_1 <: \text{``T''}e_2}\;(<:\mathrm{code}) \\[1ex] &\frac{\Theta\vdash A <: B}{\Theta\vdash\forall\alpha.A <: \forall\alpha.B}\;(<:\mathrm{all}) && \frac{\Theta,e_1 <: e_2 \vdash A <: B}{\Theta\vdash e_1 <: e_2 \Rightarrow A <: e_1 <: e_2 \Rightarrow B}\;(<:\mathrm{sub}) \end{aligned}

Figure 12. Subtyping rules of λRefα\lambda_{\mathrm{Ref}}^\alpha

Typing rules of lambda Ref
k:ιΓ;e⊢k:ι  (⊢code-const)(x:T)α∈ΓΓ;α⊢x:T  (⊢code-var)\frac{k:\iota}{\Gamma;e\vdash k:\iota}\;(\vdash\text{code-const}) \qquad \frac{(x:T)^\alpha\in\Gamma}{\Gamma;\alpha\vdash x:T}\;(\vdash\text{code-var})
Γ,α,e<:α,(x:S)α⊢N∘:Tα∉ΓΓ;e⊢λ(x:S)α.N∘:S→T  (⊢code-abs)\frac{\Gamma,\alpha,e<:\alpha,(x:S)^\alpha\vdash N^\circ:T\qquad\alpha\notin\Gamma}{\Gamma;e\vdash\lambda(x:S)^\alpha.N^\circ:S\to T}\;(\vdash\text{code-abs})
Γ;e⊢L∘:S→TΓ;e⊢M∘:SΓ;e⊢L∘M∘:T  (⊢code-app)\frac{\Gamma;e\vdash L^\circ:S\to T\qquad\Gamma;e\vdash M^\circ:S}{\Gamma;e\vdash L^\circ M^\circ:T}\;(\vdash\text{code-app})
Γ⊢M:“T”eΓ;e⊢∼M:T  (⊢splice)Γ;e1⊢M∘:TSub⁡(Γ)⊢e1<:e2Γ;e2⊢M∘:T  (⊢code-subenv)\frac{\Gamma\vdash M:\text{``T''}_e}{\Gamma;e\vdash\sim M:T}\;(\vdash\text{splice}) \qquad \frac{\Gamma;e_1\vdash M^\circ:T\qquad\operatorname{Sub}(\Gamma)\vdash e_1<:e_2}{\Gamma;e_2\vdash M^\circ:T}\;(\vdash\text{code-subenv})
k:ιΓ⊢k:ι  (⊢meta-const)x:A∈ΓΓ⊢x:A  (⊢meta-var)\frac{k:\iota}{\Gamma\vdash k:\iota}\;(\vdash\text{meta-const}) \qquad \frac{x:A\in\Gamma}{\Gamma\vdash x:A}\;(\vdash\text{meta-var})
Γ⊢AΓ,x:A⊢M:BΓ⊢λx:A.M:A→B  (⊢meta-abs)\frac{\Gamma\vdash A\qquad\Gamma,x:A\vdash M:B}{\Gamma\vdash\lambda x:A.M:A\to B}\;(\vdash\text{meta-abs})
Γ⊢L:A→BΓ⊢M:AΓ⊢LM:B  (⊢meta-app)Γ,α⊢M:Aα∉ΓΓ⊢Λα.M:∀α.A  (⊢ec-abs)\frac{\Gamma\vdash L:A\to B\qquad\Gamma\vdash M:A}{\Gamma\vdash LM:B}\;(\vdash\text{meta-app}) \qquad \frac{\Gamma,\alpha\vdash M:A\qquad\alpha\notin\Gamma}{\Gamma\vdash\Lambda\alpha.M:\forall\alpha.A}\;(\vdash\text{ec-abs})
Γ⊢M:∀α.AΓ⊢eΓ⊢M[e]:A[α:=e]  (⊢ec-app)\frac{\Gamma\vdash M:\forall\alpha.A\qquad\Gamma\vdash e}{\Gamma\vdash M[e]:A[\alpha:=e]}\;(\vdash\text{ec-app})
Γ⊢e1Γ⊢e2Γ,e1<:e2⊢M:AΓ⊢e1<:e2⇒M:e1<:e2⇒A  (⊢sub-intro)\frac{\Gamma\vdash e_1\qquad\Gamma\vdash e_2\qquad\Gamma,e_1<:e_2\vdash M:A}{\Gamma\vdash e_1<:e_2\Rightarrow M:e_1<:e_2\Rightarrow A}\;(\vdash\text{sub-intro})
Γ⊢M:e1<:e2⇒ASub⁡(Γ)⊢e1<:e2Γ⊢M∙:A  (⊢sub-elim)\frac{\Gamma\vdash M:e_1<:e_2\Rightarrow A\qquad\operatorname{Sub}(\Gamma)\vdash e_1<:e_2}{\Gamma\vdash M^\bullet:A}\;(\vdash\text{sub-elim})
Γ⊢eΓ;e⊢M∘:TΓ⊢“M∘”e:“T”e  (⊢quote)\frac{\Gamma\vdash e\qquad\Gamma;e\vdash M^\circ:T}{\Gamma\vdash\text{``}M^\circ\text{''}_e:\text{``T''}_e}\;(\vdash\text{quote})
Γ⊢M:AΓ⊢ref⁡M:Ref⁡A  (⊢ref)Γ⊢L:Ref⁡AΓ⊢M:AΓ⊢L:=M:Unit⁡  (⊢assign)\frac{\Gamma\vdash M:A}{\Gamma\vdash\operatorname{ref}M:\operatorname{Ref}A}\;(\vdash\text{ref}) \qquad \frac{\Gamma\vdash L:\operatorname{Ref}A\qquad\Gamma\vdash M:A}{\Gamma\vdash L:=M:\operatorname{Unit}}\;(\vdash\text{assign})
Γ⊢M:Ref⁡AΓ⊢!M:A  (⊢deref)Γ⊢M:AΓ⊢BSub⁡(Γ)⊢A<:BΓ⊢M:B  (⊢meta-sub)\frac{\Gamma\vdash M:\operatorname{Ref}A}{\Gamma\vdash!M:A}\;(\vdash\text{deref}) \qquad \frac{\Gamma\vdash M:A\qquad\Gamma\vdash B\qquad\operatorname{Sub}(\Gamma)\vdash A<:B}{\Gamma\vdash M:B}\;(\vdash\text{meta-sub})

Figure 13. Typing rules of λRef⁡α\lambda_{\operatorname{Ref}}^\alpha.

Subtyping between classifiers. The subtyping judgment between classifiers, Θ⊢e1<:e2\Theta\vdash e_1 <: e_2, is defined in Figure 3. It derives subtyping from a subtype context Θ\Theta, which Sub⁡(Γ)\operatorname{Sub}(\Gamma) extracts from the subtype constraints in Γ\Gamma. The constraints in Θ\Theta form the edges of a graph, with an edge from ε\varepsilon to every classifier. The subtyping e1<:e2e_1 <: e_2 holds when the graph has a path from e1e_1 to e2e_2.

Typing code terms. The typing rule for code variables (⊢\vdashcode-var) enforces that a variable may only occur where the nearest enclosing scope has the same classifier. The rule for code λ\lambda-abstractions (⊢\vdashcode-abs) binds both a code variable xx and the classifier α\alpha. It also links α\alpha to the enclosing scope by the constraint e<:αe <: \alpha and types the body of the λ\lambda under the extended context (highlighted). A splice (⊢\vdashsplice) types its meta term, which must produce code of the enclosing classifier. The subsumption rule between classifiers (⊢\vdashcode-subenv) lets code typed under e1e_1 be used in the scope represented by e2e_2 when e1<:e2e_1 <: e_2 holds (highlighted).

Typing meta terms. A quotation (⊢quote\vdash_{\mathit{quote}}) types its code term with the annotated classifier ee as the enclosing scope. Classifier abstraction and application follow System F with classifiers in place of types, and their rules are in the Appendix. Subtype constraint introduction (⊢sub-intro\vdash_{\mathit{sub\text{-}intro}}) extends Γ\Gamma with the assumption e1<:e2e_1 <: e_2 (highlighted), and subtype constraint elimination (⊢sub-elim\vdash_{\mathit{sub\text{-}elim}}) discharges the assumption by checking that e1<:e2e_1 <: e_2 holds (highlighted).

Operational Semantics of λRefα\lambda_{\mathrm{Ref}}^{\alpha}

Figure 4 presents selected reduction rules for λRefα\lambda_{\mathrm{Ref}}^{\alpha}. Each configuration consists of a runtime context (Γ\Gamma), a term, and a heap (μ\mu). The heap maps addresses to values. Code and meta reduction rules are mutually recursive through quotation and splice. We first define the runtime language, which extends λRefα\lambda_{\mathrm{Ref}}^{\alpha} with terms that arise at runtime. We then describe the reduction semantics. The full version of the reduction semantics is in Figure 15 of the Appendix.

Runtime language of λRefα\lambda_{\mathrm{Ref}}^{\alpha}. The runtime language extends λRefα\lambda_{\mathrm{Ref}}^{\alpha} with two terms that arise only at runtime, addresses and code abstraction values (λˉ\bar{\lambda}). Their syntax and typing rules are defined at the top of Figure 4. A code abstraction value is a code abstraction whose classifier and parameter have been allocated in the global runtime context Γ\Gamma. Its typing rule (⊢\vdash code-abs-val) checks the allocated entries in Γ\Gamma (highlighted). The global allocation for code variables allows open code to be stored in the heap while keeping the heap well typed. An address is typed by the heap typing context Σ\Sigma (⊢\vdash addr), which the typing judgments of the runtime language carry in addition to Γ\Gamma.

Runtime language and selected reduction rules of $\lambda_{\mathrm{Ref}}^{\alpha}$

Figure 4. Runtime language and selected reduction rules of λRefα\lambda_{\mathrm{Ref}}^{\alpha} (excerpt of Figures 14 and 15 of the Appendix).

Reducing code terms. The code reduction e⊢⟨Γ;M∘;μ⟩⟶∘⟨Γ′;N∘;μ′⟩e \vdash\langle\Gamma; M^{\circ}; \mu\rangle\mathrel{\longrightarrow^{\circ}} \langle\Gamma'; N^{\circ}; \mu'\rangle reduces a code term under the enclosing classifier ee. The reduction (1) eliminates splices and (2) allocates variables and classifiers of code λ\lambda-abstractions globally. A splice first reduces its sub-term by the meta reduction (ξ\xi-splice). The splice then cancels against the quotation that the sub-term produces (β\beta-splice). The code-$\lambda$ rule performs the global allocation. It first allocates a fresh classifier β\beta for the scope and a fresh code variable yy. It then extends the runtime context Γ\Gamma with β\beta, the edge e<:βe {<:} \beta to the enclosing scope, and the code variable entry (y:T)β(y:T)^{\beta} (highlighted). Finally, it turns the code λ\lambda-abstraction into a code abstraction value, renaming variables to globally allocated ones. A code abstraction value then reduces its body under its classifier annotation (ξ\xi-code-λˉ\bar{\lambda}).

Reducing meta terms. The meta reduction takes the form ⟨Γ;M;μ⟩⟶m⟨Γ′;N;μ′⟩\langle\Gamma; M; \mu\rangle\mathrel{\longrightarrow^{m}} \langle\Gamma'; N; \mu'\rangle. The reduction follows that of a call-by-value λ\lambda-calculus with references, except when the term contains a quotation or discharges a subtype constraint. A quotation reduces the code inside by turning to code reduction (ξ\xi-quote). A constraint elimination reduces by simply discarding the subtype constraint, because the subtyping is already checked statically (β\beta-sub).

Target Language: λRefCtx\lambda_{\mathrm{Ref}}^{\mathrm{Ctx}} with Contextual Modal Types

In this section, we define λRefCtx\lambda_{\mathrm{Ref}}^{\mathrm{Ctx}}, the target language of the compilation. λRefCtx\lambda_{\mathrm{Ref}}^{\mathrm{Ctx}} is a two-level calculus based on CMTT [20], so a code type records the context of the variables that the code may mention, and a splice applies an explicit substitution from one context to another. To these, λRefCtx\lambda_{\mathrm{Ref}}^{\mathrm{Ctx}} adds context polymorphism from λV\lambda_{\mathrm{V}} [19], and general mutable references from Contextual MetaML [29]. To support compilation, λRefCtx\lambda_{\mathrm{Ref}}^{\mathrm{Ctx}} also includes two additional language features: first-class substitutions and reference proxies. We define the syntax and types in § 4.1, the type system in § 0.4.2, and the operational semantics in § 4.3.

context variablesα,β,γcode typing contextsΓ∘,Δ∘::=∅∣α∣x:T∣Γ∘,Δ∘meta typesA,B::=ι∣A→B∣Ref⁡A∣“Γ∘⊢T”∣∀α.A∣Γ∘⇛Δ∘meta typing contextsΓ::=∅∣Γ,α∣Γ,x:Acode termsL∘,M∘,N∘::=k∣x∣λ(x:T).N∘∣L∘ M∘∣∼M Nmeta termsL,M,N::=k∣x∣a∣λ(x:A).N∣L M∣Λα.N∣M[Γ∘]∣“Γ∘⊢M∘”∣id⁡Γ∘∣(M,x=N∘)∣weaken⁡Δ∘(M)∣M∘N∣ref⁡M∣L:=M∣!M∣proxy⁡L M N\begin{aligned} \text{context variables}\quad&\alpha,\beta,\gamma\\ \text{code typing contexts}\quad&\Gamma^{\circ},\Delta^{\circ} ::= \varnothing\mid\alpha\mid x:T \mid\Gamma^{\circ},\Delta^{\circ}\\ \text{meta types}\quad&A,B ::= \iota\mid A \to B \mid\operatorname{Ref} A \mid\text{``}\Gamma^{\circ} \vdash T\text{''}\\ &\qquad\mid\forall\alpha.A \mid\Gamma^{\circ} \mathbin{\Rrightarrow} \Delta^{\circ}\\ \text{meta typing contexts}\quad&\Gamma::= \varnothing\mid\Gamma,\alpha\mid\Gamma,x:A\\ \text{code terms}\quad&L^{\circ},M^{\circ},N^{\circ} ::= k \mid x \mid\lambda(x:T).N^{\circ} \mid L^{\circ}\ M^{\circ} \mid\mathord{\sim}M\ N\\ \text{meta terms}\quad&L,M,N ::= k \mid x \mid a \mid\lambda(x:A).N \mid L\ M\\ &\qquad\mid\Lambda\alpha.N \mid M[\Gamma^{\circ}] \mid\text{``}\Gamma^{\circ}\vdash M^{\circ}\text{''}\\ &\qquad\mid\operatorname{id}_{\Gamma^{\circ}} \mid(M,x=N^{\circ}) \mid\operatorname{weaken}_{\Delta^{\circ}}(M) \mid M\mathbin{\circ}N\\ &\qquad\mid\operatorname{ref}M \mid L:=M \mid{!}M \mid\operatorname{proxy}L\ M\ N \end{aligned}

Fig. 5. Syntax of λRef⁡Ctx⁡\lambda_{\operatorname{Ref}}^{\operatorname{Ctx}}.

Types and Syntax of λRef⁡Ctx⁡\lambda_{\operatorname{Ref}}^{\operatorname{Ctx}}

Figure 5 defines the syntax of λRef⁡Ctx⁡\lambda_{\operatorname{Ref}}^{\operatorname{Ctx}}, where most language constructs resemble those in λRef⁡α\lambda_{\operatorname{Ref}}^{\alpha}. We describe the CMTT-specific features of λRef⁡Ctx⁡\lambda_{\operatorname{Ref}}^{\operatorname{Ctx}} below.

Contexts in types and quotations. Standard in a CMTT-based calculus, a quotation $\Gamma^{\circ}\vdash M^{\circ}$'' and a code type Γ∘⊢T\Gamma^{\circ}\vdash T'' carry a code context Γ∘\Gamma^{\circ}. We represent a code typing context as a binary tree whose leaves are the empty context, a single mapping x:Tx:T, and context variables α\alpha. The inner node Γ∘,Δ∘\Gamma^{\circ},\Delta^{\circ} concatenates two contexts. We use a binary tree because this representation is closed under instantiating a context variable. Replacing the leaf α\alpha by a context yields a context, and the rest of the tree is unchanged.

Polymorphism over contexts. We follow λ∀[ ]\lambda_{\forall}[\ ] [19] and introduce context polymorphism to λRef⁡Ctx⁡\lambda_{\operatorname{Ref}}^{\operatorname{Ctx}}. The universal type ∀α.A\forall\alpha.A quantifies over contexts. Context abstraction Λα.N\Lambda\alpha.N binds a context variable, and context application M[Γ∘]M[\Gamma^{\circ}] instantiates it with a context.

First-class substitutions. Standard in CMTT, a splice ∼M N\mathord{\sim}M\ N carries a substitution NN that maps the variables of the spliced code’s context to code in the current context. Unique to λRef⁡Ctx⁡\lambda_{\operatorname{Ref}}^{\operatorname{Ctx}}, substitutions are first-class metalanguage terms to support the compilation of bounded quantification (§ 0.2.3). A substitution term of type Γ∘⇛Δ∘\Gamma^{\circ}\mathbin{\Rrightarrow}\Delta^{\circ} substitutes code in Δ∘\Delta^{\circ} for the variables of Γ∘\Gamma^{\circ}. Its four introduction forms follow the calculus of explicit substitutions [1]. The identity id⁡Γ∘\operatorname{id}_{\Gamma^{\circ}} maps every variable of Γ∘\Gamma^{\circ} to itself, the extension (M,x=N∘)(M,x=N^{\circ}) maps xx to N∘N^{\circ} and the other variables as MM does, the weakening weaken⁡Δ∘(M)\operatorname{weaken}_{\Delta^{\circ}}(M) lets MM target a context extended by Δ∘\Delta^{\circ}, and the composition M∘NM\mathbin{\circ}N applies MM and then NN.

Reference proxies. Following Herman et al. [10], we introduce proxies of mutable references to λRef⁡Ctx⁡\lambda_{\operatorname{Ref}}^{\operatorname{Ctx}} to support the compilation of subtyping between reference types. We use the syntax proxy⁡ L M N\operatorname{proxy}\ L\ M\ N, which wraps a reference LL. The proxy applies the function MM to every value read from LL, and applies NN to every value written to LL.

Type System of λRefCtx\lambda_{\mathrm{Ref}}^{\mathrm{Ctx}}

Figure 6 shows selected typing rules of λRefCtx\lambda_{\mathrm{Ref}}^{\mathrm{Ctx}}, and the complete rules are in Figure 17 of the Appendix. All judgments carry a meta typing context Γ\Gamma, which binds context and meta variables. The term typing judgments also carry a heap typing context Σ\Sigma, whose definition is standard, mapping addresses to types.

Selected typing rules of lambda Ref

Figure 6. Selected typing rules of λRefCtx\lambda_{\mathrm{Ref}}^{\mathrm{Ctx}} (excerpt of Figure 17 of the Appendix).

Typing rules of the contextual modal calculus
x:T∈x:Tx:T∈Γ∘x:T∈(Γ∘,Δ∘)x:T∈Δ∘x:T∈(Γ∘,Δ∘)\frac{x:T\in x:T}{} \qquad \frac{x:T\in\Gamma^\circ}{x:T\in(\Gamma^\circ,\Delta^\circ)} \qquad \frac{x:T\in\Delta^\circ}{x:T\in(\Gamma^\circ,\Delta^\circ)}
k:ιΓ;Σ;Γ∘⊢k:ι(⊢code-const)x:T∈Γ∘Γ;Σ;Γ∘⊢x:T(⊢code-var)\frac{k:\iota}{\Gamma;\Sigma;\Gamma^\circ\vdash k:\iota} \quad(\vdash\text{code-const}) \qquad \frac{x:T\in\Gamma^\circ}{\Gamma;\Sigma;\Gamma^\circ\vdash x:T} \quad(\vdash\text{code-var})
Γ;Σ;(Γ∘,x:S)⊢N∘:TΓ;Σ;Γ∘⊢λ(x:S).N∘:S→T(⊢code-abs)Γ;Σ;Γ∘⊢L∘:S→TΓ;Σ;Γ∘⊢M∘:SΓ;Σ;Γ∘⊢L∘M∘:T(⊢code-app)\frac{\Gamma;\Sigma;(\Gamma^\circ,x:S)\vdash N^\circ:T} {\Gamma;\Sigma;\Gamma^\circ\vdash\lambda(x:S).N^\circ:S\to T} \quad(\vdash\text{code-abs}) \qquad \frac{\Gamma;\Sigma;\Gamma^\circ\vdash L^\circ:S\to T \qquad \Gamma;\Sigma;\Gamma^\circ\vdash M^\circ:S} {\Gamma;\Sigma;\Gamma^\circ\vdash L^\circ M^\circ:T} \quad(\vdash\text{code-app})
Γ;Σ⊢M:⌜Γ∘⊢T⌝Γ;Σ⊢N:Γ1∘⇒Γ2∘Γ;Σ;Γ2∘⊢∼M N:T(⊢splice)\frac{\Gamma;\Sigma\vdash M:\ulcorner\Gamma^\circ\vdash T\urcorner \qquad \Gamma;\Sigma\vdash N:\Gamma^\circ_1\Rightarrow\Gamma^\circ_2} {\Gamma;\Sigma;\Gamma^\circ_2\vdash\sim M\,N:T} \quad(\vdash\text{splice})
k:ιΓ;Σ⊢k:ι(⊢const)x:A∈ΓΓ;Σ⊢x:A(⊢var)a:A∈ΣΓ;Σ⊢a:Ref⁡A(⊢addr)\frac{k:\iota}{\Gamma;\Sigma\vdash k:\iota} \quad(\vdash\text{const}) \qquad \frac{x:A\in\Gamma}{\Gamma;\Sigma\vdash x:A} \quad(\vdash\text{var}) \qquad \frac{a:A\in\Sigma}{\Gamma;\Sigma\vdash a:\operatorname{Ref} A} \quad(\vdash\text{addr})
Γ⊢A tyΓ,x:A;Σ⊢N:BΓ;Σ⊢λ(x:A).N:A→B(⊢abs)Γ;Σ⊢L:A→BΓ;Σ⊢M:AΓ;Σ⊢L M:B(⊢app)\frac{\Gamma\vdash A\ \mathrm{ty}\qquad\Gamma,x:A;\Sigma\vdash N:B} {\Gamma;\Sigma\vdash\lambda(x:A).N:A\to B} \quad(\vdash\text{abs}) \qquad \frac{\Gamma;\Sigma\vdash L:A\to B\qquad\Gamma;\Sigma\vdash M:A} {\Gamma;\Sigma\vdash L\,M:B} \quad(\vdash\text{app})
Γ⊢Γ∘ ctxΓ;Σ;Γ∘⊢M∘:TΓ;Σ⊢⌜Γ∘⊢M∘⌝:⌜Γ∘⊢T⌝(⊢quote)\frac{\Gamma\vdash\Gamma^\circ\ \mathrm{ctx}\qquad \Gamma;\Sigma;\Gamma^\circ\vdash M^\circ:T} {\Gamma;\Sigma\vdash\ulcorner\Gamma^\circ\vdash M^\circ\urcorner:\ulcorner\Gamma^\circ\vdash T\urcorner} \quad(\vdash\text{quote})
Γ,α;Σ⊢N:Aα∉ΓΓ;Σ⊢Λα.N:∀α.A(⊢ctx-abs)Γ;Σ⊢M:∀α.AΓ⊢Γ∘ ctxΓ;Σ⊢M[Γ∘]:A[α:=Γ∘](⊢ctx-app)\frac{\Gamma,\alpha;\Sigma\vdash N:A\qquad\alpha\notin\Gamma} {\Gamma;\Sigma\vdash\Lambda\alpha.N:\forall\alpha.A} \quad(\vdash\text{ctx-abs}) \qquad \frac{\Gamma;\Sigma\vdash M:\forall\alpha.A\qquad\Gamma\vdash\Gamma^\circ\ \mathrm{ctx}} {\Gamma;\Sigma\vdash M[\Gamma^\circ]:A[\alpha:=\Gamma^\circ]} \quad(\vdash\text{ctx-app})
Γ;Σ⊢M:AΓ;Σ⊢ref⁡M:Ref⁡A(⊢ref)Γ;Σ⊢M:Ref⁡AΓ;Σ⊢!M:A(⊢deref)\frac{\Gamma;\Sigma\vdash M:A} {\Gamma;\Sigma\vdash\operatorname{ref}M:\operatorname{Ref} A} \quad(\vdash\text{ref}) \qquad \frac{\Gamma;\Sigma\vdash M:\operatorname{Ref} A} {\Gamma;\Sigma\vdash!M:A} \quad(\vdash\text{deref})
Γ;Σ⊢L:Ref⁡AΓ;Σ⊢M:AΓ;Σ⊢L:=M:Unit⁡(⊢assign)Γ;Σ⊢L:Ref⁡AΓ;Σ⊢M:A→BΓ;Σ⊢N:B→AΓ;Σ⊢proxy⁡ L M N:Ref⁡B(⊢proxy)\frac{\Gamma;\Sigma\vdash L:\operatorname{Ref} A\qquad\Gamma;\Sigma\vdash M:A} {\Gamma;\Sigma\vdash L:=M:\operatorname{Unit}} \quad(\vdash\text{assign}) \qquad \frac{\Gamma;\Sigma\vdash L:\operatorname{Ref} A\qquad \Gamma;\Sigma\vdash M:A\to B\qquad \Gamma;\Sigma\vdash N:B\to A} {\Gamma;\Sigma\vdash\operatorname{proxy}\ L\ M\ N:\operatorname{Ref} B} \quad(\vdash\text{proxy})
Γ⊢Γ∘ ctxΓ;Σ⊢id⁡Γ∘:Γ∘⇒Γ∘(⊢id)Γ⊢Γ∘ ctxΓ;Σ⊢weaken⁡Γ∘(id⁡∅):∅⇒Γ∘(⊢weaken-empty)\frac{\Gamma\vdash\Gamma^\circ\ \mathrm{ctx}} {\Gamma;\Sigma\vdash\operatorname{id}_{\Gamma^\circ}:\Gamma^\circ\Rightarrow\Gamma^\circ} \quad(\vdash\text{id}) \qquad \frac{\Gamma\vdash\Gamma^\circ\ \mathrm{ctx}} {\Gamma;\Sigma\vdash\operatorname{weaken}_{\Gamma^\circ}(\operatorname{id}_{\varnothing}):\varnothing\Rightarrow\Gamma^\circ} \quad(\vdash\text{weaken-empty})
Γ;Σ⊢M:Γ1∘⇒Γ2∘Γ⊢Γ3∘ ctxΓ;Σ⊢weaken⁡Γ3∘(M):Γ1∘⇒(Γ2∘,Γ3∘)(⊢weaken)\frac{\Gamma;\Sigma\vdash M:\Gamma^\circ_1\Rightarrow\Gamma^\circ_2\qquad \Gamma\vdash\Gamma^\circ_3\ \mathrm{ctx}} {\Gamma;\Sigma\vdash\operatorname{weaken}_{\Gamma^\circ_3}(M): \Gamma^\circ_1\Rightarrow(\Gamma^\circ_2,\Gamma^\circ_3)} \quad(\vdash\text{weaken})
Γ;Σ⊢M:Γ∘⇒Δ∘Γ;Σ;Δ∘⊢N∘:TΓ;Σ⊢(M,x=N∘):(Γ∘,x:T)⇒Δ∘(⊢ext)\frac{\Gamma;\Sigma\vdash M:\Gamma^\circ\Rightarrow\Delta^\circ\qquad \Gamma;\Sigma;\Delta^\circ\vdash N^\circ:T} {\Gamma;\Sigma\vdash(M,x=N^\circ):(\Gamma^\circ,x:T)\Rightarrow\Delta^\circ} \quad(\vdash\text{ext})
Γ;Σ⊢M:Γ1∘⇒Γ2∘Γ;Σ⊢N:Γ2∘⇒Γ3∘Γ;Σ⊢M∘N:Γ1∘⇒Γ3∘(⊢compose)\frac{\Gamma;\Sigma\vdash M:\Gamma^\circ_1\Rightarrow\Gamma^\circ_2\qquad \Gamma;\Sigma\vdash N:\Gamma^\circ_2\Rightarrow\Gamma^\circ_3} {\Gamma;\Sigma\vdash M\circ N:\Gamma^\circ_1\Rightarrow\Gamma^\circ_3} \quad(\vdash\text{compose})

Figure 17. Typing rules of λRefCtx\lambda_{\mathrm{Ref}}^{\mathrm{Ctx}}.

Well-formed types. A meta type is well formed, written Γ⊢A ty\Gamma\vdash A\ \mathrm{ty}, when every type and code context inside it is well formed. A code context is well formed, written Γ⊢Γ∘ ctx\Gamma\vdash\Gamma^{\circ}\ \mathrm{ctx}, when all its context variables are well scoped in Γ\Gamma. Their definitions are in Figure 16 of the Appendix.

Well-formedness rules for code contexts and meta types in $\lambda_{\mathrm{Ref}}^{\mathrm{Ctx}}$

Figure 16. Well-formedness of code contexts and meta types in λRefCtx\lambda_{\mathrm{Ref}}^{\mathrm{Ctx}}.

Typing code terms. The typing judgment for code terms, Γ;Σ;Γ∘⊢M∘:T\Gamma;\Sigma;\Gamma^{\circ} \vdash M^{\circ}:T, carries the code context Γ∘\Gamma^{\circ}. A code variable is typed by looking the variable up in Γ∘\Gamma^{\circ} ($\vdash$code-var). The lookup relation x:T∈Γ∘x:T \in\Gamma^{\circ} finds the leaf (x:T)(x:T) in the tree Γ∘\Gamma^{\circ}. A code abstraction extends Γ∘\Gamma^{\circ} with its parameter for the abstraction’s body ($\vdash$code-abs). The rule for splice ($\vdash$splice) is the one rule that connects two code contexts. Its first argument MM produces code in some context Γ1∘\Gamma^{\circ}_{1}, and its second argument NN is a substitution from Γ1∘\Gamma^{\circ}_{1} to the current context Γ2∘\Gamma^{\circ}_{2}.

Typing meta terms. The typing judgment for meta terms takes the form Γ;Σ⊢M:A\Gamma;\Sigma\vdash M:A. A quotation $\Gamma^{\circ} \vdash M^{\circ}$'' has type Γ∘⊢T\Gamma^{\circ} \vdash T'' when M∘M^{\circ} has type TT in Γ∘\Gamma^{\circ} ($\vdash$quote). Context abstraction and application follow System F with contexts in place of types, and their rules are in the Appendix. Now we discuss the typing of the new features in λRefCtx\lambda_{\mathrm{Ref}}^{\mathrm{Ctx}} that support compilation: reference proxies and first-class substitutions. A proxy of a reference of type Ref⁡A\operatorname{Ref} A, with an output function of A→BA \to B and an input function of B→AB \to A, has type Ref⁡B\operatorname{Ref} B (⊢proxy\vdash proxy, highlighted in blue). The substitution rules, highlighted in green, type a substitution by its domain and codomain. The identity on Γ∘\Gamma^{\circ} goes from Γ∘\Gamma^{\circ} to itself (⊢id\vdash id). Weakening the identity on empty gives the empty substitution into any context (⊢weaken-empty\vdash weaken\text{-}empty). A weakening weaken⁡Γ3∘(M)\operatorname{weaken}_{\Gamma^{\circ}_{3}}(M) keeps the domain of MM and joins its codomain Γ2∘\Gamma^{\circ}_{2} with Γ3∘\Gamma^{\circ}_{3} (⊢weaken\vdash weaken). An extension (M,x=N∘)(M,x=N^{\circ}) adds x:Tx:T to the domain of MM, where TT is the type of N∘N^{\circ} (⊢ext\vdash ext). A composition M∘NM \mathbin{\circ} N chains two substitutions whose contexts match in the middle (⊢compose\vdash compose).

Operational Semantics of LambdaCtxRef

Figure 7 shows selected reduction rules of λRefCtx\lambda_{\mathrm{Ref}}^{\mathrm{Ctx}}, and the complete rules are in Figure 18 of the Appendix. The semantics consists of two mutually recursive reduction relations, one for code and the other for meta.

Selected reduction rules of $\lambda_{\mathrm{Ref}}^{\mathrm{Ctx}}$

Figure 7. Selected reduction rules of λRefCtx\lambda_{\mathrm{Ref}}^{\mathrm{Ctx}} (excerpt of Figure 18 of the Appendix).

Operational semantics of lambda Ref

Figure 18. Operational semantics of λRefCtx\lambda^{\mathrm{Ctx}}_{\mathrm{Ref}}.

Values. A code value is a term in STLC with no remaining splice. The meta values are constants, addresses, abstractions, quotations of code values, proxies of values, and substitution values. A substitution value is built from the four substitution introduction forms, each with values in its sub-terms. Its meaning is given by the interpretation ⟦V⟧\llbracket V \rrbracket, defined in Figure 18 of the Appendix. The interpretation of VV is a substitution that substitutes code for each variable in the domain of VV. Applying ⟦V⟧\llbracket V \rrbracket to a code value performs the substitution.

Reducing code terms. The code reduction takes the form M∘∣μ→∘N∘∣μ′M^{\circ} \mid\mu\mathrel{\to^{\circ}} N^{\circ} \mid\mu'. Code reduces only to eliminate splices. A splice first reduces its meta term to a quotation (ξ1\xi_{1}-splice) and its substitution to a substitution value (ξ2\xi_{2}-splice).

The rule β\beta-splice is the heart of the semantics, highlighted in green. It reduces the splice of a quotation ``Γ∘⊢V∘\Gamma^{\circ} \vdash V^{\circ}'' under a substitution value VV to [[V]](V∘)[[V]](V^{\circ}), which is the quoted code with the substitution applied.

Reducing meta terms. The meta reduction takes the form M∣μ⟶mN∣μ′M \mid\mu\longrightarrow^{m} N \mid\mu'. Meta terms reduce as in a call-by-value λ\lambda-calculus with references, except in three cases. The code attached to a substitution extension reduces when it contains splices (ξ\xi-ext). Context application substitutes the context Γ∘\Gamma^{\circ} for the context variable α\alpha in the body (β\beta-ctx). A proxy forwards dereference and assignment to the reference it wraps, applying its output function to every value read (proxy-deref) and its input function to every value written (proxy-assign).

Compilation From λRefα\lambda^{\alpha}_{\mathrm{Ref}} to λRefCtx\lambda^{\mathrm{Ctx}}_{\mathrm{Ref}}

In this section, we formally define the compilation from λRefα\lambda^{\alpha}_{\mathrm{Ref}} to λRefCtx\lambda^{\mathrm{Ctx}}_{\mathrm{Ref}}. The compilation has three parts. First, classifiers compile to code contexts, and types compile accordingly (§ 5.1). Second, subtyping between classifiers compiles to substitutions, and subtyping between types compiles to coercion functions (§ 5.2). Third, terms compile according to their typing derivations, with classifier polymorphism becoming context polymorphism and subtype constraints becoming abstraction over and application to substitutions (§ 5.3).

Compiling Classifiers and Types

Figure 8 shows selected compilation rules for classifiers and types. The complete compilation rules are in Figure 19 of the Appendix.

Compilation contexts. A compilation context Ξ\Xi, defined at the top of Figure 8, is a typing context of λRefα\lambda^{\alpha}_{\mathrm{Ref}} extended by the two highlighted entries. First, a compilation context distinguishes the two ways a classifier enters scope, because they compile differently. A classifier bound by a code abstraction is recorded as α\alpha and compiles to a concrete context, whereas a classifier bound by a classifier abstraction is recorded as αpoly\alpha_{\mathrm{poly}} and compiles to a context variable. Second, it distinguishes the two kinds of subtype constraints. A constraint e1<:e2e_1 <: e_2 comes from a code abstraction, whereas a named constraint s:e1<:e2s : e_1 <: e_2 comes from a constraint introduction and binds the variable ss as evidence for the constraint.

Compilation contexts and selected compilation rules of classifiers and types

Figure 8. Compilation contexts, and selected compilation rules of classifiers and types (excerpt of Figure 19 of the Appendix).

Compiling classifiers to contexts. The judgment Ξ⊢e⇝Γ∘\Xi\vdash e \rightsquigarrow\Gamma^{\circ} computes the context that corresponds to a classifier. The empty classifier compiles to the empty context (⇝empty\rightsquigarrow\mathrm{empty}), and a polymorphic classifier compiles to a context variable (\rightsquigarrow\mathrm{poly\mathchar`-ec}). The interesting case is a classifier α\alpha introduced by a code abstraction (⇝ec\rightsquigarrow\mathrm{ec}). Its context is calculated by following the subtyping in Ξ\Xi. We first include the code variable entry (x:T)α(x:T)^{\alpha} in the result context. We then follow the edge e<:αe <: \alpha, which leads to the enclosing classifier ee. The context of ee is computed in turn. Following the edges up to ε\varepsilon or to a polymorphic classifier, we collect exactly the variables that the scope of α\alpha may mention.

Compiling types. The judgment Ξ⊢A⇝A′\Xi\vdash A \rightsquigarrow A' compiles types, and its rules mirror the well-formedness rules of types in λRef\lambda_{\mathrm{Ref}}. A code type ``Te′′T''_e compiles to Γ∘⊢T′′\Gamma^{\circ} \vdash T'', where Γ∘\Gamma^{\circ} is the context of ee (⇝code\rightsquigarrow\mathrm{code}). A subtype-constrained type e1<:e2⇒Ae_1 <: e_2 \Rightarrow A compiles to the function type B→A′B \to A' (⇝constraint\rightsquigarrow\mathrm{constraint}). Its argument type BB is the type of the evidence for the constraint, computed by the judgment Ξ⊢e1<:e2⇝B\Xi\vdash e_1 <: e_2 \rightsquigarrow B that we define in the next subsection. Its body AA is compiled to A′A' under Ξ\Xi extended with the evidence variable ss for the constraint. The remaining types compile recursively.

Compiling Subtyping

Figure 9 presents selected compilation rules of subtyping. The complete rules are in Figure 20 of the Appendix. The compilation follows the coercion semantics of subtyping [2, 22]. Subtyping between classifiers compiles to a first-class substitution, and subtyping between types compiles to a coercion function.

Compilation rules for subtyping between classifiers and between types
Ξ⊢e1⇝Γ1∘Ξ⊢e2⇝Γ2∘Ξ⊢e1<:e2⇝Γ1∘⇒Γ2∘(⇝sub-ec)Ξ⊢e1<:e2⇝AΞ⊢e⇝Γ∘Ξ⊢ϵ<:e⇝weaken⁡Γ∘(id⁡∅)(⇝sub-empty)Ξ⊢e⇝Γ∘Ξ⊢e<:e⇝id⁡Γ∘(⇝sub-refl)e<:α∈Ξ(x:T)α∈ΞΞ⊢e⇝Γ∘Ξ⊢e<:α⇝weaken⁡x:T(id⁡Γ∘)(⇝sub-ax)(s:e1<:e2)∈ΞΞ⊢e1<:e2⇝s(⇝sub-var)Ξ⊢e1<:e3⇝MΞ⊢e3<:e2⇝NΞ⊢e1<:e2⇝M∘N(⇝sub-trans)Ξ⊢A<:B⇝MΞ⊢e1⇝Γ∘Ξ⊢e2⇝Δ∘Ξ⊢e1<:e2⇝MΞ⊢‘‘T′′e1<:‘‘T′′e2⇝λ(c:‘‘Γ∘⊢T′′).‘‘Δ∘⊢∼cM′′(⇝sub-code)Ξ⊢A⇝A′Ξ⊢A<:B⇝MΞ⊢B<:A⇝NΞ⊢Ref⁡A<:Ref⁡B⇝λ(r:Ref⁡A′).proxy⁡ r M N(⇝sub-ref)Ξ⊢e1<:e2⇝CΞ,(s:e1<:e2)⊢A⇝A′Ξ,(s:e1<:e2)⊢A<:B⇝Ms∉ΞΞ⊢(e1<:e2⇒A)<:(e1<:e2⇒B)⇝λ(f:C→A′).λ(s:C).M(f s)(⇝sub-constraint)\begin{aligned} &\frac{\Xi\vdash e_1 \leadsto\Gamma_1^\circ\qquad\Xi\vdash e_2 \leadsto\Gamma_2^\circ}{\Xi\vdash e_1 <: e_2 \leadsto\Gamma_1^\circ\Rightarrow\Gamma_2^\circ} \quad(\leadsto\text{sub-ec}) \\ &\Xi\vdash e_1 <: e_2 \leadsto A \\ &\frac{\Xi\vdash e \leadsto\Gamma^\circ}{\Xi\vdash\epsilon<: e \leadsto\operatorname{weaken}_{\Gamma^\circ}(\operatorname{id}_{\varnothing})} \quad(\leadsto\text{sub-empty}) \\ &\frac{\Xi\vdash e \leadsto\Gamma^\circ}{\Xi\vdash e <: e \leadsto\operatorname{id}_{\Gamma^\circ}} \quad(\leadsto\text{sub-refl}) \\ &\frac{e <: \alpha\in\Xi\qquad(x:T)^\alpha\in\Xi\qquad\Xi\vdash e \leadsto\Gamma^\circ}{\Xi\vdash e <: \alpha\leadsto\operatorname{weaken}_{x:T}(\operatorname{id}_{\Gamma^\circ})} \quad(\leadsto\text{sub-ax}) \\ &\frac{(s:e_1 <: e_2) \in\Xi}{\Xi\vdash e_1 <: e_2 \leadsto s} \quad(\leadsto\text{sub-var}) \\ &\frac{\Xi\vdash e_1 <: e_3 \leadsto M \qquad\Xi\vdash e_3 <: e_2 \leadsto N}{\Xi\vdash e_1 <: e_2 \leadsto M \mathbin{\circ} N} \quad(\leadsto\text{sub-trans}) \\ &\Xi\vdash A <: B \leadsto M \\ &\frac{\Xi\vdash e_1 \leadsto\Gamma^\circ\qquad\Xi\vdash e_2 \leadsto\Delta^\circ\qquad\Xi\vdash e_1 <: e_2 \leadsto M}{\Xi\vdash``T''e_1 <: ``T''e_2 \leadsto\lambda(c:``\Gamma^\circ\vdash T'').``\Delta^\circ\vdash\sim_c M''} \quad(\leadsto\text{sub-code}) \\ &\frac{\Xi\vdash A \leadsto A' \qquad\Xi\vdash A <: B \leadsto M \qquad\Xi\vdash B <: A \leadsto N}{\Xi\vdash\operatorname{Ref} A <: \operatorname{Ref} B \leadsto\lambda(r:\operatorname{Ref} A').\operatorname{proxy}\ r\ M\ N} \quad(\leadsto\text{sub-ref}) \\ &\frac{\Xi\vdash e_1 <: e_2 \leadsto C \qquad\Xi,(s:e_1 <: e_2) \vdash A \leadsto A' \qquad\Xi,(s:e_1 <: e_2) \vdash A <: B \leadsto M \qquad s \notin\Xi}{\Xi\vdash(e_1 <: e_2 \Rightarrow A) <: (e_1 <: e_2 \Rightarrow B) \leadsto\lambda(f:C \to A').\lambda(s:C).M(f\ s)} \quad(\leadsto\text{sub-constraint}) \end{aligned}

Figure 9. Compilation of subtyping between classifiers and between types (excerpt of Figure 20 of the Appendix).

Compilation of subtyping between classifiers and between types

Figure 20. Compilation of subtyping between classifiers and between types.

Compiling classifier subtyping to a substitution. The judgment Ξ⊢e1<:e2⇝M\Xi\vdash e_1 <: e_2 \rightsquigarrow M compiles two classifiers e1e_1 and e2e_2, the former a subtype of the latter, to a substitution MM. The type of MM is given by Ξ⊢e1<:e2⇝A\Xi\vdash e_1 <: e_2 \rightsquigarrow A, which produces a substitution type from the compiled context of e1e_1 to that of e2e_2. The rules of Ξ⊢e1<:e2⇝M\Xi\vdash e_1 <: e_2 \rightsquigarrow M mirror the subtyping rules for classifiers in λRef\lambda_{\mathrm{Ref}}. The axiom ε<:e\varepsilon<: e compiles to the empty substitution into the context of ee (\rightsquigarrow\mathrm{sub\mathchar`-empty}). Reflexivity compiles to the identity (\rightsquigarrow\mathrm{sub\mathchar`-refl}). The two kinds of subtyping entries in Ξ\Xi compile differently. A subtyping entry e<:αe <: \alpha, introduced by a code λ\lambda, compiles to the identity on the context of ee, weakened by the code variable (x:T)(x:T) bound by that code λ\lambda (\rightsquigarrow\mathrm{sub\mathchar`-ax}). A named constraint, introduced by a subtype constraint, compiles to the variable ss for the evidence of that constraint (\rightsquigarrow\mathrm{sub\mathchar`-var}). Transitivity compiles to composition (\rightsquigarrow\mathrm{sub\mathchar`-trans}).

Compiling type subtyping to a coercion. The judgment Ξ⊢A<:B⇝M\Xi\vdash A <: B \rightsquigarrow M compiles two types AA and BB, with AA a subtype of BB, to a coercion function MM. The compilation rules mirror the subtyping rules for types in λRef\lambda_{\mathrm{Ref}}. The coercion converts from the compiled type of AA to that of BB. It applies the substitution that the underlying classifier subtyping compiles to, and the rule for code types is the key (\rightsquigarrow\mathrm{sub\mathchar`-code}). The subtyping $T''_{e_1} <: Te2′′T''_{e_2} compiles to a function that takes code in the context that e1e_1 corresponds to, Γ∘\Gamma^\circ, and moves it to the context that e2e_2 corresponds to, Δ∘\Delta^\circ. The body of the function splices the code under the substitution that e1<:e2e_1 <: e_2 compiles to (highlighted) and re-quotes the result in Δ∘\Delta^\circ. A reference type is special, because a reference can be both read from and written to. Consequently, its coercion wraps the reference in a proxy whose output and input functions are the coercions in the two directions, read and write (⇝sub-ref\leadsto\text{sub-ref}). A subtype-constrained type also deserves a closer look (⇝sub-constraint\leadsto\text{sub-constraint}). The types e1<:e2⇒Ae_1 <: e_2 \Rightarrow A and e1<:e2⇒Be_1 <: e_2 \Rightarrow B compile to the function types C→A′C \to A' and C→B′C \to B', where CC is the type of the evidence for the constraint, and A′,B′A', B' are what A,BA, B compile to, respectively. The coercion is thus a function between function types, and the compilation follows the types. The coercion takes a function ff and evidence ss, applies ff to ss, and coerces the result. The remaining cases follow the standard coercion semantics.

Compiling Terms

Figure 10 shows selected compilation rules for code terms and meta terms. The complete compilation rules are in Figure 21 of the Appendix. The rules mirror the typing rules of λRefα\lambda_{\mathrm{Ref}}^\alpha and do two things. First, wherever a typing rule of λRefα\lambda_{\mathrm{Ref}}^\alpha mentions a classifier, the compilation rule converts it into the context that the classifier corresponds to (the green highlights). Second, wherever a typing rule of λRefα\lambda_{\mathrm{Ref}}^\alpha uses subtyping, the compiled term carries the substitution or coercion that the subtyping compiles to (the blue highlights). The remaining rules compile recursively, except that a code abstraction drops its classifier (⇝code-abs\rightsquigarrow code\text{-}abs) and a splice gains the identity substitution that a splice of λRefCtx\lambda_{\mathrm{Ref}}^{\mathrm{Ctx}} requires (⇝splice\rightsquigarrow splice).

Selected compilation rules of terms

Figure 10. Selected compilation rules of terms (excerpt of Figure 21 of the Appendix).

Compilation rules for code terms and meta terms
Ξ;e⊢M∘:T⇝M∘′k:ιΞ;e⊢k:ι⇝k  (⇝code-const)(x:T)α∈ΞΞ;α⊢x:T⇝x  (⇝code-var)Ξ,α,e<:α,(x:S)α⊢N∘:T⇝N∘′α∉ΞΞ;e⊢λ(x:S).N∘:S→T⇝λ(x:S).N∘′  (⇝code-abs)Ξ;e⊢L∘:S→T⇝L∘′Ξ;e⊢M∘:S⇝M∘′Ξ;e⊢L∘M∘:T⇝L∘′M∘′  (⇝code-app)Ξ⊢M:“T”e⇝M′Ξ⊢e⇝ΓΞ;e⊢∼M:T⇝∼M′id⁡Γ∘  (⇝splice)Ξ;e1⊢M∘:T⇝M∘′Ξ⊢e1⇝Γ∘Ξ⊢e1<:e2⇝MΞ;e2⊢M∘:T⇝∼“Γ∘⊢M∘′”M  (⇝subenv)Ξ⊢M:A⇝M′k:ιΞ⊢k:ι⇝k  (⇝const)x:A∈ΞΞ⊢x:A⇝x  (⇝var)Ξ⊢A⇝A′Ξ,x:A⊢N:B⇝N′Ξ⊢λ(x:A).N:A→B⇝λ(x:A′).N′  (⇝abs)Ξ⊢L:A→B⇝L′Ξ⊢M:A⇝M′Ξ⊢LM:B⇝L′M′  (⇝app)Ξ⊢M:A⇝M′Ξ⊢ref⁡M:Ref⁡A⇝ref⁡M′  (⇝ref)Ξ⊢L:Ref⁡A⇝L′Ξ⊢M:A⇝M′Ξ⊢L:=M:Unit⁡⇝L′:=M′  (⇝assign)Ξ⊢M:Ref⁡A⇝M′Ξ⊢!M:A⇝!M′  (⇝deref)Ξ;e⊢M∘:T⇝M∘′Ξ⊢e⇝Γ∘Ξ⊢“M∘”e:“T”e⇝“Γ∘⊢M∘′”  (⇝quote)Ξ,αpoly⊢N:A⇝N′α∉ΞΞ⊢Λα.N:∀α.A⇝Λα.N′  (⇝ec-abs)Ξ⊢M:∀α.A⇝M′Ξ⊢e⇝Γ∘Ξ⊢M[e]:A[α:=e]⇝M′[Γ∘]  (⇝ec-app)Ξ⊢e1⇝Γ1∘Ξ⊢e2⇝Γ2∘Ξ,(s:e1<:e2)⊢N:A⇝N′s∉ΞΞ⊢(e1<:e2⇒N):(e1<:e2⇒A)⇝λ(s:Γ1∘⇝Γ2∘).N′  (⇝sub-intro)Ξ⊢M:(e1<:e2⇒A)⇝M′Ξ⊢e1<:e2⇝NΞ⊢M∙:A⇝M′N  (⇝sub-elim)Ξ⊢M:A⇝M′Ξ⊢A<:B⇝LΞ⊢M:B⇝LM′  (⇝subsumption)\begin{aligned} &\boxed{\Xi; e \vdash M^{\circ}:T \rightsquigarrow M^{\circ\prime}}\\ &\frac{k:\iota}{\Xi;e\vdash k:\iota\rightsquigarrow k}\;(\rightsquigarrow\mathrm{code\text{-}const}) \qquad \frac{(x:T)^{\alpha}\in\Xi}{\Xi;\alpha\vdash x:T\rightsquigarrow x}\;(\rightsquigarrow\mathrm{code\text{-}var})\\ &\frac{\Xi,\alpha,e<:\alpha,(x:S)^{\alpha}\vdash N^{\circ}:T\rightsquigarrow N^{\circ\prime}\qquad\alpha\notin\Xi}{\Xi;e\vdash\lambda(x:S).N^{\circ}:S\to T\rightsquigarrow\lambda(x:S).N^{\circ\prime}}\;(\rightsquigarrow\mathrm{code\text{-}abs})\\ &\frac{\Xi;e\vdash L^{\circ}:S\to T\rightsquigarrow L^{\circ\prime}\qquad\Xi;e\vdash M^{\circ}:S\rightsquigarrow M^{\circ\prime}}{\Xi;e\vdash L^{\circ}M^{\circ}:T\rightsquigarrow L^{\circ\prime}M^{\circ\prime}}\;(\rightsquigarrow\mathrm{code\text{-}app})\\ &\frac{\Xi\vdash M:\text{``}T\text{''}_{e}\rightsquigarrow M'\qquad\Xi\vdash e\rightsquigarrow\Gamma}{\Xi;e\vdash\sim M:T\rightsquigarrow\sim M'\operatorname{id}_{\Gamma}\circ}\;(\rightsquigarrow\mathrm{splice})\\ &\frac{\Xi;e_{1}\vdash M^{\circ}:T\rightsquigarrow M^{\circ\prime}\qquad\Xi\vdash e_{1}\rightsquigarrow\Gamma^{\circ}\qquad\Xi\vdash e_{1}<:e_{2}\rightsquigarrow M}{\Xi;e_{2}\vdash M^{\circ}:T\rightsquigarrow\sim\text{``}\Gamma^{\circ}\vdash M^{\circ\prime}\text{''}M}\;(\rightsquigarrow\mathrm{subenv})\\ &\boxed{\Xi\vdash M:A\rightsquigarrow M'}\\ &\frac{k:\iota}{\Xi\vdash k:\iota\rightsquigarrow k}\;(\rightsquigarrow\mathrm{const}) \qquad \frac{x:A\in\Xi}{\Xi\vdash x:A\rightsquigarrow x}\;(\rightsquigarrow\mathrm{var})\\ &\frac{\Xi\vdash A\rightsquigarrow A'\qquad\Xi,x:A\vdash N:B\rightsquigarrow N'}{\Xi\vdash\lambda(x:A).N:A\to B\rightsquigarrow\lambda(x:A').N'}\;(\rightsquigarrow\mathrm{abs})\\ &\frac{\Xi\vdash L:A\to B\rightsquigarrow L'\qquad\Xi\vdash M:A\rightsquigarrow M'}{\Xi\vdash LM:B\rightsquigarrow L'M'}\;(\rightsquigarrow\mathrm{app})\\ &\frac{\Xi\vdash M:A\rightsquigarrow M'}{\Xi\vdash\operatorname{ref}M:\operatorname{Ref}A\rightsquigarrow\operatorname{ref}M'}\;(\rightsquigarrow\mathrm{ref})\\ &\frac{\Xi\vdash L:\operatorname{Ref}A\rightsquigarrow L'\qquad\Xi\vdash M:A\rightsquigarrow M'}{\Xi\vdash L:=M:\operatorname{Unit}\rightsquigarrow L':=M'}\;(\rightsquigarrow\mathrm{assign})\\ &\frac{\Xi\vdash M:\operatorname{Ref}A\rightsquigarrow M'}{\Xi\vdash!M:A\rightsquigarrow!M'}\;(\rightsquigarrow\mathrm{deref})\\ &\frac{\Xi;e\vdash M^{\circ}:T\rightsquigarrow M^{\circ\prime}\qquad\Xi\vdash e\rightsquigarrow\Gamma^{\circ}}{\Xi\vdash\text{``}M^{\circ}\text{''}_{e}:\text{``}T\text{''}_{e}\rightsquigarrow\text{``}\Gamma^{\circ}\vdash M^{\circ\prime}\text{''}}\;(\rightsquigarrow\mathrm{quote})\\ &\frac{\Xi,\alpha_{\mathrm{poly}}\vdash N:A\rightsquigarrow N'\qquad\alpha\notin\Xi}{\Xi\vdash\Lambda\alpha.N:\forall\alpha.A\rightsquigarrow\Lambda\alpha.N'}\;(\rightsquigarrow\mathrm{ec\text{-}abs})\\ &\frac{\Xi\vdash M:\forall\alpha.A\rightsquigarrow M'\qquad\Xi\vdash e\rightsquigarrow\Gamma^{\circ}}{\Xi\vdash M[e]:A[\alpha:=e]\rightsquigarrow M'[\Gamma^{\circ}]}\;(\rightsquigarrow\mathrm{ec\text{-}app})\\ &\frac{\Xi\vdash e_{1}\rightsquigarrow\Gamma_{1}^{\circ}\qquad\Xi\vdash e_{2}\rightsquigarrow\Gamma_{2}^{\circ}\qquad\Xi,(s:e_{1}<:e_{2})\vdash N:A\rightsquigarrow N'\qquad s\notin\Xi}{\Xi\vdash(e_{1}<:e_{2}\Rightarrow N):(e_{1}<:e_{2}\Rightarrow A)\rightsquigarrow\lambda(s:\Gamma_{1}^{\circ}\rightsquigarrow\Gamma_{2}^{\circ}).N'}\;(\rightsquigarrow\mathrm{sub\text{-}intro})\\ &\frac{\Xi\vdash M:(e_{1}<:e_{2}\Rightarrow A)\rightsquigarrow M'\qquad\Xi\vdash e_{1}<:e_{2}\rightsquigarrow N}{\Xi\vdash M^{\bullet}:A\rightsquigarrow M'N}\;(\rightsquigarrow\mathrm{sub\text{-}elim})\\ &\frac{\Xi\vdash M:A\rightsquigarrow M'\qquad\Xi\vdash A<:B\rightsquigarrow L}{\Xi\vdash M:B\rightsquigarrow LM'}\;(\rightsquigarrow\mathrm{subsumption}) \end{aligned}

Figure 21. Compilation of code terms and meta terms.

Converting classifiers into contexts. The three rules with green highlights replace a classifier by its context. The context is computed by Ξ⊢e⇝Γ∘\Xi\vdash e \rightsquigarrow\Gamma^\circ of Figure 8. A quotation $M^\circ$''$_e$ compiles to Γ∘⊢M′∘\Gamma^\circ\vdash M^{\prime\circ}'' (⇝quote\rightsquigarrow quote). A classifier abstraction compiles to a context abstraction (⇝ec-abs\leadsto ec\text{-}abs). A classifier application M[e]M[e] compiles to a context application M′[Γ′]M'[\Gamma'] (⇝ec-app\leadsto ec\text{-}app).

Compiling subtyping. The four rules with blue highlights insert the substitution or coercion that a subtyping compiles to. Classifier subsumption from e1e_1 to e2e_2 quotes the code in the context of e1e_1 and splices it under the substitution for e1<:e2e_1 <: e_2, which carries the code to the context of e2e_2 (⇝subenv\leadsto subenv). Subtype constraint introduction compiles to an abstraction over the evidence variable ss for the constraint, which has a substitution type, with the body compiled under Ξ\Xi extended with the named constraint s:e1<:e2s : e_1 <: e_2 (⇝sub-intro\leadsto sub\text{-}intro). Constraint elimination M∙M\mathbin{\bullet} compiles to an application to the substitution for the discharged constraint (⇝sub-elim\leadsto sub\text{-}elim). Subsumption from AA to BB compiles to an application of the coercion for A<:BA <: B (⇝subsumption\leadsto subsumption). In summary, these four rules replace every use of subtyping in the λRefα\lambda^\alpha_{\mathrm{Ref}} typing derivation by an explicit term in λRefCtx\lambda^{\mathrm{Ctx}}_{\mathrm{Ref}}.

Metatheoretical Results

In this section, we prove that the compilation of § 1 preserves types (§ 6.1) and is semantically correct (§ 6.2). All lemmas and theorems in this section are mechanized in Agda. Table 1 of the Appendix maps each result to Agda.

ResultsAgda identifiersAgda files
Section 6.1: Compilation preserves types
Well-formed compilation context (Definition 1)CompileCtxWFCompileCtx.agda
Compiled contexts and types are well formed (Lemma 1)ec-compiles, ec-unique, ec-wf, type-compiles, type-unique, type-wfCompile.agda, CompilePres.agda
Subtyping compiles to well-typed terms (Lemma 2)compile-ec-subtyping-preserves-types, compile-subtyping-preserves-typesCompilePres.agda
Compilation preserves types under a compilation context (Lemma 3)compile-code, compile-metaCompilePres.agda
Compilation preserves types (Theorem 1)compile-preserves-typesCompilePres.agda
Section 6.2: Semantic correctness
Simulation relation (Figures 24–26)_;_;_⊢_:_∼_, _;_⊢_:_∼_, _⊢_<:_⇝_⇒_Simulation/Relation.agda, Simulation/Subtyping.agda
Compilation embeds into simulation (Lemma 4)embed-code-compilation, embed-meta-compilationSimulation/Relation.agda
Forward simulation (Lemma 5)forward-simulationᵒ, forward-simulation, forward-simulation*ForwardSimulation.agda
Backward simulation (Lemma 6)backward-simulationᵒ, backward-simulation, backward-simulation*BackwardSimulation.agda
Semantic correctness (Theorem 2)semantic-preservation, semantic-reflectionCompilerCorrectness.agda

Table 1. Mapping from metatheoretical results to their Agda mechanization. File paths are relative to the root of the Agda development (the Theories/ directory).

Compilation Preserves Types

We first relate compilation contexts to typing contexts. We then define the well-formedness of compilation contexts. With the definitions in hand, we prove that the compilation from λRefα\lambda^\alpha_{\mathrm{Ref}} to λRefCtx\lambda^{\mathrm{Ctx}}_{\mathrm{Ref}} preserves types (Theorem 1).

Relating the contexts. The lemmas that lead to type preservation involve three judgments: the typing judgment of λRefα\lambda^\alpha_{\mathrm{Ref}}, the compilation judgment Ξ⊢M:A⇝M′\Xi\vdash M : A \leadsto M', and the typing judgment of λRefCtx\lambda^{\mathrm{Ctx}}_{\mathrm{Ref}}. Since each judgment has its own context, Ξ\Xi needs a reading in both languages. For the source language λRefα\lambda^\alpha_{\mathrm{Ref}}, the operator ∣Ξ∣|\Xi|, defined in Figure 22 of the Appendix, turns Ξ\Xi into a λRefα\lambda^\alpha_{\mathrm{Ref}} typing context. It forgets the two annotations that only the compilation needs, namely which classifiers are polymorphic and which subtype constraints have evidence variables. For the target language λRefCtx\lambda^{\mathrm{Ctx}}_{\mathrm{Ref}}, the judgment ⊢Ξ⇝Γ\vdash\Xi\leadsto\Gamma, also defined in Figure 22 of the Appendix, compiles the entries of the metalanguage in Ξ\Xi to a meta typing context Γ\Gamma. The code-level entries of Ξ\Xi are dropped, because the code context of a code term comes instead from its classifier.

Mappings from a compilation context to a typing context

Figure 22. Mappings from a compilation context to a typing context of the source language, λRefα\lambda^{\alpha}_{\mathrm{Ref}}, and a meta typing context of the target language, λRefCtx\lambda^{\mathrm{Ctx}}_{\mathrm{Ref}}.

Well-formed compilation contexts. The compilation lemmas require compilation contexts to be well formed. Intuitively, a compilation context is well formed when each scope has at most one parent scope, which may be empty (∅\varnothing), and two distinct scopes do not share a classifier. Definition 1 of the Appendix formalizes this intuition, and every compilation context that the compilation in § 1 builds is well formed.

Compilation preserves types. The first lemma shows that every well-formed classifier compiles to exactly one context, and every well-formed type to exactly one type, both of which are well formed in the target context.

Lemma 1 (Compiled contexts and types are well formed). Suppose ⊢Ξ\vdash\Xi and ⊢Ξ⇝Γ\vdash\Xi\leadsto\Gamma.

  1. If ∣Ξ∣⊢e|\Xi| \vdash e, then Ξ⊢e⇝Γ∘\Xi\vdash e \leadsto\Gamma^{\circ} and Γ⊢Γ∘ ctx\Gamma\vdash\Gamma^{\circ}\ \mathrm{ctx} for a unique Γ∘\Gamma^{\circ}.

  2. If ∣Ξ∣⊢A|\Xi| \vdash A, then Ξ⊢A⇝A′\Xi\vdash A \leadsto A^{\prime} and Γ⊢A′ ty\Gamma\vdash A^{\prime}\ \mathrm{ty} for a unique A′A^{\prime}.

The second lemma shows that the terms the compilation inserts for subtyping are well typed under the meta typing context Γ\Gamma that Ξ\Xi compiles to. The heap typing context is empty because the compilation does not produce addresses.

Lemma 2 (Subtyping compiles to well-typed terms). Suppose ⊢Ξ\vdash\Xi and ⊢Ξ⇝Γ\vdash\Xi\leadsto\Gamma.

  1. If Ξ⊢e1⇝Γ1∘\Xi\vdash e_{1} \leadsto\Gamma^{\circ}_{1}, Ξ⊢e2⇝Γ2∘\Xi\vdash e_{2} \leadsto\Gamma^{\circ}_{2}, and Ξ⊢e1<:e2⇝M\Xi\vdash e_{1} <: e_{2} \leadsto M, then Γ;∅⊢M:Γ1∘⇒Γ2∘\Gamma;\varnothing\vdash M : \Gamma^{\circ}_{1} \Rightarrow\Gamma^{\circ}_{2}.

  2. If Ξ⊢A⇝A′\Xi\vdash A \leadsto A^{\prime}, Ξ⊢B⇝B′\Xi\vdash B \leadsto B^{\prime}, and Ξ⊢A<:B⇝M\Xi\vdash A <: B \leadsto M, then Γ;∅⊢M:A′→B′\Gamma;\varnothing\vdash M : A^{\prime} \to B^{\prime}.

The main lemma shows that every well-typed λRefα\lambda^{\alpha}_{\mathrm{Ref}} term compiles, and that the compiled term is well typed in λRefCtx\lambda^{\mathrm{Ctx}}_{\mathrm{Ref}} at the compiled type.

Lemma 3 (Compilation of code and meta terms preserves types). Suppose ⊢Ξ\vdash\Xi and ⊢Ξ⇝Γ\vdash\Xi\leadsto\Gamma.

  1. If ∣Ξ∣;e⊢M∘:T|\Xi|;e \vdash M^{\circ}:T and Ξ⊢e⇝Γ∘\Xi\vdash e \leadsto\Gamma^{\circ}, then there exists an M∘′M^{\circ\prime} such that Ξ;e⊢M∘:T⇝M∘′\Xi;e \vdash M^{\circ}:T \leadsto M^{\circ\prime}, and Γ;∅;Γ∘⊢M∘′:T\Gamma;\varnothing;\Gamma^{\circ} \vdash M^{\circ\prime}:T.

  2. If ∣Ξ∣⊢M:A|\Xi| \vdash M:A, then there exist A′A^{\prime} and M′M^{\prime} such that Ξ⊢A⇝A′\Xi\vdash A \leadsto A^{\prime} and Ξ⊢M:A⇝M′\Xi\vdash M:A \leadsto M^{\prime}, and Γ;∅⊢M′:A′\Gamma;\varnothing\vdash M^{\prime}:A^{\prime}.

The type preservation theorem is a corollary of Lemma 3 for closed programs.

Theorem 1 (Compilation of λRefα\lambda^{\alpha}_{\mathrm{Ref}} programs to λRefCtx\lambda^{\mathrm{Ctx}}_{\mathrm{Ref}} preserves types). If a λRefα\lambda^{\alpha}_{\mathrm{Ref}} term MM is well typed, ∅⊢M:A\varnothing\vdash M:A, then there exist a type A′A^{\prime} and a term M′M^{\prime} in λRefCtx\lambda^{\mathrm{Ctx}}_{\mathrm{Ref}} such that ∅⊢A⇝A′\varnothing\vdash A \leadsto A^{\prime}, ∅⊢M:A⇝M′\varnothing\vdash M:A \leadsto M^{\prime}, and ∅;∅⊢M′:A′\varnothing;\varnothing\vdash M^{\prime}:A^{\prime}.

Semantic Correctness of Compilation Through Bisimulation

We now prove the semantic correctness of compilation (Theorem 2). We first define reification, which turns code values of λRefα\lambda^{\alpha}_{\mathrm{Ref}} into STLC terms. We then prove bisimulation between λRefα\lambda^{\alpha}_{\mathrm{Ref}} and λRefCtx\lambda^{\mathrm{Ctx}}_{\mathrm{Ref}} and obtain the theorem as a corollary.

Reification. The code value that a λRefα\lambda^{\alpha}_{\mathrm{Ref}} program generates contains globally allocated variables, because the code-λ rule allocates a global variable for each code abstraction. To obtain code in STLC, reification converts each global variable to a fresh lexical one, so that every code abstraction value (λ‾\overline{\lambda}) becomes a lexical λ\lambda of STLC. Reification is defined in Figure 23 of the Appendix. The code values of λRefCtx\lambda^{\mathrm{Ctx}}_{\mathrm{Ref}} do not need reification, because they are already STLC terms.

Reification rules for the code values of lambda Ref into STLC terms

Figure 23. Reification of the code values of λRefα\lambda_{\mathrm{Ref}}^{\alpha} into STLC terms.

Bisimulation. The simulation relation on terms is similar to the compilation relation of § 1, but it is closed under reduction. It relates a code or meta term of λRefα\lambda^{\alpha}_{\mathrm{Ref}} to one of λRefCtx\lambda^{\mathrm{Ctx}}_{\mathrm{Ref}} under a compilation context Ξ\Xi and a heap typing context Σ\Sigma. The full simulation rules are in Figures 24 and Figure 25 of the Appendix. The simulation relation differs from the compilation in two ways. First, it relates terms that arise only at runtime, such as code abstraction values (λ\lambda) and addresses. Second, it has value rules for catching up with a coercion. Once a subsumed source term is a value, the coerced target term still has to apply its coercion or substitution, in steps that the source does not take. The value simulation rules relate the source value to the value that the target reaches once it has caught up. The embedding lemma says that a term is in simulation with its compilation.

Simulation of meta terms

Figure 25. Simulation of meta terms.

Lemma 4 (Compilation embeds into simulation). Let Σ\Sigma be any heap typing context.

  1. If Ξ;e⊢M∘:T⇝M∘′\Xi; e \vdash M^\circ: T \rightsquigarrow M^{\circ\prime}, then Σ;Ξ;e⊢M∘:T∼M∘′\Sigma; \Xi; e \vdash M^\circ: T \sim M^{\circ\prime}.

  2. If Ξ⊢M:A⇝M′\Xi\vdash M : A \rightsquigarrow M^\prime, then Σ;Ξ⊢M:A∼M′\Sigma; \Xi\vdash M : A \sim M^\prime.

The simulation relation on configurations, written Σ;Ψ⊢C:A∼M∣μ\Sigma; \Psi\vdash C : A \sim M \mid\mu, relates a configuration CC of λRefα\lambda^{\alpha}_{\mathrm{Ref}} to a term MM and a heap μ\mu of λRefCtx\lambda^{\mathrm{Ctx}}_{\mathrm{Ref}}. The simulation context Ψ\Psi holds the classifiers, subtype constraints, and code variables that code abstractions add: Ψ::=∅∣Ψ,α∣Ψ,(x:T)α∣Ψ,e1<:e2\Psi::= \emptyset\mid\Psi, \alpha\mid\Psi, (x:T)^\alpha\mid\Psi, e_1 <: e_2. We write ⟶m∗\longrightarrow^{m*} for the reflexive, transitive closure of a reduction relation and ⊆\subseteq for inclusion of contexts viewed as sets. The next two lemmas establish a bisimulation.

Lemma 5 (Forward simulation). If Σ;Ψ⊢C:A∼M∣μ\Sigma; \Psi\vdash C : A \sim M \mid\mu and C⟶mDC \longrightarrow^m D, then there exist Ψ′\Psi^\prime, Σ′\Sigma^\prime, NN, and μ′\mu^\prime such that Ψ⊆Ψ′\Psi\subseteq\Psi^\prime, Σ⊆Σ′\Sigma\subseteq\Sigma^\prime, M∣μ⟶m∗N∣μ′M \mid\mu\longrightarrow^{m*} N \mid\mu^\prime, and Σ′;Ψ′⊢D:A∼N∣μ′\Sigma^\prime; \Psi^\prime\vdash D : A \sim N \mid\mu^\prime.

Lemma 6 (Backward simulation). If Σ;Ψ⊢C:A∼M∣μ\Sigma; \Psi\vdash C : A \sim M \mid\mu and M∣μ⟶mN∣μ′M \mid\mu\longrightarrow^m N \mid\mu^\prime, then there exist Ψ′\Psi^\prime, Σ′\Sigma^\prime, DD, N′N^\prime, and μ′′\mu^{\prime\prime} such that Ψ⊆Ψ′\Psi\subseteq\Psi^\prime, Σ⊆Σ′\Sigma\subseteq\Sigma^\prime, C⟶m∗DC \longrightarrow^{m*} D, N∣μ′⟶m∗N′∣μ′′N \mid\mu^\prime\longrightarrow^{m*} N^\prime\mid\mu^{\prime\prime}, and Σ′;Ψ′⊢D:A∼N′∣μ′′\Sigma^\prime; \Psi^\prime\vdash D : A \sim N^\prime\mid\mu^{\prime\prime}.

Semantic correctness. A configuration CC of λRefα\lambda^{\alpha}_{\mathrm{Ref}} diverges, written C⇑C \Uparrow, if every configuration that CC reduces to can take a step, and likewise M∣μ⇑M \mid\mu\Uparrow in λRefCtx\lambda^{\mathrm{Ctx}}_{\mathrm{Ref}}. The semantic correctness theorem states that a closed λRefα\lambda^{\alpha}_{\mathrm{Ref}} program MM of code type ``TT''ε_\varepsilon and its compilation M′M^\prime behave the same. If one of them evaluates to a quotation of a code value, so does the other, and the code value of MM reifies to the generated code of M′M^\prime. The generated code is an STLC term of type TT. If one of them diverges, so does the other. The proof relates MM to M′M^\prime by Lemma 4 and keeps them related by Lemmas 5 and 6 as either side reduces.

Theorem 2 (Semantic correctness). Suppose a λRefα\lambda^{\alpha}_{\mathrm{Ref}} term MM is well typed, ∅⊢M:“T”ε\emptyset\vdash M : \text{``}T\text{''}_\varepsilon. Then there exists a well-typed λRefα\lambda^{\alpha}_{\mathrm{Ref}} term M′M^\prime that MM compiles to, ∅⊢M:“T”ε⇝M′\emptyset\vdash M : \text{``}T\text{''}_\varepsilon\rightsquigarrow M^\prime and ∅;∅⊢M′:“∅⊢T”\emptyset; \emptyset\vdash M^\prime: \text{``}\emptyset\vdash T\text{''}, such that the following hold.

  1. If ∅;M;∅⟶m∗⟨Γ;V;μ⟩\emptyset; M; \emptyset\longrightarrow^{m*} \langle\Gamma; V; \mu\rangle for a value VV, then V=“V∘”εV = \text{``}V^\circ\text{''}_\varepsilon for a code value V∘V^\circ, and there exist a code value W∘W^\circ and a heap μ′\mu^\prime such that M′∣∅⟶m∗“∅⊢W∘”∣μ′M^\prime\mid\emptyset\longrightarrow^{m*} \text{``}\emptyset\vdash W^\circ\text{''} \mid\mu^\prime and reify⁡(V∘)=W∘\operatorname{reify}(V^\circ) = W^\circ.

  1. If M' \mid\varnothing\longrightarrow^{m^*} ``\varnothing\vdash W^\circ'' \mid\mu' for a code value W∘W^\circ, then there exist a code value V∘V^\circ, a context Γ\Gamma, and a heap μ\mu such that \langle\varnothing; M; \varnothing\rangle\longrightarrow^{m^*} \langle\Gamma; ``V^\circ''_\varepsilon; \mu\rangle and reify⁡(V∘)=W∘\operatorname{reify}(V^\circ) = W^\circ.

  1. ⟨∅;M;∅⟩⇑\langle\varnothing; M; \varnothing\rangle\Uparrow if and only if M′∣∅⇑M' \mid\varnothing\Uparrow.

In the first two cases, the generated code is a well-typed STLC term, ∅⊢W∘:T\varnothing\vdash W^\circ: T.

Conclusion

This paper connects two approaches to the scopes of generated code, environment classifiers and contextual modal type theory, through a type-directed compilation. We proved in Agda that the compilation preserves types and behavior. CMTT is thus at least as expressive as environment classifiers. We conjecture that the converse compilation is hard, because environment classifiers impose a stricter regime on how the metaprogram scopes generated code.

Appendix

The appendix is organized as follows.

  1. Table 1 maps the metatheoretical results to their Agda mechanization.

  2. Figure 11 defines the well-formedness for ECs and meta types of λRefα\lambda_{\mathrm{Ref}}^{\alpha}.

  3. Figure 12 defines the subtyping rules of λRefα\lambda_{\mathrm{Ref}}^{\alpha}.

  4. Figure 13 defines the typing rules of λRefα\lambda_{\mathrm{Ref}}^{\alpha}.

  5. Figure 14 defines the typing rules for the runtime language of λRefα\lambda_{\mathrm{Ref}}^{\alpha}.

  6. Figure 15 defines the reduction semantics of λRefα\lambda_{\mathrm{Ref}}^{\alpha}.

  7. Figure 16 defines the well-formedness for code contexts and types of λRefCtx\lambda_{\mathrm{Ref}}^{\mathrm{Ctx}}.

  8. Figure 17 defines the typing rules of λRefCtx\lambda_{\mathrm{Ref}}^{\mathrm{Ctx}}.

  9. Figure 18 defines the reduction semantics of λRefCtx\lambda_{\mathrm{Ref}}^{\mathrm{Ctx}}.

  10. Figure 19 defines the compilation of classifiers and types.

  11. Figure 20 defines the compilation of subtyping.

  12. Figure 21 defines the compilation of code terms and meta terms.

  13. Figure 22 maps a compilation context to a typing context of λRefα\lambda_{\mathrm{Ref}}^{\alpha} and to a meta typing context of λRefCtx\lambda_{\mathrm{Ref}}^{\mathrm{Ctx}}.

  14. Definition 1 defines well-formed compilation contexts.

  15. Figure 23 defines the reification of the code values of λRefα\lambda_{\mathrm{Ref}}^{\alpha} into STLC terms.

  16. fig:24 defines the simulation relation on configurations, heaps, and code terms.

  17. Figure 25 defines the simulation relation on meta terms.

  18. Figure 26 defines the coerced value relation that the simulation relation uses.

Typing rules for the runtime language of lambda Ref
⋯Γ⊢α(x:S)α⊆Γe<:α∈ΓΓ;Σ;α⊢N∘:TΓ;Σ;e⊢λ(x:S)α.N∘:S→T  (⊢code-abs-val)\cdots\qquad \frac{\Gamma\vdash\alpha\qquad(x:S)^\alpha\subseteq\Gamma\qquad e<:\alpha\in\Gamma\qquad\Gamma;\Sigma;\alpha\vdash N^\circ:T}{\Gamma;\Sigma;e\vdash\lambda(x:S)^\alpha.N^\circ:S\to T}\;(\vdash\text{code-abs-val})
⋯a:A∈ΣΓ;Σ⊢a:Ref⁡A  (⊢addr)\cdots\qquad \frac{a:A\in\Sigma}{\Gamma;\Sigma\vdash a:\operatorname{Ref}A}\;(\vdash\text{addr})

Figure 14. Typing rules for the runtime language of λRef⁡α\lambda_{\operatorname{Ref}}^\alpha.

Operational semantics of lambda Ref

Figure 15. Operational semantics of λRefα\lambda^{\alpha}_{\mathrm{Ref}}.

Compilation contexts, and compilation of classifiers and types

Figure 19. Compilation contexts, and compilation of classifiers and types.

![Coerced value relation inference rules]

Figure 26. The coerced value relation.

Auxiliary Definitions for λRefα\lambda_{\mathrm{Ref}}^\alpha

code frames F∘::=□ M∘∣V∘□F^{\circ} ::= \square\ M^{\circ}\mid V^{\circ}\square

meta frames F::=□ M∣V □∣ref⁡ □∣□:=M∣V:=□∣! □∣□[e]∣□∙F ::= \square\ M\mid V\ \square\mid\operatorname{ref}\ \square\mid\square:= M\mid V := \square\mid!\ \square\mid\square[e]\mid\square\mathbin{\bullet}

e⊢⟨Γ;M∘;μ⟩⟶∘⟨Γ′;N∘;μ′⟩e \vdash\langle\Gamma; M^{\circ}; \mu\rangle\longrightarrow^{\circ} \langle\Gamma'; N^{\circ}; \mu'\rangle
e⊢⟨Γ;M∘;μ⟩⟶∘⟨Γ′;N∘;μ′⟩e⊢⟨Γ;F∘[M∘];μ⟩⟶∘⟨Γ′;F∘[N∘];μ′⟩(ξ-code)\frac{e \vdash\langle\Gamma; M^{\circ}; \mu\rangle\longrightarrow^{\circ} \langle\Gamma'; N^{\circ}; \mu'\rangle}{e \vdash\langle\Gamma; F^{\circ}[M^{\circ}]; \mu\rangle\longrightarrow^{\circ} \langle\Gamma'; F^{\circ}[N^{\circ}]; \mu'\rangle} \quad(\xi\text{-code})
⟨Γ;M;μ⟩⟶m⟨Γ′;N;μ′⟩e⊢⟨Γ;∼M;μ⟩⟶∘⟨Γ′;∼N;μ′⟩(ξ-splice)\frac{\langle\Gamma; M; \mu\rangle\longrightarrow^{m} \langle\Gamma'; N; \mu'\rangle}{e \vdash\langle\Gamma; \sim M; \mu\rangle\longrightarrow^{\circ} \langle\Gamma'; \sim N; \mu'\rangle} \quad(\xi\text{-splice})
e⊢⟨Γ;∼ ⁣‘‘V∘ ⁣e′′;μ⟩⟶∘⟨Γ;V∘;μ⟩(β-splice)e \vdash\langle\Gamma; \sim\!``V^{\circ}\!''_{e}; \mu\rangle\longrightarrow^{\circ} \langle\Gamma; V^{\circ}; \mu\rangle \quad(\beta\text{-splice})
β,y∉Γe⊢⟨Γ;λ(x:T)α.N∘;μ⟩⟶∘⟨(Γ,β,(e<:β),(y:T)β);λ‾(y:T)β.N∘[α:=β][x:=y];μ⟩(code-λ)\frac{\beta,y\notin\Gamma}{e \vdash\langle\Gamma; \lambda(x:T)^{\alpha}.N^{\circ}; \mu\rangle\longrightarrow^{\circ} \langle(\Gamma,\beta,(e<:\beta),(y:T)^{\beta});\overline{\lambda}(y:T)^{\beta}.N^{\circ}[\alpha:=\beta][x:=y];\mu\rangle} \quad(\text{code-}\lambda)
α⊢⟨Γ;N∘;μ⟩⟶∘⟨Γ′;N∘′;μ′⟩e⊢⟨Γ;λ(x:T)α.N∘;μ⟩⟶∘⟨Γ′;λ‾(x:T)α.N∘′;μ′⟩(ξ-code-λ‾)\frac{\alpha\vdash\langle\Gamma; N^{\circ}; \mu\rangle\longrightarrow^{\circ} \langle\Gamma'; N^{\circ\prime}; \mu'\rangle}{e \vdash\langle\Gamma; \lambda(x:T)^{\alpha}.N^{\circ}; \mu\rangle\longrightarrow^{\circ} \langle\Gamma'; \overline{\lambda}(x:T)^{\alpha}.N^{\circ\prime}; \mu'\rangle} \quad(\xi\text{-code-}\overline{\lambda})
⟨Γ;M;μ⟩⟶m⟨Γ′;N;μ′⟩\langle\Gamma; M; \mu\rangle\longrightarrow^{m} \langle\Gamma'; N; \mu'\rangle
⟨Γ;M;μ⟩⟶m⟨Γ′;N;μ′⟩⟨Γ;F[M];μ⟩⟶m⟨Γ′;F[N];μ′⟩(ξ-meta)\frac{\langle\Gamma; M; \mu\rangle\longrightarrow^{m} \langle\Gamma'; N; \mu'\rangle}{\langle\Gamma; F[M]; \mu\rangle\longrightarrow^{m} \langle\Gamma'; F[N]; \mu'\rangle} \quad(\xi\text{-meta})
e⊢⟨Γ;M∘;μ⟩⟶∘⟨Γ′;N∘;μ′⟩⟨Γ;‘‘M∘ ⁣e′′;μ⟩⟶m⟨Γ′;‘‘N∘ ⁣e′′;μ′⟩(ξ-quote)\frac{e \vdash\langle\Gamma; M^{\circ}; \mu\rangle\longrightarrow^{\circ} \langle\Gamma'; N^{\circ}; \mu'\rangle}{\langle\Gamma; ``M^{\circ}\!''_{e}; \mu\rangle\longrightarrow^{m} \langle\Gamma'; ``N^{\circ}\!''_{e}; \mu'\rangle} \quad(\xi\text{-quote})
⟨Γ;(λx:A.M) V;μ⟩⟶m⟨Γ;M[x:=V];μ⟩(β)\langle\Gamma;(\lambda x:A.M)\ V;\mu\rangle\longrightarrow^{m} \langle\Gamma;M[x:=V];\mu\rangle \quad(\beta)
⟨Γ;(Λα.M)[e];μ⟩⟶m⟨Γ;M[α:=e];μ⟩(β-ec)\langle\Gamma;(\Lambda\alpha.M)[e];\mu\rangle\longrightarrow^{m} \langle\Gamma;M[\alpha:=e];\mu\rangle \quad(\beta\text{-ec})
⟨Γ;(e1<:e2⇒M)∙;μ⟩⟶m⟨Γ;M;μ⟩(β-sub)\langle\Gamma;(e_{1}<:e_{2}\Rightarrow M)\mathbin{\bullet};\mu\rangle\longrightarrow^{m} \langle\Gamma;M;\mu\rangle \quad(\beta\text{-sub})
a∉dom⁡(μ)⟨Γ;ref⁡ V;μ⟩⟶m⟨Γ;a;μ[a↦V]⟩(ref⁡)\frac{a\notin\operatorname{dom}(\mu)}{\langle\Gamma;\operatorname{ref}\ V;\mu\rangle\longrightarrow^{m} \langle\Gamma;a;\mu[a\mapsto V]\rangle} \quad(\operatorname{ref})
μ(a)=V⟨Γ;!a;μ⟩⟶m⟨Γ;V;μ⟩(deref⁡)\frac{\mu(a)=V}{\langle\Gamma;!a;\mu\rangle\longrightarrow^{m} \langle\Gamma;V;\mu\rangle} \quad(\operatorname{deref})
⟨Γ;a:=V;μ⟩⟶m⟨Γ;();μ[a↦V]⟩(assign⁡)\langle\Gamma;a:=V;\mu\rangle\longrightarrow^{m} \langle\Gamma;();\mu[a\mapsto V]\rangle \quad(\operatorname{assign})

Auxiliary Definitions for λRefCtx\lambda_{\mathrm{Ref}}^{\mathrm{Ctx}}

 Γ⊢∅ ctxα∈ΓΓ⊢α ctx Γ⊢x:T ctxΓ⊢Γ∘ ctxΓ⊢Δ∘ ctxΓ⊢Γ∘,Δ∘ ctx Γ⊢ι tyΓ⊢A tyΓ⊢B tyΓ⊢A→B tyΓ⊢A tyΓ⊢Ref A tyΓ⊢Γ∘ ctxΓ⊢‘‘Γ∘⊢T′′ tyΓ,α⊢A tyΓ⊢∀α. A tyΓ⊢Γ∘ ctxΓ⊢Δ∘ ctxΓ⊢Γ∘⇒Δ∘ ty\begin{aligned} &\frac{}{\,\Gamma\vdash\varnothing\ \mathrm{ctx}} &&\frac{\alpha\in\Gamma}{\Gamma\vdash\alpha\ \mathrm{ctx}} &&\frac{}{\,\Gamma\vdash x:T\ \mathrm{ctx}}\\[1em] &\frac{\Gamma\vdash\Gamma^{\circ}\ \mathrm{ctx}\qquad\Gamma\vdash\Delta^{\circ}\ \mathrm{ctx}}{\Gamma\vdash\Gamma^{\circ},\Delta^{\circ}\ \mathrm{ctx}}\\[1em] &\frac{}{\,\Gamma\vdash\iota\ \mathrm{ty}} &&\frac{\Gamma\vdash A\ \mathrm{ty}\qquad\Gamma\vdash B\ \mathrm{ty}}{\Gamma\vdash A \to B\ \mathrm{ty}} &&\frac{\Gamma\vdash A\ \mathrm{ty}}{\Gamma\vdash\mathrm{Ref}\ A\ \mathrm{ty}}\\[1em] &\frac{\Gamma\vdash\Gamma^{\circ}\ \mathrm{ctx}}{\Gamma\vdash``\Gamma^{\circ} \vdash T''\ \mathrm{ty}} &&\frac{\Gamma,\alpha\vdash A\ \mathrm{ty}}{\Gamma\vdash\forall\alpha.\,A\ \mathrm{ty}} &&\frac{\Gamma\vdash\Gamma^{\circ}\ \mathrm{ctx}\qquad\Gamma\vdash\Delta^{\circ}\ \mathrm{ctx}}{\Gamma\vdash\Gamma^{\circ} \Rightarrow\Delta^{\circ}\ \mathrm{ty}} \end{aligned}

Figure 16 defines the well-formedness judgments for code contexts and meta types of λRefCtx\lambda_{\mathrm{Ref}}^{\mathrm{Ctx}}. A code context is well formed when every context variable in it is well scoped in Γ\Gamma. Three rules for meta types concern context variables and code contexts. The rule for ∀α. A\forall\alpha.\,A binds the context variable α\alpha in the body AA. The rule for code types ``Γ∘⊢T\Gamma^{\circ} \vdash T'' requires Γ∘\Gamma^{\circ} to be well formed. The rule for substitution types Γ∘⇒Δ∘\Gamma^{\circ} \Rightarrow\Delta^{\circ} requires both the domain Γ∘\Gamma^{\circ} and the codomain Δ∘\Delta^{\circ} to be well formed.

code valuesV∘,W∘::=k∣x∣λ(x:T).V∘∣V∘ W∘meta valuesV,W::=k∣a∣λ(x:A).N∣Λα.N∣⟦Γ∘⊢V∘⟧∣proxy⁡ V V V∣id⁡Γ∘∣(V,x=V∘)∣weaken⁡Γ∘(V∘)∣V∘Wheapsμ::=∅∣μ,a↦Vcode framesF∘::=□ M∘∣V∘ □∣λ(x:T).□meta framesF::=□ M∣V □∣□[Γ∘]∣□∘M∣V∘□∣weaken⁡Γ∘(□)∣(□,x=V∘)∣ref⁡ □∣□:=M∣V:=□∣!□∣proxy⁡ □ M N∣proxy⁡ V □ N∣proxy⁡ V W □\begin{aligned} \text{code values}\quad V^{\circ},W^{\circ} &::= k \mid x \mid\lambda(x:T).V^{\circ} \mid V^{\circ}\ W^{\circ}\\ \text{meta values}\quad V,W &::= k \mid a \mid\lambda(x:A).N \mid\Lambda\alpha.N \mid\llbracket\Gamma^{\circ}\vdash V^{\circ}\rrbracket\mid\operatorname{proxy}\ V\ V\ V\\ &\qquad\mid\operatorname{id}_{\Gamma^{\circ}} \mid(V,x=V^{\circ}) \mid\operatorname{weaken}_{\Gamma^{\circ}}(V^{\circ}) \mid V\circ W\\ \text{heaps}\quad\mu&::= \varnothing\mid\mu,a\mapsto V\\ \text{code frames}\quad F^{\circ} &::= \square\ M^{\circ}\mid V^{\circ}\ \square\mid\lambda(x:T).\square\\ \text{meta frames}\quad F &::= \square\ M\mid V\ \square\mid\square[\Gamma^{\circ}]\mid\square\circ M\mid V\circ\square\\ &\qquad\mid\operatorname{weaken}_{\Gamma^{\circ}}(\square)\mid(\square,x=V^{\circ})\mid\operatorname{ref}\ \square\mid\square:=M\mid V:=\square\\ &\qquad\mid!\square\mid\operatorname{proxy}\ \square\ M\ N\mid\operatorname{proxy}\ V\ \square\ N\mid\operatorname{proxy}\ V\ W\ \square \end{aligned}
⟦V⟧(x)=M∘⟦id⁡Γ∘⟧(x)=x⟦(V,y=V∘)⟧(x)={V∘if x=y⟦V⟧(x)if x≠y⟦weaken⁡Γ∘(V∘)⟧(x)=⟦V⟧(x)⟦V∘W⟧(x)=⟦W⟧(⟦V⟧(x))⟦V⟧(M∘)=N∘⟦V⟧(k)=k⟦V⟧(x)=⟦V⟧(x)⟦V⟧(λ(x:T).M∘)=λ(x:T).⟦V⟧(M∘)⟦V⟧(L∘ M∘)=(⟦V⟧(L∘))(⟦V⟧(M∘))⟦V⟧(∼M N)=∼M N\begin{aligned} \llbracket V\rrbracket(x)&=M^{\circ}\\ \llbracket\operatorname{id}_{\Gamma^{\circ}}\rrbracket(x)&=x\\ \llbracket(V,y=V^{\circ})\rrbracket(x)&= \begin{cases} V^{\circ} & \text{if }x=y\\ \llbracket V\rrbracket(x)&\text{if }x\ne y \end{cases}\\ \llbracket\operatorname{weaken}_{\Gamma^{\circ}}(V^{\circ})\rrbracket(x)&=\llbracket V\rrbracket(x)\\ \llbracket V\circ W\rrbracket(x)&=\llbracket W\rrbracket(\llbracket V\rrbracket(x))\\ \llbracket V\rrbracket(M^{\circ})&=N^{\circ}\\ \llbracket V\rrbracket(k)&=k\\ \llbracket V\rrbracket(x)&=\llbracket V\rrbracket(x)\\ \llbracket V\rrbracket(\lambda(x:T).M^{\circ})&=\lambda(x:T).\llbracket V\rrbracket(M^{\circ})\\ \llbracket V\rrbracket(L^{\circ}\ M^{\circ})&=(\llbracket V\rrbracket(L^{\circ}))(\llbracket V\rrbracket(M^{\circ}))\\ \llbracket V\rrbracket(\sim M\ N)&=\sim M\ N \end{aligned}
M∘∣μ⟶∘N∘∣μ′F∘[M∘]∣μ⟶∘F∘[N∘]∣μ′(ξ-code)M∣μ⟶mM′∣μ′∼M N∣μ⟶∘∼M′ N∣μ′(ξ1-splice)N∣μ⟶mN′∣μ′∼V N∣μ⟶∘∼V N′∣μ′(ξ2-splice)∼⟦Γ∘⊢V∘⟧ V∣μ⟶∘⟦V⟧(V∘)∣μ(β-splice)M∣μ⟶mN∣μ′F[M]∣μ⟶mF[N]∣μ′(ξ-meta)M∘∣μ⟶∘N∘⟦Γ∘⊢M∘⟧∣μ⟶m⟦Γ∘⊢N∘⟧∣μ(ξ-quote)(λ(x:A).N) V∣μ⟶mN[x:=V]∣μ(Λα.N)[Γ∘]∣μ⟶mN[α:=Γ∘]∣μ(β-ctx)a∉dom⁡(μ)ref⁡ V∣μ⟶ma∣μ,a↦V(ref⁡)a:=V∣μ⟶m()∣μ[a↦V](assign⁡)μ(a)=V!a∣μ⟶mV∣μ(deref⁡)!(proxy⁡ V1 V2 V3)∣μ⟶mV2(!V1)∣μ(proxy⁡-deref⁡)(proxy⁡ V1 V2 V3):=W∣μ⟶mV1:=(V3 W)∣μ(proxy⁡-assign⁡)\begin{aligned} \frac{M^{\circ}\mid\mu\longrightarrow^{\circ}N^{\circ}\mid\mu'}{F^{\circ}[M^{\circ}]\mid\mu\longrightarrow^{\circ}F^{\circ}[N^{\circ}]\mid\mu'}&\quad(\xi\text{-code})\\ \frac{M\mid\mu\longrightarrow^{m}M'\mid\mu'}{\sim M\ N\mid\mu\longrightarrow^{\circ}\sim M'\ N\mid\mu'}&\quad(\xi_{1}\text{-splice})\\ \frac{N\mid\mu\longrightarrow^{m}N'\mid\mu'}{\sim V\ N\mid\mu\longrightarrow^{\circ}\sim V\ N'\mid\mu'}&\quad(\xi_{2}\text{-splice})\\ \frac{}{ \sim\llbracket\Gamma^{\circ}\vdash V^{\circ}\rrbracket\ V\mid\mu\longrightarrow^{\circ}\llbracket V\rrbracket(V^{\circ})\mid\mu}&\quad(\beta\text{-splice})\\ \frac{M\mid\mu\longrightarrow^{m}N\mid\mu'}{F[M]\mid\mu\longrightarrow^{m}F[N]\mid\mu'}&\quad(\xi\text{-meta})\\ \frac{M^{\circ}\mid\mu\longrightarrow^{\circ}N^{\circ}}{\llbracket\Gamma^{\circ}\vdash M^{\circ}\rrbracket\mid\mu\longrightarrow^{m}\llbracket\Gamma^{\circ}\vdash N^{\circ}\rrbracket\mid\mu}&\quad(\xi\text{-quote})\\ \frac{(\lambda(x:A).N)\ V\mid\mu\longrightarrow^{m}N[x:=V]\mid\mu}{(\Lambda\alpha.N)[\Gamma^{\circ}]\mid\mu\longrightarrow^{m}N[\alpha:=\Gamma^{\circ}]\mid\mu}&\quad(\beta\text{-ctx})\\ \frac{a\notin\operatorname{dom}(\mu)}{\operatorname{ref}\ V\mid\mu\longrightarrow^{m}a\mid\mu,a\mapsto V}&\quad(\operatorname{ref})\\ \frac{}{a:=V\mid\mu\longrightarrow^{m}()\mid\mu[a\mapsto V]}&\quad(\operatorname{assign})\\ \frac{\mu(a)=V}{!a\mid\mu\longrightarrow^{m}V\mid\mu}&\quad(\operatorname{deref})\\ \frac{}{!(\operatorname{proxy}\ V_{1}\ V_{2}\ V_{3})\mid\mu\longrightarrow^{m}V_{2}(!V_{1})\mid\mu}&\quad(\operatorname{proxy}\text{-}\operatorname{deref})\\ \frac{}{(\operatorname{proxy}\ V_{1}\ V_{2}\ V_{3}):=W\mid\mu\longrightarrow^{m}V_{1}:=(V_{3}\ W)\mid\mu}&\quad(\operatorname{proxy}\text{-}\operatorname{assign}) \end{aligned}

Evaluation in λRefCtx\lambda_{\mathrm{Ref}}^{\mathrm{Ctx}} is call by value and left to right, as the frames (non-recursive evaluation contexts) of Figure 18 specify. The congruence rules of each level are grouped into a single rule that reduces under a frame (ξ\xi-code, ξ\xi-meta).

Auxiliary Definitions for the Compilation From λRefα\lambda_{\mathrm{Ref}}^{\alpha} to λRefCtxx\lambda_{\mathrm{Ref}}^{\mathrm{Ctxx}}

compilation contexts Ξ::=∅∣Ξ,α∣Ξ,αpoly∣Ξ,x:A∣Ξ,s:e1<:e2∣Ξ,(x:T)∣Ξ,e1<:e2\Xi::= \emptyset\mid\Xi, \alpha\mid\Xi, \alpha_{\mathrm{poly}} \mid\Xi, x:A \mid\Xi, s:e_1 <: e_2 \mid\Xi,(x:T) \mid\Xi,e_1 <: e_2

Ξ⊢e⇝Γ∘\Xi\vdash e \leadsto\Gamma^{\circ}
Ξ⊢ε⇝∅  (⇝empty)\frac{}{\Xi\vdash\varepsilon\leadsto\emptyset}\;(\leadsto\mathrm{empty})
αpoly∈ΞΞ⊢α⇝α  (⇝poly-ec)\frac{\alpha_{\mathrm{poly}} \in\Xi}{\Xi\vdash\alpha\leadsto\alpha}\;(\leadsto\mathrm{poly\text{-}ec})
α∈Ξe<:α∈Ξ(x:T)α∈ΞΞ⊢e⇝Γ∘Ξ⊢α⇝(Γ∘,x:T)  (⇝ec)\frac{\alpha\in\Xi\qquad e<:\alpha\in\Xi\qquad(x:T)^{\alpha} \in\Xi\qquad\Xi\vdash e \leadsto\Gamma^{\circ}}{\Xi\vdash\alpha\leadsto(\Gamma^{\circ},x:T)}\;(\leadsto\mathrm{ec})
Ξ⊢A⇝A′\Xi\vdash A \leadsto A'
Ξ⊢ι⇝ιΞ⊢ι⇝ι  (⇝base)\frac{\Xi\vdash\iota\leadsto\iota}{\Xi\vdash\iota\leadsto\iota}\;(\leadsto\mathrm{base})
Ξ⊢A⇝A′Ξ⊢B⇝B′Ξ⊢(A→B)⇝(A′→B′)  (⇝fun)\frac{\Xi\vdash A \leadsto A' \qquad\Xi\vdash B \leadsto B'}{\Xi\vdash(A \to B) \leadsto(A' \to B')}\;(\leadsto\mathrm{fun})
Ξ⊢A⇝A′Ξ⊢Ref⁡A⇝Ref⁡A′  (⇝ref)\frac{\Xi\vdash A \leadsto A'}{\Xi\vdash\operatorname{Ref} A \leadsto\operatorname{Ref} A'}\;(\leadsto\mathrm{ref})
Ξ⊢e⇝Γ∘Ξ⊢“T”e⇝“Γ∘⊢T”  (⇝code)\frac{\Xi\vdash e \leadsto\Gamma^{\circ}}{\Xi\vdash\text{``}T\text{''}e \leadsto\text{``}\Gamma^{\circ} \vdash T\text{''}}\;(\leadsto\mathrm{code})
Ξ,αpoly⊢A⇝A′α∉ΞΞ⊢∀α.A⇝∀α.A′  (⇝forall)\frac{\Xi,\alpha_{\mathrm{poly}} \vdash A \leadsto A' \qquad\alpha\notin\Xi}{\Xi\vdash\forall\alpha.A \leadsto\forall\alpha.A'}\;(\leadsto\mathrm{forall})
Ξ⊢e1<:e2⇝BΞ,s:e1<:e2⊢A⇝A′s∉ΞΞ⊢(e1<:e2⇒A)⇝(B→A′)  (⇝constraint)\frac{\Xi\vdash e_1 <: e_2 \leadsto B \qquad\Xi,s:e_1 <: e_2 \vdash A \leadsto A' \qquad s \notin\Xi}{\Xi\vdash(e_1 <: e_2 \Rightarrow A) \leadsto(B \to A')}\;(\leadsto\mathrm{constraint})

Auxiliary Definitions for the Metatheoretical Results

∣∅∣=∅∣Ξ,αpoly∣=∣Ξ∣,α∣Ξ,α∣=∣Ξ∣,α∣Ξ,s:e1<:e2∣=∣Ξ∣,e1<:e2∣Ξ,x:A∣=∣Ξ∣,x:A∣Ξ,(x:T)α∣=∣Ξ∣,(x:T)α∣Ξ,e1<:e2∣=∣Ξ∣,e1<:e2(1)\begin{aligned} |\varnothing| &= \varnothing& |\Xi,\alpha_{\mathrm{poly}}| &= |\Xi|,\alpha\\ |\Xi,\alpha| &= |\Xi|,\alpha& |\Xi,s:e_1<:e_2| &= |\Xi|,e_1<:e_2 \\ |\Xi,x:A| &= |\Xi|,x:A & |\Xi,(x:T)^{\alpha}| &= |\Xi|,(x:T)^{\alpha} \\ |\Xi,e_1<:e_2| &= |\Xi|,e_1<:e_2 \tag*{(1)} \end{aligned}
\begin{aligned} &\frac{}{\,\vdash\varnothing\leadsto\varnothing\,}\;(\leadsto\mathrm{ctx\mathchar`-empty}) && \frac{\vdash\Xi\leadsto\Gamma}{\vdash\Xi,\alpha_{\mathrm{poly}}\leadsto\Gamma,\alpha}\;(\leadsto\mathrm{ctx\mathchar`-poly})\\[6pt] & \frac{\vdash\Xi\leadsto\Gamma\qquad|\Xi|\vdash e\ \alpha\notin\Xi} {\vdash\Xi,\alpha,e<:\alpha,(x:T)^{\alpha}\leadsto\Gamma}\;(\leadsto\mathrm{ctx\mathchar`-code})\\[6pt] & \frac{\vdash\Xi\leadsto\Gamma\qquad\Xi\vdash A\leadsto A'} {\vdash\Xi,x:A\leadsto\Gamma,x:A'}\;(\leadsto\mathrm{ctx\mathchar`-var}) && \frac{\vdash\Xi\leadsto\Gamma\qquad\Xi\vdash e_1<:e_2\leadsto A} {\vdash\Xi,(s:e_1<:e_2)\leadsto\Gamma,s:A}\;(\leadsto\mathrm{ctx\mathchar`-sub\mathchar`-var}) \tag*{(2)} \end{aligned}

Figure 22 defines the operator ∣Ξ∣|\Xi| and the compilation ⊢Ξ⇝Γ\vdash\Xi\leadsto\Gamma of a compilation context.

The operator ∣Ξ∣|\Xi| takes a compilation context and returns a λRefα\lambda^{\alpha}_{\mathrm{Ref}} typing context that corresponds to it. The operator turns a polymorphic classifier αpoly\alpha_{\mathrm{poly}} into the classifier α\alpha and a named constraint s:e1<:e2s:e_1<:e_2 into the constraint e1<:e2e_1<:e_2. It keeps every other entry.

The compilation ⊢Ξ⇝Γ\vdash\Xi\leadsto\Gamma turns a compilation context into a λRefCtx\lambda^{\mathrm{Ctx}}_{\mathrm{Ref}} metalanguage typing context. The judgment turns a polymorphic classifier into a context variable (\leadsto\mathrm{ctx\mathchar`-poly}), gives a meta variable its compiled type (\leadsto\mathrm{ctx\mathchar`-var}), and gives the evidence variable ss the substitution type that its subtype constraint compiles to (\leadsto\mathrm{ctx\mathchar`-sub\mathchar`-var}). A code abstraction adds three entries together, namely its classifier, the subtype constraint to its parent ee, and its code variable. All three are dropped (\leadsto\mathrm{ctx\mathchar`-code}).

The compilation lemmas of § 2.1 require compilation contexts to be well formed.

Definition 1 (Well-formed compilation context). A compilation context Ξ\Xi is well formed, written ⊢Ξ\vdash\Xi, if the following hold.

  1. Every entry mentions only well-scoped classifiers and types:

∀x:A∈Ξ.∣Ξ∣⊢A∀(s:e1<:e2)∈Ξ.∣Ξ∣⊢e1 and ∣Ξ∣⊢e2∀(x:T)α∈Ξ.α∈Ξ∀(e1<:e2)∈Ξ.∣Ξ∣⊢e1 and ∃α∈Ξ. e2=α\begin{aligned} &\forall x:A \in\Xi.\quad|\Xi| \vdash A\\ &\forall(s:e_1 <: e_2) \in\Xi.\quad|\Xi| \vdash e_1\ \text{and}\ |\Xi| \vdash e_2\\ &\forall(x:T)^{\alpha} \in\Xi.\quad\alpha\in\Xi\\ &\forall(e_1 <: e_2) \in\Xi.\quad|\Xi| \vdash e_1\ \text{and}\ \exists\alpha\in\Xi.\ e_2 = \alpha \end{aligned}
  1. Every classifier has at most one parent:

if e1<:α∈Ξe_1 <: \alpha\in\Xi and e2<:α∈Ξe_2 <: \alpha\in\Xi then e1=e2e_1 = e_2.

  1. Every classifier points to at most one code variable:

if (x:T)α∈Ξ(x:T)^{\alpha} \in\Xi and (y:S)α∈Ξ(y:S)^{\alpha} \in\Xi then x=yx = y and T=ST = S.

Reification turns a code value of λRefα\lambda_{\mathrm{Ref}}^{\alpha} into STLC.

simulation contexts Ψ::=∅∣Ψ,α∣Ψ,(x:T)∣Ψ,e1<:e2\Psi::= \varnothing\mid\Psi,\alpha\mid\Psi,(x:T) \mid\Psi,e_1 <: e_2

dom⁡(Ψ)={α∣α∈Ψ}∪{x∣(x:T)∈Ψ}\operatorname{dom}(\Psi) = \{\alpha\mid\alpha\in\Psi\} \cup\{x \mid(x:T) \in\Psi\}

dom⁡(Γ)=dom⁡(Ψ)Σ;Ψ;e⊢M∘:T∼N∘Σ;Ψ⊢μ∼μ′Σ;Ψ;e⊢⟨Γ;M∘;μ⟩:T∼N∘∣μ′(∼code-config)\frac{ \operatorname{dom}(\Gamma)=\operatorname{dom}(\Psi) \qquad \Sigma;\Psi;e \vdash M^{\circ}:T \sim N^{\circ} \qquad \Sigma;\Psi\vdash\mu\sim\mu' }{ \Sigma;\Psi;e \vdash\langle\Gamma;M^{\circ};\mu\rangle:T \sim N^{\circ}\mid\mu' } \quad(\sim\mathsf{code\text{-}config})
dom⁡(Γ)=dom⁡(Ψ)Σ;Ψ⊢M:A∼NΣ;Ψ⊢μ∼μ′Σ;Ψ⊢⟨Γ;M;μ⟩:A∼N∣μ′(∼config)\frac{ \operatorname{dom}(\Gamma)=\operatorname{dom}(\Psi) \qquad \Sigma;\Psi\vdash M:A \sim N \qquad \Sigma;\Psi\vdash\mu\sim\mu' }{ \Sigma;\Psi\vdash\langle\Gamma;M;\mu\rangle:A \sim N\mid\mu' } \quad(\sim\mathsf{config})
dom⁡(Σ)=dom⁡(μ)=dom⁡(μ′)∀(a:A)∈Σ. Σ;Ξ⊢μ(a):A∼μ′(a)Σ;Ξ⊢μ∼μ′(∼heap)\frac{ \operatorname{dom}(\Sigma)=\operatorname{dom}(\mu)=\operatorname{dom}(\mu') \qquad \forall(a:A)\in\Sigma.\ \Sigma;\Xi\vdash\mu(a):A\sim\mu'(a) }{ \Sigma;\Xi\vdash\mu\sim\mu' } \quad(\sim\mathsf{heap})
k:ιΣ;Ξ;e⊢k:ι∼k(∼code-const)\frac{ k:\iota }{ \Sigma;\Xi;e\vdash k:\iota\sim k } \quad(\sim\mathsf{code\text{-}const})
(x:T)α∈ΞΣ;Ξ;α⊢x:T∼x(∼code-var)\frac{ (x:T)^{\alpha}\in\Xi }{ \Sigma;\Xi;\alpha\vdash x:T\sim x } \quad(\sim\mathsf{code\text{-}var})
Σ;Ξ,α,e<:α,(x:S)α⊢N∘:T∼N∘′α∉ΞΣ;Ξ;e⊢λ(x:S)α.N∘:S→T∼λ(x:S).N∘′(∼code-abs)\frac{ \Sigma;\Xi,\alpha,e<:\alpha,(x:S)^{\alpha}\vdash N^{\circ}:T\sim N^{\circ'} \qquad \alpha\notin\Xi }{ \Sigma;\Xi;e\vdash\lambda(x:S)^{\alpha}.N^{\circ}:S\to T\sim\lambda(x:S).N^{\circ'} } \quad(\sim\mathsf{code\text{-}abs})
α∈Ξ(x:S)α∈ΞΣ;Ξ;α⊢N∘:T∼N∘′Σ;Ξ;e⊢λ(x:S)α.N∘:S→T∼λ(x:S).N∘′(∼code-abs-val)\frac{ \alpha\in\Xi \qquad (x:S)^{\alpha}\in\Xi \qquad \Sigma;\Xi;\alpha\vdash N^{\circ}:T\sim N^{\circ'} }{ \Sigma;\Xi;e\vdash\lambda(x:S)^{\alpha}.N^{\circ}:S\to T\sim\lambda(x:S).N^{\circ'} } \quad(\sim\mathsf{code\text{-}abs\text{-}val})
Σ;Ξ;e⊢L∘:S→T∼L∘′Σ;Ξ;e⊢M∘:S∼M∘′Σ;Ξ;e⊢L∘ M∘:T∼L∘′ M∘′(∼code-app)\frac{ \Sigma;\Xi;e\vdash L^{\circ}:S\to T\sim L^{\circ'} \qquad \Sigma;\Xi;e\vdash M^{\circ}:S\sim M^{\circ'} }{ \Sigma;\Xi;e\vdash L^{\circ}\ M^{\circ}:T\sim L^{\circ'}\ M^{\circ'} } \quad(\sim\mathsf{code\text{-}app})
Σ;Ξ⊢M:“T”e∼M′Ξ⊢e⇝Γ∘Σ;Ξ;e⊢(∼M):T∼(∼M′id⁡Γ∘)(∼splice)\frac{ \Sigma;\Xi\vdash M:\text{``}T\text{''}_{e}\sim M' \qquad \Xi\vdash e\rightsquigarrow\Gamma^{\circ} }{ \Sigma;\Xi;e\vdash(\sim M):T\sim(\sim M'\operatorname{id}_{\Gamma^{\circ}}) } \quad(\sim\mathsf{splice})
Σ;Ξ;e1⊢M∘:T∼M∘′Ξ⊢e1⇝Γ∘Ξ⊢e1<:e2⇝MΣ;Ξ;e2⊢M∘:T∼(∼“Γ∘⊢M∘′” M)(∼subenv)\frac{ \Sigma;\Xi;e_1\vdash M^{\circ}:T\sim M^{\circ'} \qquad \Xi\vdash e_1\rightsquigarrow\Gamma^{\circ} \qquad \Xi\vdash e_1<:e_2\rightsquigarrow M }{ \Sigma;\Xi;e_2\vdash M^{\circ}:T\sim(\sim\text{``}\Gamma^{\circ}\vdash M^{\circ'}\text{''}\ M) } \quad(\sim\mathsf{subenv})
Σ;Ξ;e1⊢V∘:T∼V∘′Ξ⊢“T”e1<:“T”e2▹“Γ∘⊢V∘′”⇒“Δ∘⊢W∘′”Σ;Ξ;e2⊢V∘:T∼W∘′(∼subenv-val)\frac{ \Sigma;\Xi;e_1\vdash V^{\circ}:T\sim V^{\circ'} \qquad \Xi\vdash\text{``}T\text{''}_{e_1}<:\text{``}T\text{''}_{e_2}\triangleright\text{``}\Gamma^{\circ}\vdash V^{\circ'}\text{''}\Rightarrow\text{``}\Delta^{\circ}\vdash W^{\circ'}\text{''} }{ \Sigma;\Xi;e_2\vdash V^{\circ}:T\sim W^{\circ'} } \quad(\sim\mathsf{subenv\text{-}val})

Fig. 24. Simulation of configurations, heaps, and code terms.

The simulation relation between λRefα\lambda_{\mathrm{Ref}}^{\alpha} and λRefCtx\lambda_{\mathrm{Ref}}^{\mathrm{Ctx}} is defined in Figures 24, 25, and 26. The highlighted rules extend the compilation rules, so that the simulation relation is closed under reduction.

\begin{aligned} &\Sigma;\Xi\vdash M : A \sim N \\[4pt] &\frac{k:\iota}{\Sigma;\Xi\vdash k:\iota\sim k}\;(\sim\mathrm{const}) \qquad \frac{x:A \in\Xi}{\Sigma;\Xi\vdash x:A \sim x}\;(\sim\mathrm{var}) \\[8pt] &\frac{\Xi\vdash A \leadsto A'\qquad\Sigma;\Xi,x:A \vdash N:B \sim N'}{\Sigma;\Xi\vdash\lambda(x:A).N:A\to B \sim\lambda(x:A').N'}\;(\sim\mathrm{abs}) \\[8pt] &\frac{\Sigma;\Xi\vdash L:A\to B \sim L'\qquad\Sigma;\Xi\vdash M:A \sim M'}{\Sigma;\Xi\vdash LM:B \sim L'M'}\;(\sim\mathrm{app}) \qquad \frac{a:A \in\Sigma}{\Sigma;\Xi\vdash a:\operatorname{Ref} A \sim a}\;(\sim\mathrm{addr}) \\[8pt] &\frac{\Sigma;\Xi\vdash M:A \sim M'}{\Sigma;\Xi\vdash\operatorname{ref} M:\operatorname{Ref} A \sim\operatorname{ref} M'}\;(\sim\mathrm{ref}) \\[8pt] &\frac{\Sigma;\Xi\vdash L:\operatorname{Ref} A \sim L'\qquad\Sigma;\Xi\vdash M:A \sim M'}{\Sigma;\Xi\vdash L:=M:\operatorname{Unit} \sim L':=M'}\;(\sim\mathrm{assign}) \\[8pt] &\frac{\Sigma;\Xi\vdash M:\operatorname{Ref} A \sim M'}{\Sigma;\Xi\vdash{!M}:A \sim{!M'}}\;(\sim\mathrm{deref}) \\[8pt] &\frac{\Sigma;\Xi;e \vdash M^{\circ}:T \sim M^{\circ\prime}\qquad\Xi\vdash e \leadsto\Gamma^{\circ}}{\Sigma;\Xi\vdash\left\llbracket M^{\circ}\right\rrbracket_e:\left\llbracket T\right\rrbracket_e \sim\left\llbracket\Gamma^{\circ}\vdash M^{\circ\prime}\right\rrbracket}\;(\sim\mathrm{quote}) \\[8pt] &\frac{\Sigma;\Xi,\alpha^{\mathrm{poly}}\vdash N:A \sim N'\qquad\alpha\notin\Xi}{\Sigma;\Xi\vdash\Lambda\alpha.N:\forall\alpha.A \sim\Lambda\alpha.N'}\;(\sim\mathrm{ec\text{-}abs}) \\[8pt] &\frac{\Sigma;\Xi\vdash M:\forall\alpha.A \sim M'\qquad\Xi\vdash e \leadsto\Gamma}{\Sigma;\Xi\vdash M[e]:A[\alpha:=e] \sim M'[\Gamma]}\;(\sim\mathrm{ec\text{-}app}) \\[8pt] &\frac{\Xi\vdash e_1 \leadsto\Gamma_1\qquad\Xi\vdash e_2 \leadsto\Gamma_2\qquad\Sigma;\Xi,(s:e_1<:e_2)\vdash N:A \sim N'\qquad s\notin\Xi}{\Sigma;\Xi\vdash(e_1<:e_2\Rightarrow N):(e_1<:e_2\Rightarrow A) \sim\lambda(s:\Gamma_1\Rightarrow\Gamma_2).N'}\;(\sim\mathrm{sub\text{-}intro}) \\[8pt] &\frac{\Sigma;\Xi\vdash M:(e_1<:e_2\Rightarrow A) \sim M'\qquad\Xi\vdash e_1<:e_2 \leadsto N}{\Sigma;\Xi\vdash M^{\bullet}:A \sim M'N}\;(\sim\mathrm{sub\text{-}elim}) \\[8pt] &\frac{\Sigma;\Xi\vdash M:A \sim M'\qquad\Xi\vdash A<:B \leadsto L}{\Sigma;\Xi\vdash M:B \sim LM'}\;(\sim\mathrm{subsumption}) \\[8pt] &\frac{\Sigma;\Xi\vdash V:A \sim V'\qquad\Xi\vdash A<:B \triangleright V'\Rightarrow W'}{\Sigma;\Xi\vdash V:B \sim W'}\;(\sim\mathrm{subsumption\text{-}val}) \end{aligned}

References

  1. [1]Abadi, M., Cardelli, L., Curien, P.L., Levy, J.J.: Explicit substitutions. Journal of Functional Programming 1(4), 375–416 (1991)
  2. [2]Breazu-Tannen, V., Coquand, T., Gunter, C.A., Scedrov, A.: Inheritance as implicit coercion. Information and Computation 93(1), 172 – 221 (1991). http://dx.doi.org/10.1016/0890-5401(91)90055-7, http://www.sciencedirect.com/science/article/pii/0890540191000557, selections from 1989 {IEEE} Symposium on Logic in Computer Science
  3. [3]Calcagno, C., Moggi, E., Sheard, T.: Closed types for a safe imperative metaml. J. Funct. Program. 13(3), 545–571 (May 2003). https://doi.org/10.1017/S0956796802004598, https://doi.org/10.1017/S0956796802004598
  4. [4]Calcagno, C., Moggi, E., Taha, W.: Closed types as a simple approach to safe imperative multi-stage programming. In: International Colloquium on Automata, Languages, and Programming. pp. 25–36. Springer (2000)
  5. [5]Calcagno, C., Taha, W., Huang, L., Leroy, X.: Implementing multi-stage languages using asts, gensym, and reflection. In: 2nd International Conference on Generative Programming and Component Engineering (GPCE’03) (2003)
  6. [6]Davies, R.: A temporal-logic approach to binding-time analysis. In: Proceedings 11th Annual IEEE Symposium on Logic in Computer Science. pp. 184–195. IEEE (1996)
  7. [7]Davies, R.: A temporal logic approach to binding-time analysis. J. ACM 64(1) (Mar 2017). https://doi.org/10.1145/3011069, https://doi.org/10.1145/3011069
  8. [8]Davies, R., Pfenning, F.: A modal analysis of staged computation. In: POPL ’96: Proceedings of the 23rd ACM SIGPLAN-SIGACT symposium on Principles of programming languages. pp. 258–270. ACM Press, New York, NY, USA (1996)
  9. [9]Davies, R., Pfenning, F.: A modal analysis of staged computation. J. ACM 48(3), 555–604 (2001)
  10. [10]Herman, D., Tomb, A., Flanagan, C.: Space-efficient gradual typing. Higher-Order and Symbolic Computation 23(2), 167–189 (2010). https://doi.org/10.1007/s10990-011-9066-z
  11. [11]Isoda, K., Yokoyama, A., Kameyama, Y.: Type-safe code generation with algebraic effects and handlers. In: Proceedings of the 23rd ACM SIGPLAN International Conference on Generative Programming: Concepts and Experiences. pp. 53–65 (2024)DOI
  12. [12]Jang, J., Gélineau, S., Monnier, S., Pientka, B.: Moebius: metaprogramming using contextual types: the stage where system f can pattern match on itself. Proc. ACM Program. Lang. 6(POPL) (Jan 2022). https://doi.org/10.1145/3498700, https://doi.org/10.1145/3498700
  13. [13]Kim, I.S., Yi, K., Calcagno, C.: A polymorphic modal type system for lisp-like multi-staged languages. In: POPL ’06: Conference record of the 33rd ACM SIGPLAN-SIGACT symposium on Principles of programming languages. pp. 257–268. ACM Press, New York, NY, USA (2006)
  14. [14]Kiselyov, O.: The design and implementation of her metaocaml: System description. In: International Symposium on Functional and Logic Programming. pp. 86–102. Springer (2014)
  15. [15]Kiselyov, O., Kameyama, Y., Sudo, Y.: Refined environment classifiers: Type- and scope-safe code generation with mutable cells. In: Igarashi, A. (ed.) Programming Languages and Systems - 14th Asian Symposium, APLAS 2016, Proceedings. pp. 271–291. Lecture Notes in Computer Science (including subseries Lecture Notes in Artificial Intelligence and Lecture Notes in Bioinformatics), Springer Verlag, Germany (2016). https://doi.org/10.1007/978-3-319-47958-3_15, funding Information: We thank anonymous reviewers for many helpful comments. This work was partially supported by JSPS KAKENHI Grant Numbers 15K12007, 16K12409, 15H02681. Publisher Copyright: © Springer International Publishing AG 2016; 14th Asian Symposium on Programming Languages and Systems, APLAS 2016 ; Conference date: 21-11-2016 Through 23-11-2016
  16. [16]Lee, M., Xie, N., Kiselyov, O., Yallop, J.: Handling scope checks: A comparative framework for dynamic scope extrusion checks. Proceedings of the ACM on Programming Languages 10(POPL), 1123–1152 (2026)
  17. [17]Moggi, E., Taha, W., Benaissa, Z.E.A., Sheard, T.: An idealized metaml: Simpler, and more expressive. In: European Symposium on Programming. pp. 193–207. Springer (1999)DOI
  18. [18]Murase, Y., Igarashi, A.: Refinedˆ 2 environment classifiers. arXiv preprint arXiv:2608.07888 (2026)arxiv.org/abs/2608.07888
  19. [19]Murase, Y., Nishiwaki, Y., Igarashi, A.: Contextual modal type theory with polymorphic contexts. In: European Symposium on Programming. pp. 281–308. Springer Nature Switzerland Cham (2023)arxiv.org/abs/1801.09225
  20. [20]Nanevski, A., Pfenning, F., Pientka, B.: Contextual modal type theory. ACM Trans. Comput. Logic 9(3) (Jun 2008). https://doi.org/10.1145/1352582.1352591, https://doi.org/10.1145/1352582.1352591
  21. [21]Pfenning, F., Davies, R.: A judgmental reconstruction of modal logic. Mathematical. Structures in Comp. Sci. 11(4), 511–540 (Aug 2001). https://doi.org/10.1017/S0960129501003322, http://dx.doi.org/10.1017/S0960129501003322
  22. [22]Pierce, B.C.: Types and Programming Languages. MIT Press (2002)
  23. [23]Rhiger, M.: Staged computation with staged lexical scope. In: European Symposium on Programming. pp. 559–578. Springer (2012)DOI
  24. [24]Taha, W., Calcagno, C., Leroy, X., Pizzi, E., Pasalic, E., Eckhardt, J.L., Kajabachev, R., Kiselyov, O.: MetaOCaml: A compiled, type-safe multi-stage programming language. Available online from http://www.metaocaml.org/ (2004)
  25. [25]Taha, W., Nielsen, M.F.: Environment classifiers. In: POPL ’03: Proceedings of the 30th ACM SIGPLAN-SIGACT symposium on Principles of programming languages. pp. 26–37. ACM Press, New York, NY, USA (2003)
  26. [26]Taha, W., Sheard, T.: Multi-stage programming with explicit annotations. In: PEPM ’97: Proceedings of the 1997 ACM SIGPLAN symposium on Partial evaluation and semantics-based program manipulation. pp. 203–217. ACM Press, New York, NY, USA (1997)
  27. [27]Taha, W., Sheard, T.: MetaML and multi-stage programming with explicit annotations. Theoretical computer science 248(1-2), 211–242 (2000)DOI
  28. [28]Tsukada, T., Igarashi, A.: A logical foundation for environment classifiers. Logical Methods in Computer Science 6 (2010)
  29. [29]Yin, H., Murawski, A.S., Ong, C.H.L.: Contextual metaml: Syntax and full abstraction. arXiv preprint arXiv:2602.03033 (2026)arxiv.org/abs/2602.03033

Paper details

vlnc:W_h8wtdvwd
Version
v1 · latest
Published
Added
License
CC BY 4.0
Usage

views · 0

downloads · 0

A view counts once per browser per day, a download each time the PDF is requested.

  • Computer Science

Not recorded for this paper

View statement
Looking for related papers…
Contents