-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcomonad.tex
199 lines (179 loc) · 4.02 KB
/
comonad.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
\begin{frame}
\begin{center}
Speaking of useful operations\ldots
\end{center}
\end{frame}
\begin{frame}
\begin{block}{Comonad is}
Any functor \lstinline{F} supporting:
\begin{center}
\begin{itemize}
\item<1-> \lstinline{extract :: F a -> a}
\item<2-> \lstinline{duplicate :: F a -> F (F a)}
\item<3-> \emph{satisfying laws of identity and associativity}
\end{itemize}
\end{center}
\end{block}
\end{frame}
\begin{frame}
\begin{center}
Does a \lstinline{ListZipper} satisfy the requirements for a comonad?
\end{center}
\end{frame}
\begin{frame}[fragile]
\begin{block}{Yes}
\begin{center}
\begin{lstlisting}[style=haskell]
fmap :: (a -> b) -> ListZipper a -> ListZipper b
extract :: ListZipper x -> x
duplicate :: ListZipper w -> ListZipper (ListZipper w)
\end{lstlisting}
\end{center}
\end{block}
\end{frame}
\begin{frame}
\begin{center}
What about a \lstinline{TreeZipper}?
\end{center}
\end{frame}
\begin{frame}[fragile]
\begin{block}{Yes}
\begin{center}
\begin{lstlisting}[style=haskell]
fmap :: (a -> b) -> TreeZipper a -> TreeZipper b
extract :: TreeZipper x -> x
duplicate :: TreeZipper w -> TreeZipper (TreeZipper w)
\end{lstlisting}
\end{center}
\end{block}
\end{frame}
\begin{frame}[fragile]
\begin{block}{Actually}
\begin{center}
\textbf{All} zippers are comonads \emph{(Uustalu, 2005)}
\end{center}
\end{block}
\end{frame}
\begin{frame}[fragile]
\begin{block}{Here is a utility function}
\begin{center}
\begin{lstlisting}[style=haskell]
-- Do any (max: 2) adjacent focii of the list zipper
-- satisfy the given predicate?
adjacentFociiSatisfy ::
(a -> Bool)
-> ListZipper a
-> Bool
adjacentFociiSatisfy p z =
let mvs k = any p (focus <$> k z)
in mvs moveLeft || mvs moveRight
\end{lstlisting}
\end{center}
\end{block}
\end{frame}
\begin{frame}[fragile]
\begin{block}{Requirement}
\begin{center}
Find all zippers with a focus adjacent to a given value
\end{center}
\end{block}
\end{frame}
\begin{frame}[fragile]
\begin{block}{Find all zippers with a focus adjacent to a given value}
\begin{itemize}
\item<1-> \lstinline{duplicate}
we now have \lstinline{ListZipper (ListZipper a)}
\item<2-> \lstinline{toList}
we now have \lstinline{[ListZipper a]}
\item<3-> \lstinline{filter} with \lstinline{adjacentFociiSatisfy}
we still have \lstinline{[ListZipper a]}
\end{itemize}
\end{block}
\end{frame}
\begin{frame}[fragile]
\begin{block}{\lstinline{allWithAdjacent}}
\begin{center}
\begin{lstlisting}[style=haskell]
allWithAdjacent ::
Eq a =>
a
-> ListZipper a
-> [ListZipper a]
allWithAdjacent n =
filter (adjacentFociiSatisfy (==n)) . toList . duplicate
\end{lstlisting}
\end{center}
\end{block}
\end{frame}
\begin{frame}[fragile]
\begin{block}{\lstinline{allWithAdjacent}}
\begin{center}
\begin{lstlisting}[style=haskell]
> allWithAdjacent 3 (ListZipper [3,2,1] 4 [5..10])
[
ListZipper {
lefts = [1]
, focus = 2
, rights = [3,4,5,6,7,8,9,10]
}
, ListZipper {
lefts = [3,2,1]
, focus = 4
, rights = [5,6,7,8,9,10]
}
]
\end{lstlisting}
\end{center}
\end{block}
\end{frame}
\begin{frame}[fragile]
\begin{block}{\lstinline{allWithAdjacent}}
\begin{center}
\begin{lstlisting}[style=haskell]
> allWithAdjacent 3 (ListZipper [3,2,1] 4 [7,2,6,3])
[
ListZipper {
lefts = [1]
, focus = 2
, rights = [3,4,7,2,6,3]
}
, ListZipper {
lefts = [3,2,1]
, focus = 4
, rights = [7,2,6,3]
}
, ListZipper {
lefts = [2,7,4,3,2,1]
, focus = 6
, rights = [3]
}
]
\end{lstlisting}
\end{center}
\end{block}
\end{frame}
\begin{frame}
\begin{center}
Who's wanted that in their text editor before?
\end{center}
\end{frame}
\begin{frame}
\begin{center}
What were you editing? JSON? Your pilot logbook?
\end{center}
\end{frame}
\begin{frame}
\begin{center}
What about \textbf{a programming language}?
\end{center}
\end{frame}
% \begin{frame}
% \begin{center}
% I'll leave \lstinline{TreeZipper} comonad operations to your imagination
% \end{center}
% \end{frame}
\begin{frame}
\begin{block}{other uses of zippers}
\includegraphics[width=1.00\textheight]{image/xmonad.jpg}
\end{block}
\end{frame}