/
init.html
5035 lines (4150 loc) · 202 KB
/
init.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
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Initial</title>
<meta name="generator" content="Org mode" />
<meta name="author" content="Ubuntu" />
<link rel="stylesheet" type="text/css" href="./code/style.css"/>
<script type="text/javascript">
/*
@licstart The following is the entire license notice for the
JavaScript code in this tag.
Copyright (C) 2012-2013 Free Software Foundation, Inc.
The JavaScript code in this tag is free software: you can
redistribute it and/or modify it under the terms of the GNU
General Public License (GNU GPL) as published by the Free Software
Foundation, either version 3 of the License, or (at your option)
any later version. The code is distributed WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
As additional permission under GNU GPL version 3 section 7, you
may distribute non-source (e.g., minimized or compacted) forms of
that code without the copy of the GNU GPL normally required by
section 4, provided you include this license notice and a URL
through which recipients can access the Corresponding Source.
@licend The above is the entire license notice
for the JavaScript code in this tag.
*/
<!--/*--><![CDATA[/*><!--*/
function CodeHighlightOn(elem, id)
{
var target = document.getElementById(id);
if(null != target) {
elem.cacheClassElem = elem.className;
elem.cacheClassTarget = target.className;
target.className = "code-highlighted";
elem.className = "code-highlighted";
}
}
function CodeHighlightOff(elem, id)
{
var target = document.getElementById(id);
if(elem.cacheClassElem)
elem.className = elem.cacheClassElem;
if(elem.cacheClassTarget)
target.className = elem.cacheClassTarget;
}
/*]]>*///-->
</script>
</head>
<body>
<div id="preamble" class="status">
<p><a href="index.html">top</a> | <a href="theindex.html">index</a> | <a href="sitemap.html">sitemap</a> | <a href="https://github.com/1337777/OOO1337777">edit</a></p>
</div>
<div id="content">
<h1 class="title">Initial</h1>
<div id="table-of-contents">
<h2>Table of Contents</h2>
<div id="text-table-of-contents">
<ul>
<li><a href="#org281109a">1. Functions : functional-hold, delayed-substitution</a></li>
<li><a href="#orge89c35e">2. Data : class, constructor functions, destructor function</a>
<ul>
<li><a href="#orgf7029dd">2.1. Binary data</a></li>
<li><a href="#orgc6fa629">2.2. Numbers data</a></li>
</ul>
</li>
<li><a href="#org61b34fe">3. Parametrism : parametric data, parametric functions</a>
<ul>
<li><a href="#orgf587c97">3.1. Option data</a></li>
<li><a href="#orgd9d18ca">3.2. List data</a></li>
</ul>
</li>
<li><a href="#orgc89115f">4. Classification</a>
<ul>
<li><a href="#orge26dc28">4.1. List classification</a></li>
<li><a href="#org69f7c7e">4.2. Equality classification</a></li>
</ul>
</li>
<li><a href="#org9a96012">5. Deduction : functions</a>
<ul>
<li><a href="#orga0e64d6">5.1. Goal as nested-stack</a></li>
<li><a href="#orgdf84ff0">5.2. Intro from, apply in, specialize of, substitution by - the nested-stack</a></li>
<li><a href="#org35646e4">5.3. Revert to, generalize in, unification of - the nested-stack</a>
<ul>
<li><a href="#org3e00b9d">5.3.1. revert</a></li>
<li><a href="#org9853a5a">5.3.2. generalize, unification-generalize</a></li>
<li><a href="#org8c9a58f">5.3.3. revert-then-intro</a></li>
<li><a href="#orgb9c59cd">5.3.4. equational-generalize</a></li>
<li><a href="#orgc7d906f">5.3.5. lessorequal-generalize</a></li>
<li><a href="#org2d588a1">5.3.6. initial-accumulator-generalize</a></li>
<li><a href="#orgd6a6abc">5.3.7. forward-generalize, backward-generalize</a></li>
<li><a href="#orgb73d66b">5.3.8. weakening-generalize</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#orgd24389c">6. Deduction : data</a>
<ul>
<li><a href="#org78af1ae">6.1. inference</a></li>
<li><a href="#org1025155">6.2. classifying inference</a></li>
<li><a href="#org287d260">6.3. alternative</a></li>
<li><a href="#orga148bcb">6.4. recursion</a></li>
</ul>
</li>
<li><a href="#orgd8d9bee">7. Deduction : classification</a>
<ul>
<li><a href="#orga09a6b7">7.1. meta-computation</a></li>
<li><a href="#org8bd99e8">7.2. boolean-computational reflection</a></li>
<li><a href="#org8625402">7.3. boolean simultaneous-substitution</a></li>
<li><a href="#orge4c4b54">7.4. recursion</a></li>
</ul>
</li>
<li><a href="#orgf1dddce">8. Review of some long deductions</a>
<ul>
<li><a href="#orgffff74e">8.1. Accumulating division</a></li>
<li><a href="#orgcccb2a5">8.2. Review of OOO1337777</a></li>
</ul>
</li>
<li><a href="#orgda864d4">9. ConfusPlay</a>
<ul>
<li><a href="#orga20992e">9.1. ConfusPlay 1</a></li>
<li><a href="#orga2a7ed5">9.2. ConfusPlay 2, refer ConfusPlay 1</a></li>
</ul>
</li>
</ul>
</div>
</div>
<p>
Initial view of some randomly-selected things in the <code>PROGRAMME</code> , which sometimes are
<span class="underline">generating instances</span> : instead of grammatically-describing some <i>general</i> class,
oneself sensibly-describes <i>one instance</i> of the class which is (almost-)generating
all the other instances of the class.
</p>
<div id="outline-container-org281109a" class="outline-2">
<h2 id="org281109a"><span class="section-number-2">1</span> Functions : functional-hold, delayed-substitution</h2>
<div class="outline-text-2" id="text-1">
<p>
Oneself lacks to write and solve the question
</p>
<pre class="example">
2 + 3
</pre>
<p>
In reality, oneself may want / lack to write and solve many such questions. Therefore
oneself may <span class="underline">hold</span> / <span class="underline">bind</span> some <span class="underline">name</span> / <span class="underline">identifier</span> such as <code>a</code> and <code>p</code> and write
the <span class="underline">functional-hold</span> / <span class="underline">function</span> ( "abstraction" )
</p>
<pre class="example">
fun p => fun a => a + p
</pre>
<p>
and then <span class="underline">(delay-)substitute</span> / <span class="underline">instantiate</span> / <span class="underline">apply</span> this single expression by many
<span class="underline">input</span> ( <span class="underline">outer</span> ) <span class="underline">parameter</span> <code>p</code> other than <code>3</code> and many <span class="underline">input</span> ( <span class="underline">inner</span> )
<span class="underline">argument</span> <code>a</code> other than <code>2</code>, and get <span class="underline">output</span> such as this <span class="underline">delayed-substitution</span>
( "application" )
</p>
<pre class="example">
(fun p => fun a => a + p) 3 2
</pre>
<p>
Does this solves the initial question ? What is the <span class="underline">motivation</span> ?
</p>
<p>
Primo, one motivation may be to memorize things. Therefore oneself may do some <span class="underline">outer</span>
programming / <span class="underline">commanding</span> of the <code>COQ</code> computer, and ( <span class="underline">top / globally</span> ) <span class="underline">behold</span> /
<span class="underline">define</span> some <span class="underline">name</span> / <span class="underline">identifier</span> <code>f</code> which <span class="underline">memorize</span> / <span class="underline">fold</span> / <span class="underline">shorten</span> some
common expressions such as :
</p>
<div class="org-src-container">
<pre class="src src-coq"><span style="font-weight: bold;">Definition</span> <span style="font-weight: bold;">f</span> := <span style="font-weight: bold; text-decoration: underline;">fun</span> <span style="font-weight: bold; font-style: italic;">p</span> => <span style="font-weight: bold; text-decoration: underline;">fun</span> <span style="font-weight: bold; font-style: italic;">a</span> => a + p .
</pre>
</div>
<pre class="example">
f is defined
</pre>
<p>
Secondo, another motivation may be to <span class="underline">confirm</span> / <span class="underline">check</span> that whatever has been
memorized fulfill some prescribed/wanted <span class="underline">classification</span> / <span class="underline">property</span> /
<span class="underline">specification</span> / <span class="underline">type</span>, by doing <span class="underline">logical</span> <code>COQ</code> commands such as :
</p>
<div class="org-src-container">
<pre class="src src-coq"><span style="font-weight: bold;">Definition</span> <span style="font-weight: bold;">f</span> : nat -> nat -> nat := <span style="font-weight: bold; text-decoration: underline;">fun</span> <span style="font-weight: bold; font-style: italic;">p</span> => <span style="font-weight: bold; text-decoration: underline;">fun</span> <span style="font-weight: bold; font-style: italic;">a</span> => a + p .
</pre>
</div>
<pre class="example">
f is defined
</pre>
<div class="org-src-container">
<pre class="src src-coq"><span style="font-weight: bold;">Check</span> f.
</pre>
</div>
<pre class="example">
f
: nat -> nat -> nat
</pre>
<p>
in contrast to, which is false,
</p>
<pre class="example">
Definition f : nat -> nat -> bool := fun p => fun a => a + p .
</pre>
<pre class="example">
Toplevel input, characters 55-60:
> Definition f : nat -> nat -> bool := fun p => fun a => a + p .
> ^^^^^
Error:
In environment
p : nat
a : nat
The term "a + p" has type "nat" while it is expected to have type "bool".
</pre>
<p>
where <code>nat</code> is the more primitive class <code>0 | 1 | 2 | 3 | ...</code> and <code>bool</code> is the more
primitive class <code>true | false</code> and <code>nat -> nat -> nat</code> is some more composite class
which iterates two <span class="underline">function class former</span> ( <span class="underline">inference</span> <code>.. -> ..</code> or <span class="underline">classifying
inference</span> / <span class="underline">quantification</span> <code>forall .. , ..</code> ) such to classify such functions.
</p>
<p>
In short : whenever assuming <code>(x : A)</code> ( <code>x</code> is some identifier classified by the
class <code>A</code> ) one has <code>(b : B)</code> ( <code>b</code> is some term classified by the classification <code>B</code>
), … therefore one may (introduce <code>forall</code> by) <i>form</i> this term and its
classification <code>((fun x : A => b) : (forall x : A, B))</code> , also written <code>((fun x : A =>
b) : (A -> B))</code> when the identifier <code>x</code> does not occur/mentionned in the textual
description of <code>B</code>. This process is named <span class="underline">functional-holding</span> / <span class="underline">functional-binding</span>
/ <span class="underline">discharge</span>.
</p>
<p>
And in the absence of some prescribed/wanted classification or in the presence of only
partial classification, the <code>COQ</code> computer will attempt to <span class="underline">infer</span> some full
classification whenever possible. This inference may fail because the given expression
is non classifiable or the prescribed/wanted classification is not contained/subclass
of some possible inferred classification.
</p>
<p>
Tertio, another motivation may be to get the output in some more primitive
<span class="underline">resolution</span> / <span class="underline">value</span> / <span class="underline">normal form</span>, by doing <span class="underline">computational</span> <code>COQ</code> commands such
as :
</p>
<div class="org-src-container">
<pre class="src src-coq"><span style="font-weight: bold;">Eval</span> <span style="text-decoration: underline;">compute</span> <span style="font-weight: bold; text-decoration: underline;">in</span> f 3 2.
</pre>
</div>
<pre class="example">
= 5
: nat
</pre>
<p>
In short : whenever <code>(f : forall x : A, B)</code> ( <code>f</code> is some term formed as above ) and
<code>(t : A)</code> ( <code>t</code> is some term classified by the class <code>A</code> ), … therefore one may
(eliminate <code>forall</code> by) <i>form</i> this term and its classification <code>((f t) : B[t/x])</code>,
where <code>(B[t/x])</code> signifies the substitution of <code>t</code> for <code>x</code> in the textual description
of <code>B</code>, also written <code>((f t) : B)</code> when the identifier <code>x</code> does not occur/mentionned
in the textual description of <code>B</code>. This process is named <span class="underline">delayed-substitution</span> /
<span class="underline">application</span>.
</p>
<p>
Finally, other <code>COQ</code> commands query the present memory of the <code>COQ</code> computer, such
as :
</p>
<div class="org-src-container">
<pre class="src src-coq"><span style="font-weight: bold;">About</span> f.
</pre>
</div>
<pre class="example">
f : nat -> nat -> nat
Argument scopes are [nat_scope nat_scope]
f is transparent
Expands to: Constant Top.f
</pre>
<div class="org-src-container">
<pre class="src src-coq"><span style="font-weight: bold;">Print</span> f.
</pre>
</div>
<pre class="example">
f = fun p : nat => addn^~ p
: nat -> nat -> nat
Argument scopes are [nat_scope nat_scope]
</pre>
<div class="org-src-container">
<pre class="src src-coq">Reset f.
</pre>
</div>
<pre class="example">
</pre>
<div class="org-src-container">
<pre class="src src-coq"><span style="font-weight: bold;">About</span> f.
</pre>
</div>
<pre class="example">
f not a defined object.
</pre>
<p>
<i>From the angle of view that computers is the "foundations" of mathematics, one may
not delay too much on the mathematical "foundations" of the <code>COQ</code> computer.</i>
</p>
</div>
</div>
<div id="outline-container-orge89c35e" class="outline-2">
<h2 id="orge89c35e"><span class="section-number-2">2</span> Data : class, constructor functions, destructor function</h2>
<div class="outline-text-2" id="text-2">
<p>
Oneself may want to <span class="underline">classify</span> / <span class="underline">type</span> some data, for example : classify <code>true |
false</code> together and name it <code>bool</code> ( or <code>bin</code> ) ; classify <code>1 | 2 | 3 | ...</code> together
and name it <code>nat</code> …
</p>
<p>
Primo, one shall say <span class="underline">alternatives</span> / <span class="underline">cases</span> to (recursively) <span class="underline">construct</span> / <span class="underline">build</span>
data in this class. Each alternative is described by some <span class="underline">constructor function</span> which
always output into this class. Moveover this constructor function may (recursively)
take input from this class. The terminology <span class="underline">constructor constant</span> is used in the
instance that this constructor function does not take any input.
</p>
<p>
Secondo, one shall say that these given alternate constructions <span class="underline">computationally or
logically fulfill</span> / <span class="underline">support</span> this class, which is that it is sufficient to focus /
touch on these (recursively) constructored data when holding this class, which is that
any (random) data in the class may be such (recursively) <span class="underline">destructed</span> / <span class="underline">eliminated</span> /
<span class="underline">matched</span> / <span class="underline">filtered</span>. This is described by one (grammatical) <span class="underline">destructor / match
function</span> which always input from this class.
</p>
</div>
<div id="outline-container-orgf7029dd" class="outline-3">
<h3 id="orgf7029dd"><span class="section-number-3">2.1</span> Binary data</h3>
<div class="outline-text-3" id="text-2-1">
<p>
The name of the two constructors are <code>true</code> and <code>false</code> , and the name of the class
/ type is <code>bool</code> ; and this is how to command <code>COQ</code> to memorize such names :
</p>
<div class="org-src-container">
<pre class="src src-coq"><span style="font-weight: bold;">Inductive</span> <span style="font-weight: bold;">bool</span> := true : bool | false : bool.
</pre>
</div>
<pre class="example">
bool is defined
bool_rect is defined
bool_ind is defined
bool_rec is defined
</pre>
<p>
And <code>COQ</code> defines and memorize additional names <code>bool_rect</code> , <code>bool_ind</code> , <code>bool_rec</code>
which are easier decoration shortening of the more primitive same (grammatical)
<span class="underline">destructor / match function</span> :
</p>
<div class="org-src-container">
<pre class="src src-coq"><span style="font-weight: bold;">Print</span> bool_rect.
</pre>
</div>
<pre class="example">
bool_rect =
fun (P : bool -> Type) (f : P true) (f0 : P false) (b : bool) =>
if b as b0 return (P b0) then f else f0
: forall P : bool -> Type, P true -> P false -> forall b : bool, P b
Argument scopes are [function_scope _ _ bool_scope]
</pre>
<p>
which says as expected that these given <code>true | false</code> alternate constructions
<span class="underline">computationally or logically fulfill</span> / <span class="underline">support</span> this class <code>bool</code> … Some instance
of this same <code>match</code> destruction / filtering / elimination function, using shorter
grammar, is :
</p>
<div class="org-src-container">
<pre class="src src-coq"><span style="font-weight: bold;">Check</span> (<span style="font-weight: bold; text-decoration: underline;">if</span> true <span style="font-weight: bold; text-decoration: underline;">then</span> 3 <span style="font-weight: bold; text-decoration: underline;">else</span> 2) .
</pre>
</div>
<pre class="example">
if true then 3 else 2
: nat
</pre>
<div class="org-src-container">
<pre class="src src-coq"><span style="font-weight: bold;">Eval</span> <span style="text-decoration: underline;">compute</span> <span style="font-weight: bold; text-decoration: underline;">in</span> ((<span style="font-weight: bold; text-decoration: underline;">fun</span> <span style="font-weight: bold; font-style: italic;">b</span> : bool => (<span style="font-weight: bold; text-decoration: underline;">if</span> b <span style="font-weight: bold; text-decoration: underline;">then</span> 3 <span style="font-weight: bold; text-decoration: underline;">else</span> 2)) false) .
</pre>
</div>
<pre class="example">
= 2
: nat
</pre>
<p>
The <code>PROGRAMME</code> contains some collection of binary / boolean operations that mirror
reasoning steps on truth values. The functions are named <code>negb</code> ( <span class="underline">negation</span> ) , <code>orb</code>
( <span class="underline">orelse</span> ) , <code>andb</code> ( <span class="underline">andthen</span> ), <code>implyb</code> ( <span class="underline">branch</span> ), correspondingly with
notations <code>~~</code> , <code>||</code> , <code>&&</code> , and <code>==></code> . The first operator is <span class="underline">prefix</span> (non
left-recursive parsing) parsed as in <code>~~ b</code> , the last three operators are <span class="underline">no-prefix</span>
( <span class="underline">infix</span> ) parsed as in <code>b1 && b2</code> .
</p>
<p>
For instance, the function <code>andb</code> ouputs true the-same-as [ the first input is true
andthen the second input is true ] :
</p>
<div class="org-src-container">
<pre class="src src-coq"><span style="font-weight: bold;">Definition</span> <span style="font-weight: bold;">andb</span> b1 b2 := <span style="font-weight: bold; text-decoration: underline;">if</span> b1 <span style="font-weight: bold; text-decoration: underline;">then</span> b2 <span style="font-weight: bold; text-decoration: underline;">else</span> false.
</pre>
</div>
</div>
</div>
<div id="outline-container-orgc6fa629" class="outline-3">
<h3 id="orgc6fa629"><span class="section-number-3">2.2</span> Numbers data</h3>
<div class="outline-text-3" id="text-2-2">
<p>
Any number is zero or the successor of an existing number :
</p>
<div class="org-src-container">
<pre class="src src-coq"><span style="font-weight: bold;">Inductive</span> <span style="font-weight: bold;">nat</span> :=
O : nat
| S : nat -> nat.
</pre>
</div>
<pre class="example">
nat is defined
nat_rect is defined
nat_ind is defined
nat_rec is defined
</pre>
<p>
This command says that the only ways to produce numbers are by using the constant
<span class="underline">symbol</span> / <span class="underline">sign</span> / <span class="underline">token</span> <code>O</code> ( capital « o » letter, to represent <code>0</code> ), or by
applying the function symbol <code>S</code> to some already existing number. In other words, <code>O</code>
is some number, <code>(S O)</code> is some number, <code>(S (S O))</code> , and so on, and these are the
only numbers.
</p>
<p>
And <code>COQ</code> defines and memorize additional names <code>nat_rect</code> , <code>nat_ind</code> , <code>nat_rec</code>
which are easier decoration shortening of the more primitive same (grammatical)
<span class="underline">destructor/match function</span> :
</p>
<div class="org-src-container">
<pre class="src src-coq"><span style="font-weight: bold;">Print</span> nat_rect.
</pre>
</div>
<pre class="example">
nat_rect =
fun (P : nat -> Type) (f : P 0) (f0 : forall n : nat, P n -> P n.+1) =>
fix F (n : nat) : P n :=
match n as n0 return (P n0) with
| 0 => f
| n0.+1 => f0 n0 (F n0)
end
: forall P : nat -> Type,
P 0 -> (forall n : nat, P n -> P n.+1) -> forall n : nat, P n
Argument scopes are [function_scope _ function_scope nat_scope]
</pre>
<p>
which says as expected that these given <code>O | S (n : nat)</code> alternate constructions
<span class="underline">computationally or logically fulfill</span> / <span class="underline">support</span> this class <code>nat</code> … One shall
clarify why this <code>fix</code> keytext for <code>nat</code> ( instead of the ealier only <code>fun</code> keytext
for <code>bool</code> ) later.
</p>
<p>
When interacting with <code>COQ</code>, oneself will often see decimal notations ( <code>0</code> <code>1</code> <code>2</code>
<code>3</code> <code>4</code> … ), but these are only some parsing and printing / display facility
provided to the programmer for readability. In other words <code>O</code> is printed <code>0</code> , <code>(S
O)</code> is printed <code>1</code> , <code>(S (S O))</code> is printed <code>2</code> … Programmers may also write decimal
numbers to describe values, but these are automatically parsed into terms built with
<code>O</code> and <code>S</code>.
</p>
<p>
Also, the <span class="underline">postfix</span> (infix) <code>x.+1</code> notation is translated as the prefix expression <code>S
x</code> . The <code>.+1</code> notation binds more strongly (at level 2) than function application (at
level 10). Attempt :
</p>
<div class="org-src-container">
<pre class="src src-coq"><span style="font-weight: bold;">Print</span> Grammar constr.
</pre>
</div>
<div class="org-src-container">
<pre class="src src-coq"><span style="font-weight: bold;">Check</span> <span style="font-weight: bold; text-decoration: underline;">fun</span> <span style="font-weight: bold; font-style: italic;">x</span> => (<span style="font-weight: bold; text-decoration: underline;">fun</span> <span style="font-weight: bold; font-style: italic;">n</span> : nat => n) x.+1 .
<span style="font-weight: bold;">Locate</span> <span style="font-style: italic;">".+1"</span> .
</pre>
</div>
<pre class="example">
fun x : nat => id x.+1
: nat -> nat
</pre>
<p>
When computing functions over number input data or deducting lemmas over number
subject data, oneself may proceed by touching only the alternative cases form of the
data, and therefore by the minimality / inductive / elimination for <code>nat</code> , the
function or deduction will be indeed over all numbers. For example here is the
definition of « beheading <code>S</code> » ( predecessor ) for numbers :
</p>
<div class="org-src-container">
<pre class="src src-coq"><span style="font-weight: bold;">Definition</span> <span style="font-weight: bold;">pred</span> n :=
<span style="font-weight: bold; text-decoration: underline;">match</span> n <span style="font-weight: bold; text-decoration: underline;">with</span>
O => O
| S t => t
<span style="font-weight: bold; text-decoration: underline;">end</span>.
</pre>
</div>
<p>
The <span class="underline">branch</span> <code>O => O</code> says that when <code>n</code> has the alternative zero form <code>O</code> then the
whole expression is transformed to <code>O</code> ; here the left <code>O</code> is some <span class="underline">filter</span> /
<span class="underline">pattern</span> and the computation decides whether <code>n</code> <span class="underline">matches</span> this pattern. The branch
<code>S t => t</code> says that when <code>n</code> has the alternative successor form <code>S t</code> then <code>t</code> is
instantiated/bound by this subterm (tail) of <code>n</code> and the whole expression is
transformed to <code>t</code> ; here <code>S t</code> is some filter / pattern containing one named
<span class="underline">filter-identifier</span> / <span class="underline">filter-variable</span> <code>t</code> and the computation decides whether <code>n</code>
matches this pattern.
</p>
<p>
Each constructor must be covered by some branch and by at most one branch. For
example, this attempt is not memorized by the <code>COQ</code> computer :
</p>
<div class="org-src-container">
<pre class="src src-coq">Fail <span style="font-weight: bold;">Definition</span> <span style="font-weight: bold;">wrong</span> (<span style="font-weight: bold; font-style: italic;">n</span> : nat) :=
<span style="font-weight: bold; text-decoration: underline;">match</span> n <span style="font-weight: bold; text-decoration: underline;">with</span> 0 => true <span style="font-weight: bold; text-decoration: underline;">end</span>.
</pre>
</div>
<pre class="example">
The command has indeed failed with message:
Non exhaustive pattern-matching: no clause found for pattern
_.+1
</pre>
<p>
Memo that the <code>COQ</code> (outer) parser and printer ( <code>CAMLP5</code> ) may prevent programmer
fatigue and translate the grammar
</p>
<div class="org-src-container">
<pre class="src src-coq"><span style="font-weight: bold;">Definition</span> <span style="font-weight: bold;">sameOn_bool_nat</span> b n :=
<span style="font-weight: bold; text-decoration: underline;">match</span> b, n <span style="font-weight: bold; text-decoration: underline;">with</span>
| true, S _ => true
| _, _ => false
<span style="font-weight: bold; text-decoration: underline;">end</span>.
</pre>
</div>
<p>
as the same as
</p>
<div class="org-src-container">
<pre class="src src-coq">Reset sameOn_bool_nat.
<span style="font-weight: bold;">Definition</span> <span style="font-weight: bold;">sameOn_bool_nat</span> b n :=
<span style="font-weight: bold; text-decoration: underline;">match</span> b <span style="font-weight: bold; text-decoration: underline;">with</span>
| true => <span style="font-weight: bold; text-decoration: underline;">if</span> n <span style="font-weight: bold; text-decoration: underline;">is</span> S t <span style="font-weight: bold; text-decoration: underline;">then</span> true <span style="font-weight: bold; text-decoration: underline;">else</span> false
| _ => false
<span style="font-weight: bold; text-decoration: underline;">end</span>.
</pre>
</div>
<p>
Now some clarification for the <code>fix</code> keytext. When computing functions over number
input data or deducting lemmas over number subject data, oneself may transform the
input arguments to the function or lemma into some other input arguments for the
<i>same</i> function or lemma. The keytext <code>fix</code> says that the same function name may be
mentioned. For example here is the definition of concatenation (addition) for
numbers :
</p>
<div class="org-src-container">
<pre class="src src-coq"><span style="font-weight: bold;">Check</span>
<span style="font-weight: bold; text-decoration: underline;">fix</span> add n m :=
<span style="font-weight: bold; text-decoration: underline;">match</span> n <span style="font-weight: bold; text-decoration: underline;">with</span>
S t => add t (S m)
| O => m
<span style="font-weight: bold; text-decoration: underline;">end</span> .
</pre>
</div>
<pre class="example">
fix add (n m : nat) {struct n} : nat :=
match n with
| 0 => m
| t.+1 => add t m.+1
end
: nat -> nat -> nat
</pre>
<p>
Memo that one of the <span class="underline">inner memories</span> / <span class="underline">accumulators</span> <code>n</code> and <code>m</code> shall be
<span class="underline">degrading</span> / <span class="underline">decreasing</span> / <span class="underline">structural</span> / <span class="underline">terminating</span>, here it is the accumulator
<code>n</code> which is decreasing ( structural, <code>{struct n}</code> ). And the <code>COQ</code> computer is very
good at detecting when something is degrading, otherwise there are many other
techniques to solves this question of termination …
</p>
<p>
Now instead of using two (inner) accumulators <code>n</code> and <code>m</code> , oneself may want to use :
</p>
<ul class="org-ul">
<li>only one inner accumulator <code>n</code> , together with</li>
<li><span class="underline">pending</span> (outer) effects / computations ( the <code>S</code> surrounding <code>(add t)</code> ) instead
of changing some second inner memory, together with</li>
<li>one <span class="underline">outer parameter</span> <code>p</code> which is <i>not changed</i> as memory during computation.</li>
</ul>
<div class="org-src-container">
<pre class="src src-coq"><span style="font-weight: bold;">Check</span>
<span style="font-weight: bold; text-decoration: underline;">fun</span> <span style="font-weight: bold; font-style: italic;">p</span> => <span style="font-weight: bold; text-decoration: underline;">fix</span> add n :=
<span style="font-weight: bold; text-decoration: underline;">match</span> n <span style="font-weight: bold; text-decoration: underline;">with</span>
S t => S (add t)
| O => p
<span style="font-weight: bold; text-decoration: underline;">end</span> .
</pre>
</div>
<pre class="example">
fun p : nat =>
fix add (n : nat) : nat := match n with
| 0 => p
| t.+1 => (add t).+1
end
: nat -> nat -> nat
</pre>
<p>
Finally oneself may command <code>COQ</code> to memorize this expression :
</p>
<div class="org-src-container">
<pre class="src src-coq"><span style="font-weight: bold;">Definition</span> <span style="font-weight: bold;">add</span> :=
<span style="font-weight: bold; text-decoration: underline;">fun</span> <span style="font-weight: bold; font-style: italic;">p</span> => <span style="font-weight: bold; text-decoration: underline;">fix</span> add n :=
<span style="font-weight: bold; text-decoration: underline;">match</span> n <span style="font-weight: bold; text-decoration: underline;">with</span>
S t => S (add t)
| O => p
<span style="font-weight: bold; text-decoration: underline;">end</span> .
</pre>
</div>
<pre class="example">
add is defined
</pre>
<p>
To prevent programmer fatigue, <code>COQ</code> has some alias command <code>Fixpoint</code> which does
<i>almost the same</i> thing :
</p>
<div class="org-src-container">
<pre class="src src-coq"><span style="font-weight: bold;">Fixpoint</span> <span style="font-weight: bold;">add</span> p n {<span style="font-weight: bold; text-decoration: underline;">struct</span> n} :=
<span style="font-weight: bold; text-decoration: underline;">match</span> n <span style="font-weight: bold; text-decoration: underline;">with</span>
S t => S (add p t)
| O => p
<span style="font-weight: bold; text-decoration: underline;">end</span> .
<span style="font-weight: bold;">Print</span> add.
</pre>
</div>
<pre class="example">
add is defined
add is recursively defined (decreasing on 2nd argument)
add =
fix add (p n : nat) {struct n} : nat :=
match n with
| 0 => p
| t.+1 => (add p t).+1
end
: nat -> nat -> nat
Argument scopes are [nat_scope nat_scope]
</pre>
<p>
or <code>COQ</code> has some combination of the <code>Section</code> command with the <code>Fixpoint</code> command
which does <i>precisely the same</i> thing, because the <code>Section</code> process is for holding /
binding <span class="underline">(outer) parameters / variables</span> :
</p>
<div class="org-src-container">
<pre class="src src-coq"><span style="font-weight: bold;">Section</span> <span style="font-weight: bold;">Section1</span>.
<span style="font-weight: bold;">Variable</span> <span style="font-weight: bold; font-style: italic;">p</span> : nat.
<span style="font-weight: bold;">Fixpoint</span> <span style="font-weight: bold;">add</span> n :=
<span style="font-weight: bold; text-decoration: underline;">match</span> n <span style="font-weight: bold; text-decoration: underline;">with</span>
S t => S (add t)
| O => p
<span style="font-weight: bold; text-decoration: underline;">end</span> .
<span style="font-weight: bold;">End</span> <span style="font-weight: bold;">Section1</span>.
<span style="font-weight: bold;">Print</span> add.
</pre>
</div>
<pre class="example">
p is declared
add is defined
add is recursively defined (decreasing on 1st argument)
</pre>
<div class="org-src-container">
<pre class="src src-coq">Reset Section1.
</pre>
</div>
<pre class="example">
</pre>
<p>
Little reminder : the <code>PROGRAMME</code> defines instead some function <code>addn</code> such that <code>addn
n p</code> is <code>add p n</code> ( this latest <code>add</code> of <code>Section1</code> ) … Moreover the <code>PROGRAMME</code>
defines addition ( named <code>addn</code> , infix notation <code>+</code> ), predecessor ( <code>predn</code> ,
postfix notation <code>.-1</code> ), doubling ( <code>doublen</code> , postfix notation <code>.*2</code> ),
multiplication ( <code>muln</code> , infix notation <code>*</code> ), subtraction ( <code>subn</code> , infix notation
<code>-</code> ), division ( <code>divn</code> , infix notation <code>%/</code> ), modulo ( <code>modn</code> , infix notation
<code>%%</code> ), exponentiation ( <code>expn</code> , infix notation <code>^</code> ), equality comparison ( <code>eqn</code> ,
infix notation <code>==</code> ), and order comparison ( <code>leq</code> , infix notation <code><=</code> ) on
(natural) numbers.
</p>
<p>
Many of these functions may be defined by reusing the general function <code>iter</code>, which
is the iterator over any number in <code>nat</code> , or by reusing the more-general function
<code>foldr</code> , which is the iterator over any list in <code>seq</code> :
</p>
<div class="org-src-container">
<pre class="src src-coq"><span style="font-weight: bold;">Print</span> iter.
<span style="font-weight: bold;">Definition</span> <span style="font-weight: bold;">add</span> (<span style="font-weight: bold; font-style: italic;">p</span> : nat) : nat -> nat :=
<span style="font-weight: bold; text-decoration: underline;">fun</span> <span style="font-weight: bold; font-style: italic;">n</span> : nat =>
iter n (<span style="font-weight: bold; text-decoration: underline;">fun</span> <span style="font-weight: bold; font-style: italic;">acc</span> : nat => S (acc)) p .
<span style="font-weight: bold;">Print</span> foldr.
</pre>
</div>
<pre class="example">
iter =
fun (T : Type) (n : nat) (f : T -> T) (x : T) =>
let fix loop (m : nat) : T := match m with
| 0 => x
| i.+1 => f (loop i)
end in
loop n
: forall T : Type, nat -> (T -> T) -> T -> T
Argument T is implicit
Argument scopes are [type_scope nat_scope function_scope _]
add is defined
foldr =
fun (T R : Type) (f : T -> R -> R) (z0 : R) =>
fix foldr (s : seq T) : R :=
match s with
| [::] => z0
| x :: s' => f x (foldr s')
end
: forall T R : Type, (T -> R -> R) -> R -> seq T -> R
Arguments T, R are implicit and maximally inserted
Argument scopes are [type_scope type_scope function_scope _ seq_scope]
</pre>
<p>
Elsewhere, as expected because of the <i>some-accumulator-is-decreasing requirement</i>,
this attempt is not memorized by the <code>COQ</code> computer :
</p>
<div class="org-src-container">
<pre class="src src-coq">Fail
<span style="font-weight: bold;">Fixpoint</span> <span style="font-weight: bold;">nat_empty</span> (<span style="font-weight: bold; font-style: italic;">n</span> : nat) {<span style="font-weight: bold; text-decoration: underline;">struct</span> n}: <span style="font-weight: bold; text-decoration: underline;">False</span> :=
<span style="font-weight: bold; text-decoration: underline;">if</span> n <span style="font-weight: bold; text-decoration: underline;">is</span> S n' <span style="font-weight: bold; text-decoration: underline;">then</span> nat_empty n' <span style="font-weight: bold; text-decoration: underline;">else</span> nat_empty 0.
Fail <span style="font-weight: bold;">Check</span> nat_empty ( <span style="font-weight: bold; font-style: italic;">3</span> : nat ) <span style="font-weight: bold; font-style: italic;">(** </span><span style="font-weight: bold; font-style: italic;">: False *</span><span style="font-weight: bold; font-style: italic;">*)</span>.
</pre>
</div>
<pre class="example">
The command has indeed failed with message:
Recursive definition of nat_empty is ill-formed.
In environment
nat_empty : nat -> False
n : nat
Recursive call to nat_empty has principal argument equal to
"0" instead of a subterm of "n".
Recursive definition is:
"fun n : nat => match n with
| 0 => nat_empty 0
| n'.+1 => nat_empty n'
end".
The command has indeed failed with message:
The reference nat_empty was not found in the current environment.
</pre>
<p>
where <code>False</code> is the <span class="underline">empty class / nat</span>. Indeed <code>nat_empty : nat -> False</code> would say
that the class <code>nat</code> is <i>empty</i> when in reality the class <code>nat</code> does contain the data
element <code>3</code>.
</p>
</div>
</div>
</div>
<div id="outline-container-org61b34fe" class="outline-2">
<h2 id="org61b34fe"><span class="section-number-2">3</span> Parametrism : parametric data, parametric functions</h2>
<div class="outline-text-2" id="text-3">
</div><div id="outline-container-orgf587c97" class="outline-3">
<h3 id="orgf587c97"><span class="section-number-3">3.1</span> Option data</h3>
<div class="outline-text-3" id="text-3-1">
<p>
Now oneself wants some version of the data type <code>bool</code> which is <span class="underline">container</span> for
more information / data, which is that each alternative <code>true | false</code> may contain
more data which says <i>how</i> true (or how false).
</p>
<p>
Suppose oneself wants to write this partial function over only the <i>odd</i> numbers :
</p>
<div class="org-src-container">
<pre class="src src-coq"><span style="font-weight: bold;">Definition</span> <span style="font-weight: bold;">pred_for_only_odd</span> (<span style="font-weight: bold; font-style: italic;">n</span> : nat) := <span style="font-weight: bold; text-decoration: underline;">if</span> odd n <span style="font-weight: bold; text-decoration: underline;">then</span> Some (n.-1) <span style="font-weight: bold; text-decoration: underline;">else</span> None.
</pre>
</div>
<p>
or suppose oneself wants to write such partial function over only the <i>small</i>
numbers :
</p>
<div class="org-src-container">
<pre class="src src-coq"><span style="font-weight: bold;">Definition</span> <span style="font-weight: bold;">odd_for_only_small</span> (<span style="font-weight: bold; font-style: italic;">n</span> : nat) := <span style="font-weight: bold; text-decoration: underline;">if</span> n < 100 <span style="font-weight: bold; text-decoration: underline;">then</span> Some (odd n) <span style="font-weight: bold; text-decoration: underline;">else</span> None.
</pre>
</div>
<p>
Therefore one may define some <span class="underline">outer parametric /_polymorphic data type</span> as such :
</p>
<div class="org-src-container">
<pre class="src src-coq"><span style="font-weight: bold;">Inductive</span> <span style="font-weight: bold;">option</span> (<span style="font-weight: bold; font-style: italic;">A</span> : <span style="font-weight: bold; text-decoration: underline;">Type</span>) := None : option A | Some : A -> option A.
</pre>
</div>
<div class="org-src-container">
<pre class="src src-coq"><span style="font-weight: bold;">About</span> option.
<span style="font-weight: bold;">About</span> None.
<span style="font-weight: bold;">About</span> Some.
</pre>
</div>
<pre class="example">
option : Type -> Type
option is template universe polymorphic
Argument scope is [type_scope]
Expands to: Inductive Coq.Init.Datatypes.option
None : forall A : Type, option A
None is template universe polymorphic
Argument A is implicit and maximally inserted
Argument scope is [type_scope]
Expands to: Constructor Coq.Init.Datatypes.None
Some : forall A : Type, A -> option A
Some is template universe polymorphic
Arguments are renamed to A, a
Argument A is implicit and maximally inserted
Argument scopes are [type_scope _]
Expands to: Constructor Coq.Init.Datatypes.Some
</pre>
<p>
The <span class="underline">parameter</span> <code>A</code> says that some different type / class <code>(option A)</code> exists for each
possible choice of some type / class <code>A</code> , for example <code>(option nat)</code> , <code>(option
bool)</code> … And <code>Type</code> is some keytext / keyword that denotes <span class="underline">the class of all data
classes</span>, and <code>option</code> is some <span class="underline">class / type former</span> function, itself of class <code>(Type
-> Type)</code>. Indeed <code>option</code> alone is not some data-type, but if oneself instantiates it
with another data type, then it forms one. For example <code>(nat : Type)</code> and <code>(bool :
Type)</code> are of class <code>Type</code>, and may be used in place of <code>A</code> to produce the types
<code>((option nat) : Type)</code> and <code>((option bool) : Type)</code>.
</p>
<div class="org-src-container">
<pre class="src src-coq"><span style="font-weight: bold;">Check</span> pred_for_only_odd : nat -> option nat.
<span style="font-weight: bold;">Check</span> odd_for_only_small : nat -> option bool.
</pre>
</div>
<p>
Memo that all the constructors of this parametric / polymorphic data type definition, in reality,
have some type parameter, where as described in some section above, the <i>classifying
inference</i> keytext <code>forall .. , ..</code> is some more general form of the <i>inference</i>
keytext <code>.. -> ..</code> .
</p>
<p>
The message <code>Argument A is implicit and ...</code> says that every time programmers write
<code>Some</code> or <code>None</code> , the <code>COQ</code> computer automatically inserts / instantiates some term
in place of the parameter <code>A</code> , such that this term does not lack to be textually
written : the parameter is <span class="underline">hidden</span> / <span class="underline">implicit</span>. And the <code>COQ</code> computer <span class="underline">infers</span> or
guesses whatever-is this type parameter
</p>
<ul class="org-ul">
<li>when looking at the first explicit argument input given to <code>Some</code> , or</li>
<li>when looking at the context surrounding the ouput of <code>None</code> .</li>
</ul>
<div class="org-src-container">
<pre class="src src-coq"><span style="font-weight: bold;">Check</span> Some 2.
</pre>
</div>
<pre class="example">
Some 2
: option nat
</pre>
<div class="org-src-container">
<pre class="src src-coq"><span style="font-weight: bold;">Check</span> <span style="font-weight: bold; text-decoration: underline;">if</span> (37 + 73) < 100 <span style="font-weight: bold; text-decoration: underline;">then</span> Some (37+73) <span style="font-weight: bold; text-decoration: underline;">else</span> None.
<span style="font-weight: bold;">Eval</span> <span style="text-decoration: underline;">compute</span> <span style="font-weight: bold; text-decoration: underline;">in</span> <span style="font-weight: bold; text-decoration: underline;">if</span> (37 + 73) < 100 <span style="font-weight: bold; text-decoration: underline;">then</span> Some (odd (37 + 73)) <span style="font-weight: bold; text-decoration: underline;">else</span> None.
Fail <span style="font-weight: bold;">Check</span> <span style="font-weight: bold; text-decoration: underline;">if</span> (37 + 73) < 100 <span style="font-weight: bold; text-decoration: underline;">then</span> Some (odd (37 + 73)) <span style="font-weight: bold; text-decoration: underline;">else</span> (None (A := nat)).
Fail <span style="font-weight: bold;">Check</span> <span style="font-weight: bold; text-decoration: underline;">if</span> (37 + 73) < 100 <span style="font-weight: bold; text-decoration: underline;">then</span> Some (odd (37 + 73)) <span style="font-weight: bold; text-decoration: underline;">else</span> (@None nat).
<span style="font-weight: bold;">Eval</span> <span style="text-decoration: underline;">compute</span> <span style="font-weight: bold; text-decoration: underline;">in</span> <span style="font-weight: bold; text-decoration: underline;">if</span> (37 + 73) < 100 <span style="font-weight: bold; text-decoration: underline;">then</span> Some (odd (37 + 73)) <span style="font-weight: bold; text-decoration: underline;">else</span> @None _.
</pre>
</div>
<pre class="example">
if 37 + 73 < 100 then Some (37 + 73) else None
: option nat
= None
: option bool
The command has indeed failed with message:
The term "None" has type "option nat" while it is expected to have type