-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlesson5.html
678 lines (591 loc) · 14.9 KB
/
lesson5.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
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<link rel="stylesheet" href="jscoq/node_modules/bootstrap/dist/css/bootstrap.min.css" />
<title>Machine-Checked Mathematics</title>
<link rel="stylesheet" href="local.css" />
<script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-MML-AM_CHTML'
async></script>
<script src="Blob.js" type="text/javascript"></script>
<script src="FileSaver.js" type="text/javascript"></script>
</head>
<body>
<div id="ide-wrapper" class="toggled">
<div id="code-wrapper">
<div id="document">
<p>
Use ALT-(up-arrow) and ALT-(down-arrow) to process this document inside your browser, line-by-line.
Use ALT-(right-arrow) to go to the cursor.
You can
<span class="save-button" onClick="save_coq_snippets()">save your edits</span>
inside your browser and
<span class="save-button" onClick="load_coq_snippets()">load them back</span>.
<!-- (edits are also saved when you close the window) -->
Finally, you can
<span class="save-button" onClick="download_coq_snippets()">download</span>
your working copy of the file, e.g., for sending it to teachers.
<hl />
</p>
<div><textarea id='coq-ta-1'>
From mathcomp Require Import mini_ssreflect.
Set Implicit Arguments.
Unset Strict Implicit.
Unset Printing Implicit Defensive.
</textarea></div>
<div><p>
<hr/>
<div class="slide">
<p>
<h2>
Reflection in the large
</h2>
<p>
<h3>
In this lecture
</h3>
<p>
<ul class="doclist">
<li> Play with the syntax of our goals
</li>
<li> Develop some automation thanks to computation
</li>
</ul>
<p>
<p>
</div>
<hr/>
<div class="slide vfill">
<p>
<h3>
Conversion rule
</h3>
<p>
The conversion rule (for the machine):
<p>
$$
\frac{\Gamma \vdash t : (\forall x:A, B) \qquad
\Gamma \vdash u : A' \qquad
\Gamma \vdash A \equiv A'}{\Gamma \vdash t~u : B[x \gets u]}
$$
<p>
The conversion rule (from a human):
<p>
<p style="text-align: center;">
types are <em>quotiented</em> by <em>computation</em>
</p>
<p>
<div>
</div>
<div><textarea id='coq-ta-2'>
Section ConversionRecap.
Variable win : Prop.
Variable lem : forall n m, n <= m = true -> win.
Arguments lem : clear implicits.
Check erefl true : true = true.
Check erefl true : 3 <= 7 = true.
Check lem 3 7 (erefl true).
End ConversionRecap.
</textarea></div>
<div><p>
</div>
<p>
Remark: <tt>erefl true</tt> works as a proof for <tt>e = true</tt>
no matter how many reduction steps it takes to normalize
<tt>e</tt> to <tt>true</tt>.
<p>
</div>
<hr/>
<div class="slide vfill">
<p>
<h2>
Recap: computation and variables
</h2>
<p>
<ul class="doclist">
<li> Why is the first goal trivial by computation?
</li>
<li> Why the second is not?
</li>
</ul>
<div>
</div>
<div><textarea id='coq-ta-3'>
Lemma ex1 (x : nat) : 0 * x = 0.
Proof. by []. Qed.
Lemma ex2 (x : nat) : x * 0 = 0.
Proof.
Fail by [].
Admitted.
</textarea></div>
<div><p>
</div>
<p>
We used the lemma <tt>muln0</tt> before. Can't we explain Coq
to use it for us?
<p>
Yes!
<p>
</div>
<hr/>
<div class="slide">
<p>
<h3>
Idea
</h3>
<p>
<ul class="doclist">
<li> we could write a program that simplifies expressions
</li>
<li> we could prove it correct
</li>
<li> we could have Coq run it for us
</li>
</ul>
<p>
<div>
</div>
<div><textarea id='coq-ta-4'>
Module A. (* A module is a box for Coq code, ignore this *)
Inductive expr :=
| Zero
| Mult (x : expr) (y : expr)
| Extra (stuff : nat).
(* Syntax of (3 * 0) * 4 *)
Definition T : expr := Mult (Mult (Extra 3) Zero) (Extra 4).
</textarea></div>
<div><p>
</div>
<p>
The <tt>expr</tt> data type is the <em>syntax</em> of expressions.
It is a data type like <tt>nat</tt> or <tt>bool</tt> are we know how to
write programs on this data.
<p>
Let's write a program that deals with <tt>Zero</tt>, i.e. that implements
the simplication rule $$n * 0 = 0$$ and $$0 * n = 0$$.
<p>
<div>
</div>
<div><textarea id='coq-ta-5'>
Fixpoint simplify (e : expr) : expr :=
match e with
| Mult x y =>
match simplify x, simplify y with
| Zero, _ => Zero
| _, Zero => Zero
| a, b => Mult a b
end
| x => x
end.
Eval lazy in simplify T. (* = Zero *)
</textarea></div>
<div><p>
</div>
<p>
We have to link these expressions and our goals.
Each expression in <tt>expr</tt> represents an expression in our goal.
Let's make this map explicit.
<p>
<div>
</div>
<div><textarea id='coq-ta-6'>
Fixpoint interp (e : expr) : nat :=
match e with
| Zero => 0
| Mult x y => (interp x) * (interp y)
| Extra x => x
end.
Print T.
Eval lazy delta [T interp] iota beta in interp T.
</textarea></div>
<div><p>
</div>
<p>
What would it mean for <tt>simplify</tt> to be correct?
<p>
<div>
</div>
<div><textarea id='coq-ta-7'>
Lemma simplify_correct (e : expr) : interp e = interp (simplify e).
Proof.
elim: e => //= x Hx y Hy.
case: (simplify x) Hx => [|x1 x2|n] -> //; case: (simplify y) Hy => [|y1 y2|m] -> //.
1,2: by rewrite muln0. (* This means: on goal number 1 and 2, do .... *)
Qed.
</textarea></div>
<div><p>
</div>
<p>
Now let's take advantage of our program
<p>
<div>
</div>
<div><textarea id='coq-ta-8'>
Lemma ex3 (x : nat) : x * 0 = 0.
Proof.
pose AST : expr := Mult (Extra x) Zero.
rewrite -[LHS]/(interp AST). (* replace the LHS with (interp AST) *)
rewrite simplify_correct.
simpl.
by [].
Qed.
Lemma ex4 (x : nat) : (x * 0) * x = 0.
Proof.
pose AST : expr := Mult (Mult (Extra x) Zero) (Extra x).
rewrite -[LHS]/(interp AST). (* replace the LHS with (interp AST) *)
by rewrite simplify_correct.
Qed.
End A.
</textarea></div>
<div><p>
</div>
<p>
</div>
<hr/>
<div class="slide">
<p>
<h3>
Let's add more simplification rules!
</h3>
<p>
The rule we want is $$n - n = 0$$ when n is a number.
<p>
<div>
</div>
<div><textarea id='coq-ta-9'>
Module B.
Inductive expr :=
| Zero
| Minus (x : expr) (y : expr)
| Extra (stuff : nat).
Fixpoint simplify (e : expr) : expr :=
match e with
| Minus x y =>
match simplify x, simplify y with
| Extra n, Extra m =>
match n == m with
| true => Zero
| false => Minus (Extra n) (Extra m)
end
| a, b => Minus a b
end
| x => x
end.
(* Syntax of (3 - 3 *)
Definition T : expr := Minus (Extra 3) (Extra 3).
Eval lazy in simplify T. (* = Zero *)
</textarea></div>
<div><p>
</div>
<p>
We have to link these expressions and out goals.
Each expression in <tt>expr</tt> represents an expression in out goal.
Let's make this map explicit.
<p>
<div>
</div>
<div><textarea id='coq-ta-10'>
Fixpoint interp (e : expr) : nat :=
match e with
| Zero => 0
| Minus x y => (interp x) - (interp y)
| Extra x => x
end.
Eval lazy delta [T interp] iota beta in interp T.
</textarea></div>
<div><p>
</div>
<p>
What would it mean for <tt>simplify</tt> to be correct?
<p>
<div>
</div>
<div><textarea id='coq-ta-11'>
Lemma simplify_correct (e : expr) : interp e = interp (simplify e).
Proof.
elim: e => //= x Hx y Hy.
case: (simplify x) Hx => [|x1 x2|n] -> //; case: (simplify y) Hy => [|y1 y2|m] -> //.
by case: eqP => [->|//]; rewrite subnn.
Qed.
</textarea></div>
<div><p>
</div>
<p>
Now let's try to take advantage of it
<p>
<div>
</div>
<div><textarea id='coq-ta-12'>
Lemma ex3 (x : nat) : x - x = 0.
Proof.
pose AST : expr := Minus (Extra x) (Extra x).
rewrite -[LHS]/(interp AST).
rewrite simplify_correct.
simpl.
Abort.
End B.
</textarea></div>
<div><p>
</div>
<p>
What went wrong is that we did not completely move variables
away from the syntax we manipulate. But it is easy to fix.
<p>
</div>
<hr/>
<div class="slide">
<p>
<h3>
Let's give a syntax to variables.
</h3>
<p>
<div>
</div>
<div><textarea id='coq-ta-13'>
Module C.
Inductive expr :=
| Zero
| Minus (x : expr) (y : expr)
| Var (n : nat). (* Extra stuff is not here, this is just an index *)
Fixpoint simplify (e : expr) : expr :=
match e with
| Minus x y =>
match simplify x, simplify y with
| Var n, Var m =>
match n == m with
| true => Zero
| false => Minus (Var n) (Var m)
end
| a, b => Minus a b
end
| y => y
end.
</textarea></div>
<div><p>
</div>
<p>
Interpretation now takes a map <tt>c</tt> from the ids of variables
to arbitrary terms in out target type.
<p>
<div>
</div>
<div><textarea id='coq-ta-14'>
Fixpoint interp (e : expr) (c : list nat) : nat :=
match e with
| Zero => 0
| Minus x y => (interp x c) - (interp y c)
| Var x => nth 0 c x
end.
(* Syntax of (3 - 3) in a map C(0) -> 3 *)
Definition T : expr := Minus (Var 0) (Var 0).
Definition C : list nat := [:: 3].
Eval lazy delta [T C interp nth] iota beta in interp T C.
Lemma simplify_correct (e : expr) (c : list nat) : interp e c = interp (simplify e) c.
Proof.
elim: e => //= x Hx y Hy.
case: (simplify x) Hx => [|x1 x2|n] -> //; case: (simplify y) Hy => [|y1 y2|m] -> //.
by case: eqP => [->|//]; rewrite subnn.
Qed.
Lemma ex3 (x : nat) : x - x = 0.
Proof.
pose AST : expr := Minus (Var 0) (Var 0).
pose CTX : list nat := [:: x].
rewrite -[LHS]/(interp AST CTX).
rewrite simplify_correct.
simpl.
by [].
Qed.
End C.
</textarea></div>
<div><p>
</div>
<p>
</div>
<hr/>
<div class="slide vfill">
<p>
<h2>
To sum up
</h2>
<p>
<ul class="doclist">
<li> computation is well defined on any term, including terms with variables
</li>
<li> computation is <em>complete</em> on closed terms (you reach a normal form
that is made of constructors)
</li>
<li> computation happens <em>inside</em> the logic (terms are quotiented wrt computation)
</li>
<li> computation can be very fast (decades of research in CS)
</li>
<li> applications of this technique
+ simplification in a ring
+ 4 color theorem
+ Pocklington primality test
</li>
</ul>
</div>
<hr/>
</div>
<div><textarea id='coq-ta-15'>
</textarea></div>
<script type="text/javascript">
var coqdoc_ids = ['coq-ta-1', 'coq-ta-2', 'coq-ta-3', 'coq-ta-4',
'coq-ta-5', 'coq-ta-6', 'coq-ta-7', 'coq-ta-8',
'coq-ta-9', 'coq-ta-10', 'coq-ta-11', 'coq-ta-12',
'coq-ta-13', 'coq-ta-14', 'coq-ta-15'];
</script>
<hr />
<script type="text/javascript">
function load_coq_snippets() {
for (i = 0; i < coqdoc_ids.length; ++i) {
document.getElementById(coqdoc_ids[i]).nextSibling.CodeMirror.setValue(
localStorage.getItem('coq-snippet-' + coqdoc_ids[i]));
}
}
function save_coq_snippets() {
for (i = 0; i < coqdoc_ids.length; ++i) {
localStorage.setItem('coq-snippet-' + coqdoc_ids[i], document.getElementById(coqdoc_ids[i]).nextSibling.CodeMirror.getValue());
}
alert("Coq snippets saved.");
}
function download_coq_snippets() {
var chunks = []
for (i = 0; i < coqdoc_ids.length; ++i) {
chunks.push(document.getElementById(coqdoc_ids[i]).nextSibling.CodeMirror.getValue())
}
var data = new Blob(chunks, { type: "text/plain;charset=utf-8" });
saveAs(data, 'source.v');
}
alignWithTop = true;
current = 0;
slides = [];
function select_current() {
for (var i = 0; i < slides.length; i++) {
var s = document.getElementById('slideno' + i);
if (i == current) {
s.setAttribute('class', 'slideno selected');
} else {
s.setAttribute('class', 'slideno');
}
}
}
function mk_tooltip(label, text) {
var t = document.createElement("div");
t.setAttribute('class', 'slide-tooltip');
t.innerHTML = label;
var s = document.createElement("span");
s.setAttribute('class', 'slide-tooltiptext slide-tooltip-left');
s.innerHTML = text;
t.appendChild(s);
return t;
}
function find_hx(e) {
for (var i = 0; i < e.children.length; i++) {
var x = e.children[i];
if (x.tagName == "H1" ||
x.tagName == "H2" ||
x.tagName == "H3" ||
x.tagName == "H4") return x.textContent;
}
return null;
}
function init_slides() {
var toolbar = document.getElementById('document');
if (toolbar) {
var tools = document.createElement("div");
var tprev = document.createElement("div");
var tnext = document.createElement("div");
tools.setAttribute('id', 'tools');
tprev.setAttribute('id', 'prev');
tprev.setAttribute('onclick', 'prev_slide();');
tnext.setAttribute('id', 'next');
tnext.setAttribute('onclick', 'next_slide();');
toolbar.prepend(tools);
tools.appendChild(tprev);
tools.appendChild(tnext);
slides = document.getElementsByClassName('slide');
for (var i = 0; i < slides.length; i++) {
var s = document.createElement("div");
s.setAttribute('id', 'slideno' + i);
s.setAttribute('class', 'slideno');
s.setAttribute('onclick', 'goto_slide(' + i + ');');
var title = find_hx(slides[i]);
if (title == null) {
title = "goto slide " + i;
}
var t = mk_tooltip(i, title);
s.appendChild(t)
tools.appendChild(s);
}
select_current();
} else {
//retry later
setTimeout(init_slides, 100);
}
}
function on_screen(rect) {
return (
rect.top >= 0 &&
rect.top <= (window.innerHeight || document.documentElement.clientHeight)
);
}
function update_scrolled() {
for (var i = slides.length - 1; i >= 0; i--) {
var rect = slides[i].getBoundingClientRect();
if (on_screen(rect)) {
current = i;
select_current();
}
}
}
function goto_slide(n) {
current = n;
var element = slides[current];
console.log(element);
element.scrollIntoView(alignWithTop);
select_current();
}
function next_slide() {
current++;
if (current >= slides.length) { current = slides.length - 1; }
var element = slides[current];
console.log(element);
element.scrollIntoView(alignWithTop);
select_current();
}
function prev_slide() {
current--;
if (current < 0) { current = 0; }
var element = slides[current];
element.scrollIntoView(alignWithTop);
select_current();
}
window.onload = init_slides;
window.onbeforeunload = save_coq_snippets;
window.onscroll = update_scrolled;
</script>
</div> <!-- /#document -->
</div> <!-- /#code-wrapper -->
</div> <!-- /#ide-wrapper -->
<script src="./jscoq/ui-js/jscoq-loader.js" type="text/javascript"></script>
<script type="text/javascript">
var coq;
loadJsCoq('./jscoq/')
.then(loadJs("./jscoq/node_modules/codemirror/addon/runmode/runmode"))
.then(loadJs("./jscoq/node_modules/codemirror/addon/runmode/colorize"))
.then(function () {
var coqInline = document.getElementsByClassName("inline-coq");
CodeMirror.colorize(coqInline);
})
.then(function () {
coq = new CoqManager(coqdoc_ids,
{ base_path: './jscoq/',
init_pkgs: ['init'],
all_pkgs: ['init','math-comp']
}
);
});
</script>
</body>
</html>