Download PDF

Reliability and Expressivity of Bidirectional Transformations

Publication date: 2026-05-21

Author:

Xie, Ruifeng
Schrijvers, Tom ; Hu, Zhenjiang

Abstract:

Synchronisation tasks are widely present in various practical programs. Bidirectional transformations (BXs, or lenses), originally proposed by Foster et al., are a generic theory for describing the synchronisation between two different data structures, with applications in databases, software model transformation, graph transformation, etc. BX frameworks not only provide practical ways to develop bidirectional programs, but also specify the correctness properties and supports compositional programming via law-preserving combinators. Despite the theoretical strengths, generic BX theories are not widely used in production. On one hand, existing BX frameworks cannot guarantee the reliability of lenses. It is expected that certain updates cannot be synchronised properly, yet this possibility of synchronisation failure is not explicitly and formally communicated for these lenses. The problem is made more severe because BX theories promote compositional programming, and it is very hard to reason about failure points through nested levels of compositions. On the other hand, practical applications often require access to certain side effects, such as logging, runtime errors, mutable states, or even unrestricted I/O, but existing BX frameworks are not expressive enough to support liberal use of side effects. Traditional BX theories are based on pure functions, which cannot perform any side effect; recent theories like monadic lenses have limited support for side effects described using monads, but no framework support using two different effects in the two directions of a lens. Reliability and expressivity, these are two main limitations of BX theories. To address the two limitations, we propose two developments of BX theories: contract lenses and effectful lenses. We introduce contracts into lenses to ensure their reliability. These contracts formally specify valid updates that are accepted by the backward transformation of a lens, and the theory makes sure that all valid updates can be properly synchronised. We propose a collection of generic recursion patterns for bidirectional transformations, and develop corresponding calculation laws. With these generic recursion patterns, we can develop structured bidirectional transformations, which are easy to understand and maintain. With these calculation laws, we can effectively optimize bidirectional programs, while preserving its very semantics. We introduce effects into lenses to improve their expressivity. The effects are described using monads, which are widely adopted in functional programming to describe arbitrary side effects. Our effectful lenses can have different effects in their two directions, and the effects need not be cancellable. We also define the pure-likeness predicates and use them to generalise the two well-known round-trip properties to effectful lenses. Moreover, composition preserves the two well-known round-trip properties, and we also provide a rich combinator language, which enables compositional programming for effectful lenses. We also present a case study to illustrate the flexibility and expressivity of our framework. We also consider a practical synchronisation task, the synchronisation between the textual form of a program and its syntax tree, namely parsing and printing. We develop the theory of biparsers as an instance of effectful lenses. Compared to previous approaches, our biparser framework supports exact printing of abstract syntax trees, which recovers the original source text for the parsed data. We demonstrate the expressivity of our framework with biparsers for subsets of JSON and YAML as a case study. In conclusion, we propose two developments of BX theories to ensure the reliability and improve the expressivity, making bidirectional transformations a practical tool for trustworthy programming and practical programming with effects in production.