Optics
https://gyazo.com/d29d91eebe317383282754fe075491ce
Overview
Defining Iso, Lens, Prism, Traversal as type synonym allows a form of subtyping
Lens and Prism are subtypes of Traversal
Iso is a subtype of Lens, Prism, Traversal
This subtyping allows us to compose with each other easily
Lenses and traversals can be composed with each other. Composing two lenses gives a lens again.
Composing two traversals or a traversal and a lens gives a traversal.
Effectful updates (IO) are also possible (and baked right into the definition).
lens package provides us these functions These lenses are called van Laarhoven lenses
Definition
code:optics.hs
type Traversal s t a b = forall f . Applicative f =>(a -> f b) -> s -> f t
type Lens s t a b = forall f. Functor f => (a -> f b) -> s -> f t
type Prism s t a b = forall f p . (Applicative f, Choice p) => p a (f b) -> p s (f t)
type Iso s t a b = forall f p.(Functor f,Choice p) => p a (f b) -> p s (f t)
Optics
Useful links