Papers
Topics
Authors
Recent
Assistant
AI Research Assistant
Well-researched responses based on relevant abstracts and paper content.
Custom Instructions Pro
Preferences or requirements that you'd like Emergent Mind to consider when generating responses.
Gemini 2.5 Flash
Gemini 2.5 Flash 71 tok/s
Gemini 2.5 Pro 38 tok/s Pro
GPT-5 Medium 36 tok/s Pro
GPT-5 High 39 tok/s Pro
GPT-4o 110 tok/s Pro
Kimi K2 191 tok/s Pro
GPT OSS 120B 462 tok/s Pro
Claude Sonnet 4.5 36 tok/s Pro
2000 character limit reached

Bidirectional Compilers: Inverting Program Translation

Updated 6 October 2025
  • Bidirectional compilers are program translation systems that enable invertible conversions between languages, ensuring round-tripping and verified transformations.
  • They leverage relational compilation and monadic frameworks to support DSL lifting, compositional reasoning, and cross-language synthesis.
  • Practical applications include secure compilation, interoperability, and rapid prototyping of verified lifters with formal methods ensuring type safety.

Bidirectional compilers are program translation systems engineered to enable explicit and invertible conversions between two or more programming languages or representations. Rather than treating compilation as a strictly one-way process—translating source to target—they support both forward translation and "lifting" from a target language representation back into the source. This capability addresses needs in verified compilation, DSL (domain-specific language) lifting, interoperability, and cross-language program synthesis. Bidirectional compiler research synthesizes principles from programming languages, logic programming, operational semantics, and formal verification.

1. Principles of Bidirectional Compilation

Bidirectional compilation hinges on inverting the traditional notion of a compiler as a deterministic function. Instead, the transformation is formulated as either a relation (as in (Spargo et al., 30 Sep 2025)) or as a structure that encapsulates both the forward and backward semantics. In relational compilation, the translation is specified as a relation CS×T\mathcal{C} \subseteq S \times T where SS is the set of source representations and TT is the set of target representations. Forward compilation corresponds to existentially quantifying over TT given SS; backward lifting existentially quantifies over SS given TT.

Another approach is monadic bidirectional programming (Xia et al., 2019), where bidirectional programs are composed using familiar abstractions (such as monads and profunctors) enabling sequential, compositional translation in both directions. Here, both forward\mathsf{forward} and backward\mathsf{backward} computations are combined into a single structure, often using product types and functorial lifting.

Key properties include:

  • Invertibility: Ability to reconstruct high-level source from low-level target.
  • Round-tripping: Ensuring that compile-then-lift (or lift-then-compile) yields the original program.
  • Compositionality: Support for modular reasoning about round-tripping and correctness at the granularity of program fragments.

2. Relational Compilation and DSL Lifting

The CoCompiler (Spargo et al., 30 Sep 2025) exemplifies relational compilation for bidirectional compilers. The approach reformulates a verified Lustre-to-C compiler (Vélus) as a relation in Walrus, a relational programming language inspired by miniKanren and embedded in Haskell. This "compile" relation relates Lustre ASTs and canonical C code—crucially, it admits solving for Lustre given a C program or vice versa.

