-
Notifications
You must be signed in to change notification settings - Fork 4
/
slides2.html
249 lines (237 loc) · 33.1 KB
/
slides2.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="generator" content="pandoc">
<meta name="author" content="Jesper Cockx">
<meta name="dcterms.date" content="2019-09-01">
<title>Correct-by-construction programming in Agda</title>
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, minimal-ui">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/reveal.js/3.7.0//css/reveal.css">
<style type="text/css">
code{white-space: pre-wrap;}
span.smallcaps{font-variant: small-caps;}
span.underline{text-decoration: underline;}
div.column{display: inline-block; vertical-align: top; width: 50%;}
</style>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/reveal.js/3.7.0//css/theme/black.css" id="theme">
<link rel="stylesheet" href="white.css"/>
<link rel="stylesheet" href="Agda.css"/>
<link rel="stylesheet" href="patchTheme.css"/>
<!-- Printing and PDF exports -->
<script>
var link = document.createElement( 'link' );
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = window.location.search.match( /print-pdf/gi ) ? 'https://cdnjs.cloudflare.com/ajax/libs/reveal.js/3.7.0//css/print/pdf.css' : 'https://cdnjs.cloudflare.com/ajax/libs/reveal.js/3.7.0//css/print/paper.css';
document.getElementsByTagName( 'head' )[0].appendChild( link );
</script>
<!--[if lt IE 9]>
<script src="https://cdnjs.cloudflare.com/ajax/libs/reveal.js/3.7.0//lib/js/html5shiv.js"></script>
<![endif]-->
</head>
<body>
<div class="reveal">
<div class="slides">
<section id="title-slide">
<h1 class="title">Correct-by-construction programming in Agda</h1>
<p class="subtitle">Lecture 2: indexed datatypes and dependent pattern matching</p>
<p class="author">Jesper Cockx</p>
<p class="date">1 September 2019</p>
</section>
<section><section id="recap-correct-by-construction-programming" class="title-slide slide level1"><h1>Recap: correct-by-construction programming</h1></section><section id="indices-capture-invariants" class="slide level2">
<h2>Indices capture invariants</h2>
<p>The indices of a datatype capture important invariants of our programs:</p>
<ul>
<li>The length of a list</li>
<li>The lower and upper bounds of a search tree</li>
<li>The type of a syntax tree (!)</li>
</ul>
</section><section id="extrinsic-vs-intrinsic-verification" class="slide level2">
<h2>Extrinsic vs intrinsic verification</h2>
<p>Two styles of verification:</p>
<ul>
<li><strong>Extrinsic</strong>: write a program and then prove its properties</li>
<li><strong>Intrinsic</strong>: define properties at the type-level and write programs that satisfy them <em>by construction</em></li>
</ul>
<p>Intrinsic verification is a good fit for <strong>complex</strong> programs with <strong>simple</strong> invariants</p>
<p>For small programs and/or complex invariants, extrinsic verification may work better</p>
</section><section id="let-the-types-guide-you" class="slide level2">
<h2>Let the types guide you</h2>
<p>By encoding invariants in the types, they can guide the construction of our programs:</p>
<ul>
<li>Rule out impossible cases (absurd patterns <code>()</code>)</li>
<li>Automatic case splitting (C-c C-c)</li>
<li>Program inference (C-c C-a)</li>
<li>…</li>
</ul>
</section></section>
<section><section id="prototype-indexed-datatype-length-indexed-vectors" class="title-slide slide level1"><h1>Prototype indexed datatype: length-indexed vectors</h1></section><section id="what-are-vectors" class="slide level2">
<h2>What are vectors?</h2>
<!--
<pre class="Agda"><a id="1260" class="Keyword">open</a> <a id="1265" class="Keyword">import</a> <a id="1272" href="Data.Bool.html" class="Module">Data.Bool</a> <a id="1282" class="Keyword">using</a> <a id="1288" class="Symbol">(</a><a id="1289" href="Agda.Builtin.Bool.html#135" class="Datatype">Bool</a><a id="1293" class="Symbol">;</a> <a id="1295" href="Agda.Builtin.Bool.html#160" class="InductiveConstructor">true</a><a id="1299" class="Symbol">;</a> <a id="1301" href="Agda.Builtin.Bool.html#154" class="InductiveConstructor">false</a><a id="1306" class="Symbol">)</a>
<a id="1308" class="Keyword">open</a> <a id="1313" class="Keyword">import</a> <a id="1320" href="Data.Nat.html" class="Module">Data.Nat</a> <a id="1329" class="Keyword">using</a> <a id="1335" class="Symbol">(</a><a id="1336" href="Agda.Builtin.Nat.html#165" class="Datatype">ℕ</a><a id="1337" class="Symbol">;</a> <a id="1339" href="Agda.Builtin.Nat.html#183" class="InductiveConstructor">zero</a><a id="1343" class="Symbol">;</a> <a id="1345" href="Agda.Builtin.Nat.html#196" class="InductiveConstructor">suc</a><a id="1348" class="Symbol">)</a>
<a id="1350" class="Keyword">open</a> <a id="1355" class="Keyword">import</a> <a id="1362" href="Data.Integer.html" class="Module">Data.Integer</a> <a id="1375" class="Keyword">using</a> <a id="1381" class="Symbol">(</a><a id="1382" href="Agda.Builtin.Int.html#219" class="Datatype">ℤ</a><a id="1383" class="Symbol">)</a>
<a id="1386" class="Keyword">postulate</a>
<a id="⋯"></a><a id="1398" href="slides2.html#1398" class="Postulate">⋯</a> <a id="1400" class="Symbol">:</a> <a id="1402" class="Symbol">∀</a> <a id="1404" class="Symbol">{</a><a id="1405" href="slides2.html#1405" class="Bound">ℓ</a><a id="1406" class="Symbol">}</a> <a id="1408" class="Symbol">{</a><a id="1409" href="slides2.html#1409" class="Bound">A</a> <a id="1411" class="Symbol">:</a> <a id="1413" class="PrimitiveType">Set</a> <a id="1417" href="slides2.html#1405" class="Bound">ℓ</a><a id="1418" class="Symbol">}</a> <a id="1420" class="Symbol">→</a> <a id="1422" href="slides2.html#1409" class="Bound">A</a>
</pre>-->
If <code>n : ℕ</code>, then <code>Vec A n</code> consists of vectors of <code>A</code>s of length <code>n</code>:
<pre class="Agda"><a id="1507" class="Keyword">data</a> <a id="Vec"></a><a id="1512" href="slides2.html#1512" class="Datatype">Vec</a> <a id="1516" class="Symbol">(</a><a id="1517" href="slides2.html#1517" class="Bound">A</a> <a id="1519" class="Symbol">:</a> <a id="1521" class="PrimitiveType">Set</a><a id="1524" class="Symbol">)</a> <a id="1526" class="Symbol">:</a> <a id="1528" href="Agda.Builtin.Nat.html#165" class="Datatype">ℕ</a> <a id="1530" class="Symbol">→</a> <a id="1532" class="PrimitiveType">Set</a> <a id="1536" class="Keyword">where</a>
<a id="Vec.[]"></a><a id="1544" href="slides2.html#1544" class="InductiveConstructor">[]</a> <a id="1548" class="Symbol">:</a> <a id="1550" href="slides2.html#1512" class="Datatype">Vec</a> <a id="1554" href="slides2.html#1517" class="Bound">A</a> <a id="1556" class="Number">0</a>
<a id="Vec._∷_"></a><a id="1560" href="slides2.html#1560" class="InductiveConstructor Operator">_∷_</a> <a id="1564" class="Symbol">:</a> <a id="1566" class="Symbol">∀</a> <a id="1568" class="Symbol">{</a><a id="1569" href="slides2.html#1569" class="Bound">n</a><a id="1570" class="Symbol">}</a> <a id="1572" class="Symbol">→</a> <a id="1574" href="slides2.html#1517" class="Bound">A</a> <a id="1576" class="Symbol">→</a> <a id="1578" href="slides2.html#1512" class="Datatype">Vec</a> <a id="1582" href="slides2.html#1517" class="Bound">A</a> <a id="1584" href="slides2.html#1569" class="Bound">n</a> <a id="1586" class="Symbol">→</a> <a id="1588" href="slides2.html#1512" class="Datatype">Vec</a> <a id="1592" href="slides2.html#1517" class="Bound">A</a> <a id="1594" class="Symbol">(</a><a id="1595" href="Agda.Builtin.Nat.html#196" class="InductiveConstructor">suc</a> <a id="1599" href="slides2.html#1569" class="Bound">n</a><a id="1600" class="Symbol">)</a>
</pre>
Compare to lists:
<pre class="Agda"><a id="1629" class="Keyword">data</a> <a id="List"></a><a id="1634" href="slides2.html#1634" class="Datatype">List</a> <a id="1639" class="Symbol">(</a><a id="1640" href="slides2.html#1640" class="Bound">A</a> <a id="1642" class="Symbol">:</a> <a id="1644" class="PrimitiveType">Set</a><a id="1647" class="Symbol">)</a> <a id="1649" class="Symbol">:</a> <a id="1651" class="PrimitiveType">Set</a> <a id="1655" class="Keyword">where</a>
<a id="List.[]"></a><a id="1663" href="slides2.html#1663" class="InductiveConstructor">[]</a> <a id="1667" class="Symbol">:</a> <a id="1669" href="slides2.html#1634" class="Datatype">List</a> <a id="1674" href="slides2.html#1640" class="Bound">A</a>
<a id="List._∷_"></a><a id="1678" href="slides2.html#1678" class="InductiveConstructor Operator">_∷_</a> <a id="1682" class="Symbol">:</a> <a id="1684" href="slides2.html#1640" class="Bound">A</a> <a id="1686" class="Symbol">→</a> <a id="1688" href="slides2.html#1634" class="Datatype">List</a> <a id="1693" href="slides2.html#1640" class="Bound">A</a> <a id="1695" class="Symbol">→</a> <a id="1697" href="slides2.html#1634" class="Datatype">List</a> <a id="1702" href="slides2.html#1640" class="Bound">A</a>
</pre>
</section><section id="functions-on-vectors" class="slide level2">
<h2>Functions on vectors</h2>
<p><strong>Question</strong>: what should be the type of <code>head</code> and <code>tail</code> functions on <code>Vec</code>?</p>
<p>How about <code>_++_</code> (append) and <code>map</code>?</p>
</section><section id="indexing-into-vectors" class="slide level2">
<h2>Indexing into vectors</h2>
An index into a vector of length <code>n</code> is a number between <code>0</code> and <code>n-1</code>:
<pre class="Agda"><a id="1954" class="Keyword">data</a> <a id="Fin"></a><a id="1959" href="slides2.html#1959" class="Datatype">Fin</a> <a id="1963" class="Symbol">:</a> <a id="1965" href="Agda.Builtin.Nat.html#165" class="Datatype">ℕ</a> <a id="1967" class="Symbol">→</a> <a id="1969" class="PrimitiveType">Set</a> <a id="1973" class="Keyword">where</a>
<a id="Fin.zero"></a><a id="1981" href="slides2.html#1981" class="InductiveConstructor">zero</a> <a id="1986" class="Symbol">:</a> <a id="1988" class="Symbol">∀</a> <a id="1990" class="Symbol">{</a><a id="1991" href="slides2.html#1991" class="Bound">n</a><a id="1992" class="Symbol">}</a> <a id="1994" class="Symbol">→</a> <a id="1996" href="slides2.html#1959" class="Datatype">Fin</a> <a id="2000" class="Symbol">(</a><a id="2001" href="Agda.Builtin.Nat.html#196" class="InductiveConstructor">suc</a> <a id="2005" href="slides2.html#1991" class="Bound">n</a><a id="2006" class="Symbol">)</a>
<a id="Fin.suc"></a><a id="2010" href="slides2.html#2010" class="InductiveConstructor">suc</a> <a id="2015" class="Symbol">:</a> <a id="2017" class="Symbol">∀</a> <a id="2019" class="Symbol">{</a><a id="2020" href="slides2.html#2020" class="Bound">n</a><a id="2021" class="Symbol">}</a> <a id="2023" class="Symbol">→</a> <a id="2025" href="slides2.html#1959" class="Datatype">Fin</a> <a id="2029" href="slides2.html#2020" class="Bound">n</a> <a id="2031" class="Symbol">→</a> <a id="2033" href="slides2.html#1959" class="Datatype">Fin</a> <a id="2037" class="Symbol">(</a><a id="2038" href="Agda.Builtin.Nat.html#196" class="InductiveConstructor">suc</a> <a id="2042" href="slides2.html#2020" class="Bound">n</a><a id="2043" class="Symbol">)</a>
<a id="lookup"></a><a id="2046" href="slides2.html#2046" class="Function">lookup</a> <a id="2053" class="Symbol">:</a> <a id="2055" class="Symbol">∀</a> <a id="2057" class="Symbol">{</a><a id="2058" href="slides2.html#2058" class="Bound">A</a><a id="2059" class="Symbol">}</a> <a id="2061" class="Symbol">{</a><a id="2062" href="slides2.html#2062" class="Bound">n</a><a id="2063" class="Symbol">}</a> <a id="2065" class="Symbol">→</a> <a id="2067" href="slides2.html#1512" class="Datatype">Vec</a> <a id="2071" href="slides2.html#2058" class="Bound">A</a> <a id="2073" href="slides2.html#2062" class="Bound">n</a> <a id="2075" class="Symbol">→</a> <a id="2077" href="slides2.html#1959" class="Datatype">Fin</a> <a id="2081" href="slides2.html#2062" class="Bound">n</a> <a id="2083" class="Symbol">→</a> <a id="2085" href="slides2.html#2058" class="Bound">A</a>
<a id="2087" href="slides2.html#2046" class="Function">lookup</a> <a id="2094" href="slides2.html#2094" class="Bound">xs</a> <a id="2097" href="slides2.html#2097" class="Bound">i</a> <a id="2099" class="Symbol">=</a> <a id="2101" href="slides2.html#1398" class="Postulate">⋯</a>
</pre>
<p><code>lookup</code> is a total function!</p>
</section></section>
<section><section id="intrinsically-well-typed-syntax" class="title-slide slide level1"><h1>Intrinsically well-typed syntax</h1></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"><a id="2306" class="Keyword">module</a> <a id="V1"></a><a id="2313" href="slides2.html#2313" class="Module">V1</a> <a id="2316" class="Keyword">where</a>
</pre>-->
<pre class="Agda"> <a id="2337" class="Keyword">data</a> <a id="V1.Type"></a><a id="2342" href="slides2.html#2342" class="Datatype">Type</a> <a id="2347" class="Symbol">:</a> <a id="2349" class="PrimitiveType">Set</a> <a id="2353" class="Keyword">where</a>
<a id="V1.Type.int"></a><a id="2363" href="slides2.html#2363" class="InductiveConstructor">int</a> <a id="V1.Type.bool"></a><a id="2367" href="slides2.html#2367" class="InductiveConstructor">bool</a> <a id="2372" class="Symbol">:</a> <a id="2374" href="slides2.html#2342" class="Datatype">Type</a>
<a id="2382" class="Keyword">data</a> <a id="V1.Exp"></a><a id="2387" href="slides2.html#2387" class="Datatype">Exp</a> <a id="2391" class="Symbol">:</a> <a id="2393" href="slides2.html#2342" class="Datatype">Type</a> <a id="2398" class="Symbol">→</a> <a id="2400" class="PrimitiveType">Set</a>
<a id="2408" class="Comment">-- ...</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"> <a id="2525" class="Keyword">data</a> <a id="V1.Op"></a><a id="2530" href="slides2.html#2530" class="Datatype">Op</a> <a id="2533" class="Symbol">:</a> <a id="2535" class="Symbol">(</a><a id="2536" href="slides2.html#2536" class="Bound">dom</a> <a id="2540" href="slides2.html#2540" class="Bound">codom</a> <a id="2546" class="Symbol">:</a> <a id="2548" href="slides2.html#2342" class="Datatype">Type</a><a id="2552" class="Symbol">)</a> <a id="2554" class="Symbol">→</a> <a id="2556" class="PrimitiveType">Set</a> <a id="2560" class="Keyword">where</a>
<a id="V1.Op.plus"></a><a id="2570" href="slides2.html#2570" class="InductiveConstructor">plus</a> <a id="2576" class="Symbol">:</a> <a id="2578" href="slides2.html#2530" class="Datatype">Op</a> <a id="2581" href="slides2.html#2363" class="InductiveConstructor">int</a> <a id="2586" href="slides2.html#2363" class="InductiveConstructor">int</a>
<a id="V1.Op.gt"></a><a id="2594" href="slides2.html#2594" class="InductiveConstructor">gt</a> <a id="2600" class="Symbol">:</a> <a id="2602" href="slides2.html#2530" class="Datatype">Op</a> <a id="2605" href="slides2.html#2363" class="InductiveConstructor">int</a> <a id="2610" href="slides2.html#2367" class="InductiveConstructor">bool</a>
<a id="V1.Op.and"></a><a id="2619" href="slides2.html#2619" class="InductiveConstructor">and</a> <a id="2625" class="Symbol">:</a> <a id="2627" href="slides2.html#2530" class="Datatype">Op</a> <a id="2630" href="slides2.html#2367" class="InductiveConstructor">bool</a> <a id="2635" href="slides2.html#2367" class="InductiveConstructor">bool</a>
<a id="2643" class="Keyword">data</a> <a id="2648" href="slides2.html#2387" class="Datatype">Exp</a> <a id="2652" class="Keyword">where</a>
<a id="V1.Exp.eInt"></a><a id="2662" href="slides2.html#2662" class="InductiveConstructor">eInt</a> <a id="2668" class="Symbol">:</a> <a id="2670" class="Symbol">(</a><a id="2671" href="slides2.html#2671" class="Bound">i</a> <a id="2673" class="Symbol">:</a> <a id="2675" href="Agda.Builtin.Int.html#219" class="Datatype">ℤ</a><a id="2676" class="Symbol">)</a> <a id="2689" class="Symbol">→</a> <a id="2691" href="slides2.html#2387" class="Datatype">Exp</a> <a id="2695" href="slides2.html#2363" class="InductiveConstructor">int</a>
<a id="V1.Exp.eBool"></a><a id="2703" href="slides2.html#2703" class="InductiveConstructor">eBool</a> <a id="2709" class="Symbol">:</a> <a id="2711" class="Symbol">(</a><a id="2712" href="slides2.html#2712" class="Bound">b</a> <a id="2714" class="Symbol">:</a> <a id="2716" href="Agda.Builtin.Bool.html#135" class="Datatype">Bool</a><a id="2720" class="Symbol">)</a> <a id="2730" class="Symbol">→</a> <a id="2732" href="slides2.html#2387" class="Datatype">Exp</a> <a id="2736" href="slides2.html#2367" class="InductiveConstructor">bool</a>
<a id="V1.Exp.eOp"></a><a id="2745" href="slides2.html#2745" class="InductiveConstructor">eOp</a> <a id="2751" class="Symbol">:</a> <a id="2753" class="Symbol">∀{</a><a id="2755" href="slides2.html#2755" class="Bound">t</a> <a id="2757" href="slides2.html#2757" class="Bound">t'</a><a id="2759" class="Symbol">}</a> <a id="2761" class="Symbol">(</a><a id="2762" href="slides2.html#2762" class="Bound">op</a> <a id="2765" class="Symbol">:</a> <a id="2767" href="slides2.html#2530" class="Datatype">Op</a> <a id="2770" href="slides2.html#2755" class="Bound">t</a> <a id="2772" href="slides2.html#2757" class="Bound">t'</a><a id="2774" class="Symbol">)</a>
<a id="2786" class="Symbol">→</a> <a id="2788" class="Symbol">(</a><a id="2789" href="slides2.html#2789" class="Bound">e</a> <a id="2791" href="slides2.html#2791" class="Bound">e'</a> <a id="2794" class="Symbol">:</a> <a id="2796" href="slides2.html#2387" class="Datatype">Exp</a> <a id="2800" href="slides2.html#2755" class="Bound">t</a><a id="2801" class="Symbol">)</a> <a id="2807" class="Symbol">→</a> <a id="2809" href="slides2.html#2387" class="Datatype">Exp</a> <a id="2813" href="slides2.html#2757" class="Bound">t'</a>
</pre>
<p>See <a href="https://jespercockx.github.io/ohrid19-agda/src/V1/html/V1.WellTypedSyntax.html">WellTypedSyntax.agda</a>.</p>
</section><section id="evaluating-well-typed-syntax" class="slide level2">
<h2>Evaluating well-typed syntax</h2>
We can interpret <code>C</code> types as Agda types:
<pre class="Agda"> <a id="V1.Val"></a><a id="3011" href="slides2.html#3011" class="Function">Val</a> <a id="3015" class="Symbol">:</a> <a id="3017" href="slides2.html#2342" class="Datatype">Type</a> <a id="3022" class="Symbol">→</a> <a id="3024" class="PrimitiveType">Set</a>
<a id="3030" href="slides2.html#3011" class="Function">Val</a> <a id="3034" href="slides2.html#2363" class="InductiveConstructor">int</a> <a id="3039" class="Symbol">=</a> <a id="3041" href="Agda.Builtin.Int.html#219" class="Datatype">ℤ</a>
<a id="3045" href="slides2.html#3011" class="Function">Val</a> <a id="3049" href="slides2.html#2367" class="InductiveConstructor">bool</a> <a id="3054" class="Symbol">=</a> <a id="3056" href="Agda.Builtin.Bool.html#135" class="Datatype">Bool</a>
</pre>
<p>We can now define <code>eval</code> for well-typed expressions:</p>
<pre class="Agda">
<a id="V1.eval"></a><a id="3127" href="slides2.html#3127" class="Function">eval</a> <a id="3132" class="Symbol">:</a> <a id="3134" class="Symbol">∀</a> <a id="3136" class="Symbol">{</a><a id="3137" href="slides2.html#3137" class="Bound">t</a><a id="3138" class="Symbol">}</a> <a id="3140" class="Symbol">→</a> <a id="3142" href="slides2.html#2387" class="Datatype">Exp</a> <a id="3146" href="slides2.html#3137" class="Bound">t</a> <a id="3148" class="Symbol">→</a> <a id="3150" href="slides2.html#3011" class="Function">Val</a> <a id="3154" href="slides2.html#3137" class="Bound">t</a>
<a id="3158" href="slides2.html#3127" class="Function">eval</a> <a id="3163" class="Symbol">=</a> <a id="3165" href="slides2.html#1398" 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/ohrid19-agda/src/V1/html/V1.Interpreter.html">Interpreter.agda</a>.</p>
</section></section>
<section><section id="dealing-with-variables" class="title-slide slide level1"><h1>Dealing with variables</h1></section><section id="extending-well-typed-syntax-with-variables" class="slide level2">
<h2>Extending well-typed syntax with variables</h2>
A <strong>context</strong> is a list containing the types of variables in scope
<pre class="Agda"><a id="3492" class="Keyword">data</a> <a id="Type"></a><a id="3497" href="slides2.html#3497" class="Datatype">Type</a> <a id="3502" class="Symbol">:</a> <a id="3504" class="PrimitiveType">Set</a> <a id="3508" class="Keyword">where</a> <a id="Type.int"></a><a id="3514" href="slides2.html#3514" class="InductiveConstructor">int</a> <a id="Type.bool"></a><a id="3518" href="slides2.html#3518" class="InductiveConstructor">bool</a> <a id="3523" class="Symbol">:</a> <a id="3525" href="slides2.html#3497" class="Datatype">Type</a>
<a id="Cxt"></a><a id="3531" href="slides2.html#3531" class="Function">Cxt</a> <a id="3535" class="Symbol">=</a> <a id="3537" href="slides2.html#1634" class="Datatype">List</a> <a id="3542" href="slides2.html#3497" class="Datatype">Type</a>
</pre>
A <strong>variable</strong> is an index into the context
<pre class="Agda"><a id="3600" class="Keyword">data</a> <a id="Var"></a><a id="3605" href="slides2.html#3605" class="Datatype">Var</a> <a id="3609" class="Symbol">:</a> <a id="3611" class="Symbol">(</a><a id="3612" href="slides2.html#3612" class="Bound">Γ</a> <a id="3614" class="Symbol">:</a> <a id="3616" href="slides2.html#3531" class="Function">Cxt</a><a id="3619" class="Symbol">)</a> <a id="3621" class="Symbol">(</a><a id="3622" href="slides2.html#3622" class="Bound">t</a> <a id="3624" class="Symbol">:</a> <a id="3626" href="slides2.html#3497" class="Datatype">Type</a><a id="3630" class="Symbol">)</a> <a id="3632" class="Symbol">→</a> <a id="3634" class="PrimitiveType">Set</a> <a id="3638" class="Keyword">where</a>
<a id="Var.here"></a><a id="3646" href="slides2.html#3646" class="InductiveConstructor">here</a> <a id="3652" class="Symbol">:</a> <a id="3654" class="Symbol">∀</a> <a id="3656" class="Symbol">{</a><a id="3657" href="slides2.html#3657" class="Bound">Γ</a> <a id="3659" href="slides2.html#3659" class="Bound">t</a><a id="3660" class="Symbol">}</a> <a id="3665" class="Symbol">→</a> <a id="3667" href="slides2.html#3605" class="Datatype">Var</a> <a id="3671" class="Symbol">(</a><a id="3672" href="slides2.html#3659" class="Bound">t</a> <a id="3674" href="slides2.html#1678" class="InductiveConstructor Operator">∷</a> <a id="3676" href="slides2.html#3657" class="Bound">Γ</a><a id="3677" class="Symbol">)</a> <a id="3679" href="slides2.html#3659" class="Bound">t</a>
<a id="Var.there"></a><a id="3683" href="slides2.html#3683" class="InductiveConstructor">there</a> <a id="3689" class="Symbol">:</a> <a id="3691" class="Symbol">∀</a> <a id="3693" class="Symbol">{</a><a id="3694" href="slides2.html#3694" class="Bound">Γ</a> <a id="3696" href="slides2.html#3696" class="Bound">t</a> <a id="3698" href="slides2.html#3698" class="Bound">t'</a><a id="3700" class="Symbol">}</a> <a id="3702" class="Symbol">→</a> <a id="3704" href="slides2.html#3605" class="Datatype">Var</a> <a id="3708" href="slides2.html#3694" class="Bound">Γ</a> <a id="3710" href="slides2.html#3696" class="Bound">t</a> <a id="3712" class="Symbol">→</a> <a id="3714" href="slides2.html#3605" class="Datatype">Var</a> <a id="3718" class="Symbol">(</a><a id="3719" href="slides2.html#3698" class="Bound">t'</a> <a id="3722" href="slides2.html#1678" class="InductiveConstructor Operator">∷</a> <a id="3724" href="slides2.html#3694" class="Bound">Γ</a><a id="3725" class="Symbol">)</a> <a id="3727" href="slides2.html#3696" class="Bound">t</a>
</pre>
<p>(compare this to the definition of <code>Fin</code>)</p>
</section><section id="well-typed-syntax-with-variables" class="slide level2">
<h2>Well-typed syntax with variables</h2>
<p>The type <code>Exp</code> is now parametrized by a context <code>Γ</code>:</p>
<pre class="Agda"><a id="3875" class="Keyword">data</a> <a id="Exp"></a><a id="3880" href="slides2.html#3880" class="Datatype">Exp</a> <a id="3884" class="Symbol">(</a><a id="3885" href="slides2.html#3885" class="Bound">Γ</a> <a id="3887" class="Symbol">:</a> <a id="3889" href="slides2.html#3531" class="Function">Cxt</a><a id="3892" class="Symbol">)</a> <a id="3894" class="Symbol">:</a> <a id="3896" href="slides2.html#3497" class="Datatype">Type</a> <a id="3901" class="Symbol">→</a> <a id="3903" class="PrimitiveType">Set</a> <a id="3907" class="Keyword">where</a>
<a id="3915" class="Comment">-- ...</a>
<a id="Exp.eVar"></a><a id="3924" href="slides2.html#3924" class="InductiveConstructor">eVar</a> <a id="3930" class="Symbol">:</a> <a id="3932" class="Symbol">∀{</a><a id="3934" href="slides2.html#3934" class="Bound">t</a><a id="3935" class="Symbol">}</a> <a id="3937" class="Symbol">(</a><a id="3938" href="slides2.html#3938" class="Bound">x</a> <a id="3940" class="Symbol">:</a> <a id="3942" href="slides2.html#3605" class="Datatype">Var</a> <a id="3946" href="slides2.html#3885" class="Bound">Γ</a> <a id="3948" href="slides2.html#3934" class="Bound">t</a><a id="3949" class="Symbol">)</a> <a id="3951" class="Symbol">→</a> <a id="3953" href="slides2.html#3880" class="Datatype">Exp</a> <a id="3957" href="slides2.html#3885" class="Bound">Γ</a> <a id="3959" href="slides2.html#3934" class="Bound">t</a>
</pre>
<p>See <a href="https://jespercockx.github.io/ohrid19-agda/src/V2/html/V2.WellTypedSyntax.html">WellTypedSyntax.agda</a>.</p>
</section><section id="the-all-type" class="slide level2">
<h2>The <code>All</code> type</h2>
<p><code>All P xs</code> contains an element of <code>P x</code> for each <code>x</code> in the list <code>xs</code>:</p>
<pre class="Agda"><a id="4169" class="Keyword">data</a> <a id="All"></a><a id="4174" href="slides2.html#4174" class="Datatype">All</a> <a id="4178" class="Symbol">{</a><a id="4179" href="slides2.html#4179" class="Bound">A</a> <a id="4181" class="Symbol">:</a> <a id="4183" class="PrimitiveType">Set</a><a id="4186" class="Symbol">}</a> <a id="4188" class="Symbol">(</a><a id="4189" href="slides2.html#4189" class="Bound">P</a> <a id="4191" class="Symbol">:</a> <a id="4193" href="slides2.html#4179" class="Bound">A</a> <a id="4195" class="Symbol">→</a> <a id="4197" class="PrimitiveType">Set</a><a id="4200" class="Symbol">)</a> <a id="4202" class="Symbol">:</a> <a id="4204" href="slides2.html#1634" class="Datatype">List</a> <a id="4209" href="slides2.html#4179" class="Bound">A</a> <a id="4211" class="Symbol">→</a> <a id="4213" class="PrimitiveType">Set</a> <a id="4217" class="Keyword">where</a>
<a id="All.[]"></a><a id="4225" href="slides2.html#4225" class="InductiveConstructor">[]</a> <a id="4229" class="Symbol">:</a> <a id="4231" href="slides2.html#4174" class="Datatype">All</a> <a id="4235" href="slides2.html#4189" class="Bound">P</a> <a id="4237" href="slides2.html#1663" class="InductiveConstructor">[]</a>
<a id="All._∷_"></a><a id="4242" href="slides2.html#4242" class="InductiveConstructor Operator">_∷_</a> <a id="4246" class="Symbol">:</a> <a id="4248" class="Symbol">∀</a> <a id="4250" class="Symbol">{</a><a id="4251" href="slides2.html#4251" class="Bound">x</a> <a id="4253" href="slides2.html#4253" class="Bound">xs</a><a id="4255" class="Symbol">}</a> <a id="4257" class="Symbol">→</a> <a id="4259" href="slides2.html#4189" class="Bound">P</a> <a id="4261" href="slides2.html#4251" class="Bound">x</a> <a id="4263" class="Symbol">→</a> <a id="4265" href="slides2.html#4174" class="Datatype">All</a> <a id="4269" href="slides2.html#4189" class="Bound">P</a> <a id="4271" href="slides2.html#4253" class="Bound">xs</a> <a id="4274" class="Symbol">→</a> <a id="4276" href="slides2.html#4174" class="Datatype">All</a> <a id="4280" href="slides2.html#4189" class="Bound">P</a> <a id="4282" class="Symbol">(</a><a id="4283" href="slides2.html#4251" class="Bound">x</a> <a id="4285" href="slides2.html#1678" class="InductiveConstructor Operator">∷</a> <a id="4287" href="slides2.html#4253" class="Bound">xs</a><a id="4289" class="Symbol">)</a>
</pre>
</section><section id="evaluation-environments" class="slide level2">
<h2>Evaluation environments</h2>
During evaluation we need a value for <code>All</code> variables
<pre class="Agda"><a id="Val"></a><a id="4382" href="slides2.html#4382" class="Function">Val</a> <a id="4386" class="Symbol">:</a> <a id="4388" href="slides2.html#3497" class="Datatype">Type</a> <a id="4393" class="Symbol">→</a> <a id="4395" class="PrimitiveType">Set</a>
<a id="4399" href="slides2.html#4382" class="Function">Val</a> <a id="4403" href="slides2.html#3514" class="InductiveConstructor">int</a> <a id="4408" class="Symbol">=</a> <a id="4410" href="Agda.Builtin.Int.html#219" class="Datatype">ℤ</a>
<a id="4412" href="slides2.html#4382" class="Function">Val</a> <a id="4416" href="slides2.html#3518" class="InductiveConstructor">bool</a> <a id="4421" class="Symbol">=</a> <a id="4423" href="Agda.Builtin.Bool.html#135" class="Datatype">Bool</a>
<a id="Env"></a><a id="4429" href="slides2.html#4429" class="Function">Env</a> <a id="4433" class="Symbol">:</a> <a id="4435" href="slides2.html#3531" class="Function">Cxt</a> <a id="4439" class="Symbol">→</a> <a id="4441" class="PrimitiveType">Set</a>
<a id="4445" href="slides2.html#4429" class="Function">Env</a> <a id="4449" href="slides2.html#4449" class="Bound">Γ</a> <a id="4451" class="Symbol">=</a> <a id="4453" href="slides2.html#4174" class="Datatype">All</a> <a id="4457" href="slides2.html#4382" class="Function">Val</a> <a id="4461" href="slides2.html#4449" class="Bound">Γ</a>
</pre>
<p>We can now extend <code>eval</code> to expressions with variables:</p>
<pre class="Agda"><a id="eval"></a><a id="4529" href="slides2.html#4529" class="Function">eval</a> <a id="4534" class="Symbol">:</a> <a id="4536" class="Symbol">∀</a> <a id="4538" class="Symbol">{</a><a id="4539" href="slides2.html#4539" class="Bound">Γ</a><a id="4540" class="Symbol">}</a> <a id="4542" class="Symbol">{</a><a id="4543" href="slides2.html#4543" class="Bound">t</a><a id="4544" class="Symbol">}</a> <a id="4546" class="Symbol">→</a> <a id="4548" href="slides2.html#4429" class="Function">Env</a> <a id="4552" href="slides2.html#4539" class="Bound">Γ</a> <a id="4554" class="Symbol">→</a> <a id="4556" href="slides2.html#3880" class="Datatype">Exp</a> <a id="4560" href="slides2.html#4539" class="Bound">Γ</a> <a id="4562" href="slides2.html#4543" class="Bound">t</a> <a id="4564" class="Symbol">→</a> <a id="4566" href="slides2.html#4382" class="Function">Val</a> <a id="4570" href="slides2.html#4543" class="Bound">t</a>
<a id="4572" href="slides2.html#4529" class="Function">eval</a> <a id="4577" class="Symbol">=</a> <a id="4579" href="slides2.html#1398" class="Postulate">⋯</a>
</pre>
<p>See definition of <code>eval</code> in <a href="https://jespercockx.github.io/ohrid19-agda/src/V2/html/V2.Interpreter.html">Interpreter.agda</a>.</p>
</section><section id="exercises" class="slide level2">
<h2>Exercises</h2>
<p>Extend the well-typed syntax and interpreter with the syntactic constructions you added before.</p>
</section></section>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/reveal.js/3.7.0//lib/js/head.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/reveal.js/3.7.0//js/reveal.js"></script>
<script>
// Full list of configuration options available at:
// https://github.com/hakimel/reveal.js#configuration
Reveal.initialize({
// Push each slide change to the browser history
history: true,
// Vertical centering of slides
center: false,
// Transition style
transition: 'linear', // none/fade/slide/convex/concave/zoom
// The "normal" size of the presentation, aspect ratio will be preserved
// when the presentation is scaled to fit different resolutions. Can be
// specified using percentage units.
width: 1280,
height: 720,
// Factor of the display size that should remain empty around the content
margin: 0.2,
math: {
mathjax: 'https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js',
config: 'TeX-AMS_HTML-full',
tex2jax: {
inlineMath: [['\\(','\\)']],
displayMath: [['\\[','\\]']],
balanceBraces: true,
processEscapes: false,
processRefs: true,
processEnvironments: true,
preview: 'TeX',
skipTags: ['script','noscript','style','textarea','pre','code'],
ignoreClass: 'tex2jax_ignore',
processClass: 'tex2jax_process'
},
},
// Optional reveal.js plugins
dependencies: [
{ src: 'https://cdnjs.cloudflare.com/ajax/libs/reveal.js/3.7.0//lib/js/classList.js', condition: function() { return !document.body.classList; } },
{ src: 'https://cdnjs.cloudflare.com/ajax/libs/reveal.js/3.7.0//plugin/zoom-js/zoom.js', async: true },
{ src: 'https://cdnjs.cloudflare.com/ajax/libs/reveal.js/3.7.0//plugin/math/math.js', async: true },
{ src: 'https://cdnjs.cloudflare.com/ajax/libs/reveal.js/3.7.0//plugin/notes/notes.js', async: true }
]
});
</script>
</body>
</html>