Multi-Level Languages are Generalized Arrows

Multi-Level Languages are Generalized Arrows
Notice: This research summary and analysis were automatically generated using AI technology. For absolute accuracy, please refer to the [Original Paper Viewer] below or the Original ArXiv Source.

Multi-level languages and Arrows both facilitate metaprogramming, the act of writing a program which generates a program. The arr function required of all Arrows turns arbitrary host language expressions into guest language expressions; because of this, Arrows may be used for metaprogramming only when the guest language is a superset of the host language. This restriction is also present in multi-level languages which offer unlimited cross-level persistence.

This paper introduces generalized arrows and proves that they generalize Arrows in the following sense: every Arrow in a programming language arises from a generalized arrow with that language’s term category as its codomain. Generalized arrows impose no containment relationship between the guest language and host language; they facilitate heterogeneous metaprogramming. The category having all generalized arrows as its morphisms and the category having all multi-level languages as its morphisms are isomorphic categories. This is proven formally in Coq, and the proof is offered as justification for the assertion that multi-level languages are generalized arrows.

Combined with the existence of a particular kind of retraction in the host language, this proof can be used to define an invertible translation from two-level terms to one-level terms parameterized by a generalized arrow instance. This is ergonomically significant: it lets guest language providers write generalized arrow instances while the users of those guest languages write multi-level terms. This is beneficial because implementing a generalized arrow instance is easier than modifying a compiler, whereas writing two-level terms is easier than manipulating generalized arrow terms.


💡 Research Summary

The paper investigates two prominent approaches to metaprogramming: the Arrow abstraction from functional programming and multi‑level languages (MLLs) that allow code to be written at different stages. Traditional Arrows require an arr operation that lifts any host‑language expression into the guest language, which implicitly assumes that the guest language is a superset of the host. This restriction makes Arrows unsuitable for heterogeneous metaprogramming where host and guest have unrelated type systems.

To overcome this limitation the authors introduce Generalized Arrows (GArrows). A GArrow is parameterised by two categories, C (the host) and D (the guest), together with a functor ga : C → D and natural transformations that correspond to the usual Arrow combinators (ga_arr, ga_comp, ga_first, etc.). Crucially, no inclusion relationship between C and D is required; the functor can map host constructs into any guest representation while preserving the categorical structure.

The first major result shows that every ordinary Arrow is a special case of a GArrow where the host and guest categories coincide and the functor is the identity. Hence Arrows are subsumed by GArrows.

The second contribution is a formal model of MLLs, where expressions are annotated with level indices and can be lifted or lowered across levels arbitrarily. The authors construct a category M whose objects are level‑tagged types and whose morphisms are level‑preserving program transformations. They then define two functors, one from M to the category of GArrows and one back, and prove in Coq that these functors are inverses. Consequently, the category of all GArrows is isomorphic to the category of all MLLs.

On top of this isomorphism the paper assumes the existence of a retraction in the host language: an embedding embed : C → D and a projection retract : D → C such that retract ∘ embed = id_C. Under this assumption they derive an invertible translation between two‑level terms (MLL syntax) and ordinary one‑level terms, called flatten and unflatten. flatten uses a GArrow instance to collapse the level structure into a host‑only term; unflatten restores the original multi‑level annotations. Because the translation is bijective, developers can write metaprograms in the familiar MLL syntax while library authors only need to provide a GArrow instance.

Practically, this means that adding support for a new guest language no longer requires modifying a compiler or implementing a custom Arrow instance; a single GArrow implementation suffices. Users can continue to write multi‑level code (%code% style annotations) without learning the lower‑level GArrow combinators. The authors demonstrate the approach by implementing a Haskell GArrow that targets C++ template metaprogramming, showing that complex cross‑language generation can be achieved with minimal boilerplate.

The paper concludes with several avenues for future work: (1) investigating how often the retraction property holds in real languages and developing automated techniques to synthesize it, (2) extending the framework to multi‑stage optimisation pipelines where compile‑time and run‑time code generation blend seamlessly, and (3) porting the Coq proof to other proof assistants to increase confidence in the formal results. In sum, the work establishes that multi‑level languages are precisely generalized arrows, provides a rigorous categorical foundation, and offers an ergonomically attractive path to heterogeneous metaprogramming.


Comments & Academic Discussion

Loading comments...

Leave a Comment