Contextual Metaprogramming for Session Types
Pedro Ângelo, Atsushi Igarashi, Yuito Murase, Vasco T. Vasconcelos
TL;DR
Contextual Metaprogramming for Session Types addresses integrating staged metaprogramming with session-typed concurrency. The authors develop a multilevel contextual modal linear lambda calculus that combines a box modality $\square\tau$ with contextual types $\overline\tau \vdash^{n} T$ and a linear/unrestricted resource system. They demonstrate how code fragments can be produced, transmitted via channels, and executed on demand, while preserving channel linearity and session protocols. A formal type system with context split and a type checker ensures typing judgments for terms and processes, and the accompanying proofs establish preservation and progress for a concurrent runtime. The work enables robust server-side code generation, distributed computation, and multi-tier metaprogramming with typed, deadlock-averse communication patterns.
Abstract
We propose the integration of staged metaprogramming into a session-typed message passing functional language. We build on a model of contextual modal type theory with multi-level contexts, where contextual values, closing arbitrary terms over a series of variables, may be boxed and transmitted in messages. Once received, one such value may then be unboxed and locally applied before being run. To motivate this integration, we present examples of real-world use cases, for which our system would be suitable, such as servers preparing and shipping code on demand via session typed messages. We present a type system that distinguishes linear (used exactly once) from unrestricted (used an unbounded number of times) resources, and further define a type checker, suitable for a concrete implementation. We show type preservation, a progress result for sequential computations and absence of runtime errors for the concurrent runtime environment, as well as the correctness of the type checker.
