-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathslides3.html
398 lines (386 loc) · 37.4 KB
/
slides3.html
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="generator" content="pandoc">
<meta name="author" content="Jesper Cockx">
<meta name="dcterms.date" content="2019-09-02">
<title>Correct-by-construction programming in Agda</title>
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, minimal-ui">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/reveal.js/3.7.0//css/reveal.css">
<style type="text/css">
code{white-space: pre-wrap;}
span.smallcaps{font-variant: small-caps;}
span.underline{text-decoration: underline;}
div.column{display: inline-block; vertical-align: top; width: 50%;}
</style>
<style type="text/css">
a.sourceLine { display: inline-block; line-height: 1.25; }
a.sourceLine { pointer-events: none; color: inherit; text-decoration: inherit; }
a.sourceLine:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode { white-space: pre; position: relative; }
div.sourceCode { margin: 1em 0; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
code.sourceCode { white-space: pre-wrap; }
a.sourceLine { text-indent: -1em; padding-left: 1em; }
}
pre.numberSource a.sourceLine
{ position: relative; left: -4em; }
pre.numberSource a.sourceLine::before
{ content: attr(data-line-number);
position: relative; left: -1em; text-align: right; vertical-align: baseline;
border: none; pointer-events: all; display: inline-block;
-webkit-touch-callout: none; -webkit-user-select: none;
-khtml-user-select: none; -moz-user-select: none;
-ms-user-select: none; user-select: none;
padding: 0 4px; width: 4em;
color: #aaaaaa;
}
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa; padding-left: 4px; }
div.sourceCode
{ }
@media screen {
a.sourceLine::before { text-decoration: underline; }
}
code span.al { color: #ff0000; font-weight: bold; } /* Alert */
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
code span.at { color: #7d9029; } /* Attribute */
code span.bn { color: #40a070; } /* BaseN */
code span.bu { } /* BuiltIn */
code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
code span.ch { color: #4070a0; } /* Char */
code span.cn { color: #880000; } /* Constant */
code span.co { color: #60a0b0; font-style: italic; } /* Comment */
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
code span.do { color: #ba2121; font-style: italic; } /* Documentation */
code span.dt { color: #902000; } /* DataType */
code span.dv { color: #40a070; } /* DecVal */
code span.er { color: #ff0000; font-weight: bold; } /* Error */
code span.ex { } /* Extension */
code span.fl { color: #40a070; } /* Float */
code span.fu { color: #06287e; } /* Function */
code span.im { } /* Import */
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
code span.kw { color: #007020; font-weight: bold; } /* Keyword */
code span.op { color: #666666; } /* Operator */
code span.ot { color: #007020; } /* Other */
code span.pp { color: #bc7a00; } /* Preprocessor */
code span.sc { color: #4070a0; } /* SpecialChar */
code span.ss { color: #bb6688; } /* SpecialString */
code span.st { color: #4070a0; } /* String */
code span.va { color: #19177c; } /* Variable */
code span.vs { color: #4070a0; } /* VerbatimString */
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
</style>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/reveal.js/3.7.0//css/theme/black.css" id="theme">
<link rel="stylesheet" href="white.css"/>
<link rel="stylesheet" href="Agda.css"/>
<link rel="stylesheet" href="patchTheme.css"/>
<!-- Printing and PDF exports -->
<script>
var link = document.createElement( 'link' );
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = window.location.search.match( /print-pdf/gi ) ? 'https://cdnjs.cloudflare.com/ajax/libs/reveal.js/3.7.0//css/print/pdf.css' : 'https://cdnjs.cloudflare.com/ajax/libs/reveal.js/3.7.0//css/print/paper.css';
document.getElementsByTagName( 'head' )[0].appendChild( link );
</script>
<!--[if lt IE 9]>
<script src="https://cdnjs.cloudflare.com/ajax/libs/reveal.js/3.7.0//lib/js/html5shiv.js"></script>
<![endif]-->
</head>
<body>
<div class="reveal">
<div class="slides">
<section id="title-slide">
<h1 class="title">Correct-by-construction programming in Agda</h1>
<p class="subtitle">Lecture 3: Side effects, type classes, and monads</p>
<p class="author">Jesper Cockx</p>
<p class="date">2 September 2019</p>
</section>
<section><section id="type-classes" class="title-slide slide level1"><h1>Type classes</h1></section><section id="what-is-a-type-class" class="slide level2">
<h2>What is a type class?</h2>
<p>A type class offers one or more functions for a <strong>generic</strong> type. <strong>Examples</strong>:</p>
<ul>
<li><code>Print A</code>:
<ul>
<li><code>print : A → String</code></li>
</ul></li>
<li><code>Monoid M</code> type class:
<ul>
<li><code>∅ : M</code></li>
<li><code>_+_ : M → M → M</code></li>
</ul></li>
<li><code>Eq A</code>:
<ul>
<li><code>_==_ : A → A → Bool</code> and/or <code>_≟_ : (x y : A) -> Dec (x ≡ y)</code></li>
</ul></li>
<li><code>Functor F</code>:
<ul>
<li><code>fmap : {A B : Set} → F A → F B</code></li>
</ul></li>
</ul>
</section><section id="parametric-vs-ad-hoc-overloading" class="slide level2">
<h2>Parametric vs ad-hoc overloading</h2>
<p>Why not have <code>print : {A : Set} -> A -> String</code>?</p>
<div class="fragment">
<p>Because of <em>parametricity</em>, <code>print</code> would have to be a constant function.</p>
<p>Type classes allow a <em>different</em> implementation at each type!</p>
</div>
</section><section id="type-classes-in-agda" class="slide level2">
<h2>Type classes in Agda</h2>
<p>A type class is implemented by using a <strong>record type</strong> + <strong>instance arguments</strong></p>
<ul>
<li>Record type: a <strong>dictionary</strong> holding implementation of each function for a specific type</li>
<li>Instance arguments: <em>automatically</em> pick the ‘right’ dictionary for the given type</li>
</ul>
</section><section id="instance-arguments" class="slide level2">
<h2>Instance arguments</h2>
<p><em>Instance arguments</em> are Agda’s builtin mechanism for ad-hoc overloading (~ type classes in Haskell).</p>
<p>Syntax:</p>
<ul>
<li>Using an instance: <code>{{x : A}} → ...</code></li>
<li>Defining new instances: <code>instance ...</code></li>
</ul>
<p>When using a function of type <code>{{x : A}} → B</code>, Agda will automatically resolve the argument if there is a <strong>unique</strong> instance of the right type in scope.</p>
</section><section id="defining-a-typeclass-with-instance-arguments" class="slide level2">
<h2>Defining a typeclass with instance arguments</h2>
<!--
<pre class="Agda"><a id="1555" class="Keyword">module</a> <a id="1562" href="slides3.html" class="Module">_</a> <a id="1564" class="Keyword">where</a>
<a id="1570" class="Keyword">open</a> <a id="1575" class="Keyword">import</a> <a id="1582" href="Data.Bool.Base.html" class="Module">Data.Bool.Base</a>
<a id="1597" class="Keyword">open</a> <a id="1602" class="Keyword">import</a> <a id="1609" href="Data.String.Base.html" class="Module">Data.String.Base</a>
</pre>-->
<pre class="Agda"><a id="1643" class="Keyword">record</a> <a id="Print"></a><a id="1650" href="slides3.html#1650" class="Record">Print</a> <a id="1656" class="Symbol">{</a><a id="1657" href="slides3.html#1657" class="Bound">ℓ</a><a id="1658" class="Symbol">}</a> <a id="1660" class="Symbol">(</a><a id="1661" href="slides3.html#1661" class="Bound">A</a> <a id="1663" class="Symbol">:</a> <a id="1665" class="PrimitiveType">Set</a> <a id="1669" href="slides3.html#1657" class="Bound">ℓ</a><a id="1670" class="Symbol">)</a> <a id="1672" class="Symbol">:</a> <a id="1674" class="PrimitiveType">Set</a> <a id="1678" href="slides3.html#1657" class="Bound">ℓ</a> <a id="1680" class="Keyword">where</a>
<a id="1688" class="Keyword">field</a>
<a id="Print.print"></a><a id="1698" href="slides3.html#1698" class="Field">print</a> <a id="1704" class="Symbol">:</a> <a id="1706" href="slides3.html#1661" class="Bound">A</a> <a id="1708" class="Symbol">→</a> <a id="1710" href="Agda.Builtin.String.html#247" class="Postulate">String</a>
<a id="1717" class="Keyword">open</a> <a id="1722" href="slides3.html#1650" class="Module">Print</a> <a id="1728" class="Symbol">{{...}}</a> <a id="1737" class="Comment">-- print : {{r : Print A}} → A → String</a>
<a id="1778" class="Keyword">instance</a>
<a id="PrintBool"></a><a id="1789" href="slides3.html#1789" class="Function">PrintBool</a> <a id="1799" class="Symbol">:</a> <a id="1801" href="slides3.html#1650" class="Record">Print</a> <a id="1807" href="Agda.Builtin.Bool.html#135" class="Datatype">Bool</a>
<a id="1814" href="slides3.html#1698" class="Field">print</a> <a id="1820" class="Symbol">{{</a><a id="1822" href="slides3.html#1789" class="Function">PrintBool</a><a id="1831" class="Symbol">}}</a> <a id="1834" href="Agda.Builtin.Bool.html#160" class="InductiveConstructor">true</a> <a id="1840" class="Symbol">=</a> <a id="1842" class="String">"true"</a>
<a id="1851" href="slides3.html#1698" class="Field">print</a> <a id="1857" class="Symbol">{{</a><a id="1859" href="slides3.html#1789" class="Function">PrintBool</a><a id="1868" class="Symbol">}}</a> <a id="1871" href="Agda.Builtin.Bool.html#154" class="InductiveConstructor">false</a> <a id="1877" class="Symbol">=</a> <a id="1879" class="String">"false"</a>
<a id="PrintString"></a><a id="1890" href="slides3.html#1890" class="Function">PrintString</a> <a id="1902" class="Symbol">:</a> <a id="1904" href="slides3.html#1650" class="Record">Print</a> <a id="1910" href="Agda.Builtin.String.html#247" class="Postulate">String</a>
<a id="1919" href="slides3.html#1698" class="Field">print</a> <a id="1925" class="Symbol">{{</a><a id="1927" href="slides3.html#1890" class="Function">PrintString</a><a id="1938" class="Symbol">}}</a> <a id="1941" href="slides3.html#1941" class="Bound">x</a> <a id="1943" class="Symbol">=</a> <a id="1945" href="slides3.html#1941" class="Bound">x</a>
<a id="testPrint"></a><a id="1948" href="slides3.html#1948" class="Function">testPrint</a> <a id="1958" class="Symbol">:</a> <a id="1960" href="Agda.Builtin.String.html#247" class="Postulate">String</a>
<a id="1967" href="slides3.html#1948" class="Function">testPrint</a> <a id="1977" class="Symbol">=</a> <a id="1979" class="Symbol">(</a><a id="1980" href="slides3.html#1698" class="Field">print</a> <a id="1986" href="Agda.Builtin.Bool.html#160" class="InductiveConstructor">true</a><a id="1990" class="Symbol">)</a> <a id="1992" href="Data.String.Base.html#1744" class="Function Operator">++</a> <a id="1995" class="Symbol">(</a><a id="1996" href="slides3.html#1698" class="Field">print</a> <a id="2002" class="String">"a string"</a><a id="2012" class="Symbol">)</a>
</pre>
</section></section>
<section><section id="monads" class="title-slide slide level1"><h1>Monads</h1></section><section id="section" class="slide level2">
<h2></h2>
<p>“To be is to do” –Socrates</p>
<p>“To do is to be” –Sartre</p>
<p>“Do be do be do” –Sinatra.</p>
</section><section id="side-effects-in-a-pure-language" class="slide level2">
<h2>Side effects in a pure language</h2>
<p>Agda is a <strong>pure</strong> language: functions have no side effects</p>
<p>But a typechecker has many side effects:</p>
<ul>
<li>raise error messages</li>
<li>read or write files</li>
<li>maintain a state for declared variables</li>
</ul>
</section><section id="monads-1" class="slide level2">
<h2>Monads</h2>
<p><code>Monad</code> is a typeclass with two fields <code>return</code> and <code>_>>=_</code>.</p>
<p><code>M A</code> ~ “computations that may have some side-effects (depending on M) and return an A”</p>
<p>Examples: <code>Maybe</code>, <code>Reader</code>, <code>Error</code>, <code>State</code>, …</p>
<p>See <a href="https://jespercockx.github.io/ohrid19-agda/src/html/Library.Error.html">Library/Error.agda</a></p>
</section><section id="do-notation" class="slide level2">
<h2><code>do</code> notation</h2>
<p><code>do</code> is syntactic sugar for repeated binds: instead of</p>
<!--
<pre class="Agda"><a id="2740" class="Keyword">open</a> <a id="2745" class="Keyword">import</a> <a id="2752" href="Data.Vec.html" class="Module">Data.Vec</a> <a id="2761" class="Keyword">using</a> <a id="2767" class="Symbol">(</a><a id="2768" href="Data.Vec.Base.html#935" class="Datatype">Vec</a><a id="2771" class="Symbol">)</a>
<a id="2773" class="Keyword">open</a> <a id="2778" class="Keyword">import</a> <a id="2785" href="Library.html" class="Module">Library</a> <a id="2793" class="Keyword">hiding</a> <a id="2800" class="Symbol">(</a><a id="2801" href="Data.List.Relation.Unary.All.html#1176" class="Datatype">All</a><a id="2804" class="Symbol">;</a> <a id="2806" href="Library.Monad.html#867" class="Record">IMonad</a><a id="2812" class="Symbol">;</a> <a id="2814" href="Library.Monad.html#1110" class="Function">return</a><a id="2820" class="Symbol">;</a> <a id="2822" href="Library.Monad.html#1002" class="Field Operator">_>>=_</a><a id="2827" class="Symbol">)</a>
<a id="2829" class="Keyword">module</a> <a id="2836" href="slides3.html#2836" class="Module">_</a> <a id="2838" class="Keyword">where</a>
<a id="2846" class="Keyword">open</a> <a id="2851" class="Keyword">import</a> <a id="2858" href="Library.html" class="Module">Library</a> <a id="2866" class="Keyword">hiding</a> <a id="2873" class="Symbol">(</a><a id="2874" href="Data.List.Relation.Unary.All.html#1176" class="Datatype">All</a><a id="2877" class="Symbol">;</a> <a id="2879" href="Library.Monad.html#867" class="Record">IMonad</a><a id="2885" class="Symbol">)</a>
</pre>-->
<pre class="Agda"> <a id="2902" href="slides3.html#2902" class="Function">_</a> <a id="2904" class="Symbol">:</a> <a id="2906" href="Data.Maybe.Base.html#795" class="Datatype">Maybe</a> <a id="2912" href="Agda.Builtin.Int.html#219" class="Datatype">ℤ</a>
<a id="2916" class="Symbol">_</a> <a id="2918" class="Symbol">=</a> <a id="2920" class="Symbol">(</a><a id="2921" href="Data.Maybe.Base.html#829" class="InductiveConstructor">just</a> <a id="2926" class="Symbol">(</a><a id="2927" href="Agda.Builtin.Int.html#264" class="InductiveConstructor Operator">-[1+</a> <a id="2932" class="Number">3</a> <a id="2934" href="Agda.Builtin.Int.html#264" class="InductiveConstructor Operator">]</a><a id="2935" class="Symbol">))</a> <a id="2938" href="Library.Monad.html#1002" class="Field Operator">>>=</a> <a id="2942" class="Symbol">λ</a> <a id="2944" href="slides3.html#2944" class="Bound">x</a> <a id="2946" class="Symbol">→</a>
<a id="2954" class="Symbol">(</a><a id="2955" href="Data.Maybe.Base.html#829" class="InductiveConstructor">just</a> <a id="2960" class="Symbol">(</a><a id="2961" href="Agda.Builtin.Int.html#237" class="InductiveConstructor Operator">+</a> <a id="2963" class="Number">5</a><a id="2964" class="Symbol">)</a> <a id="2970" class="Symbol">)</a> <a id="2972" href="Library.Monad.html#1002" class="Field Operator">>>=</a> <a id="2976" class="Symbol">λ</a> <a id="2978" href="slides3.html#2978" class="Bound">y</a> <a id="2980" class="Symbol">→</a>
<a id="2988" href="Library.Monad.html#1110" class="Function">return</a> <a id="2995" class="Symbol">(</a><a id="2996" href="slides3.html#2944" class="Bound">x</a> <a id="2998" href="Data.Integer.Base.html#2950" class="Function Operator">+</a> <a id="3000" href="slides3.html#2978" class="Bound">y</a><a id="3001" class="Symbol">)</a>
</pre>
you can write:
<pre class="Agda"> <a id="3028" href="slides3.html#3028" class="Function">_</a> <a id="3030" class="Symbol">:</a> <a id="3032" href="Data.Maybe.Base.html#795" class="Datatype">Maybe</a> <a id="3038" href="Agda.Builtin.Int.html#219" class="Datatype">ℤ</a>
<a id="3042" class="Symbol">_</a> <a id="3044" class="Symbol">=</a> <a id="3046" class="Keyword">do</a>
<a id="3053" href="slides3.html#3053" class="Bound">x</a> <a id="3055" href="Library.Monad.html#1002" class="Field Operator">←</a> <a id="3057" href="Data.Maybe.Base.html#829" class="InductiveConstructor">just</a> <a id="3062" class="Symbol">(</a><a id="3063" href="Agda.Builtin.Int.html#264" class="InductiveConstructor Operator">-[1+</a> <a id="3068" class="Number">3</a> <a id="3070" href="Agda.Builtin.Int.html#264" class="InductiveConstructor Operator">]</a><a id="3071" class="Symbol">)</a>
<a id="3077" href="slides3.html#3077" class="Bound">y</a> <a id="3079" href="Library.Monad.html#1002" class="Field Operator">←</a> <a id="3081" href="Data.Maybe.Base.html#829" class="InductiveConstructor">just</a> <a id="3086" class="Symbol">(</a><a id="3087" href="Agda.Builtin.Int.html#237" class="InductiveConstructor Operator">+</a> <a id="3089" class="Number">5</a><a id="3090" class="Symbol">)</a>
<a id="3096" href="Library.Monad.html#1110" class="Function">return</a> <a id="3103" class="Symbol">(</a><a id="3104" href="slides3.html#3053" class="Bound">x</a> <a id="3106" href="Data.Integer.Base.html#2950" class="Function Operator">+</a> <a id="3108" href="slides3.html#3077" class="Bound">y</a><a id="3109" class="Symbol">)</a>
</pre>
</section><section id="pattern-matching-with-do" class="slide level2">
<h2>Pattern matching with <code>do</code></h2>
<p>There can be a <em>pattern</em> to the left of a <code>←</code>, alternative cases can be handled in a local <code>where</code></p>
<pre class="Agda"> <a id="3253" href="slides3.html#3253" class="Function">pred</a> <a id="3258" class="Symbol">:</a> <a id="3260" href="Agda.Builtin.Nat.html#165" class="Datatype">ℕ</a> <a id="3262" class="Symbol">→</a> <a id="3264" href="Data.Maybe.Base.html#795" class="Datatype">Maybe</a> <a id="3270" href="Agda.Builtin.Nat.html#165" class="Datatype">ℕ</a>
<a id="3274" href="slides3.html#3253" class="Function">pred</a> <a id="3279" href="slides3.html#3279" class="Bound">n</a> <a id="3281" class="Symbol">=</a> <a id="3283" class="Keyword">do</a>
<a id="3290" href="Agda.Builtin.Nat.html#196" class="InductiveConstructor">suc</a> <a id="3294" href="slides3.html#3294" class="Bound">m</a> <a id="3296" href="Library.Monad.html#1002" class="Field Operator">←</a> <a id="3298" href="Data.Maybe.Base.html#829" class="InductiveConstructor">just</a> <a id="3303" href="slides3.html#3279" class="Bound">n</a>
<a id="3311" class="Keyword">where</a> <a id="3317" href="Agda.Builtin.Nat.html#183" class="InductiveConstructor">zero</a> <a id="3322" class="Symbol">→</a> <a id="3324" href="Data.Maybe.Base.html#859" class="InductiveConstructor">nothing</a>
<a id="3336" href="Library.Monad.html#1110" class="Function">return</a> <a id="3343" href="slides3.html#3294" class="Bound">m</a>
</pre>
</section><section id="dependent-pattern-matching-with-do" class="slide level2">
<h2>Dependent pattern matching with <code>do</code></h2>
<pre class="Agda"> <a id="3397" class="Keyword">postulate</a>
<a id="3411" href="slides3.html#3411" class="Postulate">test</a> <a id="3416" class="Symbol">:</a> <a id="3418" class="Symbol">(</a><a id="3419" href="slides3.html#3419" class="Bound">m</a> <a id="3421" href="slides3.html#3421" class="Bound">n</a> <a id="3423" class="Symbol">:</a> <a id="3425" href="Agda.Builtin.Nat.html#165" class="Datatype">ℕ</a><a id="3426" class="Symbol">)</a> <a id="3428" class="Symbol">→</a> <a id="3430" href="Data.Maybe.Base.html#795" class="Datatype">Maybe</a> <a id="3436" class="Symbol">(</a><a id="3437" href="slides3.html#3419" class="Bound">m</a> <a id="3439" href="Agda.Builtin.Equality.html#125" class="Datatype Operator">≡</a> <a id="3441" href="slides3.html#3421" class="Bound">n</a><a id="3442" class="Symbol">)</a>
<a id="3447" href="slides3.html#3447" class="Function">cast</a> <a id="3452" class="Symbol">:</a> <a id="3454" class="Symbol">(</a><a id="3455" href="slides3.html#3455" class="Bound">m</a> <a id="3457" href="slides3.html#3457" class="Bound">n</a> <a id="3459" class="Symbol">:</a> <a id="3461" href="Agda.Builtin.Nat.html#165" class="Datatype">ℕ</a><a id="3462" class="Symbol">)</a> <a id="3464" class="Symbol">→</a> <a id="3466" href="Data.Vec.Base.html#935" class="Datatype">Vec</a> <a id="3470" href="Agda.Builtin.Int.html#219" class="Datatype">ℤ</a> <a id="3472" href="slides3.html#3455" class="Bound">m</a> <a id="3474" class="Symbol">→</a> <a id="3476" href="Data.Maybe.Base.html#795" class="Datatype">Maybe</a> <a id="3482" class="Symbol">(</a><a id="3483" href="Data.Vec.Base.html#935" class="Datatype">Vec</a> <a id="3487" href="Agda.Builtin.Int.html#219" class="Datatype">ℤ</a> <a id="3489" href="slides3.html#3457" class="Bound">n</a><a id="3490" class="Symbol">)</a>
<a id="3494" href="slides3.html#3447" class="Function">cast</a> <a id="3499" href="slides3.html#3499" class="Bound">m</a> <a id="3501" href="slides3.html#3501" class="Bound">n</a> <a id="3503" href="slides3.html#3503" class="Bound">xs</a> <a id="3506" class="Symbol">=</a> <a id="3508" class="Keyword">do</a>
<a id="3515" href="Agda.Builtin.Equality.html#182" class="InductiveConstructor">refl</a> <a id="3520" href="Library.Monad.html#1002" class="Field Operator">←</a> <a id="3522" href="slides3.html#3411" class="Postulate">test</a> <a id="3527" href="slides3.html#3499" class="Bound">m</a> <a id="3529" href="slides3.html#3501" class="Bound">n</a>
<a id="3535" href="Library.Monad.html#1110" class="Function">return</a> <a id="3542" href="slides3.html#3503" class="Bound">xs</a>
</pre>
<p>Pattern matching allows typechecker to learn new facts!</p>
</section><section id="type-checking-expressions" class="slide level2">
<h2>Type-checking expressions</h2>
<p>See <a href="https://jespercockx.github.io/ohrid19-agda/src/html/V1/V1.TypeChecker.html">V1/TypeChecker.agda</a>.</p>
<p>Exercise: extend the typechecker with rules for the new syntactic constructions you added before</p>
</section></section>
<section><section id="indexed-monads" class="title-slide slide level1"><h1>Indexed monads</h1></section><section id="typechecking-variable-declarations" class="slide level2">
<h2>Typechecking variable declarations</h2>
<p>For type-checking variables, we need the following side-effects:</p>
<ul>
<li>For checking <em>expressions</em>: find variable with given name (⇒ read-only access)</li>
<li>For checking <em>declarations</em>: add new variable with given name (⇒ read-write access)</li>
</ul>
<p>How to ensure <strong>statically</strong> that each variable in scope has a name?</p>
</section><section id="reminder-the-all-type" class="slide level2">
<h2>Reminder: The <code>All</code> type</h2>
For <code>P : A → Set</code> and <code>xs : List A</code>, <code>All P xs</code> associates an element of <code>P x</code> to each <code>x ∈ xs</code>:
<pre class="Agda"><a id="4330" class="Keyword">data</a> <a id="All"></a><a id="4335" href="slides3.html#4335" class="Datatype">All</a> <a id="4339" class="Symbol">{</a><a id="4340" href="slides3.html#4340" class="Bound">A</a> <a id="4342" class="Symbol">:</a> <a id="4344" class="PrimitiveType">Set</a><a id="4347" class="Symbol">}</a> <a id="4349" class="Symbol">(</a><a id="4350" href="slides3.html#4350" class="Bound">P</a> <a id="4352" class="Symbol">:</a> <a id="4354" href="slides3.html#4340" class="Bound">A</a> <a id="4356" class="Symbol">→</a> <a id="4358" class="PrimitiveType">Set</a><a id="4361" class="Symbol">)</a> <a id="4363" class="Symbol">:</a> <a id="4365" href="Agda.Builtin.List.html#121" class="Datatype">List</a> <a id="4370" href="slides3.html#4340" class="Bound">A</a> <a id="4372" class="Symbol">→</a> <a id="4374" class="PrimitiveType">Set</a> <a id="4378" class="Keyword">where</a>
<a id="All.[]"></a><a id="4386" href="slides3.html#4386" class="InductiveConstructor">[]</a> <a id="4390" class="Symbol">:</a> <a id="4392" href="slides3.html#4335" class="Datatype">All</a> <a id="4396" href="slides3.html#4350" class="Bound">P</a> <a id="4398" href="Agda.Builtin.List.html#158" class="InductiveConstructor">[]</a>
<a id="All._∷_"></a><a id="4403" href="slides3.html#4403" class="InductiveConstructor Operator">_∷_</a> <a id="4407" class="Symbol">:</a> <a id="4409" class="Symbol">∀</a> <a id="4411" class="Symbol">{</a><a id="4412" href="slides3.html#4412" class="Bound">x</a> <a id="4414" href="slides3.html#4414" class="Bound">xs</a><a id="4416" class="Symbol">}</a> <a id="4418" class="Symbol">→</a> <a id="4420" href="slides3.html#4350" class="Bound">P</a> <a id="4422" href="slides3.html#4412" class="Bound">x</a> <a id="4424" class="Symbol">→</a> <a id="4426" href="slides3.html#4335" class="Datatype">All</a> <a id="4430" href="slides3.html#4350" class="Bound">P</a> <a id="4432" href="slides3.html#4414" class="Bound">xs</a> <a id="4435" class="Symbol">→</a> <a id="4437" href="slides3.html#4335" class="Datatype">All</a> <a id="4441" href="slides3.html#4350" class="Bound">P</a> <a id="4443" class="Symbol">(</a><a id="4444" href="slides3.html#4412" class="Bound">x</a> <a id="4446" href="Agda.Builtin.List.html#173" class="InductiveConstructor Operator">∷</a> <a id="4448" href="slides3.html#4414" class="Bound">xs</a><a id="4450" class="Symbol">)</a>
</pre>
<ul>
<li>Value for each variable: <code>Env Γ = All Val Γ</code></li>
<li>Name for each variable: <code>TCCxt Γ = All (\_ → Name) Γ</code></li>
</ul>
</section><section id="adding-new-variables-to-the-typechecking-context" class="slide level2">
<h2>Adding new variables to the typechecking context</h2>
<p>We need to <em>modify</em> both <code>Γ : Cxt</code> and <code>ρ : TCCxt Γ</code>!</p>
<p>Possible solutions:</p>
<ul>
<li>decouple names from the context?</li>
<li>use state of type <code>Σ Cxt TCCxt</code>?</li>
<li><strong>index</strong> the monad by the context Γ!</li>
</ul>
</section><section id="indexed-monads-1" class="slide level2">
<h2>Indexed monads</h2>
<p>An <strong>indexed monad</strong> = a monad with two extra parameters for the (static) <em>input</em> and <em>output</em> states</p>
<pre class="Agda"><a id="4927" class="Keyword">record</a> <a id="IMonad"></a><a id="4934" href="slides3.html#4934" class="Record">IMonad</a> <a id="4941" class="Symbol">{</a><a id="4942" href="slides3.html#4942" class="Bound">I</a> <a id="4944" class="Symbol">:</a> <a id="4946" class="PrimitiveType">Set</a><a id="4949" class="Symbol">}</a> <a id="4951" class="Symbol">(</a><a id="4952" href="slides3.html#4952" class="Bound">M</a> <a id="4954" class="Symbol">:</a> <a id="4956" href="slides3.html#4942" class="Bound">I</a> <a id="4958" class="Symbol">→</a> <a id="4960" href="slides3.html#4942" class="Bound">I</a> <a id="4962" class="Symbol">→</a> <a id="4964" class="PrimitiveType">Set</a> <a id="4968" class="Symbol">→</a> <a id="4970" class="PrimitiveType">Set</a><a id="4973" class="Symbol">)</a> <a id="4975" class="Symbol">:</a> <a id="4977" class="PrimitiveType">Set₁</a> <a id="4982" class="Keyword">where</a>
<a id="4990" class="Keyword">field</a>
<a id="IMonad.return"></a><a id="5000" href="slides3.html#5000" class="Field">return</a> <a id="5007" class="Symbol">:</a> <a id="5009" class="Symbol">∀</a> <a id="5011" class="Symbol">{</a><a id="5012" href="slides3.html#5012" class="Bound">A</a> <a id="5014" href="slides3.html#5014" class="Bound">i</a><a id="5015" class="Symbol">}</a> <a id="5017" class="Symbol">→</a> <a id="5019" href="slides3.html#5012" class="Bound">A</a> <a id="5021" class="Symbol">→</a> <a id="5023" href="slides3.html#4952" class="Bound">M</a> <a id="5025" href="slides3.html#5014" class="Bound">i</a> <a id="5027" href="slides3.html#5014" class="Bound">i</a> <a id="5029" href="slides3.html#5012" class="Bound">A</a>
<a id="IMonad._>>=_"></a><a id="5035" href="slides3.html#5035" class="Field Operator">_>>=_</a> <a id="5042" class="Symbol">:</a> <a id="5044" class="Symbol">∀</a> <a id="5046" class="Symbol">{</a><a id="5047" href="slides3.html#5047" class="Bound">A</a> <a id="5049" href="slides3.html#5049" class="Bound">B</a> <a id="5051" href="slides3.html#5051" class="Bound">i</a> <a id="5053" href="slides3.html#5053" class="Bound">j</a> <a id="5055" href="slides3.html#5055" class="Bound">k</a><a id="5056" class="Symbol">}</a>
<a id="5069" class="Symbol">→</a> <a id="5071" href="slides3.html#4952" class="Bound">M</a> <a id="5073" href="slides3.html#5051" class="Bound">i</a> <a id="5075" href="slides3.html#5053" class="Bound">j</a> <a id="5077" href="slides3.html#5047" class="Bound">A</a> <a id="5079" class="Symbol">→</a> <a id="5081" class="Symbol">(</a><a id="5082" href="slides3.html#5047" class="Bound">A</a> <a id="5084" class="Symbol">→</a> <a id="5086" href="slides3.html#4952" class="Bound">M</a> <a id="5088" href="slides3.html#5053" class="Bound">j</a> <a id="5090" href="slides3.html#5055" class="Bound">k</a> <a id="5092" href="slides3.html#5049" class="Bound">B</a><a id="5093" class="Symbol">)</a> <a id="5095" class="Symbol">→</a> <a id="5097" href="slides3.html#4952" class="Bound">M</a> <a id="5099" href="slides3.html#5051" class="Bound">i</a> <a id="5101" href="slides3.html#5055" class="Bound">k</a> <a id="5103" href="slides3.html#5049" class="Bound">B</a>
</pre>
<p>Examples:</p>
<ul>
<li><code>TCDecl</code> monad (see <a href="https://jespercockx.github.io/ohrid19-agda/src/html/V2/V2.TypeChecker.html">V2/TypeChecker.agda</a>).</li>
<li><code>Exec</code> monad (see <a href="https://jespercockx.github.io/ohrid19-agda/src/html/V3/V3.TypeChecker.html">V3/Interpreter.agda</a>).</li>
</ul>
</section></section>
<section><section id="haskell-ffi" class="title-slide slide level1"><h1>Haskell FFI</h1></section><section id="why-use-an-ffi" class="slide level2">
<h2>Why use an FFI?</h2>
<p>We now have a correct-by-construction typechecker + interpreter.</p>
<p>However, some things are still missing:</p>
<ul>
<li>Parser</li>
<li>Pretty printer</li>
</ul>
<p><strong>Goal.</strong> Use Haskell library for generating these <em>automatically</em></p>
<!--
<pre class="Agda"><a id="5615" class="Keyword">module</a> <a id="5622" href="slides3.html#5622" class="Module">_</a> <a id="5624" class="Keyword">where</a>
<a id="5631" class="Keyword">module</a> <a id="FFI"></a><a id="5638" href="slides3.html#5638" class="Module">FFI</a> <a id="5642" class="Keyword">where</a>
</pre>-->
</section><section id="haskell-ffi-basics" class="slide level2">
<h2>Haskell FFI: basics</h2>
<p>Import a Haskell module:</p>
<pre class="Agda"> <a id="5719" class="Symbol">{-#</a> <a id="5723" class="Keyword">FOREIGN</a> <a id="5731" class="Pragma">GHC</a> <a id="5735" class="Pragma">import</a> <a id="5742" class="Pragma">HaskellModule.hs</a> <a id="5759" class="Symbol">#-}</a>
</pre>
<p>Bind Haskell function to Agda name:</p>
<!--
<pre class="Agda"> <a id="5816" class="Keyword">postulate</a> <a id="FFI.AgdaType"></a><a id="5826" href="slides3.html#5826" class="Postulate">AgdaType</a> <a id="5835" class="Symbol">:</a> <a id="5837" class="PrimitiveType">Set</a>
</pre>-->
<pre class="Agda"> <a id="5860" class="Keyword">postulate</a> <a id="FFI.agdaName"></a><a id="5870" href="slides3.html#5870" class="Postulate">agdaName</a> <a id="5879" class="Symbol">:</a> <a id="5881" href="slides3.html#5826" class="Postulate">AgdaType</a>
<a id="5892" class="Symbol">{-#</a> <a id="5896" class="Keyword">COMPILE</a> <a id="5904" class="Pragma">GHC</a> <a id="5908" href="slides3.html#5870" class="Postulate">agdaName</a> <a id="5917" class="Pragma">=</a> <a id="5919" class="Pragma">haskellCode</a> <a id="5931" class="Symbol">#-}</a>
</pre>
<p>Bind Haskell datatype to Agda datatype:</p>
<pre class="Agda"> <a id="5987" class="Keyword">data</a> <a id="FFI.D"></a><a id="5992" href="slides3.html#5992" class="Datatype">D</a> <a id="5994" class="Symbol">:</a> <a id="5996" class="PrimitiveType">Set</a> <a id="6000" class="Keyword">where</a> <a id="FFI.D.c₁"></a><a id="6006" href="slides3.html#6006" class="InductiveConstructor">c₁</a> <a id="FFI.D.c₂"></a><a id="6009" href="slides3.html#6009" class="InductiveConstructor">c₂</a> <a id="6012" class="Symbol">:</a> <a id="6014" href="slides3.html#5992" class="Datatype">D</a>
<a id="6018" class="Symbol">{-#</a> <a id="6022" class="Keyword">COMPILE</a> <a id="6030" class="Pragma">GHC</a> <a id="6034" href="slides3.html#5992" class="Datatype">D</a> <a id="6036" class="Pragma">=</a> <a id="6038" class="Pragma">data</a> <a id="6043" class="Pragma">hsData</a> <a id="6050" class="Pragma">(hsCon₁</a> <a id="6058" class="Pragma">|</a> <a id="6060" class="Pragma">hsCon₂)</a> <a id="6068" class="Symbol">#-}</a>
</pre>
</section><section id="haskell-ffi-example" class="slide level2">
<h2>Haskell FFI example:</h2>
<div class="sourceCode" id="cb1"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb1-1" data-line-number="1"> <span class="co">-- In file `While/V1/Abs.hs`:</span></a>
<a class="sourceLine" id="cb1-2" data-line-number="2"> <span class="kw">data</span> <span class="dt">Type</span> <span class="fu">=</span> <span class="dt">TBool</span> <span class="fu">|</span> <span class="dt">TInt</span></a></code></pre></div>
<pre class="Agda"> <a id="6185" class="Comment">-- In file `AST.agda`:</a>
<a id="6210" class="Symbol">{-#</a> <a id="6214" class="Keyword">FOREIGN</a> <a id="6222" class="Pragma">GHC</a> <a id="6226" class="Pragma">import</a> <a id="6233" class="Pragma">While.Abs</a> <a id="6243" class="Symbol">#-}</a>
<a id="6249" class="Keyword">data</a> <a id="FFI.Type"></a><a id="6254" href="slides3.html#6254" class="Datatype">Type</a> <a id="6259" class="Symbol">:</a> <a id="6261" class="PrimitiveType">Set</a> <a id="6265" class="Keyword">where</a>
<a id="FFI.Type.bool"></a><a id="6275" href="slides3.html#6275" class="InductiveConstructor">bool</a> <a id="FFI.Type.int"></a><a id="6280" href="slides3.html#6280" class="InductiveConstructor">int</a> <a id="6284" class="Symbol">:</a> <a id="6286" href="slides3.html#6254" class="Datatype">Type</a>
<a id="6294" class="Symbol">{-#</a> <a id="6298" class="Keyword">COMPILE</a> <a id="6306" class="Pragma">GHC</a> <a id="6310" href="slides3.html#6254" class="Datatype">Type</a> <a id="6315" class="Pragma">=</a> <a id="6317" class="Pragma">data</a> <a id="6322" class="Pragma">Type</a>
<a id="6331" class="Pragma">(</a> <a id="6333" class="Pragma">TBool</a>
<a id="6343" class="Pragma">|</a> <a id="6345" class="Pragma">TInt</a>
<a id="6354" class="Pragma">)</a> <a id="6356" class="Symbol">#-}</a>
</pre>
</section><section id="bnfc-the-backus-naur-form-compiler" class="slide level2">
<h2>BNFC: the Backus-Naur Form Compiler</h2>
<p>BNFC is a Haskell library for generating Haskell code from a grammar:</p>
<ul>
<li>Datatypes for abstract syntax</li>
<li>Parser</li>
<li>Pretty-printer</li>
</ul>
<p>See <a href="https://jespercockx.github.io/ohrid19-agda/src/V1/While.cf">While.cf</a> for the grammar of WHILE.</p>
</section><section id="exercises" class="slide level2">
<h2>Exercises</h2>
<p>Extend the typechecker and the BNFC grammar with the new syntactic constructions you added.</p>
<p>Don’t forget to update the Haskell bindings in <a href="https://jespercockx.github.io/ohrid19-agda/src/V1/html/V1.AST.html">AST.agda</a>!</p>
<p>Running the test suite:</p>
<p> <code>make parser</code>: compile the parser and run it on <a href="https://jespercockx.github.io/ohrid19-agda/test/gcd.c">/test/gcd.c</a>. - <code>make test</code>: compile the whole typechecker and run it on the examples in <code>/test</code></p>
</section></section>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/reveal.js/3.7.0//lib/js/head.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/reveal.js/3.7.0//js/reveal.js"></script>
<script>
// Full list of configuration options available at:
// https://github.com/hakimel/reveal.js#configuration
Reveal.initialize({
// Push each slide change to the browser history
history: true,
// Vertical centering of slides
center: false,
// Transition style
transition: 'linear', // none/fade/slide/convex/concave/zoom
// The "normal" size of the presentation, aspect ratio will be preserved
// when the presentation is scaled to fit different resolutions. Can be
// specified using percentage units.
width: 1280,
height: 720,
// Factor of the display size that should remain empty around the content
margin: 0.2,
math: {
mathjax: 'https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js',
config: 'TeX-AMS_HTML-full',
tex2jax: {
inlineMath: [['\\(','\\)']],
displayMath: [['\\[','\\]']],
balanceBraces: true,
processEscapes: false,
processRefs: true,
processEnvironments: true,
preview: 'TeX',
skipTags: ['script','noscript','style','textarea','pre','code'],
ignoreClass: 'tex2jax_ignore',
processClass: 'tex2jax_process'
},
},
// Optional reveal.js plugins
dependencies: [
{ src: 'https://cdnjs.cloudflare.com/ajax/libs/reveal.js/3.7.0//lib/js/classList.js', condition: function() { return !document.body.classList; } },
{ src: 'https://cdnjs.cloudflare.com/ajax/libs/reveal.js/3.7.0//plugin/zoom-js/zoom.js', async: true },
{ src: 'https://cdnjs.cloudflare.com/ajax/libs/reveal.js/3.7.0//plugin/math/math.js', async: true },
{ src: 'https://cdnjs.cloudflare.com/ajax/libs/reveal.js/3.7.0//plugin/notes/notes.js', async: true }
]
});
</script>
</body>
</html>