arXiv:2610.36798cs.PLOct 2026Cited by 1 paper on PaperVantage

Manuscript Offprint

arXiv:2610.36798

Polymorphic First-Class Environments

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

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

JINHAO TAN, The University of Hong Kong, China

JAM KABEER ALI KHAN, The University of Hong Kong, China

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

First-class environments expose runtime environments as ordinary values and support a substitution-free, environment-based operational semantics. They also support applications such as first-class modules, dynamic composition of environment fragments, and sandboxing via local environments. Existing environment-based calculi, however, are mostly studied in a simply typed setting and stop short of System F and polymorphism.

We present FEF_{E}, a polymorphic calculus with first-class environments that conservatively extends System F with a small-step environment semantics. FEF_{E} eliminates all forms of substitution in System F. Term and type abstractions evaluate to closures, boxes run code under local environments, and box types expose the local typing assumptions of both. Manifest types, together with a two-sided type equivalence, play a dual role: they model type-level instantiation, and enable transparent type components in environments and modules. We prove type soundness, normalization, decidability and standard properties of type equivalence, and conservativity over System F. The metatheory of FEF_{E} is based on instantiation lemmas rather than substitution lemmas. This foundation supports polymorphic first-class modules with dynamic composition and sandboxing. We study FEF_{E} in two forms. Our main presentation is an explicit, type-passing calculus that follows conventional System F. We also develop an implicitly-typed variant aligned with the semantics of polymorphic functional languages. All metatheory is mechanized in the Rocq theorem prover.

1 Introduction

Runtime environments are central to programming language implementations. They record the bindings used to execute code, but programmers usually cannot manipulate them directly in the language. First-class environments [Gelernter et al. 1987; Miller and Rozas 1991; Nishizaki 1994, 2000; Queinnec and Roure 1996; Sato et al. 2001, 2002; Shutt 2010; Tan and Oliveira 2023] turn these environments into ordinary values that can be created, passed, inspected, and composed. Once environments become first-class, they can serve not only as a runtime device, but also as a programming construct for controlling the context in which code executes.

Recent work shows that first-class environments already support several useful forms of modularity. In λE\lambda_{E} [Tan and Oliveira 2024], environment fragments can act as first-class modules and can be composed dynamically through dependent merges [Tan and Oliveira 2023]. Boxes of the form e1▹e2e_{1} \triangleright e_{2} evaluate e2e_{2} under the local environment e1e_{1}, giving a direct account of sandboxing. Because the supplied environment determines which bindings and resources are available during execution, the same mechanism also supports capability-style modularity, making authority explicit [Melicher et al. 2017; Miller 2006; Miller et al. 2000, 2008].

First-class environments are also attractive semantically. Standard presentations of the lambda calculus and System F organize reduction and metatheory around capture-avoiding substitution, while implementations execute programs with closures and environments. This mismatch has motivated several lines of work that aim to eliminate or internalize meta-substitution, from explicit substitutions [Abadi et al. 1991; Curien et al. 1996] to explicit environments and environment-based semantics [Nishizaki 1994, 2000; Sato et al. 2001; Tan and Oliveira 2024]. In an environment-based account, bindings are carried explicitly at runtime rather than propagated by substitution. Such a semantics is closer to implementations and can also be attractive for mechanization.

Authors' Contact Information: Jinhao Tan, jhtan@cs.hku.hk, The University of Hong Kong, Hong Kong, China; Jam Kabeer Ali Khan, jamkhan@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.

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

For calculi with first-class environments, the simply typed case is now reasonably well understood, but the polymorphic case remains a key challenge. λE\lambda_{E}, for instance, is conservative over the Simply Typed Lambda Calculus and stops short of System F. Extending first-class environments from the simply typed setting to System F requires more than adding universal quantification. In a standard presentation of System F, substitution appears in three places: term beta-reduction, type beta-reduction, and type instantiation at type application. An environment-based semantics already gives a natural account of term substitution through closures, and the same operational idea can be used for type abstractions. But this polymorphic setting introduces further typing problems as well: closures and boxes must now carry enough information to track local typing assumptions compositionally.

This paper presents FEF_E: a calculus with polymorphism and first-class environments. FEF_E introduces two central devices. Box types expose the local typing assumptions needed to type closures and boxes in the presence of polymorphism, so polymorphic environments can be typed compositionally. Manifest types have a dual role. Semantically, they model type-level instantiation. The same mechanism is also what supports manifest type components in modules, as found in ML-style module systems [Leroy 1994; MacQueen 1984; Milner et al. 1997; Rossberg 2018; Rossberg and Dreyer 2013] and related designs with type members [Amin et al. 2014]. Unlike ML modules, FEF_E supports only transparent type components, while sealing is left to future work (Section 6.3). Manifest types are the sole mechanism for type instantiation in FEF_E, avoiding a second, meta-level mechanism that would have to interact with type equivalence. FEF_E preserves a substitution-free, environment-based account and remains conservative over System F.

The resulting metatheory differs substantially from standard developments of System F. Traditional System F metatheory relies heavily on substitution lemmas. In FEF_{E} these are replaced by instantiation lemmas, which describe how typing is preserved when moving between universal types and their manifest instantiations. Combined with de Bruijn indices and an environment-based semantics, this yields a direct mechanization in Rocq. We develop FEF_{E} in two forms. The first form is an explicit, type-passing calculus, which is our main focus in the paper. It follows the conventional presentation of System F, with explicit type abstractions and applications. Although this calculus passes types at run time, types are computationally irrelevant: we prove that a type-erasing semantics reaches the same results. The second form is an implicitly-typed variant that aligns with the semantics of polymorphic functional languages.

FEF_{E} supports capability-style modules that combine dynamic module composition with polymorphism and manifest type components. Environment fragments remain first-class and compositional, supporting dynamic assembly, while boxes run code under explicit local environments, supporting sandboxing. Manifest type components make type information transparently available across module boundaries, so this form of capability-style modularity extends beyond the simply typed setting. FEF_{E} isolates how first-class environments scale to polymorphism through explicit local typing assumptions, manifest type components, and type equivalence. FEF_{E} is thus a System F-level core for polymorphic instantiation, dynamic composition, and sandboxing through first-class environments. Contributions. This paper makes the following contributions:

  • A polymorphic first-class-environment calculus that gives a uniform object-level account of polymorphic instantiation, transparent type components, and environment-based execution, while conservatively extending System F with a substitution-free small-step semantics.

  • Box types for explicit local typing assumptions, and manifest types with two-sided type equivalence, which express type-level instantiation without substitution and support transparent type components in environments and modules.

  • Type soundness, normalization, and decidable type equivalence (with reflexivity, symmetry, and transitivity), conservativity over System F, and a substitution-free metatheory based on instantiation lemmas rather than substitution lemmas.

  • A full Rocq mechanization of the metatheory, covering the explicit calculus, an implicitly-typed variant aligned with the semantics of polymorphic functional languages, and type-preserving elaboration of simple form of first-class modules into FEF_E, plus a prototype implementation.

The mechanization and implementation are available online. 1^{1} The examples can be run and modified in our online playground: https://jam-khan.github.io/EnvML/