Introduction
This paper furthers our understanding of how the choice of language semantics impacts proofs of compiler correctness and delivers two novel denotational proofs of correctness for closure conversion. But before explaining our results, let us review the state of the art. Thanks to major compiler verification projects such as CompCert [41 ] and CakeML [38 , 58 ] we have a fairly good understanding of the use of operational semantics in proofs of compiler correctness. CompCert employs small-step semantics and relates them using simulations [42 ] . CakeML uses interpreters and reasons about them using induction on evaluation and value relations. Both proofs were large undertakings. The proof of CompCert is 100,000 lines of Coq and took 6 person-years, and the proof of the CakeML compiler backend is about 100,000 lines of HOL4 [84 ] , by six people over two years [54 ] . In contrast, the literature is less complete when it comes to the use of denotational semantics in compiler correctness proofs, but there is evidence that denotational semantics can yield simpler proofs due to equational and compositional reasoning [50 , 83 , 93 ] . This paper fills a fundamental gap in the literature, establishing the first proofs of correctness with respect to denotational semantics for a compiler from a Turing-complete, higher-order language to a first-order language. Why is there such a fundamental gap in the literature? We suspect two reasons: (1) the denotational semantics for Turing-complete languages are more complex than those for strongly-normalizing languages, and (2) compilers for higher-order languages that target a first-order language must go through closure conversion, which involves a change of representation, and that is more challenging to reason about in denotational semantics. Recall that closure conversion replaces each function with a pair of code and the values of its free variables, and replaces each function call with code that takes the pair apart [13 , 81 ] and calls its first component with the second component as an extra argument.
To date, every proof of compiler correctness with respect to a denotational semantics stops short of closure conversion for a Turing-complete language, in several different ways. Nielsen [ [57 ] proved that defunctionalization, a close cousin of closure conversion, preserves the denotations of programs. However, compiler correctness requires that the denotation of programs is also reflected. Otherwise a compiler could translate a diverging source program into a converging target. Nielsen [ [57 ] says “Eventually, we want to prove full correctness, i.e., semantic equivalence of the two programs.” Banerjee et al. [ [5 ] proved both directions, but gave an operational semantics to the source language. They report that they “know of no other proofs of correctness (that is, both directions) for ordinary or CFA-based defunctionalization in the presence of recursion.” Chlipala [ [15 ] proved correctness of a compiler from the simply typed λ \lambda λ -calculus (STLC) to assembly, including closure conversion, but the STLC is not Turing-complete, which let him interpret each type as a Coq type and each function as a total Coq function. Other denotational proofs of compiler correctness avoid closure conversion, because either their source language is first-order [14 , 68 , 91 ] or their target language is still higher-order [8 , 17 , 73 ] . When Chlipala [ [16 ] proved correct a compiler for a language with general recursion, he switched to operational semantics, explaining that the denotational approach “cannot be applied directly to Turing-complete programming languages.”
A natural setting to study the compilation of higher-order languages is Plotkin’s FPC language [66 ] , a call-by-value λ \lambda λ -calculus with sums, products, and iso-recursive types. The recursive types of FPC enable the fixed-point combinator, so FPC has general recursion and is Turing-complete. That being said, FPC lacks many of the features of modern programming languages, such as mutable state, exceptions, and multi-threading, to name a few. The claims in this paper are specific to the compilation of FPC. We make no claims about the impact of semantic styles on proofs of correctness for compilers of other programming languages. Our proofs of correctness, like most of those cited above, are for whole programs. They relate the meaning of a closed source program to the meaning of its compiled code. Compositional compiler correctness [61 , 82 ] (Ramanathan et al. 2015), which addresses linking with separately compiled code or code written in another language, is just as important. Cheng et al. [ [14 ] achieve compositional compiler correctness with a denotational semantics of open modules and a semantic linking operator, for imperative first-order languages, including the front end of CompCert. Extending our denotational proofs to open terms and linking, through closure conversion, would be a good next step.
The compiler for FPC that we study begins with a source optimization pass based on the cp0 pass of Chez Scheme [22 , 86 ] , includes three passes for closure conversion, and four back-end passes that target a typed low-level intermediate language that is on the way to x86 assembly. For this compiler we seek to answer three research questions:
RQ1 Can we prove correctness of this compiler with respect to a denotational semantics?
RQ2 How do the sizes of mechanized correctness proofs for this compiler compare across different operational and denotational semantic styles, pass by pass and overall?
RQ3 Which properties of a pass determine the size of its correctness proof in each style?
We answer RQ1 affirmatively, in two denotational models (Theorems 4.1 and 4.2 ). Our first model is a graph model, in which a program denotes a set of inductively defined graph elements [23 , 67 , 76 ] . Each element is a finite description of the program’s behavior, and in particular, the semantics of a function is given by a set of elements that represent input–output entries. To prove semantic reflection for closure conversion we introduce a new kind of relation between denotations, the approximant logical relation . Our key insight is to relate a finite set of target graph elements (a finite approximation) to a possibly infinite source denotation, and to define the relation by recursion on the depth of the elements in the finite set (Lemma 4.6 in § 4.2 ). Our second model uses Scott domains, in which a program denotes an element of a universal domain of tagged values [78 ] . There we define the relation by recursion on the stages of the bilimit that constructs the domain (Lemma G.5 in § 4.3 ), as Reynolds [ [71 ] did to relate the direct and continuation semantics of an untyped λ \lambda λ -calculus. Other styles of denotational semantics remain to be evaluated for compiler correctness and closure conversion, among them synthetic domain theory [10 , 34 , 48 , 72 ] , guarded interaction trees [29 ] , categorical semantics [28 ] , the partiality monad [Danielsson 2012], and game semantics [35 ] .
On top of our denotational results for closure conversion, we develop two correctness proofs for the eight-pass compiler from FPC to a typed low-level intermediate language. For seven of the eight passes, correctness is expressed as equality of denotations (Lemma 4.3 ) and the proofs are by induction on terms and use equational reasoning. The exception is closure conversion, in particular the split closure pass that separates the code of each closure from its environment, whose semantic reflection proof requires the logical relations we discussed above.
To answer RQ2 and RQ3, we compare the two denotational proofs of correctness for our FPC compiler with three more proofs of correctness, each with respect to an operational semantics (Propositions F.1 –F.3 ) and proved using the standard technique for its style: clocked interpreters with relations on values, abstract machines with simulations, and reduction semantics with simulations. We mechanize all five compiler correctness proofs in the Agda proof assistant [2 ] . Earlier work that compares proofs of compiler correctness focuses on a single phase of a compiler [36 , 37 , 58 ] ; ours is the first on a whole compiler for a higher-order language. Each of the five correctness results is with respect to a different semantics, so one might wonder if the semantics and therefore the results are equivalent. To check that our mechanized semantics agree, we prove the graph model adequate with respect to the interpreters, the abstract machines, and the reduction semantics, and the Scott-domain semantics adequate with respect to the interpreters (Proposition 5.1), so the five results are equivalent.
No one semantic style gives the smallest proof for every pass (RQ2), but for every pass, the smallest proof is either the graph-model proof or the interpreter proof. Not counting language-independent infrastructure, the interpreters give the smallest proof, at 35,860 lexical tokens of Agda, and the graph model gives the second smallest, at 40,905. The sizes split by the kind of pass (RQ3). The denotational proofs tend to be the smallest for passes that preserve the meaning of a program exactly but change how it runs, such as the source optimizer simplify . The operational proofs are several times smaller for split closure , which changes the representation of closures (§ 6 ).
§ 2 presents the compiler and § 3 gives background on the graph model. § 4 presents the correctness of each pass wrt. the graph model and the proof for split closure wrt.
Scott domains. § 5 gives the adequacy theorems for the denotational semantics, § 6 compares the five proofs, § 7 discusses related work, and § 8 concludes. Appendix § 15 .§ 5 defines the Scott domains. For background on domain theory, we refer the reader to Scott [ [78 ] , Winskel [ [88 ] , and Abramsky and Jung [ [1 ] . The other appendices define the semantics of every language and compiler pass. Our Agda development is in the supplementary material, which can be accessed using the following link:
https://github.com/cty12/denot-compiler-supplementary
The Compiler and Its Languages
Our compiler translates FPC, the call-by-value λ \lambda λ -calculus with sums, products, and iso-recursive types of Plotkin [ [66 ] , extended with integers, to Instrμ _\mu μ , a typed low-level intermediate language, in eight passes through six intermediate languages. Figure 1 shows the sequence of passes and languages. Figure 2 presents the grammars of all the languages and Appendix § 11 gives their typing rules.1 Appendix § 12 lists the full definition of each compiler pass.
Figure 1. The passes of the compiler and the languages between them.
Figure 2. The grammars of the compiler’s languages.
Throughout Figure 2 , c c c ranges over constants, p p p over the primitives sub and eq, and f k f_k f k over the names of a program’s global definitions, where f k f_k f k refers to the definition d k d_k d k . In the languages prior to Imp, x x x , y y y , and z z z range over variables. From Imp on, they range over fixed locations in a function’s stack frame. We write binders as one or more names followed by a period, and the scope of the binders is the term after the period. Every language has a graph-model semantics (§ 3 ), given in full in Appendix § 11 .
The source language. FPC has integers, n n n -ary tuples, binary sums, the primitives sub and eq, and iso-recursive types. The primitive eq returns inl ⟨ ⟩ \operatorname{inl}\langle\rangle inl ⟨ ⟩ for true and inr ⟨ ⟩ \operatorname{inr}\langle\rangle inr ⟨ ⟩ for false, so case serves as the conditional. A recursive type μ X . A \mu X.A μ X . A is isomorphic to its unfolding A [ μ X . A / X ] A[\mu X.A/X] A [ μ X . A / X ] , and the two coercions of this isomorphism are explicit. The fold operation turns a term of the unfolding into a term of the recursive type, and unfold turns it back:
Γ ⊢ M : A [ μ X . A / X ] Γ ⊢ fold M : μ X . A Γ ⊢ M : μ X . A Γ ⊢ unfold M : A [ μ X . A / X ] \begin{aligned}
\Gamma\vdash M &: A[\mu X.A/X] \\
\Gamma\vdash\operatorname{fold} M &: \mu X.A
\end{aligned}
\qquad
\begin{aligned}
\Gamma\vdash M &: \mu X.A \\
\Gamma\vdash\operatorname{unfold} M &: A[\mu X.A/X]
\end{aligned} Γ ⊢ M Γ ⊢ fold M : A [ μ X . A / X ] : μ X . A Γ ⊢ M Γ ⊢ unfold M : μ X . A : A [ μ X . A / X ]
Neither coercion does anything at run time. Recursive types make self-application typable, so a program can define the call-by-value fixed-point combinator to write recursive functions.
The optimizer. The first pass, simplify (§C.1), translates from FPC to FPC. It is a simplified version of the optimizer cp0 of Chez Scheme Waddell and Dybvig [ [86 ] . It propagates known values, folds constants, reduces a case, projection, or unfold of a known value, and inlines small known functions.
Closure conversion. The next three passes perform closure conversion. The enclose pass (§C.2) translates from FPC to EnvClos. It turns each λ \lambda λ -abstraction into a closure clos ( z , y . N ) M ⃗ \operatorname{clos}(z,y.N)\ \vec{M} clos ( z , y . N ) M , which captures the values M ⃗ \vec{M} M of the variables that the code N N N uses. The closure binds two variables in its code, the environment z z z and the parameter y y y , and the code, which has no other free variables, reads the i i i th captured value as get i z \operatorname{get}_i z get i z .2 The split closure pass (§C.3) then separates a closure into its code and its environment, translating from EnvClos to PairClos. In this target IR, code ( z , y . N ) (z,y.N) ( z , y . N ) is closed code of type code ( Δ ; A → C ) \operatorname{code}(\Delta; A \to C) code ( Δ ; A → C ) , which expects an environment of type tup ( Δ ) \operatorname{tup}(\Delta) tup ( Δ ) and an argument of type A A A . The form pair L M \operatorname{pair}\ L\ M pair L M builds a closure, which is a pair of code L L L and an environment M M M of the same Δ \Delta Δ . Its type, A → C A \to C A → C , hides Δ \Delta Δ , as in the existential type ∃ Δ . code ( Δ ; A → C ) × tup ( Δ ) \exists\Delta.\operatorname{code}(\Delta; A \to C) \times\operatorname{tup}(\Delta) ∃Δ. code ( Δ ; A → C ) × tup ( Δ ) of typed closure conversion Minamide et al. [1996], so closures with different environments have the same type. Thus pair packs the existential, but its dynamic semantics is the usual for a pair. The form call L M \operatorname{call}\ L\ M call L M calls the code of the closure L L L with its environment and the argument M M M .
z : tup ( Δ ) , y : A ⊢ N : C Γ ⊢ code ( z , y . N ) : code ( Δ ; A → C ) Γ ⊢ L : code ( Δ ; A → C ) Γ ⊢ M : tup ( Δ ) Γ ⊢ pair L M : A → C Γ ⊢ L : A → C Γ ⊢ M : A Γ ⊢ call L M : C \begin{aligned}
z &: \operatorname{tup}(\Delta),\ y : A \vdash N : C \\
\Gamma&\vdash\operatorname{code}(z,y.N) : \operatorname{code}(\Delta;A \to C)
\end{aligned}
\qquad
\begin{aligned}
\Gamma\vdash L &: \operatorname{code}(\Delta;A \to C) \\
\Gamma\vdash M &: \operatorname{tup}(\Delta) \\
\Gamma&\vdash\operatorname{pair}\ L\ M : A \to C
\end{aligned}
\qquad
\begin{aligned}
\Gamma\vdash L &: A \to C \\
\Gamma\vdash M &: A \\
\Gamma&\vdash\operatorname{call}\ L\ M : C
\end{aligned} z Γ : tup ( Δ ) , y : A ⊢ N : C ⊢ code ( z , y . N ) : code ( Δ ; A → C ) Γ ⊢ L Γ ⊢ M Γ : code ( Δ ; A → C ) : tup ( Δ ) ⊢ pair L M : A → C Γ ⊢ L Γ ⊢ M Γ : A → C : A ⊢ call L M : C
Table 1. Lexical tokens of Agda in the five correctness proofs. The smallest number in each row is in bold.
Finally, the hoist pass (§C.4) (similar to the hoisting phase of Morrisett et al. [1999]) translates from PairClos to GlobalFun. It turns the code of every function into a global definition d k d_k d k , and each occurrence of the code into a reference f k f_k f k to it, so a GlobalFun program is a list of function definitions and a main term.
The back end. The pass remove complex operands (RCO) , (§C.5) translates from GlobalFun to MNF, a monadic normal form [32 ] . It binds each operand that is not atomic to a variable with let, so the operands of every operation in MNF are atomic. The atomic terms are variables, constants, and function names. The lift locals pass (§C.6) translates from MNF to Imp. It gives each variable its own location in the function’s stack frame, called a slot . A function body declares its frame Φ \Phi Φ , which lists the types of its slots. The type system tracks which slots are initialized. The explicate control pass (§C.7) translates from Imp to CFG. It turns the nested cases into a control-flow graph. A CFG program has a function body for each global definition and for the main program. The name of the tail non-terminal is short for tail position. A function body consists of numbered blocks and a starting block, each of which is a tail.
Finally, select instructions (§C.8) translates from CFG to Instrμ _\mu μ , a typed low-level language in the style of TAL [51 ] , with an x86 flavor. Instrμ _\mu μ has three registers (rax, rdi, and rsi), a stack frame of slots, and an abstract heap. Programs have the same structure as in CFG. Instruction operands a a a are slot numbers, constants, names of function definitions, or registers. Instructions write to a destination d s t dst d s t , a register or a slot.
The pass translates each simple expression of CFG into instructions that write its value to a destination, using the registers to stage arithmetic and calls. A closure (pair), a tuple, or an injection becomes the instruction alloc con, dst, which allocates on the heap the result of the constructor c o n con co n . A call callc x a of the closure in slot x x x on the argument a a a becomes the instruction sequence fst x, %rax; snd x, %rdi; movq a, %rsi; callq %rax. The first two instructions load the code and the environment of the closure, the third puts the argument in place, and the call leaves its result in rax. The type system allows only one closure to be open at a time, so that a call never combines the code of one closure with the environment of another. The coercions fold and unfold become annotations on operands, fold a and unfold a, that do nothing.
Instrμ _\mu μ abstracts from a real instruction set. Its heap is abstract, with no addresses or memory layout, and control flow within a function is acyclic. Neither abstraction concerns closure conversion, because in Instrμ _\mu μ , as in machine code, a closure is a heap object that pairs a function’s code pointer with a tuple of captured values, and the caller passes the tuple, along with the argument, in argument-passing registers before it calls the code pointer.
Graph Models
A denotational semantics maps each program to a mathematical object that describes its behavior. In a graph model, that object is a set of finite pieces of evidence about the behavior, which we call elements . Our model is a variant of the model of Engeler [ [23 ] , which we extend with constants, pairs, tuples, and disjoint sums. Figure 3 defines elements, several semantic operators on sets of elements, and the semantics of FPC. We first explain how the model represents functions, then introduce the elements, and then the operators. We review how the model handles recursion and divergence, and why we require sets of elements to be consistent. We end this section with the properties of the semantics that our proofs rely on, including a β \beta β -law.
!Graph elements, finite lists of elements, denotations, indices, consistency, operators on denotations, and semantics of FPC. Graph elements: u , v , w : : = c ∣ V ↦ w ∣ ν ∣ ⟨ ∣ u , v ∣ ⟩ ∣ tup i ( v ) ∣ left ( v ) ∣ right ( v ) ∣ ⟨ ⟩ u,v,w ::= c \mid V \mapsto w \mid\nu\mid\langle|u,v|\rangle\mid\operatorname{tup}_i(v) \mid\operatorname{left}(v) \mid\operatorname{right}(v) \mid\langle\rangle u , v , w ::= c ∣ V ↦ w ∣ ν ∣ ⟨ ∣ u , v ∣ ⟩ ∣ tup i ( v ) ∣ left ( v ) ∣ right ( v ) ∣ ⟨ ⟩ . Finite lists of elements: U , V , W ∈ List ( Element ) U,V,W \in\operatorname{List}(\operatorname{Element}) U , V , W ∈ List ( Element ) . Denotations: D , E ⊆ Element D,E \subseteq\operatorname{Element} D , E ⊆ Element . Indices: i , j ∈ N i,j \in\mathbb{N} i , j ∈ N . Consistency: u ∼ v u \sim v u ∼ v ; con ( V ) ⟺ u ∼ v \operatorname{con}(V) \Longleftrightarrow u \sim v con ( V ) ⟺ u ∼ v for all u , v ∈ V u,v \in V u , v ∈ V . Operators on denotations: op ( D 1 , … , D n ) ⊆ Element \operatorname{op}(D_1,\ldots,D_n) \subseteq\operatorname{Element} op ( D 1 , … , D n ) ⊆ Element . Λ ( F ) = { V ↦ w ∣ V ≠ [ ] , con ( V ) , w ∈ F ( V ) } ∪ { ν } \Lambda(F)=\{V\mapsto w\mid V\ne[\ ],\operatorname{con}(V),w\in F(V)\}\cup\{\nu\} Λ ( F ) = { V ↦ w ∣ V = [ ] , con ( V ) , w ∈ F ( V )} ∪ { ν } . D ∙ E = { w ∣ V ↦ w ∈ D , V ≠ [ ] , V ⊆ E } D\mathbin{\bullet}E=\{w\mid V\mapsto w\in D,V\ne[\ ],V\subseteq E\} D ∙ E = { w ∣ V ↦ w ∈ D , V = [ ] , V ⊆ E } . pair ( D , E ) = { ⟨ ∣ u , v ∣ ⟩ ∣ u ∈ D , v ∈ E } \operatorname{pair}(D,E)=\{\langle|u,v|\rangle\mid u\in D,v\in E\} pair ( D , E ) = {⟨ ∣ u , v ∣ ⟩ ∣ u ∈ D , v ∈ E } . car ( D ) = { u ∣ ⟨ ∣ u , v ∣ ⟩ ∈ D } \operatorname{car}(D)=\{u\mid\langle|u,v|\rangle\in D\} car ( D ) = { u ∣ ⟨ ∣ u , v ∣ ⟩ ∈ D } . cdr ( D ) = { v ∣ ⟨ ∣ u , v ∣ ⟩ ∈ D } \operatorname{cdr}(D)=\{v\mid\langle|u,v|\rangle\in D\} cdr ( D ) = { v ∣ ⟨ ∣ u , v ∣ ⟩ ∈ D } . T ( D 1 , … , D n ) = { tup i ( v ) ∣ v ∈ D i } \mathcal{T}(D_1,\ldots,D_n)=\{\operatorname{tup}_i(v)\mid v\in D_i\} T ( D 1 , … , D n ) = { tup i ( v ) ∣ v ∈ D i } if D 1 , … , D n ≠ ∅ D_1,\ldots,D_n\ne\varnothing D 1 , … , D n = ∅ , and ∅ \varnothing ∅ otherwise. inl ( D ) = { left ( v ) ∣ v ∈ D } \operatorname{inl}(D)=\{\operatorname{left}(v)\mid v\in D\} inl ( D ) = { left ( v ) ∣ v ∈ D } ; inr ( D ) = { right ( v ) ∣ v ∈ D } \operatorname{inr}(D)=\{\operatorname{right}(v)\mid v\in D\} inr ( D ) = { right ( v ) ∣ v ∈ D } . π t ( D ) = { v ∣ t ( v ) ∈ D } \pi_t(D)=\{v\mid t(v)\in D\} π t ( D ) = { v ∣ t ( v ) ∈ D } for t ∈ { tup i , left , right } t\in\{\operatorname{tup}_i,\operatorname{left},\operatorname{right}\} t ∈ { tup i , left , right } . C ( D , F , G ) = { w ∣ V ≠ [ ] , con ( V ) , left ( V ) ⊆ D , w ∈ F ( V ) } ∪ { w ∣ V ≠ [ ] , con ( V ) , right ( V ) ⊆ D , w ∈ G ( V ) } C(D,F,G)=\{w\mid V\ne[\ ],\operatorname{con}(V),\operatorname{left}(V)\subseteq D,w\in F(V)\}\cup\{w\mid V\ne[\ ],\operatorname{con}(V),\operatorname{right}(V)\subseteq D,w\in G(V)\} C ( D , F , G ) = { w ∣ V = [ ] , con ( V ) , left ( V ) ⊆ D , w ∈ F ( V )} ∪ { w ∣ V = [ ] , con ( V ) , right ( V ) ⊆ D , w ∈ G ( V )} . left ( V ) = { left ( v ) ∣ v ∈ V } \operatorname{left}(V)=\{\operatorname{left}(v)\mid v\in V\} left ( V ) = { left ( v ) ∣ v ∈ V } ; right ( V ) = { right ( v ) ∣ v ∈ V } \operatorname{right}(V)=\{\operatorname{right}(v)\mid v\in V\} right ( V ) = { right ( v ) ∣ v ∈ V } . prim p ( D , E ) = { δ ( p , n , m ) ∣ n ∈ D , m ∈ E integers } \operatorname{prim}_p(D,E)=\{\delta(p,n,m)\mid n\in D,m\in E\text{ integers}\} prim p ( D , E ) = { δ ( p , n , m ) ∣ n ∈ D , m ∈ E integers } . δ ( sub , n , m ) = n − m \delta(\operatorname{sub},n,m)=n-m δ ( sub , n , m ) = n − m ; δ ( eq , n , m ) = left ( ⟨ ⟩ ) \delta(\operatorname{eq},n,m)=\operatorname{left}(\langle\rangle) δ ( eq , n , m ) = left (⟨ ⟩) if n = m n=m n = m , and right ( ⟨ ⟩ ) \operatorname{right}(\langle\rangle) right (⟨ ⟩) otherwise. Semantics of FPC: ⟦ M ⟧ ρ = D \llbracket M\rrbracket_\rho=D [ [ M ] ] ρ = D ; ⟦ x ⟧ ρ = ρ ( x ) \llbracket x\rrbracket_\rho=\rho(x) [ [ x ] ] ρ = ρ ( x ) ; ⟦ c ⟧ ρ = { c } \llbracket c\rrbracket_\rho=\{c\} [ [ c ] ] ρ = { c } ; ⟦ λ x . N ⟧ ρ = Λ ( λ X . ⟦ N ⟧ ρ [ x : = X ] ) \llbracket\lambda x.N\rrbracket_\rho=\Lambda(\lambda X.\llbracket N\rrbracket_{\rho[x:=X]}) [ [ λ x . N ] ] ρ = Λ ( λ X . [ [ N ] ] ρ [ x := X ] ) ; ⟦ L M ⟧ ρ = ⟦ L ⟧ ρ ∙ ⟦ M ⟧ ρ \llbracket L\ M\rrbracket_\rho=\llbracket L\rrbracket_\rho\mathbin{\bullet}\llbracket M\rrbracket_\rho [ [ L M ] ] ρ = [ [ L ] ] ρ ∙ [ [ M ] ] ρ ; ⟦ tuple ( M 1 , … , M n ) ⟧ ρ = T ( ⟦ M 1 ⟧ ρ , … , ⟦ M n ⟧ ρ ) \llbracket\operatorname{tuple}(M_1,\ldots,M_n)\rrbracket_\rho=\mathcal{T}(\llbracket M_1\rrbracket_\rho,\ldots,\llbracket M_n\rrbracket_\rho) [ [ tuple ( M 1 , … , M n ) ] ] ρ = T ( [ [ M 1 ] ] ρ , … , [ [ M n ] ] ρ ) ; ⟦ get i M ⟧ ρ = π tup i ( ⟦ M ⟧ ρ ) \llbracket\operatorname{get}_i\ M\rrbracket_\rho=\pi_{\operatorname{tup}_i}(\llbracket M\rrbracket_\rho) [ [ get i M ] ] ρ = π tup i ( [ [ M ] ] ρ ) ; ⟦ inl M ⟧ ρ = inl ( ⟦ M ⟧ ρ ) \llbracket\operatorname{inl}\ M\rrbracket_\rho=\operatorname{inl}(\llbracket M\rrbracket_\rho) [ [ inl M ] ] ρ = inl ( [ [ M ] ] ρ ) ; ⟦ inr M ⟧ ρ = inr ( ⟦ M ⟧ ρ ) \llbracket\operatorname{inr}\ M\rrbracket_\rho=\operatorname{inr}(\llbracket M\rrbracket_\rho) [ [ inr M ] ] ρ = inr ( [ [ M ] ] ρ ) ; ⟦ prim p L M ⟧ ρ = prim p ( ⟦ L ⟧ ρ , ⟦ M ⟧ ρ ) \llbracket\operatorname{prim}_p\ L\ M\rrbracket_\rho=\operatorname{prim}_p(\llbracket L\rrbracket_\rho,\llbracket M\rrbracket_\rho) [ [ prim p L M ] ] ρ = prim p ( [ [ L ] ] ρ , [ [ M ] ] ρ ) ; ⟦ fold M ⟧ ρ = ⟦ M ⟧ ρ \llbracket\operatorname{fold}\ M\rrbracket_\rho=\llbracket M\rrbracket_\rho [ [ fold M ] ] ρ = [ [ M ] ] ρ ; ⟦ let x = M in N ⟧ ρ = Λ ( λ X . ⟦ N ⟧ ρ [ x : = X ] ) ∙ ⟦ M ⟧ ρ \llbracket\operatorname{let}\ x=M\ \operatorname{in}\ N\rrbracket_\rho=\Lambda(\lambda X.\llbracket N\rrbracket_{\rho[x:=X]})\mathbin{\bullet}\llbracket M\rrbracket_\rho [ [ let x = M in N ] ] ρ = Λ ( λ X . [ [ N ] ] ρ [ x := X ] ) ∙ [ [ M ] ] ρ ; ⟦ unfold M ⟧ ρ = ⟦ M ⟧ ρ \llbracket\operatorname{unfold}\ M\rrbracket_\rho=\llbracket M\rrbracket_\rho [ [ unfold M ] ] ρ = [ [ M ] ] ρ ; ⟦ case L ( x . M ) ( y . N ) ⟧ ρ = C ( ⟦ L ⟧ ρ , λ X . ⟦ M ⟧ ρ [ x : = X ] , λ Y . ⟦ N ⟧ ρ [ y : = Y ] ) \llbracket\operatorname{case}\ L\ (x.M)\ (y.N)\rrbracket_\rho=C(\llbracket L\rrbracket_\rho,\lambda X.\llbracket M\rrbracket_{\rho[x:=X]},\lambda Y.\llbracket N\rrbracket_{\rho[y:=Y]}) [ [ case L ( x . M ) ( y . N ) ] ] ρ = C ( [ [ L ] ] ρ , λ X . [ [ M ] ] ρ [ x := X ] , λY . [ [ N ] ] ρ [ y := Y ] ) bbox=0.08,0.12,0.92,0.70
Figure 3. The graph model (elements, denotations, and operators) and the semantics of FPC.
Functions as tables. One can describe a function by tabulating it. Each row of the table pairs an input with the corresponding output, and applying the function means looking up a row. Two problems arise with this simple approach. First, a function on an infinite domain (such as the integers) needs an infinite table. Second, a function that is applied to itself (such as the Y combinator) implies that the function’s table must contain itself. would need a table that contains a copy of itself. Graph models solve both problems. For the first problem, a terminating program applies each function to only finitely many arguments, so finite tables suffice [67 , 76 ] . However, to handle all possible contexts, the meaning of a function is the set of all its finite tables, or equivalently the set of all their rows. For the second problem, a row applies to an argument when the row’s input is included in the argument, rather than equal to it. A table then needs only a smaller copy of itself as an input entry, and elements, defined as an inductive data type, can provide that.
Graph elements. An element is a finite and partial description of a term’s behavior (Figure 3 ). A row of a table is represented by an element V ↦ w V \mapsto w V ↦ w , called an entry , whose input V V V is a list of elements3 and whose output w w w is an element.4 The element ν \nu ν records that a term evaluates to a function, without describing any of the function’s rows. The other elements describe the data of our languages: a constant c c c , a pair ⟨ u , v ⟩ \langle u,v\rangle ⟨ u , v ⟩ , a tuple element tup i ( v ) \operatorname{tup}_i(v) tup i ( v ) which says that component i i i of a tuple is described by v v v , the empty tuple ⟨ ⟩ \langle\rangle ⟨ ⟩ , and the injections left ( v ) \operatorname{left}(v) left ( v ) and right ( v ) \operatorname{right}(v) right ( v ) of a sum. A denotation is a set of elements.
Operators. Each case of the semantics of FPC (Figure 3 ) applies an operator to the denotations of the sub terms. In the semantics, an environment ρ \rho ρ maps each variable to a set of elements, ρ [ x : = D ] \rho[x:=D] ρ [ x := D ] maps x x x to D D D and every other variable as ρ \rho ρ does, and λ \lambda λ denotes a mathematical function.
The meaning of a function, given by the operator Λ \Lambda Λ , is the set of all entries V ↦ w V \mapsto w V ↦ w such that the output w w w is in the meaning of the function’s body in an environment that maps its parameter to the input V V V , together with ν \nu ν . The input V V V must be nonempty and consistent, written con ( V ) \operatorname{con}(V) con ( V ) , which we explain at the end of this section. For example, ⟦ λ x . x + 1 ⟧ \llbracket\lambda x.x+1\rrbracket [ [ λ x . x + 1 ] ] contains the entry [ n ] ↦ n + 1 [n]\mapsto n+1 [ n ] ↦ n + 1 for every integer n n n . The meaning of the twice function ⟦ λ f . λ x . f ( f x ) ⟧ \llbracket\lambda f.\lambda x.f(f\ x)\rrbracket [ [ λ f . λ x . f ( f x ) ] ] contains the entries
[ [ 0 ] ↦ 1 , [ 1 ] ↦ 2 ] ↦ ( [ 0 ] ↦ 2 ) and [ [ 2 ] ↦ 4 , [ 4 ] ↦ 8 ] ↦ ( [ 2 ] ↦ 8 ) [[0]\mapsto1,\ [1]\mapsto2]\mapsto([0]\mapsto2)\qquad\text{and}\qquad[[2]\mapsto4,\ [4]\mapsto8]\mapsto([2]\mapsto8) [[ 0 ] ↦ 1 , [ 1 ] ↦ 2 ] ↦ ([ 0 ] ↦ 2 ) and [[ 2 ] ↦ 4 , [ 4 ] ↦ 8 ] ↦ ([ 2 ] ↦ 8 )
whose input has one entry for each call to f f f . Because of ν \nu ν , a λ \lambda λ -abstraction has a nonempty meaning, as befits a value in a call-by-value language, even if it is never applied.
The operator D ∙ E D\mathbin{\bullet}E D ∙ E gives the meaning of application. It collects the outputs of the entries of D D D whose input is contained in the argument E E E . For example, we apply the increment function to 3 to get 4, ⟦ ( λ x . x + 1 ) ( 2 + 1 ) ⟧ = { 4 } \llbracket(\lambda x.x+1)\ (2+1)\rrbracket=\{4\} [ [ ( λ x . x + 1 ) ( 2 + 1 ) ] ] = { 4 } , because the input of entry [ 3 ] ↦ 4 [3]\mapsto4 [ 3 ] ↦ 4 is contained in ⟦ 2 + 1 ⟧ = { 3 } \llbracket2+1\rrbracket=\{3\} [ [ 2 + 1 ] ] = { 3 } . Likewise, applying the twice function to ( λ x . x + 1 ) (\lambda x.x+1) ( λ x . x + 1 ) and then to 0 gives { 2 } \{2\} { 2 } because { [ 0 ] ↦ 1 , [ 1 ] ↦ 2 } \{[0]\mapsto1,\ [1]\mapsto2\} {[ 0 ] ↦ 1 , [ 1 ] ↦ 2 } is contained in ⟦ λ x . x + 1 ⟧ \llbracket\lambda x.x+1\rrbracket [ [ λ x . x + 1 ] ] .
The other operators build and take apart data. The operator pair builds pairs, and car and cdr take their components. The operator T \mathcal{T} T builds tuples, and π tup i \pi_{\operatorname{tup}_i} π tup i takes component i i i . The operators inl and inr build injections, and C C C gives the meaning of a case. Like Λ \Lambda Λ , it binds the variable of a branch to a nonempty, consistent list, here of the payloads of the injections in the scrutinee. The operator prim p \operatorname{prim}_p prim p applies a primitive to the integers in its operands. The empty tuple ⟨ ⟩ \langle\rangle ⟨ ⟩ also fills the positions of an environment that hold no variable, because we represent environments as total functions whose positions are nonempty. The environment ρ 0 \rho_0 ρ 0 of a closed program maps every variable to { ⟨ ⟩ } \{\langle\rangle\} {⟨ ⟩} , and we define ⟦ M ⟧ = ⟦ M ⟧ ρ 0 \llbracket M\rrbracket=\llbracket M\rrbracket_{\rho_0} [ [ M ] ] = [ [ M ] ] ρ 0 .
Recursion and divergence. Recursion in FPC comes from self-application, which the recursive types of FPC make typable. The constructs fold and unfold mean the identity, so recursive types cost nothing in the semantics. Because entries apply to arguments that include their input, the self-applied function in a fixed-point combinator such as Z Z Z has a meaning whose tables contain smaller copies of themselves, like a set of nested dolls [79 ] .
A program that diverges produces no evidence, so its meaning is the empty set. For example, let Ω = Δ ( fold Δ ) \Omega=\Delta\ (\operatorname{fold}\ \Delta) Ω = Δ ( fold Δ ) with Δ = ( λ x . ( unfold x ) x ) \Delta=(\lambda x.(\operatorname{unfold}\ x)\ x) Δ = ( λ x . ( unfold x ) x ) . Then ⟦ Ω ⟧ = ∅ \llbracket\Omega\rrbracket=\varnothing [ [ Ω ] ] = ∅ because an output of Ω \Omega Ω would need an entry of ⟦ Δ ⟧ \llbracket\Delta\rrbracket [ [ Δ ] ] whose input v v v matches a strictly smaller entry of ⟦ Δ ⟧ \llbracket\Delta\rrbracket [ [ Δ ] ] with the same output, and so on forever, which is impossible for inductively defined elements.
Consistency. We write u ∼ v u\sim v u ∼ v when the elements u u u and v v v are consistent, which means that they could describe the same value. Appendix § 10 .§ 1 gives the definition. The operator Λ \Lambda Λ builds only entries whose inputs are consistent, which rules out entries such as [ 3 , 5 ] ↦ 6 [3,5]\mapsto6 [ 3 , 5 ] ↦ 6 in the meaning of λ x . x + 1 \lambda x.x+1 λ x . x + 1 , because the input [ 3 , 5 ] [3,5] [ 3 , 5 ] of that entry describes no single argument, i.e. 3 conflicts with 5. Our model requires consistency because the proof for the split closure pass needs it (§ 4.2 ). The meaning of a call in PairClos takes the curried function from one pair and the environment from another, so it may combine the function of one pair with the environment of a different pair. Consistency ensures that the environments of these pairs are consistent with each other.
The properties of the semantics. The meaning of a term is monotone in its environment (Lemma A.1 ), and it is continuous in the sense that each element of the meaning depends on only finitely many elements of the environment. These properties, together with consistency, give the graph model the following β \beta β laws. Its premises come from the definitions of Λ \Lambda Λ and application, which use only entries whose input is nonempty and consistent. A function F F F on sets is called finitary at D D D if every w ∈ F ( D ) w \in F(D) w ∈ F ( D ) is in F ( V ) F(V) F ( V ) for some nonempty V ⊆ D V \subseteq D V ⊆ D , and similarly for a function of two sets.
Lemma 3.1 (β \beta β laws).
If F F F is monotone and finitary at D D D and D D D is consistent, then Λ ( F ) ∙ D = F ( D ) \Lambda(F) \mathbin{\bullet} D = F(D) Λ ( F ) ∙ D = F ( D ) .
If F F F is finitary at T T T and E E E , and both T T T and E E E are consistent, then F ( T , E ) ⊆ ( Λ ( λ X . Λ ( λ Y . F ( X , Y ) ) ) ∙ T ) ∙ E F(T,E) \subseteq(\Lambda(\lambda X.\Lambda(\lambda Y.F(X,Y))) \mathbin{\bullet} T) \mathbin{\bullet} E F ( T , E ) ⊆ ( Λ ( λ X .Λ ( λY . F ( X , Y ))) ∙ T ) ∙ E .
Denotational Compiler Correctness
This section proves the following two theorems about the FPC compiler.
Theorem 4.1 (Compiler correctness, graph model). Let M M M be a closed term of FPC of type Int. For every constant c c c , c ∈ ⟦ M ⟧ c \in\llbracket M\rrbracket c ∈ [ [ M ] ] if and only if c ∈ ⟦ compile ( M ) ⟧ c \in\llbracket\operatorname{compile}(M)\rrbracket c ∈ [ [ compile ( M ) ] ] . Moreover, ⟦ M ⟧ ≠ ∅ \llbracket M\rrbracket\ne\varnothing [ [ M ] ] = ∅ if and only if ⟦ compile ( M ) ⟧ ≠ ∅ \llbracket\operatorname{compile}(M)\rrbracket\ne\varnothing [ [ compile ( M ) ] ] = ∅ .
Theorem 4.2 (Compiler correctness, Scott domains). Assume propositional truncation, function extensionality, and propositional extensionality. Let M M M be a closed term of FPC of type Int. For every constant c c c , ⟦ M ⟧ ρ 0 = ⌊ ( c , ( ) ) ⌋ \llbracket M\rrbracket_{\rho_0} = \lfloor(c,())\rfloor [ [ M ] ] ρ 0 = ⌊( c , ( ))⌋ if and only if ⟦ compile ( M ) ⟧ = ⌊ ( c , ( ) ) ⌋ \llbracket\operatorname{compile}(M)\rrbracket= \lfloor(c,())\rfloor [ [ compile ( M ) ] ] = ⌊( c , ( ))⌋ . Moreover, ⟦ M ⟧ ρ 0 \llbracket M\rrbracket_{\rho_0} [ [ M ] ] ρ 0 is defined if and only if ⟦ compile ( M ) ⟧ \llbracket\operatorname{compile}(M)\rrbracket [ [ compile ( M ) ] ] is defined.
Theorems 4.1 and 4.2, whose proofs we mechanize in Agda, answer RQ1 affirmatively. The same statements hold for closed terms of every type; we state them for Int, the type of a whole program’s result, to align them with the operational correctness results (§ 6 ). Each proof is a composition of one correctness lemma per pass. We first show how most of the passes can be proved correct by equational reasoning (§ 4.1 ). We then prove that the split closure pass is correct using a logical relation (§ 4.2 ). Finally, we sketch the corresponding proof of correctness for Scott domains (§ 4.3 ).
Passes Proved by Equational Reasoning
For seven of the eight passes, correctness is an equation between the meaning of a program and the meaning of its translation.
Lemma 4.3 (Passes proved by an equation). Let p p p be a pass other than split closure , and let M M M be a closed term of the source language of p p p . Then ⟦ M ⟧ = ⟦ p ( M ) ⟧ \llbracket M\rrbracket= \llbracket p(M)\rrbracket [ [ M ] ] = [ [ p ( M ) ] ] .
Each proof is by induction on the term, generalizing Lemma 4.3 to open terms in a source and a target environment in which each variable means the same as its translation, with side conditions such as nonemptiness and consistency. Every semantics is compositional and the passes translate most forms homomorphically, so most cases apply the same operator to equal arguments. The real work is in the few forms that a pass changes, where an equation about the operators of the graph model turns the source form into the target form. Below we give these calculations for **simplify and for the last step of enclose . We skip over side conditions about non-empty and consistent denotations but of course they are present in the Agda mechanization.
Appendix § 13 gives the rest of the equational proofs, including the proof for select instructions , whose key case turns a call into fst, snd, movq, and callq. Where a step needs a nonempty set, we show the nonempty case; otherwise both sides are empty.
Simplify. The pass simplify keeps a static environment E E E (Appendix § 12 .§ 1 ). It maps each variable to the term that replaces it and, when the variable is bound to a statically known function, to that function as a λ \lambda λ -abstraction ⟨ λ x . N , E f ⟩ \langle\lambda x. N, E_f \rangle ⟨ λ x . N , E f ⟩ paired with the static environment E f E_f E f where it was defined. The main lemma for simplify carries the invariant that E E E agrees with the source environment ρ \rho ρ and the target environment ρ ′ \rho' ρ ′ . If E E E replaces a variable by the term M ′ M' M ′ , the variable means ⟦ M ′ ⟧ ρ ′ \llbracket M' \rrbracket\rho' [ [ M ′ ] ] ρ ′ , and if E E E maps it to the function ⟨ λ x . N , E f ⟩ \langle\lambda x. N, E_f \rangle ⟨ λ x . N , E f ⟩ , it means ⟦ λ x . N ⟧ ρ f \llbracket\lambda x. N \rrbracket_{\rho_f} [ [ λ x . N ] ] ρ f for some environment ρ f \rho_f ρ f such that E f E_f E f agrees with ρ f \rho_f ρ f and ρ ′ \rho' ρ ′ . The key case is inlining. Suppose L L L is statically known to be the function ⟨ λ x . N , E f ⟩ \langle\lambda x. N, E_f \rangle ⟨ λ x . N , E f ⟩ , the operand M M M simplifies to a small value M ′ M' M ′ , and the body simplifies to N ′ N' N ′ . Then
⟦ L M ⟧ ρ = ⟦ L ⟧ ρ ∙ ⟦ M ⟧ ρ (meaning of application) = ⟦ λ x . N ⟧ ρ f ∙ ⟦ M ′ ⟧ ρ ′ (invariant for L , induction hypothesis for M ) = ⟦ N ⟧ ρ f [ x : = ⟦ M ′ ⟧ ρ ′ ] (Lemma 3.1, part 1) = ⟦ N ′ ⟧ ρ ′ (induction hypothesis for N ) \begin{aligned}
\llbracket L\,M \rrbracket_\rho&= \llbracket L \rrbracket_\rho\mathbin{\bullet} \llbracket M \rrbracket_\rho&& \text{(meaning of application)}\\
&= \llbracket\lambda x. N \rrbracket_{\rho_f} \mathbin{\bullet} \llbracket M' \rrbracket_{\rho'} && \text{(invariant for }L\text{, induction hypothesis for }M\text{)}\\
&= \llbracket N \rrbracket_{\rho_f}[x := \llbracket M' \rrbracket_{\rho'}] && \text{(Lemma 3.1, part 1)}\\
&= \llbracket N' \rrbracket_{\rho'} && \text{(induction hypothesis for }N\text{)}
\end{aligned} [ [ L M ] ] ρ = [ [ L ] ] ρ ∙ [ [ M ] ] ρ = [ [ λ x . N ] ] ρ f ∙ [ [ M ′ ] ] ρ ′ = [ [ N ] ] ρ f [ x := [ [ M ′ ] ] ρ ′ ] = [ [ N ′ ] ] ρ ′ (meaning of application) (invariant for L , induction hypothesis for M ) (Lemma 3.1, part 1) (induction hypothesis for N )
The last step applies because the inlined body is simplified in E f E_f E f extended to replace x x x by M ′ M' M ′ , and this static environment agrees with ρ f [ x : = ⟦ M ′ ⟧ ρ ′ ] \rho_f[x := \llbracket M' \rrbracket_{\rho'}] ρ f [ x := [ [ M ′ ] ] ρ ′ ] and ρ ′ \rho' ρ ′ . The invariant for L L L gives the agreement of E f E_f E f , and both x x x and its replacement M ′ M' M ′ mean ⟦ M ′ ⟧ ρ ′ \llbracket M' \rrbracket_{\rho'} [ [ M ′ ] ] ρ ′ .
Enclose. The enclose pass works in three steps, capture , prune , and concretize (§C.2). The first two produce closures of an intermediate language, Clos, written clos ( x ⃗ , y . N ) M ⃗ \operatorname{clos}(\vec{x},y.N)\,\vec{M} clos ( x , y . N ) M , which bind a name x i x_i x i for each captured value M i M_i M i . The last step, concretize φ \operatorname{concretize}_\varphi concretize φ , stores the captured values in a tuple. Each variable x x x satisfies ⟦ x ⟧ ρ = ⟦ φ ( x ) ⟧ ρ ′ \llbracket x \rrbracket_\rho= \llbracket\varphi(x) \rrbracket_{\rho'} [ [ x ] ] ρ = [ [ φ ( x ) ] ] ρ ′ , where its reference φ ( x ) \varphi(x) φ ( x ) is either x x x or get i z \operatorname{get}_i\,z get i z . Let M i ′ = concretize φ ( M i ) M'_i = \operatorname{concretize}_\varphi(M_i) M i ′ = concretize φ ( M i ) , D i = ⟦ M i ⟧ ρ D_i = \llbracket M_i \rrbracket_\rho D i = [ [ M i ] ] ρ , D i ′ = ⟦ M i ′ ⟧ ρ ′ D'_i = \llbracket M'_i \rrbracket_{\rho'} D i ′ = [ [ M i ′ ] ] ρ ′ , and N ′ = concretize φ ′ ( N ) N' = \operatorname{concretize}_{\varphi'}(N) N ′ = concretize φ ′ ( N ) :
⟦ clos ( x ⃗ , y . N ) M ⃗ ⟧ ρ = guard ( D ⃗ ; Λ ( Y ⃗ . ⟦ N ⟧ ρ 0 [ x ⃗ : = D ⃗ , y : = Y ⃗ ] ) ) (meaning of a Clos closure) = Λ ( Y ⃗ . ⟦ N ⟧ ρ 0 [ x ⃗ : = D ⃗ , y : = Y ⃗ ] ) (each D i is nonempty) = Λ ( Y ⃗ . ⟦ N ⟧ ρ 0 [ x ⃗ : = D ⃗ ′ , y : = Y ⃗ ] ) (induction hypothesis for M ⃗ ) = Λ ( Y ⃗ . ⟦ N ′ ⟧ ρ 0 [ z : = T ( D ⃗ ′ ) , y : = Y ⃗ ] ) (induction hypothesis for N ) = Λ ( X ⃗ . Λ ( Y ⃗ . ⟦ N ′ ⟧ ρ 0 [ z : = X ⃗ , y : = Y ⃗ ] ) ) ∙ T ( D ⃗ ′ ) (Lemma 3.1, part 1) = ⟦ clos ( z , y . N ′ ) M ⃗ ′ ⟧ ρ ′ (meaning of an EnvClos closure) \begin{aligned}
\llbracket\operatorname{clos}(\vec{x},y.N)\,\vec{M} \rrbracket_\rho
&= \operatorname{guard}(\vec{D};\Lambda(\vec{Y}.\llbracket N \rrbracket_{\rho_0}[\vec{x}:=\vec{D},y:=\vec{Y}])) && \text{(meaning of a Clos closure)}\\
&= \Lambda(\vec{Y}.\llbracket N \rrbracket_{\rho_0}[\vec{x}:=\vec{D},y:=\vec{Y}]) && \text{(each }D_i\text{ is nonempty)}\\
&= \Lambda(\vec{Y}.\llbracket N \rrbracket_{\rho_0}[\vec{x}:=\vec{D}',y:=\vec{Y}]) && \text{(induction hypothesis for }\vec{M}\text{)}\\
&= \Lambda(\vec{Y}.\llbracket N' \rrbracket_{\rho_0}[z:=\mathcal{T}(\vec{D}'),y:=\vec{Y}]) && \text{(induction hypothesis for }N\text{)}\\
&= \Lambda(\vec{X}.\Lambda(\vec{Y}.\llbracket N' \rrbracket_{\rho_0}[z:=\vec{X},y:=\vec{Y}])) \mathbin{\bullet} \mathcal{T}(\vec{D}') && \text{(Lemma 3.1, part 1)}\\
&= \llbracket\operatorname{clos}(z,y.N')\,\vec{M}' \rrbracket_{\rho'} && \text{(meaning of an EnvClos closure)}
\end{aligned} [ [ clos ( x , y . N ) M ] ] ρ = guard ( D ; Λ ( Y . [ [ N ] ] ρ 0 [ x := D , y := Y ])) = Λ ( Y . [ [ N ] ] ρ 0 [ x := D , y := Y ]) = Λ ( Y . [ [ N ] ] ρ 0 [ x := D ′ , y := Y ]) = Λ ( Y . [ [ N ′ ] ] ρ 0 [ z := T ( D ′ ) , y := Y ]) = Λ ( X .Λ ( Y . [ [ N ′ ] ] ρ 0 [ z := X , y := Y ])) ∙ T ( D ′ ) = [ [ clos ( z , y . N ′ ) M ′ ] ] ρ ′ (meaning of a Clos closure) (each D i is nonempty) (induction hypothesis for M ) (induction hypothesis for N ) (Lemma 3.1, part 1) (meaning of an EnvClos closure)
The induction hypothesis for N N N applies because the code reads x i x_i x i as get i z \operatorname{get}_i\,z get i z , and π tup i ( T ( D ⃗ ′ ) ) = D i ′ \pi_{\operatorname{tup}_i}(\mathcal{T}(\vec{D}')) = D'_i π tup i ( T ( D ′ )) = D i ′ . Each interesting case of the seven passes is a short chain of equations like these, which is why their denotational proofs are small (RQ3, § 6 ). The exception is split closure , whose correctness is not an equation (§ 4.2 ).
Correctness of split closure and the Approximant Logical Relation
The split closure pass separates the code of each closure from its environment, making the closure a pair. Its proof is the hardest part of the compiler proof. This section shows why semantic reflection for split closure resisted our earlier attempts and how the approximant logical relation proves it.
In EnvClos, the source of split closure , a closure clos ( z , y . N ) M ⃗ \operatorname{clos}(z,y.N)\,\vec{M} clos ( z , y . N ) M still means a function. Parameter z z z is for the environment and y y y is for the argument. The meaning of N N N is a curried function of the environment X X X and then the argument Y Y Y . The meaning of the closure is this
( U ↦ w ) ⊑ D ′ = ( ∃ u , t . ⟨ u , t ⟩ ∈ D ′ ) ∧ ∀ E ′ . con ( E ′ ) ∧ U ⊑ ∗ E ′ ⇒ w ⊑ ( car ( D ′ ) ∙ cdr ( D ′ ) ) ∙ E ′ (U \mapsto w) \sqsubseteq D' = (\exists u,t.\ \langle u,t\rangle\in D') \land\forall E'.\ \operatorname{con}(E') \land U \sqsubseteq^{*} E' \Rightarrow w \sqsubseteq(\operatorname{car}(D') \mathbin{\bullet} \operatorname{cdr}(D')) \mathbin{\bullet} E' ( U ↦ w ) ⊑ D ′ = ( ∃ u , t . ⟨ u , t ⟩ ∈ D ′ ) ∧ ∀ E ′ . con ( E ′ ) ∧ U ⊑ ∗ E ′ ⇒ w ⊑ ( car ( D ′ ) ∙ cdr ( D ′ )) ∙ E ′
v ⊑ D ′ = ∃ u , t . ⟨ u , t ⟩ ∈ D ′ v \sqsubseteq D' = \exists u,t.\ \langle u,t\rangle\in D' v ⊑ D ′ = ∃ u , t . ⟨ u , t ⟩ ∈ D ′
t ( v ) ⊑ D ′ = v ⊑ π t ( D ′ ) for t ∈ { tup i , left , right } t(v) \sqsubseteq D' = v \sqsubseteq\pi_t(D') \qquad\text{for } t \in\{\operatorname{tup}_i,\operatorname{left},\operatorname{right}\} t ( v ) ⊑ D ′ = v ⊑ π t ( D ′ ) for t ∈ { tup i , left , right }
v ⊑ D ′ = v ∈ D ′ for the other elements v \sqsubseteq D' = v \in D' \qquad\text{for the other elements} v ⊑ D ′ = v ∈ D ′ for the other elements
[ ] ⊑ ∗ E ′ = true [\,] \sqsubseteq^{*} E' = \operatorname{true} [ ] ⊑ ∗ E ′ = true
( u : : U ) ⊑ ∗ E ′ = u ⊑ E ′ ∧ U ⊑ ∗ E ′ (u :: U) \sqsubseteq^{*} E' = u \sqsubseteq E' \land U \sqsubseteq^{*} E' ( u :: U ) ⊑ ∗ E ′ = u ⊑ E ′ ∧ U ⊑ ∗ E ′
function applied to the captured values M ⃗ \vec{M} M . An application L M L\ M L M simply applies the function:
⟦ clos ( z , y . N ) M ⃗ ⟧ ρ = Λ ( λ X . Λ ( λ Y . ⟦ N ⟧ ρ 0 [ z : = X , y : = Y ] ) ) ∙ T ( ⟦ M ⃗ ⟧ ρ ) \llbracket\operatorname{clos}\ (z,y.\ N)\ \vec{M}\rrbracket_{\rho}
= \Lambda(\lambda X.\ \Lambda(\lambda Y.\ \llbracket N\rrbracket_{\rho_0}[z:=X,y:=Y])) \mathbin{\bullet} \mathcal{T}(\llbracket\vec{M}\rrbracket_{\rho}) [ [ clos ( z , y . N ) M ] ] ρ = Λ ( λ X . Λ ( λY . [ [ N ] ] ρ 0 [ z := X , y := Y ])) ∙ T ( [ [ M ] ] ρ )
⟦ L M ⟧ ρ = ⟦ L ⟧ ρ ∙ ⟦ M ⟧ ρ \llbracket L\ M\rrbracket_{\rho}
= \llbracket L\rrbracket_{\rho} \mathbin{\bullet} \llbracket M\rrbracket_{\rho} [ [ L M ] ] ρ = [ [ L ] ] ρ ∙ [ [ M ] ] ρ
In PairClos, the target language, a closure is a pair of code and environment, and a call takes the pair apart. The meaning of code ( z , y . N ) (z,y.\ N) ( z , y . N ) is the curried function. The meaning of pair L M L\ M L M is the set of pairs ⟨ u , v ⟩ \langle u,v\rangle ⟨ u , v ⟩ with u u u from the code L L L and v v v from the environment M M M . A call applies the first components of the pairs to their second components and then to the argument:
⟦ code ( z , y . N ) ⟧ ρ = Λ ( λ X . Λ ( λ Y . ⟦ N ⟧ ρ 0 [ z : = X , y : = Y ] ) ) \llbracket\operatorname{code}\ (z,y.\ N)\rrbracket_{\rho}
= \Lambda(\lambda X.\ \Lambda(\lambda Y.\ \llbracket N\rrbracket_{\rho_0}[z:=X,y:=Y])) [ [ code ( z , y . N ) ] ] ρ = Λ ( λ X . Λ ( λY . [ [ N ] ] ρ 0 [ z := X , y := Y ]))
⟦ pair L M ⟧ ρ = { ⟨ u , v ⟩ ∣ u ∈ ⟦ L ⟧ ρ , v ∈ ⟦ M ⟧ ρ } \llbracket\operatorname{pair}\ L\ M\rrbracket_{\rho}
= \{\langle u,v\rangle\mid u \in\llbracket L\rrbracket_{\rho},\ v \in\llbracket M\rrbracket_{\rho}\} [ [ pair L M ] ] ρ = {⟨ u , v ⟩ ∣ u ∈ [ [ L ] ] ρ , v ∈ [ [ M ] ] ρ }
⟦ callc L M ⟧ ρ = ( car ( ⟦ L ⟧ ρ ) ∙ cdr ( ⟦ L ⟧ ρ ) ) ∙ ⟦ M ⟧ ρ \llbracket\operatorname{callc}\ L\ M\rrbracket_{\rho}
= (\operatorname{car}(\llbracket L\rrbracket_{\rho}) \mathbin{\bullet} \operatorname{cdr}(\llbracket L\rrbracket_{\rho})) \mathbin{\bullet} \llbracket M\rrbracket_{\rho} [ [ callc L M ] ] ρ = ( car ( [ [ L ] ] ρ ) ∙ cdr ( [ [ L ] ] ρ )) ∙ [ [ M ] ] ρ
The split closure pass translates each closure into a pair and each application into a callc. It translates every other construct homomorphically:
split ( clos ( z , y . N ) M ⃗ ) = pair ( code ( z , y . split ( N ) ) ) ( tuple split ( M ⃗ ) ) \operatorname{split}(\operatorname{clos}\ (z,y.\ N)\ \vec{M})
= \operatorname{pair}\ (\operatorname{code}\ (z,y.\ \operatorname{split}(N)))\ (\operatorname{tuple}\ \operatorname{split}(\vec{M})) split ( clos ( z , y . N ) M ) = pair ( code ( z , y . split ( N ))) ( tuple split ( M ))
split ( L M ) = callc split ( L ) split ( M ) \operatorname{split}(L\ M)
= \operatorname{callc}\ \operatorname{split}(L)\ \operatorname{split}(M) split ( L M ) = callc split ( L ) split ( M )
Why preservation is easy and reflection is not. The correctness of split closure is not an equation, because the pass changes the denotation of a function. In EnvClos, a closure denotes the graph of the function. In PairClos, it denotes pairs of the graph of the function curried over the values of its free variables and the tuple of those values. Lemma 4.6 therefore states only that a closed program and its translation produce the same constants and that one produces something exactly when the other does. The forward half, preservation, requires only a relation between a single source element and the set of target elements, defined by recursion on the source element (Figure 4 ). We read v ⊑ D ′ v \sqsubseteq D' v ⊑ D ′ as “v v v is included in the behavior of D ′ D' D ′ .” The source element is finite, so the recursion is structural, and preservation is a routine induction on M M M .
Figure 4. The relation v ⊑ D ′ v \sqsubseteq D' v ⊑ D ′ (“v v v is included in the behavior of D ′ D' D ′ ”) for the preservation half of split closure . It relates a source element v v v (of EnvClos) to a set D ′ D' D ′ of target elements (of PairClos) and is defined by recursion on v v v . The relation U ⊑ ∗ E ′ U \sqsubseteq^{*} E' U ⊑ ∗ E ′ extends it to a finite list U U U .
The backward half, reflection, must show that the source accounts for every element that the target produces. Here the representation of closures gets in the way. Consider the function ( λ y . x + y ) (\lambda y.\ x+y) ( λ y . x + y ) in an environment where x x x is 9. In EnvClos, its meaning includes entries [ 1 ] ↦ 10 [1] \mapsto10 [ 1 ] ↦ 10 and [ 6 ] ↦ 15 [6] \mapsto15 [ 6 ] ↦ 15 . After split closure , its code takes the environment as an extra parameter. The meaning of the code is a table of curried entries , which map an environment to an entry. It has entries for every environment, such as the following two:
[ 9 ] ↦ ( [ 1 ] ↦ 10 ) [ 2 ] ↦ ( [ 1 ] ↦ 3 ) ⋯ [9] \mapsto([1] \mapsto10)
\qquad
[2] \mapsto([1] \mapsto3)
\qquad\cdots [ 9 ] ↦ ([ 1 ] ↦ 10 ) [ 2 ] ↦ ([ 1 ] ↦ 3 ) ⋯
The program pairs this table only with environment [9], so the entry for [2] cannot be related to source function. Worse, the denotation of a call takes the table from car and the environment from cdr separately, so it may combine the table of one pair with the environment of another.
Design alternatives: which sides to relate. Before we define our relation for reflection, we explain how we chose its shape, because the obvious designs fail. A relation for reflection relates a source side to a target side, and each side can be a single graph element, a finite list of elements, or a set that may be infinite. Of these nine designs, the following reasons rule out all but one (Appendix E gives examples and the variants we tried in Agda).
A single target element , against any source side, fails because a call combines the curried entry of one pair with the environment of another, so no single pair accounts for its result.
A single source element or a finite source list , against any target side, fails in the function case, because infinitely many related source argument sets have no element in common, so no finite source side has an entry for each of them.
Whole denotations on both sides handle the mixing, but the function case uses the relation in negative position, where nothing decreases. A well-founded definition would need a later modality or a construction by stages, as in our Scott-domain proof (§ 4.3 ).
A target set of bounded depth against the source denotation is well founded, but the target denotation is unbounded, and the outputs of a call can come from curried entries of any depth.
The remaining design, which we adopt, relates a finite list of target elements to the whole source denotation. We also had to choose what the definition recurses on. Recursion on types, as in the proofs of Chlipala [ [15 ] and Nielsen [ [57 ] , is not an option. Because fold and unfold mean the identity, the relation at μ X . A \mu X . A μ X . A would be the relation at A [ μ X . A / X ] A[\mu X . A/X] A [ μ X . A / X ] , which is not a smaller type. Our relation instead recurses on the depth of the target elements.
The key insight. When a call ( car ( D ′ ) ∙ cdr ( D ′ ) ) ∙ E ′ (\operatorname{car}(D') \mathbin{\bullet} \operatorname{cdr}(D')) \mathbin{\bullet} E' ( car ( D ′ ) ∙ cdr ( D ′ )) ∙ E ′ produces a result w w w , it uses only finitely many pairs of D ′ D' D ′ . One supplies a curried entry U e ↦ ( U a ↦ w ) U_e \mapsto(U_a \mapsto w) U e ↦ ( U a ↦ w ) , and others supply the environments in U e U_e U e . These pairs form a finite list V ⊆ D ′ V \subseteq D' V ⊆ D ′ that contains both halves of the call, so the relation matches the curried functions and the environments inside V V V . The relation lets a call use exactly the curried entries whose environments V V V itself provides, which excludes the junk. The finite list also makes the relation well founded.
The approximant logical relation. Figure 5 defines the approximant logical relation D ⪰ V D \mathrel{\succeq} V D ⪰ V , which relates a finite list V V V of target elements to a source set D D D . We read D ⪰ V D \mathrel{\succeq} V D ⪰ V as “V V V approximates D D D ”. Its building block is the usable entry of a list. A target closure is a set of pairs ⟨ u , t ⟩ \langle u,t\rangle ⟨ u , t ⟩ of a curried function u u u and an environment t t t , so the entries of u u u have the form U e ↦ ( U a ↦ w ) U_e \mapsto(U_a \mapsto w) U e ↦ ( U a ↦ w ) , which maps the environment U e U_e U e to a function that maps the arguments U a U_a U a to w w w . We write U a ↦ w ∈ V U_a \mapsto w \in V U a ↦ w ∈ V , read “U a ↦ w U_a \mapsto w U a ↦ w is a usable entry of V V V ”, when such an entry occurs among the curried functions car(V) and the environments U e U_e U e are among the environments cdr(V) that V V V itself provides. The second condition makes the relation sound in the presence of junk. A curried entry for an environment that the program never supplies, such as the entry for [2] in the example above, is not usable. A call on D ′ D' D ′ takes the curried function from car(D') and the environment from cdr(D'). In the same way, the curried function and argument of a usable entry may come from different parts of V V V .
Figure 5. The approximant logical relation D ⊒ V D \mathrel{\sqsupseteq} V D ⊒ V between a set D D D of source (EnvClos) elements and a finite list V V V of target (PairClos) elements. Here c c c ranges over constants and π \pi π over the projections of tuple components and sum injections; car and cdr are as in Figure 3 , con is consistency (§ 3 ), and depth ( V ) \operatorname{depth}(V) depth ( V ) is the largest depth of an element of V V V (Appendix § 10 .§ 2 ).
We write V ⋅ U ⇝ W V \mathbin{\cdot} U \leadsto W V ⋅ U ⇝ W , read “V V V maps U U U to W W W ”, when every argument in U U U is taken by some usable entry of V V V (the first conjunct, U ⊆ args ( V ) U \subseteq\operatorname{args}(V) U ⊆ args ( V ) ) and each result w w w in W W W is produced by a usable entry of V V V whose arguments are among U U U . We call this a self-contained call , because V V V supplies both the curried entries and the environments.
The relation D ⪰ k + 1 V D \mathrel{\succeq_{k+1}} V D ⪰ k + 1 V has four clauses, which use ⪰ k \mathrel{\succeq_k} ⪰ k at the recursive occurrences:
Usable entries
U a ↦ w ∈ V U_a \mapsto w \in V U a ↦ w ∈ V
U a ↦ w ∈ V ⟺ ∃ U e . ( U e ↦ ( U a ↦ w ) ) ∈ car ( V ) ∧ U e ≠ [ ] ∧ U e ⊆ cdr ( V ) U_a \mapsto w \in V \Longleftrightarrow\exists U_e.\ (U_e \mapsto(U_a \mapsto w)) \in\operatorname{car}(V) \land U_e \ne[\,] \land U_e \subseteq\operatorname{cdr}(V) U a ↦ w ∈ V ⟺ ∃ U e . ( U e ↦ ( U a ↦ w )) ∈ car ( V ) ∧ U e = [ ] ∧ U e ⊆ cdr ( V )
Arguments of usable entries
args ( V ) = D \operatorname{args}(V)=D args ( V ) = D
args ( V ) = { u ∣ U a ↦ w ∈ V , u ∈ U a } \operatorname{args}(V)=\{u \mid U_a \mapsto w \in V,\ u \in U_a\} args ( V ) = { u ∣ U a ↦ w ∈ V , u ∈ U a }
Self-contained calls
V ⋅ U ⇝ W V \mathbin{\cdot} U \leadsto W V ⋅ U ⇝ W
V ⋅ U ⇝ W ⟺ U ⊆ args ( V ) ∧ ( ∀ w ∈ W . ∃ U a . U a ↦ w ∈ V ∧ U a ≠ [ ] ∧ U a ⊆ U ) V \mathbin{\cdot} U \leadsto W \Longleftrightarrow U \subseteq\operatorname{args}(V) \land(\forall w \in W.\ \exists U_a.\ U_a \mapsto w \in V \land U_a \ne[\,] \land U_a \subseteq U) V ⋅ U ⇝ W ⟺ U ⊆ args ( V ) ∧ ( ∀ w ∈ W . ∃ U a . U a ↦ w ∈ V ∧ U a = [ ] ∧ U a ⊆ U )
The approximant logical relation
D ⊒ k V D ⊒ V D \mathrel{\sqsupseteq_k} V \qquad D \mathrel{\sqsupseteq} V D ⊒ k V D ⊒ V
D ⊒ 0 V ⟺ true D \mathrel{\sqsupseteq_0} V \Longleftrightarrow\text{true} D ⊒ 0 V ⟺ true
D ⊒ k + 1 V ⟺ ( ∀ c . c ∈ V ⇒ c ∈ D ) ∧ ( V ≠ [ ] ⇒ D ≠ ∅ ) ∧ ( ∀ U , W , E . V ⋅ U ⇝ W ∧ con ( E ) ∧ con ( U ) ∧ E ⊒ k U ⇒ ( D ∙ E ) ⊒ k W ) ∧ ( ∀ π , W . W ⊆ π ( V ) ⇒ π ( D ) ⊒ k W ) \begin{aligned}
D \mathrel{\sqsupseteq_{k+1}} V \Longleftrightarrow\;&(\forall c.\ c \in V \Rightarrow c \in D)\\
&\land(V \ne[\,] \Rightarrow D \ne\varnothing)\\
&\land(\forall U,W,E.\ V \mathbin{\cdot} U \leadsto W \land\operatorname{con}(E) \land\operatorname{con}(U) \land E \mathrel{\sqsupseteq_k} U \Rightarrow(D \mathbin{\bullet} E) \mathrel{\sqsupseteq_k} W)\\
&\land(\forall\pi,W.\ W \subseteq\pi(V) \Rightarrow\pi(D) \mathrel{\sqsupseteq_k} W)
\end{aligned} D ⊒ k + 1 V ⟺ ( ∀ c . c ∈ V ⇒ c ∈ D ) ∧ ( V = [ ] ⇒ D = ∅ ) ∧ ( ∀ U , W , E . V ⋅ U ⇝ W ∧ con ( E ) ∧ con ( U ) ∧ E ⊒ k U ⇒ ( D ∙ E ) ⊒ k W ) ∧ ( ∀ π , W . W ⊆ π ( V ) ⇒ π ( D ) ⊒ k W )
D ⊒ V ⟺ D ⊒ 1 + depth ( V ) V D \mathrel{\sqsupseteq} V \Longleftrightarrow D \mathrel{\sqsupseteq_{1+\operatorname{depth}(V)}} V D ⊒ V ⟺ D ⊒ 1 + depth ( V ) V
every constant in V V V is in D D D ;
if V V V is nonempty, then D D D is nonempty;
whenever V V V maps U U U to W W W and E E E is a consistent set related to U U U , the set D ∙ E D \mathbin{\bullet} E D ∙ E is related to W W W ; and
every sublist of a projection of V V V is related to the same projection of D D D .
Appendix § 14 works clause 3 through an example.
Termination of the Approximant Logical Relation. The relation D ⊒ V D \mathrel{\sqsupseteq} V D ⊒ V is defined by recursion on the depth of V V V . Every recursive occurrence, including the negative one at the arguments of a call, is at a list shallower than V V V . The arguments U U U are shallower than V V V because the first conjunct of a self-contained call requires U ⊆ args ( V ) U \subseteq\operatorname{args}(V) U ⊆ args ( V ) ; without that conjunct, U U U could contain elements deeper than V V V . The results W W W are shallower than V V V because each of them is the output of an entry in V V V . Finally, the projections of V V V are shallower than V V V because they consist of components of the elements of V V V .
The index k k k of ⊒ k \mathrel{\sqsupseteq_k} ⊒ k serves only to ensure termination. Any index that bounds the depth of V V V gives the same relation, and therefore D ⊒ V D \mathrel{\sqsupseteq} V D ⊒ V is equivalent to the four clauses with ⊒ \mathrel{\sqsupseteq} ⊒ itself at the recursive occurrences (Lemma 4.4), and the proofs never mention the index.
Lemma 4.4 (Depth irrelevance and unfolding).
If every element in V V V has depth less than k k k and less than j j j , then D ⊒ k V D \mathrel{\sqsupseteq_k} V D ⊒ k V if and only if D ⊒ j V D \mathrel{\sqsupseteq_j} V D ⊒ j V .
D ⊒ V D \mathrel{\sqsupseteq} V D ⊒ V if and only if the four clauses of D ⊒ k + 1 V D \mathrel{\sqsupseteq_{k+1}} V D ⊒ k + 1 V hold with ⊒ \mathrel{\sqsupseteq} ⊒ in place of ⊒ k \mathrel{\sqsupseteq_k} ⊒ k .
The relation is also monotone in D D D and antitone in V V V , that is, enlarging the source set or taking a sublist of the target list preserves it.
The fundamental lemma. A source environment ρ \rho ρ and a target environment ρ ′ \rho' ρ ′ are related, written ρ ⪰ ρ ′ \rho\succeq\rho' ρ ⪰ ρ ′ and read “ρ \rho ρ approximates ρ ′ \rho' ρ ′ ”, when ρ ( x ) ⪰ V \rho(x) \succeq V ρ ( x ) ⪰ V for every variable x x x and every V ⊆ ρ ′ ( x ) V \subseteq\rho'(x) V ⊆ ρ ′ ( x ) . We also require that both environments be consistent and that ρ \rho ρ map every variable to a nonempty set.
Lemma 4.5 (Fundamental lemma). Let M M M be a term of E n v C l o s \mathit{EnvClos} EnvClos . If ρ ⪰ ρ ′ \rho\succeq\rho' ρ ⪰ ρ ′ and V ⊆ ⟦ s p l i t ( M ) ⟧ ρ ′ V \subseteq\llbracket\mathit{split}(M)\rrbracket_{\rho'} V ⊆ [ [ split ( M ) ] ] ρ ′ , then ⟦ M ⟧ ρ ⪰ V \llbracket M\rrbracket_\rho\succeq V [ [ M ] ] ρ ⪰ V .
The proof is by induction on M M M . Throughout, the second part of Lemma 4.4 lets us pass between D ⪰ V D \succeq V D ⪰ V and its four clauses, with ⪰ \succeq ⪰ at the recursive occurrences. The case of a variable follows from ρ ⪰ ρ ′ \rho\succeq\rho' ρ ⪰ ρ ′ , and the cases of fold, unfold, tuples, injections, let, and case follow from the induction hypotheses for their subterms. Two cases carry the weight.
Case M = L K M = L\ K M = L K . Then s p l i t ( M ) = c a l l c s p l i t ( L ) s p l i t ( K ) \mathit{split}(M) = \mathop{\mathrm{call}}_c\ \mathit{split}(L)\ \mathit{split}(K) split ( M ) = call c split ( L ) split ( K ) , and we write W W W for the lemma’s list V V V , so W ⊆ ( car ( D ′ ) ∙ cdr ( D ′ ) ) ∙ E ′ W \subseteq(\operatorname{car}(D') \mathbin{\bullet} \operatorname{cdr}(D')) \mathbin{\bullet} E' W ⊆ ( car ( D ′ ) ∙ cdr ( D ′ )) ∙ E ′ for D ′ = ⟦ s p l i t ( L ) ⟧ ρ ′ D' = \llbracket\mathit{split}(L)\rrbracket_{\rho'} D ′ = [ [ split ( L ) ] ] ρ ′ and E ′ = ⟦ s p l i t ( K ) ⟧ ρ ′ E' = \llbracket\mathit{split}(K)\rrbracket_{\rho'} E ′ = [ [ split ( K ) ] ] ρ ′ . Each element of W W W is produced by a curried entry from car ( D ′ ) \operatorname{car}(D') car ( D ′ ) , finitely many environments from cdr ( D ′ ) \operatorname{cdr}(D') cdr ( D ′ ) , and finitely many arguments from E ′ E' E ′ . We collect the pairs of D ′ D' D ′ that supply these curried entries and environments into a finite list V ⊆ D ′ V \subseteq D' V ⊆ D ′ , and the arguments into a finite list U ⊆ E ′ U \subseteq E' U ⊆ E ′ . Then V V V maps U U U to W W W . The induction hypotheses give ⟦ L ⟧ ρ ⪰ V \llbracket L\rrbracket_\rho\succeq V [ [ L ] ] ρ ⪰ V and ⟦ K ⟧ ρ ⪰ U \llbracket K\rrbracket_\rho\succeq U [ [ K ] ] ρ ⪰ U , and both ⟦ K ⟧ ρ \llbracket K\rrbracket_\rho [ [ K ] ] ρ and U U U are consistent. Clause 3 for ⟦ L ⟧ ρ ⪰ V \llbracket L\rrbracket_\rho\succeq V [ [ L ] ] ρ ⪰ V , applied to this self-contained call with E = ⟦ K ⟧ ρ E = \llbracket K\rrbracket_\rho E = [ [ K ] ] ρ , gives ⟦ L ⟧ ρ ∙ ⟦ K ⟧ ρ ⪰ W \llbracket L\rrbracket_\rho\mathbin{\bullet} \llbracket K\rrbracket_\rho\succeq W [ [ L ] ] ρ ∙ [ [ K ] ] ρ ⪰ W . That is, ⟦ M ⟧ ρ ⪰ W \llbracket M\rrbracket_\rho\succeq W [ [ M ] ] ρ ⪰ W .
Case M = clos ( z , y . N ) K ⃗ M = \operatorname{clos}(z,y.N)\ \vec{K} M = clos ( z , y . N ) K . Then s p l i t ( M ) = pair ( code ( z , y . s p l i t ( N ) ) ) ( tuple s p l i t ( K ⃗ ) ) \mathit{split}(M) = \operatorname{pair}(\operatorname{code}(z,y.\mathit{split}(N)))(\operatorname{tuple}\mathit{split}(\vec{K})) split ( M ) = pair ( code ( z , y . split ( N ))) ( tuple split ( K )) , so V V V is a list of pairs whose environments lie in T ′ = T ( ⟦ s p l i t ( K ⃗ ) ⟧ ρ ′ ) T' = \mathcal{T}(\llbracket\mathit{split}(\vec{K})\rrbracket_{\rho'}) T ′ = T ( [ [ split ( K ) ] ] ρ ′ ) . Let D = ⟦ M ⟧ ρ D = \llbracket M\rrbracket_\rho D = [ [ M ] ] ρ and T = T ( ⟦ K ⃗ ⟧ ρ ) T = \mathcal{T}(\llbracket\vec{K}\rrbracket_\rho) T = T ( [ [ K ] ] ρ ) . We prove the four clauses of D ⪰ V D \succeq V D ⪰ V . Clauses 1 and 4 hold trivially because V V V contains only pairs.
For clause 2, suppose ⟨ u , t ′ ⟩ ∈ V \langle u,t'\rangle\in V ⟨ u , t ′ ⟩ ∈ V . Then t ′ ∈ T ′ t' \in T' t ′ ∈ T ′ , so v i ∈ ⟦ s p l i t ( K i ) ⟧ ρ ′ v_i \in\llbracket\mathit{split}(K_i)\rrbracket_{\rho'} v i ∈ [ [ split ( K i ) ] ] ρ ′ for some v i v_i v i at each i i i . The induction hypothesis for K i K_i K i , with ρ ⪰ ρ ′ \rho\succeq\rho' ρ ⪰ ρ ′ and [ v i ] ⊆ ⟦ s p l i t ( K i ) ⟧ ρ ′ [v_i] \subseteq\llbracket\mathit{split}(K_i)\rrbracket_{\rho'} [ v i ] ⊆ [ [ split ( K i ) ] ] ρ ′ , gives ⟦ K i ⟧ ρ ⪰ [ v i ] \llbracket K_i\rrbracket_\rho\succeq[v_i] [ [ K i ] ] ρ ⪰ [ v i ] , and clause 2 of the ⪰ \succeq ⪰ relation makes ⟦ K i ⟧ ρ \llbracket K_i\rrbracket_\rho [ [ K i ] ] ρ nonempty. Hence t ∈ T t \in T t ∈ T for some t t t . Every Λ ( F ) \Lambda(F) Λ ( F ) contains ν \nu ν , so the curried function of the source closure contains the entry [ t ] ↦ ν [t] \mapsto\nu [ t ] ↦ ν , and applying that to t ∈ T t \in T t ∈ T gives ν ∈ D \nu\in D ν ∈ D .
Clause 3 is the heart of the proof. Suppose that V V V maps U U U to W W W and E ⪰ U E \succeq U E ⪰ U . The bodies of the source and target closures run in the environments ρ N = ρ 0 [ z : = T , y : = E ] \rho_N = \rho_0[z:=T,y:=E] ρ N = ρ 0 [ z := T , y := E ] and ρ N ′ = ρ 0 [ z : = cdr ( V ) , y : = U ] \rho'_N = \rho_0[z:=\operatorname{cdr}(V),y:=U] ρ N ′ = ρ 0 [ z := cdr ( V ) , y := U ] . We show the two premises of the induction hypothesis for the body N N N . First, W ⊆ ⟦ s p l i t ( N ) ⟧ ρ N ′ W \subseteq\llbracket\mathit{split}(N)\rrbracket_{\rho'_N} W ⊆ [ [ split ( N ) ] ] ρ N ′ . Each w ∈ W w \in W w ∈ W comes from a curried entry U e ↦ ( U a ↦ w ) U_e \mapsto(U_a \mapsto w) U e ↦ ( U a ↦ w ) in car ( V ) \operatorname{car}(V) car ( V ) with U e ⊆ cdr ( V ) U_e \subseteq\operatorname{cdr}(V) U e ⊆ cdr ( V ) and U a ⊆ U U_a \subseteq U U a ⊆ U , so w ∈ ⟦ s p l i t ( N ) ⟧ ρ 0 [ z : = U e , y : = U a ] ⊆ ⟦ s p l i t ( N ) ⟧ ρ N ′ w \in\llbracket\mathit{split}(N)\rrbracket_{\rho_0[z:=U_e,y:=U_a]} \subseteq\llbracket\mathit{split}(N)\rrbracket_{\rho'_N} w ∈ [ [ split ( N ) ] ] ρ 0 [ z := U e , y := U a ] ⊆ [ [ split ( N ) ] ] ρ N ′ by monotonicity (Lemma A.1). Second, ρ N ⪰ ρ N ′ \rho_N \succeq\rho'_N ρ N ⪰ ρ N ′ . We have T ⪰ cdr ( V ) T \succeq\operatorname{cdr}(V) T ⪰ cdr ( V ) by the induction hypothesis for K ⃗ \vec{K} K , because cdr ( V ) ⊆ T ′ \operatorname{cdr}(V) \subseteq T' cdr ( V ) ⊆ T ′ , and E ⪰ U E \succeq U E ⪰ U . With the two premises in hand, the induction hypothesis for N N N gives ⟦ N ⟧ ρ N ⪰ W \llbracket N\rrbracket_{\rho_N} \succeq W [ [ N ] ] ρ N ⪰ W . Finally, Lemma 3.1 yields ⟦ N ⟧ ρ N ⊆ D ∙ E \llbracket N\rrbracket_{\rho_N} \subseteq D \mathbin{\bullet} E [ [ N ] ] ρ N ⊆ D ∙ E , and ⪰ \succeq ⪰ is monotone in its source set, so D ∙ E ⪰ W D \mathbin{\bullet} E \succeq W D ∙ E ⪰ W .
Lemma 4.6 (Correctness of split closure). Let M M M be a closed term of E n v C l o s \mathit{EnvClos} EnvClos . For every constant c c c , c ∈ ⟦ M ⟧ c \in\llbracket M\rrbracket c ∈ [ [ M ] ] if and only if c ∈ ⟦ s p l i t ( M ) ⟧ c \in\llbracket\mathit{split}(M)\rrbracket c ∈ [ [ split ( M ) ] ] . Moreover, ⟦ M ⟧ ≠ ∅ \llbracket M\rrbracket\ne\varnothing [ [ M ] ] = ∅ if and only if ⟦ s p l i t ( M ) ⟧ ≠ ∅ \llbracket\mathit{split}(M)\rrbracket\ne\varnothing [ [ split ( M ) ] ] = ∅ .
Because ρ 0 ⪰ ρ 0 \rho_0 \succeq\rho_0 ρ 0 ⪰ ρ 0 , reflection follows from Lemma 4.5. If c ∈ ⟦ s p l i t ( M ) ⟧ c \in\llbracket\mathit{split}(M)\rrbracket c ∈ [ [ split ( M ) ] ] , the lemma applied to the one-element list [ c ] [c] [ c ] gives ⟦ M ⟧ ⪰ [ c ] \llbracket M\rrbracket\succeq[c] [ [ M ] ] ⪰ [ c ] , and clause 1 gives c ∈ ⟦ M ⟧ c \in\llbracket M\rrbracket c ∈ [ [ M ] ] . If ⟦ s p l i t ( M ) ⟧ \llbracket\mathit{split}(M)\rrbracket [ [ split ( M ) ] ] contains an element v v v , the lemma applied to [ v ] [v] [ v ] and clause 2 show that ⟦ M ⟧ \llbracket M\rrbracket [ [ M ] ] is nonempty. Preservation follows in the same way from the routine induction on M M M mentioned above, which shows that if v ∈ ⟦ M ⟧ v \in\llbracket M\rrbracket v ∈ [ [ M ] ] then v ⊑ ⟦ s p l i t ( M ) ⟧ ρ ′ v \sqsubseteq\llbracket\mathit{split}(M)\rrbracket_{\rho'} v ⊑ [ [ split ( M ) ] ] ρ ′ , for environments related pointwise by ⊑ \sqsubseteq ⊑ . For a constant, c ⊑ D ′ c \sqsubseteq D' c ⊑ D ′ means c ∈ D ′ c \in D' c ∈ D ′ , and v ⊑ D ′ v \sqsubseteq D' v ⊑ D ′ implies D ′ ≠ ∅ D' \ne\varnothing D ′ = ∅ .
Proof of Compiler Correctness with Scott Domains
We also proved the compiler correct with Scott domains (Appendix F.5). There a value is an element of a universal domain D \mathbb{D} D of tagged values, divergence is its least element ⊥ \bot ⊥ , and split closure is again the only pass that needs a relation. For space, this section only summarizes that proof; Appendix § 16 gives it in full. We define the relation by recursion on the stages D 0 , D 1 , … D_0, D_1, \ldots D 0 , D 1 , … of the bilimit that constructs D D D , as Reynolds [ [71 ] did. Let d d d be a source value and d ′ d' d ′ a target value, and write π n ∞ ( d ) ∈ D n \pi_n^{\infty}(d) \in D_n π n ∞ ( d ) ∈ D n for the approximation of d d d at stage n n n . For preservation, the stage relation π n ∞ ( d ) R n + d ′ \pi_n^{\infty}(d)\ R_n^{+}\ d' π n ∞ ( d ) R n + d ′ relates the approximation of d d d to the whole of d ′ d' d ′ . We write d ⊴ + d ′ d \unlhd^{+} d' d ⊴ + d ′ , read “d ′ d' d ′ preserves d d d ”, when it holds at every stage. For reflection, the roles swap, and d ′ ⊴ − d d' \unlhd^{-} d d ′ ⊴ − d , read “d d d reflects d ′ d' d ′ ”, holds when π n ∞ ( d ′ ) R n − d \pi_n^{\infty}(d')\ R_n^{-}\ d π n ∞ ( d ′ ) R n − d at every stage (Figure 7 in Appendix § 16 ). The clauses for closures are
Figure 7. The relations of the Scott-domain proof of split closure, between a source (EnvClos) value d d d and a target (PairClos) value d ′ d' d ′ . The superscript ± \pm ± stands for either direction. We read s R n ± w s \mathrel{R^{\pm}_{n}} w s R n ± w as “s s s and w w w are related at stage n n n ”, Pres ( d , d ′ ) \operatorname{Pres}(d,d') Pres ( d , d ′ ) as “d ′ d' d ′ preserves d d d ”, and Reflect ( d , d ′ ) \operatorname{Reflect}(d,d') Reflect ( d , d ′ ) as “d d d reflects d ′ d' d ′ ”. Here g ′ g' g ′ is the strict function that agrees with g g g on defined values.
( f u n , h ) M n + ( p r , ( c , e ) ) ⟺ ∀ v , a . v R n + a ⇒ h ( v ) R n + call ( c , e , a ) ( p r , ( c , e ) ) M m + 2 − ( f u n , g ) ⟺ ∀ v , a . v R m − a ⇒ call m ( ( c , e ) , v ) R m − g ′ ( a ) \begin{aligned}
(\mathsf{fun},h)\ M_n^{+}\ (\mathsf{pr},(c,e)) &\Longleftrightarrow\forall v,a.\ v\ R_n^{+}\ a \Rightarrow h(v)\ R_n^{+}\ \operatorname{call}(c,e,a) \\
(\mathsf{pr},(c,e))\ M_{m+2}^{-}\ (\mathsf{fun},g) &\Longleftrightarrow\forall v,a.\ v\ R_m^{-}\ a \Rightarrow\operatorname{call}_m((c,e),v)\ R_m^{-}\ g'(a)
\end{aligned} ( fun , h ) M n + ( pr , ( c , e )) ( pr , ( c , e )) M m + 2 − ( fun , g ) ⟺ ∀ v , a . v R n + a ⇒ h ( v ) R n + call ( c , e , a ) ⟺ ∀ v , a . v R m − a ⇒ call m (( c , e ) , v ) R m − g ′ ( a )
where call ( c , e , a ) \operatorname{call}(c,e,a) call ( c , e , a ) applies the curried function c c c to the environment e e e and then to the argument a a a . The preservation clause has the shape of the relation that Nielsen [ [57 ] used for defunctionalization. The reflection clause relates calls across stages, because a call of a pair at stage m + 2 m+2 m + 2 applies its curried function twice and shows its result only at stage m m m (call m \operatorname{call}_m call m ). Because the closure clause looks three levels into the target domain but only one into the source, the method of Pitts [ [63 ] does not apply directly. The result, Lemma G.5, is the counterpart of Lemma 4.6 and the key step in proving Theorem 4.2.
Adequacy
Each of our five correctness results speaks of a different semantics. As due diligence on our mechanized definitions, we prove the graph model of FPC adequate with respect to the clocked interpreters, the CEK machines, and the reduction semantics, and we prove the Scott-domain semantics of FPC adequate with respect to the clocked interpreters. Each proof adapts a proof from the literature [65 , 66 , 79 , 89 ] . Here is the statement of adequacy of the graph model for FPC with respect to the interpreter.
Proposition 5.1 (Adequacy). Let M M M be a closed term of FPC. For every constant c c c , c ∈ ⟦ M ⟧ c \in\llbracket M \rrbracket c ∈ [ [ M ] ] iff the interpreter of FPC terminates on M M M returning c c c , and ⟦ M ⟧ \llbracket M \rrbracket [ [ M ] ] is empty iff the interpreter diverges on M M M , that is, times out at every clock.
We have also proved that the graph model of I n s t r μ \mathrm{Instr}_{\mu} Instr μ is adequate with respect to the interpreter for I n s t r μ \mathrm{Instr}_{\mu} Instr μ . These adequacy results therefore cover every semantics in our comparison and make the five correctness results equivalent.
Evaluation: Comparison with Other Semantics
To compare the sizes of the proofs across the five styles (RQ2) and to explain the differences (RQ3), we proved the same compiler correct three more times using several styles of operational semantics. This section summarizes the five developments, compares the sizes of their proofs pass by pass, and explains the differences.
Setup
All five developments prove the correctness of the same eight-pass compiler (§ 2 ), from the same Agda definitions of the languages and passes, and each is our best effort in its style.
Graph model. The semantics of each language is the graph-model semantics of Appendix § 11 , and we prove each pass as in § 4 .
Scott domains. Meanings are continuous functions into a universal domain of tagged values (Appendix § 15 .§ 5 ), built as a bilimit with the domain theory of TypeTopology [Escardó 2026].
Abstract machines and simulations , in the style of CompCert [41 ] . Each language has a small-step CEK machine (Appendix § 15 .§ 3 ), and we prove each pass by a forward simulation. The backward direction follows from forward simulation and determinism.
Clocked interpreters and value relations , in the style of CakeML [38 ] . (Clocked interpreters go back to Boyer and Moore [ [12 ] .) Each language has a total interpreter with a clock that ticks each time the interpreted program calls a function (Appendix § 15 .§ 2 ), and we prove each pass by showing that if one interpreter returns a result at some clock, the other returns a related result at some clock.
Reduction semantics and simulations. We define a reduction semantics for each language, using substitution for function application (Appendix § 15 .§ 4 ), and we prove each pass by a simulation. We include reduction semantics in this comparison because of its central role in the theory of programming languages [62 , 90 ] .
The correctness results and their proofs. Each development proves the compiler correct with respect to its own semantics, for closed programs of integer type. The denotational theorems state that a program and its compilation produce the same integer and that one has a meaning exactly when the other does; the operational propositions state that both terminate with the same integer or both diverge, for three different operational semantics. We do not regard the operational or denotational formulation of correctness as more fundamental than the other. Scott and Strachey [ [77 ] proposed a mathematical semantics as “the standard against which to judge an implementation”, while Plotkin [ [65 ] judged denotational semantics by their closeness to an operational one. Our adequacy proofs (§ 5 ) make the choice immaterial. For programs of integer type, the five results are equivalent. We therefore count each development up to its own correctness result and report adequacy separately.
We measure each proof in lexical tokens of Agda (identifiers, operators, keywords, and delimiters, as Agda’s lexer sees them), not counting comments or imports. A count of lines depends on layout [60 ] , and a count of lexical tokens, essentially the program length of Halstead [1977], does not. The counts include the semantics of the languages, the proof of each pass, and the composition of the passes. They exclude the code that all five proofs use unchanged: the intrinsically typed syntax of the languages and the functions that implement the passes (17,929 lexical tokens). We report separately the language-independent infrastructure of each style.
AI Use. All of the Agda code of the five developments was written by Claude Code (Opus 5.5 and Sonnet 5.5) under our direction. Claude started from our earlier Agda mechanizations of graph models of the λ \lambda λ -calculus and ISWIM [39 ] , which included adequacy proofs and a partial proof of correctness for closure conversion. The results do not depend on trusting the AI. Agda checks every development in safe mode, with no postulates. However, the Scott-domain theorems take three standard axioms as hypotheses because it depends on the TypeTopology library. The authors checked all the definitions and every statement of the theorems and propositions.
Results
Table 1 gives the sizes of the proofs for each pass of the compiler, with one column per semantic style, with the totals in the last rows. The proof with clocked interpreters is the smallest, at 35,860 lexical tokens, followed by the graph model at 40,905, abstract machines at 50,631, and reduction semantics at 68,243. One pass decides the graph model’s place. The proof of split closure alone is 8,672, more than a fifth of the total, against 773 to 2,534 in the operational styles. Without split closure , the graph model would be the smallest, at 32,233 against 35,087 for the interpreters. The second denotational proof, with Scott domains, is 54,972, between the abstract machines and reduction semantics: 1.3 times the graph model, and larger in every row but the semantics of the languages. Counting the infrastructure (Table 2 ) changes the order. The interpreters remain the smallest at 42,401, followed by the machines at 57,582, the graph model at 68,890, reduction semantics at 88,456, and Scott domains at 149,416. For comparison, Benton et al. [ [7 ] report 4,400 lines of Coq for their domain theory, against 6,327 lines for TypeTopology’s.
Table 2. Lexical tokens of Agda in the infrastructure that each proof relies on, which Table 1 excludes. For Scott domains, TypeTopology’s domain theory is the modules on dcpos and lifting that the proof imports. Not counted: the Agda standard library and TypeTopology’s general foundations (224,533 lexical tokens).
Analysis
We identify two tendencies in the proof sizes of Table 1 (RQ3). The size of a denotational proof tends to depend most on whether the pass preserves the meaning of a program exactly. When it does, the proof is an induction whose cases are congruences and a few laws of the model (§ 4.1 ), largely independent of how the pass changes the way the program runs. The size of an operational proof tends to depend instead on how closely the run of the target follows the run of the source. On the denotational side, the correctness of seven of the eight passes is an equation between the meaning of a program and the meaning of its translation; only split closure is the exception. On the operational side, the runs of the source and target correspond closely for most passes, including split closure , but differ more for simplify and, to a lesser degree, hoist . Combining the two tendencies gives three groups of passes, which we discuss in turn.
Equal meanings, similar runs: the interpreter proofs tend to be smaller. For enclose , remove complex operands , lift locals , explicate control , and select instructions , both the graph-model and the interpreter proofs are short. The interpreter proofs are smaller, but by only 7% for enclose and 10% for explicate control . The Scott-domain proofs of these passes are 1.3 to 3.0 times the interpreter proofs, for the reasons given at the end of this section. The congruence cases take one line per construct in both styles. The difference lies in the obligations of the graph model. Because a term denotes a set of values, some operators require their sets to be nonempty or consistent, and some laws have premises, such as the β \beta β -law of Lemma 3.1 and the commuting conversions that move a let or case past a continuation. An interpreter computes a single value, so these obligations do not arise. These obligations cost the most in the graph-model proof of select instructions , whose function prologue needs two uses of Lemma 3.1 , and which is about twice the interpreter proof. The commuting conversion that the continuation-passing semantics of Imp needs makes the graph-model proof of lift locals 1.2 times the interpreter proof. The tendency concerns the interpreters only. The proofs in the reduction semantics, which works by substitution, are often several times larger even for these passes, as for enclose and lift locals .
Equal meanings, different runs: the denotational proofs tend to be smallest. simplify is the clearest case, at 7,044 lexical tokens in the graph model and 9,393 with Scott domains, against 11,339 to 14,589 for the operational proofs. This pass reduces expression under binders and inlines calls, so the target takes fewer steps than the source. Each operational proof must replay those reductions as steps of the source program: on open terms in the reduction semantics, or as a run-ahead of the source machine or interpreter, with stuttering where the target does more. For the interpreters, the target needs fewer clock ticks than the source, which breaks the usual clock-for-clock argument. The hoist pass fits the pattern less cleanly, at 2,796 in the graph model and 3,282 with Scott domains, against 3,387 to 4,740 for the operational proofs. The interpreter proof is clock for clock; the runs differ only in where the code of a function lives, because in the target it is a reference into a table of definitions. The interpreter proof relates the two tables value by value, while the denotational proof shows that the meaning of a reference equals the meaning of the code it replaces.
Changed representations: the operational proofs are much smaller. Split closure changes the representation of closures, so its correctness is not an equation, yet the run of the target follows the run of the source closely. Its denotational proofs take 8,672 lexical tokens in the graph model and 15,211 with Scott domains, against 773 to 2,534 in the operational styles. For the graph model, most of the difference has three sources. First, the denotational proof needs two directions, while a simulation proof gets the backward direction for free from forward simulation and the determinism of the target. The graph model has an analogue of determinism, the consistency of denotations, and it does part of the job. If the source produces a constant, the forward direction puts that constant in the target, and consistency rules out any other. But the graph model represents divergence as the empty set, the absence of evidence, and a forward relation cannot carry absence. Showing that a diverging program compiles to a diverging program is therefore a separate argument, and that argument is reflection. Second, reflection needs the finite lists, the self-contained calls, and the lemma that extracts them from a target call. Third, the β \beta β -law of Lemma 3.1 holds only under side conditions of nonemptiness, consistency, and continuity, which a machine does not need. The Scott-domain proof shares the first source, and its stage relations replace the other two with the costs discussed next.
The graph model is smaller than Scott domains. The two denotational proofs have the same structure, yet the Scott-domain proof is 1.3 times the graph model, larger in every row but the semantics: 1.05 to 1.8 times for the equational passes, and 15,211 lexical tokens against 8,672 for split closure . Three properties of Scott domains explain much of the difference. First, a Scott domain has infinite elements, so the relation for split closure is built by stages (§ 4.3 ). The proof shows that each stage is a proposition, downward closed, admissible, and preserved by maps between stages. Second, the relation for a call at one stage refers to an earlier stage, and relating calls across stages is the most expensive part of the proof. Third, the universal domain is a sum of tagged elements. The primitives and the tags need their own lemmas, and the shared lemmas are mostly laws of strictness and definedness, which replace nonemptiness. In exchange, the proof needs no consistency. The graph-model relations recurse on finite elements, and neither of the first two costs arises. The infrastructure widens the gap further (Table 2 ).
Threats to Validity
We measure the size of the proofs, which is only a proxy for the effort of writing, checking, and maintaining them. Lexical tokens, lines, and non-whitespace characters rank the totals alike, but lines depend on layout. Size also says little about difficulty, which depends on whether a proof has precedent in the literature or its authors must invent it. Because experience carries over from one development to the next, we gave every development a final cleanup pass under the same rules (shorten and clarify the proofs but change no statement and move nothing into the uncounted infrastructure). The cleanup did not change the order of the four other proofs; the Scott-domain proof, written last, shrank by more than a quarter, moving it ahead of reduction semantics.
The comparison covers one compiler, and the totals depend on its selection of passes. The denotational proofs tend to be smaller for optimizations such as simplify and several times larger for passes that change the representation of values such as split closure , so a compiler with more of one kind or the other could rank the styles differently. The sizes also depend on choices of semantics, for example the continuation-passing semantics of Imp, which makes explicate control an equation, and on proof engineering that could still shrink any of the five developments. Like our graph model, the Scott-domain semantics uses one universal domain of tagged values, as Milner [ [46 ] and MacQueen et al. [ [43 ] did. A domain for each type, more usual for typed languages [9 ] (Dockins 2014), would avoid the case analysis on tags, a tenth of the Scott-domain proof of split closure , but would need a domain equation for each recursive type.
The sizes also depend on the proof assistant. Agda’s main automation is normalization, which probably favors the interpreters, whose results it computes directly; with the tactics of Rocq or Lean, the relative sizes of the five styles could differ.
Related Work
The introduction discusses the work closest to ours. This section covers compiler proofs by semantic style, operational proofs of closure conversion, relations between denotations, and graph models.
Semantic styles in compiler correctness. The idea that a compiler is correct when a diagram of meanings commutes goes back to Morris [ [50 ] and Thatcher et al. [ [85 ] , and Milne and Strachey [ [45 ] related semantics at different levels of abstraction with inclusive predicates. Among proofs of particular compilers, those of Pardo et al. [ [59 ] and Siek [ [79 ] are denotational for both the source and the target of each pass. Pardo et al. [ [59 ] give clocked denotations to a first-order imperative language and a stack language, and Siek [ [79 ] proves an inliner correct by equality of graph-model denotations. Other proofs mix the styles, with a denotational semantics on only one side. Benton and Hur [ [8 ] relate a domain-theoretic semantics of a typed language with recursion to an operational SCE machine, using step-indexed biorthogonal relations; the target machine has primitive closures. Mullen et al. [52 ] gives a denotation only to its total source language and proves every pass operationally. Most compiler proofs, finally, are operational on both sides. The CLI stack Moore [49 ] ; Young [92 ] runs the source for n n n steps given by a clock function, and shows that the target reproduces every step of the source that ends without error, but says nothing about divergence. CompCert Leroy [41 ] and CakeML [38 ] , discussed in the introduction, use simulations and clocked big-step semantics, respectively. The calculated compilers of Bahr and Hutton [ [4 ] derive a compiler and its virtual machine from a definitional interpreter; the virtual machine keeps closures as values. Our back end adapts the operational proofs of Siek [ [80 ] for a first-order compiler.
Operational proofs of closure conversion. Typed closure conversion represents a closure as a package of an existential type, as our PairClos does. Minamide et al. [1996] prove it correct with logical relations between source and target terms, Ahmed and Blume [ [3 ] show that it preserves observational equivalence, and Bowman and Ahmed [ [11 ] extend it to the calculus of constructions. Dargaye [ [19 ] and Hur and Dreyer [2011] prove closure conversion correct within compilers for core ML. Pottier and Gauthier [ [69 ] prove that defunctionalization preserves convergence and divergence with a simulation. The simulation proof of Wand and Steckler [ [87 ] covers only terminating programs, and Neis et al. [ [55 ] use parametric inter-language simulations. Ghica and Zanasi [ [30 ] present closure conversion for the λ \lambda λ -calculus with string diagrams and show that a closure-converted β \beta β -redex reduces to the same result as the original. That lemma would be the main case of an operational proof by simulation.
Categorical accounts of closure conversion. Schweimeier and Jeffrey [ [73 ] justify closure conversion for an ML-like language with references and recursive functions with a categorical model presented as flow graphs. They prove that every program is equal, by the equations of these categories, to a program whose functions are all at top level. Their closures keep a function type, with the code and the environment inseparable, so the target is still higher-order, and they place the transformation “part-way between closure conversion and lambda-lifting.” Their equations hold in every model of these categories. A statement about divergence therefore requires a particular model that is adequate for the execution of programs, a step the paper leaves open. In a blog post, New [ [56 ] gives a categorical account of the parametricity argument of Ahmed and Blume [ [3 ] , deriving the isomorphism between A → B A \to B A → B and ∃ Γ . Γ × ( Γ × A → B ) \exists\Gamma.\ \Gamma\times(\Gamma\times A \to B) ∃Γ. Γ × ( Γ × A → B ) from the coYoneda lemma.
Relations between denotations. Relating two denotational semantics is an old problem. Our Scott-domain relation for split closure (§ 4.3 ) follows Reynolds [ [71 ] , who related the direct and continuation semantics of a language by a relation built stage by stage along the inverse-limit construction of the domains [75 ] . His index ranges over the stages of both domains, and his relation is the intersection over all stages. Ours recurses on the stages of one side only and relates each approximation to the whole other value. The approximant logical relation (§ 4.2 ) has a similar shape, but its index bounds the depth of finite elements of the target side, and depth irrelevance (Lemma 4.4) makes an intersection unnecessary. Nielsen [ [57 ] relates the denotational meanings of a program and its defunctionalization with a type-indexed relation whose function clause, like that of our preservation relation, relates a function to a closure whose calls give related results. His source has no recursive types, so recursion on types suffices, and he proves only preservation. Later work related two semantics with inclusive predicates [45 , 83 ] , finitary projections [53 ] , or types [27 , 44 ] . Pitts [ [63 ] showed that a relation specified by a mixed-variance equation over a recursively defined domain exists and is unique, but his method does not apply directly to split closure (§ 4.3 ).
Graph models and consistency. Our model descends from the graph models of [76 , 23 , 67 ] and is related to the filter models of [6 ] . Our consistency is pairwise, as in the coherent domains of Plotkin [ [64 ] , and our rule for entries is the function-space consistency of information systems [40 , 78 ] in the pairwise form of Schwichtenberg [ [74 ] .
Conclusion
Our main results are denotational proofs, one for a graph model and another for Scott domains, that closure conversion preserves and reflects the meaning of programs, divergence included, for a Turing-complete language. The proof of semantic reflection for the graph model rests on our new approximant logical relation . We extend the proofs to an eight-pass compiler and mechanize them in Agda. Comparing the size of these proofs to operational proofs of the same compiler shows a split by the kind of pass. Where a pass keeps the representation of values, its denotational proof is an equation between meanings, and the graph-model proofs are smaller than the machine and reduction proofs. For the source optimizer, the graph-model proof is the smallest of the five. Where a pass changes that representation, as split closure does, proving semantic reflection requires a relation between denotations, and the operational proofs, which follow the run of the target and get reflection from determinism, are several times smaller. Not counting infrastructure, the clocked interpreters give the smallest proof, the graph model the second, and Scott domains the fourth.
Data-Availability Statement
The artifact is our Agda development: the five correctness proofs of the compiler, the adequacy proofs of § 5 , the shared definitions of the languages and passes, and the script that computes the proof sizes of § 6 . It checks with Agda 2.8 and its standard library 2.4 in safe mode, with no postulates, in about two minutes from scratch on a desktop workstation (Intel Core Ultra 9 285K, 64 GB). The Scott-domain proofs also need the modules of TypeTopology that it imports, which the artifact includes, lightly patched, from a pinned commit. With them, the Scott-domain proofs check in safe mode in about six and a half more minutes, and its theorems take propositional truncation, function extensionality, and propositional extensionality as hypotheses. A Dockerfile builds and checks everything. The README of the artifact lists the commands that check each development and regenerate Table 1 and Table 2 , and it maps each claim of the paper to the file that states it. An anonymized version is provided as supplementary material, and we will submit it for Artifact Evaluation.
References
The appendices complete the definitions of the language semantics and compiler passes used in the paper. Appendix A completes the graph model of § 3 ; Appendix B gives the type system and graph-model semantics of each language; Appendix C defines the compiler passes of § 2 ; Appendix D expands the equational proofs in § 4.1 to more compiler passes; Appendix E works through an example of the approximant logical relation of § 4.2 ; and Appendix F gives the semantics of the source and target languages in the four other semantic styles of § 6 , with their correctness results; and Appendix G gives the Scott-domain proof of split closure . Each section begins with a line, marked “Agda:”, that names the modules of the artifact that contain the section’s definitions and proofs, with paths relative to its agda/ directory.
The Graph Model
This appendix completes the definition of the graph model of § 3 : consistency, which Λ \Lambda Λ and C C C require of their inputs and the proof of split closure relies on; the depth of an element, on which the approximant logical relation recurses; and the operators of the semantics not shown in Figure 3 .
Consistency
Agda: Framework/GraphModel/Domain.agda, proof/Framework/GraphModel/Domain.agda
Consistency is defined on elements, and from it on sets and lists (§ 3 ). Two constants are consistent when they are equal, two entries are consistent when consistent inputs lead to consistent outputs, and elements that describe different kinds of values, such as left ( u ) \operatorname{left}(u) left ( u ) and right ( v ) \operatorname{right}(v) right ( v ) , are not consistent. A set D D D is consistent, written con ( D ) \operatorname{con}(D) con ( D ) , when its elements are pairwise consistent, and two lists are consistent with each other, written V ≈ V ′ V \approx V' V ≈ V ′ , when every element of one is consistent with every element of the other.
Consistency of elements
c ∼ c ′ ⟺ c = c ′ c \sim c' \Longleftrightarrow c = c' c ∼ c ′ ⟺ c = c ′
( V ↦ w ) ∼ ( V ′ ↦ w ′ ) ⟺ ( V ≈ V ′ ⇒ w ∼ w ′ ) (V \mapsto w) \sim(V' \mapsto w') \Longleftrightarrow(V \approx V' \Rightarrow w \sim w') ( V ↦ w ) ∼ ( V ′ ↦ w ′ ) ⟺ ( V ≈ V ′ ⇒ w ∼ w ′ )
( V ↦ w ) ∼ ν , ν ∼ ν (V \mapsto w) \sim\nu,\qquad\nu\sim\nu ( V ↦ w ) ∼ ν , ν ∼ ν
⟨ u , v ⟩ ∼ ⟨ u ′ , v ′ ⟩ ⟺ u ∼ u ′ ∧ v ∼ v ′ \langle u,v\rangle\sim\langle u',v'\rangle\Longleftrightarrow u \sim u' \land v \sim v' ⟨ u , v ⟩ ∼ ⟨ u ′ , v ′ ⟩ ⟺ u ∼ u ′ ∧ v ∼ v ′
tup i ( v ) ∼ tup j ( v ′ ) ⟺ i = j ⇒ v ∼ v ′ \operatorname{tup}_{i}(v) \sim\operatorname{tup}_{j}(v') \Longleftrightarrow i = j \Rightarrow v \sim v' tup i ( v ) ∼ tup j ( v ′ ) ⟺ i = j ⇒ v ∼ v ′
left ( v ) ∼ left ( v ′ ) ⟺ v ∼ v ′ \operatorname{left}(v) \sim\operatorname{left}(v') \Longleftrightarrow v \sim v' left ( v ) ∼ left ( v ′ ) ⟺ v ∼ v ′
right ( v ) ∼ right ( v ′ ) ⟺ v ∼ v ′ \operatorname{right}(v) \sim\operatorname{right}(v') \Longleftrightarrow v \sim v' right ( v ) ∼ right ( v ′ ) ⟺ v ∼ v ′
and no other pairs of elements are consistent.
Consistency of sets and lists
con ( D ) ⟺ u ∼ v for all u , v ∈ D \operatorname{con}(D) \Longleftrightarrow u \sim v\ \text{for all }u,v \in D con ( D ) ⟺ u ∼ v for all u , v ∈ D
V ≈ V ′ ⟺ u ∼ v for all u ∈ V and v ∈ V ′ V \approx V' \Longleftrightarrow u \sim v\ \text{for all }u \in V\ \text{and }v \in V' V ≈ V ′ ⟺ u ∼ v for all u ∈ V and v ∈ V ′
Fig. 6. Consistency of elements, sets, and lists.
Depth
Agda: proof/Framework/GraphModel/DomainUtilBase.agda
The approximant logical relation of § 4.2 is defined by recursion on the depth of a list of elements. The depth of an element is the height of its syntax tree, not counting constants, ν \nu ν and ⟨ ⟩ \langle\rangle ⟨ ⟩ ; the depth of a finite list is the largest depth of its elements (zero for the empty list):
depth ( V ↦ w ) = 1 + max ( depth ( V ) , depth ( w ) ) depth ( ⟨ u , v ⟩ ) = 1 + max ( depth ( u ) , depth ( v ) ) depth ( t ( v ) ) = 1 + depth ( v ) for t ∈ { tup i , left , right } depth ( v ) = 0 for v ∈ { c , ν , ⟨ ⟩ } depth ( V ) = max v ∈ V depth ( v ) \begin{aligned}
\operatorname{depth}(V \mapsto w) &= 1 + \max(\operatorname{depth}(V),\operatorname{depth}(w))\\
\operatorname{depth}(\langle u,v\rangle) &= 1 + \max(\operatorname{depth}(u),\operatorname{depth}(v))\\
\operatorname{depth}(t(v)) &= 1 + \operatorname{depth}(v) \quad\text{for } t \in\{\operatorname{tup}_i,\operatorname{left},\operatorname{right}\}\\
\operatorname{depth}(v) &= 0 \quad\text{for } v \in\{c,\nu,\langle\rangle\}\\
\operatorname{depth}(V) &= \max_{v\in V}\operatorname{depth}(v)
\end{aligned} depth ( V ↦ w ) depth (⟨ u , v ⟩) depth ( t ( v )) depth ( v ) depth ( V ) = 1 + max ( depth ( V ) , depth ( w )) = 1 + max ( depth ( u ) , depth ( v )) = 1 + depth ( v ) for t ∈ { tup i , left , right } = 0 for v ∈ { c , ν , ⟨ ⟩} = v ∈ V max depth ( v )
Further Operators
Agda: Framework/GraphModel/Ops.agda, Semantics/Graph/Prim.agda
Besides the operators of Figure 3 , the semantics of the languages use the following ones. The empty tuple means T ( ⟨ ⟩ ) = { ⟨ ⟩ } T(\langle\rangle)=\{\langle\rangle\} T (⟨ ⟩) = {⟨ ⟩} . A guard makes a set strict in other sets:
guard ( D 1 , … , D n ; E ) = { E if D 1 , … , D n are nonempty , ∅ otherwise . \operatorname{guard}(D_1,\ldots,D_n;E)=
\begin{cases}
E & \text{if } D_1,\ldots,D_n \text{ are nonempty},\\
\varnothing& \text{otherwise}.
\end{cases} guard ( D 1 , … , D n ; E ) = { E ∅ if D 1 , … , D n are nonempty , otherwise .
Monotonicity
Agda: FPC/proof/Semantics/GraphModel/{FPC,EnvClos,PairClos}.agda
Lemma A.1 (Monotonicity). Let M M M be a term of FPC, EnvClos, or PairClos. If ρ ( x ) ⊆ ρ ′ ( x ) \rho(x) \subseteq\rho'(x) ρ ( x ) ⊆ ρ ′ ( x ) for every variable x x x , then ⟦ M ⟧ ρ ⊆ ⟦ M ⟧ ρ ′ \llbracket M\rrbracket_\rho\subseteq\llbracket M\rrbracket_{\rho'} [ [ M ] ] ρ ⊆ [ [ M ] ] ρ ′ .
The Languages
This appendix gives, for each language of § 2 in the order of the pipeline, its type system and its semantics in the graph model. In this Appendix, we write variables by name. In the Agda development, we instead write variables as de Bruijn indices, and every term is well scoped by construction. The type systems are those of our intrinsically typed Agda definitions, so every pass maps well-typed terms to well-typed terms. The semantics are the ones that Theorem 4.1 and the proofs of § 4 speak of. Figure 2 lists the base types. The result type out ( p ) \operatorname{out}(p) out ( p ) of a primitive is Int for sub and tup() + tup() for eq.
In the semantics, an environment ρ \rho ρ maps each variable to a set of elements, and ρ [ x : = X ] \rho[x:=X] ρ [ x := X ] maps x x x to X X X and every other variable as ρ \rho ρ does. Every position of an environment must be nonempty, so the environment ρ 0 \rho_0 ρ 0 of a closed program maps every variable to { ⟨ ⟩ } \{\langle\rangle\} {⟨ ⟩} , and so do the positions of an environment that hold no variable. The meaning of a closed program M M M is ⟦ M ⟧ = ⟦ M ⟧ ρ 0 \llbracket M\rrbracket=\llbracket M\rrbracket_{\rho_0} [ [ M ] ] = [ [ M ] ] ρ 0 . The operators are those of Figure 3 and Appendix § 10 .§ 3 .
The FPC Language
Agda: FPC/Language/FPC.agda, FPC/Semantics/GraphModel/FPC.agda
FPC is the source language, and the first pass, simplify , maps it to itself. A judgment Γ ⊢ M : A \Gamma\vdash M:A Γ ⊢ M : A types a term in a context Γ \Gamma Γ of variable types.
Typing of FPC Γ ⊢ M : A \Gamma\vdash M:A Γ ⊢ M : A
x : A ∈ Γ Γ ⊢ x : A Γ , x : A ⊢ N : C Γ ⊢ λ x . N : A → C Γ ⊢ L : A → C Γ ⊢ M : A Γ ⊢ L M : C c a constant of base type B Γ ⊢ c : B Γ ⊢ M i : A i Γ ⊢ tuple M ⃗ : tup ( A ⃗ ) Γ ⊢ M : tup ( A ⃗ ) 1 ≤ i ≤ ∣ A ⃗ ∣ Γ ⊢ get i M : A i Γ ⊢ M : A Γ ⊢ inl M : A + C Γ ⊢ M : C Γ ⊢ inr M : A + C Γ ⊢ L : A + C Γ , x : A ⊢ M : D Γ , y : C ⊢ N : D Γ ⊢ case L ( x . M ) ( y . N ) : D Γ ⊢ L : Int Γ ⊢ M : Int Γ ⊢ prim p L M : out ( p ) Γ ⊢ M : A Γ , x : A ⊢ N : C Γ ⊢ let x = M in N : C Γ ⊢ M : A [ μ X . A / X ] Γ ⊢ fold M : μ X . A Γ ⊢ M : μ X . A Γ ⊢ unfold M : A [ μ X . A / X ] \begin{aligned}
&\frac{x:A\in\Gamma}{\Gamma\vdash x:A}
&&\frac{\Gamma,x:A\vdash N:C}{\Gamma\vdash\lambda x.N:A\to C}
&&\frac{\Gamma\vdash L:A\to C\quad\Gamma\vdash M:A}{\Gamma\vdash LM:C}
&&\frac{c\text{ a constant of base type }B}{\Gamma\vdash c:B}\\
&\frac{\Gamma\vdash M_i:A_i}{\Gamma\vdash\operatorname{tuple}\ \vec{M}:\operatorname{tup}(\vec{A})}
&&\frac{\Gamma\vdash M:\operatorname{tup}(\vec{A})\quad1\leq i\leq|\vec{A}|}{\Gamma\vdash\operatorname{get}_i M:A_i}\\
&\frac{\Gamma\vdash M:A}{\Gamma\vdash\operatorname{inl}M:A+C}
&&\frac{\Gamma\vdash M:C}{\Gamma\vdash\operatorname{inr}M:A+C}
&&\frac{\Gamma\vdash L:A+C\quad\Gamma,x:A\vdash M:D\quad\Gamma,y:C\vdash N:D}{\Gamma\vdash\operatorname{case}\ L\ (x.M)\ (y.N):D}\\
&\frac{\Gamma\vdash L:\operatorname{Int}\quad\Gamma\vdash M:\operatorname{Int}}{\Gamma\vdash\operatorname{prim}_p\ L\ M:\operatorname{out}(p)}
&&\frac{\Gamma\vdash M:A\quad\Gamma,x:A\vdash N:C}{\Gamma\vdash\operatorname{let}\ x=M\ \operatorname{in}\ N:C}\\
&\frac{\Gamma\vdash M:A[\mu X.A/X]}{\Gamma\vdash\operatorname{fold}\ M:\mu X.A}
&&\frac{\Gamma\vdash M:\mu X.A}{\Gamma\vdash\operatorname{unfold}\ M:A[\mu X.A/X]}
\end{aligned} Γ ⊢ x : A x : A ∈ Γ Γ ⊢ tuple M : tup ( A ) Γ ⊢ M i : A i Γ ⊢ inl M : A + C Γ ⊢ M : A Γ ⊢ prim p L M : out ( p ) Γ ⊢ L : Int Γ ⊢ M : Int Γ ⊢ fold M : μ X . A Γ ⊢ M : A [ μ X . A / X ] Γ ⊢ λ x . N : A → C Γ , x : A ⊢ N : C Γ ⊢ get i M : A i Γ ⊢ M : tup ( A ) 1 ≤ i ≤ ∣ A ∣ Γ ⊢ inr M : A + C Γ ⊢ M : C Γ ⊢ let x = M in N : C Γ ⊢ M : A Γ , x : A ⊢ N : C Γ ⊢ unfold M : A [ μ X . A / X ] Γ ⊢ M : μ X . A Γ ⊢ L M : C Γ ⊢ L : A → C Γ ⊢ M : A Γ ⊢ case L ( x . M ) ( y . N ) : D Γ ⊢ L : A + C Γ , x : A ⊢ M : D Γ , y : C ⊢ N : D Γ ⊢ c : B c a constant of base type B
Figure 3 gives the semantics of FPC.
The Clos and EnvClos Languages
Agda: FPC/Language/Clos.agda, FPC/Language/EnvClos.agda, FPC/Semantics/GraphModel/Clos.agda, FPC/Semantics/GraphModel/EnvClos.agda
Clos is the intermediate language between the steps of enclose , whose closures bind a name for each captured value. EnvClos, the target of enclose and the source of split closure , stores the captured values of a closure in one tuple. Both type and interpret their other forms as FPC does. A closure’s type does not show the types Δ = A 1 , … , A n \Delta=A_1,\ldots,A_n Δ = A 1 , … , A n of its captured values.
Typing of closures
Γ ⊢ M i : A i x 1 : A 1 , … , x n : A n , y : A ⊢ N : C Γ ⊢ clos ( x ⃗ , y . N ) M ⃗ : A → C ( C l o s ) \frac{\Gamma\vdash M_i:A_i}{\frac{x_1:A_1,\ldots,x_n:A_n,y:A\vdash N:C}{\Gamma\vdash\operatorname{clos}(\vec{x},y.N)\ \vec{M}:A\to C}\ (\mathrm{Clos})} Γ ⊢ clos ( x , y . N ) M : A → C x 1 : A 1 , … , x n : A n , y : A ⊢ N : C ( Clos ) Γ ⊢ M i : A i
Γ ⊢ M i : A i z : tup ( Δ ) , y : A ⊢ N : C Γ ⊢ clos ( z , y . N ) M ⃗ : A → C ( E n v C l o s ) \frac{\Gamma\vdash M_i:A_i}{\frac{z:\operatorname{tup}(\Delta),y:A\vdash N:C}{\Gamma\vdash\operatorname{clos}(z,y.N)\ \vec{M}:A\to C}\ (\mathrm{EnvClos})} Γ ⊢ clos ( z , y . N ) M : A → C z : tup ( Δ ) , y : A ⊢ N : C ( EnvClos ) Γ ⊢ M i : A i
Below, ⟦ M ⃗ ⟧ ρ \llbracket\vec{M}\rrbracket_\rho [ [ M ] ] ρ stands for ⟦ M 1 ⟧ ρ , … , ⟦ M n ⟧ ρ \llbracket M_1\rrbracket_\rho,\ldots,\llbracket M_n\rrbracket_\rho [ [ M 1 ] ] ρ , … , [ [ M n ] ] ρ . A Clos closure is the function of its parameter y y y whose code runs with the captured values for x 1 , … , x n x_1,\ldots,x_n x 1 , … , x n ; closures are strict, so it is guarded by the captured values. An EnvClos closure applies the curried meaning of its code to the tuple of the captured values (§ 4.2 ).
Semantics of closures
⟦ M ⟧ ρ = D \llbracket M\rrbracket_\rho=D [ [ M ] ] ρ = D
⟦ clos ( x ⃗ , y . N ) M ⃗ ⟧ ρ = guard ( ⟦ M 1 ⟧ ρ , … , ⟦ M n ⟧ ρ ; Λ ( λ Y . ⟦ N ⟧ ρ 0 [ x ⃗ : = ⟦ M ⃗ ⟧ ρ , y : = Y ] ) ) ( C l o s ) \llbracket\operatorname{clos}(\vec{x},y.N)\ \vec{M}\rrbracket_\rho
=\operatorname{guard}(\llbracket M_1\rrbracket_\rho,\ldots,\llbracket M_n\rrbracket_\rho;\Lambda(\lambda Y.\llbracket N\rrbracket_{\rho_0}[\vec{x}:=\llbracket\vec{M}\rrbracket_\rho,y:=Y]))\quad(\mathrm{Clos}) [ [ clos ( x , y . N ) M ] ] ρ = guard ( [ [ M 1 ] ] ρ , … , [ [ M n ] ] ρ ; Λ ( λY . [ [ N ] ] ρ 0 [ x := [ [ M ] ] ρ , y := Y ])) ( Clos )
⟦ clos ( z , y . N ) M ⃗ ⟧ ρ = Λ ( λ X . Λ ( λ Y . ⟦ N ⟧ ρ 0 [ z : = X , y : = Y ] ) ) ∙ T ( ⟦ M ⃗ ⟧ ρ ) ( E n v C l o s ) \llbracket\operatorname{clos}(z,y.N)\ \vec{M}\rrbracket_\rho
=\Lambda(\lambda X.\Lambda(\lambda Y.\llbracket N\rrbracket_{\rho_0}[z:=X,y:=Y]))\mathbin{\bullet}\mathcal{T}(\llbracket\vec{M}\rrbracket_\rho)\quad(\mathrm{EnvClos}) [ [ clos ( z , y . N ) M ] ] ρ = Λ ( λ X .Λ ( λY . [ [ N ] ] ρ 0 [ z := X , y := Y ])) ∙ T ( [ [ M ] ] ρ ) ( EnvClos )
The PairClos Language
Agda: FPC/Language/PairClos.agda, FPC/Semantics/GraphModel/PairClos.agda
PairClos, the target of split closure , replaces closures and application with code, pairs, and calls, and types and interprets its other forms as FPC does.
Typing of PairClos
z : tup ( Δ ) , y : A ⊢ N : C Γ ⊢ code ( z , y . N ) : code ( Δ ; A → C ) \frac{z:\operatorname{tup}(\Delta),y:A\vdash N:C}{\Gamma\vdash\operatorname{code}(z,y.N):\operatorname{code}(\Delta;A\to C)} Γ ⊢ code ( z , y . N ) : code ( Δ ; A → C ) z : tup ( Δ ) , y : A ⊢ N : C
Γ ⊢ L : code ( Δ ; A → C ) Γ ⊢ M : tup ( Δ ) Γ ⊢ pair L M : A → C \frac{\Gamma\vdash L:\operatorname{code}(\Delta;A\to C)\quad\Gamma\vdash M:\operatorname{tup}(\Delta)}{\Gamma\vdash\operatorname{pair}\ L\ M:A\to C} Γ ⊢ pair L M : A → C Γ ⊢ L : code ( Δ ; A → C ) Γ ⊢ M : tup ( Δ )
Γ ⊢ L : A → C Γ ⊢ M : A Γ ⊢ callc L M : C \frac{\Gamma\vdash L:A\to C\quad\Gamma\vdash M:A}{\Gamma\vdash\operatorname{callc}\ L\ M:C} Γ ⊢ callc L M : C Γ ⊢ L : A → C Γ ⊢ M : A
Semantics of PairClos
⟦ M ⟧ ρ = D (1) \llbracket M\rrbracket_\rho=D \tag*{(1)} [ [ M ] ] ρ = D (1)
⟦ code ( z , y . N ) ⟧ ρ = Λ ( λ X . Λ ( λ Y . ⟦ N ⟧ ρ 0 [ z : = X , y : = Y ] ) ) \llbracket\operatorname{code}(z,y.N)\rrbracket_\rho= \Lambda(\lambda X.\Lambda(\lambda Y.\llbracket N\rrbracket_{\rho_0}[z:=X,y:=Y])) [ [ code ( z , y . N ) ] ] ρ = Λ ( λ X .Λ ( λY . [ [ N ] ] ρ 0 [ z := X , y := Y ]))
⟦ pair L M ⟧ ρ = pair ( ⟦ L ⟧ ρ , ⟦ M ⟧ ρ ) \llbracket\operatorname{pair}\ L\ M\rrbracket_\rho= \operatorname{pair}(\llbracket L\rrbracket_\rho,\llbracket M\rrbracket_\rho) [ [ pair L M ] ] ρ = pair ( [ [ L ] ] ρ , [ [ M ] ] ρ )
⟦ callc L M ⟧ ρ = ( car ( ⟦ L ⟧ ρ ) ∙ cdr ( ⟦ L ⟧ ρ ) ) ∙ ⟦ M ⟧ ρ \llbracket\operatorname{callc}\ L\ M\rrbracket_\rho= (\operatorname{car}(\llbracket L\rrbracket_\rho)\mathbin{\bullet}\operatorname{cdr}(\llbracket L\rrbracket_\rho))\mathbin{\bullet}\llbracket M\rrbracket_\rho [ [ callc L M ] ] ρ = ( car ( [ [ L ] ] ρ ) ∙ cdr ( [ [ L ] ] ρ )) ∙ [ [ M ] ] ρ
The GlobalFun and MNF Languages
Agda: FPC/Language/GlobalFun.agda, FPC/Language/Mon.agda, FPC/Semantics/GraphModel/GlobalFun.agda
GlobalFun, the target of hoist , makes the code of every function a global definition. A GlobalFun program has a table Σ = Σ 0 , … , Σ m − 1 \Sigma= \Sigma_0,\ldots,\Sigma_{m-1} Σ = Σ 0 , … , Σ m − 1 of the signatures Σ k = ( Δ k ; A k → C k ) \Sigma_k = (\Delta_k;A_k \to C_k) Σ k = ( Δ k ; A k → C k ) of its definitions. The k k k -th definition, referred to by f k f_k f k , has environment types Δ k \Delta_k Δ k , argument type A k A_k A k , and result type C k C_k C k . A term is typed by the table and its context, Σ ; Γ ⊢ M : A \Sigma;\Gamma\vdash M:A Σ ; Γ ⊢ M : A , and a program by its table, Σ ⊢ P : A \Sigma\vdash P:A Σ ⊢ P : A . The other forms are typed as in PairClos.
Typing of GlobalFun
Σ ; Γ ⊢ M : A Σ ⊢ P : A Σ k = ( Δ k ; A k → C k ) Σ ; Γ ⊢ f k : code ( Δ k ; A k → C k ) d k = code ( z , y . N k ) Σ k = ( Δ k ; A k → C k ) Σ ; z : tup ( Δ k ) , y : A k ⊢ N k : C k Σ ; ∅ ⊢ M : A Σ ⊢ ( d ⃗ ; M ) : A \begin{gathered}
\Sigma;\Gamma\vdash M:A \qquad\Sigma\vdash P:A\\
\Sigma_k=(\Delta_k;A_k\to C_k)\\
\frac{\Sigma;\Gamma\vdash f_k:\operatorname{code}(\Delta_k;A_k\to C_k)}{}\\
\frac{d_k=\operatorname{code}(z,y.N_k)\qquad\Sigma_k=(\Delta_k;A_k\to C_k)\qquad\Sigma;z:\operatorname{tup}(\Delta_k),y:A_k\vdash N_k:C_k\qquad\Sigma;\varnothing\vdash M:A}{\Sigma\vdash(\vec d;M):A}
\end{gathered} Σ ; Γ ⊢ M : A Σ ⊢ P : A Σ k = ( Δ k ; A k → C k ) Σ ; Γ ⊢ f k : code ( Δ k ; A k → C k ) Σ ⊢ ( d ; M ) : A d k = code ( z , y . N k ) Σ k = ( Δ k ; A k → C k ) Σ ; z : tup ( Δ k ) , y : A k ⊢ N k : C k Σ ; ∅ ⊢ M : A
The premises under the arrow stand for one copy for each index k k k of a definition d k d_k d k of d ⃗ \vec d d . A term means a set given a table T T T that gives each definition its meaning, ⟦ M ⟧ T ρ \llbracket M\rrbracket_T\rho [ [ M ] ] T ρ ; the other forms are interpreted as in PairClos, passing T T T along. A definition means the curried function of its code, given the table T k T_k T k of the definitions older than it (T k ( j ) = T ( j ) T_k(j)=T(j) T k ( j ) = T ( j ) for j < k j<k j < k , and T k ( j ) = ∅ T_k(j)=\varnothing T k ( j ) = ∅ otherwise).
Semantics of GlobalFun
⟦ M ⟧ T ρ = D T ( k ) = D \llbracket M\rrbracket_T\rho=D \qquad T(k)=D [ [ M ] ] T ρ = D T ( k ) = D
⟦ f k ⟧ T ρ = T ( k ) T ( k ) = Λ ( λ X . Λ ( λ Y . ⟦ N k ⟧ T k ρ 0 [ z : = X , y : = Y ] ) ) \llbracket f_k\rrbracket_T\rho=T(k) \qquad T(k)=\Lambda(\lambda X.\Lambda(\lambda Y.\llbracket N_k\rrbracket_{T_k}\rho_0[z:=X,y:=Y])) [ [ f k ] ] T ρ = T ( k ) T ( k ) = Λ ( λ X .Λ ( λY . [ [ N k ] ] T k ρ 0 [ z := X , y := Y ]))
⟦ ( d 0 , … , d m − 1 ; M ) ⟧ = ⟦ M ⟧ T ρ 0 \llbracket(d_0,\ldots,d_{m-1};M)\rrbracket=\llbracket M\rrbracket_{T}\rho_0 [ [ ( d 0 , … , d m − 1 ; M ) ] ] = [ [ M ] ] T ρ 0
MNF, the target of remove complex operands , has the syntax of Figure 2 , is typed as GlobalFun, and has the meaning of its embedding into GlobalFun, because an atom and an operation on atoms are GlobalFun terms already.
The Imp Language
Agda: FPC/Language/Frame.agda, FPC/Language/Imp.agda, FPC/Semantics/GraphModel/Imp.agda
Imp, the target of lift locals , replaces variables by slots of a stack frame. A function body ( Φ ; e ) (\Phi;e) ( Φ ; e ) declares its frame, the list Φ = A 0 , … , A n − 1 \Phi=A_0,\ldots,A_{n-1} Φ = A 0 , … , A n − 1 of the types of its slots. We write x : A ∈ Φ x:A\in\Phi x : A ∈ Φ when slot x x x has type A A A , which it keeps for the whole body. A function body is typed by the types Φ p \Phi_p Φ p of its parameters, Σ ; Φ p ⊢ d : A \Sigma;\Phi_p\vdash d:A Σ ; Φ p ⊢ d : A . Its frame starts with the slots of the parameters, 0 , … , ∣ Φ p ∣ − 1 0,\ldots,|\Phi_p|-1 0 , … , ∣ Φ p ∣ − 1 , and its expression starts with exactly those slots initialized. An expression is typed by the set I I I of the slots that are initialized where it starts, and a slot x x x can be read only where it is initialized. An assignment x : = e 1 ; e 2 x:=e_1;e_2 x := e 1 ; e 2 writes the value of e 1 e_1 e 1 to slot x x x , and case x y e 1 e 2 x\ y\ e_1\ e_2 x y e 1 e 2 writes the payload of an injection a a a to slot x x x and runs e 1 e_1 e 1 , or to slot y y y and runs e 2 e_2 e 2 . Both are a slot that is not initialized, so a write never overwrites a variable that is in scope. A constant c c c , a function reference f k f_k f k , and an operation on atoms o a ⃗ o\ \vec a o a are typed as in MNF, with the atoms of an operation typed by the rules here.
Typing of Imp
Σ ; Φ ; I ⊢ e : C Σ ; Φ p ⊢ d : A Σ ⊢ P : A x : A ∈ Φ x ∈ I Σ ; Φ ; I ⊢ x : A x : A ∈ Φ x ∉ I Σ ; Φ ; I ⊢ e 1 : A Σ ; Φ ; I ∪ { x } ⊢ e 2 : C Σ ; Φ ; I ⊢ x : = e 1 ; e 2 : C \begin{gathered}
\Sigma;\Phi;I\vdash e:C \qquad\Sigma;\Phi_p\vdash d:A \qquad\Sigma\vdash P:A\\
\frac{x:A\in\Phi\qquad x\in I}{\Sigma;\Phi;I\vdash x:A}
\qquad
\frac{x:A\in\Phi\qquad x\notin I\qquad\Sigma;\Phi;I\vdash e_1:A\qquad\Sigma;\Phi;I\cup\{x\}\vdash e_2:C}{\Sigma;\Phi;I\vdash x:=e_1;e_2:C}
\end{gathered} Σ ; Φ ; I ⊢ e : C Σ ; Φ p ⊢ d : A Σ ⊢ P : A Σ ; Φ ; I ⊢ x : A x : A ∈ Φ x ∈ I Σ ; Φ ; I ⊢ x := e 1 ; e 2 : C x : A ∈ Φ x ∈ / I Σ ; Φ ; I ⊢ e 1 : A Σ ; Φ ; I ∪ { x } ⊢ e 2 : C
x : A 1 ∈ Φ y : A 2 ∈ Φ Σ ; Φ ; I ⊢ a : A 1 + A 2 x , y ∉ I Σ ; Φ ; I ∪ { x } ⊢ e 1 : C Σ ; Φ ; I ∪ { y } ⊢ e 2 : C Σ ; Φ ; I ⊢ case a x y e 1 e 2 : C Φ = Φ p , Φ ′ Σ ; Φ ; { 0 , … , ∣ Φ p ∣ − 1 } ⊢ e : A Σ ; Φ p ⊢ ( Φ ; e ) : A Σ k = ( Δ k ; A k → C k ) Σ ; tup ( Δ k ) , A k ⊢ d k : C k Σ ; ∅ ⊢ d : A k Σ ⊢ ( d ⃗ ; d ) : A \begin{gathered}
x : A_1 \in\Phi\qquad y : A_2 \in\Phi\qquad\Sigma;\Phi;I \vdash a : A_1 + A_2 \qquad x,y \notin I\\
\frac{\Sigma;\Phi;I\cup\{x\}\vdash e_1:C \qquad\Sigma;\Phi;I\cup\{y\}\vdash e_2:C}{\Sigma;\Phi;I\vdash\operatorname{case}\ a\ x\ y\ e_1\ e_2:C}\\
\frac{\Phi=\Phi_p,\Phi' \qquad\Sigma;\Phi;\{0,\ldots,|\Phi_p|-1\}\vdash e:A}{\Sigma;\Phi_p\vdash(\Phi;e):A}\\
\frac{\Sigma_k=(\Delta_k;A_k\to C_k)\qquad\Sigma;\operatorname{tup}(\Delta_k),A_k\vdash d_k:C_k}{\Sigma;\varnothing\vdash d:A}\ ^k\\
\Sigma\vdash(\vec d;d):A
\end{gathered} x : A 1 ∈ Φ y : A 2 ∈ Φ Σ ; Φ ; I ⊢ a : A 1 + A 2 x , y ∈ / I Σ ; Φ ; I ⊢ case a x y e 1 e 2 : C Σ ; Φ ; I ∪ { x } ⊢ e 1 : C Σ ; Φ ; I ∪ { y } ⊢ e 2 : C Σ ; Φ p ⊢ ( Φ ; e ) : A Φ = Φ p , Φ ′ Σ ; Φ ; { 0 , … , ∣ Φ p ∣ − 1 } ⊢ e : A Σ ; ∅ ⊢ d : A Σ k = ( Δ k ; A k → C k ) Σ ; tup ( Δ k ) , A k ⊢ d k : C k k Σ ⊢ ( d ; d ) : A
The premises under the arrow stand for one copy for each index k k k of a definition d k d_k d k of d ⃗ \vec d d . A definition has two parameters, its environment in slot 0 and its argument in slot 1; the main function body has none, so it starts with no slot initialized. The semantics is in continuation-passing style. The frame σ \sigma σ maps each slot to a set, and the continuation κ \kappa κ takes the meaning of the expression. A write updates its slot in place. An atom means A ⟦ x ⟧ σ = σ ( x ) \mathcal{A}\llbracket x\rrbracket\sigma=\sigma(x) A [ [ x ] ] σ = σ ( x ) , A ⟦ c ⟧ σ = { c } \mathcal{A}\llbracket c\rrbracket\sigma=\{c\} A [ [ c ] ] σ = { c } , or A ⟦ f k ⟧ σ = T ( k ) \mathcal{A}\llbracket f_k\rrbracket\sigma=T(k) A [ [ f k ] ] σ = T ( k ) , and a simple expression S ⟦ o a ⃗ ⟧ σ \mathcal{S}\llbracket o\ \vec a\rrbracket\sigma S [ [ o a ] ] σ applies the GlobalFun operator of o o o to the meanings of its atoms. An assignment is strict in the value it assigns, like a let.
Semantics of Imp
⟦ e ⟧ T σ κ = D \llbracket e\rrbracket_T\sigma\kappa=D [ [ e ] ] T σ κ = D
⟦ r h s ⟧ T σ κ = κ ( S ⟦ r h s ⟧ σ ) \llbracket rhs\rrbracket_T\sigma\kappa=\kappa(\mathcal{S}\llbracket rhs\rrbracket\sigma) [ [ r h s ] ] T σ κ = κ ( S [ [ r h s ] ] σ )
⟦ x : = e 1 ; e 2 ⟧ T σ κ = ⟦ e 1 ⟧ T σ ( λ D . Λ ( λ X . ⟦ e 2 ⟧ T σ [ x : = X ] κ ) ∙ D ) \llbracket x:=e_1;e_2\rrbracket_T\sigma\kappa=\llbracket e_1\rrbracket_T\sigma(\lambda D.\Lambda(\lambda X.\llbracket e_2\rrbracket_T\sigma[x:=X]\kappa)\mathbin{\bullet}D) [ [ x := e 1 ; e 2 ] ] T σ κ = [ [ e 1 ] ] T σ ( λ D .Λ ( λ X . [ [ e 2 ] ] T σ [ x := X ] κ ) ∙ D )
⟦ case a x y e 1 e 2 ⟧ T σ κ = C ( A ⟦ a ⟧ σ , λ X . ⟦ e 1 ⟧ T σ [ x : = X ] κ , λ X . ⟦ e 2 ⟧ T σ [ y : = X ] κ ) \llbracket\operatorname{case}\ a\ x\ y\ e_1\ e_2\rrbracket_T\sigma\kappa=C(\mathcal{A}\llbracket a\rrbracket\sigma,\lambda X.\llbracket e_1\rrbracket_T\sigma[x:=X]\kappa,\lambda X.\llbracket e_2\rrbracket_T\sigma[y:=X]\kappa) [ [ case a x y e 1 e 2 ] ] T σ κ = C ( A [ [ a ] ] σ , λ X . [ [ e 1 ] ] T σ [ x := X ] κ , λ X . [ [ e 2 ] ] T σ [ y := X ] κ )
T ( k ) = Λ ( λ X . Λ ( λ Y . ⟦ e k ⟧ T k ρ 0 [ 0 : = X , 1 : = Y ] ( λ D . D ) ) ) T(k)=\Lambda(\lambda X.\Lambda(\lambda Y.\llbracket e_k\rrbracket_{T_k}\rho_0[0:=X,1:=Y](\lambda D.D))) T ( k ) = Λ ( λ X .Λ ( λY . [ [ e k ] ] T k ρ 0 [ 0 := X , 1 := Y ] ( λ D . D )))
⟦ ( d ⃗ ; ( Φ ; e ) ) ⟧ = ⟦ e ⟧ T ρ 0 ( λ D . D ) \llbracket(\vec d;(\Phi;e))\rrbracket=\llbracket e\rrbracket_T\rho_0(\lambda D.D) [ [ ( d ; ( Φ ; e )) ] ] = [ [ e ] ] T ρ 0 ( λ D . D )
The CFG Language
Agda: FPC/Language/CFG.agda, FPC/Semantics/GraphModel/CFG.agda
CFG, the target of explicate control , organizes each function body as a control-flow graph. A function body ( Φ ; t ⃗ ; t ) (\Phi;\vec t;t) ( Φ ; t ; t ) declares its frame Φ \Phi Φ , as in Imp, and has numbered blocks t ⃗ \vec t t and a starting tail t t t . A tail has the result type A A A of its function body, written Σ ; Φ ; I ⃗ ; I ⊢ t : A \Sigma;\Phi;\vec I;I\vdash t:A Σ ; Φ ; I ; I ⊢ t : A , and is typed by the list I ⃗ \vec I I of the sets of slots that are initialized where the blocks it may jump to start, one set I l I_l I l for each block t l t_l t l , and the set I I I of the slots that are initialized where it starts. As in Imp, a function body is typed by the types Φ p \Phi_p Φ p of its parameters, Σ ; Φ p ⊢ d : A \Sigma;\Phi_p\vdash d:A Σ ; Φ p ⊢ d : A , and its starting tail starts with exactly the slots of the parameters initialized. A program is typed by the same rule as in Imp, Σ ⊢ P : A \Sigma\vdash P:A Σ ⊢ P : A , with CFG function bodies in place of Imp ones, so the figure omits it. A label l l l is an index into I ⃗ \vec I I . Block t l t_l t l is typed over the blocks older than it, whose sets are the first l l l sets I ⃗ < l \vec I_{<l} I < l of I ⃗ \vec I I , so control flow is acyclic. Imp tracks the initialized slots by scope, and CFG tracks them along the flow of control. An assignment x : = r h s ; t x:=rhs;t x := r h s ; t initializes slot x x x , and its tail t t t may start with fewer initialized slots (I ′ ⊆ I ∪ { x } I'\subseteq I\cup\{x\} I ′ ⊆ I ∪ { x } ), which forgets the slots of variables whose scope has ended. Join points need this. explicate control (Appendix C.7) translates an assignment of a case to slot x x x into a case whose branches each assign x x x and jump to one block for the rest. Each branch has also initialized its own payload slot, but goto requires the initialized slots to be exactly those of the block it jumps to. So each branch’s assignment forgets its payload slot, and both branches reach the block with the same initialized slots. The statement case a x y l 1 l 2 a\ x\ y\ l_1\ l_2 a x y l 1 l 2 writes the payload of the injection a a a to slot x x x and jumps to block l 1 l_1 l 1 , or to slot y y y and jumps to block l 2 l_2 l 2 . The judgment Σ ; Φ ; I ⃗ ⊢ r h s : A \Sigma;\Phi;\vec I\vdash rhs:A Σ ; Φ ; I ⊢ r h s : A types a simple expression r h s rhs r h s , an atom or an operation on atoms a ⃗ \vec a a , by the rules for atoms and operations on atoms in Imp.
Typing of CFG
Σ ; Φ ; I ⊢ r h s : A Σ ; Φ ; I ⃗ ; I ⊢ return r h s : A x : C ∈ Φ Σ ; Φ ; I ⊢ r h s : C I ′ ⊆ I ∪ { x } Σ ; Φ ; I ⃗ ; I ′ ⊢ t : A Σ ; Φ ; I ⃗ ; I ⊢ x : = r h s ; t : A (2) \frac{\Sigma;\Phi;I\vdash rhs:A}{\Sigma;\Phi;\vec I;I\vdash\operatorname{return}\ rhs:A}
\qquad
\frac{x:C\in\Phi\qquad\Sigma;\Phi;I\vdash rhs:C\qquad I'\subseteq I\cup\{x\}\qquad\Sigma;\Phi;\vec I;I'\vdash t:A}{\Sigma;\Phi;\vec I;I\vdash x:=rhs;t:A} \tag*{(2)} Σ ; Φ ; I ; I ⊢ return r h s : A Σ ; Φ ; I ⊢ r h s : A Σ ; Φ ; I ; I ⊢ x := r h s ; t : A x : C ∈ Φ Σ ; Φ ; I ⊢ r h s : C I ′ ⊆ I ∪ { x } Σ ; Φ ; I ; I ′ ⊢ t : A (2)
I l = I Σ ; Φ ; I ‾ ; I ⊢ goto l : A x : A 1 ∈ Φ y : A 2 ∈ Φ Σ ; Φ ; I ⊢ a : A 1 + A 2 I l 1 = I ∪ { x } I l 2 = I ∪ { y } Σ ; Φ ; I ‾ ; I ⊢ case a x y l 1 l 2 : A \frac{I_l = I}{\Sigma;\Phi;\overline{I};I \vdash\operatorname{goto}\ l:A}
\qquad
\frac{x:A_1 \in\Phi\quad y:A_2 \in\Phi\quad\Sigma;\Phi;I \vdash a:A_1+A_2 \quad I_{l_1}=I\cup\{x\} \quad I_{l_2}=I\cup\{y\}}{\Sigma;\Phi;\overline{I};I \vdash\operatorname{case}\ a\ x\ y\ l_1\ l_2:A} Σ ; Φ ; I ; I ⊢ goto l : A I l = I Σ ; Φ ; I ; I ⊢ case a x y l 1 l 2 : A x : A 1 ∈ Φ y : A 2 ∈ Φ Σ ; Φ ; I ⊢ a : A 1 + A 2 I l 1 = I ∪ { x } I l 2 = I ∪ { y }
Φ = Φ p , Φ ′ Σ ; Φ ; I l ‾ ; I l ⊢ t l : A Σ ; Φ ; I l ‾ ; I l ⊢ t l : A Σ ; Φ ; I ‾ ; { 0 , … , ∣ Φ p ∣ − 1 } ⊢ t : A Σ ; Φ p ⊢ ( Φ ; t ‾ ; t ) : A \frac{\Phi=\Phi_p,\Phi' \qquad\frac{\Sigma;\Phi;\overline{I_l};I_l\vdash t_l:A}{\phantom{\Sigma;\Phi;\overline{I_l};I_l\vdash t_l:A}} \quad\Sigma;\Phi;\overline{I};\{0,\ldots,|\Phi_p|-1\}\vdash t:A}{\Sigma;\Phi_p\vdash(\Phi;\overline{t};t):A} Σ ; Φ p ⊢ ( Φ ; t ; t ) : A Φ = Φ p , Φ ′ Σ ; Φ ; I l ; I l ⊢ t l : A Σ ; Φ ; I l ; I l ⊢ t l : A Σ ; Φ ; I ; { 0 , … , ∣ Φ p ∣ − 1 } ⊢ t : A
The premises under the arrow stand for one copy for each index l l l of a block t l t_l t l of t ‾ \overline{t} t . A tail means a set, given its frame σ \sigma σ and the meanings B B B of the blocks it can see. A write updates its slot in place.
Semantics of CFG
⟦ t ⟧ T , B σ = D \llbracket t\rrbracket_{T,B}\sigma=D [ [ t ] ] T , B σ = D
⟦ return r h s ⟧ T , B σ = S ⟦ r h s ⟧ σ ⟦ x : = r h s ; t ⟧ T , B σ = Λ ( λ X . ⟦ t ⟧ T , B σ [ x : = X ] ) ∙ S ⟦ r h s ⟧ σ ⟦ goto l ⟧ T , B σ = B ( l ) ( σ ) ⟦ case a x y l 1 l 2 ⟧ T , B σ = C ( A ⟦ a ⟧ σ , λ X . B ( l 1 ) ( σ [ x : = X ] ) , λ X . B ( l 2 ) ( σ [ y : = X ] ) ) \begin{aligned}
\llbracket\operatorname{return}\ rhs\rrbracket_{T,B}\sigma&=S\llbracket rhs\rrbracket_\sigma\\
\llbracket x:=rhs;t\rrbracket_{T,B}\sigma&=\Lambda(\boldsymbol{\lambda}X.\llbracket t\rrbracket_{T,B}\sigma[x:=X])\mathbin{\bullet}S\llbracket rhs\rrbracket_\sigma\\
\llbracket\operatorname{goto}\ l\rrbracket_{T,B}\sigma&=B(l)(\sigma)\\
\llbracket\operatorname{case}\ a\ x\ y\ l_1\ l_2\rrbracket_{T,B}\sigma&=C(A\llbracket a\rrbracket_\sigma,\boldsymbol{\lambda}X.B(l_1)(\sigma[x:=X]),\boldsymbol{\lambda}X.B(l_2)(\sigma[y:=X]))
\end{aligned} [ [ return r h s ] ] T , B σ [ [ x := r h s ; t ] ] T , B σ [ [ goto l ] ] T , B σ [ [ case a x y l 1 l 2 ] ] T , B σ = S [ [ r h s ] ] σ = Λ ( λ X . [ [ t ] ] T , B σ [ x := X ]) ∙ S [ [ r h s ] ] σ = B ( l ) ( σ ) = C ( A [ [ a ] ] σ , λ X . B ( l 1 ) ( σ [ x := X ]) , λ X . B ( l 2 ) ( σ [ y := X ]))
The blocks of a function body ( Φ ; t ‾ ; t ) (\Phi;\overline{t};t) ( Φ ; t ; t ) mean B ( l ) ( σ ) = ⟦ t l ⟧ T , B l σ B(l)(\sigma)=\llbracket t_l\rrbracket_{T,B_l}\sigma B ( l ) ( σ ) = [ [ t l ] ] T , B l σ , where B l B_l B l gives the blocks older than l l l , and the function body means ⟦ t ⟧ T , B \llbracket t\rrbracket_{T,B} [ [ t ] ] T , B . Definitions and programs are given meaning as in Imp, with the function body in place of the expression and no continuation.
The Instrμ _\mu μ Language
Agda: FPC/Language/X86.agda, FPC/Semantics/GraphModel/X86.agda
Instrμ _\mu μ is the target language, produced by select instructions ; § 2 describes its instructions informally. A function body declares its frame Φ \Phi Φ , as in CFG. An instruction is typed by the type of the machine state it starts in and the one it leaves. A state type ψ = ⟨ Θ ; I ; O ; F ⟩ \psi=\langle\Theta;I;O;F\rangle ψ = ⟨ Θ ; I ; O ; F ⟩ gives the types Θ \Theta Θ of the registers, the set I I I of the initialized slots, the open closure O O O , and whether the flag F F F is set. The open closure O O O is either none, written ⋅ \cdot ⋅ , or the initialized slot x x x , of a function type, that holds the closure that fst opened. The register types Θ \Theta Θ map a register r r r to either nothing, indicating an unset register, or the type Θ ( r ) \Theta(r) Θ ( r ) of what it holds: a value of a type A A A , the code opencode ( A → C ) \operatorname{opencode}(A\to C) opencode ( A → C ) of the open closure, or the environment openenv \operatorname{openenv} openenv of the open closure; ∅ \varnothing ∅ maps every register to nothing. The type opencode ( A → C ) \operatorname{opencode}(A\to C) opencode ( A → C ) is the type of the function reference of the open closure, which names the global function definition that takes an argument of type A A A and returns a C C C . openenv \operatorname{openenv} openenv is the type of the closing environment of the open closure, the tuple of the values it captured. Neither names the types of the environment, which the function type A → C A\to C A → C of the closure hides. We write τ \tau τ for any of these types of what a register holds, τ : : = A ∣ opencode ( A → C ) ∣ openenv \tau::= A\mid\operatorname{opencode}(A\to C)\mid\operatorname{openenv} τ ::= A ∣ opencode ( A → C ) ∣ openenv ; operands and destinations are typed at τ \tau τ , because they may be registers. The operands of an instruction are typed by these rules, with the atoms typed as in Imp, over the initialized slots of the state.
Typing of operands
Σ ; Φ ; I ⊢ a : A \Sigma;\Phi;I\vdash a:A Σ ; Φ ; I ⊢ a : A
Σ ; Φ ⊢ I a : A Σ ; Φ ; ⟨ Θ ; I ; O ; F ⟩ ⊢ a : A Θ ( r ) = τ Σ ; Φ ; ⟨ Θ ; I ; O ; F ⟩ ⊢ r : τ \frac{\Sigma;\Phi\vdash I\ a:A}{\Sigma;\Phi;\langle\Theta;I;O;F\rangle\vdash a:A}
\qquad
\frac{\Theta(r)=\tau}{\Sigma;\Phi;\langle\Theta;I;O;F\rangle\vdash r:\tau} Σ ; Φ ; ⟨ Θ ; I ; O ; F ⟩ ⊢ a : A Σ ; Φ ⊢ I a : A Σ ; Φ ; ⟨ Θ ; I ; O ; F ⟩ ⊢ r : τ Θ ( r ) = τ
Σ ; Φ ; ψ ⊢ a : A [ μ X . A / X ] \Sigma;\Phi;\psi\vdash a:A[\mu X.A/X] Σ ; Φ ; ψ ⊢ a : A [ μ X . A / X ]
Σ ; Φ ; ψ ⊢ fold a : μ X . A Σ ; Φ ; ψ ⊢ unfold a : A [ μ X . A / X ] \Sigma;\Phi;\psi\vdash\operatorname{fold}\ a:\mu X.A
\qquad
\Sigma;\Phi;\psi\vdash\operatorname{unfold}\ a:A[\mu X.A/X] Σ ; Φ ; ψ ⊢ fold a : μ X . A Σ ; Φ ; ψ ⊢ unfold a : A [ μ X . A / X ]
A destination, where an instruction writes a value, is either a register or a slot of the frame. Writing a value of type τ \tau τ to a destination d s t dst d s t changes the state ψ \psi ψ to a new state ψ ′ \psi' ψ ′ , written Φ ⊢ d s t : ψ ⇒ τ ψ ′ \Phi\vdash dst:\psi\Rightarrow_\tau\psi' Φ ⊢ d s t : ψ ⇒ τ ψ ′ . A register can hold a value of any type τ \tau τ (DST-REG), but a slot x x x holds only a value of the type A A A that the frame gives it, x : A ∈ Φ x:A\in\Phi x : A ∈ Φ (DST-SLOT). A write to a slot initializes it, lets the rest start with fewer initialized slots (I ′ ⊆ I ∪ { x } I'\subseteq I\cup\{x\} I ′ ⊆ I ∪ { x } ), as in CFG (Appendix § 11 .§ 6 ), and closes the open closure.
Typing of destinations
Φ ⊢ d s t : ψ ⇒ τ ψ ′ \Phi\vdash dst:\psi\Rightarrow_\tau\psi' Φ ⊢ d s t : ψ ⇒ τ ψ ′
\Phi\vdash\%r : \langle\Theta; I; O; F \rangle\Rightarrow_{\tau} \langle\Theta[r:\tau]; I; O; F \rangle
\qquad\textsc{dst-reg}
\frac{x : A \in\Phi\qquad I' \subseteq I \cup\{x\}}
{\Phi\vdash x : \langle\Theta; I; O; F \rangle\Rightarrow_A \langle\Theta; I'; \cdot; F \rangle}
\qquad\textsc{dst-slot}
A constructor c o n con co n , which alloc allocates, builds a closure, a tuple, or an injection from operands typed in the state ψ \psi ψ . A closure p a i r ( a 1 , a 2 ) pair(a_1,a_2) p ai r ( a 1 , a 2 ) pairs the code of a definition with the tuple of its environment, whose types Δ \Delta Δ its function type hides.
Typing of constructors
Σ ; Φ ; ψ ⊢ c o n : A \Sigma; \Phi; \psi\vdash con : A Σ ; Φ ; ψ ⊢ co n : A
Σ ; Φ ; ψ ⊢ a 1 : code ( Δ ; A → C ) Σ ; Φ ; ψ ⊢ a 2 : tup ( Δ ) Σ ; Φ ; ψ ⊢ p a i r ( a 1 , a 2 ) : A → C \frac{\Sigma; \Phi; \psi\vdash a_1 : \operatorname{code}(\Delta; A \to C)
\qquad
\Sigma; \Phi; \psi\vdash a_2 : \operatorname{tup}(\Delta)}
{\Sigma; \Phi; \psi\vdash pair(a_1,a_2) : A \to C} Σ ; Φ ; ψ ⊢ p ai r ( a 1 , a 2 ) : A → C Σ ; Φ ; ψ ⊢ a 1 : code ( Δ ; A → C ) Σ ; Φ ; ψ ⊢ a 2 : tup ( Δ )
Σ ; Φ ; ψ ⊢ a i : A i i Σ ; Φ ; ψ ⊢ t u p l e ( a ⃗ ) : tup ( A ⃗ ) \frac{\Sigma; \Phi; \psi\vdash a_i : A_i \quad i}
{\Sigma; \Phi; \psi\vdash tuple(\vec{a}) : \operatorname{tup}(\vec{A})} Σ ; Φ ; ψ ⊢ t u pl e ( a ) : tup ( A ) Σ ; Φ ; ψ ⊢ a i : A i i
Σ ; Φ ; ψ ⊢ a : A Σ ; Φ ; ψ ⊢ i n l ( a ) : A + C Σ ; Φ ; ψ ⊢ a : C Σ ; Φ ; ψ ⊢ i n r ( a ) : A + C \frac{\Sigma; \Phi; \psi\vdash a : A}
{\Sigma; \Phi; \psi\vdash inl(a) : A + C}
\qquad
\frac{\Sigma; \Phi; \psi\vdash a : C}
{\Sigma; \Phi; \psi\vdash inr(a) : A + C} Σ ; Φ ; ψ ⊢ in l ( a ) : A + C Σ ; Φ ; ψ ⊢ a : A Σ ; Φ ; ψ ⊢ in r ( a ) : A + C Σ ; Φ ; ψ ⊢ a : C
With ψ = ⟨ Θ ; I ; O ; F ⟩ \psi= \langle\Theta; I; O; F \rangle ψ = ⟨ Θ ; I ; O ; F ⟩ , the instructions are typed as follows, using the typing of operands, constructors, and destinations above.
Typing of instructions
Σ ; Φ ⊢ i : ψ ⇒ ψ ′ \Sigma; \Phi\vdash i : \psi\Rightarrow\psi' Σ ; Φ ⊢ i : ψ ⇒ ψ ′
\frac{\Sigma; \Phi; \psi\vdash a : \tau\qquad\Phi\vdash dst : \psi\Rightarrow_{\tau} \psi'}
{\Sigma; \Phi\vdash movq\ a,dst : \psi\Rightarrow\psi'}
\qquad\textsc{ins-movq}
\frac{\Sigma; \Phi; \psi\vdash a : \operatorname{Int} \qquad\Theta(r) = \operatorname{Int}}
{\Sigma; \Phi\vdash subq\ a,\%r : \psi\Rightarrow\psi}
\qquad\textsc{ins-subq}
\frac{\Sigma; \Phi; \psi\vdash a_1 : \operatorname{Int} \qquad\Sigma; \Phi; \psi\vdash a_2 : \operatorname{Int}}
{\Sigma; \Phi\vdash cmpq\ a_2,a_1 : \psi\Rightarrow\langle\Theta; I; O; \operatorname{set}\rangle}
\qquad\textsc{ins-cmpq}
\frac{\Phi\vdash dst : \langle\Theta; I; O; \operatorname{set}\rangle\Rightarrow_{\operatorname{Bool}} \psi'}
{\Sigma; \Phi\vdash sete\ dst : \langle\Theta; I; O; \operatorname{set}\rangle\Rightarrow\psi'}
\qquad\textsc{ins-sete}
\frac{\Sigma; \Phi; \psi\vdash con : A \qquad\Phi\vdash dst : \psi\Rightarrow_A \psi'}
{\Sigma; \Phi\vdash alloc\ con,dst : \psi\Rightarrow\psi'}
\qquad\textsc{ins-alloc}
\frac{\Sigma; \Phi; \psi\vdash a : \operatorname{tup}(\vec{A}) \qquad1 \leq i \leq|\vec{A}| \qquad\Phi\vdash dst : \psi\Rightarrow_{A_i} \psi'}
{\Sigma; \Phi\vdash load\ get_i\ a,dst : \psi\Rightarrow\psi'}
\qquad\textsc{ins-load}
\frac{x : A \to C \in\Phi\qquad x \in I}
{\Sigma; \Phi\vdash fst\ x,\%r : \psi\Rightarrow\langle wipe(\Theta)[r:\operatorname{opencode}(A \to C)]; I; x; F\rangle}
\qquad\textsc{ins-fst}
\Sigma; \Phi\vdash snd\ x,\%r : \langle\Theta; I; x; F\rangle\Rightarrow\langle\Theta[r:\operatorname{openenv}]; I; x; F\rangle
\qquad\textsc{ins-snd}
\frac{\Sigma; \Phi; \psi\vdash a : \operatorname{opencode}(A \to C) \qquad\Theta(rdi)=\operatorname{openenv} \qquad\Theta(rsi)=A}
{\Sigma; \Phi\vdash callq\ a : \psi\Rightarrow\langle rax:C; I; \cdot; \operatorname{unset}\rangle}
\qquad\textsc{ins-callq}
The rules for f s t fst f s t , s n d snd s n d , and c a l l q callq c a l l q ensure that a call combines the code and the environment of one closure, as § 2 requires. The types opencode ( A → C ) \operatorname{opencode}(A \to C) opencode ( A → C ) and openenv \operatorname{openenv} openenv do not name the closure, so two constraints keep its code and environment together. First, w i p e ( Θ ) wipe(\Theta) w i p e ( Θ ) in \textsc{ins-fst} unsets the registers that hold the code or the environment of an earlier open closure. Second, a write to a slot closes the open closure (\textsc{dst-slot} ), so s n d snd s n d runs only while slot x x x still holds the closure that f s t fst f s t opened. This never rejects the code that select instructions produces, because it writes no slot between f s t fst f s t and c a l l q callq c a l l q . After the call, only rax is set, and no closure is open.
A sequence s s s has the result type A A A of its function body, written s : A s : A s : A , in its starting state ψ \psi ψ and with the sets I ⃗ \vec{I} I of initialized slots of the blocks it may jump to. retq requires that rax holds an A A A , and jmp need not constrain A A A , because the block it jumps to has the same result type A A A . As in CFG, case x y z l 1 l 2 x\ y\ z\ l_1\ l_2 x y z l 1 l 2 writes the payload of the injection in slot x x x to slot y y y and jumps to block l 1 l_1 l 1 , or to slot z z z and jumps to block l 2 l_2 l 2 . A label l l l is an index into I I I , and I l I_l I l is the set of the slots that are initialized where block l l l starts. Because jmp does not change the initialized slots, it requires them to be I l I_l I l . Because case initializes slot y y y or slot z z z , it requires blocks l 1 l_1 l 1 and l 2 l_2 l 2 to start with I ∪ { y } I \cup\{y\} I ∪ { y } and I ∪ { z } I \cup\{z\} I ∪ { z } . Only the initialized slots pass from one block to the next. The registers, the open closure, and the flag do not, because every block starts with them unset.
Typing of sequences
Σ ; Φ ; ψ ; I ⃗ ⊢ s : A \Sigma; \Phi; \psi; \vec{I} \vdash s : A Σ ; Φ ; ψ ; I ⊢ s : A
Σ ; Φ ⊢ i : ψ ⇒ ψ ′ Σ ; Φ ; ψ ′ ; I ˉ ⊢ s : A Σ ; Φ ; ψ ; I ˉ ⊢ i ; s : A Θ ( r a x ) = A Σ ; Φ ; ⟨ Θ ; I ; O ; F ⟩ ; I ˉ ⊢ r e t q : A \frac{\Sigma;\Phi\vdash i:\psi\Rightarrow\psi' \qquad\Sigma;\Phi;\psi';\bar{I} \vdash s:A}{\Sigma;\Phi;\psi;\bar{I} \vdash i;s:A}
\qquad
\frac{\Theta(\mathrm{rax})=A}{\Sigma;\Phi;\langle\Theta;I;O;F\rangle;\bar{I}\vdash\mathrm{retq}:A} Σ ; Φ ; ψ ; I ˉ ⊢ i ; s : A Σ ; Φ ⊢ i : ψ ⇒ ψ ′ Σ ; Φ ; ψ ′ ; I ˉ ⊢ s : A Σ ; Φ ; ⟨ Θ ; I ; O ; F ⟩ ; I ˉ ⊢ retq : A Θ ( rax ) = A
I l = I Σ ; Φ ; ⟨ Θ ; I ; O ; F ⟩ ; I ˉ ⊢ j m p l : A x : A 1 + A 2 ∈ Φ x ∈ I y : A 1 ∈ Φ z : A 2 ∈ Φ I l 1 = I ∪ { y } I l 2 = I ∪ { z } Σ ; Φ ; ⟨ Θ ; I ; O ; F ⟩ ; I ˉ ⊢ c a s e x y z l 1 l 2 : A \frac{I_l=I}{\Sigma;\Phi;\langle\Theta;I;O;F\rangle;\bar{I}\vdash\mathrm{jmp}\ l:A}
\qquad
\frac{x:A_1+A_2\in\Phi\quad x\in I \quad y:A_1\in\Phi\quad z:A_2\in\Phi\qquad I_{l_1}=I\cup\{y\} \quad I_{l_2}=I\cup\{z\}}{\Sigma;\Phi;\langle\Theta;I;O;F\rangle;\bar{I}\vdash\mathrm{case}\ x\ y\ z\ l_1\ l_2:A} Σ ; Φ ; ⟨ Θ ; I ; O ; F ⟩ ; I ˉ ⊢ jmp l : A I l = I Σ ; Φ ; ⟨ Θ ; I ; O ; F ⟩ ; I ˉ ⊢ case x y z l 1 l 2 : A x : A 1 + A 2 ∈ Φ x ∈ I y : A 1 ∈ Φ z : A 2 ∈ Φ I l 1 = I ∪ { y } I l 2 = I ∪ { z }
A function body ( Φ ; s ˉ ; s ) (\Phi;\bar{s};s) ( Φ ; s ˉ ; s ) has result type A A A in the state ψ \psi ψ that its start sequence s s s begins in, written Σ ; ψ ⊢ d : A \Sigma;\psi\vdash d:A Σ ; ψ ⊢ d : A . As in CFG, block s l s_l s l is typed over the blocks older than it, whose sets are the first l l l sets I < l I_{<l} I < l of I ˉ \bar{I} I ˉ . A program is typed by the table Σ \Sigma Σ of the signatures Σ k = ( Δ k ; A k → C k ) \Sigma_k=(\Delta_k;A_k\to C_k) Σ k = ( Δ k ; A k → C k ) of its definitions, as in GlobalFun, written Σ ⊢ P : A \Sigma\vdash P:A Σ ⊢ P : A .
Typing of function bodies and programs
Σ ; Φ ; ⟨ ∅ ; I l ; ⋅ ; u n s e t ⟩ ; I ˉ < l ⊢ s l : A Σ ; Φ ; ψ ; I ˉ ⊢ s : A Σ ; ψ ⊢ ( Φ ; s ˉ ; s ) : A Σ k = ( Δ k ; A k → C k ) Σ ; ⟨ r d i : tup ( Δ k ) , r s i : A k ; ∅ ; ⋅ ; u n s e t ⟩ ⊢ d k : C k Σ ; ⟨ ∅ ; ∅ ; ⋅ ; u n s e t ⟩ ⊢ d : A Σ ⊢ ( d ˉ ; d ) : A \frac{ \frac{\Sigma;\Phi;\langle\varnothing;I_l;\cdot;\mathrm{unset}\rangle;\bar{I}_{<l}\vdash s_l:A \qquad\Sigma;\Phi;\psi;\bar{I}\vdash s:A}{\Sigma;\psi\vdash(\Phi;\bar{s};s):A}}
{ \frac{\Sigma_k=(\Delta_k;A_k\to C_k)\qquad\Sigma;\langle\mathrm{rdi}:\operatorname{tup}(\Delta_k),\mathrm{rsi}:A_k;\varnothing;\cdot;\mathrm{unset}\rangle\vdash d_k:C_k}{\Sigma;\langle\varnothing;\varnothing;\cdot;\mathrm{unset}\rangle\vdash d:A}}
\qquad
\Sigma\vdash(\bar{d};d):A Σ ; ⟨ ∅ ; ∅ ; ⋅ ; unset ⟩ ⊢ d : A Σ k = ( Δ k ; A k → C k ) Σ ; ⟨ rdi : tup ( Δ k ) , rsi : A k ; ∅ ; ⋅ ; unset ⟩ ⊢ d k : C k Σ ; ψ ⊢ ( Φ ; s ˉ ; s ) : A Σ ; Φ ; ⟨ ∅ ; I l ; ⋅ ; unset ⟩ ; I ˉ < l ⊢ s l : A Σ ; Φ ; ψ ; I ˉ ⊢ s : A Σ ⊢ ( d ˉ ; d ) : A
The premises under an arrow stand for one copy for each index l l l of a block s l s_l s l of s ˉ \bar{s} s ˉ , or each index k k k of a definition d k d_k d k of d ˉ \bar{d} d ˉ . Each block s l s_l s l starts with its registers and flag unset, no closure open, and the slots I l I_l I l initialized, and may jump only to the blocks older than it, so blocks are acyclic as in CFG. A definition starts with its environment in rdi, its argument in rsi, and no slot initialized; the main function body starts with nothing set.
The semantics of a sequence is a function of the frame σ \sigma σ , the registers R R R (a map from registers to sets), and the flag F F F (a set); an instruction transforms the meaning κ \kappa κ of the rest of the sequence. An unset register and an unset flag hold { ⟨ ⟩ } \{\langle\rangle\} {⟨ ⟩} . An operand means O ⟦ a ⟧ σ R = A ⟦ a ⟧ σ O\llbracket a\rrbracket_{\sigma R}=A\llbracket a\rrbracket_{\sigma} O [ [ a ] ] σ R = A [ [ a ] ] σ for an atom, R ( r ) R(r) R ( r ) for % r \%r % r , and O ⟦ a ⟧ σ R O\llbracket a\rrbracket_{\sigma R} O [ [ a ] ] σ R for fold a a a and unfold a a a . The meaning O ⟦ c o n ⟧ σ R O\llbracket\mathrm{con}\rrbracket_{\sigma R} O [ [ con ] ] σ R of a constructor, which alloc allocates, applies pair, T T T , inl, or inr to the meanings of its operands. A write to a destination, written put, updates a register, or binds a slot as an assignment in CFG does.
Writing a destination
put ( d s t , D ) κ σ R F = E \operatorname{put}(\mathrm{dst},D)\ \kappa\ \sigma R F=E put ( dst , D ) κ σ R F = E
put ( % r , D ) κ σ R F = κ σ R [ r : = D ] F put ( x , D ) κ σ R F = Λ ( λ X . κ ( σ [ x : = X ] ) R F ) ∙ D \operatorname{put}(\%r,D)\ \kappa\ \sigma R F=\kappa\ \sigma\ R[r:=D]\ F
\qquad
\operatorname{put}(x,D)\ \kappa\ \sigma R F=\Lambda(\lambda X.\ \kappa\ (\sigma[x:=X])\ R\ F)\bullet D put ( % r , D ) κ σ R F = κ σ R [ r := D ] F put ( x , D ) κ σ R F = Λ ( λ X . κ ( σ [ x := X ]) R F ) ∙ D
Semantics of instructions
⟦ i ⟧ κ σ R F = D \llbracket i\rrbracket\kappa\ \sigma R F=D [ [ i ] ] κ σ R F = D
⟦ m o v q a , d s t ⟧ κ σ R F = put ( d s t , O ⟦ a ⟧ σ R ) κ σ R F ⟦ s u b q a , % r ⟧ κ σ R F = κ σ R [ r : = prim s u b ( R ( r ) , O ⟦ a ⟧ σ R ) ] F ⟦ c m p q a 2 , a 1 ⟧ κ σ R F = κ σ R ( prim e q ( O ⟦ a 1 ⟧ σ R , O ⟦ a 2 ⟧ σ R ) ) ⟦ s e t e d s t ⟧ κ σ R F = put ( d s t , F ) κ σ R F ⟦ a l l o c c o n , d s t ⟧ κ σ R F = put ( d s t , O ⟦ c o n ⟧ σ R ) κ σ R F ⟦ l o a d get i a , d s t ⟧ κ σ R F = put ( d s t , π t u p i ( O ⟦ a ⟧ σ R ) ) κ σ R F ⟦ f s t x , % r ⟧ κ σ R F = κ σ R [ r : = car ( σ ( x ) ) ] F ⟦ s n d x , % r ⟧ κ σ R F = κ σ R [ r : = cdr ( σ ( x ) ) ] F ⟦ c a l l q a ⟧ κ σ R F = guard ( C , κ σ R C { ⟨ ⟩ } ) \begin{aligned}
\llbracket\mathrm{movq}\ a,\mathrm{dst}\rrbracket\kappa\ \sigma R F&=\operatorname{put}(\mathrm{dst},O\llbracket a\rrbracket_{\sigma R})\ \kappa\ \sigma R F\\
\llbracket\mathrm{subq}\ a,\%r\rrbracket\kappa\ \sigma R F&=\kappa\ \sigma\ R[r:=\operatorname{prim}_{\mathrm{sub}}(R(r),O\llbracket a\rrbracket_{\sigma R})]\ F\\
\llbracket\mathrm{cmpq}\ a_2,a_1\rrbracket\kappa\ \sigma R F&=\kappa\ \sigma\ R\bigl(\operatorname{prim}_{\mathrm{eq}}(O\llbracket a_1\rrbracket_{\sigma R},O\llbracket a_2\rrbracket_{\sigma R})\bigr)\\
\llbracket\mathrm{sete}\ \mathrm{dst}\rrbracket\kappa\ \sigma R F&=\operatorname{put}(\mathrm{dst},F)\ \kappa\ \sigma R F\\
\llbracket\mathrm{alloc}\ \mathrm{con},\mathrm{dst}\rrbracket\kappa\ \sigma R F&=\operatorname{put}(\mathrm{dst},O\llbracket\mathrm{con}\rrbracket_{\sigma R})\ \kappa\ \sigma R F\\
\llbracket\mathrm{load}\ \operatorname{get}_i\ a,\mathrm{dst}\rrbracket\kappa\ \sigma R F&=\operatorname{put}(\mathrm{dst},\pi_{\mathrm{tup}_i}(O\llbracket a\rrbracket_{\sigma R}))\ \kappa\ \sigma R F\\
\llbracket\mathrm{fst}\ x,\%r\rrbracket\kappa\ \sigma R F&=\kappa\ \sigma\ R[r:=\operatorname{car}(\sigma(x))]\ F\\
\llbracket\mathrm{snd}\ x,\%r\rrbracket\kappa\ \sigma R F&=\kappa\ \sigma\ R[r:=\operatorname{cdr}(\sigma(x))]\ F\\
\llbracket\mathrm{callq}\ a\rrbracket\kappa\ \sigma R F&=\operatorname{guard}(C,\kappa\ \sigma R_C\ \{\langle\rangle\})
\end{aligned} [ [ movq a , dst ] ] κ σ R F [ [ subq a , % r ] ] κ σ R F [ [ cmpq a 2 , a 1 ] ] κ σ R F [ [ sete dst ] ] κ σ R F [ [ alloc con , dst ] ] κ σ R F [ [ load get i a , dst ] ] κ σ R F [ [ fst x , % r ] ] κ σ R F [ [ snd x , % r ] ] κ σ R F [ [ callq a ] ] κ σ R F = put ( dst , O [ [ a ] ] σ R ) κ σ R F = κ σ R [ r := prim sub ( R ( r ) , O [ [ a ] ] σ R )] F = κ σ R ( prim eq ( O [ [ a 1 ] ] σ R , O [ [ a 2 ] ] σ R ) ) = put ( dst , F ) κ σ R F = put ( dst , O [ [ con ] ] σ R ) κ σ R F = put ( dst , π tup i ( O [ [ a ] ] σ R )) κ σ R F = κ σ R [ r := car ( σ ( x ))] F = κ σ R [ r := cdr ( σ ( x ))] F = guard ( C , κ σ R C {⟨ ⟩})
where C = ( O ⟦ a ⟧ σ R ∙ R ( r d i ) ) ∙ R ( r s i ) C=(O\llbracket a\rrbracket_{\sigma R}\bullet R(\mathrm{rdi}))\bullet R(\mathrm{rsi}) C = ( O [ [ a ] ] σ R ∙ R ( rdi )) ∙ R ( rsi )
Here R C R_C R C maps rax to C C C and the other registers to { ⟨ ⟩ } \{\langle\rangle\} {⟨ ⟩} . Only a call can diverge, so only callq needs a guard, which makes the rest of the sequence strict in the result C C C .
Semantics of sequences
⟦ s ⟧ β σ R F = D \llbracket s\rrbracket\beta\ \sigma R F=D [ [ s ] ] β σ R F = D
\llbracket i; s \rrbrbracket_B = \llbracket i \rrbracket\llbracket s \rrbracket_B
⟦ retq ⟧ B σ R F = R ( rax ) \llbracket\operatorname{retq} \rrbracket_B \sigma_{RF} = R(\operatorname{rax}) [ [ retq ] ] B σ R F = R ( rax )
⟦ jmp l ⟧ B σ R F = B ( l ) ( σ ) \llbracket\operatorname{jmp}\ l \rrbracket_B \sigma_{RF} = B(l)(\sigma) [ [ jmp l ] ] B σ R F = B ( l ) ( σ )
⟦ case x y z l 1 l 2 ⟧ B σ R F = C ( σ ( x ) , λ X . B ( l 1 ) ( σ [ y : = X ] ) , λ X . B ( l 2 ) ( σ [ z : = X ] ) ) \llbracket\operatorname{case}\ x\ y\ z\ l_1\ l_2 \rrbracket_B\ \sigma_{RF}
= C\left(\sigma(x),\lambda X.B(l_1)(\sigma[y:=X]),\lambda X.B(l_2)(\sigma[z:=X])\right) [ [ case x y z l 1 l 2 ] ] B σ R F = C ( σ ( x ) , λ X . B ( l 1 ) ( σ [ y := X ]) , λ X . B ( l 2 ) ( σ [ z := X ]) )
where a block means B ( l ) ( σ ) = ⟦ s l ⟧ B l σ R 0 { ⟨ ⟩ } B(l)(\sigma)=\llbracket s_l\rrbracket_{B_l}\sigma R_0\{\langle\rangle\} B ( l ) ( σ ) = [ [ s l ] ] B l σ R 0 {⟨ ⟩} , with R 0 R_0 R 0 mapping every register to { ⟨ ⟩ } \{\langle\rangle\} {⟨ ⟩} and B l B_l B l giving the blocks older than l l l . A definition means T ( k ) = Λ ( λ X . Λ ( λ Y . ⟦ s ⟧ B ρ 0 R X , Y { ⟨ ⟩ } ) ) T(k)=\Lambda(\lambda X.\Lambda(\lambda Y.\llbracket s\rrbracket_B\rho_0R_{X,Y}\{\langle\rangle\})) T ( k ) = Λ ( λ X .Λ ( λY . [ [ s ] ] B ρ 0 R X , Y {⟨ ⟩})) for its function body s s s with blocks B B B , where R X , Y R_{X,Y} R X , Y maps rdi to X X X , rsi to Y Y Y , and rax to { ⟨ ⟩ } \{\langle\rangle\} {⟨ ⟩} ; the tables are as in GlobalFun. A program means its main function body:
⟦ d ⃗ ; ( Φ ; s 0 , … , s j − 1 ; s ) ⟧ = ⟦ s ⟧ B ρ 0 R 0 { ⟨ ⟩ } \llbracket\vec{d};\;(\Phi;s_0,\ldots,s_{j-1};s)\rrbracket
= \llbracket s\rrbracket_B\rho_0R_0\{\langle\rangle\} [ [ d ; ( Φ ; s 0 , … , s j − 1 ; s ) ] ] = [ [ s ] ] B ρ 0 R 0 {⟨ ⟩}
Its start sequence s s s runs with the meanings B B B of its blocks s 0 , … , s j − 1 s_0,\ldots,s_{j-1} s 0 , … , s j − 1 , as above, in the frame ρ 0 \rho_0 ρ 0 , which maps every slot to { ⟨ ⟩ } \{\langle\rangle\} {⟨ ⟩} , with the registers R 0 R_0 R 0 , which are all unset, and with the flag { ⟨ ⟩ } \{\langle\rangle\} {⟨ ⟩} , which is unset.
The Compiler Passes
Agda: FPC/Compiler/Pipeline.agda (compile)
This appendix defines each pass of § 2 as a recursive function on the syntax of Figure 2 . In each pass, the clauses not shown are homomorphic. They translate the subterms and rebuild the same form. No pass inspects a recursive type; fold and unfold pass through every pass as operations with one operand. The subsections follow the order of the pipeline; § 4.1 and § 4.2 and Appendix § 13 prove each definition correct. The compiler is the composition of the passes:
compile = select ∘ explicate ∘ lift ∘ rco ∘ hoist ∘ split ∘ enclose ∘ simplify \operatorname{compile}=\operatorname{select}\circ\operatorname{explicate}\circ\operatorname{lift}\circ\operatorname{rco}\circ\operatorname{hoist}
\circ\operatorname{split}\circ\operatorname{enclose}\circ\operatorname{simplify} compile = select ∘ explicate ∘ lift ∘ rco ∘ hoist ∘ split ∘ enclose ∘ simplify
simplify (FPC to FPC)
Agda: FPC/Compiler/Simplify.agda (simplify)
The pass works with syntactic values , the terms
V : : = x ∣ λ x . N ∣ c ∣ tuple ( V 1 , … , V n ) ∣ inl V ∣ inr V ∣ fold V V ::= x \mid\lambda x.N \mid c \mid\operatorname{tuple}(V_1,\ldots,V_n) \mid\operatorname{inl}\ V \mid\operatorname{inr}\ V \mid\operatorname{fold}\ V V ::= x ∣ λ x . N ∣ c ∣ tuple ( V 1 , … , V n ) ∣ inl V ∣ inr V ∣ fold V
A syntactic value is small when it is a variable, a constant, ⟨ ⟩ \langle\rangle ⟨ ⟩ , or inl a \operatorname{inl}\ a inl a or inr a \operatorname{inr}\ a inr a of one of those; only small values are duplicated. On a term, simplify returns a pair ( M ′ , θ ) (M',\theta) ( M ′ , θ ) of the simplified term M ′ M' M ′ and the static information θ \theta θ about the term’s value:
θ : : = V ∣ ⟨ λ x . N , E ⟩ ∣ ⊥ \theta::= V \mid\langle\lambda x.N,E\rangle\mid\bot θ ::= V ∣ ⟨ λ x . N , E ⟩ ∣ ⊥
that is, a syntactic value V V V that the term is known to equal, a function ⟨ λ x . N , E ⟩ \langle\lambda x.N,E\rangle ⟨ λ x . N , E ⟩ (its body before simplification, with the environment E E E where it was defined), or nothing, ⊥ \bot ⊥ . An environment E E E maps each variable to such a pair: the term that replaces the variable and the static information about its value. We write s e l f ( V ) = ( V , V ) self(V)=(V,V) se l f ( V ) = ( V , V ) , and i n f o ( M ) = ( M , M ) info(M)=(M,M) in f o ( M ) = ( M , M ) if M M M is a syntactic value and ( M , ⊥ ) (M,\bot) ( M , ⊥ ) otherwise. Under a binder x x x that stays, x x x is itself and is known only as itself: E ′ = E [ x : = s e l f ( x ) ] E'=E[x:=self(x)] E ′ = E [ x := se l f ( x )] .
The pass carries fuel ϕ \phi ϕ that limits nested inlining. Every equation passes ϕ \phi ϕ along unchanged except the one for a call, which inlines with one less. We write the fuel as a superscript, as in simp E ϕ \operatorname{simp}^{\phi}_{E} simp E ϕ , and omit it wherever it does not change.
simp E ( x ) = E ( x ) simp E ( λ x . N ) = ( λ x . N ′ , ⟨ λ x . N , E ⟩ ) where ( N ′ , θ ) = simp E [ x : = self ( x ) ] ( N ) simp E ( L M ) = app E ( L , simp E ( M ) ) simp E ( c ) = self ( c ) simp E ( tuple ( M 1 , … , M n ) ) = info ( tuple ( M 1 ′ , … , M n ′ ) ) where ( M i ′ , θ i ) = simp E ( M i ) simp E ( get i L ) = get i ( simp E ( L ) ) simp E ( inl M ) = info ( inl M ′ ) where ( M ′ , θ ) = simp E ( M ) and likewise for inr and fold simp E ( case L ( x . M ) ( y . N ) ) = case E ( simp E ( L ) , x . M , y . N ) simp E ( prim p L M ) = prim p ( simp E ( L ) , simp E ( M ) ) simp E ( let x = M in N ) = let E ( x . N , simp E ( M ) ) simp E ( unfold L ) = unfold ( simp E ( L ) ) \begin{aligned}
\operatorname{simp}_{E}(x) &= E(x) \\
\operatorname{simp}_{E}(\lambda x.N) &= (\lambda x.N', \langle\lambda x.N,E\rangle) \\
&\text{where } (N',\theta)=\operatorname{simp}_{E[x:=\operatorname{self}(x)]}(N) \\
\operatorname{simp}_{E}(L\ M) &= \operatorname{app}_{E}(L,\operatorname{simp}_{E}(M)) \\
\operatorname{simp}_{E}(c) &= \operatorname{self}(c) \\
\operatorname{simp}_{E}(\operatorname{tuple}(M_1,\ldots,M_n)) &= \operatorname{info}(\operatorname{tuple}(M'_1,\ldots,M'_n)) \\
&\text{where } (M'_i,\theta_i)=\operatorname{simp}_{E}(M_i) \\
\operatorname{simp}_{E}(\operatorname{get}_i\ L) &= \operatorname{get}_i(\operatorname{simp}_{E}(L)) \\
\operatorname{simp}_{E}(\operatorname{inl}\ M) &= \operatorname{info}(\operatorname{inl}\ M')\quad\text{where }(M',\theta)=\operatorname{simp}_{E}(M) \\
&\text{and likewise for inr and fold} \\
\operatorname{simp}_{E}(\operatorname{case}\ L\ (x.M)\ (y.N)) &= \operatorname{case}_{E}(\operatorname{simp}_{E}(L),x.M,y.N) \\
\operatorname{simp}_{E}(\operatorname{prim}_p\ L\ M) &= \operatorname{prim}_p(\operatorname{simp}_{E}(L),\operatorname{simp}_{E}(M)) \\
\operatorname{simp}_{E}(\operatorname{let}\ x=M\ \operatorname{in}\ N) &= \operatorname{let}_{E}(x.N,\operatorname{simp}_{E}(M)) \\
\operatorname{simp}_{E}(\operatorname{unfold}\ L) &= \operatorname{unfold}(\operatorname{simp}_{E}(L))
\end{aligned} simp E ( x ) simp E ( λ x . N ) simp E ( L M ) simp E ( c ) simp E ( tuple ( M 1 , … , M n )) simp E ( get i L ) simp E ( inl M ) simp E ( case L ( x . M ) ( y . N )) simp E ( prim p L M ) simp E ( let x = M in N ) simp E ( unfold L ) = E ( x ) = ( λ x . N ′ , ⟨ λ x . N , E ⟩) where ( N ′ , θ ) = simp E [ x := self ( x )] ( N ) = app E ( L , simp E ( M )) = self ( c ) = info ( tuple ( M 1 ′ , … , M n ′ )) where ( M i ′ , θ i ) = simp E ( M i ) = get i ( simp E ( L )) = info ( inl M ′ ) where ( M ′ , θ ) = simp E ( M ) and likewise for inr and fold = case E ( simp E ( L ) , x . M , y . N ) = prim p ( simp E ( L ) , simp E ( M )) = let E ( x . N , simp E ( M )) = unfold ( simp E ( L ))
An application of a λ \lambda λ -abstraction is a β \beta β -redex; any other application is a call, which inlines a known function when there is fuel left and its body, simplified at the call, has at most 24 nodes:
app E ( λ x . N , I ) = let E ( x . N , I ) app E ( L , I ) = call ( simp E ( L ) , I ) otherwise call ϕ + 1 ( ( L ′ , ⟨ λ x . N , E f ⟩ ) , I ) = P if ∣ P ∣ ≤ 24 , where P = let E f ϕ ( x . N , I ) = ( L ′ M ′ , ⊥ ) otherwise, where I = ( M ′ , θ ′ ) call ϕ ( ( L ′ , θ ) , ( M ′ , θ ′ ) ) = ( L ′ M ′ , ⊥ ) in all other cases \begin{aligned}
\operatorname{app}_{E}(\lambda x.N,I) &= \operatorname{let}_{E}(x.N,I) \\
\operatorname{app}_{E}(L,I) &= \operatorname{call}(\operatorname{simp}_{E}(L),I)\qquad\text{otherwise} \\
\operatorname{call}^{\phi+1}((L',\langle\lambda x.N,E_f\rangle),I) &= P\quad\text{if }|P|\leq24,\quad\text{where }P=\operatorname{let}_{E_f}^{\phi}(x.N,I) \\
&=(L'M',\bot)\quad\text{otherwise, where }I=(M',\theta') \\
\operatorname{call}^{\phi}((L',\theta),(M',\theta')) &=(L'M',\bot)\quad\text{in all other cases}
\end{aligned} app E ( λ x . N , I ) app E ( L , I ) call ϕ + 1 (( L ′ , ⟨ λ x . N , E f ⟩) , I ) call ϕ (( L ′ , θ ) , ( M ′ , θ ′ )) = let E ( x . N , I ) = call ( simp E ( L ) , I ) otherwise = P if ∣ P ∣ ≤ 24 , where P = let E f ϕ ( x . N , I ) = ( L ′ M ′ , ⊥ ) otherwise, where I = ( M ′ , θ ′ ) = ( L ′ M ′ , ⊥ ) in all other cases
Here ∣ P ∣ |P| ∣ P ∣ is the number of nodes in the term of P P P . A body bound to an operand, by let, β \beta β , or inlining, substitutes the operand if it is small, and otherwise keeps it bound by a let:
let E ( x . N , ( M ′ , θ ) ) = simp E [ x : = ( M ′ , θ ) ] ( N ) if M ′ is a small syntactic value = ( let x = M ′ in N ′ , ⊥ ) if M ′ is another syntactic value , where ( N ′ , θ ′ ′ ) = simp E [ x : = ( x , θ ) ] ( N ) = ( let x = M ′ in N ′ , ⊥ ) otherwise , where ( N ′ , θ ′ ′ ) = simp E [ x : = self ( x ) ] ( N ) \begin{aligned}
\operatorname{let}_{E}(x.N,(M',\theta)) &= \operatorname{simp}_{E[x:=(M',\theta)]}(N)\quad\text{if }M'\text{ is a small syntactic value} \\
&=(\operatorname{let}\ x=M'\ \operatorname{in}\ N',\bot)\quad\text{if }M'\text{ is another syntactic value}, \\
&\qquad\text{where }(N',\theta'')=\operatorname{simp}_{E[x:=(x,\theta)]}(N) \\
&=(\operatorname{let}\ x=M'\ \operatorname{in}\ N',\bot)\quad\text{otherwise}, \\
&\qquad\text{where }(N',\theta'')=\operatorname{simp}_{E[x:=\operatorname{self}(x)]}(N)
\end{aligned} let E ( x . N , ( M ′ , θ )) = simp E [ x := ( M ′ , θ )] ( N ) if M ′ is a small syntactic value = ( let x = M ′ in N ′ , ⊥ ) if M ′ is another syntactic value , where ( N ′ , θ ′′ ) = simp E [ x := ( x , θ )] ( N ) = ( let x = M ′ in N ′ , ⊥ ) otherwise , where ( N ′ , θ ′′ ) = simp E [ x := self ( x )] ( N )
A projection, unfold, case, or primitive on a known value reduces:
get i ( ( L ′ , tuple ( θ 1 , … , θ n ) ) ) = self ( θ i ) if θ i is small = ( get i L ′ , θ i ) otherwise get i ( ( L ′ , θ ) ) = ( get i L ′ , ⊥ ) if θ is not a tuple unfold ( ( L ′ , fold θ ) ) = self ( θ ) if θ is small = ( unfold L ′ , θ ) otherwise unfold ( ( L ′ , θ ) ) = ( unfold L ′ , ⊥ ) if θ is not a fold case E ( ( L ′ , inl θ ) , x . M , y . N ) = simp E [ x : = self ( θ ) ] ( M ) if θ is small (and likewise for inr) case E ( ( L ′ , θ ) , x . M , y . N ) = ( case L ′ ( x . M ′ ) ( y . N ′ ) , ⊥ ) otherwise , where ( M ′ , θ 1 ) = simp E [ x : = self ( x ) ] ( M ) and ( N ′ , θ 2 ) = simp E [ y : = self ( y ) ] ( N ) prim p ( ( L ′ , n ) , ( M ′ , m ) ) = self ( res ( δ ( p , n , m ) ) ) if n and m are integers prim p ( ( L ′ , θ 1 ) , ( M ′ , θ 2 ) ) = ( prim p L ′ M ′ , ⊥ ) otherwise \begin{aligned}
\operatorname{get}_i((L',\operatorname{tuple}(\theta_1,\ldots,\theta_n))) &= \operatorname{self}(\theta_i)\quad\text{if }\theta_i\text{ is small} \\
&=(\operatorname{get}_i\ L',\theta_i)\quad\text{otherwise} \\
\operatorname{get}_i((L',\theta)) &=(\operatorname{get}_i\ L',\bot)\quad\text{if }\theta\text{ is not a tuple} \\
\operatorname{unfold}((L',\operatorname{fold}\ \theta)) &= \operatorname{self}(\theta)\quad\text{if }\theta\text{ is small} \\
&=(\operatorname{unfold}\ L',\theta)\quad\text{otherwise} \\
\operatorname{unfold}((L',\theta)) &=(\operatorname{unfold}\ L',\bot)\quad\text{if }\theta\text{ is not a fold} \\
\operatorname{case}_{E}((L',\operatorname{inl}\ \theta),x.M,y.N) &= \operatorname{simp}_{E[x:=\operatorname{self}(\theta)]}(M)\quad\text{if }\theta\text{ is small} \\
&\text{(and likewise for inr)} \\
\operatorname{case}_{E}((L',\theta),x.M,y.N) &=(\operatorname{case}\ L'\ (x.M')\ (y.N'),\bot)\quad\text{otherwise}, \\
&\qquad\text{where }(M',\theta_1)=\operatorname{simp}_{E[x:=\operatorname{self}(x)]}(M) \\
&\qquad\text{and }(N',\theta_2)=\operatorname{simp}_{E[y:=\operatorname{self}(y)]}(N) \\
\operatorname{prim}_p((L',n),(M',m)) &= \operatorname{self}(\operatorname{res}(\delta(p,n,m)))\quad\text{if }n\text{ and }m\text{ are integers} \\
\operatorname{prim}_p((L',\theta_1),(M',\theta_2)) &=(\operatorname{prim}_p\ L'\ M',\bot)\quad\text{otherwise}
\end{aligned} get i (( L ′ , tuple ( θ 1 , … , θ n ))) get i (( L ′ , θ )) unfold (( L ′ , fold θ )) unfold (( L ′ , θ )) case E (( L ′ , inl θ ) , x . M , y . N ) case E (( L ′ , θ ) , x . M , y . N ) prim p (( L ′ , n ) , ( M ′ , m )) prim p (( L ′ , θ 1 ) , ( M ′ , θ 2 )) = self ( θ i ) if θ i is small = ( get i L ′ , θ i ) otherwise = ( get i L ′ , ⊥ ) if θ is not a tuple = self ( θ ) if θ is small = ( unfold L ′ , θ ) otherwise = ( unfold L ′ , ⊥ ) if θ is not a fold = simp E [ x := self ( θ )] ( M ) if θ is small (and likewise for inr) = ( case L ′ ( x . M ′ ) ( y . N ′ ) , ⊥ ) otherwise , where ( M ′ , θ 1 ) = simp E [ x := self ( x )] ( M ) and ( N ′ , θ 2 ) = simp E [ y := self ( y )] ( N ) = self ( res ( δ ( p , n , m ))) if n and m are integers = ( prim p L ′ M ′ , ⊥ ) otherwise
Here res ( k ) = k \operatorname{res}(k)=k res ( k ) = k for an integer k k k , res ( t r u e ) = inl ⟨ ⟩ \operatorname{res}(\mathrm{true})=\operatorname{inl}\langle\rangle res ( true ) = inl ⟨ ⟩ , and res ( f a l s e ) = inr ⟨ ⟩ \operatorname{res}(\mathrm{false})=\operatorname{inr}\langle\rangle res ( false ) = inr ⟨ ⟩ . Finally,
s i m p l i f y ( M ) = M ′ \mathbf{simplify}(M)=M' simplify ( M ) = M ′
where ( M ′ , θ ) = s i m p E 0 4 ( M ) (M',\theta)=\mathbf{simp}_{E_0}^{4}(M) ( M ′ , θ ) = simp E 0 4 ( M ) and E 0 ( x ) = self ( x ) E_0(x)=\operatorname{self}(x) E 0 ( x ) = self ( x ) for every x x x .
simplify drops only evaluations whose value is known, which have no effect, and otherwise keeps the order of evaluation. An operand that is not a syntactic value is never substituted but stays bound by a let.
enclose
Agda: FPC/Compiler/{Capture,Prune,Concretize,Pipeline}.agda (capture, prune-program, concretize-program, enclose)
enclose works in three steps, which go through the intermediate language Clos:
e n c l o s e = concretize ∅ ∘ p r u n e ∘ c a p t u r e \mathbf{enclose}=\operatorname{concretize}_{\emptyset}\circ\mathbf{prune}\circ\mathbf{capture} enclose = concretize ∅ ∘ prune ∘ capture
Capture. The first step makes each function a closure that captures the variables in scope, in order:
c a p t u r e ( λ y . N ) = clos ( x 1 , … , x k , y . c a p t u r e ( N ) ) ( x 1 , … , x k ) \mathbf{capture}(\lambda y.N)=\operatorname{clos}(x_1,\ldots,x_k,y.\mathbf{capture}(N))(x_1,\ldots,x_k) capture ( λ y . N ) = clos ( x 1 , … , x k , y . capture ( N )) ( x 1 , … , x k )
where x 1 , … , x k x_1,\ldots,x_k x 1 , … , x k are the variables in scope.
Prune. The second step removes the captured values that the code does not use. Closures are strict, so a captured term is dropped only when the code does not use its name and the term is a variable, which is all that capture captures:
p r u n e ( clos ( x 1 , … , x n , y . N ) ( M 1 , … , M n ) ) = clos ( x i 1 , … , x i k , y . p r u n e ( N ) ) ( p r u n e ( M i 1 ) , … , p r u n e ( M i k ) ) \begin{aligned}
\mathbf{prune}(\operatorname{clos}(x_1,\ldots,x_n,y.N)(M_1,\ldots,M_n))={}&\\
\operatorname{clos}(x_{i_1},\ldots,x_{i_k},y.\mathbf{prune}(N))(&\mathbf{prune}(M_{i_1}),\ldots,\mathbf{prune}(M_{i_k}))
\end{aligned} prune ( clos ( x 1 , … , x n , y . N ) ( M 1 , … , M n )) = clos ( x i 1 , … , x i k , y . prune ( N )) ( prune ( M i 1 ) , … , prune ( M i k ))
where { i 1 < ⋯ < i k } = { i ∣ x i ∈ F V ( N ) ∨ M i is not a variable } \{i_1<\cdots<i_k\}=\{i\mid x_i\in FV(N)\mathbin{\lor}M_i\text{ is not a variable}\} { i 1 < ⋯ < i k } = { i ∣ x i ∈ F V ( N ) ∨ M i is not a variable }
Concretize. The last step stores the captured values of each closure in one tuple, bound to z z z in its code, and the code reads the name x i x_i x i as get i z \operatorname{get}_i z get i z . The map φ \varphi φ sends each Clos variable to a reference, x x x or get i z \operatorname{get}_i z get i z , in EnvClos:
concretize φ ( x ) = φ ( x ) \operatorname{concretize}_{\varphi}(x)=\varphi(x) concretize φ ( x ) = φ ( x )
concretize φ ( clos ( x 1 , … , x n , y . N ) ( M 1 , … , M n ) ) = clos ( z , y . concretize φ ′ ( N ) ) ( concretize φ ( M 1 ) , … , concretize φ ( M n ) ) \begin{aligned}
\operatorname{concretize}_{\varphi}(\operatorname{clos}(x_1,\ldots,x_n,y.N)(M_1,\ldots,M_n))={}&\\
\operatorname{clos}(z,y.\operatorname{concretize}_{\varphi'}(N))(&\operatorname{concretize}_{\varphi}(M_1),\ldots,\operatorname{concretize}_{\varphi}(M_n))
\end{aligned} concretize φ ( clos ( x 1 , … , x n , y . N ) ( M 1 , … , M n )) = clos ( z , y . concretize φ ′ ( N )) ( concretize φ ( M 1 ) , … , concretize φ ( M n ))
where φ ′ = { y ↦ y , x 1 ↦ get 1 z , … , x n ↦ get n z } \varphi'=\{y\mapsto y,x_1\mapsto\operatorname{get}_1z,\ldots,x_n\mapsto\operatorname{get}_nz\} φ ′ = { y ↦ y , x 1 ↦ get 1 z , … , x n ↦ get n z }
Under a let or case the bound variable maps to itself, and a program starts with the empty map.
split closure
Agda: FPC/Compiler/Split.agda (split-program)
A closure becomes a pair of its code and the tuple of its captured values, and an application opens the closure and calls its code:
s p l i t ( clos ( z , y . N ) M ⃗ ) = pair ( code ( z , y . s p l i t ( N ) ) ) ( tuple s p l i t ( M ⃗ ) ) \mathbf{split}(\operatorname{clos}(z,y.N)\,\vec{M})=\operatorname{pair}(\operatorname{code}(z,y.\mathbf{split}(N)))(\operatorname{tuple}\,\mathbf{split}(\vec{M})) split ( clos ( z , y . N ) M ) = pair ( code ( z , y . split ( N ))) ( tuple split ( M ))
s p l i t ( L M ) = callc s p l i t ( L ) s p l i t ( M ) \mathbf{split}(L\,M)=\operatorname{callc}\,\mathbf{split}(L)\,\mathbf{split}(M) split ( L M ) = callc split ( L ) split ( M )
hoist
Agda: FPC/Compiler/Hoist.agda (hoist)
The code of every function becomes a global definition, and the code itself a reference to it. The pass threads the list d ⃗ \vec{d} d of the definitions made so far, from left to right. For code, it first translates the body, so the functions nested in it become definitions first. For any other form o p op o p with subterms A 1 , … , A n A_1,\ldots,A_n A 1 , … , A n (each under its binders x ⃗ i \vec{x}_i x i ):
glob ( x , d ⃗ ) = ( x , d ⃗ ) glob ( code ( z , y . N ) , d ⃗ ) = ( f ∣ d ⃗ 1 ∣ , ( d ⃗ 1 , code ( z , y . N ′ ) ) ) where ( N ′ , d ⃗ 1 ) = glob ( N , d ⃗ ) glob ( o p ( x ⃗ 1 . A 1 , … , x ⃗ n . A n ) , d ⃗ 0 ) = ( o p ( x ⃗ 1 . A 1 ′ , … , x ⃗ n . A n ′ ) , d ⃗ n ) where ( A i ′ , d ⃗ i ) = glob ( A i , d ⃗ i − 1 ) hoist ( M ) = ( d ⃗ ; M ′ ) where ( M ′ , d ⃗ ) = glob ( M , ϵ ) \begin{aligned}
\operatorname{glob}(x,\vec{d}) &= (x,\vec{d})\\
\operatorname{glob}(\operatorname{code}(z,y.N),\vec{d}) &= (f_{\lvert\vec{d}_1\rvert},(\vec{d}_1,\operatorname{code}(z,y.N')))\\
&\qquad\text{where }(N',\vec{d}_1)=\operatorname{glob}(N,\vec{d})\\
\operatorname{glob}(op(\vec{x}_1.A_1,\ldots,\vec{x}_n.A_n),\vec{d}_0) &= (op(\vec{x}_1.A'_1,\ldots,\vec{x}_n.A'_n),\vec{d}_n)\\
&\qquad\text{where }(A'_i,\vec{d}_i)=\operatorname{glob}(A_i,\vec{d}_{i-1})\\
\operatorname{hoist}(M) &= (\vec{d};M')\quad\text{where }(M',\vec{d})=\operatorname{glob}(M,\epsilon)
\end{aligned} glob ( x , d ) glob ( code ( z , y . N ) , d ) glob ( o p ( x 1 . A 1 , … , x n . A n ) , d 0 ) hoist ( M ) = ( x , d ) = ( f ∣ d 1 ∣ , ( d 1 , code ( z , y . N ′ ))) where ( N ′ , d 1 ) = glob ( N , d ) = ( o p ( x 1 . A 1 ′ , … , x n . A n ′ ) , d n ) where ( A i ′ , d i ) = glob ( A i , d i − 1 ) = ( d ; M ′ ) where ( M ′ , d ) = glob ( M , ϵ )
Each definition refers only to the definitions made before it.
remove complex operands (GlobalFun to MNF)
Agda: FPC/Compiler/RCO.agda (rco-program)
Every complex operand is bound to a fresh variable by a let, from left to right, so the operands of every operation become atoms. A case or let is complex; its branches and body are translated in place, so nothing is duplicated. An operand that is an atom stays in place; any other operand is translated and bound to a fresh variable:
a t o m i z e ( a ) = ( ϵ ; a ) a t o m i z e ( M ) = ( x = r c o ( M ) ; x ) otherwise ( x fresh ) a t o m i z e ( M 1 , … , M n ) = ( b ⃗ 1 ⋯ b ⃗ n ; a 1 , … , a n ) where ( b ⃗ i ; a i ) = a t o m i z e ( M i ) l e t s ( x 1 = N 1 ⋯ x k = N k , N ) = let x 1 = N 1 in ⋯ let x k = N k in N r c o ( a ) = a r c o ( o ( M 1 , … , M n ) ) = l e t s ( b ⃗ , o ( a 1 , … , a n ) ) where ( b ⃗ ; a 1 , … , a n ) = a t o m i z e ( M 1 , … , M n ) r c o ( case L ( x . M ) ( y . N ) ) = l e t s ( b ⃗ , case a ( x . r c o ( M ) ) ( y . r c o ( N ) ) ) where ( b ⃗ ; a ) = a t o m i z e ( L ) r c o ( let x = M in N ) = let x = r c o ( M ) in r c o ( N ) r c o ( code ( z , y . N ) ) = code ( z , y . r c o ( N ) ) r c o ( ( d 0 , … , d m − 1 ; M ) ) = ( r c o ( d 0 ) , … , r c o ( d m − 1 ) ; r c o ( M ) ) \begin{aligned}
\mathbf{atomize}(a) &= (\epsilon; a)\\
\mathbf{atomize}(M) &= (x=\mathbf{rco}(M); x)\quad\text{otherwise }(x\text{ fresh})\\
\mathbf{atomize}(M_1,\ldots,M_n) &= (\vec{b}_1\cdots\vec{b}_n; a_1,\ldots,a_n)\quad\text{where }(\vec{b}_i; a_i)=\mathbf{atomize}(M_i)\\
\mathbf{lets}(x_1=N_1\cdots x_k=N_k,N) &= \operatorname{let}x_1=N_1\operatorname{in}\cdots\operatorname{let}x_k=N_k\operatorname{in}N\\
\mathbf{rco}(a) &= a\\
\mathbf{rco}(o(M_1,\ldots,M_n)) &= \mathbf{lets}(\vec{b},o(a_1,\ldots,a_n))\\
&\qquad\text{where }(\vec{b};a_1,\ldots,a_n)=\mathbf{atomize}(M_1,\ldots,M_n)\\
\mathbf{rco}(\operatorname{case}L\ (x.M)\ (y.N)) &= \mathbf{lets}(\vec{b},\operatorname{case}a\ (x.\mathbf{rco}(M))\ (y.\mathbf{rco}(N)))\\
&\qquad\text{where }(\vec{b};a)=\mathbf{atomize}(L)\\
\mathbf{rco}(\operatorname{let}x=M\operatorname{in}N) &= \operatorname{let}x=\mathbf{rco}(M)\operatorname{in}\mathbf{rco}(N)\\
\mathbf{rco}(\operatorname{code}(z,y.N)) &= \operatorname{code}(z,y.\mathbf{rco}(N))\\
\mathbf{rco}((d_0,\ldots,d_{m-1};M)) &= (\mathbf{rco}(d_0),\ldots,\mathbf{rco}(d_{m-1});\mathbf{rco}(M))
\end{aligned} atomize ( a ) atomize ( M ) atomize ( M 1 , … , M n ) lets ( x 1 = N 1 ⋯ x k = N k , N ) rco ( a ) rco ( o ( M 1 , … , M n )) rco ( case L ( x . M ) ( y . N )) rco ( let x = M in N ) rco ( code ( z , y . N )) rco (( d 0 , … , d m − 1 ; M )) = ( ϵ ; a ) = ( x = rco ( M ) ; x ) otherwise ( x fresh ) = ( b 1 ⋯ b n ; a 1 , … , a n ) where ( b i ; a i ) = atomize ( M i ) = let x 1 = N 1 in ⋯ let x k = N k in N = a = lets ( b , o ( a 1 , … , a n )) where ( b ; a 1 , … , a n ) = atomize ( M 1 , … , M n ) = lets ( b , case a ( x . rco ( M )) ( y . rco ( N ))) where ( b ; a ) = atomize ( L ) = let x = rco ( M ) in rco ( N ) = code ( z , y . rco ( N )) = ( rco ( d 0 ) , … , rco ( d m − 1 ) ; rco ( M ))
where o o o ranges over the operations of MNF (Figure 2 ).
lift locals (MNF to Imp)
Agda: FPC/Compiler/LiftLocals.agda (lift-program)
Every variable that a function body binds gets a slot of its own. The translation lift Γ , n ( e ) \operatorname{lift}_{\Gamma,n}(e) lift Γ , n ( e ) takes the map Γ \Gamma Γ from the variables in scope to their slots and the first unused slot n n n , and returns the translation of e e e and the first slot that it leaves unused. A variable x x x becomes its slot Γ ( x ) \Gamma(x) Γ ( x ) . A let gives its variable the slot after those of the variables bound in its right-hand side, and a case gives a slot to the variable of each branch, before the variables bound in that branch. A definition code ( z , y . N ) \operatorname{code}(z,y.N) code ( z , y . N ) starts with z z z in slot 0 and y y y in slot 1, and the main expression starts with no variables in scope.
l i f t Γ , n ( x ) = ( Γ ( x ) , n ) l i f t Γ , n ( l e t x = M i n N ) = ( s : = e 1 ; e 2 , n 2 ) w h e r e ( e 1 , s ) = l i f t Γ , n ( M ) a n d ( e 2 , n 2 ) = l i f t Γ [ x : = s ] , s + 1 ( N ) l i f t Γ , n ( c a s e a ( x . M ) ( y . N ) ) = ( c a s e l i f t Γ ( a ) n n 1 e 1 e 2 , n 2 ) w h e r e ( e 1 , n 1 ) = l i f t Γ [ x : = n ] , n + 1 ( M ) a n d ( e 2 , n 2 ) = l i f t Γ [ y : = n 1 ] , n 1 + 1 ( N ) l i f t ( c o d e ( z , y . N ) ) = ( Φ ; e ) w h e r e ( e , n ) = l i f t [ z : = 0 , y : = 1 ] , 2 ( N ) l i f t ( ( d 0 , … , d m − 1 ; M ) ) = ( l i f t ( d 0 ) , … , l i f t ( d m − 1 ) ; ( Φ ; e ) ) w h e r e ( e , n ) = l i f t ∅ , 0 ( M ) \begin{aligned}
\mathbf{lift}_{\Gamma,n}(x) &= (\Gamma(x),n) \\
\mathbf{lift}_{\Gamma,n}(\mathbf{let}\ x=M\ \mathbf{in}\ N) &= (s := e_1; e_2,n_2) \\
&\qquad\mathbf{where}\ (e_1,s)=\mathbf{lift}_{\Gamma,n}(M)\ \mathbf{and}\ (e_2,n_2)=\mathbf{lift}_{\Gamma[x:=s],s+1}(N) \\
\mathbf{lift}_{\Gamma,n}(\mathbf{case}\ a\ (x.M)\ (y.N)) &= (\mathbf{case}\ \mathbf{lift}_{\Gamma}(a)\ n\ n_1\ e_1\ e_2,n_2) \\
&\qquad\mathbf{where}\ (e_1,n_1)=\mathbf{lift}_{\Gamma[x:=n],n+1}(M)\ \mathbf{and}\ (e_2,n_2)=\mathbf{lift}_{\Gamma[y:=n_1],n_1+1}(N) \\
\mathbf{lift}(\mathbf{code}\ (z,y.N)) &= (\Phi;e)\quad\mathbf{where}\ (e,n)=\mathbf{lift}_{[z:=0,y:=1],2}(N) \\
\mathbf{lift}((d_0,\ldots,d_{m-1};M)) &= (\mathbf{lift}(d_0),\ldots,\mathbf{lift}(d_{m-1});(\Phi;e))\quad\mathbf{where}\ (e,n)=\mathbf{lift}_{\varnothing,0}(M)
\end{aligned} lift Γ , n ( x ) lift Γ , n ( let x = M in N ) lift Γ , n ( case a ( x . M ) ( y . N )) lift ( code ( z , y . N )) lift (( d 0 , … , d m − 1 ; M )) = ( Γ ( x ) , n ) = ( s := e 1 ; e 2 , n 2 ) where ( e 1 , s ) = lift Γ , n ( M ) and ( e 2 , n 2 ) = lift Γ [ x := s ] , s + 1 ( N ) = ( case lift Γ ( a ) n n 1 e 1 e 2 , n 2 ) where ( e 1 , n 1 ) = lift Γ [ x := n ] , n + 1 ( M ) and ( e 2 , n 2 ) = lift Γ [ y := n 1 ] , n 1 + 1 ( N ) = ( Φ ; e ) where ( e , n ) = lift [ z := 0 , y := 1 ] , 2 ( N ) = ( lift ( d 0 ) , … , lift ( d m − 1 ) ; ( Φ ; e )) where ( e , n ) = lift ∅ , 0 ( M )
Every other form is a simple expression, with l i f t Γ \mathbf{lift}_{\Gamma} lift Γ applied to its atoms, and leaves n n n unchanged. The frame Φ \Phi Φ of a function body lists the types of its n n n slots. When a variable is bound, its slot is uninitialized, as Imp’s assignment and case rules require. The initialized slots are those of the variables in scope, which are all below n n n , and the new variable’s slot is n n n or above. For example, lift locals translates l e t y = ( l e t z = 5 i n l e t w = z − 1 i n w − 1 ) i n y − 2 \mathbf{let}\ y=(\mathbf{let}\ z=5\ \mathbf{in}\ \mathbf{let}\ w=z-1\ \mathbf{in}\ w-1)\ \mathbf{in}\ y-2 let y = ( let z = 5 in let w = z − 1 in w − 1 ) in y − 2 into the following expression, in which, as in AT&T syntax, $ n \$n $ n is the integer constant n n n and a bare number is a slot:
2 : = ( 0 : = $ 5 ; 1 : = prim s u b 0 $ 1 ; prim s u b 1 $ 1 ) ; prim s u b 2 $ 2. 2 := (0 := \$5; 1 := \operatorname{prim}_{\mathrm{sub}}\ 0\ \$1; \operatorname{prim}_{\mathrm{sub}}\ 1\ \$1); \operatorname{prim}_{\mathrm{sub}}\ 2\ \$2. 2 := ( 0 := $5 ; 1 := prim sub 0 $1 ; prim sub 1 $1 ) ; prim sub 2 $2.
The variables z z z and w w w of the right-hand side get slots 0 and 1, and y y y gets slot 2, which is the first slot that the right-hand side leaves unused. Each assignment writes an uninitialized slot. The assignment to z z z writes slot 0 where no other slot is initialized, the one to w w w writes slot 1 where only slot 0 is, and the one to y y y writes slot 2 where again no slot is initialized. The right-hand side writes slots 0 and 1, but they count as initialized only in the scopes of z z z and w w w , which end with the right-hand side. The Agda development lists the frame from the newest slot to the oldest, and a slot is a de Bruijn index into that list, so slot k k k here is index ∣ Φ ∣ − 1 − k |\Phi|-1-k ∣Φ∣ − 1 − k there. In the example, y y y , w w w , and z z z are the indices 0, 1, and 2.
explicate control (Imp to CFG)
Agda: FPC/Compiler/Explicate.agda (explicate)
tail (e e e ) translates e e e in tail position, and assign (e , x , k e,x,k e , x , k ) translates “write e e e to slot x x x , then run the tail k k k ” in continuation-passing style. A case on the right of an assignment adds k k k as a block, the join point, to which both branches jump. add (t t t ) adds the block t t t and returns its label. Both functions thread the blocks made so far, each typed over the older ones, so a label made earlier stays valid as more blocks are added. Blocks are added in the order in which the equations compute them, inner calls first and from left to right: tail (e 2 e_2 e 2 ) before the assignment of e 1 e_1 e 1 , l l l before l 1 l_1 l 1 , and l 1 l_1 l 1 before l 2 l_2 l 2 .
t a i l ( r h s ) = r e t u r n r h s t a i l ( x : = e 1 ; e 2 ) = a s s i g n ( e 1 , x , t a i l ( e 2 ) ) t a i l ( c a s e a x y e 1 e 2 ) = c a s e a x y l 1 l 2 w h e r e l i = a d d ( t a i l ( e i ) ) a s s i g n ( r h s , x , k ) = x : = r h s ; k a s s i g n ( x ′ : = e 1 ; e 2 , x , k ) = a s s i g n ( e 1 , x ′ , a s s i g n ( e 2 , x , k ) ) a s s i g n ( c a s e a y z e 1 e 2 , x , k ) = c a s e a y z l 1 l 2 w h e r e l = a d d ( k ) a n d l i = a d d ( a s s i g n ( e i , x , g o t o l ) ) e x p l i c a t e ( e ) = ( t ⃗ ; t ) w h e r e t = t a i l ( e ) a n d t ⃗ a r e t h e b l o c k s i t a d d s \begin{aligned}
\mathbf{tail}(rhs) &= \mathbf{return}\ rhs \\
\mathbf{tail}(x := e_1; e_2) &= \mathbf{assign}(e_1,x,\mathbf{tail}(e_2)) \\
\mathbf{tail}(\mathbf{case}\ a\ x\ y\ e_1\ e_2) &= \mathbf{case}\ a\ x\ y\ l_1\ l_2\quad\mathbf{where}\ l_i=\mathbf{add}(\mathbf{tail}(e_i)) \\
\mathbf{assign}(rhs,x,k) &= x := rhs;\ k \\
\mathbf{assign}(x' := e_1; e_2,x,k) &= \mathbf{assign}(e_1,x',\mathbf{assign}(e_2,x,k)) \\
\mathbf{assign}(\mathbf{case}\ a\ y\ z\ e_1\ e_2,x,k) &= \mathbf{case}\ a\ y\ z\ l_1\ l_2 \\
&\qquad\mathbf{where}\ l=\mathbf{add}(k)\ \mathbf{and}\ l_i=\mathbf{add}(\mathbf{assign}(e_i,x,\mathbf{goto}\ l)) \\
\mathbf{explicate}(e) &= (\vec{t};t)\quad\mathbf{where}\ t=\mathbf{tail}(e)\ \mathbf{and}\ \vec{t}\ \mathbf{are\ the\ blocks\ it\ adds}
\end{aligned} tail ( r h s ) tail ( x := e 1 ; e 2 ) tail ( case a x y e 1 e 2 ) assign ( r h s , x , k ) assign ( x ′ := e 1 ; e 2 , x , k ) assign ( case a y z e 1 e 2 , x , k ) explicate ( e ) = return r h s = assign ( e 1 , x , tail ( e 2 )) = case a x y l 1 l 2 where l i = add ( tail ( e i )) = x := r h s ; k = assign ( e 1 , x ′ , assign ( e 2 , x , k )) = case a y z l 1 l 2 where l = add ( k ) and l i = add ( assign ( e i , x , goto l )) = ( t ; t ) where t = tail ( e ) and t are the blocks it adds
and explicate maps over the definitions and main expression of a program, keeping the frame of each function body. The second equation for assign writes x ′ x' x ′ and the slots that e 2 e_2 e 2 writes before it writes x x x . The tail k k k starts with the slots initialized where the assignment starts, plus x x x , so the typing of CFG lets it forget the others. For the example of §C.6, explicate control gives the tail
0 : = $ 5 ; 1 : = prim s u b 0 $ 1 ; 2 : = prim s u b 1 $ 1 ; return prim s u b 2 $ 2 , 0 := \$5;\quad1 := \operatorname{prim}_{\mathrm{sub}} 0\ \$1;\quad2 := \operatorname{prim}_{\mathrm{sub}} 1\ \$1;\quad\operatorname{return}\operatorname{prim}_{\mathrm{sub}} 2\ \$2, 0 := $5 ; 1 := prim sub 0 $1 ; 2 := prim sub 1 $1 ; return prim sub 2 $2 ,
whose last tail starts with only slot 2 initialized.
select instructions (CFG to Instrμ _{\mu} μ )
Agda: FPC/Compiler/Select.agda (select)
The code of a simple expression r h s rhs r h s is selected in continuation-passing style. The code sel ( r h s , ω , k ) (rhs,\omega,k) ( r h s , ω , k ) sends the value of r h s rhs r h s to ω \omega ω , which is either rax or a slot x x x ,
ω : : = ret ∣ to x \omega::= \operatorname{ret} \mid\operatorname{to}\ x ω ::= ret ∣ to x
and then the instructions k k k run. We write ω ˉ \bar{\omega} ω ˉ for the destination of ω \omega ω , where ret = % rax \operatorname{ret} = \%\operatorname{rax} ret = % rax and to x = x \operatorname{to}\ x = x to x = x . An atom is a slot, a constant, or a function reference, and a constant has a base type and a function reference a code type. So an atom of a function type or a sum type is a slot x x x , and therefore callc and case take a slot.
sel ( a , ω , k ) = movq a , ω ˉ ; k \operatorname{sel}(a,\omega,k)=\operatorname{movq}\ a,\bar{\omega};\ k sel ( a , ω , k ) = movq a , ω ˉ ; k
sel ( pair a 1 a 2 , ω , k ) = alloc pair ( a 1 , a 2 ) , ω ˉ ; k \operatorname{sel}(\operatorname{pair}\ a_1\ a_2,\omega,k)=\operatorname{alloc\ pair}(a_1,a_2),\bar{\omega};\ k sel ( pair a 1 a 2 , ω , k ) = alloc pair ( a 1 , a 2 ) , ω ˉ ; k
sel ( callc x a , ω , k ) = fst x , % rax ; snd x , % rdi ; movq a , % rsi ; callq % rax ; movq % rax , ω ˉ ; k \operatorname{sel}(\operatorname{callc}\ x\ a,\omega,k)=\operatorname{fst}\ x,\%\operatorname{rax};\ \operatorname{snd}\ x,\%\operatorname{rdi};\ \operatorname{movq}\ a,\%\operatorname{rsi};\ \operatorname{callq}\ \%\operatorname{rax};\ \operatorname{movq}\ \%\operatorname{rax},\bar{\omega};\ k sel ( callc x a , ω , k ) = fst x , % rax ; snd x , % rdi ; movq a , % rsi ; callq % rax ; movq % rax , ω ˉ ; k
sel ( tuple ( a 1 , … , a n ) , ω , k ) = alloc tuple ( a 1 , … , a n ) , ω ˉ ; k \operatorname{sel}(\operatorname{tuple}\ (a_1,\ldots,a_n),\omega,k)=\operatorname{alloc\ tuple}(a_1,\ldots,a_n),\bar{\omega};\ k sel ( tuple ( a 1 , … , a n ) , ω , k ) = alloc tuple ( a 1 , … , a n ) , ω ˉ ; k
sel ( get i a , ω , k ) = load get i a , ω ˉ ; k \operatorname{sel}(\operatorname{get}_{i}\ a,\omega,k)=\operatorname{load\ get}_{i}\ a,\bar{\omega};\ k sel ( get i a , ω , k ) = load get i a , ω ˉ ; k
sel ( inl a , ω , k ) = alloc inl ( a ) , ω ˉ ; k \operatorname{sel}(\operatorname{inl}\ a,\omega,k)=\operatorname{alloc\ inl}(a),\bar{\omega};\ k sel ( inl a , ω , k ) = alloc inl ( a ) , ω ˉ ; k
and likewise for inr.
sel ( prim s u b a 1 a 2 , ω , k ) = movq a 1 , % rax ; subq a 2 , % rax ; movq % rax , ω ˉ ; k \operatorname{sel}(\operatorname{prim}_{\mathrm{sub}}\ a_1\ a_2,\omega,k)=\operatorname{movq}\ a_1,\%\operatorname{rax};\ \operatorname{subq}\ a_2,\%\operatorname{rax};\ \operatorname{movq}\ \%\operatorname{rax},\bar{\omega};\ k sel ( prim sub a 1 a 2 , ω , k ) = movq a 1 , % rax ; subq a 2 , % rax ; movq % rax , ω ˉ ; k
sel ( prim e q a 1 a 2 , ω , k ) = cmpq a 2 , a 1 ; sete ω ˉ ; k \operatorname{sel}(\operatorname{prim}_{\mathrm{eq}}\ a_1\ a_2,\omega,k)=\operatorname{cmpq}\ a_2,a_1;\ \operatorname{sete}\ \bar{\omega};\ k sel ( prim eq a 1 a 2 , ω , k ) = cmpq a 2 , a 1 ; sete ω ˉ ; k
sel ( fold a , ω , k ) = movq fold a , ω ˉ ; k \operatorname{sel}(\operatorname{fold}\ a,\omega,k)=\operatorname{movq}\ \operatorname{fold}\ a,\bar{\omega};\ k sel ( fold a , ω , k ) = movq fold a , ω ˉ ; k
sel ( unfold a , ω , k ) = movq unfold a , ω ˉ ; k \operatorname{sel}(\operatorname{unfold}\ a,\omega,k)=\operatorname{movq}\ \operatorname{unfold}\ a,\bar{\omega};\ k sel ( unfold a , ω , k ) = movq unfold a , ω ˉ ; k
tail ( return r h s ) = sel ( r h s , ret , retq ) \operatorname{tail}(\operatorname{return}\ rhs)=\operatorname{sel}(rhs,\operatorname{ret},\operatorname{retq}) tail ( return r h s ) = sel ( r h s , ret , retq )
tail ( x : = r h s ; t ) = sel ( r h s , to x , tail ( t ) ) \operatorname{tail}(x := rhs;\ t)=\operatorname{sel}(rhs,\operatorname{to}\ x,\operatorname{tail}(t)) tail ( x := r h s ; t ) = sel ( r h s , to x , tail ( t ))
tail ( goto l ) = jmp l \operatorname{tail}(\operatorname{goto}\ l)=\operatorname{jmp}\ l tail ( goto l ) = jmp l
tail ( case x y z l 1 l 2 ) = case x y z l 1 l 2 \operatorname{tail}(\operatorname{case}\ x\ y\ z\ l_1\ l_2)=\operatorname{case}\ x\ y\ z\ l_1\ l_2 tail ( case x y z l 1 l 2 ) = case x y z l 1 l 2
select ( ( Φ ; t 0 , … , t j − 1 ; t ) ) = ( Φ ; tail ( t 0 ) , … , tail ( t j − 1 ) ; tail ( t ) ) \operatorname{select}((\Phi;t_0,\ldots,t_{j-1};t))=(\Phi;\operatorname{tail}(t_0),\ldots,\operatorname{tail}(t_{j-1});\operatorname{tail}(t)) select (( Φ ; t 0 , … , t j − 1 ; t )) = ( Φ ; tail ( t 0 ) , … , tail ( t j − 1 ) ; tail ( t ))
select + ( ( Φ ; t 0 , … , t j − 1 ; t ) ) = ( Φ ; tail ( t 0 ) , … , tail ( t j − 1 ) ; movq % rdi , 0 ; movq % rsi , 1 ; tail ( t ) ) \operatorname{select}^{+}((\Phi;t_0,\ldots,t_{j-1};t))=(\Phi;\operatorname{tail}(t_0),\ldots,\operatorname{tail}(t_{j-1});\operatorname{movq}\ \%\operatorname{rdi},0;\ \operatorname{movq}\ \%\operatorname{rsi},1;\ \operatorname{tail}(t)) select + (( Φ ; t 0 , … , t j − 1 ; t )) = ( Φ ; tail ( t 0 ) , … , tail ( t j − 1 ) ; movq % rdi , 0 ; movq % rsi , 1 ; tail ( t ))
Each function body keeps its frame Φ \Phi Φ , and an assignment to a slot becomes a write to that slot in place. The main function body uses select , and each definition uses select + ^{+} + , whose prologue moves the environment and argument from rdi and rsi to slots 0 and 1: select ( ( d 0 , … , d m − 1 ; d ) ) = ( select + ( d 0 ) , … , select + ( d m − 1 ) ; select ( d ) ) \operatorname{select}((d_0,\ldots,d_{m-1};d))=(\operatorname{select}^{+}(d_0),\ldots,\operatorname{select}^{+}(d_{m-1});\operatorname{select}(d)) select (( d 0 , … , d m − 1 ; d )) = ( select + ( d 0 ) , … , select + ( d m − 1 ) ; select ( d )) .
Calculations for the Other Equational Passes
Agda: FPC/proof/Correctness/GraphModel/, one file per pass or step (CaptureCorrect.agda, ...)
This appendix gives the key case of the proofs of the first two steps of enclose and of the five equational passes that § 4.1 does not show, in the order of the pipeline. As there, each proof generalizes the equation to open terms under related environments, the other cases are congruences, and where a step needs a set to be nonempty we show the nonempty case.
Enclose: capture. The environments agree on the variables x ⃗ \vec{x} x in scope, and the target environment is nonempty on them. A λ \lambda λ -abstraction becomes a closure that captures all of x ⃗ \vec{x} x . With N ′ = capture ( N ) N' = \operatorname{capture}(N) N ′ = capture ( N ) and F = λ Y . ⟦ N ′ ⟧ ρ 0 [ x ⃗ : = ρ ′ ( x ⃗ ) , y : = Y ] F = \lambda Y.\llbracket N'\rrbracket_{\rho_0}[\vec{x}:=\rho'(\vec{x}),y:=Y] F = λY . [ [ N ′ ] ] ρ 0 [ x := ρ ′ ( x ) , y := Y ] :
⟦ λ y . N ⟧ ρ = Λ ( λ Y . ⟦ N ⟧ ρ [ y : = Y ] ) (meaning of λ ) = Λ ( F ) (induction hypothesis for N ) = guard ( ρ ′ ( x 1 ) , … , ρ ′ ( x k ) ; Λ ( F ) ) ( ρ ′ is nonempty on x ⃗ ) = ⟦ clos ( x ⃗ , y . N ′ ) x ⃗ ⟧ ρ (meaning of a closure) \begin{aligned}
\llbracket\lambda y.N\rrbracket_\rho&= \Lambda(\lambda Y.\llbracket N\rrbracket_\rho[y:=Y]) && \text{(meaning of }\lambda\text{)}\\
&= \Lambda(F) && \text{(induction hypothesis for }N\text{)}\\
&= \operatorname{guard}(\rho'(x_1),\ldots,\rho'(x_k);\Lambda(F)) && \text{(}\rho'\text{ is nonempty on }\vec{x}\text{)}\\
&= \llbracket\operatorname{clos}(\vec{x},y.N')\vec{x}\rrbracket_\rho&& \text{(meaning of a closure)}
\end{aligned} [ [ λ y . N ] ] ρ = Λ ( λY . [ [ N ] ] ρ [ y := Y ]) = Λ ( F ) = guard ( ρ ′ ( x 1 ) , … , ρ ′ ( x k ) ; Λ ( F )) = [ [ clos ( x , y . N ′ ) x ] ] ρ (meaning of λ ) (induction hypothesis for N ) ( ρ ′ is nonempty on x ) (meaning of a closure)
Enclose: prune. The environments agree on the variables that a term uses, and the source environment is nonempty. Let D i = ⟦ M i ⟧ ρ D_i=\llbracket M_i\rrbracket_\rho D i = [ [ M i ] ] ρ and D i ′ = ⟦ prune ( M i ) ⟧ ρ ′ D'_i=\llbracket\operatorname{prune}(M_i)\rrbracket_{\rho'} D i ′ = [ [ prune ( M i ) ] ] ρ ′ , let I I I be the indices of the kept captured values, and let N ′ = prune ( N ) N'=\operatorname{prune}(N) N ′ = prune ( N ) :
⟦ clos ( x ⃗ , y . N ) M ⃗ ⟧ ρ = guard ( D ⃗ ; Λ ( λ Y . ⟦ N ⟧ ρ 0 [ x ⃗ : = D ⃗ , y : = Y ] ) ) (meaning of a closure) = guard ( D ⃗ ′ ; Λ ( λ Y . ⟦ N ⟧ ρ 0 [ x ⃗ : = D ⃗ , y : = Y ] ) ) (the guards hold together) = guard ( D ⃗ ′ ; Λ ( λ Y . ⟦ N ′ ⟧ ρ 0 [ x ⃗ I : = D ⃗ ′ , y : = Y ] ) ) (induction hypothesis for N ) = ⟦ clos ( x ⃗ I , y . N ′ ) M ⃗ ′ ⟧ ρ (meaning of a closure) \begin{aligned}
\llbracket\operatorname{clos}(\vec{x},y.N)\vec{M}\rrbracket_\rho
&= \operatorname{guard}(\vec{D};\Lambda(\lambda Y.\llbracket N\rrbracket_{\rho_0}[\vec{x}:=\vec{D},y:=Y])) && \text{(meaning of a closure)}\\
&= \operatorname{guard}(\vec{D}';\Lambda(\lambda Y.\llbracket N\rrbracket_{\rho_0}[\vec{x}:=\vec{D},y:=Y])) && \text{(the guards hold together)}\\
&= \operatorname{guard}(\vec{D}';\Lambda(\lambda Y.\llbracket N'\rrbracket_{\rho_0}[\vec{x}_I:=\vec{D}',y:=Y])) && \text{(induction hypothesis for }N\text{)}\\
&= \llbracket\operatorname{clos}(\vec{x}_I,y.N')\vec{M}'\rrbracket_\rho&& \text{(meaning of a closure)}
\end{aligned} [ [ clos ( x , y . N ) M ] ] ρ = guard ( D ; Λ ( λY . [ [ N ] ] ρ 0 [ x := D , y := Y ])) = guard ( D ′ ; Λ ( λY . [ [ N ] ] ρ 0 [ x := D , y := Y ])) = guard ( D ′ ; Λ ( λY . [ [ N ′ ] ] ρ 0 [ x I := D ′ , y := Y ])) = [ [ clos ( x I , y . N ′ ) M ′ ] ] ρ (meaning of a closure) (the guards hold together) (induction hypothesis for N ) (meaning of a closure)
The guards hold together because a dropped captured value is a variable, which the environment makes nonempty. The induction hypothesis applies because N N N uses only x ⃗ I \vec{x}_I x I and y y y .
Hoist. The induction hypothesis says that a term means the same as its translation under the table T ′ T' T ′ of the definitions made so far. For code, hoist first translates the body N N N to N ′ N' N ′ and then adds the definition d k = code ( z , y . N ′ ) d_k=\operatorname{code}(z,y.N') d k = code ( z , y . N ′ ) :
⟦ code ( z , y . N ) ⟧ ρ = Λ ( λ X . Λ ( λ Y . ⟦ N ⟧ ρ 0 [ z : = X , y : = Y ] ) ) (meaning of code) = Λ ( λ X . Λ ( λ Y . ⟦ N ′ ⟧ T k ρ 0 [ z : = X , y : = Y ] ) ) (induction hypothesis for N ) = T ( k ) (meaning of definition d k ) = ⟦ f k ⟧ T ρ (meaning of f k ) \begin{aligned}
\llbracket\operatorname{code}(z,y.N)\rrbracket_\rho
&= \Lambda(\lambda X.\Lambda(\lambda Y.\llbracket N\rrbracket_{\rho_0}[z:=X,y:=Y])) && \text{(meaning of code)}\\
&= \Lambda(\lambda X.\Lambda(\lambda Y.\llbracket N'\rrbracket_{T_k\rho_0}[z:=X,y:=Y])) && \text{(induction hypothesis for }N\text{)}\\
&= T(k) && \text{(meaning of definition }d_k\text{)}\\
&= \llbracket f_k\rrbracket_{T}\rho&& \text{(meaning of }f_k\text{)}
\end{aligned} [ [ code ( z , y . N ) ] ] ρ = Λ ( λ X .Λ ( λY . [ [ N ] ] ρ 0 [ z := X , y := Y ])) = Λ ( λ X .Λ ( λY . [ [ N ′ ] ] T k ρ 0 [ z := X , y := Y ])) = T ( k ) = [ [ f k ] ] T ρ (meaning of code) (induction hypothesis for N ) (meaning of definition d k ) (meaning of f k )
The other cases also use a weakening lemma, which says that a term means the same under a larger table that agrees on the definitions the term refers to.
Remove complex operands. When an operand M M M is not an atom, remove complex operands binds it to a fresh variable x x x with a let. For a projection, with D = ⟦ rco ( M ) ⟧ T ′ ρ D=\llbracket\operatorname{rco}(M)\rrbracket_{T'\rho} D = [ [ rco ( M ) ] ] T ′ ρ :
⟦ get i M ⟧ T ρ = π i t u p ( ⟦ M ⟧ T ρ ) (meaning of get i ) = π i t u p ( D ) (induction hypothesis for M ) = ⟦ get i x ⟧ T ′ ρ [ x : = D ] (meaning of get i ) = Λ ( λ X . ⟦ get i x ⟧ T ′ ρ [ x : = X ] ) ∙ D (Lemma 3.1, part 1) = ⟦ let x = rco ( M ) in get i x ⟧ T ρ (meaning of let) \begin{aligned}
\llbracket\operatorname{get}_i M\rrbracket_{T\rho}
&= \pi^{\mathrm{tup}}_i(\llbracket M\rrbracket_{T\rho}) && \text{(meaning of }\operatorname{get}_i\text{)}\\
&= \pi^{\mathrm{tup}}_i(D) && \text{(induction hypothesis for }M\text{)}\\
&= \llbracket\operatorname{get}_i x\rrbracket_{T'\rho}[x:=D] && \text{(meaning of }\operatorname{get}_i\text{)}\\
&= \Lambda(\lambda X.\llbracket\operatorname{get}_i x\rrbracket_{T'\rho}[x:=X])\mathbin{\bullet}D && \text{(Lemma 3.1, part 1)}\\
&= \llbracket\operatorname{let} x=\operatorname{rco}(M)\operatorname{ in\ get}_i x\rrbracket_{T\rho} && \text{(meaning of let)}
\end{aligned} [ [ get i M ] ] T ρ = π i tup ( [ [ M ] ] T ρ ) = π i tup ( D ) = [ [ get i x ] ] T ′ ρ [ x := D ] = Λ ( λ X . [ [ get i x ] ] T ′ ρ [ x := X ]) ∙ D = [ [ let x = rco ( M ) in get i x ] ] T ρ (meaning of get i ) (induction hypothesis for M ) (meaning of get i ) (Lemma 3.1, part 1) (meaning of let)
The Agda development proves this once for every strict operation and every continuation, and chains it over several operands.
Lift locals. The semantics of Imp is in continuation-passing style, so the induction hypothesis relates an MNF term e e e and its translation under a continuation κ \kappa κ : κ ( ⟦ e ⟧ T ρ ) = ⟦ lift Γ ( e ) ⟧ T ′ σ κ \kappa(\llbracket e\rrbracket_{T\rho})=\llbracket\operatorname{lift}_\Gamma(e)\rrbracket_{T'\sigma}\kappa κ ( [ [ e ] ] T ρ ) = [ [ lift Γ ( e ) ] ] T ′ σ κ , for an environment ρ \rho ρ and a frame σ \sigma σ that holds the value of a variable in its slot, ρ ( v ) = σ ( Γ ( v ) ) \rho(v)=\sigma(\Gamma(v)) ρ ( v ) = σ ( Γ ( v )) . A let becomes an assignment to the slot s s s of its variable x x x . Because s s s is not initialized, it is not the slot of a variable in scope, so σ [ s : = X ] \sigma[s:=X] σ [ s := X ] holds ρ [ x : = X ] \rho[x:=X] ρ [ x := X ] in the slots of Γ , x : s \Gamma,x:s Γ , x : s . With κ ′ = λ D . λ ( X . ⟦ lift T ′ , x : s ( N ) ⟧ T ′ ρ ′ κ ) ∙ D \kappa'=\lambda D.\lambda(X.\llbracket\operatorname{lift}_{T',x:s}(N)\rrbracket_{T'\rho'}\kappa)\mathbin{\bullet}D κ ′ = λ D . λ ( X . [ [ lift T ′ , x : s ( N ) ] ] T ′ ρ ′ κ ) ∙ D :
κ ( ⟦ let x = M in N ⟧ T ρ ) = κ ( λ X . ⟦ N ⟧ T ′ ρ [ x : = X ] ) ∙ ⟦ M ⟧ T ρ (meaning of let) = λ ( λ X . κ ( ⟦ N ⟧ T ′ ρ [ x : = X ] ) ) ∙ ⟦ M ⟧ T ρ (a let commutes with κ ) = λ ( λ X . ⟦ lift T ′ , x : s ( N ) ⟧ T ′ σ [ s : = X ] κ ) ∙ ⟦ M ⟧ T ρ (induction hypothesis for N ) = ⟦ lift T ( M ) ⟧ T ′ ρ σ κ ′ (induction hypothesis for M ) = ⟦ s : = lift T ( M ) ; lift T ′ , x : s ( N ) ⟧ T ′ ρ σ κ (meaning of assignment) \begin{aligned}
\kappa(\llbracket\operatorname{let} x=M\operatorname{ in}N\rrbracket_{T\rho})
&= \kappa(\lambda X.\llbracket N\rrbracket_{T'\rho}[x:=X])\mathbin{\bullet}\llbracket M\rrbracket_{T\rho} && \text{(meaning of let)}\\
&= \lambda(\lambda X.\kappa(\llbracket N\rrbracket_{T'\rho}[x:=X]))\mathbin{\bullet}\llbracket M\rrbracket_{T\rho} && \text{(a let commutes with }\kappa\text{)}\\
&= \lambda(\lambda X.\llbracket\operatorname{lift}_{T',x:s}(N)\rrbracket_{T'\sigma[s:=X]}\kappa)\mathbin{\bullet}\llbracket M\rrbracket_{T\rho} && \text{(induction hypothesis for }N\text{)}\\
&= \llbracket\operatorname{lift}_T(M)\rrbracket_{T'\rho}\sigma\kappa' && \text{(induction hypothesis for }M\text{)}\\
&= \llbracket s:=\operatorname{lift}_T(M);\operatorname{lift}_{T',x:s}(N)\rrbracket_{T'\rho}\sigma\kappa&& \text{(meaning of assignment)}
\end{aligned} κ ( [ [ let x = M in N ] ] T ρ ) = κ ( λ X . [ [ N ] ] T ′ ρ [ x := X ]) ∙ [ [ M ] ] T ρ = λ ( λ X . κ ( [ [ N ] ] T ′ ρ [ x := X ])) ∙ [ [ M ] ] T ρ = λ ( λ X . [ [ lift T ′ , x : s ( N ) ] ] T ′ σ [ s := X ] κ ) ∙ [ [ M ] ] T ρ = [ [ lift T ( M ) ] ] T ′ ρ σ κ ′ = [ [ s := lift T ( M ) ; lift T ′ , x : s ( N ) ] ] T ′ ρ σ κ (meaning of let) (a let commutes with κ ) (induction hypothesis for N ) (induction hypothesis for M ) (meaning of assignment)
The second step, a commuting conversion, needs ⟦ M ⟧ T ρ \llbracket M \rrbracket_T \rho [ [ M ] ] T ρ to be consistent and the body to be monotone. The case case follows the same pattern.
Explicate control. The induction hypothesis says that an Imp expression e e e , run with the continuation κ x , k \kappa_{x,k} κ x , k that writes slot x x x and then runs the tail k k k , means the same as the tail assign ( e , x , k ) (e,x,k) ( e , x , k ) , given the meanings B B B of the blocks. The key case is an assignment whose right-hand side is a case. The pass adds k k k as a block l l l , the join point, so B ( l ) B(l) B ( l ) is the meaning of k k k , and each branch e i e_i e i becomes a block l i = add ( assign ( e i , x , goto l ) ) l_i = \operatorname{add}(\operatorname{assign}(e_i,x,\operatorname{goto}\ l)) l i = add ( assign ( e i , x , goto l )) . The continuation κ x , k \kappa_{x,k} κ x , k writes slot x x x of the frame σ \sigma σ in which the assignment starts, while the tail writes slot x x x of the frame in which e e e ends, which may also hold the slots that e e e writes. The tail k k k does not read those slots, because they are not initialized where the assignment starts, so k k k means the same in both frames, because the meaning of a tail depends only on the slots it starts with. We show the first branch; the second is the same:
⟦ case a y z e 1 e 2 ⟧ T σ κ x , k = C ( A ⟦ a ⟧ σ , λ X . ⟦ e 1 ⟧ T σ [ y : = X ] κ x , k , … ) (Imp) = C ( A ⟦ a ⟧ σ , λ X . ⟦ e 1 ⟧ T σ [ y : = X ] κ x , goto l , … ) (join point l ; k does not read y ) = C ( A ⟦ a ⟧ σ , λ X . B ( l 1 ) ( σ [ y : = X ] ) , … ) (ind. hyp.) = ⟦ case a y z l 1 l 2 ⟧ T ′ , B σ (CFG) \begin{aligned}
\llbracket\operatorname{case}\ a\ y\ z\ e_1\ e_2 \rrbracket_T\ \sigma\ \kappa_{x,k}
&= C(\mathcal{A}\llbracket a\rrbracket_\sigma,\lambda X.\llbracket e_1\rrbracket_T\ \sigma[y:=X]\ \kappa_{x,k},\ldots) && \text{(Imp)}\\
&= C(\mathcal{A}\llbracket a\rrbracket_\sigma,\lambda X.\llbracket e_1\rrbracket_T\ \sigma[y:=X]\ \kappa_{x,\operatorname{goto}\ l},\ldots) && \text{(join point }l\text{; }k\text{ does not read }y\text{)}\\
&= C(\mathcal{A}\llbracket a\rrbracket_\sigma,\lambda X.B(l_1)(\sigma[y:=X]),\ldots) && \text{(ind. hyp.)}\\
&= \llbracket\operatorname{case}\ a\ y\ z\ l_1\ l_2\rrbracket_{T',B}\ \sigma&& \text{(CFG)}
\end{aligned} [ [ case a y z e 1 e 2 ] ] T σ κ x , k = C ( A [ [ a ] ] σ , λ X . [ [ e 1 ] ] T σ [ y := X ] κ x , k , … ) = C ( A [ [ a ] ] σ , λ X . [ [ e 1 ] ] T σ [ y := X ] κ x , goto l , … ) = C ( A [ [ a ] ] σ , λ X . B ( l 1 ) ( σ [ y := X ]) , … ) = [ [ case a y z l 1 l 2 ] ] T ′ , B σ (Imp) (join point l ; k does not read y ) (ind. hyp.) (CFG)
A further lemma says that blocks added later do not change the meaning of a label made earlier.
Select instructions. The induction hypothesis says that a CFG tail t t t means the same as its instructions tail ( t ) \operatorname{tail}(t) tail ( t ) , for any registers R R R and flag F F F . A call x : = callc y a ; t x := \operatorname{callc}\ y\ a;\ t x := callc y a ; t writes the result C = ( car ( σ ( y ) ) ∙ cdr ( σ ( y ) ) ) ∙ A ⟦ a ⟧ σ C = (\operatorname{car}(\sigma(y)) \mathbin{\bullet} \operatorname{cdr}(\sigma(y))) \mathbin{\bullet} \mathcal{A}\llbracket a\rrbracket_\sigma C = ( car ( σ ( y )) ∙ cdr ( σ ( y ))) ∙ A [ [ a ] ] σ to slot x x x . Let s s s be the sequence movq % r a x , x ; tail ( t ) \operatorname{movq}\ \%rax,x;\operatorname{tail}(t) movq % r a x , x ; tail ( t ) , let R C R_C R C hold C C C in rax, and let R 1 R_1 R 1 be R R R with the curried function of the closure in rax, its environment in rdi, and the argument in rsi:
⟦ x : = callc y a ; t ⟧ T , B σ = Λ ( λ X . ⟦ t ⟧ T , B ( σ [ x : = X ] ) ) ∙ C (meaning of assignment) = Λ ( λ X . ⟦ tail ( t ) ⟧ ( σ [ x : = X ] ) R C { ⟨ ⟩ } ) ∙ C (induction hypothesis for t ) = ⟦ s ⟧ σ R C { ⟨ ⟩ } (meaning of a write to x ) = guard ( C ; ⟦ s ⟧ σ R C { ⟨ ⟩ } ) (an application to ∅ is ∅ ) = ⟦ callq % r a x ; s ⟧ σ R 1 F (meaning of callq) = ⟦ fst y , % r a x ; snd y , % r d i ; movq a , % r s i ; s ⟧ σ R F (meaning of the loads) \begin{aligned}
\llbracket x := \operatorname{callc}\ y\ a;\ t\rrbracket_{T,B}\ \sigma
&= \Lambda(\lambda X.\llbracket t\rrbracket_{T,B}(\sigma[x:=X]))\mathbin{\bullet} C && \text{(meaning of assignment)}\\
&= \Lambda(\lambda X.\llbracket\operatorname{tail}(t)\rrbracket(\sigma[x:=X])\ R_C\{\langle\rangle\})\mathbin{\bullet} C && \text{(induction hypothesis for }t\text{)}\\
&= \llbracket s\rrbracket\ \sigma\ R_C\{\langle\rangle\} && \text{(meaning of a write to }x\text{)}\\
&= \operatorname{guard}(C;\llbracket s\rrbracket\ \sigma\ R_C\{\langle\rangle\}) && \text{(an application to }\varnothing\text{ is }\varnothing\text{)}\\
&= \llbracket\operatorname{callq}\ \%rax;\ s\rrbracket\ \sigma\ R_1\ F && \text{(meaning of callq)}\\
&= \llbracket\operatorname{fst}\ y,\ \%rax;\ \operatorname{snd}\ y,\ \%rdi;\ \operatorname{movq}\ a,\ \%rsi;\ s\rrbracket\ \sigma\ R\ F && \text{(meaning of the loads)}
\end{aligned} [ [ x := callc y a ; t ] ] T , B σ = Λ ( λ X . [ [ t ] ] T , B ( σ [ x := X ])) ∙ C = Λ ( λ X . [ [ tail ( t ) ] ] ( σ [ x := X ]) R C {⟨ ⟩}) ∙ C = [ [ s ] ] σ R C {⟨ ⟩} = guard ( C ; [ [ s ] ] σ R C {⟨ ⟩}) = [ [ callq % r a x ; s ] ] σ R 1 F = [ [ fst y , % r a x ; snd y , % r d i ; movq a , % r s i ; s ] ] σ R F (meaning of assignment) (induction hypothesis for t ) (meaning of a write to x ) (an application to ∅ is ∅ ) (meaning of callq) (meaning of the loads)
The step for callq holds because callq applies the curried function car ( σ ( y ) ) \operatorname{car}(\sigma(y)) car ( σ ( y )) in rax to the environment cdr ( σ ( y ) ) \operatorname{cdr}(\sigma(y)) cdr ( σ ( y )) in rdi and then to the argument A ⟦ a ⟧ σ \mathcal{A}\llbracket a\rrbracket_\sigma A [ [ a ] ] σ in rsi, which gives C C C .
An Example of the Approximant Logical Relation
Agda: FPC/proof/Correctness/GraphModel/ApproxLR.agda (the definitions; the example itself is not mechanized)
This appendix works clause 3 of the approximant logical relation (Figure 5 ) through the example of § 4.2 : λ y . x + y \lambda y.x+y λ y . x + y with bound to 9, whose source meaning D D D contains [ 1 ] ↦ 10 [1]\mapsto10 [ 1 ] ↦ 10 . Take the one-element list V = [ ⟨ u , v ⟩ ] V=[\langle u,v\rangle] V = [⟨ u , v ⟩] with u = [ 9 ] ↦ [ 1 ] ↦ 10 u=[9]\mapsto[1]\mapsto10 u = [ 9 ] ↦ [ 1 ] ↦ 10 . The environment [ 9 ] [9] [ 9 ] of the curried entry is in cdr ( V ) \operatorname{cdr}(V) cdr ( V ) , so [ 1 ] ↦ 10 ∈ V [1]\mapsto10\in V [ 1 ] ↦ 10 ∈ V is a usable entry, and V V V maps [ 1 ] [1] [ 1 ] to [ 10 ] [10] [ 10 ] . Clause 3 then asks that D ∙ E ≥ [ 10 ] D\mathbin{\bullet}E\ge[10] D ∙ E ≥ [ 10 ] for every consistent E E E related to [ 1 ] [1] [ 1 ] , that is, every E E E that contains 1. It holds because D D D has the entry [ 1 ] ↦ 10 [1]\mapsto10 [ 1 ] ↦ 10 and [ 1 ] ⊆ E [1]\subseteq E [ 1 ] ⊆ E , so 10 ∈ D ∙ E 10\in D\mathbin{\bullet}E 10 ∈ D ∙ E , which is clause 1 for [ 10 ] [10] [ 10 ] . Now take V ′ = [ ⟨ u ′ , 9 ⟩ ] V'=[\langle u',9\rangle] V ′ = [⟨ u ′ , 9 ⟩] with the junk entry u ′ = [ 2 ] ↦ [ 1 ] ↦ 3 u'=[2]\mapsto[1]\mapsto3 u ′ = [ 2 ] ↦ [ 1 ] ↦ 3 . Its environment [ 2 ] [2] [ 2 ] is not in cdr ( V ′ ) = { 9 } \operatorname{cdr}(V')=\{9\} cdr ( V ′ ) = { 9 } , so V ′ V' V ′ has no usable entry, clause 3 asks nothing, and D ≥ V ′ D\ge V' D ≥ V ′ holds although 3 ∉ D ∙ E 3\notin D\mathbin{\bullet}E 3 ∈ / D ∙ E . Both V V V and V ′ V' V ′ are lists of elements of the target closure, which pairs every entry of its curried function with the environment 9.
The shapes that fail. § 4.2 rules out all but one of the nine combinations of a source side and a target side. The details follow.
A single target element. A clause for one pair that accepted environments from anywhere would have to account for junk entries such as the curried entry for [ 2 ] [2] [ 2 ] in the example above. We tried. two variants in Agda: a single element against a single element, and one target element inside its whole target set. In the second, the function case must relate the whole argument set, which cannot shrink to match the index.
A single source element or a finite source list. A target closure that shows no usable curried entry is related to every nonempty source set, such as the singleton of [ n ] ↦ n [n] \mapsto n [ n ] ↦ n for each n n n , and no finite source side for λ f . f \lambda f.\,f λ f . f has an entry for all of them; we check this case in Agda.
Whole denotations on both sides. A later modality would need a step to discharge, which a denotational semantics does not have; the Scott-domain proof instead builds its relation by stages along the bilimit (Appendix § 16 ).
A target set of bounded depth. The argument for a call succeeds for finitely many outputs at a time. We show in Agda, by induction on the bound, that the relation holds on a bounded set exactly when it holds on each finite sublist.
The Other Four Semantics and Their Correctness Results
Agda: FPC/Theorems/CorrectWrtClockedInterpreters.agda, CorrectWrtAbstractMachines.agda, CorrectWrtReductionSemantics.agda, CorrectWrtScottDomains.agda
Each of the five developments of § 6 proves the whole compiler correct with respect to its own semantics of the languages. For the graph model, the semantics are those of Appendix § 11 and the theorem is Theorem 4.1 . This appendix gives, for each of the other four styles, the semantics of the source language FPC and the target language I n s t r μ \mathrm{Instr}_{\mu} Instr μ . For the operational styles, it also states the correctness result for the whole compiler, so that each can be read on its own. The theorem for Scott domains is Theorem 4.2 . The semantics of the intermediate languages follow the same pattern and are in the artifact.
Values and Observations
Agda: Semantics/Observation.agda, Framework/Smallstep.agda, Framework/Clocked.agda
The three operational styles share their values and their observations. A value of type A A A is one of
v , w : : = c ∣ c o d e f ∣ p a i r ( v , w ) ∣ t u p ( v 1 , … , v n ) ∣ i n l v ∣ i n r v ∣ f o l d v v,w ::= c \mid\mathit{code}\ f \mid\mathit{pair}(v,w) \mid\mathit{tup}(v_1,\ldots,v_n) \mid\mathit{inl}\ v \mid\mathit{inr}\ v \mid\mathit{fold}\ v v , w ::= c ∣ code f ∣ pair ( v , w ) ∣ tup ( v 1 , … , v n ) ∣ inl v ∣ inr v ∣ fold v
where a closure pair ( v , w ) (v,w) ( v , w ) pairs a code value v v v with the tuple w w w of its environment. In FPC, the code of a closure is the body N N N of a λ \lambda λ -abstraction and its environment is the environment ρ \rho ρ of the λ \lambda λ -abstraction, a list of values; we write ⟨ λ x . N , ρ ⟩ \langle\lambda x.\,N,\rho\rangle ⟨ λ x . N , ρ ⟩ for p a i r ( c o d e N , t u p ( ρ ) ) \mathit{pair}(\mathit{code}\ N,\mathit{tup}(\rho)) pair ( code N , tup ( ρ )) . In I n s t r μ \mathrm{Instr}_{\mu} Instr μ , the code of a closure is the name f f f of a definition. A run is observed through its final value, forgetting the code and environment inside closures. An observation is one of
o : : = c ∣ f u n ∣ t u p l e ( o 1 , … , o n ) ∣ l e f t ( o ) ∣ r i g h t ( o ) ∣ e r r o r o ::= c \mid\mathit{fun} \mid\mathit{tuple}(o_1,\ldots,o_n) \mid\mathit{left}(o) \mid\mathit{right}(o) \mid\mathit{error} o ::= c ∣ fun ∣ tuple ( o 1 , … , o n ) ∣ left ( o ) ∣ right ( o ) ∣ error
and the observation of a value is
o b s ( c ) = c o b s ( p a i r ( v , w ) ) = f u n o b s ( t u p ( v 1 , … , v n ) ) = t u p l e ( o b s ( v 1 ) , … , o b s ( v n ) ) o b s ( f o l d v ) = o b s ( v ) o b s ( i n l v ) = l e f t ( o b s ( v ) ) o b s ( i n r v ) = r i g h t ( o b s ( v ) ) \begin{aligned}
\mathit{obs}(c) &= c & \mathit{obs}(\mathit{pair}(v,w)) &= \mathit{fun} \\
\mathit{obs}(\mathit{tup}(v_1,\ldots,v_n)) &= \mathit{tuple}(\mathit{obs}(v_1),\ldots,\mathit{obs}(v_n)) & \mathit{obs}(\mathit{fold}\ v) &= \mathit{obs}(v) \\
\mathit{obs}(\mathit{inl}\ v) &= \mathit{left}(\mathit{obs}(v)) & \mathit{obs}(\mathit{inr}\ v) &= \mathit{right}(\mathit{obs}(v))
\end{aligned} obs ( c ) obs ( tup ( v 1 , … , v n )) obs ( inl v ) = c = tuple ( obs ( v 1 ) , … , obs ( v n )) = left ( obs ( v )) obs ( pair ( v , w )) obs ( fold v ) obs ( inr v ) = fun = obs ( v ) = right ( obs ( v ))
and o b s ( c o d e f ) = f u n \mathit{obs}(\mathit{code}\ f)=\mathit{fun} obs ( code f ) = fun . A run stops with the observation error when it gets stuck, which no well-typed program reaches. The primitives compute δ s u b ( n , m ) = n − m \delta_{\mathrm{sub}}(n,m)=n-m δ sub ( n , m ) = n − m and δ e q ( n , m ) = i n l t u p ( ) \delta_{\mathrm{eq}}(n,m)=\mathit{inl}\ \mathit{tup}() δ eq ( n , m ) = inl tup ( ) if n = m n=m n = m and i n r t u p ( ) \mathit{inr}\ \mathit{tup}() inr tup ( ) otherwise.
Machines. A machine is a set of states, a start state, and a step function that maps each state ζ \zeta ζ either to a next state, written ζ ⟶ ζ ′ \zeta\longrightarrow\zeta' ζ ⟶ ζ ′ , or to a final observation o o o , written ζ ⟶ s t o p ( o ) \zeta\longrightarrow\mathit{stop}(o) ζ ⟶ stop ( o ) . A machine terminates with o o o when a sequence of steps leads from the start state to a state ζ \zeta ζ with ζ ⟶ s t o p ( o ) \zeta\longrightarrow\mathit{stop}(o) ζ ⟶ stop ( o ) , and it diverges when for every n n n there is a sequence of n n n steps from the start state. We use the same notation for each machine; the CEK machines of FPC and Instrμ _\mu μ (Appendix § 15 .§ 3 ) and the reduction semantics of FPC (Appendix § 15 .§ 4 ) each define their own ⟶ \longrightarrow ⟶ and ⟶ s t o p \longrightarrow_{\mathrm{stop}} ⟶ stop .
Clocked interpreters. A clocked interpreter maps a clock k ∈ N k \in\mathbb{N} k ∈ N to a result, ranged over by e e e , which times out, reports an error, or returns a value v v v :
e : : = t i m e o u t ∣ e r r o r ∣ r e t v e ::= \mathrm{timeout} \mid\mathrm{error} \mid\mathrm{ret}\ v e ::= timeout ∣ error ∣ ret v
We write v ← e ; e ′ v \leftarrow e;\ e' v ← e ; e ′ for the result e ′ e' e ′ which depends on v v v when e e e is r e t v \mathrm{ret}\ v ret v , and for e e e itself when e e e is timeout or error. An interpreter terminates with o o o when for some clock k k k its result is r e t v \mathrm{ret}\ v ret v with obs ( v ) = o \operatorname{obs}(v) = o obs ( v ) = o , or error with o = e r r o r o = \mathrm{error} o = error , and it diverges when its result is timeout for every clock.
Equivalence. Two machines, or two interpreters, are equivalent when, for every observation o o o , one terminates with o o o if and only if the other does, and one diverges if and only if the other does. The compiler correctness proofs for the three operational semantics state that a program and its compilation are equivalent.
Clocked Interpreters
Agda: FPC/Semantics/ClockedInterpreters/FPC.agda, FPC/Semantics/ClockedInterpreters/X86.agda
Interpreter for FPC. The interpreter eval k ( ρ , M ) \operatorname{eval}_k(\rho, M) eval k ( ρ , M ) evaluates a term M M M in an environment ρ \rho ρ of values at clock k k k . The clock ticks at each call of the interpreted program.
The interpreter of FPC
eval k ( ρ , M ) = e call k ( f , w ) = e \operatorname{eval}_k(\rho, M) = e \qquad\operatorname{call}_k(f,w) = e eval k ( ρ , M ) = e call k ( f , w ) = e
eval k ( ρ , x ) = ret ρ ( x ) eval k ( ρ , λ x . N ) = ret ⟨ λ x . N , ρ ⟩ eval k ( ρ , L M ) = f ← eval k ( ρ , L ) ; w ← eval k ( ρ , M ) ; call k ( f , w ) eval k ( ρ , c ) = ret c eval k ( ρ , tuple ( M 1 , … , M n ) ) = v 1 ← eval k ( ρ , M 1 ) ; … ; v n ← eval k ( ρ , M n ) ; ret tup ( v 1 , … , v n ) eval k ( ρ , get i M ) = tup ( v 1 , … , v n ) ← eval k ( ρ , M ) ; ret v i eval k ( ρ , inl M ) = v ← eval k ( ρ , M ) ; ret ( inl v ) (likewise inr and fold) eval k ( ρ , case L ( x . M ) ( y . N ) ) = u ← eval k ( ρ , L ) ; { eval k ( ρ [ x : = v ] , M ) if u = inl v eval k ( ρ [ y : = v ] , N ) if u = inr v eval k ( ρ , prim p L M ) = n ← eval k ( ρ , L ) ; m ← eval k ( ρ , M ) ; ret δ p ( n , m ) eval k ( ρ , let x = M in N ) = v ← eval k ( ρ , M ) ; eval k ( ρ [ x : = v ] , N ) eval k ( ρ , unfold M ) = fold v ← eval k ( ρ , M ) ; ret v \begin{aligned}
\operatorname{eval}_k(\rho, x) &= \operatorname{ret}\ \rho(x) \\
\operatorname{eval}_k(\rho, \lambda x.\,N) &= \operatorname{ret}\ \langle\lambda x.\,N,\rho\rangle\\
\operatorname{eval}_k(\rho, L\ M) &= f \leftarrow\operatorname{eval}_k(\rho,L);\ w \leftarrow\operatorname{eval}_k(\rho,M);\ \operatorname{call}_k(f,w) \\
\operatorname{eval}_k(\rho,c) &= \operatorname{ret}\ c \\
\operatorname{eval}_k(\rho,\operatorname{tuple}(M_1,\ldots,M_n)) &= v_1 \leftarrow\operatorname{eval}_k(\rho,M_1);\ \ldots;\ v_n \leftarrow\operatorname{eval}_k(\rho,M_n);\ \operatorname{ret}\ \operatorname{tup}(v_1,\ldots,v_n) \\
\operatorname{eval}_k(\rho,\operatorname{get}_i\ M) &= \operatorname{tup}(v_1,\ldots,v_n) \leftarrow\operatorname{eval}_k(\rho,M);\ \operatorname{ret}\ v_i \\
\operatorname{eval}_k(\rho,\operatorname{inl}\ M) &= v \leftarrow\operatorname{eval}_k(\rho,M);\ \operatorname{ret}\ (\operatorname{inl}\ v)\qquad\text{(likewise inr and fold)} \\
\operatorname{eval}_k(\rho,\operatorname{case}\ L\ (x.\,M)\ (y.\,N)) &= u \leftarrow\operatorname{eval}_k(\rho,L);\
\begin{cases}
\operatorname{eval}_k(\rho[x:=v],M) & \text{if } u = \operatorname{inl}\ v \\
\operatorname{eval}_k(\rho[y:=v],N) & \text{if } u = \operatorname{inr}\ v
\end{cases} \\
\operatorname{eval}_k(\rho,\operatorname{prim}_p\ L\ M) &= n \leftarrow\operatorname{eval}_k(\rho,L);\ m \leftarrow\operatorname{eval}_k(\rho,M);\ \operatorname{ret}\ \delta_p(n,m) \\
\operatorname{eval}_k(\rho,\operatorname{let}\ x=M\ \operatorname{in}\ N) &= v \leftarrow\operatorname{eval}_k(\rho,M);\ \operatorname{eval}_k(\rho[x:=v],N) \\
\operatorname{eval}_k(\rho,\operatorname{unfold}\ M) &= \operatorname{fold}\ v \leftarrow\operatorname{eval}_k(\rho,M);\ \operatorname{ret}\ v
\end{aligned} eval k ( ρ , x ) eval k ( ρ , λ x . N ) eval k ( ρ , L M ) eval k ( ρ , c ) eval k ( ρ , tuple ( M 1 , … , M n )) eval k ( ρ , get i M ) eval k ( ρ , inl M ) eval k ( ρ , case L ( x . M ) ( y . N )) eval k ( ρ , prim p L M ) eval k ( ρ , let x = M in N ) eval k ( ρ , unfold M ) = ret ρ ( x ) = ret ⟨ λ x . N , ρ ⟩ = f ← eval k ( ρ , L ) ; w ← eval k ( ρ , M ) ; call k ( f , w ) = ret c = v 1 ← eval k ( ρ , M 1 ) ; … ; v n ← eval k ( ρ , M n ) ; ret tup ( v 1 , … , v n ) = tup ( v 1 , … , v n ) ← eval k ( ρ , M ) ; ret v i = v ← eval k ( ρ , M ) ; ret ( inl v ) (likewise inr and fold) = u ← eval k ( ρ , L ) ; { eval k ( ρ [ x := v ] , M ) eval k ( ρ [ y := v ] , N ) if u = inl v if u = inr v = n ← eval k ( ρ , L ) ; m ← eval k ( ρ , M ) ; ret δ p ( n , m ) = v ← eval k ( ρ , M ) ; eval k ( ρ [ x := v ] , N ) = fold v ← eval k ( ρ , M ) ; ret v
call 0 ( f , w ) = t i m e o u t \operatorname{call}_0(f,w) = \mathrm{timeout} call 0 ( f , w ) = timeout
call k + 1 ( ⟨ λ x . N , ρ ⟩ , w ) = eval k ( ρ [ x : = w ] , N ) \operatorname{call}_{k+1}(\langle\lambda x.\,N,\rho\rangle,w) = \operatorname{eval}_k(\rho[x:=w],N) call k + 1 (⟨ λ x . N , ρ ⟩ , w ) = eval k ( ρ [ x := w ] , N )
A pattern on the left of ← \leftarrow ← always matches, because the interpreter is defined on well-typed terms and values. The interpreter of a closed program M M M is k ↦ eval k ( ∅ , M ) k \mapsto\operatorname{eval}_k(\varnothing,M) k ↦ eval k ( ∅ , M ) .
Interpreter for Instrμ _\mu μ . A machine state m = ⟨ R , σ , u , b ⟩ m = \langle R,\sigma,u,b\rangle m = ⟨ R , σ , u , b ⟩ of Instrμ _\mu μ has the registers R R R , which map each of rax, rdi, and rsi to a value or unset; the frame σ \sigma σ , which holds a value in each initialized slot; the environment u u u of the open closure, if any; and the flag b b b , which is a Boolean or unset. We write m ( r ) m(r) m ( r ) for R ( r ) R(r) R ( r ) . arg ( a , m ) \operatorname{arg}(a,m) arg ( a , m ) converts an operand a a a into a value, obj ( c o n , m ) \operatorname{obj}(con,m) obj ( co n , m ) converts a constructor c o n con co n to be allocated into a value, and put ( d s t , v , m ) \operatorname{put}(dst,v,m) put ( d s t , v , m ) stores v v v in a destination d s t dst d s t .
Auxiliary definitions
arg ( a , m ) = v obj ( c o n , m ) = v put ( d s t , v , m ) = m ′ arg ( x , ⟨ R , σ , u , b ⟩ ) = σ ( x ) arg ( c , m ) = c arg ( f , m ) = code f arg ( % r , ⟨ R , σ , u , b ⟩ ) = R ( r ) arg ( fold a , m ) = fold ( arg ( a , m ) ) arg ( unfold a , m ) = fold v ← arg ( a , m ) ; v obj ( pair ( a 1 , a 2 ) , m ) = pair ( arg ( a 1 , m ) , arg ( a 2 , m ) ) obj ( tuple ( a 1 , … , a n ) , m ) = tup ( arg ( a 1 , m ) , … , arg ( a n , m ) ) obj ( inl ( a ) , m ) = inl ( arg ( a , m ) ) obj ( inr ( a ) , m ) = inr ( arg ( a , m ) ) put ( % r , v , ⟨ R , σ , u , b ⟩ ) = ⟨ R [ r : = v ] , σ , u , b ⟩ put ( x , v , ⟨ R , σ , u , b ⟩ ) = ⟨ R , σ [ x : = v ] ∣ I ′ , none , b ⟩ \begin{aligned}
\operatorname{arg}(a,m)&=v & \operatorname{obj}(con,m)&=v & \operatorname{put}(dst,v,m)&=m'\\
\operatorname{arg}(x,\langle R,\sigma,u,b\rangle)&=\sigma(x)\\
\operatorname{arg}(c,m)&=c\\
\operatorname{arg}(f,m)&=\operatorname{code} f\\
\operatorname{arg}(\%r,\langle R,\sigma,u,b\rangle)&=R(r)\\
\operatorname{arg}(\operatorname{fold} a,m)&=\operatorname{fold}(\operatorname{arg}(a,m))\\
\operatorname{arg}(\operatorname{unfold} a,m)&=\operatorname{fold} v\leftarrow\operatorname{arg}(a,m);v\\
\operatorname{obj}(\operatorname{pair}(a_1,a_2),m)&=\operatorname{pair}(\operatorname{arg}(a_1,m),\operatorname{arg}(a_2,m))\\
\operatorname{obj}(\operatorname{tuple}(a_1,\ldots,a_n),m)&=\operatorname{tup}(\operatorname{arg}(a_1,m),\ldots,\operatorname{arg}(a_n,m))\\
\operatorname{obj}(\operatorname{inl}(a),m)&=\operatorname{inl}(\operatorname{arg}(a,m))\\
\operatorname{obj}(\operatorname{inr}(a),m)&=\operatorname{inr}(\operatorname{arg}(a,m))\\
\operatorname{put}(\%r,v,\langle R,\sigma,u,b\rangle)&=\langle R[r:=v],\sigma,u,b\rangle\\
\operatorname{put}(x,v,\langle R,\sigma,u,b\rangle)&=\langle R,\sigma[x:=v]\mathbin{|}_{I'},\operatorname{none},b\rangle
\end{aligned} arg ( a , m ) arg ( x , ⟨ R , σ , u , b ⟩) arg ( c , m ) arg ( f , m ) arg ( % r , ⟨ R , σ , u , b ⟩) arg ( fold a , m ) arg ( unfold a , m ) obj ( pair ( a 1 , a 2 ) , m ) obj ( tuple ( a 1 , … , a n ) , m ) obj ( inl ( a ) , m ) obj ( inr ( a ) , m ) put ( % r , v , ⟨ R , σ , u , b ⟩) put ( x , v , ⟨ R , σ , u , b ⟩) = v = σ ( x ) = c = code f = R ( r ) = fold ( arg ( a , m )) = fold v ← arg ( a , m ) ; v = pair ( arg ( a 1 , m ) , arg ( a 2 , m )) = tup ( arg ( a 1 , m ) , … , arg ( a n , m )) = inl ( arg ( a , m )) = inr ( arg ( a , m )) = ⟨ R [ r := v ] , σ , u , b ⟩ = ⟨ R , σ [ x := v ] ∣ I ′ , none , b ⟩ obj ( co n , m ) = v put ( d s t , v , m ) = m ′
Here σ [ x : = v ] \sigma[x:=v] σ [ x := v ] updates slot x x x in place, and ∣ I ′ \mathbin{|}_{I'} ∣ I ′ keeps the slots I ′ I' I ′ that the rest of the sequence starts with (DST-SLOT, Appendix B.7), which forgets the values of variables whose scope has ended. Writing a slot also closes the open closure. DST-SLOT resets the open closure to ⋅ \cdot ⋅ , and put \operatorname{put} put closes it by setting it to none. The instructions other than callq update the state as i n s t r instr in s t r specifies.
The effect of an instruction
instr ( i , m ) = m ′ \operatorname{instr}(i,m)=m' instr ( i , m ) = m ′
instr ( movq a , d s t , m ) = put ( d s t , arg ( a , m ) , m ) instr ( subq a , % r , m ) = put ( % r , δ sub ( m ( r ) , arg ( a , m ) ) , m ) instr ( cmpq a 2 , a 1 , ⟨ R , σ , u , b ⟩ ) = ⟨ R , σ , u , ( arg ( a 1 , m ) = arg ( a 2 , m ) ) ⟩ instr ( sete d s t , ⟨ R , σ , u , b ⟩ ) = { put ( d s t , inl tup ( ) , m ) if b = true put ( d s t , inr tup ( ) , m ) if b = false instr ( alloc c o n , d s t , m ) = put ( d s t , obj ( c o n , m ) , m ) instr ( load get i a , d s t , m ) = tup ( v 1 , … , v n ) ← arg ( a , m ) ; put ( d s t , v i , m ) instr ( fst x , % r , ⟨ R , σ , u , b ⟩ ) = pair ( c , u ′ ) ← σ ( x ) ; ⟨ R ∘ [ r : = c ] , σ , u ′ , b ⟩ instr ( snd x , % r , ⟨ R , σ , u , b ⟩ ) = ⟨ R [ r : = u ] , σ , u , b ⟩ \begin{aligned}
\operatorname{instr}(\operatorname{movq}\ a,dst,m)&=\operatorname{put}(dst,\operatorname{arg}(a,m),m)\\
\operatorname{instr}(\operatorname{subq}\ a,\%r,m)&=\operatorname{put}(\%r,\delta_{\operatorname{sub}}(m(r),\operatorname{arg}(a,m)),m)\\
\operatorname{instr}(\operatorname{cmpq}\ a_2,a_1,\langle R,\sigma,u,b\rangle)&=\langle R,\sigma,u,(\operatorname{arg}(a_1,m)=\operatorname{arg}(a_2,m))\rangle\\
\operatorname{instr}(\operatorname{sete}\ dst,\langle R,\sigma,u,b\rangle)&=
\begin{cases}
\operatorname{put}(dst,\operatorname{inl}\operatorname{tup}(),m)&\text{if }b=\operatorname{true}\\
\operatorname{put}(dst,\operatorname{inr}\operatorname{tup}(),m)&\text{if }b=\operatorname{false}
\end{cases}\\
\operatorname{instr}(\operatorname{alloc}\ con,dst,m)&=\operatorname{put}(dst,\operatorname{obj}(con,m),m)\\
\operatorname{instr}(\operatorname{load}\ \operatorname{get}_i\ a,dst,m)&=\operatorname{tup}(v_1,\ldots,v_n)\leftarrow\operatorname{arg}(a,m);\operatorname{put}(dst,v_i,m)\\
\operatorname{instr}(\operatorname{fst}\ x,\%r,\langle R,\sigma,u,b\rangle)&=\operatorname{pair}(c,u')\leftarrow\sigma(x);\langle R^\circ[r:=c],\sigma,u',b\rangle\\
\operatorname{instr}(\operatorname{snd}\ x,\%r,\langle R,\sigma,u,b\rangle)&=\langle R[r:=u],\sigma,u,b\rangle
\end{aligned} instr ( movq a , d s t , m ) instr ( subq a , % r , m ) instr ( cmpq a 2 , a 1 , ⟨ R , σ , u , b ⟩) instr ( sete d s t , ⟨ R , σ , u , b ⟩) instr ( alloc co n , d s t , m ) instr ( load get i a , d s t , m ) instr ( fst x , % r , ⟨ R , σ , u , b ⟩) instr ( snd x , % r , ⟨ R , σ , u , b ⟩) = put ( d s t , arg ( a , m ) , m ) = put ( % r , δ sub ( m ( r ) , arg ( a , m )) , m ) = ⟨ R , σ , u , ( arg ( a 1 , m ) = arg ( a 2 , m ))⟩ = { put ( d s t , inl tup ( ) , m ) put ( d s t , inr tup ( ) , m ) if b = true if b = false = put ( d s t , obj ( co n , m ) , m ) = tup ( v 1 , … , v n ) ← arg ( a , m ) ; put ( d s t , v i , m ) = pair ( c , u ′ ) ← σ ( x ) ; ⟨ R ∘ [ r := c ] , σ , u ′ , b ⟩ = ⟨ R [ r := u ] , σ , u , b ⟩
Here m m m on the right is the state on the left, and R ∘ R^\circ R ∘ is the result of wiping R R R , which unsets the registers that held the code or the environment of an earlier open closure, as wipe ( Θ ) \operatorname{wipe}(\Theta) wipe ( Θ ) unsets their types in INS-FST (Appendix B.7). As in the interpreter of FPC, a pattern on the left of ← \leftarrow ← always matches, because instructions are typed by the state they start in. The interpreter run k ( B , m , s ) \operatorname{run}_k(B,m,s) run k ( B , m , s ) runs a sequence s s s in the state m m m , where B = s 0 , … , s n − 1 B=s_0,\ldots,s_{n-1} B = s 0 , … , s n − 1 lists the blocks that s s s may jump to. A label l l l names the block s l s_l s l of B B B . Because control flow within a function is acyclic, a block may jump only to the blocks created before it, which run after it, so jumping to l l l runs s l s_l s l with the blocks B l = s 0 , … , s l − 1 B_l=s_0,\ldots,s_{l-1} B l = s 0 , … , s l − 1 and pops the newer ones, which s l s_l s l cannot reach. A callq to the code code f \operatorname{code} f code f runs c a l l k ( c o d e f , u , w ) call_k(\mathrm{code}\ f,u,w) c a l l k ( code f , u , w ) , which does not use B B B . The program has a global list of definitions, fixed for the whole run, and c a l l k call_k c a l l k looks up definition f f f in it, whose function body has the start s f s_f s f and the blocks B f B_f B f ; when c a l l k call_k c a l l k returns, the caller resumes with its own B B B .
The interpreter of I n s t r μ \mathrm{Instr}_{\mu} Instr μ
r u n k ( B , m , s ) = e j u m p k ( B , l , σ ) = e c a l l k ( c , u , w ) = e \begin{aligned}
run_k(B,m,s)&=e & jump_k(B,l,\sigma)&=e & call_k(c,u,w)&=e
\end{aligned} r u n k ( B , m , s ) = e j u m p k ( B , l , σ ) = e c a l l k ( c , u , w ) = e
r u n k ( B , m , i ; s ) = r u n k ( B , instr ( i , m ) , s ) for i not callq r u n k ( B , m , callq a ; s ) = v ← c a l l k ( arg ( a , m ) , m ( r d i ) , m ( r s i ) ) ; r u n k ( B , m v , s ) r u n k ( B , m , r e t q ) = ret m ( r a x ) r u n k ( B , ⟨ R , σ , u , b ⟩ , jmp l ) = j u m p k ( B , l , σ ) r u n k ( B , ⟨ R , σ , u , b ⟩ , case x y z l 1 l 2 ) = { j u m p k ( B , l 1 , σ [ y : = v ] ) if σ ( x ) = inl v j u m p k ( B , l 2 , σ [ z : = v ] ) if σ ( x ) = inr v j u m p k ( B , l , σ ) = r u n k ( B l , m σ , s l ) c a l l 0 ( c o d e f , u , w ) = timeout c a l l k + 1 ( c o d e f , u , w ) = r u n k ( B f , m u , w , s f ) \begin{aligned}
run_k(B,m,i;s)&=run_k(B,\operatorname{instr}(i,m),s) && \text{for }i\text{ not callq}\\
run_k(B,m,\operatorname{callq}\ a;s)&=v\leftarrow call_k(\operatorname{arg}(a,m),m(\mathrm{rdi}),m(\mathrm{rsi}));\ run_k(B,m_v,s)\\
run_k(B,m,\mathrm{retq})&=\operatorname{ret}\ m(\mathrm{rax})\\
run_k(B,\langle R,\sigma,u,b\rangle,\operatorname{jmp}\ l)&=jump_k(B,l,\sigma)\\
run_k(B,\langle R,\sigma,u,b\rangle,\operatorname{case}\ x\ y\ z\ l_1\ l_2)&=
\begin{cases}
jump_k(B,l_1,\sigma[y:=v]) & \text{if }\sigma(x)=\operatorname{inl}\ v\\
jump_k(B,l_2,\sigma[z:=v]) & \text{if }\sigma(x)=\operatorname{inr}\ v
\end{cases}\\
jump_k(B,l,\sigma)&=run_k(B_l,m_\sigma,s_l)\\
call_0(\mathrm{code}\ f,u,w)&=\operatorname{timeout}\\
call_{k+1}(\mathrm{code}\ f,u,w)&=run_k(B_f,m_{u,w},s_f)
\end{aligned} r u n k ( B , m , i ; s ) r u n k ( B , m , callq a ; s ) r u n k ( B , m , retq ) r u n k ( B , ⟨ R , σ , u , b ⟩ , jmp l ) r u n k ( B , ⟨ R , σ , u , b ⟩ , case x y z l 1 l 2 ) j u m p k ( B , l , σ ) c a l l 0 ( code f , u , w ) c a l l k + 1 ( code f , u , w ) = r u n k ( B , instr ( i , m ) , s ) = v ← c a l l k ( arg ( a , m ) , m ( rdi ) , m ( rsi )) ; r u n k ( B , m v , s ) = ret m ( rax ) = j u m p k ( B , l , σ ) = { j u m p k ( B , l 1 , σ [ y := v ]) j u m p k ( B , l 2 , σ [ z := v ]) if σ ( x ) = inl v if σ ( x ) = inr v = r u n k ( B l , m σ , s l ) = timeout = r u n k ( B f , m u , w , s f ) for i not callq
Here m v m_v m v has the frame of m m m , rax set to v v v , and nothing else set; m σ m_\sigma m σ has the frame σ \sigma σ and nothing else set; and m u , w m_{u,w} m u , w has u u u in rdi, w w w in rsi, a frame with no slot initialized, and nothing else set. The interpreter of a program is k ↦ r u n k ( B , m 0 , s ) k\mapsto run_k(B,m_0,s) k ↦ r u n k ( B , m 0 , s ) for the main function body s s s with blocks B B B , where m 0 m_0 m 0 has no slot initialized and nothing set.
Proposition F.1 (Correctness, clocked interpreters). Let M M M be a closed term of FPC of type Int. For every integer n n n , the interpreter of FPC on M M M terminates with n n n if and only if the interpreter of I n s t r μ \mathrm{Instr}_{\mu} Instr μ on c o m p i l e ( M ) compile(M) co m p i l e ( M ) does, and one diverges if and only if the other does. More generally, for every closed term M M M , the two interpreters are equivalent.
Abstract Machines
Agda: FPC/Semantics/AbstractMachines/FPC.agda, FPC/Semantics/AbstractMachines/X86.agda
Machine for FPC. The machine is the CEK machine of Felleisen and Friedman [ [26 ] , extended from the λ \lambda λ -calculus to the constructs of FPC.
States and continuations ς \varsigma ς and κ \kappa κ
ς : : = eval ( M , ρ , κ ) ∣ ret ( v , κ ) \varsigma::= \operatorname{eval}(M,\rho,\kappa)\mid\operatorname{ret}(v,\kappa) ς ::= eval ( M , ρ , κ ) ∣ ret ( v , κ )
κ : : = halt ∣ appL ( M , ρ , κ ) ∣ appR ( f , κ ) ∣ tupK ( v ⃗ , M , ρ , κ ) ∣ getK i ( κ ) ∣ inlK ( κ ) ∣ inrK ( κ ) ∣ caseK ( x . M , y . N , ρ , κ ) ∣ primL p ( M , ρ , κ ) ∣ primR p ( n , κ ) ∣ letK ( x . N , ρ , κ ) ∣ foldK ( κ ) ∣ unfoldK ( κ ) \begin{aligned}
\kappa::= {}&\operatorname{halt}\mid\operatorname{appL}(M,\rho,\kappa)\mid\operatorname{appR}(f,\kappa)\mid\operatorname{tupK}(\vec{v},M,\rho,\kappa)\mid\operatorname{getK}_i(\kappa)\mid\operatorname{inlK}(\kappa)\mid\operatorname{inrK}(\kappa)\\
&\mid\operatorname{caseK}(x.M,y.N,\rho,\kappa)\mid\operatorname{primL}_p(M,\rho,\kappa)\mid\operatorname{primR}_p(n,\kappa)\\
&\mid\operatorname{letK}(x.N,\rho,\kappa)\mid\operatorname{foldK}(\kappa)\mid\operatorname{unfoldK}(\kappa)
\end{aligned} κ ::= halt ∣ appL ( M , ρ , κ ) ∣ appR ( f , κ ) ∣ tupK ( v , M , ρ , κ ) ∣ getK i ( κ ) ∣ inlK ( κ ) ∣ inrK ( κ ) ∣ caseK ( x . M , y . N , ρ , κ ) ∣ primL p ( M , ρ , κ ) ∣ primR p ( n , κ ) ∣ letK ( x . N , ρ , κ ) ∣ foldK ( κ ) ∣ unfoldK ( κ )
A state either evaluates a term M M M in an environment ρ \rho ρ with a continuation κ \kappa κ , eval ( M , ρ , κ ) \operatorname{eval}(M,\rho,\kappa) eval ( M , ρ , κ ) , or returns a value v v v to a continuation, ret ( v , κ ) \operatorname{ret}(v,\kappa) ret ( v , κ ) . The first is the CEK triple ⟨ M , ρ , κ ⟩ \langle M,\rho,\kappa\rangle ⟨ M , ρ , κ ⟩ . The second is the return state of Felleisen and Friedman [ [26 ] , ⟨ ‡ , ∅ , ( κ ret v ) ⟩ \langle\ddagger,\varnothing,(\kappa\ \operatorname{ret}\ v)\rangle ⟨ ‡ , ∅ , ( κ ret v )⟩ , whose environment is always empty, so we drop it. A continuation is a stack of frames, and each frame holds the work that remains after a subterm returns its value. For example, halt \operatorname{halt} halt is the empty continuation (their stop \operatorname{stop} stop ), appL ( M , ρ , κ ) \operatorname{appL}(M,\rho,\kappa) appL ( M , ρ , κ ) holds the argument M M M that the machine evaluates next (their arg \operatorname{arg} arg ), and appR ( f , κ ) \operatorname{appR}(f,\kappa) appR ( f , κ ) holds the function f f f that awaits the argument’s value (their fun \operatorname{fun} fun ). The other frames are for primitives, let, tuples, sums and recursive types. An environment is a list of values, and the environment inside a closure is stored as a tuple.
Steps of the CEK machine of FPC
eval ( x , ρ , κ ) ⟶ ret ( ρ ( x ) , κ ) eval ( λ x . N , ρ , κ ) ⟶ ret ( ⟨ λ x . N , ρ ⟩ , κ ) eval ( L M , ρ , κ ) ⟶ eval ( L , ρ , appL ( M , ρ , κ ) ) eval ( c , ρ , κ ) ⟶ ret ( c , κ ) eval ( tuple ( ) , ρ , κ ) ⟶ ret ( tup ( ) , κ ) eval ( tuple ( M 1 , … , M n ) , ρ , κ ) ⟶ eval ( M 1 , ρ , tupK ( ( ) , ( M 2 , … , M n ) , ρ , κ ) ) eval ( get i M , ρ , κ ) ⟶ eval ( M , ρ , getK i ( κ ) ) (likewise inl, inr, fold, unfold) eval ( case L ( x . M ) ( y . N ) , ρ , κ ) ⟶ eval ( L , ρ , caseK ( x . M , y . N , ρ , κ ) ) eval ( prim p L M , ρ , κ ) ⟶ eval ( L , ρ , primL p ( M , ρ , κ ) ) eval ( let x = M in N , ρ , κ ) ⟶ eval ( M , ρ , letK ( x . N , ρ , κ ) ) ret ( f , appL ( M , ρ , κ ) ) ⟶ eval ( M , ρ , appR ( f , κ ) ) ret ( w , appR ( ⟨ λ x . N , ρ ⟩ , κ ) ) ⟶ eval ( N , ρ [ x : = w ] , κ ) ret ( v , tupK ( v ⃗ , ( ) , ρ , κ ) ) ⟶ ret ( tup ( v ⃗ , v ) , κ ) ret ( v , tupK ( v ⃗ , ( M , M ⃗ ) , ρ , κ ) ) ⟶ eval ( M , ρ , tupK ( ( v ⃗ , v ) , M ⃗ , ρ , κ ) ) ret ( tup ( v 1 , … , v n ) , getK i ( κ ) ) ⟶ ret ( v i , κ ) ret ( v , inlK ( κ ) ) ⟶ ret ( inl v , κ ) (likewise inr, fold) ret ( inl v , caseK ( x . M , y . N , ρ , κ ) ) ⟶ eval ( M , ρ [ x : = v ] , κ ) ret ( inr v , caseK ( x . M , y . N , ρ , κ ) ) ⟶ eval ( N , ρ [ y : = v ] , κ ) ret ( n , primL p ( M , ρ , κ ) ) ⟶ eval ( M , ρ , primR p ( n , κ ) ) ret ( m , primR p ( n , κ ) ) ⟶ ret ( δ p ( n , m ) , κ ) ret ( v , letK ( x . N , ρ , κ ) ) ⟶ eval ( N , ρ [ x : = v ] , κ ) ret ( fold v , unfoldK ( κ ) ) ⟶ ret ( v , κ ) \begin{aligned}
\operatorname{eval}(x,\rho,\kappa)&\longrightarrow\operatorname{ret}(\rho(x),\kappa)\\
\operatorname{eval}(\lambda x.N,\rho,\kappa)&\longrightarrow\operatorname{ret}(\langle\lambda x.N,\rho\rangle,\kappa)\\
\operatorname{eval}(L\ M,\rho,\kappa)&\longrightarrow\operatorname{eval}(L,\rho,\operatorname{appL}(M,\rho,\kappa))\\
\operatorname{eval}(c,\rho,\kappa)&\longrightarrow\operatorname{ret}(c,\kappa)\\
\operatorname{eval}(\operatorname{tuple}(),\rho,\kappa)&\longrightarrow\operatorname{ret}(\operatorname{tup}(),\kappa)\\
\operatorname{eval}(\operatorname{tuple}(M_1,\ldots,M_n),\rho,\kappa)&\longrightarrow\operatorname{eval}(M_1,\rho,\operatorname{tupK}((),(M_2,\ldots,M_n),\rho,\kappa))\\
\operatorname{eval}(\operatorname{get}_i\ M,\rho,\kappa)&\longrightarrow\operatorname{eval}(M,\rho,\operatorname{getK}_i(\kappa))\quad\text{(likewise inl, inr, fold, unfold)}\\
\operatorname{eval}(\operatorname{case}\ L\ (x.M)\ (y.N),\rho,\kappa)&\longrightarrow\operatorname{eval}(L,\rho,\operatorname{caseK}(x.M,y.N,\rho,\kappa))\\
\operatorname{eval}(\operatorname{prim}_p\ L\ M,\rho,\kappa)&\longrightarrow\operatorname{eval}(L,\rho,\operatorname{primL}_p(M,\rho,\kappa))\\
\operatorname{eval}(\operatorname{let}\ x=M\ \operatorname{in}\ N,\rho,\kappa)&\longrightarrow\operatorname{eval}(M,\rho,\operatorname{letK}(x.N,\rho,\kappa))\\
\operatorname{ret}(f,\operatorname{appL}(M,\rho,\kappa))&\longrightarrow\operatorname{eval}(M,\rho,\operatorname{appR}(f,\kappa))\\
\operatorname{ret}(w,\operatorname{appR}(\langle\lambda x.N,\rho\rangle,\kappa))&\longrightarrow\operatorname{eval}(N,\rho[x:=w],\kappa)\\
\operatorname{ret}(v,\operatorname{tupK}(\vec v,(),\rho,\kappa))&\longrightarrow\operatorname{ret}(\operatorname{tup}(\vec v,v),\kappa)\\
\operatorname{ret}(v,\operatorname{tupK}(\vec v,(M,\vec M),\rho,\kappa))&\longrightarrow\operatorname{eval}(M,\rho,\operatorname{tupK}((\vec v,v),\vec M,\rho,\kappa))\\
\operatorname{ret}(\operatorname{tup}(v_1,\ldots,v_n),\operatorname{getK}_i(\kappa))&\longrightarrow\operatorname{ret}(v_i,\kappa)\\
\operatorname{ret}(v,\operatorname{inlK}(\kappa))&\longrightarrow\operatorname{ret}(\operatorname{inl}\ v,\kappa)\quad\text{(likewise inr, fold)}\\
\operatorname{ret}(\operatorname{inl}\ v,\operatorname{caseK}(x.M,y.N,\rho,\kappa))&\longrightarrow\operatorname{eval}(M,\rho[x:=v],\kappa)\\
\operatorname{ret}(\operatorname{inr}\ v,\operatorname{caseK}(x.M,y.N,\rho,\kappa))&\longrightarrow\operatorname{eval}(N,\rho[y:=v],\kappa)\\
\operatorname{ret}(n,\operatorname{primL}_p(M,\rho,\kappa))&\longrightarrow\operatorname{eval}(M,\rho,\operatorname{primR}_p(n,\kappa))\\
\operatorname{ret}(m,\operatorname{primR}_p(n,\kappa))&\longrightarrow\operatorname{ret}(\delta_p(n,m),\kappa)\\
\operatorname{ret}(v,\operatorname{letK}(x.N,\rho,\kappa))&\longrightarrow\operatorname{eval}(N,\rho[x:=v],\kappa)\\
\operatorname{ret}(\operatorname{fold}\ v,\operatorname{unfoldK}(\kappa))&\longrightarrow\operatorname{ret}(v,\kappa)
\end{aligned} eval ( x , ρ , κ ) eval ( λ x . N , ρ , κ ) eval ( L M , ρ , κ ) eval ( c , ρ , κ ) eval ( tuple ( ) , ρ , κ ) eval ( tuple ( M 1 , … , M n ) , ρ , κ ) eval ( get i M , ρ , κ ) eval ( case L ( x . M ) ( y . N ) , ρ , κ ) eval ( prim p L M , ρ , κ ) eval ( let x = M in N , ρ , κ ) ret ( f , appL ( M , ρ , κ )) ret ( w , appR (⟨ λ x . N , ρ ⟩ , κ )) ret ( v , tupK ( v , ( ) , ρ , κ )) ret ( v , tupK ( v , ( M , M ) , ρ , κ )) ret ( tup ( v 1 , … , v n ) , getK i ( κ )) ret ( v , inlK ( κ )) ret ( inl v , caseK ( x . M , y . N , ρ , κ )) ret ( inr v , caseK ( x . M , y . N , ρ , κ )) ret ( n , primL p ( M , ρ , κ )) ret ( m , primR p ( n , κ )) ret ( v , letK ( x . N , ρ , κ )) ret ( fold v , unfoldK ( κ )) ⟶ ret ( ρ ( x ) , κ ) ⟶ ret (⟨ λ x . N , ρ ⟩ , κ ) ⟶ eval ( L , ρ , appL ( M , ρ , κ )) ⟶ ret ( c , κ ) ⟶ ret ( tup ( ) , κ ) ⟶ eval ( M 1 , ρ , tupK (( ) , ( M 2 , … , M n ) , ρ , κ )) ⟶ eval ( M , ρ , getK i ( κ )) (likewise inl, inr, fold, unfold) ⟶ eval ( L , ρ , caseK ( x . M , y . N , ρ , κ )) ⟶ eval ( L , ρ , primL p ( M , ρ , κ )) ⟶ eval ( M , ρ , letK ( x . N , ρ , κ )) ⟶ eval ( M , ρ , appR ( f , κ )) ⟶ eval ( N , ρ [ x := w ] , κ ) ⟶ ret ( tup ( v , v ) , κ ) ⟶ eval ( M , ρ , tupK (( v , v ) , M , ρ , κ )) ⟶ ret ( v i , κ ) ⟶ ret ( inl v , κ ) (likewise inr, fold) ⟶ eval ( M , ρ [ x := v ] , κ ) ⟶ eval ( N , ρ [ y := v ] , κ ) ⟶ eval ( M , ρ , primR p ( n , κ )) ⟶ ret ( δ p ( n , m ) , κ ) ⟶ eval ( N , ρ [ x := v ] , κ ) ⟶ ret ( v , κ )
The machine of a closed program M M M starts in eval ( M , ∅ , halt ) \operatorname{eval}(M,\varnothing,\operatorname{halt}) eval ( M , ∅ , halt ) . The terminal state is ret ( v , halt ) \operatorname{ret}(v,\operatorname{halt}) ret ( v , halt ) .
We define the final observation as follows.
Final observation of the CEK machine of FPC
ret ( v , halt ) ⟶ stop ( obs ( v ) ) \operatorname{ret}(v,\operatorname{halt})\longrightarrow\operatorname{stop}(\operatorname{obs}(v)) ret ( v , halt ) ⟶ stop ( obs ( v ))
In the Agda development, terms, environments and continuations are intrinsically typed, and a variable is a de Bruijn index into the environment, so a state is well typed by construction. The CEK transitions are defined in the step function, step : State → Next State \operatorname{step}:\operatorname{State}\to\operatorname{Next\ State} step : State → Next State , which maps each state to a next state or to a final observation.
Machine for Instr μ \operatorname{Instr}_{\mu} Instr μ . In the notation of Appendix § 15 .§ 2 , a state of the machine of Instr μ \operatorname{Instr}_{\mu} Instr μ , run ( s , B , Σ , m ) \operatorname{run}(s,B,\Sigma,m) run ( s , B , Σ , m ) , runs a sequence s s s with blocks B B B , a stack Σ \Sigma Σ of the callers’ return points, and registers, frame, open closure and flag m m m . A return point retK ( s , B , σ ) \operatorname{retK}(s,B,\sigma) retK ( s , B , σ ) holds the rest s s s of the caller’s sequence, its blocks, and its frame, which the callee’s retq \operatorname{retq} retq restores.
States and stacks
ς : : = run ( s , B , Σ , m ) \varsigma::= \operatorname{run}(s,B,\Sigma,m) ς ::= run ( s , B , Σ , m )
Σ : : = halt ∣ retK ( s , B , σ ) : : Σ \Sigma::= \operatorname{halt} \mid\operatorname{retK}(s,B,\sigma) :: \Sigma Σ ::= halt ∣ retK ( s , B , σ ) :: Σ
Steps of the machine of Instr μ \operatorname{Instr}_{\mu} Instr μ
ς ⟶ ς ′ \varsigma\longrightarrow\varsigma' ς ⟶ ς ′
run ( i ; s , B , Σ , m ) ⟶ run ( s , B , Σ , instr ( i , m ) ) for i not callq \operatorname{run}(i;s,B,\Sigma,m) \longrightarrow\operatorname{run}(s,B,\Sigma,\operatorname{instr}(i,m))
\qquad\text{for } i \text{ not callq} run ( i ; s , B , Σ , m ) ⟶ run ( s , B , Σ , instr ( i , m )) for i not callq
run ( callq a ; s , B , Σ , m ) ⟶ run ( s f , B f , retK ( s , B , σ ) : : Σ , m u , w ) \operatorname{run}(\operatorname{callq}\ a;s,B,\Sigma,m) \longrightarrow\operatorname{run}(s_f,B_f,\operatorname{retK}(s,B,\sigma) :: \Sigma,m_{u,w}) run ( callq a ; s , B , Σ , m ) ⟶ run ( s f , B f , retK ( s , B , σ ) :: Σ , m u , w )
where arg ( a , m ) = code f \operatorname{arg}(a,m)=\operatorname{code}\ f arg ( a , m ) = code f , u = m ( rdi ) u=m(\operatorname{rdi}) u = m ( rdi ) , w = m ( rsi ) w=m(\operatorname{rsi}) w = m ( rsi )
run ( retq , B , retK ( s , B ′ , σ ′ ) : : Σ , m ) ⟶ run ( s , B ′ , Σ , m ′ ) \operatorname{run}(\operatorname{retq},B,\operatorname{retK}(s,B',\sigma') :: \Sigma,m) \longrightarrow\operatorname{run}(s,B',\Sigma,m') run ( retq , B , retK ( s , B ′ , σ ′ ) :: Σ , m ) ⟶ run ( s , B ′ , Σ , m ′ )
where m ′ m' m ′ has the frame σ ′ \sigma' σ ′ and only rax set, to m ( rax ) m(\operatorname{rax}) m ( rax )
run ( jmp l , B , Σ , m ) ⟶ run ( s l , B l , Σ , m σ ) \operatorname{run}(\operatorname{jmp}\ l,B,\Sigma,m) \longrightarrow\operatorname{run}(s_l,B_l,\Sigma,m_{\sigma}) run ( jmp l , B , Σ , m ) ⟶ run ( s l , B l , Σ , m σ )
run ( case x y z l 1 l 2 , B , Σ , m ) ⟶ run ( s l 1 , B l 1 , Σ , m σ [ y : = v ] ) \operatorname{run}(\operatorname{case}\ x\ y\ z\ l_1\ l_2,B,\Sigma,m) \longrightarrow\operatorname{run}(s_{l_1},B_{l_1},\Sigma,m_{\sigma[y:=v]}) run ( case x y z l 1 l 2 , B , Σ , m ) ⟶ run ( s l 1 , B l 1 , Σ , m σ [ y := v ] )
if σ ( x ) = inl v \sigma(x)=\operatorname{inl}\ v σ ( x ) = inl v
(and likewise for inr \operatorname{inr} inr , z z z , and l 2 l_2 l 2 )
Final observation of the machine of Instr μ \operatorname{Instr}_{\mu} Instr μ
ς ⟶ stop ( o ) \varsigma\longrightarrow\operatorname{stop}(o) ς ⟶ stop ( o )
run ( retq , B , halt , m ) ⟶ stop ( obs ( m ( rax ) ) ) \operatorname{run}(\operatorname{retq},B,\operatorname{halt},m) \longrightarrow\operatorname{stop}(\operatorname{obs}(m(\operatorname{rax}))) run ( retq , B , halt , m ) ⟶ stop ( obs ( m ( rax )))
The machine of a program starts in run ( s , B , halt , m 0 ) \operatorname{run}(s,B,\operatorname{halt},m_0) run ( s , B , halt , m 0 ) for the main function body s s s with blocks B B B .
Proposition F.2 (Correctness, abstract machines). Let M M M be a closed term of FPC of type Int. For every integer n n n , the machine of FPC on M M M terminates with n n n if and only if the machine of Instr μ \operatorname{Instr}_{\mu} Instr μ on compile ( M ) \operatorname{compile}(M) compile ( M ) does, and one diverges if and only if the other does. More generally, for every closed term M M M , the two machines are equivalent.
Reduction Semantics
Agda: FPC/Semantics/ReductionSemantics/FPC.agda, Framework/Reduction.agda
The reduction semantics of FPC rewrites closed terms by substitution. The evaluation order is call by value and left to right. We define values as follows.
V , W : : = λ x . N ∣ c ∣ tuple ( V 1 , … , V n ) ∣ inl V ∣ inr V ∣ fold V V,W ::= \lambda x.N \mid c \mid\operatorname{tuple}(V_1,\ldots,V_n) \mid\operatorname{inl} V \mid\operatorname{inr} V \mid\operatorname{fold} V V , W ::= λ x . N ∣ c ∣ tuple ( V 1 , … , V n ) ∣ inl V ∣ inr V ∣ fold V
The Agda development defines the reduction semantics as a function (instead of a relation). The function r e d red r e d maps a closed term M M M to a result, ranged over by r e s res r es , which says whether M M M is a value (val), reduces to a term M ′ M' M ′ (to M ′ M' M ′ ), or is stuck (error). The stuck case is there only to make r e d red r e d total. A closed well-typed term does not get stuck, because FPC is type safe.
r e s : : = val ∣ to M ′ ∣ error res ::= \operatorname{val} \mid\operatorname{to}\ M' \mid\operatorname{error} r es ::= val ∣ to M ′ ∣ error
The definition of r e d red r e d below is the core of the reduction semantics. The function r e d red r e d is deterministic and total by construction. In each case, the subterms are reduced left to right. A subterm that reduces makes the whole term reduce in the same position, and once the subterms are values the term takes its own step.
Reduction of a closed term
r e d ( M ) = r e s (3) red(M)=res \tag*{(3)} r e d ( M ) = r es (3)
red ( V ) = val red ( L M ) = to ( L ′ M ) if red ( L ) = to L ′ = to ( V M ′ ) if L = V , red ( M ) = to M ′ = to ( N [ W / x ] ) if L = λ x . N , M = W red ( tuple ( V ⃗ , M , M ⃗ ) ) = to ( tuple ( V ⃗ , M ′ , M ⃗ ) ) if red ( M ) = to M ′ red ( get i M ) = to ( get i M ′ ) if red ( M ) = to M ′ = to V i if M = tuple ( V 1 , … , V n ) red ( inl M ) = to ( inl M ′ ) if red ( M ) = to M ′ (likewise inr and fold) red ( case L ( x . M ) ( y . N ) ) = to ( case L ′ ( x . M ) ( y . N ) ) if red ( L ) = to L ′ = to ( M [ V / x ] ) if L = inl V = to ( N [ V / y ] ) if L = inr V red ( prim p L M ) = to ( prim p L ′ M ) if red ( L ) = to L ′ = to ( prim p V M ′ ) if L = V , red ( M ) = to M ′ = to δ p ( n , m ) if L = n , M = m red ( let x = M in N ) = to ( let x = M ′ in N ) if red ( M ) = to M ′ = to ( N [ V / x ] ) if M = V red ( unfold M ) = to ( unfold M ′ ) if red ( M ) = to M ′ = to V if M = fold V red ( M ) = error otherwise \begin{aligned}
\operatorname{red}(V) &= \operatorname{val}\\
\operatorname{red}(L\ M) &= \operatorname{to}(L'\ M) && \text{if }\operatorname{red}(L)=\operatorname{to}\ L'\\
&= \operatorname{to}(V\ M') && \text{if }L=V,\ \operatorname{red}(M)=\operatorname{to}\ M'\\
&= \operatorname{to}(N[W/x]) && \text{if }L=\lambda x.N,\ M=W\\
\operatorname{red}(\operatorname{tuple}(\vec{V},M,\vec{M})) &= \operatorname{to}(\operatorname{tuple}(\vec{V},M',\vec{M})) && \text{if }\operatorname{red}(M)=\operatorname{to}\ M'\\
\operatorname{red}(\operatorname{get}_{i}\ M) &= \operatorname{to}(\operatorname{get}_{i}\ M') && \text{if }\operatorname{red}(M)=\operatorname{to}\ M'\\
&= \operatorname{to}V_i && \text{if }M=\operatorname{tuple}(V_1,\ldots,V_n)\\
\operatorname{red}(\operatorname{inl}\ M) &= \operatorname{to}(\operatorname{inl}\ M') && \text{if }\operatorname{red}(M)=\operatorname{to}\ M'\\
&&&\text{(likewise inr and fold)}\\
\operatorname{red}(\operatorname{case}\ L\ (x.M)\ (y.N)) &= \operatorname{to}(\operatorname{case}\ L'\ (x.M)\ (y.N)) && \text{if }\operatorname{red}(L)=\operatorname{to}\ L'\\
&= \operatorname{to}(M[V/x]) && \text{if }L=\operatorname{inl}\ V\\
&= \operatorname{to}(N[V/y]) && \text{if }L=\operatorname{inr}\ V\\
\operatorname{red}(\operatorname{prim}_{p}\ L\ M) &= \operatorname{to}(\operatorname{prim}_{p}\ L'\ M) && \text{if }\operatorname{red}(L)=\operatorname{to}\ L'\\
&= \operatorname{to}(\operatorname{prim}_{p}\ V\ M') && \text{if }L=V,\ \operatorname{red}(M)=\operatorname{to}\ M'\\
&= \operatorname{to}\delta_p(n,m) && \text{if }L=n,\ M=m\\
\operatorname{red}(\operatorname{let}\ x=M\ \operatorname{in}\ N) &= \operatorname{to}(\operatorname{let}\ x=M'\ \operatorname{in}\ N) && \text{if }\operatorname{red}(M)=\operatorname{to}\ M'\\
&= \operatorname{to}(N[V/x]) && \text{if }M=V\\
\operatorname{red}(\operatorname{unfold}\ M) &= \operatorname{to}(\operatorname{unfold}\ M') && \text{if }\operatorname{red}(M)=\operatorname{to}\ M'\\
&= \operatorname{to}V && \text{if }M=\operatorname{fold}\ V\\
\operatorname{red}(M) &= \operatorname{error} && \text{otherwise}
\end{aligned} red ( V ) red ( L M ) red ( tuple ( V , M , M )) red ( get i M ) red ( inl M ) red ( case L ( x . M ) ( y . N )) red ( prim p L M ) red ( let x = M in N ) red ( unfold M ) red ( M ) = val = to ( L ′ M ) = to ( V M ′ ) = to ( N [ W / x ]) = to ( tuple ( V , M ′ , M )) = to ( get i M ′ ) = to V i = to ( inl M ′ ) = to ( case L ′ ( x . M ) ( y . N )) = to ( M [ V / x ]) = to ( N [ V / y ]) = to ( prim p L ′ M ) = to ( prim p V M ′ ) = to δ p ( n , m ) = to ( let x = M ′ in N ) = to ( N [ V / x ]) = to ( unfold M ′ ) = to V = error if red ( L ) = to L ′ if L = V , red ( M ) = to M ′ if L = λ x . N , M = W if red ( M ) = to M ′ if red ( M ) = to M ′ if M = tuple ( V 1 , … , V n ) if red ( M ) = to M ′ (likewise inr and fold) if red ( L ) = to L ′ if L = inl V if L = inr V if red ( L ) = to L ′ if L = V , red ( M ) = to M ′ if L = n , M = m if red ( M ) = to M ′ if M = V if red ( M ) = to M ′ if M = fold V otherwise
Here δ p ( n , m ) \delta_p(n,m) δ p ( n , m ) is the result of a primitive operator, which is a value, read as a term. The observation obs ( V ) \operatorname{obs}(V) obs ( V ) of a value is as in Appendix § 15 .§ 1 , with f u n fun f u n for λ x . N \lambda x.N λ x . N .
In the Agda development, the cases that reduce a subterm are written with one combinator, step\mathchar`-in(f,res,k) . If the subterm reduces, by r e s res r es , then so does the whole term, in the evaluation context f f f ; if the subterm is a value, the reduction continues with k k k ; and if the subterm is stuck, so is the whole term. The equations above are the Agda definition with the combinator unfolded.
With the function red \operatorname{red} red defined, we define single-step reduction and final observation as follows. A closed program M M M runs by these steps, so it terminates and diverges in the sense of Appendix § 15 .§ 1 .
Steps and final observation of the reduction semantics
M ⟶ M ′ if red ( M ) = to M ′ M ⟶ stop ( obs ( M ) ) if red ( M ) = val M ⟶ stop ( error ) if red ( M ) = error \begin{aligned}
M &\longrightarrow M' && \text{if }\operatorname{red}(M)=\operatorname{to}\ M'\\
M &\longrightarrow\operatorname{stop}(\operatorname{obs}(M)) && \text{if }\operatorname{red}(M)=\operatorname{val}\\
M &\longrightarrow\operatorname{stop}(\operatorname{error}) && \text{if }\operatorname{red}(M)=\operatorname{error}
\end{aligned} M M M ⟶ M ′ ⟶ stop ( obs ( M )) ⟶ stop ( error ) if red ( M ) = to M ′ if red ( M ) = val if red ( M ) = error
The languages Imp, CFG, and Instr μ \operatorname{Instr}_{\mu} Instr μ are first order, so their abstract machines of Appendix § 15 .§ 3 serve as their reduction semantics.
Proposition F.3 (Correctness, reduction semantics). Let M M M be a closed term of FPC of type Int. For every integer n n n , the reduction semantics of FPC on M M M terminates with n n n if and only if the machine of Instr μ \operatorname{Instr}_{\mu} Instr μ on compile ( M ) \operatorname{compile}(M) compile ( M ) does, and one diverges if and only if the other does. More generally, for every closed term M M M , the two are equivalent.
Scott Domains
Agda: Framework/Scott/Lift.agda, Framework/Scott/Domain.agda, Framework/Scott/Ops.agda, FPC/Semantics/ScottDomains/FPC.agda, FPC/Semantics/ScottDomains/X86.agda
Our Agda development builds the domain of values with the constructive domain theory of TypeTopology [20 ] , in which domains are directed-complete partial orders, as the bilimit of Appendix G. The domain of values is the solution of
D ≅ L ( ∑ t ∈ T a g P a y l o a d t ( D ) ) \mathbb{D} \cong\mathcal{L}\left(\sum_{t \in Tag} Payload_t(\mathbb{D})\right) D ≅ L t ∈ T a g ∑ P a y l o a d t ( D )
where L \mathcal{L} L is lifting, the standard construction that adds a least element ⊥ \bot ⊥ below a domain [25 , 88 ] . The tag t t t of a value distinguishes constants, tuples of each arity, injections, functions, and pairs, and the payload holds its contents:
t : : = c ∣ t u p n ∣ l e f t ∣ r i g h t ∣ f u n ∣ p r P a y l o a d c ( D ) = 1 P a y l o a d f u n ( D ) = [ D → D ] P a y l o a d t u p n ( D ) = D n P a y l o a d p r ( D ) = D 2 P a y l o a d l e f t ( D ) = D P a y l o a d r i g h t ( D ) = D \begin{aligned}
t &::= c \mid tup_n \mid left \mid right \mid fun \mid pr \\
Payload_c(\mathbb{D}) &= 1 \qquad Payload_{fun}(\mathbb{D}) = [\mathbb{D} \to\mathbb{D}] \\
Payload_{tup_n}(\mathbb{D}) &= \mathbb{D}^{n} \qquad Payload_{pr}(\mathbb{D}) = \mathbb{D}^{2} \\
Payload_{left}(\mathbb{D}) &= \mathbb{D} \qquad Payload_{right}(\mathbb{D}) = \mathbb{D}
\end{aligned} t P a y l o a d c ( D ) P a y l o a d t u p n ( D ) P a y l o a d l e f t ( D ) ::= c ∣ t u p n ∣ l e f t ∣ r i g h t ∣ f u n ∣ p r = 1 P a y l o a d f u n ( D ) = [ D → D ] = D n P a y l o a d p r ( D ) = D 2 = D P a y l o a d r i g h t ( D ) = D
where 1 1 1 is the one-point domain and [ D → D ] [\mathbb{D} \to\mathbb{D}] [ D → D ] the domain of continuous functions. The least element ⊥ \bot ⊥ of D \mathbb{D} D means divergence; a value d d d is defined when d ≠ ⊥ d \ne\bot d = ⊥ , that is, when d = ⌊ ( t , x ) ⌋ d = \lfloor(t,x)\rfloor d = ⌊( t , x )⌋ for a tag t t t and a payload x x x in P a y l o a d t ( D ) Payload_t(\mathbb{D}) P a y l o a d t ( D ) , where ⌊ ⋅ ⌋ \lfloor\cdot\rfloor ⌊ ⋅ ⌋ is the injection of lifting. The empty tuple is ⟨ ⟩ = ⌊ ( t u p 0 , ( ) ) ⌋ \langle\rangle= \lfloor(tup_0,())\rfloor ⟨ ⟩ = ⌊( t u p 0 , ( ))⌋ , where ( ) () ( ) is the element of 1 1 1 and of D \mathbb{D} D . As in the graph model, the operators are strict, and each of them is also continuous. We define them as follows, where g ′ g' g ′ is the strict function that agrees with g g g on defined values and maps ⊥ \bot ⊥ to ⊥ \bot ⊥ .
Λ ( g ) = ⌊ ( f u n , g ′ ) ⌋ a p p ( d , e ) = h ( e ) if d = ⌊ ( f u n , h ) ⌋ and e is defined T ( d 1 , … , d n ) = ⌊ ( t u p n , ( d 1 , … , d n ) ) ⌋ if every d i is defined π t u p i ( d ) = d i if d = ⌊ ( t u p n , ( d 1 , … , d n ) ) ⌋ p a i r ( d , e ) = ⌊ ( p r , ( d , e ) ) ⌋ if d and e are defined c a r ( d ) = d 1 if d = ⌊ ( p r , ( d 1 , d 2 ) ) ⌋ c d r ( d ) = d 2 if d = ⌊ ( p r , ( d 1 , d 2 ) ) ⌋ i n l ( d ) = ⌊ ( l e f t , d ) ⌋ if d is defined i n r ( d ) = ⌊ ( r i g h t , d ) ⌋ if d is defined c a s e s ( d , f , g ) = f ( e ) if d = ⌊ ( l e f t , e ) ⌋ = g ( e ) if d = ⌊ ( r i g h t , e ) ⌋ p r i m p ( d , e ) = o u t ( δ ( p , n , m ) ) if d = ⌊ ( n , ( ) ⌋ and e = ⌊ ( m , ( ) ⌋ for integers n , m s e q ( d , e ) = e if d is defined \begin{aligned}
\Lambda(g) &= \lfloor(fun,g')\rfloor\\
app(d,e) &= h(e) && \text{if } d=\lfloor(fun,h)\rfloor\text{ and } e \text{ is defined} \\
\mathcal{T}(d_1,\ldots,d_n) &= \lfloor(tup_n,(d_1,\ldots,d_n))\rfloor&& \text{if every } d_i \text{ is defined} \\
\pi_{tup_i}(d) &= d_i && \text{if } d=\lfloor(tup_n,(d_1,\ldots,d_n))\rfloor\\
pair(d,e) &= \lfloor(pr,(d,e))\rfloor&& \text{if } d \text{ and } e \text{ are defined} \\
car(d) &= d_1 && \text{if } d=\lfloor(pr,(d_1,d_2))\rfloor\\
cdr(d) &= d_2 && \text{if } d=\lfloor(pr,(d_1,d_2))\rfloor\\
inl(d) &= \lfloor(left,d)\rfloor&& \text{if } d \text{ is defined} \\
inr(d) &= \lfloor(right,d)\rfloor&& \text{if } d \text{ is defined} \\
cases(d,f,g) &= f(e) && \text{if } d=\lfloor(left,e)\rfloor\\
&= g(e) && \text{if } d=\lfloor(right,e)\rfloor\\
prim_p(d,e) &= out(\delta(p,n,m)) && \text{if } d=\lfloor(n,()\rfloor\text{ and } e=\lfloor(m,()\rfloor\text{ for integers } n,m \\
seq(d,e) &= e && \text{if } d \text{ is defined}
\end{aligned} Λ ( g ) a pp ( d , e ) T ( d 1 , … , d n ) π t u p i ( d ) p ai r ( d , e ) c a r ( d ) c d r ( d ) in l ( d ) in r ( d ) c a ses ( d , f , g ) p r i m p ( d , e ) se q ( d , e ) = ⌊( f u n , g ′ )⌋ = h ( e ) = ⌊( t u p n , ( d 1 , … , d n ))⌋ = d i = ⌊( p r , ( d , e ))⌋ = d 1 = d 2 = ⌊( l e f t , d )⌋ = ⌊( r i g h t , d )⌋ = f ( e ) = g ( e ) = o u t ( δ ( p , n , m )) = e if d = ⌊( f u n , h )⌋ and e is defined if every d i is defined if d = ⌊( t u p n , ( d 1 , … , d n ))⌋ if d and e are defined if d = ⌊( p r , ( d 1 , d 2 ))⌋ if d = ⌊( p r , ( d 1 , d 2 ))⌋ if d is defined if d is defined if d = ⌊( l e f t , e )⌋ if d = ⌊( r i g h t , e )⌋ if d = ⌊( n , ( )⌋ and e = ⌊( m , ( )⌋ for integers n , m if d is defined
In every case that the equations do not cover, the result is ⊥ \bot ⊥ . The function o u t out o u t turns the result of a primitive into a value, with booleans as injections, as in the graph model (Figure 3 ): o u t ( k ) = ⌊ ( k , ( ) ) ⌋ out(k)=\lfloor(k,())\rfloor o u t ( k ) = ⌊( k , ( ))⌋ for an integer k k k , o u t ( t r u e ) = ⌊ ( l e f t , ⟨ ⟩ ) ⌋ out(true)=\lfloor(left,\langle\rangle)\rfloor o u t ( t r u e ) = ⌊( l e f t , ⟨ ⟩)⌋ , and o u t ( f a l s e ) = ⌊ ( r i g h t , ⟨ ⟩ ) ⌋ out(false)=\lfloor(right,\langle\rangle)\rfloor o u t ( f a l se ) = ⌊( r i g h t , ⟨ ⟩)⌋ . An environment maps variables to values, and the environment ρ 0 \rho_0 ρ 0 of a closed program maps every variable to the empty tuple ⟨ ⟩ \langle\rangle ⟨ ⟩ .
Semantics of FPC. The semantics of FPC has the equations of the graph-model semantics (Appendix § 11 ), with the operators of D \mathbb{D} D :
\begin{aligned}
\llbracket x\rrbracket_\rho&= \rho(x) &
\llbracket c\rrbracket_\rho&= \lfloor(c, ())\rfloor\\
\llbracket\lambda x.N\rrbracket_\rho&= \Lambda(\lambda d.\llbrbracket N\rrbracket_{\rho[x:=d]}) &
\llbracket L\ M\rrbracket_\rho&= \operatorname{app}(\llbracket L\rrbracket_\rho,\llbracket M\rrbracket_\rho) \\
\llbracket\operatorname{tuple}(M_1,\ldots,M_n)\rrbracket_\rho&= \mathcal{T}(\llbracket M_1\rrbracket_\rho,\ldots,\llbracket M_n\rrbracket_\rho) &
\llbracket\operatorname{get}_i\ M\rrbracket_\rho&= \pi_{\operatorname{tup}_i}(\llbracket M\rrbracket_\rho) \\
\llbracket\operatorname{inl}\ M\rrbracket_\rho&= \operatorname{inl}(\llbracket M\rrbracket_\rho) &
\llbracket\operatorname{inr}\ M\rrbracket_\rho&= \operatorname{inr}(\llbracket M\rrbracket_\rho) \\
\llbracket\operatorname{prim}_p\ L\ M\rrbracket_\rho&= \operatorname{prim}_p(\llbracket L\rrbracket_\rho,\llbracket M\rrbracket_\rho) &
\llbracket\operatorname{fold}\ M\rrbracket_\rho&= \llbracket M\rrbracket_\rho\\
\llbracket\operatorname{let}\ x=M\ \operatorname{in}\ N\rrbracket_\rho&= \operatorname{app}(\Lambda(\lambda d.\llbracket N\rrbracket_{\rho[x:=d]}),\llbracket M\rrbracket_\rho) &
\llbracket\operatorname{unfold}\ M\rrbracket_\rho&= \llbracket M\rrbracket_\rho\\
\llbracket\operatorname{case}\ L\ (x.M)\ (y.N)\rrbracket_\rho&= \operatorname{cases}(\llbracket L\rrbracket_\rho,(\lambda d.\llbracket M\rrbracket_{\rho[x:=d]})',(\lambda d.\llbracket N\rrbracket_{\rho[y:=d]})')
\end{aligned}
where g ′ g' g ′ is the strict function that agrees with g g g on defined values, as in Λ \Lambda Λ .
Semantics of Instr μ \operatorname{Instr}_{\mu} Instr μ . The semantics of Instr μ \operatorname{Instr}_{\mu} Instr μ has the equations of the graph-model semantics of Instr μ \operatorname{Instr}_{\mu} Instr μ (Appendix § 11 ), with the operators of D \mathbb{D} D in place of those of the graph model, seq \operatorname{seq} seq in place of guard \operatorname{guard} guard in the meaning of call q \operatorname{call}q call q , and the empty tuple ⟨ ⟩ \langle\rangle ⟨ ⟩ in place of { ⟨ ⟩ } \{\langle\rangle\} {⟨ ⟩} as the content of an unset register and an unset flag. A sequence means a continuous function of the frame, the registers, and the flag.
Theorem 4.2 (§ 4 ) states the correctness of the compiler with respect to these semantics.
The Scott-Domain Proof of split closure
Agda: FPC/proof/Correctness/ScottDomains/SplitRel.agda, SplitPreserve.agda, SplitReflect.agda, SplitTerms.agda, SplitCorrect.agda; proof/Framework/Scott/Stages.agda, StageCall.agda
As in the graph model (§ 4.2 ), split closure changes the representation of closures, so the Scott-domain proof relates the meaning of a program to the meaning of its translation by a logical relation, one for each direction. The universal domain D \mathbb{D} D (Appendix § 15 .§ 5 ) rules out the two usual ways of defining such a relation. Recursion on types does not terminate, because the types include recursive types, and recursion on values is not well founded, because values are infinite. We instead define the relations by recursion on the stages of the bilimit that constructs D \mathbb{D} D , as Reynolds [ [71 ] did. Each relation relates an approximation of one value at a stage to the whole other value, so, like the approximant logical relation, it approximates only one side. The preservation relation approximates the source, and the reflection relation approximates the target.
Stages. The stages of D \mathbb{D} D are the domains
D 0 = 1 D n + 1 = L ( ∑ t ∈ Tag Payload t ( D n ) ) D_0=1 \qquad D_{n+1}=\mathcal{L}\left(\sum_{t\in\operatorname{Tag}}\operatorname{Payload}_t(D_n)\right) D 0 = 1 D n + 1 = L t ∈ Tag ∑ Payload t ( D n )
with the maps π n : D n + 1 → D n \pi_n:D_{n+1}\to D_n π n : D n + 1 → D n and ε n : D n → D n + 1 \varepsilon_n:D_n\to D_{n+1} ε n : D n → D n + 1 between consecutive stages, and the maps π n ∞ : D → D n \pi_n^\infty:\mathbb{D}\to D_n π n ∞ : D → D n and ε n ∞ : D n → D \varepsilon_n^\infty:D_n\to\mathbb{D} ε n ∞ : D n → D between a stage and the limit. Every value d d d is the supremum of its approximations ε n ∞ ( π n ∞ ( d ) ) \varepsilon_n^\infty(\pi_n^\infty(d)) ε n ∞ ( π n ∞ ( d )) . At stage n + 1 n+1 n + 1 , the payload of a function is in [ D n → D n ] [D_n\to D_n] [ D n → D n ] and the payload of a pair is in D n 2 D_n^2 D n 2 .
A target closure is a pair ( c , e ) (c,e) ( c , e ) of a curried function and an environment, and the meaning of a call calls it on an argument a a a by call ( c , e , a ) = app ( app ( c , e ) , a ) \operatorname{call}(c,e,a)=\operatorname{app}(\operatorname{app}(c,e),a) call ( c , e , a ) = app ( app ( c , e ) , a ) . The call applies the curried function twice, and applying a function of stage k + 1 k+1 k + 1 gives a result of stage k k k . A call of a pair of stage m + 2 m+2 m + 2 therefore gives its result at stage m m m :
call m ( ( c , e ) , v ) = π m ∞ ( call ( ε m + 2 ∞ ( c ) , ε m + 1 ( π m + 1 ( e ) ) , ε m ∞ ( v ) ) ) \operatorname{call}_m((c,e),v)=\pi_m^\infty\left(\operatorname{call}\left(\varepsilon_{m+2}^\infty(c),\varepsilon_{m+1}(\pi_{m+1}(e)),\varepsilon_m^\infty(v)\right)\right) call m (( c , e ) , v ) = π m ∞ ( call ( ε m + 2 ∞ ( c ) , ε m + 1 ( π m + 1 ( e )) , ε m ∞ ( v ) ) )
for c , e ∈ D m + 2 c,e\in D_{m+2} c , e ∈ D m + 2 and v ∈ D m v\in D_m v ∈ D m .
t ≃ ± t ′ for t ∉ { f u n , p r } t \mathrel{\simeq^{\pm}} t' \quad\text{for } t \notin\{\mathrm{fun},\mathrm{pr}\} t ≃ ± t ′ for t ∈ / { fun , pr }
f u n ≃ + p r p r ≃ − f u n \mathrm{fun} \mathrel{\simeq^{+}} \mathrm{pr} \qquad\mathrm{pr} \mathrel{\simeq^{-}} \mathrm{fun} fun ≃ + pr pr ≃ − fun
The stage relations
s R n ± w ( s ∈ D n , w ∈ D ) s \mathrel{R^{\pm}_{n}} w \qquad(s \in D_n,\ w \in D) s R n ± w ( s ∈ D n , w ∈ D )
s R 0 ± w ⟺ t r u e s \mathrel{R^{\pm}_{0}} w \Longleftrightarrow\mathrm{true} s R 0 ± w ⟺ true
s R n + 1 ± w ⟺ ∀ t , x . s = ⌊ ( t , x ) ⌋ ⇒ ∃ t ′ , y . w = ⌊ ( t ′ , y ) ⌋ ∧ t ≃ ± t ′ ∧ ( t , x ) M n ± ( t ′ , y ) s \mathrel{R^{\pm}_{n+1}} w \Longleftrightarrow\forall t,x.\ s=\lfloor(t,x)\rfloor\Rightarrow\exists t',y.\ w=\lfloor(t',y)\rfloor\land t \mathrel{\simeq^{\pm}} t' \land(t,x) \mathrel{M^{\pm}_{n}} (t',y) s R n + 1 ± w ⟺ ∀ t , x . s = ⌊( t , x )⌋ ⇒ ∃ t ′ , y . w = ⌊( t ′ , y )⌋ ∧ t ≃ ± t ′ ∧ ( t , x ) M n ± ( t ′ , y )
( t , x ) M n ± ( t ′ , y ) ( x ∈ Payload t ( D n ) , y ∈ Payload t ′ ( D ) ) (t,x) \mathrel{M^{\pm}_{n}} (t',y) \qquad(x \in\operatorname{Payload}_{t}(D_n),\ y \in\operatorname{Payload}_{t'}(D)) ( t , x ) M n ± ( t ′ , y ) ( x ∈ Payload t ( D n ) , y ∈ Payload t ′ ( D ))
( c , ( ) ) M n ± ( c , ( ) ) ⟺ t r u e (c,()) \mathrel{M^{\pm}_{n}} (c,()) \Longleftrightarrow\mathrm{true} ( c , ( )) M n ± ( c , ( )) ⟺ true
( tup k , ( x 1 , … , x k ) ) M n ± ( tup k , ( y 1 , … , y k ) ) ⟺ ∀ i . x i R n ± y i (\operatorname{tup}_{k},(x_1,\ldots,x_k)) \mathrel{M^{\pm}_{n}} (\operatorname{tup}_{k},(y_1,\ldots,y_k)) \Longleftrightarrow\forall i.\ x_i \mathrel{R^{\pm}_{n}} y_i ( tup k , ( x 1 , … , x k )) M n ± ( tup k , ( y 1 , … , y k )) ⟺ ∀ i . x i R n ± y i
( left , x ) M n ± ( left , y ) ⟺ x R n ± y (likewise right) (\operatorname{left},x) \mathrel{M^{\pm}_{n}} (\operatorname{left},y) \Longleftrightarrow x \mathrel{R^{\pm}_{n}} y \qquad\text{(likewise right)} ( left , x ) M n ± ( left , y ) ⟺ x R n ± y (likewise right)
( f u n , h ) M n + ( p r , ( c , e ) ) ⟺ ∀ v ∈ D n , a ∈ D . v R n + a ⇒ h ( v ) R n + call ( c , e , a ) (\mathrm{fun},h) \mathrel{M^{+}_{n}} (\mathrm{pr},(c,e)) \Longleftrightarrow\forall v \in D_n,\ a \in D.\ v \mathrel{R^{+}_{n}} a \Rightarrow h(v) \mathrel{R^{+}_{n}} \operatorname{call}(c,e,a) ( fun , h ) M n + ( pr , ( c , e )) ⟺ ∀ v ∈ D n , a ∈ D . v R n + a ⇒ h ( v ) R n + call ( c , e , a )
( p r , ( c , e ) ) M m + 2 − ( f u n , g ) ⟺ ∀ v ∈ D m , a ∈ D . v R m − a ⇒ call m ( ( c , e ) , v ) R m − g ′ ( a ) (\mathrm{pr},(c,e)) \mathrel{M^{-}_{m+2}} (\mathrm{fun},g) \Longleftrightarrow\forall v \in D_m,\ a \in D.\ v \mathrel{R^{-}_{m}} a \Rightarrow\operatorname{call}_{m}((c,e),v) \mathrel{R^{-}_{m}} g'(a) ( pr , ( c , e )) M m + 2 − ( fun , g ) ⟺ ∀ v ∈ D m , a ∈ D . v R m − a ⇒ call m (( c , e ) , v ) R m − g ′ ( a )
( p r , ( c , e ) ) M n − ( f u n , g ) ⟺ t r u e for n < 2 (\mathrm{pr},(c,e)) \mathrel{M^{-}_{n}} (\mathrm{fun},g) \Longleftrightarrow\mathrm{true} \qquad\text{for } n<2 ( pr , ( c , e )) M n − ( fun , g ) ⟺ true for n < 2
d ⊑ ± w Pres ( d , d ′ ) Reflect ( d , d ′ ) d \mathrel{\sqsubseteq^{\pm}} w \qquad\operatorname{Pres}(d,d') \qquad\operatorname{Reflect}(d,d') d ⊑ ± w Pres ( d , d ′ ) Reflect ( d , d ′ )
d ⊑ ± w ⟺ ∀ n . π n ∞ ( d ) R n ± w d \mathrel{\sqsubseteq^{\pm}} w \Longleftrightarrow\forall n.\ \pi^{\infty}_{n}(d) \mathrel{R^{\pm}_{n}} w d ⊑ ± w ⟺ ∀ n . π n ∞ ( d ) R n ± w
Pres ( d , d ′ ) ⟺ d ⊑ + d ′ \operatorname{Pres}(d,d') \Longleftrightarrow d \mathrel{\sqsubseteq^{+}} d' Pres ( d , d ′ ) ⟺ d ⊑ + d ′
Reflect ( d , d ′ ) ⟺ d ′ ⊑ − d \operatorname{Reflect}(d,d') \Longleftrightarrow d' \mathrel{\sqsubseteq^{-}} d Reflect ( d , d ′ ) ⟺ d ′ ⊑ − d
The relations. Figure 7 defines the relations. At stage n + 1 n+1 n + 1 , a defined approximated value s s s is related to w w w when w w w is defined too, the two have related tags, and their payloads are related at stage n n n ; an undefined approximated value is related to every value. Related values have the same tag, except for closures. Under preservation, a source function is related to a target pair when the function and calls of the pair, on related arguments, give related results; this clause has the shape of the relation of Nielsen [ [57 ] for defunctionalization. Under reflection, the approximated value is the target pair, and its calls show their results two stages down. The clause for a pair at stage m + 2 m+2 m + 2 therefore speaks of stage m m m , and it says nothing at stages 0 and 1. A source value d d d and a target value d ′ d' d ′ are related for preservation when π n ∞ ( d ) R n + d ′ \pi^{\infty}_{n}(d) \mathrel{R^{+}_{n}} d' π n ∞ ( d ) R n + d ′ at every stage n n n , and for reflection when π n ∞ ( d ′ ) R n − d \pi^{\infty}_{n}(d') \mathrel{R^{-}_{n}} d π n ∞ ( d ′ ) R n − d at every stage n n n .
Properties of the stages. The proof needs four properties of each stage relation, which hold by induction on the stage.
Lemma G.1 (Stages). For both directions and every n n n :
s R n ± w s \mathrel{R^{\pm}_{n}} w s R n ± w is a proposition;
it is downward closed in s s s , that is, if s ′ ⊑ s s' \sqsubseteq s s ′ ⊑ s and s R n ± w s \mathrel{R^{\pm}_{n}} w s R n ± w , then s ′ R n ± w s' \mathrel{R^{\pm}_{n}} w s ′ R n ± w ;
it is admissible in s s s , that is, if s i R n ± w s_i \mathrel{R^{\pm}_{n}} w s i R n ± w for every i i i of a directed family, then ⨆ i s i R n ± w \bigsqcup_i s_i \mathrel{R^{\pm}_{n}} w ⨆ i s i R n ± w ;
it survives moving between stages, that is, if s R n + 1 ± w s \mathrel{R^{\pm}_{n+1}} w s R n + 1 ± w , then π n ( s ) R n ± w \pi_n(s) \mathrel{R^{\pm}_{n}} w π n ( s ) R n ± w , and if s R n ± w s \mathrel{R^{\pm}_{n}} w s R n ± w , then ε n ( s ) R n + 1 ± w \varepsilon_n(s) \mathrel{R^{\pm}_{n+1}} w ε n ( s ) R n + 1 ± w .
Consequently, if s R n ± w s \mathrel{R^{\pm}_{n}} w s R n ± w , then ε n ∞ ( s ) ⊑ ± w \varepsilon^{\infty}_{n}(s) \mathrel{\sqsubseteq^{\pm}} w ε n ∞ ( s ) ⊑ ± w .
The closure clauses make parts 3 and 4 the hard ones. For part 3 under reflection, call m \operatorname{call}_m call m is continuous in the pair, so the call of a supremum of pairs is the supremum of their calls. For part 4, call m \operatorname{call}_m call m commutes with π \pi π and ε \varepsilon ε up to the order of D m D_m D m , and part 2 absorbs the difference.
The fundamental lemma. Write ∼ \sim ∼ for either Pres or Reflect . Both relate ⊥ \bot ⊥ to every value in the approximated position, and the operators that EnvClos and PairClos share preserve both.
Lemma G.2 (Operators). If d i ∼ d i ′ d_i \sim d_i' d i ∼ d i ′ for each i i i , then T ( d 1 , … , d n ) ∼ T ( d 1 ′ , … , d n ′ ) \mathcal{T}(d_1,\ldots,d_n) \sim\mathcal{T}(d_1',\ldots,d_n') T ( d 1 , … , d n ) ∼ T ( d 1 ′ , … , d n ′ ) , and likewise for π t u p i \pi_{\mathrm{tup}_i} π tup i , inl, inr, prim p \operatorname{prim}_p prim p , and seq; constants and ⟨ ⟩ \langle\rangle ⟨ ⟩ are related to themselves. If the branches of cases map related values to related values, then cases preserves ∼ \sim ∼ .
The two languages differ only in closures and calls.
Lemma G.3 (Closures and calls). Let t ∼ t ′ t \sim t' t ∼ t ′ , and let the bodies B B B and B ′ B' B ′ map related environments and arguments to related values. Then
app ( Λ ( λ X . Λ ( λ Y . B ( X , Y ) ) ) , t ) ∼ pair ( Λ ( λ X . Λ ( λ Y . B ′ ( X , Y ) ) ) , t ′ ) . \operatorname{app}(\Lambda(\lambda X.\Lambda(\lambda Y.B(X,Y))),t) \sim\operatorname{pair}(\Lambda(\lambda X.\Lambda(\lambda Y.B'(X,Y))),t'). app ( Λ ( λ X .Λ ( λY . B ( X , Y ))) , t ) ∼ pair ( Λ ( λ X .Λ ( λY . B ′ ( X , Y ))) , t ′ ) . If l ∼ l ′ l \sim l' l ∼ l ′ and a ∼ a ′ a \sim a' a ∼ a ′ , then
app ( l , a ) ∼ app ( app ( car ( l ′ ) , cdr ( l ′ ) ) , a ′ ) . \operatorname{app}(l,a) \sim\operatorname{app}(\operatorname{app}(\operatorname{car}(l'),\operatorname{cdr}(l')),a'). app ( l , a ) ∼ app ( app ( car ( l ′ ) , cdr ( l ′ )) , a ′ ) .
Under preservation, both parts follow from the clause for functions and part 4 of Lemma G.1. Under reflection, the closure case uses the clause for pairs. A call of the approximation at stage m + 2 m+2 m + 2 of the target pair is below the approximation at stage m m m of the call of the pair itself, and part 2 of Lemma G.1 applies. In the call case, the approximation at stage n n n of the result of a call is the supremum of the calls of the approximations of the curried function, the environment and the argument, so part 3 reduces the call to the calls of approximations, and each of those is below an approximation of a call at a larger stage, of which the clause for pairs speaks.
Lemma G.4 (Fundamental lemma). For each direction, let M M M be a term of EnvClos and let ρ \rho ρ and ρ ′ \rho' ρ ′ be environments related at every variable of M M M . Then ⟦ M ⟧ ρ ∼ ⟦ split ( M ) ⟧ ρ ′ \llbracket M\rrbracket_\rho\sim\llbracket\operatorname{split}(M)\rrbracket_{\rho'} [ [ M ] ] ρ ∼ [ [ split ( M ) ] ] ρ ′ .
The proof is by induction on M M M , with the same induction for both directions. Lemma G.2 covers the shared operators, and Lemma G.3 covers the closures and calls.
Lemma G.5 (Correctness of split closure, Scott domains). Let M M M be a closed term of EnvClos. For every constant c c c , ⟦ M ⟧ ρ 0 = ⌊ ( c , ( ) ) ⌋ \llbracket M\rrbracket_{\rho_0} = \lfloor(c,())\rfloor [ [ M ] ] ρ 0 = ⌊( c , ( ))⌋ if and only if ⟦ split ( M ) ⟧ ρ 0 = ⌊ ( c , ( ) ) ⌋ \llbracket\operatorname{split}(M)\rrbracket_{\rho_0} = \lfloor(c,())\rfloor [ [ split ( M ) ] ] ρ 0 = ⌊( c , ( ))⌋ . Moreover, ⟦ M ⟧ ρ 0 \llbracket M\rrbracket_{\rho_0} [ [ M ] ] ρ 0 is defined if and only if ⟦ split ( M ) ⟧ ρ 0 \llbracket\operatorname{split}(M)\rrbracket_{\rho_0} [ [ split ( M ) ] ] ρ 0 is defined.
The empty environments are related, so Lemma G.4 gives that ⟦ M ⟧ ρ 0 \llbracket M\rrbracket_{\rho_0} [ [ M ] ] ρ 0 and ⟦ split ( M ) ⟧ ρ 0 \llbracket\operatorname{split}(M)\rrbracket_{\rho_0} [ [ split ( M ) ] ] ρ 0 are related by both Pres and Reflect . In either relation, a defined approximated value has a defined partner, and a defined value related to a constant is that constant. Preservation gives the forward implications and reflection the backward ones.