Relational compilation is formally expressed as: compile:LustreCGoal()compile : \mathsf{Lustre} \to \mathsf{C} \to Goal() where Goal()Goal() is a logical constraint system specifying correspondence between Lustre and C. Running the relation with a concrete Lustre program yields its C translation, while running it with canonical C (in Vélus's image) finds an abstract Lustre program.

This approach confers several advantages:

  • Bidirectionality "for free": Existing compilers can be repurposed for DSL lifting by recasting their translation as relations.
  • Modularity and language-agnosticism: The same pattern applies broadly if a verified forward compiler is available.
  • Rapid prototyping: Porting Vélus's phases to Walrus required only several engineer-weeks.

3. Addressing Vertical and Horizontal Lifting Problems

Bidirectional lifting divides into the "vertical" and "horizontal" problems (Editor's term):

Problem Description Solution
Vertical Lifting canonical C (output by Vélus) to Lustre Use relational "compile" relation
Horizontal Lifting arbitrary-real world C code to canonical form (then to Lustre) Apply semantics-preserving canonicalization in Haskell

Vertical lifting performs one-to-one inversion, relying on canonical structure and the uniqueness of the translation. Horizontal lifting addresses real-world C that does not conform to Vélus output, using Haskell-based canonicalization passes to transform C into a suitable form for relational compilation. These passes handle alpha-renaming, reordering, and other semantics-preserving rewrites.

This two-level strategy generalizes the lifting process to handle many-to-many relationships among semantically equivalent programs.

4. Monadic Bidirectional Frameworks

Monadic bidirectional programming (Xia et al., 2019) composes independently defined forward and backward computations within a monadic profunctor setting. For instance, a biparser unifies parsing and printing:

1
2
data Biparser u v = Biparser { parse :: String -> (v, String)
                             , print :: u -> (v, String) }

Forward composition uses the standard parser monad, while backward (contravariant) direction is monadized using structures like

Fwd  m  u  v=m  v,Bwd  m  u  v=um  v\mathtt{Fwd}\; m\; u\; v = m\; v,\qquad \mathtt{Bwd}\; m\; u\; v = u \to m\; v

with the product forming a monadic profunctor.

Applications span parsers/printers, lenses (get/put pairs), and generator/predicate systems. Verification of round-tripping properties leverages compositionality: compositional reasoning is possible when round-tripping laws are invariant under monadic bind, return, and comap (partial function application).

Monadic frameworks are expressive and support Haskell idioms (do-notation), but correct round-tripping is not enforced by construction and must be verified by reasoning about purification operations. This places more burden on users compared with specialized "lens" systems, but grants much greater flexibility.

5. Multi-Language Semantics in Verified and Secure Compilation

Modeling the compiler as a reduction system in a multi-language semantics—instead of duplicating compiler passes for open and closed terms—yields bidirectional interoperability and insights into compilation correctness (Bowman, 23 Sep 2025). Syntax for both source and target languages is embedded with boundary markers, enabling reduction rules to support transitions across languages: $\texttt{[\,S \Rightarrow A\,]}$ Reduction steps operate uniformly for both compilation and run-time translation; normalization of cross-language redexes effects ahead-of-time (AOT) compilation, while evaluation naturally models just-in-time (JIT) compilation.

Confluence of multi-language reduction ensures compiler correctness and secure compilation. Subject reduction guarantees type preservation through the entire bidirectional process: if Γt:T and tt, then Γt:T.\text{if } \Gamma \vdash t : T \text{ and } t \longrightarrow t', \text{ then } \Gamma \vdash t' : T. This guarantees that as compilation or lifting traverses language boundaries, program typings remain intact.

6. Applications, Impact, and Future Work

Bidirectional compilers underpin DSL lifting, verified round-trip compilation, cross-language program synthesis, and interoperability. The CoCompiler (Spargo et al., 30 Sep 2025) enables lifting reactive C into Lustre, from which further model transformations (e.g. to SCADE) are possible. Monadic frameworks (Xia et al., 2019) generalize to diverse bidirectional transformations in Haskell, and multi-language semantics (Bowman, 23 Sep 2025) extend to secure compilation and full abstraction proofs.

Impact includes:

  • Practical DSL lifters: Rapid construction of lifters for domain-specific languages by reusing existing verified compilers in relational form.
  • Reductions in translation complexity: Universal IRs (e.g., CrossGL in CrossTL (Niketan et al., 28 Aug 2025)) collapse the translation problem to linear scaling, easing support for new languages and maintenance.
  • Semantic correctness: Round-tripping, type preservation, and confluence properties support high-assurance software engineering.

Future directions involve extending bidirectional methods to more expressive language features, refining verification techniques, and broadening adoption in industrial, safety-critical, and legacy modernization settings. There is ongoing work on abstract frameworks (e.g. adjunctions for bidirectionality (Xia et al., 2019)) and the generalization of these ideas to effectful transformations, graphical behavioral models, and secure compilation.

Forward Email Streamline Icon: https://streamlinehq.com

Follow Topic

Get notified by email when new papers are published related to Bidirectional Compilers.