In this chapter, you will learn about a particular type of functional reference. First, let’s see define functional reference: reference means you can access and/or modify part of the values, and functional means that the flexibility and composability of functions are assured while accessing these parts.
Access a subpart
Alter the whole by modifying a subpart
Merge the lens with another lens to get a deeper view
Get-put : If something is modified by changing just the subpart, then nothing happens.
Put-get : When a particular subpart is inserted and you want to check the whole result, you will get exactly that subpart.
Put-put : If subpart a is inserted, then a is modified by inserting subpart b, and this is actually the same as just inserting b.
If these rules sound a bit odd, follow this chapter and things will become clearer.
Lens's a: When type s always contains type a, Lens s a is used to get or set the a inside of s. This is characterized as a has-a relationship.
Prism's a: When type s could contain type a, Prism s a is used to extract a if it exists; also, given the a, it may create the s. This is characterized as an is-a relationship.
Traversal's a : This finds as many a’s as can be contained in s.
Iso's a : This shows that s and a are representations of the same type.
In the previous list, the ' mark means that the lens is a simpler version of the main lens.
These examples are self-explanatory. Maybe over is a little more complex: the alteration is applied on the focal point _1. The three functions have an infix form: view as (^.), set as (.~), over as (%~).
The mathematical operators can be applied as lenses, in the following forms: (+~), (-~), (*~), (<>~).
Note that simpleTransactions has the same signature as traverse . The previous function can be successfully replaced by traverse. Moreover, it is actually recommended to use traverse instead to define your own versions.
The existence of i :: Iso's a says that having the value s, you also have the value a and the inverse. Based on this, the important functions in Iso are view i :: s -> a and review i :: a -> s. Both of them succeed and have no loss.
The existence of l :: Lens's a says that having the value s, you have also the value a, but the inverse way is not possible. It is possible that the function view l :: s -> a can eliminate some information in its way, because it is not guaranteed that a conversion is lossless. So, having the a, you can’t go backward to s.
Finally, the existence of p :: Prism's a says that having the value s, it is possible to also have the value a, but this fact is not guaranteed. Even when converting with preview p :: s -> Maybe a is possible to fail, you still have the inverse of review p :: a -> s.
Summary
What lenses are
When lenses can be used and which are the main functions for every type of lens
How you can obtain the other lenses from Iso
Note that lenses are analogous to structures in other programming languages, such as C.
References
- 1.
S. Fischer, H. Zhenjiang, and H. Pacheco, “A Clear Picture of Lens Laws,” International Conference on Mathematics of Program Construction (Springer, 2015)
- 2.
R. O’Connor, “Functor Is to Lens as Applicative Is to Biplate: Introducing Multiplate,” arXiv preprint arXiv:1103.2841 (2011)
- 3.
Lens: lenses, folds and traversals, http://hackage.haskell.org/package/lens
- 4.
Haskell/lenses and functional references, https://en.wikibooks.org/wiki/Haskell/Lenses_and_functional_references
- 5.
A little lens starter tutorial, https://www.schoolofhaskell.com/school/to-infinity-and-beyond/pick-of-the-week/a-little-lens-starter-tutorial
- 6.
Writing traversals, https://lens-by-example.chrispenner.ca/articles/traversals/writing-traversals
- 7.
Control.Lens.Tutorial, http://hackage.haskell.org/package/lens-tutorial-1.0.3/docs/Control-Lens-Tutorial.html