-
Notifications
You must be signed in to change notification settings - Fork 1
/
lesson2.html
1227 lines (1115 loc) · 26.4 KB
/
lesson2.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
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!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.
(* ignore these directives *)
Set Implicit Arguments.
Unset Strict Implicit.
Unset Printing Implicit Defensive.
Add Printing Coercion is_true.
Notation "x '= true'" := (is_true x) (x at level 100, at level 0, only printing).
Remove Printing If bool.
</textarea></div>
<div><p>
<hr/>
<div class="slide vfill">
<p>
<h2>
Programs, Specifications and Proofs
</h2>
<p>
<h3>
In this lecture, first steps on:
</h3>
<p>
<ul class="doclist">
<li> writing programs & specifications
<ul class="doclist">
<li> functions
</li>
<li> simple data
</li>
<li> containers
</li>
<li> symbolic computations
</li>
<li> higher order functions and mathematical notations
</li>
</ul>
</li>
<li> writing proofs
<ul class="doclist">
<li> proofs by computation
</li>
<li> proofs by case split
</li>
<li> proofs by rewriting
</li>
</ul>
</li>
</ul>
<h3>
Extra material:
</h3>
<p>
<ul class="doclist">
<li> <a href="cheat_sheet.html">Coq cheat sheet</a> (during ex session)
</li>
<li> <a href="https://math-comp.github.io/mcb/">Mathematical Components</a>
chapters one and two (at home)
</li>
</ul>
<h3>
Disclaimer:
</h3>
<p>
<ul class="doclist">
<li> I'm a computer scientist, I speak weird ;-)
</li>
<li> don't be afraid, raise your hand and I'll do my best to explain my lingo.
</li>
</ul>
</div>
<hr/>
<div class="slide">
<h2>
Functions
</h2>
<p>
Functions are written using the <tt>fun .. => ..</tt> syntax. Down below we write
the function:
<p>
$$ n \mapsto 1 + n $$
<p>
The command <tt>Check</tt> verifies that a term is well typed.
The precise meaning will be given tomorrow morning, for now think about
it as a well formed element of some set.
<p>
<div>
</div>
<div><textarea id='coq-ta-2'>
Check (fun n : nat => 1 + n).
</textarea></div>
<div><p>
</div>
<p>
Coq answers by annotating the term with its type (the <tt>-></tt> denotes the function
space):
<p>
$$ \mathbb{N} \to \mathbb{N} $$
<p>
Function application is written by writing the function
on the left of the argument (eg, not as in the mathematical
practice).
<p>
<div>
</div>
<div><textarea id='coq-ta-3'>
Check 2.
Check (fun n : nat => 1 + n) 2.
</textarea></div>
<div><p>
</div>
<p>
Terms (hence functions) can be given a name using
the <tt>Definition</tt> command. The command offers some
syntactic sugar for binding the function arguments.
<p>
<div>
</div>
<div><textarea id='coq-ta-4'>
Definition f : nat -> nat := (fun n : nat => 1 + n).
(* Definition f (n : nat) : nat := 1 + n. *)
</textarea></div>
<div><p>
</div>
<p>
Named terms can be printed.
<p>
<div>
</div>
<div><textarea id='coq-ta-5'>
Print f.
</textarea></div>
<div><p>
</div>
<p>
Coq is able to compute with terms, in particular
one can obtain the normal form via the <tt>Eval lazy in</tt>
command.
<p>
<div>
</div>
<div><textarea id='coq-ta-6'>
Eval lazy in f 2.
</textarea></div>
<div><p>
</div>
<p>
Notice that <quote>computation</quote> is made of many steps.
In particular <tt>f</tt> has to be unfolded (delta step)
and then the variable substituted for the argument
(beta).
<p>
<div>
</div>
<div><textarea id='coq-ta-7'>
Eval lazy delta[f] in f 2.
Eval lazy delta[f] beta in f 2.
</textarea></div>
<div><p>
</div>
<p>
Nothing but functions (and their types) are built-in in Coq.
All the rest is defined, even <tt>1</tt>, <tt>2</tt> and <tt>+</tt> are not primitive.
<p>
<p><br/><p>
<p>
<div class="note">(notes)<div class="note-text">
<p>
This slide corresponds to
section 1.1 of
<a href="https://math-comp.github.io/mcb/">the Mathematical Components book</a>
</div></div>
<p>
</div>
<hr/>
<div class="slide">
<h2>
Data types
</h2>
<p>
Data types can be declared using the <tt>Inductive</tt> command.
<p>
Many of them are already available in the Coq library called
<tt>Prelude</tt> that is automatically loaded. We hence just print
them.
<p>
<tt>Inductive bool := true | false.</tt>
<p>
<div>
</div>
<div><textarea id='coq-ta-8'>
Print bool.
</textarea></div>
<div><p>
</div>
<p>
This command declares a new type <tt>bool</tt> and declares
how the terms of this type are built.
Only <tt>true</tt> and <tt>false</tt> are *canonical* inhabitants of
<tt>bool</tt> and they are called *constructors*.
<p>
Remark: a data type declaration prescribes the shape of values, not
which operations are available nor their properties.
We are going to use programs (functions) to describe the operations
on a data type.
<p>
Coq provides one very primitive operation that works on data types.
This operation lets you take a decision based on the canonical shape
of the data. It is written <tt>match x with true => ... | false => .. end</tt>.
<p>
<div>
</div>
<div><textarea id='coq-ta-9'>
Definition twoVtree (b : bool) : nat :=
match b with
| true => 2
| false => 3
end.
Eval lazy in twoVtree true.
Eval lazy delta in twoVtree true.
Eval lazy delta beta in twoVtree true.
Eval lazy delta beta iota in twoVtree true.
</textarea></div>
<div><p>
</div>
<p>
We define a few boolean operators that will come in handy
later on.
<p>
<div>
</div>
<div><textarea id='coq-ta-10'>
Definition andb (b1 : bool) (b2 : bool) : bool :=
match b1 with true => b2 | false => false end.
Definition orb (b1 :bool) (b2 : bool) : bool :=
match b1 with true => true | false => b2 end.
Infix "&&" := andb.
Infix "||" := orb.
Check true && false || false.
</textarea></div>
<div><p>
</div>
<p>
The <tt>Infix</tt> command lets one declare infix notations.
Precendence and associativity is already declared in the
prelude of Coq, here we just associate the constants
<tt>andb</tt> and <tt>orb</tt> to these notataions.
<p>
Natural numbers are defined similarly to booleans:
<p>
<tt>Inductive nat := O | S (n : nat).</tt>
<p>
Remark that the declaration is recursive.
<p>
<div>
</div>
<div><textarea id='coq-ta-11'>
Print nat.
</textarea></div>
<div><p>
</div>
<p>
Remark:
<p>
Coq provides a special notation for literals, eg <tt>3</tt>,
that is just sugar for <tt>S (S (S O))</tt>.
<p>
<div>
</div>
<div><textarea id='coq-ta-12'>
Check 3.
</textarea></div>
<div><p>
</div>
<p>
Now let's define a simple operation on natural numbers.
<p>
As for booleans, Coq provides a primitie operation to
make decisions based on the canonical values.
<p>
<div>
</div>
<div><textarea id='coq-ta-13'>
Definition pred (n : nat) : nat :=
match n with 0 => 0 | S p => p end.
Eval lazy in pred 7.
</textarea></div>
<div><p>
</div>
<p>
Remark that <tt>p</tt> is a binder. When the <tt>match..with..end</tt>
is evaluated, and <tt>n</tt> put in normal form, then if it
is <tt>S t</tt> the variable <tt>p</tt> takes <tt>t</tt> and the second
is taken.
<p>
Since the data type of natural number is recursive Coq provides
another primitive operation called <tt>Fixpoint</tt>.
<p>
<div>
</div>
<div><textarea id='coq-ta-14'>
Fixpoint addn (n : nat) (m : nat) : nat :=
match n with
| 0 => m
| S p => S (addn p m)
end.
Infix "+" := addn.
Eval lazy in 3 + 2.
</textarea></div>
<div><p>
</div>
<p>
Let's now write the equality test for natural numbers
<p>
<div>
</div>
<div><textarea id='coq-ta-15'>
Fixpoint eqn (n : nat) (m : nat) : bool :=
match n, m with
| 0, 0 => true
| S p, S q => eqn p q
| _, _ => false
end.
Infix "==" := eqn : bool_scope.
Eval lazy in 3 == 4.
</textarea></div>
<div><p>
</div>
<p>
Other examples are subtraction and order
<p>
<div>
</div>
<div><textarea id='coq-ta-16'>
Fixpoint subn (m : nat) (n : nat) : nat :=
match m, n with
| S p, S q => subn p q
| _ , _ => m
end.
Infix "-" := subn.
Eval lazy in 3 - 2.
Eval lazy in 2 - 3. (* truncated *)
Definition leq m n := m - n == 0.
Infix "<=" := leq.
Eval lazy in 4 <= 5.
</textarea></div>
<div><p>
</div>
<p>
<p><br/><p>
<p>
<div class="note">(notes)<div class="note-text">
<p>
This slide corresponds to
section 1.2 of
<a href="https://math-comp.github.io/mcb/">the Mathematical Components book</a>
</div></div>
<p>
</div>
<p>
</div>
<hr/>
<div class="slide">
<h2>
Containers
</h2>
<p>
Containers let one aggregate data, for example to form a
pair or a list. The interesting characteristic of containers
is that they are polymorphic: the same container can be used
to hold terms of many types.
<p>
<pre class="inline-coq" data-lang="coq">
Inductive list (A : Type) := nil | cons (hd : A) (tl : list A).<p>
</pre>
<div>
</div>
<div><textarea id='coq-ta-17'>
Check cons 1 nil.
Check [:: 3; 4; 5 ].
</textarea></div>
<div><p>
</div>
<p>
The notation <tt>[:: .. ; .. ]</tt> can be used to form list
by separating the elements with <tt>;</tt>. When there are no elements
what is left is <tt>[::]</tt> that is the empty list.
<p>
And of course we can use list with other data types
<p>
<div>
</div>
<div><textarea id='coq-ta-18'>
Check [:: 3; 4; 5 ].
Check [:: true; false; true ].
</textarea></div>
<div><p>
</div>
<p>
Let's now define the <tt>size</tt> function.
<p>
<div>
</div>
<div><textarea id='coq-ta-19'>
Fixpoint size A (s : list A) : nat :=
match s with
| cons _ tl => S (size tl)
| nil => 0
end.
Eval lazy in size [:: 1; 8; 34].
Eval lazy in size [:: true; false; false].
</textarea></div>
<div><p>
</div>
<p>
Given that the contents of containers are of an
arbitrary type many common operations are parametrized
by functions that are specific to the type of the
contents.
<p>
<pre class="inline-coq" data-lang="coq">
Fixpoint map A B (f : A -> B) (s : list A) : list B :=
match s with cons e tl => cons (f e) map f tl | nil => nil end.<p>
</pre>
<div>
</div>
<div><textarea id='coq-ta-20'>
Definition l := [:: 1; 2; 3].
Check map (fun x => S x) l.
Eval lazy in map (fun x => S x) l.
</textarea></div>
<div><p>
</div>
<p>
The <a href="http://math-comp.github.io/math-comp/htmldoc/mathcomp.ssreflect.seq.html">seq</a>
library of Mathematical Components contains many combinators. Their syntax
is documented in the header of the file.
<p>
<p><br/><p>
<p>
<div class="note">(notes)<div class="note-text">
<p>
This slide corresponds to
section 1.3 of
<a href="https://math-comp.github.io/mcb/">the Mathematical Components book</a>
</div></div>
<p>
</div>
<hr/>
<div class="slide">
<h2>
Symbols
</h2>
<p>
The section mecanism is used to describe a context under
which definitions are made. Coq lets us not only define
terms, but also compute with them in that context.
<p>
We use this mecanism to talk about symbolic computation.
<p>
<div>
</div>
<div><textarea id='coq-ta-21'>
Section symbols.
Variables x : nat.
Eval lazy in pred (S x).
Eval lazy in pred x .
</textarea></div>
<div><p>
</div>
<p>
Computation can take place in presence of variables
as long as constructors can be consumed. When no
more constructors are available computation is
stuck.
<p>
Let's not look at a very common higher order
function.
<p>
<div>
</div>
<div><textarea id='coq-ta-22'>
Fixpoint foldr A T (f : T -> A -> A) (a : A) (s : list T) :=
match s with
| cons x xs => f x (foldr f a xs)
| nil => a
end.
</textarea></div>
<div><p>
</div>
<p>
The best way to understand what <tt>foldr</tt> does
is to postulate a variable <tt>f</tt> and compute.
<p>
<div>
</div>
<div><textarea id='coq-ta-23'>
Variable f : nat -> nat -> nat.
Eval lazy in foldr f 3 [:: 1; 2 ].
</textarea></div>
<div><p>
</div>
<p>
If we plug <tt>addn</tt> in place of <tt>f</tt> we
obtain a term that evaluates to a number.
<p>
<div>
</div>
<div><textarea id='coq-ta-24'>
Eval lazy in foldr addn 3 [:: 1; 2 ].
End symbols.
</textarea></div>
<div><p>
</div>
<p>
<div class="note">(notes)<div class="note-text">
<p>
This slide corresponds to
sections 1.4 and 1.5 of
<a href="https://math-comp.github.io/mcb/">the Mathematical Components book</a>
</div></div>
<p>
</div>
<hr/>
<div class="slide">
<h2>
Higher order functions and mathematical notations
</h2>
<p>
Let's try to write this formula in Coq
<p>
$$ \sum_{i=1}^n (i * 2 - 1) = n ^ 2 $$
<p>
We need a bit of infrastructure
<p>
<div>
</div>
<div><textarea id='coq-ta-25'>
Fixpoint iota (m : nat) (n : nat) : list nat :=
match n with
| 0 => nil
| S u => cons m (iota (S m) u)
end.
Eval lazy in iota 0 5.
</textarea></div>
<div><p>
</div>
<p>
Combining <tt>iota</tt> and <tt>foldr</tt> we can get pretty
close to the LaTeX source for the formula above.
<p>
<div>
</div>
<div><textarea id='coq-ta-26'>
Notation "\sum_ ( m <= i < n ) F" :=
(foldr (fun i a => F + a) 0 (iota m (n-m)))
(at level 41, m, n, i at level 50, F at level 41).
Check \sum_(1 <= x < 5) (x * 2 - 1).
Eval lazy in \sum_(1 <= x < 5) (x * 2 - 1).
</textarea></div>
<div><p>
</div>
<p>
<p><br/><p>
<p>
<div class="note">(notes)<div class="note-text">
<p>
This slide corresponds to
section 1.6 of
<a href="https://math-comp.github.io/mcb/">the Mathematical Components book</a>
</div></div>
<p>
</div>
<hr/>
<div class="slide">
<h2>
Formal statements
</h2>
<p>
Most of the statements that we consider in Mathematical
Components are equalities.
<p>
It is not surprising one can equate two numbers.
<p>
<div>
</div>
<div><textarea id='coq-ta-27'>
Lemma addnA (m n k : nat) : m + (n + k) = m + n + k.
Abort.
</textarea></div>
<div><p>
</div>
<p>
Remark: to save space, variables of the same type can be
writte <tt>(a b c : type)</tt> instead of <tt>(a : type) (b : type) ...</tt>.
<p>
We have defined many boolean tests that can
play the role of predicates.
<p>
<div>
</div>
<div><textarea id='coq-ta-28'>
Check 0 <= 4. (* not a statement *)
Check (0 <= 4) = true. (* a statement we can prove *)
</textarea></div>
<div><p>
</div>
<p>
More statements using equality and predicates in bool
<p>
<div>
</div>
<div><textarea id='coq-ta-29'>
Lemma eqn_leq (m n : nat) : (m == n) = (m <= n) && (n <= m).
Abort.
Lemma leq0n (n : nat) : 0 <= n.
Abort.
</textarea></div>
<div><p>
</div>
<p>
Remark: in the first statement <tt>=</tt> really means
<quote>if and only if</quote>.
<p>
Remark: Coq adds <tt>= true</tt> automatically when we state a lemma.
<p>
<div>
</div>
<div><textarea id='coq-ta-30'>
Check is_true.
</textarea></div>
<div><p>
</div>
<p>
<p><br/><p>
<p>
<div class="note">(notes)<div class="note-text">
This slide corresponds to
section 2.1 of
<a href="https://math-comp.github.io/mcb/">the Mathematical Components book</a>
</div></div>
<p>
<p><br/><p>
</div>
<hr/>
<div class="slide">
<h2>
Proofs by computation
</h2>
<p>
Our statements are made of programs. Hence they compute!
<p>
The <tt>by[]</tt> proof command solves trivial goals (mostly) by
computation.
<p>
<pre class="inline-coq" data-lang="coq">
Fixpoint addn n m :=
match n with 0 => m | S p => S (addn p m) end.<p>
</pre><pre class="inline-coq" data-lang="coq">
Fixpoint subn m n : nat :=
match m, n with
| S p, S q => subn p q
| _ , _ => m
end.<p>
</pre><pre class="inline-coq" data-lang="coq">
Fixpoint eqn (n : nat) (m : nat) : bool :=
match n, m with
| 0, 0 => true
| S p, S q => eqn p q
| _, _ => false
end.<p>
</pre><pre class="inline-coq" data-lang="coq">
Definition leq m n := m - n == 0.<p>
</pre>
<div>
</div>
<div><textarea id='coq-ta-31'>
Lemma addSn m n : S m + n = S (m + n).
Proof. by []. Qed.
Lemma leq0n n : 0 <= n.
Proof. by []. Qed.
Lemma ltn0 n : S n <= 0 = false.
Proof. by []. Qed.
Lemma ltnS m n : (S m <= S n) = (m <= n).
Proof. by []. Qed.
</textarea></div>
<div><p>
</div>
<p>
Let's look around.
<p>
<tt>Locate</tt> looks for a symbol to find the name behing id.
<p>
<div>
</div>
<div><textarea id='coq-ta-32'>
Locate "~~".
Print negb.
Lemma negbK (b : bool) : ~~ (~~ b) = b.
Proof. Fail by []. Abort.
</textarea></div>
<div><p>
</div>
<p>
It is not always the case the computation solves all our
problems. In particular here there are no constructors to
consume, hence computation is stuck.
<p>
To prove <tt>negbK</tt> we need a case split.
<p>
<p><br/><p>
<p>
<div class="note">(notes)<div class="note-text">
This slide corresponds to
section 2.2.1 of
<a href="https://math-comp.github.io/mcb/">the Mathematical Components book</a>
</div></div>
<p>
<p><br/><p>
</div>
<hr/>
<div class="slide">
<h2>
Proofs by case analysis
</h2>
<p>
The proof of <tt>negbK</tt> requires a case analysis: given that
<tt>b</tt> is of type bool, it can only be <tt>true</tt> or <tt>false</tt>.
<p>
The <tt>case: term</tt> command performs this proof step.
<p>
<div>
</div>
<div><textarea id='coq-ta-33'>
Lemma negbK (b : bool) : ~~ (~~ b) = b.
Proof.
case: b.
by [].
by [].
Qed.
Lemma andbC (b1 b2 : bool) : b1 && b2 = b2 && b1.
Proof.
by case: b1; case: b2.
Qed.
Lemma orbN b : b || ~~ b.
Proof.
by case: b.
Qed.
</textarea></div>
<div><p>
</div>
<p>
The constructors of <tt>bool</tt> have no arguments, but for
example the second constructor of <tt>nat</tt> has one.
<p>
In this case one has to complement the command by supplying
names for these arguments.
<p>
<div>
</div>
<div><textarea id='coq-ta-34'>
Lemma leqn0 (n : nat) : (n <= 0) = (n == 0).
Proof.
case: n => [| p].
by [].
by [].
Qed.
</textarea></div>
<div><p>
</div>
<p>
Sometimes case analysis is not enough.
<p>
<pre class="inline-coq" data-lang="coq">
Fixpoint muln (m n : nat) : nat :=
match m with 0 => 0 | S p => n + muln p n end.<p>
</pre>
<div>
</div>
<div><textarea id='coq-ta-35'>
Lemma muln_eq0 (m n : nat) :
(m * n == 0) = (m == 0) || (n == 0).
Proof.
case: m => [|p].
by [].
case: n => [|k]; last first. (* rotates the goals *)
by [].
Abort.
</textarea></div>
<div><p>
</div>
<p>
We don't know how to prove this yet.
But maybe someone proved it already...
<p>
<div>
</div>
<div><textarea id='coq-ta-36'>
Search _ (_ * 0) in MC.
</textarea></div>
<div><p>
</div>
<p>
The <tt>Search</tt> command is quite primitive but also
your best friend.
<p>
It takes a head pattern, the whildcard <tt>_</tt>
in the examples above, followed by term or patterns.
<p>
You must always use <tt>in MC</tt> in order to limit the search space
to the set of lemmas relevant to these lectures.
<p>
<div class="note">(notes)<div class="note-text">
This slide corresponds to
sections 2.2.2 and 2.5 of
<a href="https://math-comp.github.io/mcb/">the Mathematical Components book</a>
</div></div>
<p>
<p><br/><p>
</div>
<hr/>
<div class="slide">
<h2>
Proofs by rewriting
</h2>
<p>
The <tt>rewrite</tt> tactic uses an equation. If offers many
more flags than the ones we will see (hint: check the
Coq user manual, SSReflect chapter).
<p>
<div>
</div>
<div><textarea id='coq-ta-37'>
Lemma muln_eq0 (m n : nat) :
(m * n == 0) = (m == 0) || (n == 0).
Proof.
case: m => [ // |p].
case: n => [ |k //].
rewrite muln0.
by [].
Qed.
</textarea></div>
<div><p>
</div>
<p>
Let's now look at another example to learn more
about <tt>rewrite</tt>.
<p>
<div>
</div>
<div><textarea id='coq-ta-38'>
Lemma leq_mul2l (m n1 n2 : nat) :
(m * n1 <= m * n2) = (m == 0) || (n1 <= n2).
Proof.
rewrite /leq.
(* Search _ (_ * (_ - _)) in MC. *)
rewrite -mulnBr.
rewrite muln_eq0.
by [].
Qed.
</textarea></div>
<div><p>
</div>
<p>
<div class="note">(notes)<div class="note-text">
This slide corresponds to
section 2.2.3 of
<a href="https://math-comp.github.io/mcb/">the Mathematical Components book</a>
</div></div>
<p>
<p><br/><p>
</div>
<p>
<div class="slide">
<h2>
Lesson 1: sum up
</h2>
<p>
<h3>
Programs and specifications
</h3>
<p>
<ul class="doclist">
<li> <tt>fun .. => ..</tt>
</li>
<li> <tt>Check</tt>
</li>
<li> <tt>Definition</tt>
</li>