Hostname: page-component-77f85d65b8-6c7dr Total loading time: 0 Render date: 2026-03-28T16:34:25.291Z Has data issue: false hasContentIssue false

Contract lenses: Reasoning about bidirectional programs via calculation

Published online by Cambridge University Press:  06 November 2023

HANLIANG ZHANG
Affiliation:
University of Bristol, Bristol BS8 1QU, UK (e-mail: hanliang.zhang@bristol.ac.uk)
WENHAO TANG
Affiliation:
The University of Edinburgh, Edinburgh EH8 9AB, UK (e-mail: wenhao.tang@ed.ac.uk)
RUIFENG XIE
Affiliation:
Peking University, Beijing, China (e-mail: xieruifeng@pku.edu.cn)
MENG WANG
Affiliation:
University of Bristol, Bristol BS8 1QU, UK (e-mail: meng.wang@bristol.ac.uk)
ZHENJIANG HU
Affiliation:
Peking University, Beijing, China (e-mail: huzj@pku.edu.cn)
Rights & Permissions [Opens in a new window]

Abstract

Bidirectional transformations (BXs) are a mechanism for maintaining consistency between multiple representations of related data. The lens framework, which usually constructs BXs from lens combinators, has become the mainstream approach to BX programming because of its modularity and correctness by construction. However, the involved bidirectional behaviors of lenses make the equational reasoning and optimization of them much harder than unidirectional programs. We propose a novel approach to deriving efficient lenses from clear specifications via program calculation, a correct-by-construction approach to reasoning about functional programs by algebraic laws. To support bidirectional program calculation, we propose contract lenses, which extend conventional lenses with a pair of predicates to enable safe and modular composition of partial lenses. We define several contract-lens combinators capturing common computation patterns including $\textit{fold}, \textit{filter},\textit{map}$, and $\textit{scan}$, and develop several bidirectional calculation laws to reason about and optimize contract lenses. We demonstrate the effectiveness of our new calculation framework based on contract lenses with nontrivial examples.

Information

Type
Research Article
Creative Commons
Creative Common License - CCCreative Common License - BY
This is an Open Access article, distributed under the terms of the Creative Commons Attribution licence (http://creativecommons.org/licenses/by/4.0/), which permits unrestricted re-use, distribution and reproduction, provided the original article is properly cited.
Copyright
© The Author(s), 2023. Published by Cambridge University Press
Figure 0

Fig. 1. Implementation of bmapl. The left figure shows the computation of the get and the right figure shows the computation of the put.

Figure 1

Fig. 2. Implementation of bscanl. The left figure shows the computation of the get and the right figure shows the computation of the put.

Figure 2

Fig. 3. Visualization of an example calculation of bmss.

Submit a response

Discussions

No Discussions have been published for this article.