-
Notifications
You must be signed in to change notification settings - Fork 0
/
slides.html
688 lines (644 loc) · 96.7 KB
/
slides.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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="generator" content="pandoc">
<meta name="author" content="Andreas Abel and Jesper Cockx">
<meta name="dcterms.date" content="2019-01-14">
<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://cdn.jsdelivr.net/npm/reveal.js/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(title);
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://cdn.jsdelivr.net/npm/reveal.js/css/theme/white.css" id="theme">
<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://cdn.jsdelivr.net/npm/reveal.js/css/print/pdf.css' : 'https://cdn.jsdelivr.net/npm/reveal.js/css/print/paper.css';
document.getElementsByTagName( 'head' )[0].appendChild( link );
</script>
<!--[if lt IE 9]>
<script src="https://cdn.jsdelivr.net/npm/reveal.js/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">Tutorial at POPL 2019</p>
<p class="author">Andreas Abel and Jesper Cockx</p>
<p class="date">14 January 2019</p>
</section>
<section><section id="introduction-to-agda" class="title-slide slide level1"><h1>Introduction to Agda</h1></section><section id="what-is-agda" class="slide level2">
<h2>What is Agda?</h2>
<p>Agda is…</p>
<ul>
<li>A strongly typed functional programming language in the tradition of Haskell</li>
<li>An interactive theorem prover in the tradition of Martin-Löf</li>
</ul>
</section><section id="installation" class="slide level2">
<h2>Installation</h2>
<p>For this tutorial, you will need to install <strong>Agda</strong>, the <strong>Agda standard library</strong>, and the <strong>BNFC</strong> tool.</p>
<ul>
<li>Agda: <a href="https://github.com/agda/agda">github.com/agda/agda</a></li>
<li>Agda standard library: <a href="https://github.com/agda/agda-stdlib">github.com/agda/agda-stdlib</a></li>
<li>BNFC: <a href="https://github.com/BNFC/bnfc">github.com/BNFC/bnfc</a></li>
</ul>
<p>Installation instructions:</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode bash"><code class="sourceCode bash"><a class="sourceLine" id="cb1-1" title="1"><span class="fu">git</span> clone https://github.com/jespercockx/popl19-tutorial</a>
<a class="sourceLine" id="cb1-2" title="2"><span class="bu">cd</span> popl19-tutorial</a>
<a class="sourceLine" id="cb1-3" title="3"><span class="ex">./setup.sh</span></a></code></pre></div>
</section><section id="main-features-of-agda" class="slide level2">
<h2>Main features of Agda</h2>
<ul>
<li>Dependent types</li>
<li>Indexed datatypes and dependent pattern matching</li>
<li>Termination checking and productivity checking</li>
<li>A universe hierachy with universe polymorphism</li>
<li>Record types with copattern matching</li>
<li>Coinductive datatypes</li>
<li>Sized types</li>
<li>Implicit arguments</li>
<li>Instance arguments (~ Haskell’s typeclasses)</li>
<li>Parametrized modules (~ ML functors)</li>
<li>A FFI to Haskell</li>
</ul>
<p>We will use many of these in the course of this tutorial!</p>
</section><section id="emacs-mode-for-agda" class="slide level2">
<h2>Emacs mode for Agda</h2>
<p>Programs may contain <strong>holes</strong> (<code>?</code> or <code>{! !}</code>).</p>
<ul>
<li><strong><code>C-c C-l</code></strong>: typecheck and highlight the current file</li>
<li><strong><code>C-c C-,</code></strong>: get information about the hole under the cursor</li>
<li><strong><code>C-c C-space</code></strong>: give a solution</li>
<li><strong><code>C-c C-r</code></strong>: <em>refine</em> the hole
<ul>
<li>Introduce a lambda or constructor</li>
<li>Apply given function to some new holes</li>
</ul></li>
<li><strong><code>C-c C-c</code></strong>: case split on a variable</li>
</ul>
</section></section>
<section><section id="correct-by-construction-programming" class="title-slide slide level1"><h1>Correct-by-construction programming</h1></section><section id="why-use-dependent-types" class="slide level2">
<h2>Why use dependent types?</h2>
<p>With dependent types, we can <strong>statically verify</strong> that a program satisfies a given correctness property.</p>
<p>Verification is <strong>built into</strong> the language itself.</p>
</section><section id="two-approaches-to-verification-with-dependent-types" class="slide level2">
<h2>Two approaches to verification with dependent types:</h2>
<ul>
<li><strong>Extrinsic approach</strong>: first write the program and then prove correctness</li>
<li><strong>Intrinsic approach</strong>: first define the <em>type</em> of programs that satisfy the correctness property and then write the program that inhabits this type</li>
</ul>
<p>The intrinsic approach is also called <strong>correct-by-construction</strong> programming.</p>
</section><section id="example-of-extrinsic-verification" class="slide level2">
<h2>Example of extrinsic verification</h2>
<!--
<pre class="agda-code"><a id="2410" class="Symbol">{-#</a> <a id="2414" class="Keyword">OPTIONS</a> <a id="2422" class="Pragma">--guardedness</a> <a id="2436" class="Pragma">--sized-types</a> <a id="2450" class="Symbol">#-}</a>
<a id="2455" class="Keyword">open</a> <a id="2460" class="Keyword">import</a> <a id="2467" href="Data.List.Base.html" class="Module">Data.List.Base</a> <a id="2482" class="Keyword">using</a> <a id="2488" class="Symbol">(</a><a id="2489" href="Agda.Builtin.List.html#80" class="Datatype">List</a><a id="2493" class="Symbol">;</a> <a id="2495" href="Data.List.Base.html#8785" class="InductiveConstructor">[]</a><a id="2497" class="Symbol">;</a> <a id="2499" href="Agda.Builtin.List.html#132" class="InductiveConstructor Operator">_∷_</a><a id="2502" class="Symbol">)</a>
<a id="2504" class="Keyword">open</a> <a id="2509" class="Keyword">import</a> <a id="2516" href="Data.Maybe.Base.html" class="Module">Data.Maybe.Base</a> <a id="2532" class="Keyword">using</a> <a id="2538" class="Symbol">(</a><a id="2539" href="Data.Maybe.Base.html#335" class="Datatype">Maybe</a><a id="2544" class="Symbol">;</a> <a id="2546" href="Data.Maybe.Base.html#403" class="InductiveConstructor">nothing</a><a id="2553" class="Symbol">;</a> <a id="2555" href="Data.Maybe.Base.html#373" class="InductiveConstructor">just</a><a id="2559" class="Symbol">)</a>
<a id="2561" class="Keyword">open</a> <a id="2566" class="Keyword">import</a> <a id="2573" href="Data.Nat.Base.html" class="Module">Data.Nat.Base</a> <a id="2587" class="Keyword">using</a> <a id="2593" class="Symbol">(</a><a id="2594" href="Agda.Builtin.Nat.html#97" class="Datatype">ℕ</a><a id="2595" class="Symbol">;</a> <a id="2597" href="Agda.Builtin.Nat.html#115" class="InductiveConstructor">zero</a><a id="2601" class="Symbol">;</a> <a id="2603" href="Agda.Builtin.Nat.html#128" class="InductiveConstructor">suc</a><a id="2606" class="Symbol">;</a> <a id="2608" href="Agda.Builtin.Nat.html#230" class="Primitive Operator">_+_</a><a id="2611" class="Symbol">;</a> <a id="2613" href="Agda.Builtin.Nat.html#433" class="Primitive Operator">_*_</a><a id="2616" class="Symbol">;</a> <a id="2618" href="Data.Nat.Base.html#955" class="Function Operator">_<_</a><a id="2621" class="Symbol">)</a>
<a id="2623" class="Keyword">open</a> <a id="2628" class="Keyword">import</a> <a id="2635" href="Data.Product.html" class="Module">Data.Product</a> <a id="2648" class="Keyword">using</a> <a id="2654" class="Symbol">(</a><a id="2655" href="Data.Product.html#1353" class="Function Operator">_×_</a><a id="2658" class="Symbol">;</a> <a id="2660" href="Agda.Builtin.Sigma.html#168" class="InductiveConstructor Operator">_,_</a><a id="2663" class="Symbol">)</a>
<a id="2665" class="Keyword">open</a> <a id="2670" class="Keyword">import</a> <a id="2677" href="Agda.Builtin.Equality.html" class="Module">Agda.Builtin.Equality</a> <a id="2699" class="Keyword">using</a> <a id="2705" class="Symbol">(</a><a id="2706" href="Agda.Builtin.Equality.html#83" class="Datatype Operator">_≡_</a><a id="2709" class="Symbol">;</a> <a id="2711" href="Agda.Builtin.Equality.html#140" class="InductiveConstructor">refl</a><a id="2715" class="Symbol">)</a>
<a id="2718" class="Keyword">postulate</a>
<a id="⋯"></a><a id="2730" href="slides.html#2730" class="Postulate">⋯</a> <a id="2732" class="Symbol">:</a> <a id="2734" class="Symbol">∀</a> <a id="2736" class="Symbol">{</a><a id="2737" href="slides.html#2737" class="Bound">ℓ</a><a id="2738" class="Symbol">}</a> <a id="2740" class="Symbol">{</a><a id="2741" href="slides.html#2741" class="Bound">A</a> <a id="2743" class="Symbol">:</a> <a id="2745" class="PrimitiveType">Set</a> <a id="2749" href="slides.html#2737" class="Bound">ℓ</a><a id="2750" class="Symbol">}</a> <a id="2752" class="Symbol">→</a> <a id="2754" href="slides.html#2741" class="Bound">A</a>
<a id="2757" class="Keyword">module</a> <a id="Intro"></a><a id="2764" href="slides.html#2764" class="Module">Intro</a> <a id="2770" class="Keyword">where</a>
</pre>-->
<pre class="agda-code"> <a id="2795" class="Keyword">module</a> <a id="Extrinsic"></a><a id="2802" href="slides.html#2802" class="Module">Extrinsic</a> <a id="2812" class="Keyword">where</a>
<a id="Intro.Extrinsic._/_"></a><a id="2822" href="slides.html#2822" class="Function Operator">_/_</a> <a id="2826" class="Symbol">:</a> <a id="2828" href="Agda.Builtin.Nat.html#97" class="Datatype">ℕ</a> <a id="2830" class="Symbol">→</a> <a id="2832" href="Agda.Builtin.Nat.html#97" class="Datatype">ℕ</a> <a id="2834" class="Symbol">→</a> <a id="2836" href="Agda.Builtin.Nat.html#97" class="Datatype">ℕ</a>
<a id="2842" href="slides.html#2842" class="Bound">k</a> <a id="2844" href="slides.html#2822" class="Function Operator">/</a> <a id="2846" href="slides.html#2846" class="Bound">l</a> <a id="2848" class="Symbol">=</a> <a id="2850" href="slides.html#2730" class="Postulate">⋯</a>
<a id="Intro.Extrinsic._%_"></a><a id="2857" href="slides.html#2857" class="Function Operator">_%_</a> <a id="2861" class="Symbol">:</a> <a id="2863" href="Agda.Builtin.Nat.html#97" class="Datatype">ℕ</a> <a id="2865" class="Symbol">→</a> <a id="2867" href="Agda.Builtin.Nat.html#97" class="Datatype">ℕ</a> <a id="2869" class="Symbol">→</a> <a id="2871" href="Agda.Builtin.Nat.html#97" class="Datatype">ℕ</a>
<a id="2877" href="slides.html#2877" class="Bound">k</a> <a id="2879" href="slides.html#2857" class="Function Operator">%</a> <a id="2881" href="slides.html#2881" class="Bound">l</a> <a id="2883" class="Symbol">=</a> <a id="2885" href="slides.html#2730" class="Postulate">⋯</a>
<a id="Intro.Extrinsic.divmod-lemma"></a><a id="2892" href="slides.html#2892" class="Function">divmod-lemma</a> <a id="2905" class="Symbol">:</a> <a id="2907" class="Symbol">∀</a> <a id="2909" class="Symbol">{</a><a id="2910" href="slides.html#2910" class="Bound">k</a> <a id="2912" href="slides.html#2912" class="Bound">l</a><a id="2913" class="Symbol">}</a> <a id="2915" class="Symbol">→</a> <a id="2917" href="slides.html#2912" class="Bound">l</a> <a id="2919" href="Agda.Builtin.Nat.html#433" class="Primitive Operator">*</a> <a id="2921" class="Symbol">(</a><a id="2922" href="slides.html#2910" class="Bound">k</a> <a id="2924" href="slides.html#2822" class="Function Operator">/</a> <a id="2926" href="slides.html#2912" class="Bound">l</a><a id="2927" class="Symbol">)</a> <a id="2929" href="Agda.Builtin.Nat.html#230" class="Primitive Operator">+</a> <a id="2931" href="slides.html#2910" class="Bound">k</a> <a id="2933" href="slides.html#2857" class="Function Operator">%</a> <a id="2935" href="slides.html#2912" class="Bound">l</a> <a id="2937" href="Agda.Builtin.Equality.html#83" class="Datatype Operator">≡</a> <a id="2939" href="slides.html#2910" class="Bound">k</a>
<a id="2945" href="slides.html#2892" class="Function">divmod-lemma</a> <a id="2958" class="Symbol">=</a> <a id="2960" href="slides.html#2730" class="Postulate">⋯</a>
<a id="Intro.Extrinsic.divmod-minimal"></a><a id="2967" href="slides.html#2967" class="Function">divmod-minimal</a> <a id="2982" class="Symbol">:</a> <a id="2984" class="Symbol">∀</a> <a id="2986" class="Symbol">{</a><a id="2987" href="slides.html#2987" class="Bound">k</a> <a id="2989" href="slides.html#2989" class="Bound">l</a><a id="2990" class="Symbol">}</a> <a id="2992" class="Symbol">→</a> <a id="2994" href="slides.html#2987" class="Bound">k</a> <a id="2996" href="slides.html#2857" class="Function Operator">%</a> <a id="2998" href="slides.html#2989" class="Bound">l</a> <a id="3000" href="Data.Nat.Base.html#955" class="Function Operator"><</a> <a id="3002" href="slides.html#2989" class="Bound">l</a>
<a id="3008" href="slides.html#2967" class="Function">divmod-minimal</a> <a id="3023" class="Symbol">=</a> <a id="3025" href="slides.html#2730" class="Postulate">⋯</a>
</pre>
</section><section id="example-of-intrinsic-verification" class="slide level2">
<h2>Example of intrinsic verification</h2>
<pre class="agda-code"> <a id="3080" class="Keyword">module</a> <a id="Intrinsic"></a><a id="3087" href="slides.html#3087" class="Module">Intrinsic</a> <a id="3097" class="Keyword">where</a>
<a id="3107" class="Keyword">record</a> <a id="Intro.Intrinsic.Quotient"></a><a id="3114" href="slides.html#3114" class="Record">Quotient</a> <a id="3123" class="Symbol">(</a><a id="3124" href="slides.html#3124" class="Bound">k</a> <a id="3126" href="slides.html#3126" class="Bound">l</a> <a id="3128" class="Symbol">:</a> <a id="3130" href="Agda.Builtin.Nat.html#97" class="Datatype">ℕ</a><a id="3131" class="Symbol">)</a> <a id="3133" class="Symbol">:</a> <a id="3135" class="PrimitiveType">Set</a> <a id="3139" class="Keyword">where</a>
<a id="3151" class="Keyword">field</a>
<a id="Intro.Intrinsic.Quotient.q"></a><a id="3165" href="slides.html#3165" class="Field">q</a> <a id="Intro.Intrinsic.Quotient.r"></a><a id="3167" href="slides.html#3167" class="Field">r</a> <a id="3173" class="Symbol">:</a> <a id="3175" href="Agda.Builtin.Nat.html#97" class="Datatype">ℕ</a>
<a id="Intro.Intrinsic.Quotient.divmod"></a><a id="3185" href="slides.html#3185" class="Field">divmod</a> <a id="3193" class="Symbol">:</a> <a id="3195" href="slides.html#3126" class="Bound">l</a> <a id="3197" href="Agda.Builtin.Nat.html#433" class="Primitive Operator">*</a> <a id="3199" href="slides.html#3165" class="Field">q</a> <a id="3201" href="Agda.Builtin.Nat.html#230" class="Primitive Operator">+</a> <a id="3203" href="slides.html#3167" class="Field">r</a> <a id="3205" href="Agda.Builtin.Equality.html#83" class="Datatype Operator">≡</a> <a id="3207" href="slides.html#3124" class="Bound">k</a>
<a id="Intro.Intrinsic.Quotient.minimal"></a><a id="3217" href="slides.html#3217" class="Field">minimal</a> <a id="3225" class="Symbol">:</a> <a id="3227" href="slides.html#3167" class="Field">r</a> <a id="3229" href="Data.Nat.Base.html#955" class="Function Operator"><</a> <a id="3231" href="slides.html#3126" class="Bound">l</a>
<a id="Intro.Intrinsic.quotient"></a><a id="3238" href="slides.html#3238" class="Function">quotient</a> <a id="3247" class="Symbol">:</a> <a id="3249" class="Symbol">(</a><a id="3250" href="slides.html#3250" class="Bound">k</a> <a id="3252" href="slides.html#3252" class="Bound">l</a> <a id="3254" class="Symbol">:</a> <a id="3256" href="Agda.Builtin.Nat.html#97" class="Datatype">ℕ</a><a id="3257" class="Symbol">)</a> <a id="3259" class="Symbol">→</a> <a id="3261" href="slides.html#3114" class="Record">Quotient</a> <a id="3270" href="slides.html#3250" class="Bound">k</a> <a id="3272" href="slides.html#3252" class="Bound">l</a>
<a id="3278" href="slides.html#3238" class="Function">quotient</a> <a id="3287" class="Symbol">=</a> <a id="3289" href="slides.html#2730" class="Postulate">⋯</a>
<a id="Intro.Intrinsic._/_"></a><a id="3296" href="slides.html#3296" class="Function Operator">_/_</a> <a id="3300" class="Symbol">:</a> <a id="3302" href="Agda.Builtin.Nat.html#97" class="Datatype">ℕ</a> <a id="3304" class="Symbol">→</a> <a id="3306" href="Agda.Builtin.Nat.html#97" class="Datatype">ℕ</a> <a id="3308" class="Symbol">→</a> <a id="3310" href="Agda.Builtin.Nat.html#97" class="Datatype">ℕ</a>
<a id="3316" href="slides.html#3316" class="Bound">k</a> <a id="3318" href="slides.html#3296" class="Function Operator">/</a> <a id="3320" href="slides.html#3320" class="Bound">l</a> <a id="3322" class="Symbol">=</a> <a id="3324" href="slides.html#3165" class="Field">Quotient.q</a> <a id="3335" class="Symbol">(</a><a id="3336" href="slides.html#3238" class="Function">quotient</a> <a id="3345" href="slides.html#3316" class="Bound">k</a> <a id="3347" href="slides.html#3320" class="Bound">l</a><a id="3348" class="Symbol">)</a>
<a id="Intro.Intrinsic._%_"></a><a id="3355" href="slides.html#3355" class="Function Operator">_%_</a> <a id="3359" class="Symbol">:</a> <a id="3361" href="Agda.Builtin.Nat.html#97" class="Datatype">ℕ</a> <a id="3363" class="Symbol">→</a> <a id="3365" href="Agda.Builtin.Nat.html#97" class="Datatype">ℕ</a> <a id="3367" class="Symbol">→</a> <a id="3369" href="Agda.Builtin.Nat.html#97" class="Datatype">ℕ</a>
<a id="3375" href="slides.html#3375" class="Bound">k</a> <a id="3377" href="slides.html#3355" class="Function Operator">%</a> <a id="3379" href="slides.html#3379" class="Bound">l</a> <a id="3381" class="Symbol">=</a> <a id="3383" href="slides.html#3167" class="Field">Quotient.r</a> <a id="3394" class="Symbol">(</a><a id="3395" href="slides.html#3238" class="Function">quotient</a> <a id="3404" href="slides.html#3375" class="Bound">k</a> <a id="3406" href="slides.html#3379" class="Bound">l</a><a id="3407" class="Symbol">)</a>
</pre>
</section><section id="correct-by-construction-programming-1" class="slide level2">
<h2>Correct-by-construction programming</h2>
<p>≠ verifying <em>all</em> properties we want</p>
<p>= verifying as many properties as we can get <em>for free</em></p>
</section><section id="goal-of-this-tutorial" class="slide level2">
<h2>Goal of this tutorial</h2>
<p>Implement a correct-by-construction <strong>typechecker</strong> + <strong>interpreter</strong> for a C-like language (WHILE)</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode c"><code class="sourceCode c"><a class="sourceLine" id="cb2-1" title="1"><span class="dt">int</span> main () {</a>
<a class="sourceLine" id="cb2-2" title="2"> <span class="dt">int</span> n = <span class="dv">100</span>;</a>
<a class="sourceLine" id="cb2-3" title="3"> <span class="dt">int</span> sum = <span class="dv">0</span>;</a>
<a class="sourceLine" id="cb2-4" title="4"> <span class="dt">int</span> k = <span class="dv">0</span>;</a>
<a class="sourceLine" id="cb2-5" title="5"> <span class="cf">while</span> (n > k) {</a>
<a class="sourceLine" id="cb2-6" title="6"> k = k + <span class="dv">1</span>;</a>
<a class="sourceLine" id="cb2-7" title="7"> sum = sum + k;</a>
<a class="sourceLine" id="cb2-8" title="8"> }</a>
<a class="sourceLine" id="cb2-9" title="9"> printInt(sum);</a>
<a class="sourceLine" id="cb2-10" title="10">}</a></code></pre></div>
</section><section id="structure-of-a-while-program" class="slide level2">
<h2>Structure of a WHILE program</h2>
<div class="sourceCode" id="cb3"><pre class="sourceCode c"><code class="sourceCode c"><a class="sourceLine" id="cb3-1" title="1"><span class="dt">int</span> main () {</a>
<a class="sourceLine" id="cb3-2" title="2"> type₁ var₁ = expr₁;</a>
<a class="sourceLine" id="cb3-3" title="3"> ...</a>
<a class="sourceLine" id="cb3-4" title="4"> typeₘ varₘ = exprₘ;</a>
<a class="sourceLine" id="cb3-5" title="5"> stmt₁</a>
<a class="sourceLine" id="cb3-6" title="6"> ...</a>
<a class="sourceLine" id="cb3-7" title="7"> stmtₙ</a>
<a class="sourceLine" id="cb3-8" title="8"> printInt(expr);</a>
<a class="sourceLine" id="cb3-9" title="9">}</a></code></pre></div>
</section></section>
<section><section id="simple-data-types-and-pattern-matching" class="title-slide slide level1"><h1>Simple data types and pattern matching</h1></section><section id="some-simple-agda-datatypes" class="slide level2">
<h2>Some simple Agda datatypes</h2>
<!--
<pre class="agda-code"><a id="4061" class="Keyword">module</a> <a id="SimpleData"></a><a id="4068" href="slides.html#4068" class="Module">SimpleData</a> <a id="4079" class="Keyword">where</a>
</pre>-->
<pre class="agda-code"> <a id="4104" class="Keyword">data</a> <a id="SimpleData.Bool"></a><a id="4109" href="slides.html#4109" class="Datatype">Bool</a> <a id="4114" class="Symbol">:</a> <a id="4116" class="PrimitiveType">Set</a> <a id="4120" class="Keyword">where</a>
<a id="SimpleData.Bool.true"></a><a id="4130" href="slides.html#4130" class="InductiveConstructor">true</a> <a id="SimpleData.Bool.false"></a><a id="4135" href="slides.html#4135" class="InductiveConstructor">false</a> <a id="4141" class="Symbol">:</a> <a id="4143" href="slides.html#4109" class="Datatype">Bool</a>
<a id="4151" class="Keyword">data</a> <a id="SimpleData.Nat"></a><a id="4156" href="slides.html#4156" class="Datatype">Nat</a> <a id="4160" class="Symbol">:</a> <a id="4162" class="PrimitiveType">Set</a> <a id="4166" class="Keyword">where</a>
<a id="SimpleData.Nat.zero"></a><a id="4176" href="slides.html#4176" class="InductiveConstructor">zero</a> <a id="4181" class="Symbol">:</a> <a id="4183" href="slides.html#4156" class="Datatype">Nat</a>
<a id="SimpleData.Nat.suc"></a><a id="4191" href="slides.html#4191" class="InductiveConstructor">suc</a> <a id="4196" class="Symbol">:</a> <a id="4198" href="slides.html#4156" class="Datatype">Nat</a> <a id="4202" class="Symbol">→</a> <a id="4204" href="slides.html#4156" class="Datatype">Nat</a>
<a id="4211" class="Keyword">data</a> <a id="SimpleData.Ord"></a><a id="4216" href="slides.html#4216" class="Datatype">Ord</a> <a id="4220" class="Symbol">:</a> <a id="4222" class="PrimitiveType">Set</a> <a id="4226" class="Keyword">where</a>
<a id="SimpleData.Ord.zero"></a><a id="4236" href="slides.html#4236" class="InductiveConstructor">zero</a> <a id="4241" class="Symbol">:</a> <a id="4243" href="slides.html#4216" class="Datatype">Ord</a>
<a id="SimpleData.Ord.suc"></a><a id="4251" href="slides.html#4251" class="InductiveConstructor">suc</a> <a id="4256" class="Symbol">:</a> <a id="4258" href="slides.html#4216" class="Datatype">Ord</a> <a id="4262" class="Symbol">→</a> <a id="4264" href="slides.html#4216" class="Datatype">Ord</a>
<a id="SimpleData.Ord.sup"></a><a id="4272" href="slides.html#4272" class="InductiveConstructor">sup</a> <a id="4277" class="Symbol">:</a> <a id="4279" class="Symbol">(</a><a id="4280" href="slides.html#4156" class="Datatype">Nat</a> <a id="4284" class="Symbol">→</a> <a id="4286" href="slides.html#4216" class="Datatype">Ord</a><a id="4289" class="Symbol">)</a> <a id="4291" class="Symbol">→</a> <a id="4293" href="slides.html#4216" class="Datatype">Ord</a>
<a id="4300" class="Keyword">data</a> <a id="SimpleData.⊥"></a><a id="4305" href="slides.html#4305" class="Datatype">⊥</a> <a id="4307" class="Symbol">:</a> <a id="4309" class="PrimitiveType">Set</a> <a id="4313" class="Keyword">where</a>
<a id="4323" class="Comment">-- no constructors</a>
</pre>
</section><section id="pattern-matching-in-agda" class="slide level2">
<h2>Pattern matching in Agda</h2>
<pre class="agda-code"> <a id="SimpleData.not"></a><a id="4386" href="slides.html#4386" class="Function">not</a> <a id="4390" class="Symbol">:</a> <a id="4392" href="slides.html#4109" class="Datatype">Bool</a> <a id="4397" class="Symbol">→</a> <a id="4399" href="slides.html#4109" class="Datatype">Bool</a>
<a id="4406" href="slides.html#4386" class="Function">not</a> <a id="4410" href="slides.html#4130" class="InductiveConstructor">true</a> <a id="4416" class="Symbol">=</a> <a id="4418" href="slides.html#4135" class="InductiveConstructor">false</a>
<a id="4426" href="slides.html#4386" class="Function">not</a> <a id="4430" href="slides.html#4135" class="InductiveConstructor">false</a> <a id="4436" class="Symbol">=</a> <a id="4438" href="slides.html#4130" class="InductiveConstructor">true</a>
<a id="SimpleData.max"></a><a id="4446" href="slides.html#4446" class="Function">max</a> <a id="4450" class="Symbol">:</a> <a id="4452" href="slides.html#4156" class="Datatype">Nat</a> <a id="4456" class="Symbol">→</a> <a id="4458" href="slides.html#4156" class="Datatype">Nat</a> <a id="4462" class="Symbol">→</a> <a id="4464" href="slides.html#4156" class="Datatype">Nat</a>
<a id="4470" href="slides.html#4446" class="Function">max</a> <a id="4474" href="slides.html#4176" class="InductiveConstructor">zero</a> <a id="4482" href="slides.html#4482" class="Bound">l</a> <a id="4490" class="Symbol">=</a> <a id="4492" href="slides.html#4482" class="Bound">l</a>
<a id="4496" href="slides.html#4446" class="CatchallClause Function">max</a><a id="4499" class="CatchallClause"> </a><a id="4500" href="slides.html#4500" class="CatchallClause Bound">k</a><a id="4501" class="CatchallClause"> </a><a id="4508" href="slides.html#4176" class="CatchallClause InductiveConstructor">zero</a> <a id="4516" class="Symbol">=</a> <a id="4518" href="slides.html#4500" class="Bound">k</a>
<a id="4522" href="slides.html#4446" class="Function">max</a> <a id="4526" class="Symbol">(</a><a id="4527" href="slides.html#4191" class="InductiveConstructor">suc</a> <a id="4531" href="slides.html#4531" class="Bound">k</a><a id="4532" class="Symbol">)</a> <a id="4534" class="Symbol">(</a><a id="4535" href="slides.html#4191" class="InductiveConstructor">suc</a> <a id="4539" href="slides.html#4539" class="Bound">l</a><a id="4540" class="Symbol">)</a> <a id="4542" class="Symbol">=</a> <a id="4544" href="slides.html#4191" class="InductiveConstructor">suc</a> <a id="4548" class="Symbol">(</a><a id="4549" href="slides.html#4446" class="Function">max</a> <a id="4553" href="slides.html#4531" class="Bound">k</a> <a id="4555" href="slides.html#4539" class="Bound">l</a><a id="4556" class="Symbol">)</a>
<a id="SimpleData.magic"></a><a id="4561" href="slides.html#4561" class="Function">magic</a> <a id="4567" class="Symbol">:</a> <a id="4569" class="Symbol">{</a><a id="4570" href="slides.html#4570" class="Bound">A</a> <a id="4572" class="Symbol">:</a> <a id="4574" class="PrimitiveType">Set</a><a id="4577" class="Symbol">}</a> <a id="4579" class="Symbol">→</a> <a id="4581" href="slides.html#4305" class="Datatype">⊥</a> <a id="4583" class="Symbol">→</a> <a id="4585" href="slides.html#4570" class="Bound">A</a>
<a id="4589" href="slides.html#4561" class="Function">magic</a> <a id="4595" class="Symbol">()</a>
</pre>
</section><section id="type-and-expression-syntax-for-while" class="slide level2">
<h2>Type and expression syntax for WHILE</h2>
<!--
<pre class="agda-code"> <a id="4659" class="Keyword">postulate</a>
<a id="SimpleData.Id"></a><a id="4673" href="slides.html#4673" class="Postulate">Id</a> <a id="SimpleData.ℤ"></a><a id="4676" href="slides.html#4676" class="Postulate">ℤ</a> <a id="SimpleData.Boolean"></a><a id="4678" href="slides.html#4678" class="Postulate">Boolean</a> <a id="4686" class="Symbol">:</a> <a id="4688" class="PrimitiveType">Set</a>
</pre>-->
<pre class="agda-code"> <a id="4711" class="Keyword">data</a> <a id="SimpleData.Type"></a><a id="4716" href="slides.html#4716" class="Datatype">Type</a> <a id="4721" class="Symbol">:</a> <a id="4723" class="PrimitiveType">Set</a> <a id="4727" class="Keyword">where</a>
<a id="SimpleData.Type.bool"></a><a id="4737" href="slides.html#4737" class="InductiveConstructor">bool</a> <a id="SimpleData.Type.int"></a><a id="4742" href="slides.html#4742" class="InductiveConstructor">int</a> <a id="4746" class="Symbol">:</a> <a id="4748" href="slides.html#4716" class="Datatype">Type</a> <a id="4766" class="Comment">-- t ::= bool | int</a>
<a id="4789" class="Keyword">data</a> <a id="SimpleData.Exp"></a><a id="4794" href="slides.html#4794" class="Datatype">Exp</a> <a id="4798" class="Symbol">:</a> <a id="4800" class="PrimitiveType">Set</a> <a id="4804" class="Keyword">where</a>
<a id="SimpleData.Exp.eId"></a><a id="4814" href="slides.html#4814" class="InductiveConstructor">eId</a> <a id="4820" class="Symbol">:</a> <a id="4822" class="Symbol">(</a><a id="4823" href="slides.html#4823" class="Bound">x</a> <a id="4825" class="Symbol">:</a> <a id="4827" href="slides.html#4673" class="Postulate">Id</a><a id="4829" class="Symbol">)</a> <a id="4836" class="Symbol">→</a> <a id="4838" href="slides.html#4794" class="Datatype">Exp</a> <a id="4843" class="Comment">-- x,y,z,...</a>
<a id="SimpleData.Exp.eInt"></a><a id="4860" href="slides.html#4860" class="InductiveConstructor">eInt</a> <a id="4866" class="Symbol">:</a> <a id="4868" class="Symbol">(</a><a id="4869" href="slides.html#4869" class="Bound">i</a> <a id="4871" class="Symbol">:</a> <a id="4873" href="slides.html#4676" class="Postulate">ℤ</a><a id="4874" class="Symbol">)</a> <a id="4882" class="Symbol">→</a> <a id="4884" href="slides.html#4794" class="Datatype">Exp</a> <a id="4889" class="Comment">-- ...-2,-1,0,1,2...</a>
<a id="SimpleData.Exp.eBool"></a><a id="4914" href="slides.html#4914" class="InductiveConstructor">eBool</a> <a id="4920" class="Symbol">:</a> <a id="4922" class="Symbol">(</a><a id="4923" href="slides.html#4923" class="Bound">b</a> <a id="4925" class="Symbol">:</a> <a id="4927" href="slides.html#4678" class="Postulate">Boolean</a><a id="4934" class="Symbol">)</a> <a id="4936" class="Symbol">→</a> <a id="4938" href="slides.html#4794" class="Datatype">Exp</a> <a id="4943" class="Comment">-- true or false</a>
<a id="SimpleData.Exp.ePlus"></a><a id="4964" href="slides.html#4964" class="InductiveConstructor">ePlus</a> <a id="4970" class="Symbol">:</a> <a id="4972" class="Symbol">(</a><a id="4973" href="slides.html#4973" class="Bound">e</a> <a id="4975" href="slides.html#4975" class="Bound">e'</a> <a id="4978" class="Symbol">:</a> <a id="4980" href="slides.html#4794" class="Datatype">Exp</a><a id="4983" class="Symbol">)</a> <a id="4986" class="Symbol">→</a> <a id="4988" href="slides.html#4794" class="Datatype">Exp</a> <a id="4993" class="Comment">-- e+e'</a>
<a id="SimpleData.Exp.eGt"></a><a id="5005" href="slides.html#5005" class="InductiveConstructor">eGt</a> <a id="5011" class="Symbol">:</a> <a id="5013" class="Symbol">(</a><a id="5014" href="slides.html#5014" class="Bound">e</a> <a id="5016" href="slides.html#5016" class="Bound">e'</a> <a id="5019" class="Symbol">:</a> <a id="5021" href="slides.html#4794" class="Datatype">Exp</a><a id="5024" class="Symbol">)</a> <a id="5027" class="Symbol">→</a> <a id="5029" href="slides.html#4794" class="Datatype">Exp</a> <a id="5034" class="Comment">-- e>e'</a>
<a id="SimpleData.Exp.eAnd"></a><a id="5046" href="slides.html#5046" class="InductiveConstructor">eAnd</a> <a id="5052" class="Symbol">:</a> <a id="5054" class="Symbol">(</a><a id="5055" href="slides.html#5055" class="Bound">e</a> <a id="5057" href="slides.html#5057" class="Bound">e'</a> <a id="5060" class="Symbol">:</a> <a id="5062" href="slides.html#4794" class="Datatype">Exp</a><a id="5065" class="Symbol">)</a> <a id="5068" class="Symbol">→</a> <a id="5070" href="slides.html#4794" class="Datatype">Exp</a> <a id="5075" class="Comment">-- e&&e'</a>
</pre>
</section><section id="statement-syntax-for-while" class="slide level2">
<h2>Statement syntax for WHILE</h2>
<pre class="agda-code"> <a id="5130" class="Keyword">data</a> <a id="SimpleData.Stm"></a><a id="5135" href="slides.html#5135" class="Datatype">Stm</a> <a id="5139" class="Symbol">:</a> <a id="5141" class="PrimitiveType">Set</a> <a id="5145" class="Keyword">where</a>
<a id="SimpleData.Stm.sAss"></a><a id="5155" href="slides.html#5155" class="InductiveConstructor">sAss</a> <a id="5162" class="Symbol">:</a> <a id="5164" class="Symbol">(</a><a id="5165" href="slides.html#5165" class="Bound">x</a> <a id="5167" class="Symbol">:</a> <a id="5169" href="slides.html#4673" class="Postulate">Id</a><a id="5171" class="Symbol">)</a> <a id="5173" class="Symbol">(</a><a id="5174" href="slides.html#5174" class="Bound">e</a> <a id="5176" class="Symbol">:</a> <a id="5178" href="slides.html#4794" class="Datatype">Exp</a><a id="5181" class="Symbol">)</a> <a id="5190" class="Symbol">→</a> <a id="5192" href="slides.html#5135" class="Datatype">Stm</a>
<a id="5202" class="Comment">-- ^ x = e;</a>
<a id="SimpleData.Stm.sWhile"></a><a id="5218" href="slides.html#5218" class="InductiveConstructor">sWhile</a> <a id="5225" class="Symbol">:</a> <a id="5227" class="Symbol">(</a><a id="5228" href="slides.html#5228" class="Bound">e</a> <a id="5230" class="Symbol">:</a> <a id="5232" href="slides.html#4794" class="Datatype">Exp</a><a id="5235" class="Symbol">)</a> <a id="5237" class="Symbol">(</a><a id="5238" href="slides.html#5238" class="Bound">ss</a> <a id="5241" class="Symbol">:</a> <a id="5243" href="Agda.Builtin.List.html#80" class="Datatype">List</a> <a id="5248" href="slides.html#5135" class="Datatype">Stm</a><a id="5251" class="Symbol">)</a> <a id="5253" class="Symbol">→</a> <a id="5255" href="slides.html#5135" class="Datatype">Stm</a>
<a id="5265" class="Comment">-- ^ while (b) { ... }</a>
</pre>
</section><section id="program-syntax-for-while" class="slide level2">
<h2>Program syntax for WHILE</h2>
<pre class="agda-code"> <a id="5332" class="Keyword">record</a> <a id="SimpleData.Decl"></a><a id="5339" href="slides.html#5339" class="Record">Decl</a> <a id="5344" class="Symbol">:</a> <a id="5346" class="PrimitiveType">Set</a> <a id="5350" class="Keyword">where</a>
<a id="5360" class="Keyword">constructor</a> <a id="SimpleData.Decl.dInit"></a><a id="5372" href="slides.html#5372" class="InductiveConstructor">dInit</a> <a id="5380" class="Comment">-- t x = e;</a>
<a id="5396" class="Keyword">field</a>
<a id="SimpleData.Decl.declType"></a><a id="5408" href="slides.html#5408" class="Field">declType</a> <a id="5417" class="Symbol">:</a> <a id="5419" href="slides.html#4716" class="Datatype">Type</a> <a id="5426" class="Comment">-- variable type (t)</a>
<a id="SimpleData.Decl.declId"></a><a id="5453" href="slides.html#5453" class="Field">declId</a> <a id="5462" class="Symbol">:</a> <a id="5464" href="slides.html#4673" class="Postulate">Id</a> <a id="5471" class="Comment">-- variable name (x)</a>
<a id="SimpleData.Decl.declExp"></a><a id="5498" href="slides.html#5498" class="Field">declExp</a> <a id="5507" class="Symbol">:</a> <a id="5509" href="slides.html#4794" class="Datatype">Exp</a> <a id="5516" class="Comment">-- initial value (e)</a>
<a id="5539" class="Keyword">open</a> <a id="5544" href="slides.html#5339" class="Module">Decl</a> <a id="5549" class="Keyword">public</a>
<a id="5559" class="Keyword">record</a> <a id="SimpleData.Program"></a><a id="5566" href="slides.html#5566" class="Record">Program</a> <a id="5574" class="Symbol">:</a> <a id="5576" class="PrimitiveType">Set</a> <a id="5580" class="Keyword">where</a>
<a id="5590" class="Keyword">constructor</a> <a id="SimpleData.Program.program"></a><a id="5602" href="slides.html#5602" class="InductiveConstructor">program</a>
<a id="5614" class="Keyword">field</a>
<a id="SimpleData.Program.theDecls"></a><a id="5626" href="slides.html#5626" class="Field">theDecls</a> <a id="5635" class="Symbol">:</a> <a id="5637" href="Agda.Builtin.List.html#80" class="Datatype">List</a> <a id="5642" href="slides.html#5339" class="Record">Decl</a> <a id="5648" class="Comment">-- t x = e; ...</a>
<a id="SimpleData.Program.theStms"></a><a id="5670" href="slides.html#5670" class="Field">theStms</a> <a id="5679" class="Symbol">:</a> <a id="5681" href="Agda.Builtin.List.html#80" class="Datatype">List</a> <a id="5686" href="slides.html#5135" class="Datatype">Stm</a> <a id="5692" class="Comment">-- ss</a>
<a id="SimpleData.Program.theMain"></a><a id="5704" href="slides.html#5704" class="Field">theMain</a> <a id="5713" class="Symbol">:</a> <a id="5715" href="slides.html#4794" class="Datatype">Exp</a> <a id="5726" class="Comment">-- printInt(e);</a>
<a id="5744" class="Keyword">open</a> <a id="5749" href="slides.html#5566" class="Module">Program</a> <a id="5757" class="Keyword">public</a>
</pre>
</section><section id="untyped-interpreter" class="slide level2">
<h2>Untyped interpreter</h2>
<p>File <a href="https://jespercockx.github.io/popl19-tutorial/src/html/UntypedInterpreter.html">UntypedInterpreter.agda</a> defines an <em>untyped</em> interpreter for WHILE:</p>
<pre class="agda-code"> <a id="5959" class="Keyword">data</a> <a id="SimpleData.Val"></a><a id="5964" href="slides.html#5964" class="Datatype">Val</a> <a id="5968" class="Symbol">:</a> <a id="5970" class="PrimitiveType">Set</a> <a id="5974" class="Keyword">where</a>
<a id="SimpleData.Val.intV"></a><a id="5984" href="slides.html#5984" class="InductiveConstructor">intV</a> <a id="5990" class="Symbol">:</a> <a id="5992" href="slides.html#4676" class="Postulate">ℤ</a> <a id="6000" class="Symbol">→</a> <a id="6002" href="slides.html#5964" class="Datatype">Val</a>
<a id="SimpleData.Val.boolV"></a><a id="6010" href="slides.html#6010" class="InductiveConstructor">boolV</a> <a id="6016" class="Symbol">:</a> <a id="6018" href="slides.html#4678" class="Postulate">Boolean</a> <a id="6026" class="Symbol">→</a> <a id="6028" href="slides.html#5964" class="Datatype">Val</a>
<a id="SimpleData.Env"></a><a id="6035" href="slides.html#6035" class="Function">Env</a> <a id="6039" class="Symbol">:</a> <a id="6041" class="PrimitiveType">Set</a>
<a id="6047" href="slides.html#6035" class="Function">Env</a> <a id="6051" class="Symbol">=</a> <a id="6053" href="Agda.Builtin.List.html#80" class="Datatype">List</a> <a id="6058" class="Symbol">(</a><a id="6059" href="slides.html#4673" class="Postulate">Id</a> <a id="6062" href="Data.Product.html#1353" class="Function Operator">×</a> <a id="6064" href="slides.html#5964" class="Datatype">Val</a><a id="6067" class="Symbol">)</a>
<a id="SimpleData.eval"></a><a id="6072" href="slides.html#6072" class="Function">eval</a> <a id="6077" class="Symbol">:</a> <a id="6079" href="slides.html#6035" class="Function">Env</a> <a id="6083" class="Symbol">→</a> <a id="6085" href="slides.html#4794" class="Datatype">Exp</a> <a id="6089" class="Symbol">→</a> <a id="6091" href="Data.Maybe.Base.html#335" class="Datatype">Maybe</a> <a id="6097" href="slides.html#5964" class="Datatype">Val</a>
<a id="6103" href="slides.html#6072" class="Function">eval</a> <a id="6108" href="slides.html#6108" class="Bound">ρ</a> <a id="6110" href="slides.html#6110" class="Bound">e</a> <a id="6112" class="Symbol">=</a> <a id="6114" href="slides.html#2730" class="Postulate">⋯</a>
<a id="SimpleData.execDecl"></a><a id="6119" href="slides.html#6119" class="Function">execDecl</a> <a id="6128" class="Symbol">:</a> <a id="6130" href="slides.html#5339" class="Record">Decl</a> <a id="6135" class="Symbol">→</a> <a id="6137" href="slides.html#6035" class="Function">Env</a> <a id="6141" class="Symbol">→</a> <a id="6143" href="Data.Maybe.Base.html#335" class="Datatype">Maybe</a> <a id="6149" href="slides.html#6035" class="Function">Env</a>
<a id="6155" href="slides.html#6119" class="Function">execDecl</a> <a id="6164" href="slides.html#6164" class="Bound">d</a> <a id="6166" href="slides.html#6166" class="Bound">ρ</a> <a id="6168" class="Symbol">=</a> <a id="6170" href="slides.html#2730" class="Postulate">⋯</a>
</pre>
</section><section id="untyped-interpreter-continued" class="slide level2">
<h2>Untyped interpreter (continued)</h2>
<p>All Agda functions must be <strong>total</strong></p>
<p>But WHILE programs can loop!</p>
<p>⇒ we must limit the number of times we repeat the loop</p>
<pre class="agda-code"> <a id="SimpleData.execStm"></a><a id="6347" href="slides.html#6347" class="Function">execStm</a> <a id="6355" class="Symbol">:</a> <a id="6357" class="Symbol">(</a><a id="6358" href="slides.html#6358" class="Bound">fuel</a> <a id="6363" class="Symbol">:</a> <a id="6365" href="Agda.Builtin.Nat.html#97" class="Datatype">ℕ</a><a id="6366" class="Symbol">)</a> <a id="6368" class="Symbol">→</a> <a id="6370" href="slides.html#5135" class="Datatype">Stm</a> <a id="6374" class="Symbol">→</a> <a id="6376" href="slides.html#6035" class="Function">Env</a> <a id="6380" class="Symbol">→</a> <a id="6382" href="Data.Maybe.Base.html#335" class="Datatype">Maybe</a> <a id="6388" href="slides.html#6035" class="Function">Env</a>
<a id="6394" href="slides.html#6347" class="Function">execStm</a> <a id="6402" href="slides.html#6402" class="Bound">fuel</a> <a id="6407" href="slides.html#6407" class="Bound">stm</a> <a id="6411" href="slides.html#6411" class="Bound">ρ</a> <a id="6413" class="Symbol">=</a> <a id="6415" href="slides.html#2730" class="Postulate">⋯</a>
<a id="SimpleData.evalPrg"></a><a id="6420" href="slides.html#6420" class="Function">evalPrg</a> <a id="6428" class="Symbol">:</a> <a id="6430" class="Symbol">(</a><a id="6431" href="slides.html#6431" class="Bound">fuel</a> <a id="6436" class="Symbol">:</a> <a id="6438" href="Agda.Builtin.Nat.html#97" class="Datatype">ℕ</a><a id="6439" class="Symbol">)</a> <a id="6441" class="Symbol">→</a> <a id="6443" href="slides.html#5566" class="Record">Program</a> <a id="6451" class="Symbol">→</a> <a id="6453" href="Data.Maybe.Base.html#335" class="Datatype">Maybe</a> <a id="6459" href="slides.html#4676" class="Postulate">ℤ</a>
<a id="6463" href="slides.html#6420" class="Function">evalPrg</a> <a id="6471" href="slides.html#6471" class="Bound">fuel</a> <a id="6476" class="Symbol">(</a><a id="6477" href="slides.html#5602" class="InductiveConstructor">program</a> <a id="6485" href="slides.html#6485" class="Bound">ds</a> <a id="6488" href="slides.html#6488" class="Bound">ss</a> <a id="6491" href="slides.html#6491" class="Bound">e</a><a id="6492" class="Symbol">)</a> <a id="6494" class="Symbol">=</a> <a id="6496" href="slides.html#2730" class="Postulate">⋯</a>
</pre>
</section><section id="exercise-1" class="slide level2">
<h2>Exercise #1</h2>
<p>Go to <a href="https://jespercockx.github.io/popl19-tutorial/src/html/AST.html">AST.agda</a> and extend the syntax with one or more of the following:</p>
<ul>
<li>Boolean negation: <code>!e</code></li>
<li>Integer subtraction: <code>e₁-e₂</code></li>
<li>Conditionals: <code class="sourceCode c"><span class="cf">if</span> (e) { ss₁ } <span class="cf">else</span> { ss₂ }</code></li>
</ul>
<p>Ignore the pragmas <code>{-# COMPILE ... #-}</code> for now.</p>
<p>Also go to <a href="https://jespercockx.github.io/popl19-tutorial/src/html/UntypedInterpreter.html">UntypedInterpreter.agda</a> and add the missing cases!</p>
</section></section>
<section><section id="haskell-ffi" class="title-slide slide level1"><h1>Haskell FFI</h1></section><section id="haskell-ffi-basics" class="slide level2">
<h2>Haskell FFI: basics</h2>
<!--
<pre class="agda-code"><a id="7014" class="Keyword">module</a> <a id="FFI"></a><a id="7021" href="slides.html#7021" class="Module">FFI</a> <a id="7025" class="Keyword">where</a>
</pre>-->
<p>Import a Haskell module:</p>
<pre class="agda-code"> <a id="7076" class="Symbol">{-#</a> <a id="7080" class="Keyword">FOREIGN</a> <a id="7088" class="Pragma">GHC</a> <a id="7092" class="Pragma">import</a> <a id="7099" class="Pragma">HaskellModule.hs</a> <a id="7116" class="Symbol">#-}</a>
</pre>
<p>Bind Haskell function to Agda name:</p>
<!--
<pre class="agda-code"> <a id="7173" class="Keyword">postulate</a> <a id="FFI.AgdaType"></a><a id="7183" href="slides.html#7183" class="Postulate">AgdaType</a> <a id="7192" class="Symbol">:</a> <a id="7194" class="PrimitiveType">Set</a>
</pre>-->
<pre class="agda-code"> <a id="7217" class="Keyword">postulate</a> <a id="FFI.agdaName"></a><a id="7227" href="slides.html#7227" class="Postulate">agdaName</a> <a id="7236" class="Symbol">:</a> <a id="7238" href="slides.html#7183" class="Postulate">AgdaType</a>
<a id="7249" class="Symbol">{-#</a> <a id="7253" class="Keyword">COMPILE</a> <a id="7261" class="Pragma">GHC</a> <a id="7265" href="slides.html#7227" class="Postulate">agdaName</a> <a id="7274" class="Pragma">=</a> <a id="7276" class="Pragma">haskellCode</a> <a id="7288" class="Symbol">#-}</a>
</pre>
<p>Bind Haskell datatype to Agda datatype:</p>
<pre class="agda-code"> <a id="7344" class="Keyword">data</a> <a id="FFI.D"></a><a id="7349" href="slides.html#7349" class="Datatype">D</a> <a id="7351" class="Symbol">:</a> <a id="7353" class="PrimitiveType">Set</a> <a id="7357" class="Keyword">where</a> <a id="FFI.D.c₁"></a><a id="7363" href="slides.html#7363" class="InductiveConstructor">c₁</a> <a id="FFI.D.c₂"></a><a id="7366" href="slides.html#7366" class="InductiveConstructor">c₂</a> <a id="7369" class="Symbol">:</a> <a id="7371" href="slides.html#7349" class="Datatype">D</a>
<a id="7375" class="Symbol">{-#</a> <a id="7379" class="Keyword">COMPILE</a> <a id="7387" class="Pragma">GHC</a> <a id="7391" href="slides.html#7349" class="Datatype">D</a> <a id="7393" class="Pragma">=</a> <a id="7395" class="Pragma">data</a> <a id="7400" class="Pragma">hsData</a> <a id="7407" class="Pragma">(hsCon₁</a> <a id="7415" class="Pragma">|</a> <a id="7417" class="Pragma">hsCon₂)</a> <a id="7425" class="Symbol">#-}</a>
</pre>
</section><section id="haskell-ffi-example" class="slide level2">
<h2>Haskell FFI example:</h2>
<div class="sourceCode" id="cb4"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb4-1" title="1"> <span class="co">-- In file `While/Abs.hs`:</span></a>
<a class="sourceLine" id="cb4-2" title="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-code"> <a id="7540" class="Comment">-- In file `AST.agda`:</a>
<a id="7565" class="Symbol">{-#</a> <a id="7569" class="Keyword">FOREIGN</a> <a id="7577" class="Pragma">GHC</a> <a id="7581" class="Pragma">import</a> <a id="7588" class="Pragma">While.Abs</a> <a id="7598" class="Symbol">#-}</a>
<a id="7604" class="Keyword">data</a> <a id="FFI.Type"></a><a id="7609" href="slides.html#7609" class="Datatype">Type</a> <a id="7614" class="Symbol">:</a> <a id="7616" class="PrimitiveType">Set</a> <a id="7620" class="Keyword">where</a>
<a id="FFI.Type.bool"></a><a id="7630" href="slides.html#7630" class="InductiveConstructor">bool</a> <a id="FFI.Type.int"></a><a id="7635" href="slides.html#7635" class="InductiveConstructor">int</a> <a id="7639" class="Symbol">:</a> <a id="7641" href="slides.html#7609" class="Datatype">Type</a>
<a id="7649" class="Symbol">{-#</a> <a id="7653" class="Keyword">COMPILE</a> <a id="7661" class="Pragma">GHC</a> <a id="7665" href="slides.html#7609" class="Datatype">Type</a> <a id="7670" class="Pragma">=</a> <a id="7672" class="Pragma">data</a> <a id="7677" class="Pragma">Type</a>
<a id="7686" class="Pragma">(</a> <a id="7688" class="Pragma">TBool</a>
<a id="7698" class="Pragma">|</a> <a id="7700" class="Pragma">TInt</a>
<a id="7709" class="Pragma">)</a> <a id="7711" 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/popl19-tutorial/src/While.cf">While.cf</a> for the grammar of WHILE.</p>
</section><section id="exercise-2" class="slide level2">
<h2>Exercise #2</h2>
<p>Extend 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/popl19-tutorial/src/html/AST.html">AST.agda</a>!</p>
<p>Testing the grammar: <code>make parser</code> will compile the parser and run it on <a href="https://jespercockx.github.io/popl19-tutorial/test/gcd.c">/test/gcd.c</a>.</p>
</section></section>
<section><section id="dependent-types-and-indexed-datatypes" class="title-slide slide level1"><h1>Dependent types and indexed datatypes</h1></section><section id="indexed-datatypes" class="slide level2">
<h2>Indexed datatypes</h2>
<p><strong>Indexed datatype</strong> = family of datatypes indexed over some base type</p>
<!--
<pre class="agda-code"><a id="8501" class="Keyword">module</a> <a id="IndexedData"></a><a id="8508" href="slides.html#8508" class="Module">IndexedData</a> <a id="8520" class="Keyword">where</a>
<a id="8528" class="Keyword">open</a> <a id="8533" class="Keyword">import</a> <a id="8540" href="Data.Nat.Base.html" class="Module">Data.Nat.Base</a>
<a id="8556" class="Keyword">open</a> <a id="8561" class="Keyword">import</a> <a id="8568" href="Data.Integer.Base.html" class="Module">Data.Integer.Base</a> <a id="8586" class="Keyword">using</a> <a id="8592" class="Symbol">(</a><a id="8593" href="Agda.Builtin.Int.html#178" class="Datatype">ℤ</a><a id="8594" class="Symbol">)</a>
<a id="8598" class="Keyword">open</a> <a id="8603" class="Keyword">import</a> <a id="8610" href="Data.List.Membership.Propositional.html" class="Module">Data.List.Membership.Propositional</a> <a id="8645" class="Keyword">using</a> <a id="8651" class="Symbol">(</a><a id="8652" href="Data.List.Membership.Setoid.html#709" class="Function Operator">_∈_</a><a id="8655" class="Symbol">;</a> <a id="8657" href="Data.List.Membership.Setoid.html#758" class="Function Operator">_∉_</a><a id="8660" class="Symbol">)</a>
<a id="8664" class="Keyword">open</a> <a id="8669" class="Keyword">import</a> <a id="8676" href="Data.List.All.html" class="Module">Data.List.All</a> <a id="8690" class="Keyword">using</a> <a id="8696" class="Symbol">(</a><a id="8697" href="Data.List.All.html#826" class="Datatype">All</a><a id="8700" class="Symbol">;</a> <a id="8702" href="Data.List.All.html#904" class="InductiveConstructor">[]</a><a id="8704" class="Symbol">;</a> <a id="8706" href="Data.List.All.html#921" class="InductiveConstructor Operator">_∷_</a><a id="8709" class="Symbol">)</a> <a id="8711" class="Keyword">hiding</a> <a id="8718" class="Symbol">(</a><a id="8719" class="Keyword">module</a> <a id="8726" href="Data.List.All.html#826" class="Module">All</a><a id="8729" class="Symbol">)</a>
<a id="8733" class="Keyword">open</a> <a id="8738" href="slides.html#7021" class="Module">FFI</a> <a id="8742" class="Keyword">using</a> <a id="8748" class="Symbol">(</a><a id="8749" href="slides.html#7609" class="Datatype">Type</a><a id="8753" class="Symbol">;</a> <a id="8755" href="slides.html#7635" class="InductiveConstructor">int</a><a id="8758" class="Symbol">;</a> <a id="8760" href="slides.html#7630" class="InductiveConstructor">bool</a><a id="8764" class="Symbol">)</a>
<a id="8769" class="Keyword">data</a> <a id="IndexedData.Boolean"></a><a id="8774" href="slides.html#8774" class="Datatype">Boolean</a> <a id="8782" class="Symbol">:</a> <a id="8784" class="PrimitiveType">Set</a> <a id="8788" class="Keyword">where</a> <a id="IndexedData.Boolean.BTrue"></a><a id="8794" href="slides.html#8794" class="InductiveConstructor">BTrue</a> <a id="IndexedData.Boolean.BFalse"></a><a id="8800" href="slides.html#8800" class="InductiveConstructor">BFalse</a> <a id="8807" class="Symbol">:</a> <a id="8809" href="slides.html#8774" class="Datatype">Boolean</a>
</pre>-->
<pre class="agda-code"> <a id="8836" class="Keyword">data</a> <a id="IndexedData.Vec"></a><a id="8841" href="slides.html#8841" class="Datatype">Vec</a> <a id="8845" class="Symbol">(</a><a id="8846" href="slides.html#8846" class="Bound">A</a> <a id="8848" class="Symbol">:</a> <a id="8850" class="PrimitiveType">Set</a><a id="8853" class="Symbol">)</a> <a id="8855" class="Symbol">:</a> <a id="8857" href="Agda.Builtin.Nat.html#97" class="Datatype">ℕ</a> <a id="8859" class="Symbol">→</a> <a id="8861" class="PrimitiveType">Set</a> <a id="8865" class="Keyword">where</a>
<a id="IndexedData.Vec.[]"></a><a id="8875" href="slides.html#8875" class="InductiveConstructor">[]</a> <a id="8879" class="Symbol">:</a> <a id="8881" href="slides.html#8841" class="Datatype">Vec</a> <a id="8885" href="slides.html#8846" class="Bound">A</a> <a id="8887" href="Agda.Builtin.Nat.html#115" class="InductiveConstructor">zero</a>
<a id="IndexedData.Vec._∷_"></a><a id="8896" href="slides.html#8896" class="InductiveConstructor Operator">_∷_</a> <a id="8900" class="Symbol">:</a> <a id="8902" class="Symbol">{</a><a id="8903" href="slides.html#8903" class="Bound">n</a> <a id="8905" class="Symbol">:</a> <a id="8907" href="Agda.Builtin.Nat.html#97" class="Datatype">ℕ</a><a id="8908" class="Symbol">}</a> <a id="8910" class="Symbol">→</a> <a id="8912" href="slides.html#8846" class="Bound">A</a> <a id="8914" class="Symbol">→</a> <a id="8916" href="slides.html#8841" class="Datatype">Vec</a> <a id="8920" href="slides.html#8846" class="Bound">A</a> <a id="8922" href="slides.html#8903" class="Bound">n</a> <a id="8924" class="Symbol">→</a> <a id="8926" href="slides.html#8841" class="Datatype">Vec</a> <a id="8930" href="slides.html#8846" class="Bound">A</a> <a id="8932" class="Symbol">(</a><a id="8933" href="Agda.Builtin.Nat.html#128" class="InductiveConstructor">suc</a> <a id="8937" href="slides.html#8903" class="Bound">n</a><a id="8938" class="Symbol">)</a>
</pre>
<p>(like GADTs in Haskell/Ocaml)</p>
</section><section id="dependent-pattern-matching" class="slide level2">
<h2>Dependent pattern matching</h2>
<pre class="agda-code"> <a id="IndexedData._++_"></a><a id="9016" href="slides.html#9016" class="Function Operator">_++_</a> <a id="9021" class="Symbol">:</a> <a id="9023" class="Symbol">{</a><a id="9024" href="slides.html#9024" class="Bound">A</a> <a id="9026" class="Symbol">:</a> <a id="9028" class="PrimitiveType">Set</a><a id="9031" class="Symbol">}</a> <a id="9033" class="Symbol">{</a><a id="9034" href="slides.html#9034" class="Bound">m</a> <a id="9036" href="slides.html#9036" class="Bound">n</a> <a id="9038" class="Symbol">:</a> <a id="9040" href="Agda.Builtin.Nat.html#97" class="Datatype">ℕ</a><a id="9041" class="Symbol">}</a>
<a id="9050" class="Symbol">→</a> <a id="9052" href="slides.html#8841" class="Datatype">Vec</a> <a id="9056" href="slides.html#9024" class="Bound">A</a> <a id="9058" href="slides.html#9034" class="Bound">m</a> <a id="9060" class="Symbol">→</a> <a id="9062" href="slides.html#8841" class="Datatype">Vec</a> <a id="9066" href="slides.html#9024" class="Bound">A</a> <a id="9068" href="slides.html#9036" class="Bound">n</a> <a id="9070" class="Symbol">→</a> <a id="9072" href="slides.html#8841" class="Datatype">Vec</a> <a id="9076" href="slides.html#9024" class="Bound">A</a> <a id="9078" class="Symbol">(</a><a id="9079" href="slides.html#9034" class="Bound">m</a> <a id="9081" href="Agda.Builtin.Nat.html#230" class="Primitive Operator">+</a> <a id="9083" href="slides.html#9036" class="Bound">n</a><a id="9084" class="Symbol">)</a>
<a id="9088" href="slides.html#8875" class="InductiveConstructor">[]</a> <a id="9097" href="slides.html#9016" class="Function Operator">++</a> <a id="9100" href="slides.html#9100" class="Bound">ys</a> <a id="9103" class="Symbol">=</a> <a id="9105" href="slides.html#9100" class="Bound">ys</a> <a id="9121" class="Comment">-- m = zero</a>
<a id="9135" class="Symbol">(</a><a id="9136" href="slides.html#9136" class="Bound">x</a> <a id="9138" href="slides.html#8896" class="InductiveConstructor Operator">∷</a> <a id="9140" href="slides.html#9140" class="Bound">xs</a><a id="9142" class="Symbol">)</a> <a id="9144" href="slides.html#9016" class="Function Operator">++</a> <a id="9147" href="slides.html#9147" class="Bound">ys</a> <a id="9150" class="Symbol">=</a> <a id="9152" href="slides.html#9136" class="Bound">x</a> <a id="9154" href="slides.html#8896" class="InductiveConstructor Operator">∷</a> <a id="9156" class="Symbol">(</a><a id="9157" href="slides.html#9140" class="Bound">xs</a> <a id="9160" href="slides.html#9016" class="Function Operator">++</a> <a id="9163" href="slides.html#9147" class="Bound">ys</a><a id="9165" class="Symbol">)</a> <a id="9168" class="Comment">-- m = suc m′</a>
<a id="IndexedData.head"></a><a id="9185" href="slides.html#9185" class="Function">head</a> <a id="9190" class="Symbol">:</a> <a id="9192" class="Symbol">{</a><a id="9193" href="slides.html#9193" class="Bound">A</a> <a id="9195" class="Symbol">:</a> <a id="9197" class="PrimitiveType">Set</a><a id="9200" class="Symbol">}</a> <a id="9202" class="Symbol">{</a><a id="9203" href="slides.html#9203" class="Bound">n</a> <a id="9205" class="Symbol">:</a> <a id="9207" href="Agda.Builtin.Nat.html#97" class="Datatype">ℕ</a><a id="9208" class="Symbol">}</a> <a id="9210" class="Symbol">→</a> <a id="9212" href="slides.html#8841" class="Datatype">Vec</a> <a id="9216" href="slides.html#9193" class="Bound">A</a> <a id="9218" class="Symbol">(</a><a id="9219" href="Agda.Builtin.Nat.html#128" class="InductiveConstructor">suc</a> <a id="9223" href="slides.html#9203" class="Bound">n</a><a id="9224" class="Symbol">)</a> <a id="9226" class="Symbol">→</a> <a id="9228" href="slides.html#9193" class="Bound">A</a>
<a id="9232" class="Comment">-- head [] -- Impossible!</a>
<a id="9268" href="slides.html#9185" class="Function">head</a> <a id="9273" class="Symbol">(</a><a id="9274" href="slides.html#9274" class="Bound">x</a> <a id="9276" href="slides.html#8896" class="InductiveConstructor Operator">∷</a> <a id="9278" href="slides.html#9278" class="Bound">xs</a><a id="9280" class="Symbol">)</a> <a id="9282" class="Symbol">=</a> <a id="9284" href="slides.html#9274" class="Bound">x</a>
</pre>
</section><section id="well-typed-syntax-representation" class="slide level2">
<h2>Well-typed syntax representation</h2>
<p>Our correct-by-construction typechecker produces <strong>intrinsically well-typed syntax</strong>:</p>
<pre class="agda-code"> <a id="IndexedData.Cxt"></a><a id="9421" href="slides.html#9421" class="Function">Cxt</a> <a id="9425" class="Symbol">=</a> <a id="9427" href="Agda.Builtin.List.html#80" class="Datatype">List</a> <a id="9432" href="slides.html#7609" class="Datatype">Type</a>
<a id="9440" class="Keyword">data</a> <a id="IndexedData.Exp"></a><a id="9445" href="slides.html#9445" class="Datatype">Exp</a> <a id="9449" class="Symbol">(</a><a id="9450" href="slides.html#9450" class="Bound">Γ</a> <a id="9452" class="Symbol">:</a> <a id="9454" href="slides.html#9421" class="Function">Cxt</a><a id="9457" class="Symbol">)</a> <a id="9459" class="Symbol">:</a> <a id="9461" href="slides.html#7609" class="Datatype">Type</a> <a id="9466" class="Symbol">→</a> <a id="9468" class="PrimitiveType">Set</a>
</pre>
<p>A term <code>e : Exp Γ t</code> is a <em>well-typed</em> WHILE expression in context <code>Γ</code>.</p>
</section><section id="well-typed-syntax" class="slide level2">
<h2>Well-typed syntax</h2>
<pre class="agda-code"> <a id="IndexedData.Var"></a><a id="9582" href="slides.html#9582" class="Function">Var</a> <a id="9586" class="Symbol">:</a> <a id="9588" class="Symbol">(</a><a id="9589" href="slides.html#9589" class="Bound">Γ</a> <a id="9591" class="Symbol">:</a> <a id="9593" href="slides.html#9421" class="Function">Cxt</a><a id="9596" class="Symbol">)</a> <a id="9598" class="Symbol">(</a><a id="9599" href="slides.html#9599" class="Bound">t</a> <a id="9601" class="Symbol">:</a> <a id="9603" href="slides.html#7609" class="Datatype">Type</a><a id="9607" class="Symbol">)</a> <a id="9609" class="Symbol">→</a> <a id="9611" class="PrimitiveType">Set</a>
<a id="9617" href="slides.html#9582" class="Function">Var</a> <a id="9621" href="slides.html#9621" class="Bound">Γ</a> <a id="9623" href="slides.html#9623" class="Bound">t</a> <a id="9625" class="Symbol">=</a> <a id="9627" href="slides.html#9623" class="Bound">t</a> <a id="9629" href="Data.List.Membership.Setoid.html#709" class="Function Operator">∈</a> <a id="9631" href="slides.html#9621" class="Bound">Γ</a>
<a id="9636" class="Keyword">data</a> <a id="IndexedData.Op"></a><a id="9641" href="slides.html#9641" class="Datatype">Op</a> <a id="9644" class="Symbol">:</a> <a id="9646" class="Symbol">(</a><a id="9647" href="slides.html#9647" class="Bound">dom</a> <a id="9651" href="slides.html#9651" class="Bound">codom</a> <a id="9657" class="Symbol">:</a> <a id="9659" href="slides.html#7609" class="Datatype">Type</a><a id="9663" class="Symbol">)</a> <a id="9665" class="Symbol">→</a> <a id="9667" class="PrimitiveType">Set</a> <a id="9671" class="Keyword">where</a>
<a id="IndexedData.Op.plus"></a><a id="9681" href="slides.html#9681" class="InductiveConstructor">plus</a> <a id="9687" class="Symbol">:</a> <a id="9689" href="slides.html#9641" class="Datatype">Op</a> <a id="9692" href="slides.html#7635" class="InductiveConstructor">int</a> <a id="9697" href="slides.html#7635" class="InductiveConstructor">int</a>
<a id="IndexedData.Op.gt"></a><a id="9705" href="slides.html#9705" class="InductiveConstructor">gt</a> <a id="9711" class="Symbol">:</a> <a id="9713" href="slides.html#9641" class="Datatype">Op</a> <a id="9716" href="slides.html#7635" class="InductiveConstructor">int</a> <a id="9721" href="slides.html#7630" class="InductiveConstructor">bool</a>
<a id="IndexedData.Op.and"></a><a id="9730" href="slides.html#9730" class="InductiveConstructor">and</a> <a id="9736" class="Symbol">:</a> <a id="9738" href="slides.html#9641" class="Datatype">Op</a> <a id="9741" href="slides.html#7630" class="InductiveConstructor">bool</a> <a id="9746" href="slides.html#7630" class="InductiveConstructor">bool</a>
<a id="9754" class="Keyword">data</a> <a id="9759" href="slides.html#9445" class="Datatype">Exp</a> <a id="9763" href="slides.html#9763" class="Bound">Γ</a> <a id="9765" class="Keyword">where</a>
<a id="IndexedData.Exp.eInt"></a><a id="9775" href="slides.html#9775" class="InductiveConstructor">eInt</a> <a id="9781" class="Symbol">:</a> <a id="9783" class="Symbol">(</a><a id="9784" href="slides.html#9784" class="Bound">i</a> <a id="9786" class="Symbol">:</a> <a id="9788" href="Agda.Builtin.Int.html#178" class="Datatype">ℤ</a><a id="9789" class="Symbol">)</a> <a id="9802" class="Symbol">→</a> <a id="9804" href="slides.html#9445" class="Datatype">Exp</a> <a id="9808" href="slides.html#9763" class="Bound">Γ</a> <a id="9810" href="slides.html#7635" class="InductiveConstructor">int</a>
<a id="IndexedData.Exp.eBool"></a><a id="9818" href="slides.html#9818" class="InductiveConstructor">eBool</a> <a id="9824" class="Symbol">:</a> <a id="9826" class="Symbol">(</a><a id="9827" href="slides.html#9827" class="Bound">b</a> <a id="9829" class="Symbol">:</a> <a id="9831" href="slides.html#8774" class="Datatype">Boolean</a><a id="9838" class="Symbol">)</a> <a id="9845" class="Symbol">→</a> <a id="9847" href="slides.html#9445" class="Datatype">Exp</a> <a id="9851" href="slides.html#9763" class="Bound">Γ</a> <a id="9853" href="slides.html#7630" class="InductiveConstructor">bool</a>
<a id="IndexedData.Exp.eOp"></a><a id="9862" href="slides.html#9862" class="InductiveConstructor">eOp</a> <a id="9868" class="Symbol">:</a> <a id="9870" class="Symbol">∀{</a><a id="9872" href="slides.html#9872" class="Bound">t</a> <a id="9874" href="slides.html#9874" class="Bound">t'</a><a id="9876" class="Symbol">}</a> <a id="9878" class="Symbol">(</a><a id="9879" href="slides.html#9879" class="Bound">op</a> <a id="9882" class="Symbol">:</a> <a id="9884" href="slides.html#9641" class="Datatype">Op</a> <a id="9887" href="slides.html#9872" class="Bound">t</a> <a id="9889" href="slides.html#9874" class="Bound">t'</a><a id="9891" class="Symbol">)</a>
<a id="9903" class="Symbol">→</a> <a id="9905" class="Symbol">(</a><a id="9906" href="slides.html#9906" class="Bound">e</a> <a id="9908" href="slides.html#9908" class="Bound">e'</a> <a id="9911" class="Symbol">:</a> <a id="9913" href="slides.html#9445" class="Datatype">Exp</a> <a id="9917" href="slides.html#9763" class="Bound">Γ</a> <a id="9919" href="slides.html#9872" class="Bound">t</a><a id="9920" class="Symbol">)</a> <a id="9924" class="Symbol">→</a> <a id="9926" href="slides.html#9445" class="Datatype">Exp</a> <a id="9930" href="slides.html#9763" class="Bound">Γ</a> <a id="9932" href="slides.html#9874" class="Bound">t'</a>
<a id="IndexedData.Exp.eVar"></a><a id="9939" href="slides.html#9939" class="InductiveConstructor">eVar</a> <a id="9945" class="Symbol">:</a> <a id="9947" class="Symbol">∀{</a><a id="9949" href="slides.html#9949" class="Bound">t</a><a id="9950" class="Symbol">}</a> <a id="9952" class="Symbol">(</a><a id="9953" href="slides.html#9953" class="Bound">x</a> <a id="9955" class="Symbol">:</a> <a id="9957" href="slides.html#9582" class="Function">Var</a> <a id="9961" href="slides.html#9763" class="Bound">Γ</a> <a id="9963" href="slides.html#9949" class="Bound">t</a><a id="9964" class="Symbol">)</a> <a id="9966" class="Symbol">→</a> <a id="9968" href="slides.html#9445" class="Datatype">Exp</a> <a id="9972" href="slides.html#9763" class="Bound">Γ</a> <a id="9974" href="slides.html#9949" class="Bound">t</a>
</pre>
<p>See <a href="https://jespercockx.github.io/popl19-tutorial/src/html/WellTypedSyntax.html">WellTypedSyntax.agda</a>.</p>
</section><section id="evaluating-well-typed-syntax" class="slide level2">
<h2>Evaluating well-typed syntax</h2>
<p>We can now define <code>eval</code> for well-typed expressions:</p>
<pre class="agda-code"> <a id="IndexedData.Val"></a><a id="10179" href="slides.html#10179" class="Function">Val</a> <a id="10183" class="Symbol">:</a> <a id="10185" href="slides.html#7609" class="Datatype">Type</a> <a id="10190" class="Symbol">→</a> <a id="10192" class="PrimitiveType">Set</a>
<a id="10198" href="slides.html#10179" class="Function">Val</a> <a id="10202" href="slides.html#7635" class="InductiveConstructor">int</a> <a id="10209" class="Symbol">=</a> <a id="10211" href="Agda.Builtin.Int.html#178" class="Datatype">ℤ</a>
<a id="10215" href="slides.html#10179" class="Function">Val</a> <a id="10219" href="slides.html#7630" class="InductiveConstructor">bool</a> <a id="10226" class="Symbol">=</a> <a id="10228" href="slides.html#8774" class="Datatype">Boolean</a>
<a id="IndexedData.Env"></a><a id="10239" href="slides.html#10239" class="Function">Env</a> <a id="10243" class="Symbol">:</a> <a id="10245" href="slides.html#9421" class="Function">Cxt</a> <a id="10249" class="Symbol">→</a> <a id="10251" class="PrimitiveType">Set</a>
<a id="10257" href="slides.html#10239" class="Function">Env</a> <a id="10261" class="Symbol">=</a> <a id="10263" href="Data.List.All.html#826" class="Datatype">All</a> <a id="10267" href="slides.html#10179" class="Function">Val</a>
<a id="IndexedData.eval"></a><a id="10274" href="slides.html#10274" class="Function">eval</a> <a id="10279" class="Symbol">:</a> <a id="10281" class="Symbol">∀</a> <a id="10283" class="Symbol">{</a><a id="10284" href="slides.html#10284" class="Bound">Γ</a><a id="10285" class="Symbol">}</a> <a id="10287" class="Symbol">{</a><a id="10288" href="slides.html#10288" class="Bound">t</a><a id="10289" class="Symbol">}</a> <a id="10291" class="Symbol">→</a> <a id="10293" href="slides.html#10239" class="Function">Env</a> <a id="10297" href="slides.html#10284" class="Bound">Γ</a> <a id="10299" class="Symbol">→</a> <a id="10301" href="slides.html#9445" class="Datatype">Exp</a> <a id="10305" href="slides.html#10284" class="Bound">Γ</a> <a id="10307" href="slides.html#10288" class="Bound">t</a> <a id="10309" class="Symbol">→</a> <a id="10311" href="slides.html#10179" class="Function">Val</a> <a id="10315" href="slides.html#10288" class="Bound">t</a>
<a id="10319" href="slides.html#10274" class="Function">eval</a> <a id="10324" class="Symbol">=</a> <a id="10326" href="slides.html#2730" class="Postulate">⋯</a>
</pre>
<p>that <strong>always</strong> returns a value (bye bye <code>Maybe</code>!)</p>
<p>See definition of <code>eval</code> in <a href="https://jespercockx.github.io/popl19-tutorial/src/html/Interpreter.html">Interpreter.agda</a>.</p>
</section><section id="exercise-3" class="slide level2">
<h2>Exercise #3</h2>
<p>Extend the well-typed syntax with the new syntactic constructions you added.</p>
</section></section>
<section id="break-30-min" class="title-slide slide level1"><h1>BREAK (30 min)</h1></section>
<section><section id="monads-and-instance-arguments" class="title-slide slide level1"><h1>Monads and instance arguments</h1></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-code"><a id="11086" class="Keyword">module</a> <a id="Instances"></a><a id="11093" href="slides.html#11093" class="Module">Instances</a> <a id="11103" class="Keyword">where</a>
<a id="11111" class="Keyword">open</a> <a id="11116" class="Keyword">import</a> <a id="11123" href="Data.Bool.Base.html" class="Module">Data.Bool.Base</a>
<a id="11140" class="Keyword">open</a> <a id="11145" class="Keyword">import</a> <a id="11152" href="Data.String.Base.html" class="Module">Data.String.Base</a>
</pre>-->
<pre class="agda-code"> <a id="11188" class="Keyword">record</a> <a id="Instances.Print"></a><a id="11195" href="slides.html#11195" class="Record">Print</a> <a id="11201" class="Symbol">{</a><a id="11202" href="slides.html#11202" class="Bound">ℓ</a><a id="11203" class="Symbol">}</a> <a id="11205" class="Symbol">(</a><a id="11206" href="slides.html#11206" class="Bound">A</a> <a id="11208" class="Symbol">:</a> <a id="11210" class="PrimitiveType">Set</a> <a id="11214" href="slides.html#11202" class="Bound">ℓ</a><a id="11215" class="Symbol">)</a> <a id="11217" class="Symbol">:</a> <a id="11219" class="PrimitiveType">Set</a> <a id="11223" href="slides.html#11202" class="Bound">ℓ</a> <a id="11225" class="Keyword">where</a>
<a id="11235" class="Keyword">field</a>
<a id="Instances.Print.print"></a><a id="11247" href="slides.html#11247" class="Field">print</a> <a id="11253" class="Symbol">:</a> <a id="11255" href="slides.html#11206" class="Bound">A</a> <a id="11257" class="Symbol">→</a> <a id="11259" href="Agda.Builtin.String.html#174" class="Postulate">String</a>
<a id="11268" class="Keyword">open</a> <a id="11273" href="slides.html#11195" class="Module">Print</a> <a id="11279" class="Symbol">{{...}}</a> <a id="11288" class="Comment">-- print : {{r : Print A}} → A → String</a>
<a id="11331" class="Keyword">instance</a>
<a id="Instances.PrintBool"></a><a id="11344" href="slides.html#11344" class="Function">PrintBool</a> <a id="11354" class="Symbol">:</a> <a id="11356" href="slides.html#11195" class="Record">Print</a> <a id="11362" href="Agda.Builtin.Bool.html#67" class="Datatype">Bool</a>
<a id="11371" href="slides.html#11247" class="Field">print</a> <a id="11377" class="Symbol">{{</a><a id="11379" href="slides.html#11344" class="Function">PrintBool</a><a id="11388" class="Symbol">}}</a> <a id="11391" href="Agda.Builtin.Bool.html#92" class="InductiveConstructor">true</a> <a id="11397" class="Symbol">=</a> <a id="11399" class="String">"true"</a>
<a id="11410" href="slides.html#11247" class="Field">print</a> <a id="11416" class="Symbol">{{</a><a id="11418" href="slides.html#11344" class="Function">PrintBool</a><a id="11427" class="Symbol">}}</a> <a id="11430" href="Agda.Builtin.Bool.html#86" class="InductiveConstructor">false</a> <a id="11436" class="Symbol">=</a> <a id="11438" class="String">"false"</a>
<a id="Instances.PrintString"></a><a id="11451" href="slides.html#11451" class="Function">PrintString</a> <a id="11463" class="Symbol">:</a> <a id="11465" href="slides.html#11195" class="Record">Print</a> <a id="11471" href="Agda.Builtin.String.html#174" class="Postulate">String</a>
<a id="11482" href="slides.html#11247" class="Field">print</a> <a id="11488" class="Symbol">{{</a><a id="11490" href="slides.html#11451" class="Function">PrintString</a><a id="11501" class="Symbol">}}</a> <a id="11504" href="slides.html#11504" class="Bound">x</a> <a id="11506" class="Symbol">=</a> <a id="11508" href="slides.html#11504" class="Bound">x</a>
<a id="Instances.testPrint"></a><a id="11513" href="slides.html#11513" class="Function">testPrint</a> <a id="11523" class="Symbol">:</a> <a id="11525" href="Agda.Builtin.String.html#174" class="Postulate">String</a>
<a id="11534" href="slides.html#11513" class="Function">testPrint</a> <a id="11544" class="Symbol">=</a> <a id="11546" class="Symbol">(</a><a id="11547" href="slides.html#11247" class="Field">print</a> <a id="11553" href="Agda.Builtin.Bool.html#92" class="InductiveConstructor">true</a><a id="11557" class="Symbol">)</a> <a id="11559" href="Data.String.Base.html#1089" class="Function Operator">++</a> <a id="11562" class="Symbol">(</a><a id="11563" href="slides.html#11247" class="Field">print</a> <a id="11569" class="String">"a string"</a><a id="11579" class="Symbol">)</a>
</pre>
</section><section id="monads" class="slide level2">
<h2>Monads</h2>
<p><code>Monad</code> is a typeclass with two fields <code>return</code> and <code>_>>=_</code>.</p>
<p>Example: <code>Error</code> monad (see <a href="https://jespercockx.github.io/popl19-tutorial/src/html/Library.Error.html">Library/Error.agda</a>)</p>
</section><section id="correct-by-construction-typechecker" class="slide level2">
<h2>Correct-by-construction typechecker</h2>
<p>See <a href="https://jespercockx.github.io/popl19-tutorial/src/html/TypeChecker.html">TypeChecker.agda</a>.</p>
</section><section id="exercise-4" class="slide level2">
<h2>Exercise #4</h2>
<p>Extend the typechecker with rules for the new syntactic constructions you added.</p>
</section></section>
<section><section id="coinduction-and-sized-types" class="title-slide slide level1"><h1>Coinduction and sized types</h1></section><section id="coinduction-in-agda" class="slide level2">
<h2>Coinduction in Agda</h2>
<p>Coinductive type may contain infinitely deep values (non well-founded trees)</p>
<!--
<pre class="agda-code"><a id="12167" class="Keyword">module</a> <a id="Coinduction"></a><a id="12174" href="slides.html#12174" class="Module">Coinduction</a> <a id="12186" class="Keyword">where</a>
<a id="12194" class="Keyword">module</a> <a id="GuardedStream"></a><a id="12201" href="slides.html#12201" class="Module">GuardedStream</a> <a id="12215" class="Keyword">where</a>
</pre>-->
<pre class="agda-code"> <a id="12242" class="Keyword">record</a> <a id="Coinduction.GuardedStream.Stream"></a><a id="12249" href="slides.html#12249" class="Record">Stream</a> <a id="12256" class="Symbol">(</a><a id="12257" href="slides.html#12257" class="Bound">A</a> <a id="12259" class="Symbol">:</a> <a id="12261" class="PrimitiveType">Set</a><a id="12264" class="Symbol">)</a> <a id="12266" class="Symbol">:</a> <a id="12268" class="PrimitiveType">Set</a> <a id="12272" class="Keyword">where</a>
<a id="12284" class="Keyword">coinductive</a>
<a id="12302" class="Keyword">field</a>
<a id="Coinduction.GuardedStream.Stream.head"></a><a id="12316" href="slides.html#12316" class="Field">head</a> <a id="12321" class="Symbol">:</a> <a id="12323" href="slides.html#12257" class="Bound">A</a>
<a id="Coinduction.GuardedStream.Stream.tail"></a><a id="12333" href="slides.html#12333" class="Field">tail</a> <a id="12338" class="Symbol">:</a> <a id="12340" href="slides.html#12249" class="Record">Stream</a> <a id="12347" href="slides.html#12257" class="Bound">A</a>
<a id="12353" class="Keyword">open</a> <a id="12358" href="slides.html#12249" class="Module">Stream</a>
<a id="Coinduction.GuardedStream.repeat"></a><a id="12370" href="slides.html#12370" class="Function">repeat</a> <a id="12377" class="Symbol">:</a> <a id="12379" class="Symbol">{</a><a id="12380" href="slides.html#12380" class="Bound">A</a> <a id="12382" class="Symbol">:</a> <a id="12384" class="PrimitiveType">Set</a><a id="12387" class="Symbol">}</a> <a id="12389" class="Symbol">→</a> <a id="12391" href="slides.html#12380" class="Bound">A</a> <a id="12393" class="Symbol">→</a> <a id="12395" href="slides.html#12249" class="Record">Stream</a> <a id="12402" href="slides.html#12380" class="Bound">A</a>
<a id="12408" href="slides.html#12370" class="Function">repeat</a> <a id="12415" href="slides.html#12415" class="Bound">x</a> <a id="12417" class="Symbol">.</a><a id="12418" href="slides.html#12316" class="Field">head</a> <a id="12423" class="Symbol">=</a> <a id="12425" href="slides.html#12415" class="Bound">x</a>
<a id="12431" href="slides.html#12370" class="Function">repeat</a> <a id="12438" href="slides.html#12438" class="Bound">x</a> <a id="12440" class="Symbol">.</a><a id="12441" href="slides.html#12333" class="Field">tail</a> <a id="12446" class="Symbol">=</a> <a id="12448" href="slides.html#12370" class="Function">repeat</a> <a id="12455" href="slides.html#12438" class="Bound">x</a>
</pre>
</section><section id="dealing-with-infinite-computations" class="slide level2">
<h2>Dealing with infinite computations</h2>
<p>Remember: all Agda functions must be <strong>total</strong></p>
<p>⇒ interpreter for WHILE takes <code>fuel</code> argument</p>
<p>Can we do better?</p>
</section><section id="going-carbon-free-with-the-delay-monad" class="slide level2">
<h2>Going carbon-free with the <code>Delay</code> monad</h2>
<p><strong>Monads</strong> allow us to use effects in a pure language</p>
<p>The <code>Delay</code> monad captures the effect of <em>non-termination</em></p>
<p>A value of type <code>Delay A</code> is</p>
<ul>
<li>either a value of type <code>A</code> produced <strong>now</strong></li>
<li>or a computation of type <code>Delay A</code> producing a value <strong>later</strong></li>
</ul>
</section><section id="the-delay-monad-definition" class="slide level2">
<h2>The Delay monad: definition</h2>
<pre class="agda-code"> <a id="12959" class="Keyword">mutual</a>
<a id="12970" class="Keyword">record</a> <a id="Coinduction.Delay"></a><a id="12977" href="slides.html#12977" class="Record">Delay</a> <a id="12983" class="Symbol">(</a><a id="12984" href="slides.html#12984" class="Bound">A</a> <a id="12986" class="Symbol">:</a> <a id="12988" class="PrimitiveType">Set</a><a id="12991" class="Symbol">)</a> <a id="12993" class="Symbol">:</a> <a id="12995" class="PrimitiveType">Set</a> <a id="12999" class="Keyword">where</a>
<a id="13011" class="Keyword">coinductive</a>
<a id="13029" class="Keyword">field</a> <a id="Coinduction.Delay.force"></a><a id="13035" href="slides.html#13035" class="Field">force</a> <a id="13041" class="Symbol">:</a> <a id="13043" href="slides.html#13062" class="Datatype">Delay'</a> <a id="13050" href="slides.html#12984" class="Bound">A</a>
<a id="13057" class="Keyword">data</a> <a id="Coinduction.Delay'"></a><a id="13062" href="slides.html#13062" class="Datatype">Delay'</a> <a id="13069" class="Symbol">(</a><a id="13070" href="slides.html#13070" class="Bound">A</a> <a id="13072" class="Symbol">:</a> <a id="13074" class="PrimitiveType">Set</a><a id="13077" class="Symbol">)</a> <a id="13079" class="Symbol">:</a> <a id="13081" class="PrimitiveType">Set</a> <a id="13085" class="Keyword">where</a>
<a id="Coinduction.Delay'.now"></a><a id="13097" href="slides.html#13097" class="InductiveConstructor">now</a> <a id="13103" class="Symbol">:</a> <a id="13105" href="slides.html#13070" class="Bound">A</a> <a id="13113" class="Symbol">→</a> <a id="13115" href="slides.html#13062" class="Datatype">Delay'</a> <a id="13122" href="slides.html#13070" class="Bound">A</a>
<a id="Coinduction.Delay'.later"></a><a id="13130" href="slides.html#13130" class="InductiveConstructor">later</a> <a id="13136" class="Symbol">:</a> <a id="13138" href="slides.html#12977" class="Record">Delay</a> <a id="13144" href="slides.html#13070" class="Bound">A</a> <a id="13146" class="Symbol">→</a> <a id="13148" href="slides.html#13062" class="Datatype">Delay'</a> <a id="13155" href="slides.html#13070" class="Bound">A</a>
<a id="13160" class="Keyword">open</a> <a id="13165" href="slides.html#12977" class="Module">Delay</a> <a id="13171" class="Keyword">public</a>
<a id="Coinduction.never"></a><a id="13181" href="slides.html#13181" class="Function">never</a> <a id="13187" class="Symbol">:</a> <a id="13189" class="Symbol">{</a><a id="13190" href="slides.html#13190" class="Bound">A</a> <a id="13192" class="Symbol">:</a> <a id="13194" class="PrimitiveType">Set</a><a id="13197" class="Symbol">}</a> <a id="13199" class="Symbol">→</a> <a id="13201" href="slides.html#12977" class="Record">Delay</a> <a id="13207" href="slides.html#13190" class="Bound">A</a>
<a id="13211" href="slides.html#13181" class="Function">never</a> <a id="13217" class="Symbol">.</a><a id="13218" href="slides.html#13035" class="Field">force</a> <a id="13224" class="Symbol">=</a> <a id="13226" href="slides.html#13130" class="InductiveConstructor">later</a> <a id="13232" href="slides.html#13181" class="Function">never</a>
</pre>
</section><section id="sized-types" class="slide level2">
<h2>Sized types</h2>
<p>Totality requirement: coinductive definitions should be <strong>productive</strong>: computing each observation should be terminating.</p>
<p>To ensure this, Agda checks that corecursive calls are <strong>guarded by constructors</strong>, but this is often quite limiting.</p>
<p>A more flexible and modular approach is to use <strong>sized types</strong>.</p>
</section><section id="the-type-size" class="slide level2">
<h2>The type <code>Size</code></h2>
<p><code>Size</code> ≃ abstract version of the natural numbers extended with <code>∞</code></p>
<p>For each <code>i : Size</code>, we have a type <code>Size< i</code> of sizes <strong>smaller than <code>i</code></strong>.</p>
<p><strong>Note</strong>: pattern matching on <code>Size</code> is not allowed!</p>
</section><section id="the-sized-delay-monad" class="slide level2">
<h2>The sized delay monad</h2>
<!--
<pre class="agda-code"><a id="13825" class="Keyword">module</a> <a id="SizedTypes"></a><a id="13832" href="slides.html#13832" class="Module">SizedTypes</a> <a id="13843" class="Keyword">where</a>
<a id="13851" class="Keyword">open</a> <a id="13856" class="Keyword">import</a> <a id="13863" href="Size.html" class="Module">Size</a>
</pre>-->
<pre class="agda-code"> <a id="13887" class="Keyword">mutual</a>
<a id="13898" class="Keyword">record</a> <a id="SizedTypes.Delay"></a><a id="13905" href="slides.html#13905" class="Record">Delay</a> <a id="13911" class="Symbol">(</a><a id="13912" href="slides.html#13912" class="Bound">i</a> <a id="13914" class="Symbol">:</a> <a id="13916" href="Agda.Builtin.Size.html#114" class="Postulate">Size</a><a id="13920" class="Symbol">)</a> <a id="13922" class="Symbol">(</a><a id="13923" href="slides.html#13923" class="Bound">A</a> <a id="13925" class="Symbol">:</a> <a id="13927" class="PrimitiveType">Set</a><a id="13930" class="Symbol">)</a> <a id="13932" class="Symbol">:</a> <a id="13934" class="PrimitiveType">Set</a> <a id="13938" class="Keyword">where</a>
<a id="13950" class="Keyword">coinductive</a>
<a id="13968" class="Keyword">constructor</a> <a id="SizedTypes.Delay.delay"></a><a id="13980" href="slides.html#13980" class="CoinductiveConstructor">delay</a>
<a id="13992" class="Keyword">field</a>
<a id="SizedTypes.Delay.force"></a><a id="14006" href="slides.html#14006" class="Field">force</a> <a id="14012" class="Symbol">:</a> <a id="14014" class="Symbol">{</a><a id="14015" href="slides.html#14015" class="Bound">j</a> <a id="14017" class="Symbol">:</a> <a id="14019" href="Agda.Builtin.Size.html#146" class="Postulate Operator">Size<</a> <a id="14025" href="slides.html#13912" class="Bound">i</a><a id="14026" class="Symbol">}</a> <a id="14028" class="Symbol">→</a> <a id="14030" href="slides.html#14051" class="Datatype">Delay'</a> <a id="14037" href="slides.html#14015" class="Bound">j</a> <a id="14039" href="slides.html#13923" class="Bound">A</a>
<a id="14046" class="Keyword">data</a> <a id="SizedTypes.Delay'"></a><a id="14051" href="slides.html#14051" class="Datatype">Delay'</a> <a id="14058" class="Symbol">(</a><a id="14059" href="slides.html#14059" class="Bound">i</a> <a id="14061" class="Symbol">:</a> <a id="14063" href="Agda.Builtin.Size.html#114" class="Postulate">Size</a><a id="14067" class="Symbol">)</a> <a id="14069" class="Symbol">(</a><a id="14070" href="slides.html#14070" class="Bound">A</a> <a id="14072" class="Symbol">:</a> <a id="14074" class="PrimitiveType">Set</a><a id="14077" class="Symbol">)</a> <a id="14079" class="Symbol">:</a> <a id="14081" class="PrimitiveType">Set</a> <a id="14085" class="Keyword">where</a>
<a id="SizedTypes.Delay'.return'"></a><a id="14097" href="slides.html#14097" class="InductiveConstructor">return'</a> <a id="14105" class="Symbol">:</a> <a id="14107" href="slides.html#14070" class="Bound">A</a> <a id="14117" class="Symbol">→</a> <a id="14119" href="slides.html#14051" class="Datatype">Delay'</a> <a id="14126" href="slides.html#14059" class="Bound">i</a> <a id="14128" href="slides.html#14070" class="Bound">A</a>
<a id="SizedTypes.Delay'.later'"></a><a id="14136" href="slides.html#14136" class="InductiveConstructor">later'</a> <a id="14144" class="Symbol">:</a> <a id="14146" href="slides.html#13905" class="Record">Delay</a> <a id="14152" href="slides.html#14059" class="Bound">i</a> <a id="14154" href="slides.html#14070" class="Bound">A</a> <a id="14156" class="Symbol">→</a> <a id="14158" href="slides.html#14051" class="Datatype">Delay'</a> <a id="14165" href="slides.html#14059" class="Bound">i</a> <a id="14167" href="slides.html#14070" class="Bound">A</a>
</pre>
<p><code>i</code> ≃ how many more steps are we allowed to observe.</p>
<p><code>Delay ∞ A</code> is the type of computations that can take <em>any</em> number of steps.</p>
</section><section id="interpreting-well-typed-while-programs" class="slide level2">
<h2>Interpreting well-typed WHILE programs</h2>
<p>WHILE statements can have two effects:</p>
<ul>
<li>Modify the environment ⇒ <code>State</code> monad</li>
<li>Go into a loop ⇒ <code>Delay</code> monad</li>
</ul>
<p>We combine both effects in the <code>Exec</code> monad.</p>
</section><section id="the-exec-monad" class="slide level2">
<h2>The <code>Exec</code> monad</h2>
<!--
<pre class="agda-code"> <a id="14557" class="Keyword">open</a> <a id="14562" class="Keyword">import</a> <a id="14569" href="Data.Unit.html" class="Module">Data.Unit</a>
<a id="14581" class="Keyword">open</a> <a id="14586" class="Keyword">import</a> <a id="14593" href="Data.Integer.Base.html" class="Module">Data.Integer.Base</a>
<a id="14613" class="Keyword">open</a> <a id="14618" href="slides.html#8508" class="Module">IndexedData</a>
<a id="14632" class="Keyword">postulate</a>
<a id="SizedTypes.Stm"></a><a id="14646" href="slides.html#14646" class="Postulate">Stm</a> <a id="14650" class="Symbol">:</a> <a id="14652" href="slides.html#9421" class="Function">Cxt</a> <a id="14656" class="Symbol">→</a> <a id="14658" class="PrimitiveType">Set</a>
<a id="SizedTypes.Program"></a><a id="14666" href="slides.html#14666" class="Postulate">Program</a> <a id="14674" class="Symbol">:</a> <a id="14676" class="PrimitiveType">Set</a>
</pre>-->
<pre class="agda-code"> <a id="14699" class="Keyword">record</a> <a id="SizedTypes.Exec"></a><a id="14706" href="slides.html#14706" class="Record">Exec</a> <a id="14711" class="Symbol">{</a><a id="14712" href="slides.html#14712" class="Bound">Γ</a> <a id="14714" class="Symbol">:</a> <a id="14716" href="slides.html#9421" class="Function">Cxt</a><a id="14719" class="Symbol">}</a> <a id="14721" class="Symbol">(</a><a id="14722" href="slides.html#14722" class="Bound">i</a> <a id="14724" class="Symbol">:</a> <a id="14726" href="Agda.Builtin.Size.html#114" class="Postulate">Size</a><a id="14730" class="Symbol">)</a> <a id="14732" class="Symbol">(</a><a id="14733" href="slides.html#14733" class="Bound">A</a> <a id="14735" class="Symbol">:</a> <a id="14737" class="PrimitiveType">Set</a><a id="14740" class="Symbol">)</a> <a id="14742" class="Symbol">:</a> <a id="14744" class="PrimitiveType">Set</a> <a id="14748" class="Keyword">where</a>
<a id="14758" class="Keyword">field</a>
<a id="SizedTypes.Exec.runExec"></a><a id="14770" href="slides.html#14770" class="Field">runExec</a> <a id="14778" class="Symbol">:</a> <a id="14780" class="Symbol">(</a><a id="14781" href="slides.html#14781" class="Bound">ρ</a> <a id="14783" class="Symbol">:</a> <a id="14785" href="slides.html#10239" class="Function">Env</a> <a id="14789" href="slides.html#14712" class="Bound">Γ</a><a id="14790" class="Symbol">)</a> <a id="14792" class="Symbol">→</a> <a id="14794" href="slides.html#13905" class="Record">Delay</a> <a id="14800" href="slides.html#14722" class="Bound">i</a> <a id="14802" class="Symbol">(</a><a id="14803" href="slides.html#14733" class="Bound">A</a> <a id="14805" href="Data.Product.html#1353" class="Function Operator">×</a> <a id="14807" href="slides.html#10239" class="Function">Env</a> <a id="14811" href="slides.html#14712" class="Bound">Γ</a><a id="14812" class="Symbol">)</a>
<a id="14816" class="Keyword">open</a> <a id="14821" href="slides.html#14706" class="Module">Exec</a> <a id="14826" class="Keyword">public</a>
<a id="SizedTypes.execStm"></a><a id="14836" href="slides.html#14836" class="Function">execStm</a> <a id="14844" class="Symbol">:</a> <a id="14846" class="Symbol">∀</a> <a id="14848" class="Symbol">{</a><a id="14849" href="slides.html#14849" class="Bound">Γ</a><a id="14850" class="Symbol">}</a> <a id="14852" class="Symbol">{</a><a id="14853" href="slides.html#14853" class="Bound">i</a><a id="14854" class="Symbol">}</a> <a id="14856" class="Symbol">(</a><a id="14857" href="slides.html#14857" class="Bound">s</a> <a id="14859" class="Symbol">:</a> <a id="14861" href="slides.html#14646" class="Postulate">Stm</a> <a id="14865" href="slides.html#14849" class="Bound">Γ</a><a id="14866" class="Symbol">)</a> <a id="14868" class="Symbol">→</a> <a id="14870" href="slides.html#14706" class="Record">Exec</a> <a id="14875" class="Symbol">{</a><a id="14876" href="slides.html#14849" class="Bound">Γ</a><a id="14877" class="Symbol">}</a> <a id="14879" href="slides.html#14853" class="Bound">i</a> <a id="14881" href="Agda.Builtin.Unit.html#69" class="Record">⊤</a>
<a id="14885" href="slides.html#14836" class="Function">execStm</a> <a id="14893" class="Symbol">=</a> <a id="14895" href="slides.html#2730" class="Postulate">⋯</a>
<a id="SizedTypes.execPrg"></a><a id="14900" href="slides.html#14900" class="Function">execPrg</a> <a id="14908" class="Symbol">:</a> <a id="14910" class="Symbol">∀</a> <a id="14912" class="Symbol">{</a><a id="14913" href="slides.html#14913" class="Bound">i</a><a id="14914" class="Symbol">}</a> <a id="14916" class="Symbol">(</a><a id="14917" href="slides.html#14917" class="Bound">prg</a> <a id="14921" class="Symbol">:</a> <a id="14923" href="slides.html#14666" class="Postulate">Program</a><a id="14930" class="Symbol">)</a> <a id="14932" class="Symbol">→</a> <a id="14934" href="slides.html#13905" class="Record">Delay</a> <a id="14940" href="slides.html#14913" class="Bound">i</a> <a id="14942" href="Agda.Builtin.Int.html#178" class="Datatype">ℤ</a>
<a id="14946" href="slides.html#14900" class="Function">execPrg</a> <a id="14954" href="slides.html#14954" class="Bound">prg</a> <a id="14958" class="Symbol">=</a> <a id="14960" href="slides.html#2730" class="Postulate">⋯</a>
</pre>
<p>See <a href="https://jespercockx.github.io/popl19-tutorial/src/html/Interpreter.html">Interpreter.agda</a> for full code.</p>
</section><section id="exercise-5" class="slide level2">
<h2>Exercise #5</h2>
<p>Extend the interpreter with rules for the new syntactic constructions you added.</p>
</section></section>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/reveal.js/lib/js/head.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/reveal.js/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.2/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://cdn.jsdelivr.net/npm/reveal.js/lib/js/classList.js', condition: function() { return !document.body.classList; } },
{ src: 'https://cdn.jsdelivr.net/npm/reveal.js/plugin/zoom-js/zoom.js', async: true },
{ src: 'https://cdn.jsdelivr.net/npm/reveal.js/plugin/math/math.js', async: true },
{ src: 'https://cdn.jsdelivr.net/npm/reveal.js/plugin/notes/notes.js', async: true }
]
});
</script>
</body>
</html>