English

Handling Scope Checks (Extended Version)

Programming Languages 2026-01-27 v1

Abstract

Metaprogramming and effect handlers interact in unexpected, and sometimes undesirable, ways. One example is scope extrusion: the generation of ill-scoped code. Scope extrusion can either be preemptively prevented, via static type systems, or retroactively detected, via dynamic checks. Static type systems exist in theory, but struggle with a range of implementation and usability problems in practice. In contrast, dynamic checks exist in practice (e.g. in MetaOCaml), but are understudied in theory. Designers of metalanguages are thus given little guidance regarding the design and implementation of checks. We present the first formal study of dynamic scope extrusion checks, introducing a calculus (λop\lambda_{\langle\langle\text{op}\rangle\rangle}) for describing and evaluating checks. Further, we introduce a novel dynamic check \unicodex2014\unicode{x2014} the "Cause-for-Concern" check \unicodex2014\unicode{x2014} which we prove correct, characterise without reference to its implementation, and argue combines the advantages of existing dynamic checks. Finally, we extend our framework with refined environment classifiers, which statically prevent scope extrusion, and compare their expressivity with the dynamic checks.

Keywords

Cite

@article{arxiv.2601.18793,
  title  = {Handling Scope Checks (Extended Version)},
  author = {Michael Lee and Ningning Xie and Oleg Kiselyov and Jeremy Yallop},
  journal= {arXiv preprint arXiv:2601.18793},
  year   = {2026}
}

Comments

Extended version of Handling Scope Checks (POPL'26): includes appendices, fixes minor typos, and tweaks phrasing for readability

R2 v1 2026-07-01T09:20:55.257Z