arXiv:2611.04993cs.PLNov 2026

Manuscript Offprint

arXiv:2611.04993

Linking as Evaluation: Correct Separate Compilation via First-Class Environments

Jam Kabeer Ali Khan, Jinhao Tan, Bruno C. d. S. Oliveira

Open access. PaperVantage converted this article to text, so layout and figures may differ from the original.

Correct Separate Compilation via First-Class Environments

JAM KABEER ALI KHAN, Independent, Hong Kong

JINHAO TAN, The University of Hong Kong, China

BRUNO C. D. S. OLIVEIRA, The University of Hong Kong, China

Separate compilation is central to modular software: components are compiled independently and linked into an executable. Separate compilation is correct when a linked program behaves like the program formed by merging its sources. This property is less routine to prove than type soundness because it requires a formal account of linking.

We present λSCE\lambda_{\mathrm{SCE}}, a simply typed calculus in which linking, modules, and environments are all first-class. Programmers control module linking through a link operator, which a type-directed elaboration translates into ordinary terms of a core calculus of first-class environments. The core calculus's existing evaluation rules then perform the linking: linking as evaluation. Linking independently compiled modules yields the same core term as elaborating their source link. Therefore, compilation and linking commute by construction. As a result, separate compilation correctness follows directly from the semantics preservation for both static and dynamic linking. All meta-theory is mechanized in Lean 4 and we implement a compiler to WebAssembly in OCaml.

Additional Key Words and Phrases: Separate Compilation, Linking, Semantics Preservation, First-Class Environments, Compiler Correctness, Lean 4 Mechanization

1 Introduction

Type soundness [52, 53] is a widely accepted sanity criterion for statically typed programming languages, supported by well-established proof techniques and mechanization frameworks. Modern software also relies on separate compilation: components are developed, typechecked, and compiled independently, then linked into larger systems [4, 26]. Separate compilation supports modularity, incremental builds, and third-party reuse. The correctness requirement is simple: linking independently compiled components should produce a program with the same behavior as linking their sources before compilation. However, establishing this correspondence is less routine than proving type soundness, even for simple types, because the proof requires a formal account of linking.

Cardelli [7] models separately compiled fragments as open terms and links them by substitution within linksets. He proves that linking preserves typing; SMLSC states the same property for Standard ML [48]. Both link outside the language, without proving that linked code behaves like the combined source program.

Other designs bring linking into the language. Units supports run-time composition [12], Alice ML expresses linking through functions [37], and MixML provides recursive module linking [39]. We know of no typed calculus with linking as an expression that proves that linking separately compiled components preserves source behavior. However, verified compilers have established separate compilation correctness, including SepCompCert [23] and CertiCoq [33]. For passes that commute with linking, SepCompCert derives this result from whole-program correctness. In both systems, however, linking remains outside the source language, and neither theorem covers dynamic linking.

We bring the two strands together: linking within the language, backed by a proof of separate compilation correctness that covers both, dynamic and static linking. Our approach is linking as evaluation: a source link elaborates into ordinary target terms so we can utilize the semantics of

Authors' Contact Information: Jam Kabeer Ali Khan, jamkhan@connect.hku.hk, Independent, Hong Kong; Jinhao Tan, jinhaot@connect.hku.hk, The University of Hong Kong, Hong Kong, China; Bruno C. d. S. Oliveira, bruno@cs.hku.hk, The University of Hong Kong, Hong Kong, China.

the target to perform the linking. Thus, evaluation provides a module's imports and produces a linked library.

We develop this approach in λSCE\lambda_{SCE}, a simply typed calculus with first-class modules, environments, and linking. First-class environments [50, 51] let programs construct the environment in which a module runs, and use a linked library or module as an environment for computation. λSCE\lambda_{SCE} elaborates into λE\lambda_E, a calculus of first-class environments [51]. A separately compiled module with imports becomes a closed function whose parameter supplies its import environment. Linking constructs this environment from the required exports of another module, applies the function, and combines their exports. Function application, field selection, and environment composition thus perform the linking using the target's existing evaluation rules. Sandboxing restricts a module to its declared import: it cannot access the scope where it is written and compiles to the same term wherever it appears. Linking supplies only the selected import, without exposing the scope at the link site. The client runs under its captured environment extended with that import.

Programmers control linking: a link is an ordinary expression. The same link can assemble compiled modules at top level, link against a module passed to a function at run time, or draw an import from the current environment. The link's elaboration uses only its operands' core terms and interfaces, so the core linker emits that same term from compiled fragments: compiling and linking commute by construction. Commutation is thus SepCompCert's easy case, holding by design and for dynamic links too.

We first prove that elaboration preserves types: well-typed source links compile to well-typed core terms. Beyond the typing guarantees studied by Cardelli, we define λSCE\lambda_{SCE}'s operational semantics and prove that elaboration preserves source semantics. That is, a source program and its elaboration evaluate to values related by elaboration or diverge together. Since the core linker produces the same term as elaborating the source link, semantics and behavior preservation establish separate compilation correctness directly from λSCE\lambda_{SCE}'s semantics. The result covers static and dynamic linking, both at top level and inside functions. The static case (Corollary 4.13) establishes, for λSCE\lambda_{SCE}, the semantic correctness property that Cardelli identifies but does not formalize. We also prove interface abstraction: a client's exports depend on its provider only through the imported values.

Our contributions are:

  • The λSCE\lambda_{SCE} calculus (§3). A calculus with first-class modules, linking, and environments. We prove its type safety.
  • Type-directed elaboration to λE\lambda_E (§4.2–4.3). We elaborate source programs into λE\lambda_E, translating first-class modules and linking into ordinary target terms. We prove type and semantics preservation.
  • Separate compilation correctness (§4.3). We derive separate compilation correctness from semantics and behavior preservation. The design of elaboration makes the proof simple. We also prove interface abstraction and confinement for sandboxed modules.
  • Extensions (§5.1). We extend the system with unions, iso-recursive types, fixpoints, and multi-import linking, showing that the approach still applies with these more expressive features.
  • Mechanization and implementation (§5.2).¹ We mechanize all results in Lean 4 [29] and implement SCE-Lang, a prototype compiler written in OCaml. It targets WebAssembly [15] with no linking instruction and supports staged linking in both the core and WebAssembly.

¹The implementation and mechanization are available at https://github.com/jam-khan/SCE-Lang

References (44)