-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlesson4.html
1068 lines (920 loc) · 24 KB
/
lesson4.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 mini_ssrfun mini_ssrbool.
From mathcomp Require Import mini_eqtype mini_ssrnat mini_seq mini_div.
From mathcomp Require Import mini_prime.
Set Implicit Arguments.
Unset Strict Implicit.
Unset Printing Implicit Defensive.
</textarea></div>
<div><p>
<p>
what follows is a slide, it creates an index item next to the scroll bar,
just move the mouse there.
<hr/>
<div class="slide vfill" id="Outline">
<p>
<h2>
Induction and proof management
</h2>
<p>
Outline:
<p>
<ul class="doclist">
<li> 1. <a href="#Stack">How to manage your Goal: <tt>move=></tt> and <tt>move:</tt></a>
</li>
<li> 2. <a href="#Elim">Proof by induction, generalizing the induction hypothesis with <tt>elim:</tt>.</a>
</li>
<li> 3. <a href="#Views">Using views, forward <tt>=> /view</tt> and backward <tt>apply/view</tt>.</a>
</li>
<li> 4. <a href="#Have">Organizing your proof: <tt>have</tt>, <tt>suff</tt>, <tt>set</tt>, <tt>pose</tt> and <tt>rewrite</tt> variants.</a>
</li>
<li> 5. <a href="#Arithmetic">A few steps into the arithmetic library: <tt>mini_div</tt>.</a>
</li>
</ul>
</div>
<hr/>
<div class="slide vfill" id="Stack">
<h2>
Goal management <a href="#Outline">↑</a>
</h2>
<p>
<ul class="doclist">
<li> naming everything can become bothersome
</li>
<li> but, we should not let the system give random names
</li>
<li> we adopt some sort of <quote>stack & heap</quote> model
</li>
</ul>
<p>
(cf <a href="cheat_sheet.html">Cheat sheet: Management of the goal</a>)
<p>
<h3>
The stack model of a goal
</h3>
<pre class="inline-coq" data-lang="coq">
(* begining of heap *)
ci : Ti
…
dj := ej : Tj
…
Fk : Pk ci
…
(* end of heap *)
=================
forall (xl : Tl) (* top of the stack *)
…,
Pn xl -> (* nth element of the stack *)
… ->
Conclusion (* bottom of the stack = no more elements *)
<p>
</pre>
<h4>
We simulate the previous stack with the following commands:
</h4>
<p>
<div>
</div>
<div><textarea id='coq-ta-2'>
Section GoalModel.
Variables (Ti Tj Tl : Type) (ej : Tj).
Variables (Pk : Ti -> Type) (Pn : Tl -> Type).
Variables (Conclusion : Ti -> Tj -> Tl -> Type).
Lemma goal_model_example (ci : Ti) (dj : Tj := ej) (Fk : Pk ci) :
forall (xl : Tl), Pn xl -> Conclusion ci dj xl.
Abort.
</textarea></div>
<div><p>
</div>
<p>
<div class="note">(notes)<div class="note-text">
This slide corresponds to section
<a href="https://coq.inria.fr/refman/proof-engine/ssreflect-proof-language.html#bookkeeping">Bookkeeping</a> of the online documentation of the ssreflect proof language.
</div></div>
<p><br/><p>
</div>
<hr/>
<div class="slide">
<h3>
Managing the stack with tacticials <tt>=></tt> and <tt>:</tt>
</h3>
<p>
<ul class="doclist">
<li> <tt>tactic=> names</tt> executes <tt>tactic</tt>, and introduces with a names.
</li>
<li> <tt>tactic: names</tt> puts the named objects on top of the stack, then execute <tt>tactic</tt>.
</li>
<li> <tt>move</tt> is the tactic that does nothing (no-op, <tt>idtac</tt>) and is just a support for the two tacticial described above.
</li>
</ul>
<div>
</div>
<div><textarea id='coq-ta-3'>
Lemma goal_model_example (ci : Ti) (dj : Tj := ej) (Fk : Pk ci) :
forall (xl : Tl), Pn xl -> Conclusion ci dj xl.
Proof.
move=> xl pnxl.
Fail move: xl.
move: ci Fk.
Abort.
End GoalModel.
</textarea></div>
<div><p>
</div>
<div class="note">(notes)<div class="note-text">
</div></div>
<p><br/><p>
</div>
<hr/>
<div class="slide">
<h3>
intro-pattern and discharge patterns
</h3>
<p>
You can write
<p>
<ul class="doclist">
<li> <tt>tactic=> i_items</tt> where <tt>i_items</tt> is a list of <quote>intro items</quote>, where each <tt>i_item</tt> can be either,
<ul class="doclist">
<li> <tt>x</tt> a name, or
</li>
<li> <tt>_</tt> to remove the top of the stack (if possible), or
</li>
<li> <tt>//</tt> close trivial subgoals, or
</li>
<li> <tt>/=</tt> perform simplifications, or
</li>
<li> <tt>//=</tt> do both the previous, or
</li>
<li> <tt>-></tt> rewrite using the top of the stack, left to right, or
</li>
<li> <tt><-</tt> same but right to left, or
</li>
<li> <tt> [i_items|…|i_items] </tt> introductions on various sub-goals
when <tt>tactic</tt> is <tt>case</tt> or <tt>elim</tt>,
</li>
<li> …
</li>
</ul>
cf <a href="https://coq.inria.fr/refman/proof-engine/ssreflect-proof-language.html#introduction-in-the-context">ssreflect documentation on introduction to the context</a>
</li>
<li> <tt>tactic: d_items</tt> where <tt>d_items</tt> is a list of <quote>discharge</quote> items <tt>d_item_1 … d_item_n</tt>, and is equivalent to <tt>move: d_item_n; …; move: d_item_1</tt>, and
<ul class="doclist">
<li> <tt>tactic</tt> must be <tt>move</tt>, <tt>case</tt>, <tt>elim</tt>, <tt>apply</tt>, <tt>exact</tt> or <tt>congr</tt>,
</li>
<li> <tt>move: name</tt> clears the name from the context,
</li>
<li> <tt>move: pattern</tt> generalize a sub-term of the goal that match the pattern,
</li>
<li> <tt>move: (name)</tt> forces <tt>name</tt> to be a pattern, hence not clearing it.
</li>
</ul>
cf <a href="https://coq.inria.fr/refman/proof-engine/ssreflect-proof-language.html#discharge">ssreflect documentation on discharge</a>
</li>
</ul>
<p><br/><p>
</div>
<hr/>
<div class="slide" id="Elim">
<p>
<h2>
Proof by induction: generalizing the induction hypothesis <a href="#Outline">↑</a>
</h2>
<p>
<h3>
Tactics <tt>elim</tt> work on the top of the stack
</h3>
<p>
Indeed, <tt>elim: x y z => [t| u v w] </tt> is the same as
<p>
<ul class="doclist">
<li> <tt>move: x y z.</tt>
</li>
<li> <tt>elim.</tt>
</li>
<li> <tt>move=> t.</tt> in one sub-goal, <tt>move=> u v w.</tt> in the other.
</li>
</ul>
Examples:
<div>
</div>
<div><textarea id='coq-ta-4'>
Lemma addnA m n p : m + (n + p) = (m + n) + p.
Proof. by elim: m => [|m IHm]//=; rewrite !addSn IHm. Qed.
</textarea></div>
<div><p>
</div>
<p><br/><p>
</div>
<hr/>
<div class="slide">
<h3>
Generalizing the induction hypothesis.
</h3>
<p>
Sometimes, we have to generalize the induction hypothesis, and in such cases, we may use the tacticial <tt>:</tt> to generalize just before performing <tt>elim</tt>. This can even be done in the same line.
<p>
<div>
</div>
<div><textarea id='coq-ta-5'>
Lemma subnDA m n p : n - (m + p) = (n - m) - p.
Proof.
have: forall m n, n - (m + p) = (n - m) - p.
by elim=> [|m' IHm]// [].
Abort.
(* this can be rewritten like this: *)
Lemma subnDA m n p : n - (m + p) = (n - m) - p.
Proof. by elim: m n => [|m IHm]// []. Qed.
(* More complicated example *)
Lemma foldl_cat' T R f (z0 : R) (s1 s2 : seq T) :
foldl f z0 (s1 ++ s2) = foldl f (foldl f z0 s1) s2.
Proof.
move: s1 z0.
elim.
done.
move=> x xs IH.
move=> acc.
rewrite /=.
by rewrite IH.
Qed.
</textarea></div>
<div><p>
</div>
<p>
This script can be abbreviated
<pre>
Proof. by elim: s1 z0 => [//|x xs IH] acc /=; rewrite IH. Qed.
</pre>
<p>
<p><br/><p>
<p>
</div>
<hr/>
<div class="slide vfill" id="Views">
<p>
<h2>
Using views. <a href="#Outline">↑</a>
</h2>
<p>
There are two types of connectives.
<p>
<ul class="doclist">
<li> connectives in <tt>Prop</tt>: <tt>P /\ Q</tt>, <tt>P \/ Q</tt>, <tt>~ P</tt>, <tt>P -> Q</tt>, <tt>forall x, P x</tt>, <tt>exists x, Q x</tt>, which denote statements.
</li>
<li> connectives in <tt>bool</tt>: <tt>P && Q</tt>, <tt>P || Q</tt>, <tt>~~ P</tt>, <tt>P ==> Q</tt>, <tt>[forall x, P x]</tt> and <tt>[exists x, Q x]</tt>, which can be computed (thus, the boolean universal and existential can only work on finite types, which are out of the scope of this lecture).
</li>
</ul>
<div>
</div>
<div><textarea id='coq-ta-6'>
Section Connectives.
(* Locate "/\". *)
Print and.
(* Locate "&&". *)
Print andb.
(* Locate "->". *)
(* Locate "==>". *)
Print implb.
Check False -> False.
Eval compute in False -> False.
Eval compute in forall P, False -> P.
Check false ==> false.
Eval compute in false ==> false.
Eval compute in forall b, false ==> b.
End Connectives.
</textarea></div>
<div><p>
</div><div>
<h4>
Let's play a little with <tt>and</tt> and <tt>andb</tt>, <tt>or</tt> and <tt>orb</tt> in order to understand the difference.
</h4>
</div><div>
</div>
<div><textarea id='coq-ta-7'>
Lemma test_and (P : bool) :
True /\ P -> P. (* true && P -> P. *)
Proof.
move=> t_p.
case: t_p => t p.
apply: p.
Qed.
Lemma test_or (P Q R : bool) :
P \/ Q -> R. (* P || Q -> R *)
Proof.
move=> p_q.
case: p_q.
Abort.
</textarea></div>
<div><p>
</div>
<h4>
Propositions:
</h4>
<p>
<ul class="doclist">
<li> structures your proof as a tree,
</li>
<li> provides a more expressive logic (closed under <tt>forall</tt>, <tt>exists</tt>…).
</li>
</ul>
<h4>
Booleans:
</h4>
<p>
<ul class="doclist">
<li> provide computation.
</li>
</ul>
<h4>
We want the best of the two worlds, and a way to link them: views.
</h4>
<p><br/><p>
<div class="note">(notes)<div class="note-text">
This slide corresponds to
section 3.x of
<a href="https://math-comp.github.io/mcb/">the Mathematical Components book</a>
</div></div>
</div>
<hr/>
<div class="slide">
<h3>
Stating and proving a reflection view
</h3>
<p>
To link a concept in bool and one in Prop we typically
use the <tt>reflect P b</tt> predicate, which is a specialisation
of equivalence <tt>P <-> b</tt>,
where one side is in <tt>Prop</tt> and the other in <tt>bool</tt>.
To prove <tt>reflect</tt> we use the tactic <tt>prove_reflect</tt>.
<p>
<div>
</div>
<div><textarea id='coq-ta-8'>
Lemma eqnP {n m : nat} :
reflect (n = m) (eqn n m).
Proof.
prove_reflect.
elim: n m => [|n IH] m; case: m => // m Hm.
by rewrite (IH m Hm).
move=> def_n; rewrite def_n {def_n}. (* clears `def_n` *)
by elim: m.
Qed.
</textarea></div>
<div><p>
</div>
<div class="note">(notes)<div class="note-text">
This slide corresponds to
sections 4.1.1 and 4.1.2 of
<a href="https://math-comp.github.io/mcb/">the Mathematical Components book</a>
</div></div>
</div>
<hr/>
<div class="slide">
<h3>
Using views in forward chaining
</h3>
<p>
The syntax <tt>/view</tt> can be put in intro patterns
to modify the top assumption using <tt>view</tt>
<div>
</div>
<div><textarea id='coq-ta-9'>
About andP.
Lemma example n m k : k <= n ->
(n <= m) && (m <= k) -> n = k.
Proof.
move=> lekn /andP nmk; case: nmk => lenm lemk.
Abort.
About negPf.
Lemma test_negPf n m (P : pred nat) : ~~ P n -> ~~ P m -> P n = P m.
Proof.
move=> /negPf.
move=> Pn_eq_false.
rewrite Pn_eq_false.
move=> /negPf->.
by [].
Qed.
</textarea></div>
<div><p>
</div>
</div>
<hr/>
<div class="slide">
<h3>
Using view in backward chaining
</h3>
<p>
The <tt>apply:</tt> tactic accepts a <tt>/view</tt> flag
to modify the goal using <tt>view</tt>.
<div>
</div>
<div><textarea id='coq-ta-10'>
Lemma leq_total m n : (m <= n) || (m >= n).
Proof.
(* About implyNb.*)
rewrite -implyNb -ltnNge.
apply/implyP.
(* About ltnW *)
by apply: ltnW.
Qed.
</textarea></div>
<div><p>
</div>
<p><br/><p>
<div class="note">(notes)<div class="note-text">
This slide corresponds to
sections 4.1.3 and 4.1.4 of
<a href="https://math-comp.github.io/mcb/">the Mathematical Components book</a>
</div>
</div>
<hr/>
<div class="slide">
<h3>
Using views with other lemmas
</h3>
<p>
<ul class="doclist">
<li> By processing an assumption through a lemma.
</li>
<li> The leading / makes the lemma work as a function.
</li>
<li> If the lemma states <tt>A -> B</tt>, we can use it as a function to get a proof of <tt>B</tt> from a proof of <tt>A</tt>.
</li>
</ul>
<div>
</div>
<div><textarea id='coq-ta-11'>
About prime_gt1.
Lemma example_2 x y : prime x -> odd y -> 2 < y + x.
Proof.
move=> /prime_gt1 x_gt_1. (* view through prime_gt1 *)
move: x_gt_1 => /ltnW.
Abort.
</textarea></div>
<div><p>
</div>
<p><br/><p>
<div class="note">(notes)<div class="note-text">
This slide corresponds to
section 4.2 of
<a href="https://math-comp.github.io/mcb/">the Mathematical Components book</a>
</div>
</div>
<hr/>
<div class="slide">
<h3>
Views summary
</h3>
<p>
<ul class="doclist">
<li> <tt>reflect</tt> and <tt>prove_reflect</tt>
</li>
<li> <tt>move=> /v H</tt> forward chaining with a view (new <tt>i_item</tt>)
</li>
<li> <tt>apply/v</tt> backward chaining with a view
</li>
</ul>
</div>
<p>
</div>
<hr/>
<div class="slide" id="Have">
<p>
<h2>
Organizing your proofs <a href="#Outline">↑</a>
</h2>
<p>
<h3>
With <tt>have:</tt> and <tt>suff:</tt>
</h3>
<p>
<ul class="doclist">
<li> <tt>have i_items : statement.</tt> asks you to prove <tt>statement</tt> first.
</li>
<li> <tt>suff i_items : statement.</tt> asks you to prove <tt>statement</tt> last.
</li>
<li> <tt>have i_items := term.</tt> generalizes <tt>term</tt> and puts in on the top of the stack.
</li>
</ul>
This last one is very useful as an alternative of <tt>Check</tt>, since you can play with
the result which is on the top of the stack.
cf <a href="https://coq.inria.fr/refman/proof-engine/ssreflect-proof-language.html?highlight=stack#the-have-tactic">ssreflect documentation on rewrite</a>
<div>
</div>
<div><textarea id='coq-ta-12'>
Lemma test_have n :
reflect (exists x y z, (x != 0) && (y != 0) && (z != 0) && (x ^ n + y ^ n == z ^ n))
((n == 1) || (n == 2)).
Proof.
have test0 : forall x y z, x ^ 0 + y ^ 0 != z ^ 0 by [].
have test1 : exists x y z, (x != 0) && (y != 0) && (z != 0)
/\ x ^ 1 + y ^ 1 = z ^ 1 by exists 1, 1, 2.
have test2 : exists x y z, (x != 0) && (y != 0) && (z != 0)
/\ x ^ 2 + y ^ 2 = z ^ 2 by exists 3, 4, 5.
suff test m : m >= 3 -> forall x y z, (x != 0) && (y != 0) && (z != 0) ->
x ^ m + y ^ m != z ^ m.
admit.
(* The rest of the proof fits in a margin *)
Abort.
</textarea></div>
<div><p>
</div>
</div>
<hr/>
<div class="slide">
<h3>
With <tt>set</tt> and <tt>pose</tt>, naming expressions
</h3>
<p>
<ul class="doclist">
<li> <tt>pose name := pattern</tt>, generalizes every hole in the pattern,
and puts a definition <tt>name</tt> in the context for it.
It does NOT change the conclusion.
</li>
<li> <tt>set name := pattern</tt>, finds the pattern in the conclusion,
and puts a definition <tt>name</tt> in the context.
Finally, it substitutes the pattern for <tt>name</tt> in the conclusion.
</li>
</ul>
<div>
</div>
<div><textarea id='coq-ta-13'>
Lemma test m n k : (2 + n + 52) * m = k.
Proof.
pose x := (_ + n + _) * m.
set y := (_ * _).
Abort.
</textarea></div>
<div><p>
</div>
</div>
<hr/>
<div class="slide">
<h3>
Variants of the <tt>rewrite</tt> tactic.
</h3>
<p>
<h4>
Use <tt>rewrite -equation</tt> to rewrite right to left.
</h4>
<div>
</div>
<div><textarea id='coq-ta-14'>
Section RtoL.
Variables (P : nat -> Prop) (n : nat) (P_addn1 : P (n + 1)).
Lemma RtoL_example : P (S n).
Proof.
rewrite -addn1.
by [].
Qed.
End RtoL.
</textarea></div>
<div><p>
<h4>
Use <tt>rewrite [pattern]equation</tt> to select what you want to rewrite.
</h4>
<div>
</div>
<div><textarea id='coq-ta-15'>
Lemma pattern_example (m n : nat) : n + 1 = 1 + m -> n + 1 = m + 1.
Proof.
rewrite [m + 1]addnC.
by [].
Qed.
</textarea></div>
<div><p>
</div>
<p>
<h4>
Use <tt>-[pattern]/term</tt> to replace a term by a convertible one.
</h4>
e.g.
<p>
<ul class="doclist">
<li> <tt>-[2]/(1 + 1)</tt> replaces <tt>2</tt> by <tt>1 + 1</tt>,
</li>
<li> <tt>-[2 ^ 2]/4</tt> replaces <tt>2 ^ 2</tt> by <tt>4</tt>,
</li>
<li> <tt>-[m]/(0 * d + m)</tt> replaces <tt>m</tt> by <tt>0 * d + m</tt>.
</li>
</ul>
<div>
</div>
<div><textarea id='coq-ta-16'>
Lemma change_example (P : nat -> nat -> bool) :
(forall m n k, P (m + n) (k + n) = P (m + k) (n + m)) ->
P 2 3 -> P 3 1.
Proof.
move=> Pmn P31.
rewrite -[3]/(2 + 1).
rewrite -[X in P _ X]/(0 + 1).
by rewrite Pmn.
Qed.
</textarea></div>
<div><p>
</div>
</div>
<hr/>
<div class="slide vfill" id="Arithmetic">
<p>
<h2>
A few steps into the arithmetic library. <a href="#Outline">↑</a>
</h2>
<p>
The main functions symbols (besides <tt>addn</tt>, <tt>muln</tt>, <tt>subn</tt>, ...)
<p>
<h3>
Large and Strict comparison.
</h3>
<div>
</div>
<div><textarea id='coq-ta-17'>
Check leq.
Check (1 <= 3).
(* 1 <= 3 *)
Print ltn. (* autosimplifying, never expect to see it *)
Notation "n < m" := (S n <= m) (only printing).
Check (1 <= 3).
(* 0 < 3 *)
</textarea></div>
<div><p>
</div>
<p>
<h3>
Euclidean division <tt>edivn</tt> and its quotient <tt>divn</tt> and remainder <tt>modn</tt>
</h3>
<p>
<div>
</div>
<div><textarea id='coq-ta-18'>
Check edivn.
Search _ edivn in MC.
Check edivn_eq.
(* forall d q r : nat, r < d -> edivn (q * d + r) d = (q, r) *)
Print divn.
(* divn m d := fst (edivn m d) *)
Check modn.
Check modn_def.
(* m d = snd (edivn m d) *)
Check ltn_mod.
(* forall m d : nat, (m d < d) = (0 < d) *)
</textarea></div>
<div><p>
</div>
<p>
<h3>
Divisibility
</h3>
<div>
</div>
<div><textarea id='coq-ta-19'>
Print dvdn.
(* dvdn d m := m d == 0 *)
Check (2 %| 4).
Eval compute in (2 %| 4).
Search muln dvdn in MC.
(*
dvdnP: forall d m : nat, reflect (exists k : nat, m = k * d) (d | n -> d | m -> d *)
</textarea></div>
<div><p>
</div>
<p>
<h3>
Equality modulo
</h3>
<p>
The notation <tt>m = n %[mod d]</tt> is an abbreviation for <tt>m %% d = n %% d</tt>.
<div>
</div>
<div><textarea id='coq-ta-20'>
Search _ (_ = _ %[mod _]) in MC.
(*
modn_mod: forall m d : nat, m d = m <tt>mod d</tt>
modnDl: forall m d : nat, d + m = m <tt>mod d</tt>
*)
</textarea></div>
<div><p>
</div>
<p>
<h3>
GCD, relative primality and primality
</h3>
<p>
<div>
</div>
<div><textarea id='coq-ta-21'>
Check gcdn.
Search _ gcdn dvdn in MC.
(*
dvdn_gcd: forall p m n : nat, (p | m) && (p | m ->
d | m -> d' | d) -> gcdn m n = d
*)
Check coprime.
Print coprime.
(* coprime m n := gcdn m n == 1 *)
Check prime.
Search _ prime in MC.
(*
primeP:
forall p : nat,
reflect (1 < p /\ (forall d : nat, d *)
</textarea></div>
<div><p>
</div>
<p>
</div>
<hr/>
</div>
<div><textarea id='coq-ta-22'>
</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', 'coq-ta-16',
'coq-ta-17', 'coq-ta-18', 'coq-ta-19', 'coq-ta-20',
'coq-ta-21', 'coq-ta-22'];
</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 (