-
Notifications
You must be signed in to change notification settings - Fork 0
/
lenslibraries.tex
129 lines (103 loc) · 2.69 KB
/
lenslibraries.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
\begin{frame}[fragile]
\frametitle{Lens Libraries}
% (|||) :: Lens a x -> Lens b x -> Lens (Either a b) x
% (***) :: Lens a b -> Lens c d -> Lens (a, c) (b, d)
% factor :: Lens (Either a b) (Either a c) -> Lens a (Either b c)
% distribute :: Lens a (Either b c) -> Lens (Either a b) (Either a c)
% unzip :: Lens s (a, b) -> (Lens s a, Lens s b)
% identity :: Lens a a
Not only does a Lens give rise to a Semigroupoid, but it also maps a set onto itself
\begin{block}{Identity Lens}
\begin{lstlisting}[language=haskell]
identityLens :: Lens a a
identityLens = Lens (const id) id
\end{lstlisting}
\end{block}
A Semigroupoid that has an identity element is called a \emph{Category}. Lens is a category.
\end{frame}
\begin{frame}[fragile]
\frametitle{Lens Libraries}
Two split lenses that map onto a value of the same element type produce a lens that can merge.
\begin{block}{Split Lens}
\begin{lstlisting}[language=haskell]
(|||) ::
Lens a x
-> Lens b x
-> Lens (Either a b) x
Lens s1 g1 ||| Lens s2 g2 =
Lens (either
(\a -> Left . s1 a)
(\b -> Right . s2 b))
(either g1 g2)
\end{lstlisting}
\end{block}
\end{frame}
\begin{frame}[fragile]
\frametitle{Lens Libraries}
Two disjoint lenses can be paired.
\begin{block}{Lens Tensor}
\begin{lstlisting}[language=haskell]
(***) ::
Lens a b
-> Lens c d
-> Lens (a, c) (b, d)
Lens s1 g1 *** Lens s2 g2 =
Lens (\(a, c) (b, d) -> (s1 a b, s2 c d))
(\(a, c) -> (g1 a, g2 c))
\end{lstlisting}
\end{block}
\end{frame}
\begin{frame}[fragile]
\frametitle{Lens Libraries}
\begin{block}{And More}
\begin{itemize}
\item \begin{lstlisting}[language=haskell]
unzip ::
Lens s (a, b)
-> (Lens s a, Lens s b)
\end{lstlisting}
\item \begin{lstlisting}[language=haskell]
factor ::
Lens (Either (a, b) (a, c))
(a, Either b c)
\end{lstlisting}
\item \begin{lstlisting}[language=haskell]
distribute ::
Lens (a, Either b c)
(Either (a, b) (a, c))
\end{lstlisting}
\end{itemize}
\end{block}
\end{frame}
\begin{frame}[fragile]
\frametitle{Lens Values}
\begin{block}{First Lens}
\begin{lstlisting}[language=haskell]
fstLens :: Lens (a, b) a
fstLens =
Lens (\(_, b) a -> (a, b)) fst
\end{lstlisting}
\end{block}
\begin{block}{Second Lens}
\begin{lstlisting}[language=haskell]
sndLens :: Lens (a, b) b
sndLens =
Lens (\(a, _) b -> (a, b)) snd
\end{lstlisting}
\end{block}
\end{frame}
\begin{frame}[fragile]
\frametitle{Lens Values}
\begin{block}{Lenses on collections}
\begin{itemize}
\item \begin{lstlisting}[language=haskell]
mapLens ::
Ord k => k -> Lens (Map k v) (Maybe v)
\end{lstlisting}
\item \begin{lstlisting}[language=haskell]
setLens ::
Ord a => a -> Lens (Set a) Bool
\end{lstlisting}
\end{itemize}
\end{block}
\end{frame}