Abstract
Bidirectional transformations (bx) have primarily been modeled as pure functions, and do not account for the possibility of the side-effects that are available in most programming languages. Recently several formulations of bx that use monads to account for effects have been proposed, both among practitioners and in academic research. The combination of bx with effects turns out to be surprisingly subtle, leading to problems with some of these proposals and increasing the complexity of others. This paper reviews the proposals for monadic lenses to date, and offers some improved definitions, paying particular attention to the obstacles to naively adding monadic effects to existing definitions of pure bx such as lenses and symmetric lenses, and the subtleties of equivalence of symmetric bidirectional transformations in the presence of effects.
Access this chapter
Tax calculation will be finalised at checkout
Purchases are for personal use only
References
Abou-Saleh, F., Cheney, J., Gibbons, J., McKinna, J., Stevens, P.: Notions of bidirectional computation and entangled state monads. In: Hinze, R., Voigtländer, J. (eds.) MPC 2015. LNCS, vol. 9129, pp. 187–214. Springer, Heidelberg (2015a)
Abou-Saleh, F., McKinna, J., Gibbons, J.: Coalgebraic aspects of bidirectional computation. In: BX 2015, CEUR-WS, vol. 1396, pp. 15–30 (2015b)
Cheney, J., McKinna, J., Stevens, P., Gibbons, J., Abou-Saleh, F.: Entangled state monads. In: Terwilliger and Hidaka (2014)
Diviánszky, P.: LGtk API correction. http://people.inf.elte.hu/divip/LGtk/CorrectedAPI.html
Foster, J.N., Greenwald, M.B., Moore, J.T., Pierce, B.C., Schmitt, A.: Combinators for bidirectional tree transformations: a linguistic approach to the view-update problem. TOPLAS 29(3), 17 (2007)
Foster, N., Matsuda, K., Voigtländer, J.: Three complementary approaches to bidirectional programming. In: Gibbons, J. (ed.) Generic and Indexed Programming. LNCS, vol. 7470, pp. 1–46. Springer, Heidelberg (2012)
Hofmann, M., Pierce, B.C., Wagner, D.: Symmetric lenses. In: POPL, pp. 371–384. ACM (2011)
Johnson, M., Rosebrugh, R.: Spans of lenses. In: Terwilliger and Hidaka (2014)
Jones, M.P., Duponcheel, L.: Composing monads. Technical report RR-1004, DCS, Yale (1993)
King, D.J., Wadler, P.: Combining monads. In: Proceedings of the 1992 Glasgow Workshop on Functional Programming, pp. 134–143 (1992)
Liang, S., Hudak, P., Jones, M.P.: Monad transformers and modular interpreters. In: POPL, pp. 333–343 (1995)
Pacheco, H., Hu, Z., Fischer, S.: Monadic combinators for “putback” style bidirectional programming. In: PEPM, pp. 39–50. ACM (2014). http://doi.acm.org/10.1145/2543728.2543737
Plotkin, G., Power, J.: Notions of computation determine monads. In: Nielsen, M., Engberg, U. (eds.) FOSSACS 2002. LNCS, vol. 2303, pp. 342–356. Springer, Heidelberg (2002)
Stevens, P.: Bidirectional model transformations in QVT: Semantic issues and open questions. SoSyM 9(1), 7–20 (2010)
Terwilliger, J., Hidaka, S. (eds.): BX Workshop (2014). http://ceur-ws.org/Vol-1133/#bx
TLCBX Project: a theory of least change for bidirectional transformations (2013–2016). http://www.cs.ox.ac.uk/projects/tlcbx/, http://groups.inf.ed.ac.uk/bx/
Wadler, P.: Comprehending monads. Math. Struct. Comput. Sci. 2(4), 461–493 (1992). http://dx.org/10.1017/S0960129500001560
Wadler, P.: Monads for functional programming. In: Jeuring, J., Meijer, E. (eds.) AFP 1995. LNCS, vol. 925, pp. 24–52. Springer, Heidelberg (1995)
Acknowledgements
The work was supported by the UK EPSRC-funded project A Theory of Least Change for Bidirectional Transformations (TLCBX Project 2013–2016) (EP/K020218/1, EP/K020919/1).
Author information
Authors and Affiliations
Corresponding author
Editor information
Editors and Affiliations
Appendices
A Proofs for Sect. 2
Theorem 2.5
If and are well-behaved, then so is \( l _{1}\mathbin {;} l _{2}\).
Proof
Suppose \( l _{1}\) and \( l _{2}\) are well-behaved, and let \( l \mathrel {=} l _{1}\mathbin {;} l _{2}\). We reason as follows for \(\mathsf {(MGetPut)}\):
For \(\mathsf {(MPutGet)}\), the proof is as follows:
\(\square \)
B Proofs for Sect. 3
Proposition 3.6
\( setBool \; x \) is well-behaved for \( x \in \{ True , False \}\), but \( setBool \; True \mathbin {;} setBool \; False \) is not well-behaved.
\(\diamondsuit \)
For the first part:
Proof
Let \( sl \mathrel {=} setBool \; x \). We consider \(\mathsf {(PutRLM)}\), and \(\mathsf {(PutLRM)}\) is symmetric.
For the second part, taking \( sl \mathrel {=} setBool \; True \mathbin {;} setBool \; False \), we proceed as follows:
However, we cannot simplify this any further. Moreover, it should be clear that the shared state will be \( True \) after this operation is performed. Considering the other side of the desired equation:
it should be clear that the shared state will be \( False \) after this operation is performed. Therefore, \(\mathsf {(PutRLM)}\) is not satisfied by \( sl \). \(\square \)
Lemma 3.8
If and are well-behaved then so is .
\(\diamondsuit \)
Proof
It suffices to consider the two lenses \( l _{1}\mathrel {=} MLens \; fst \; put _\mathrm {L}\; create _\mathrm {L}\) and \( l _{2}\mathrel {=} MLens \; snd \; put _\mathrm {R}\; create _\mathrm {R}\) in isolation. Moreover, the two cases are completely symmetric, so we only show the first.
For \(\mathsf {(MGetPut)}\), we show:
The proof for \(\mathsf {(MPutGet)}\) goes as follows. Note that it holds by construction, without appealing to well-behavedness of \( ml _{\mathrm {1}}\) or \( ml _{\mathrm {2}}\).
The proof for \(\mathsf {(MCreateGet)}\) is similar.
Finally, we show that \( put _\mathrm {L}\mathbin {{:}{:}}(\sigma _1\mathbin {\!\bowtie \!}\sigma _2)\rightarrow \sigma _1\rightarrow \mu \;(\sigma _1\mathbin {\!\bowtie \!}\sigma _2)\), and in particular, that it maintains the consistency invariant on the state space \(\sigma _1\mathbin {\!\bowtie \!}\sigma _2\). Assume that \(( s _{1}, s _{2})\mathbin {{:}{:}}\sigma _1\mathbin {\!\bowtie \!}\sigma _2\) and \( s _{1}'\mathbin {{:}{:}}\sigma _1\) are given. Thus, \( ml _{\mathrm {1}}\mathord {.} mget \; s _{1}\mathrel {=} ml _{\mathrm {2}}\mathord {.} mget \; s _{2}\). We must show that any value returned by \( put _\mathrm {L}\) also satisfies this consistency criterion. By definition,
By \(\mathsf {(MPutGet)}\), any \( s _{2}'\) resulting from \( ml _{\mathrm {2}}\mathord {.} mput \; s _{2}\;( ml _{\mathrm {1}}\mathord {.} mget \; s _{1}')\) will satisfy \( ml _{\mathrm {2}}\mathord {.} mget \; s _{2}'\mathrel {=} ml _{\mathrm {1}}\mathord {.} mget \; s _{1}'\). The proof that \( create _\mathrm {L}\mathbin {{:}{:}}\sigma _1\rightarrow \mu \;(\sigma _1\mathbin {\!\bowtie \!}\sigma _2)\) is similar, but simpler. \(\square \)
Theorem 3.10
If is well-behaved, then \( span2smlens \; sp \) is also well-behaved.
\(\diamondsuit \)
Proof
Let \( sl \mathrel {=} span2smlens \; sp \). We need to show that the laws \(\mathsf {(PutRLM)}\) and \(\mathsf {(PutLRM)}\) hold. We show \(\mathsf {(PutRLM)}\), and \(\mathsf {(PutLRM)}\) is symmetric.
We need to show that
There are two cases, depending on whether the initial state \( mc \) is \( Nothing \) or \( Just \; c \) for some \( c \).
If \( mc \mathrel {=} Nothing \) then we reason as follows:
If \( mc \mathrel {=} Just \; c \) then we reason as follows:
\(\square \)
Theorem 3.12
If \( sl \mathbin {{:}{:}} SMLens \; Id \; C \; A \; B \) is well-behaved, then \( smlens2span \; sl \) is also well-behaved, with state space \( S \) consisting of the consistent triples of \( sl \).
\(\diamondsuit \)
Proof
First we show that, given a symmetric lens \( sl \), the operations of \( sp \mathrel {=} smlens2span \; sl \) preserve consistency of the state. Assume \(( a , b , c )\) is consistent. To show that \( sp \mathord {.} left \mathord {.} mput \;( a , b , c )\; a' \) is consistent for any \( a' \), we have to show that \(( a' , b' , c' )\) is consistent, where \( a' \) is arbitrary and \( return \;( b' , c' )\mathrel {=} sl \mathord {.} mput _\mathrm {R}\;( a' , c )\). For one half of consistency, we have:
The proof that \( sl \mathord {.} mput _\mathrm {R}\;( a' , c' )\mathrel {=} return \;( b' , c' )\) is symmetric.
as required. The proof that \( sp \mathord {.} right \mathord {.} mput \;( a , b , c )\; b' \) is consistent is dual.
We will now show that \( sp \mathrel {=} smlens2span \; sl \) is a well-behaved span for any symmetric lens \( sl \). For \(\mathsf {(MGetPut)}\), we proceed as follows:
For \(\mathsf {(MPutGet)}\), we have:
The proof for \(\mathsf {(MCreateGet)}\) is similar.
\(\square \)
C Proofs for Sect. 4
Lemma 4.3
Suppose \( l _{1}\mathbin {{:}{:}} A \mathbin {\leadsto } B \) and \( l _{2}\mathbin {{:}{:}} C \mathbin {\leadsto } B \) are pure lenses. Then \(( l _{1}\mathbin {\bowtie } l _{2})\mathord {.} left \mathbin {;} l _{1}\mathrel {=}( l _{1}\mathbin {\bowtie } l _{2})\mathord {.} right \mathbin {;} l _{2}\).
\(\diamondsuit \)
Proof
Let \(( l , r )\mathrel {=} l _{1}\mathbin {\bowtie } l _{2}\). We show that each component of \( l \mathbin {;} l _{1}\) equals the corresponding component of \( r \mathbin {;} l _{2}\).
For \( get \):
For \( put \):
Finally, for \( create \):
\(\square \)
Theorem 4.9
Given , if then .
\(\diamondsuit \)
Proof
We give the details for the case \( sp _{\mathrm {1}}\curvearrowright sp _{\mathrm {2}}\). First, write \(( l _{1}, r _{1})\mathrel {=} sp _{\mathrm {1}}\) and \(( l _{2}, r _{2})\mathrel {=} sp _{\mathrm {2}}\), and suppose \( l \mathbin {{:}{:}} S _{1}\mathbin {\leadsto } S _{2}\) is a lens satisfying \( l _{1}\mathrel {=} l \mathbin {;} l _{2}\) and \( r _{1}\mathrel {=} l \mathbin {;} r _{2}\).
We need to define a bisimulation consisting of a set \( R \subseteq S _{1} \times S _{2}\) and a span such that \( fst \) is a base map from \( sp \) to \( sp _{\mathrm {1}}\) and \( snd \) is a base map from \( sp \) to \( sp _{\mathrm {2}}\). We take \( R \mathrel {=}\{( s _{1}, s _{2})\mid s _{2}\mathrel {=} l \mathord {.} get \;( s _{1})\}\) and proceed as follows:
We must now show that \( l _{0}\) and \( r _{0}\) are well-behaved (full) lenses, and that the projections \( fst \) and \( snd \) map \( sp \mathrel {=}( l _{0}, r _{0})\) to \( sp _{\mathrm {1}}\) and \( sp _{\mathrm {2}}\) respectively.
We first show that \( l _{0}\) is well-behaved; the reasoning for \( r _{0}\) is symmetric. For \(\mathsf {(MGetPut)}\) we have:
For \(\mathsf {(MPutGet)}\) we have:
Finally, for \(\mathsf {(MCreateGet)}\) we have:
Next, we show that \( fst \) is a base map from \( l _{0}\) to \( l _{1}\) and \( snd \) is a base map from \( l _{0}\) to \( l _{2}\). It is easy to show that \( fst \) is a base map from \( l _{0}\) to \( l _{1}\) by unfolding definitions and applying of monad laws. To show that \( snd \) is a base map from \( l _{0}\) to \( l _{2}\), we need to verify the following three equations that show that \( snd \) commutes with \( mget \), \( mput \) and \( mcreate \):
For the \( mget \) equation:
For the \( mput \) equation:
For the \( mcreate \) equation:
Similar reasoning suffices to show that \( fst \) is a base map from \( r _{0}\) to \( r _{1}\) and \( snd \) is a base map from \( r _{0}\) to \( r _{2}\), so we can conclude that \( R \) and \(( l , r )\) constitute a bisimulation between \( sp _{\mathrm {1}}\) and \( sp _{\mathrm {2}}\), that is, \( sp _{\mathrm {1}}\equiv _{\mathrm {b}} sp _{\mathrm {2}}\). \(\square \)
Theorem 4.10
Given , if \( sp _{\mathrm {1}}\equiv _{\mathrm {b}} sp _{\mathrm {2}}\) then \( sp _{\mathrm {1}}\equiv _{\mathrm {s}} sp _{\mathrm {2}}\).
\(\diamondsuit \)
Proof
For convenience, we again write \( sp _{\mathrm {1}}\mathrel {=}( l _{1}, r _{1})\) and \( sp _{\mathrm {2}}\mathrel {=}( l _{2}, r _{2})\). We are given \( R \) and a span constituting a bisimulation \( sp _{\mathrm {1}}\equiv _{\mathrm {b}} sp _{\mathrm {2}}\). Let \( sp _{\mathrm {0}}\mathrel {=}( l _{0}, r _{0})\). For later reference, we list the properties that must hold by virtue of this bisimulation for any \(( s _{1}, s _{2})\in R \):
In addition, it follows that:
which also implies the following identities, which we call twists:
It suffices to construct a span satisfying \( l \mathbin {;} l _{1}\mathrel {=} r \mathbin {;} l _{2}\) and \( l \mathbin {;} r _{1}\mathrel {=} r \mathbin {;} r _{2}\). Define \( l \) and \( r \) as follows:
Notice that by construction \( l \mathbin {{:}{:}} R \mathbin {\leadsto } S _{1}\) and \( r \mathbin {{:}{:}} R \mathbin {\leadsto } S _{2}\), that is, since we have used \( l _{0}\) and \( r _{0}\) to define \( l \) and \( r \), we do not need to do any more work to check that the pairs produced by \( create \) and \( put \) remain in \( R \). Notice also that \( l \) and \( r \) only use the lenses \( l _{1}\) and \( l _{2}\), not \( r _{1}\) and \( r _{2}\); we will show nevertheless that they satisfy the required properties.
First, to show that \( l \mathbin {;} l _{1}\mathrel {=} r \mathbin {;} l _{2}\), we proceed as follows for each operation. For \( get \):
For \( put \), we have:
Finally, for \( create \) we have:
Next, we show that \( l \mathbin {;} r _{1}\mathrel {=} r \mathbin {;} r _{2}\). For \( get \):
For \( put \), we have:
Finally, for \( create \) we have:
We must also show that \( l \) and \( r \) are well-behaved full lenses. To show that \( l \) is well-behaved, we proceed as follows. For \(\mathsf {(GetPut)}\):
For \(\mathsf {(PutGet)}\):
For \(\mathsf {(CreateGet)}\):
Finally, notice that \( l \) and \( r \) are defined symmetrically so essentially the same reasoning shows \( r \) is well-behaved.
To conclude, \( sp \mathrel {=}( l , r )\) constitutes a span of lenses witnessing that \( sp _{\mathrm {1}}\equiv _{\mathrm {s}} sp _{\mathrm {2}}\). \(\square \)
Rights and permissions
Copyright information
© 2016 Springer International Publishing Switzerland
About this chapter
Cite this chapter
Abou-Saleh, F., Cheney, J., Gibbons, J., McKinna, J., Stevens, P. (2016). Reflections on Monadic Lenses. In: Lindley, S., McBride, C., Trinder, P., Sannella, D. (eds) A List of Successes That Can Change the World. Lecture Notes in Computer Science(), vol 9600. Springer, Cham. https://doi.org/10.1007/978-3-319-30936-1_1
Download citation
DOI: https://doi.org/10.1007/978-3-319-30936-1_1
Published:
Publisher Name: Springer, Cham
Print ISBN: 978-3-319-30935-4
Online ISBN: 978-3-319-30936-1
eBook Packages: Computer ScienceComputer Science (R0)