arXiv:2611.03918cs.PLNov 2026

Manuscript Offprint

arXiv:2611.03918

Handling JAX

Oghenevwogaga Ebresafe, Daniel Zheng, Gordon Plotkin, Ningning Xie

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

OGHENEVWOGAGA EBRESAFE, University of Toronto, Canada

DANIEL ZHENG, Google DeepMind, USA

GORDON PLOTKIN, Google DeepMind, USA

NINGNING XIE, Google DeepMind and University of Toronto, Canada

Languages that target hardware accelerators typically expose a restricted, first-order core without recursion or first-class functions, leaving programmers without a principled way to express computational effects. This paper presents a technique for compiling effect handlers to such a constrained target. We introduce a novel translation that statically eliminates effect handlers, and prove that it preserves both typing and semantics. Moreover, we address the challenges of integrating effect handlers with structured control flow, where continuations cannot be constructed statically. The calculus and all our results are fully mechanized in the Lean 4 proof assistant. Finally, we implement our design as a library for JAX, a Python domain-specific language for accelerator programming, and demonstrate its expressiveness through practical case studies.

1 Introduction

"When walking about the countryside of Italy, the people will not hesitate to tell you that JAX has 'una anima di pura programmazione funzionale'."

  • JAX documentation¹

Languages that target hardware accelerators are deliberately restrictive. To generate efficient code for GPUs and TPUs, they typically expose a purely functional, first-order core: programs are built from array primitives and a fixed set of structured control-flow constructs such as conditionals and loops, without recursion or first-class functions. Examples include array languages such as Futhark [21], Dex [36], and Accelerate [7], and compiler intermediate representations such as XLA [17]. While these restrictions enable aggressive optimization, they leave programmers without a principled way to express computational effects such as mutable state and exceptions. Without language support, each such feature is typically implemented via a bespoke program transformation or an external library.

We study this problem in the setting of JAX [6], a domain-specific language embedded in Python for accelerator-oriented array computations, widely adopted in high-performance numerical computing and large-scale machine learning, including for Google's large language models [8, 15, 18] and by Anthropic [2]. JAX traces Python programs into a first-order intermediate representation, jaxprs, with exactly the restrictions described above, and lacks direct support for effectful features. As a result, popular JAX libraries resort to different, often ad-hoc mechanisms to implement these features, causing inconsistencies and interoperability issues across the ecosystem (see §2).

We address this challenge using algebraic effect handlers [41, 42], a flexible and composable mechanism for defining structured control-flow abstractions. While effect handlers have been incorporated into various languages [3, 29, 48], including recent work on low-level languages such as C [1, 28], C++ [16], and WebAssembly [38], all such implementations rely on features that first-order languages lack: higher-order functions, recursion, or a capturable runtime stack. The central question of this paper is therefore: how can effect handlers be compiled to a first-order target without higher-order functions or recursion? We resolve this by introducing a translation that statically

¹ https://docs.jax.dev/en/latest/notebooks/Common_Gotchas_in_JAX.html

Authors' Contact Information: Oghenevwogaga Ebresafe, gaga@cs.toronto.edu, University of Toronto, Canada; Daniel Zheng, danielzheng@google.com, Google DeepMind, USA; Gordon Plotkin, plotkin@google.com, Google DeepMind, USA; Ningning Xie, ningningxie@google.com, Google DeepMind and University of Toronto, Canada.

Oghenevwogaga Ebresafe, Daniel Zheng, Gordon Plotkin, and Ningning Xie

eliminates handlers, carefully extending the technique across loop boundaries where continuations cannot be constructed statically.

Contributions. Following the high-level overview in §2, we present our contributions in the following sections:

  • Formalization (§3): We formalize λJAXprϵ\lambda_{\text{JAXpr}}^{\epsilon}, a calculus that extends a core subset of JAX's intermediate representation, a first-order language with second-order control-flow primitives, with parameterized algebraic effect handlers. We establish preservation and progress with effects.
  • Translation (§4): We introduce a novel continuation-based translation that statically compiles effect handlers away without needing higher-order functions or recursion, and prove that it preserves both typing and semantics.
  • Iterative control flow (§5): We analyze the challenges of translating effect handlers across loop boundaries, where continuations cannot be constructed statically, and present a mechanism to support tail-resumptive and abortive handlers while preserving handler parameters and scoping.
  • Mechanization: We fully mechanize our calculus, its type system and operational semantics, translation, and all soundness proofs in the Lean 4 proof assistant.
  • Implementation (§6): We implement effecax, an effect handler library for JAX, outlining its programming interface and our implementation.
  • Evaluation (§7): We evaluate effecax across practical case studies, demonstrating that it provides an expressive, flexible, and natural interface for JAX programs.

While this paper focuses on JAX, where effecax provides immediate utility to a broad user base, neither the problem nor our solution is specific to it: the compilation applies to any first-order target, such as XLA's intermediate representation. More broadly, we hope this work offers a new perspective on compiling effect handlers to low-level target languages. Lastly, we discuss related work in §8.

References (20)