[Paper Review] Multi-Level Languages are Generalized Arrows
This paper introduces generalized arrows as a categorical foundation that generalizes both Arrows and multi-level languages, proving they are isomorphic categories. It enables heterogeneous metaprogramming by removing the need for host and guest languages to be in a subset-superset relationship, and provides a formal Coq-verified framework for translating two-level multi-level programs into one-level generalized arrow terms via a retraction in the host language.
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.
Motivation & Objective
- To address the limitation in existing metaprogramming models where host and guest languages must be in a subset-superset relationship.
- To generalize Arrows beyond their traditional constraints by introducing generalized arrows as a broader categorical framework.
- To formally prove that multi-level languages and generalized arrows form isomorphic categories, enabling bidirectional translation between them.
- To enable practical, ergonomic metaprogramming by allowing guest language authors to implement generalized arrow instances while users write multi-level terms.
- To provide a proof-of-concept implementation in Haskell using a modified GHC with a flattening pass derived from Coq-verified proofs.
Proposed method
- Defining generalized arrows as morphisms in a category whose codomain is the term category of a host language, generalizing the Arrow arr function.
- Formalizing multi-level languages as a category with terms annotated by levels and a run construct for staging.
- Proving an isomorphism between the category of generalized arrows and the category of multi-level languages using category theory and Coq.
- Leveraging a retraction in the host language (e.g., Haskell) to construct an invertible translation from two-level terms to one-level generalized arrow terms.
- Implementing a flattening pass in a modified GHC compiler that uses the extracted Coq proofs to translate multi-level programs into generalized arrow code.
- Using the generalized arrow instance to encode the semantics of multi-level constructs like copy, drop, and apply via type class methods.
Experimental results
Research questions
- RQ1Can Arrows be generalized to support heterogeneous metaprogramming where the guest language is not a superset of the host language?
- RQ2Is there a categorical isomorphism between the category of generalized arrows and the category of multi-level languages?
- RQ3Can a retraction in the host language enable an invertible, ergonomic translation from two-level programs to one-level generalized arrow programs?
- RQ4What are the structural features of generalized arrows that correspond to multi-level language constructs like staging, run, and reflection?
- RQ5How can formal proofs in Coq be extracted and compiled into a practical compiler extension for a real language like Haskell?
Key findings
- Generalized arrows generalize Arrows by removing the requirement that the guest language be a superset of the host language, enabling heterogeneous metaprogramming.
- The category of generalized arrows is isomorphic to the category of multi-level languages, formally establishing that multi-level languages are a special case of generalized arrows.
- An invertible translation from two-level multi-level terms to one-level generalized arrow terms is possible when a retraction exists in the host language, as demonstrated in Haskell with a modified GHC.
- The Coq-verified proofs of the isomorphism were successfully extracted and compiled into a new flattening pass for GHC, validating the practicality of the approach.
- The implementation supports key constructs like copy, drop, and apply through generalized arrow type classes, with the flattening pass correctly handling recursive and conditional terms.
- The current framework does not statically determine which generalized arrow operations (e.g., GArrowCopy, GArrowDrop) are needed from the type alone, indicating a need for enhanced type tracking in the compiler.
Read this paper
Open the full text in the Nubint reader and ask about any passage as you read.
Read this paper on NubintThis review was generated by AI from the original paper. Please check the paper before citing it.