Skip to main content

Reflections on Monadic Lenses

  • Chapter
  • First Online:
A List of Successes That Can Change the World

Part of the book series: Lecture Notes in Computer Science ((LNTCS,volume 9600))

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.

This is a preview of subscription content, log in via an institution to check access.

Access this chapter

eBook
USD 16.99
Price excludes VAT (USA)
  • Available as EPUB and PDF
  • Read on any device
  • Instant download
  • Own it forever
Softcover Book
USD 54.99
Price excludes VAT (USA)
  • Compact, lightweight edition
  • Dispatched in 3 to 5 business days
  • Free shipping worldwide - see info

Tax calculation will be finalised at checkout

Purchases are for personal use only

Institutional subscriptions

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)

    Google Scholar 

  • Abou-Saleh, F., McKinna, J., Gibbons, J.: Coalgebraic aspects of bidirectional computation. In: BX 2015, CEUR-WS, vol. 1396, pp. 15–30 (2015b)

    Google Scholar 

  • Cheney, J., McKinna, J., Stevens, P., Gibbons, J., Abou-Saleh, F.: Entangled state monads. In: Terwilliger and Hidaka (2014)

    Google Scholar 

  • 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)

    Article  Google Scholar 

  • 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)

    Chapter  Google Scholar 

  • Hofmann, M., Pierce, B.C., Wagner, D.: Symmetric lenses. In: POPL, pp. 371–384. ACM (2011)

    Google Scholar 

  • Johnson, M., Rosebrugh, R.: Spans of lenses. In: Terwilliger and Hidaka (2014)

    Google Scholar 

  • Jones, M.P., Duponcheel, L.: Composing monads. Technical report RR-1004, DCS, Yale (1993)

    Google Scholar 

  • King, D.J., Wadler, P.: Combining monads. In: Proceedings of the 1992 Glasgow Workshop on Functional Programming, pp. 134–143 (1992)

    Google Scholar 

  • Liang, S., Hudak, P., Jones, M.P.: Monad transformers and modular interpreters. In: POPL, pp. 333–343 (1995)

    Google Scholar 

  • 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)

    Chapter  Google Scholar 

  • Stevens, P.: Bidirectional model transformations in QVT: Semantic issues and open questions. SoSyM 9(1), 7–20 (2010)

    Google Scholar 

  • 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

    Article  MathSciNet  MATH  Google Scholar 

  • Wadler, P.: Monads for functional programming. In: Jeuring, J., Meijer, E. (eds.) AFP 1995. LNCS, vol. 925, pp. 24–52. Springer, Heidelberg (1995)

    Chapter  Google Scholar 

Download references

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

Authors

Corresponding author

Correspondence to James Cheney .

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)}\):

figure as

For \(\mathsf {(MPutGet)}\), the proof is as follows:

figure at

   \(\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.

figure au

For the second part, taking \( sl \mathrel {=} setBool \; True \mathbin {;} setBool \; False \), we proceed as follows:

figure av

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:

figure aw

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:

figure ax

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}}\).

figure ay

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,

figure az

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

figure ba

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:

figure bb

If \( mc \mathrel {=} Just \; c \) then we reason as follows:

figure bc

   \(\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:

figure bd

The proof that \( sl \mathord {.} mput _\mathrm {R}\;( a' , c' )\mathrel {=} return \;( b' , c' )\) is symmetric.

figure be

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:

figure bf

For \(\mathsf {(MPutGet)}\), we have:

figure bg

The proof for \(\mathsf {(MCreateGet)}\) is similar.

figure bh

   \(\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 \):

figure bi

For \( put \):

figure bj

Finally, for \( create \):

figure bk

   \(\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:

figure bl

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:

figure bm

For \(\mathsf {(MPutGet)}\) we have:

figure bn

Finally, for \(\mathsf {(MCreateGet)}\) we have:

figure bo

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 \):

figure bp

For the \( mget \) equation:

figure bq

For the \( mput \) equation:

figure br

For the \( mcreate \) equation:

figure bs

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 \):

figure bt

In addition, it follows that:

figure bu

which also implies the following identities, which we call twists:

figure bv

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:

figure bw

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 \):

figure bx

For \( put \), we have:

figure by

Finally, for \( create \) we have:

figure bz

Next, we show that \( l \mathbin {;} r _{1}\mathrel {=} r \mathbin {;} r _{2}\). For \( get \):

figure ca

For \( put \), we have:

figure cb

Finally, for \( create \) we have:

figure cc

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)}\):

figure cd

For \(\mathsf {(PutGet)}\):

figure ce

For \(\mathsf {(CreateGet)}\):

figure cf

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

Reprints 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)

Publish with us

Policies and ethics