-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathNat.lp
1833 lines (1573 loc) · 46.8 KB
/
Nat.lp
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
/* Library on natural numbers
by Quentin Buzet (July 2022)
following https://github.com/math-comp/math-comp/blob/master/mathcomp/ssreflect/ssrnat.v
Documentation of ssrnat.v
-------------------------
The following operations and notations are provided:
successor and predecessor
n +1 and n ∸1
basic arithmetic
m + n, m - n, m * n
Important: m - n denotes TRUNCATED subtraction: m - n = 0 if m <= n.
exponentiation, factorial
m ^ n, n !
m ^ 1 is convertible to m, and m ^ 2 to m * m
comparison
m <= n, m < n, m >= n, m > n, m == n, m <= n <= p, etc.,
comparisons are BOOLEAN operators.
Most compatibility lemmas are stated as boolean equalities; this keeps
the size of the library down. All the inequalities refer to the same
constant "leq"; in particular m < n is identical to m.+1 <= n.
maximum and minimum
maxn m n, minn m n
Note that maxn m n = m + (n - m), due to the truncating subtraction.
Not available
-------------
doubling, halving, and parity
n.*2, n./2, odd n, uphalf n, with uphalf n = n.+1./2
bool coerces to nat so we can write, e.g., n = odd n + n./2.*2.
iteration
iter n f x0 == f ( .. (f x0))
iteri n g x0 == g n.∸1 (g ... (g 0 x0))
iterop n op x x0 == op x (... op x x) (n x's) or x0 if n = 0
conditionally strict inequality `leqif'
m <= n ?= iff condition == (m <= n) and ((m == n) = condition)
This is actually a pair of boolean equalities, so rewriting with an
`leqif' lemma can affect several kinds of comparison. The transitivity
lemma for leqif aggregates the conditions, allowing for arguments of
the form ``m <= n <= p <= m, so equality holds throughout''.
countable choice
ex_minn : forall P : pred nat, (exists n, P n) -> nat
This returns the smallest n such that P n holds.
ex_maxn : forall (P : pred nat) m,
(exists n, P n) -> (forall n, P n -> n <= m) -> nat
This returns the largest n such that P n holds (given an explicit upper
bound).
Naming convention
-----------------
This file adds the following suffix conventions to those documented in
ssrbool.v and eqtype.v:
A (infix) -- conjunction, as in
ltn_neqAle : (m < n) = (m != n) && (m <= n).
B -- subtraction, as in subBn : (m - n) - p = m - (n + p).
D -- addition, as in mulnDl : (m + n) * p = m * p + n * p.
M -- multiplication, as in expnMn : (m * n) ^ p = m ^ p * n ^ p.
p (prefix) -- positive, as in
eqn_pmul2l : m > 0 -> (m * n1 == m * n2) = (n1 == n2).
P -- greater than 1, as in
ltn_Pmull : 1 < n -> 0 < m -> m < n * m.
S -- successor, as in addSn : n +1 + m = (n + m) +1.
V (infix) -- disjunction, as in
leq_eqVlt : (m <= n) = (m == n) || (m < n).
X - exponentiation, as in lognX : logn p (m ^ n) = logn p m * n in
file prime.v (the suffix is not used in this file).
Suffixes that abbreviate operations (D, B, M and X) are used to abbreviate
second-rank operations in equational lemma names that describe left-hand
sides (e.g., mulnDl); they are not used to abbreviate the main operation
of relational lemmas (e.g., leq_add2l).
For the asymmetrical exponentiation operator expn (m ^ n) a right suffix
indicates an operation on the exponent, e.g., expnM : m ^ (n1 * n2) = ...;
a trailing "n" is used to indicate the left operand, e.g.,
expnMn : (m1 * m2) ^ n = ... The operands of other operators are selected
using the l/r suffixes.
*/
require open Stdlib.Set Stdlib.Prop Stdlib.FOL Stdlib.Eq Stdlib.Bool;
inductive ℕ : TYPE ≔
| _0 : ℕ
| +1 : ℕ → ℕ; notation +1 postfix 100;
// set code for ℕ
constant symbol nat : Set;
rule τ nat ↪ ℕ;
// non confusion of constructors
symbol is0 : ℕ → 𝔹;
rule is0 _0 ↪ true
with is0 (_ +1) ↪ false;
opaque symbol s≠0 [n] : π (n +1 ≠ _0) ≔
begin
assume n h; refine ind_eq h (λ n, istrue(is0 n)) ⊤ᵢ
end;
opaque symbol 0≠s [n] : π (_0 ≠ n +1) ≔
begin
assume n h; apply @s≠0 n; symmetry; apply h
end;
opaque symbol casen n : π(n = _0 ∨ n ≠ _0) ≔
begin
induction
{ apply ∨ᵢ₁; reflexivity }
{ assume n h; apply ∨ᵢ₂; refine s≠0 }
end;
// predecessor
symbol ∸1 : ℕ → ℕ; notation ∸1 postfix 100;
rule _0 ∸1 ↪ _0
with ($x +1) ∸1 ↪ $x;
opaque symbol +1_inj [x y] : π (x +1 = y +1) → π (x = y) ≔
begin
assume x y h; apply feq (∸1) h
end;
// boolean equality on ℕ
symbol eqn : ℕ → ℕ → 𝔹;
rule eqn _0 _0 ↪ true
with eqn ($x +1) ($y +1) ↪ eqn $x $y
with eqn _0 (_ +1) ↪ false
with eqn (_ +1) _0 ↪ false;
opaque symbol eqn_correct x y : π(istrue(eqn x y)) → π(x = y) ≔
begin
induction
{ induction { reflexivity } { assume x h i; apply ⊥ₑ i } }
{ assume x h; induction
{ assume i; apply ⊥ₑ i }
{ assume y i j; apply feq (+1); apply h _ j }
}
end;
opaque symbol eqn_complete x y : π(x = y) → π(istrue(eqn x y)) ≔
begin
induction
{ assume y i; rewrite left i; apply ⊤ᵢ }
{ simplify; assume x h; induction
{ assume i; apply s≠0 i }
{ assume y i j; simplify; refine h y _; apply +1_inj; refine j }
}
end;
// addition
symbol + : ℕ → ℕ → ℕ; notation + infix left 20;
rule _0 + $y ↪ $y
with $x +1 + $y ↪ ($x + $y) +1;
opaque symbol add0n x : π (_0 + x = x) ≔
begin
assume x; reflexivity;
end;
opaque symbol addn0 x : π (x + _0 = x) ≔
begin
induction
{ reflexivity }
{ assume x' h; simplify; rewrite h; reflexivity }
end;
rule $x + _0 ↪ $x;
opaque symbol addSn x y : π (x +1 + y = (x + y) +1) ≔
begin
assume x; reflexivity;
end;
opaque symbol addnS x y : π (x + y +1 = (x + y) +1) ≔
begin
induction
{ reflexivity }
{ assume x' h y; simplify; rewrite h; reflexivity }
end;
rule $x + $y +1 ↪ ($x + $y) +1;
opaque symbol add1n n : π ((_0 +1) + n = n +1) ≔
begin
assume n; reflexivity;
end;
opaque symbol addn1 n : π (n + (_0 +1) = n +1) ≔
begin
assume n; reflexivity;
end;
opaque symbol addSnnS m n : π (m +1 + n = m + n +1) ≔
begin
assume m n; reflexivity;
end;
opaque symbol addnC x y : π (x + y = y + x) ≔
begin
induction
{ reflexivity }
{ assume x' h y; simplify; rewrite h; reflexivity }
end;
opaque symbol addnA x y z : π ((x + y) + z = x + (y + z)) ≔
begin
induction
{ reflexivity }
{ assume x' h y z; simplify; rewrite h; reflexivity }
end;
rule ($x + $y) + $z ↪ $x + ($y + $z);
opaque symbol addnCA m n p : π ((m + n) + p = (m + p) + n) ≔
begin
assume m n p; symmetry; rewrite addnA; rewrite .[p + n] addnC;
rewrite left addnA; reflexivity;
end;
opaque symbol addnAC m n p : π (m + (n + p) = n + (m + p)) ≔
begin
assume m n p; symmetry; rewrite left addnA; rewrite .[n + m] addnC;
rewrite addnA; reflexivity;
end;
opaque symbol addnCAC m n p : π (m + n + p = p + n + m) ≔
begin
assume m n p; rewrite addnC; rewrite .[m + _] addnC; rewrite addnA;
reflexivity;
end;
opaque symbol addnACl m n p : π (m + n + p = n + (p + m)) ≔
begin
assume m n p; rewrite .[m + _] addnC; rewrite addnA; rewrite .[m + _] addnC;
reflexivity;
end;
opaque symbol addnACA m n p q : π ((m + n) + (p + q) = (m + p) + (n + q)) ≔
begin
assume m n p q; simplify; rewrite left .[p + (n + q)] addnA;
rewrite .[p + n] addnC; rewrite .[(n + p) + q] addnA; reflexivity;
end;
opaque symbol addnI x y z : π (z + x = z + y) → π (x = y) ≔
begin
assume x y; induction
{ assume h; apply h;}
{ assume z h i; apply h; apply +1_inj; apply i;}
end;
opaque symbol addIn x y z : π (x + z = y + z) → π (x = y) ≔
begin
assume x y; induction
{ assume h; apply h;}
{ assume z h i; apply h; apply +1_inj; apply i;}
end;
opaque symbol addn_eq0 m n : π (m + n = _0 ⇔ m = _0 ∧ n = _0) ≔
begin
assume m n; apply ∧ᵢ {
generalize m; induction
{ assume m h; apply ∧ᵢ (eq_refl _0) h }
{ assume m h n i; apply ⊥ₑ (s≠0 i) }
} {
generalize m; induction
{ assume n h; apply ∧ₑ₂ h }
{ assume m h n i; apply ⊥ₑ (s≠0 (∧ₑ₁ i)) }
}
end;
opaque symbol eqn_add2l p m n : π ((p + m = p + n) ⇔ (m = n)) ≔
begin
assume p m n;
apply ∧ᵢ {
refine addnI m n p
} {
generalize p; induction
{ assume m n h; apply h }
{ assume z h m n i; simplify; rewrite h m n i; reflexivity }
};
end;
opaque symbol eqn_add2r p m n : π ((m + p = n + p) ⇔ (m = n)) ≔
begin
assume p m n; rewrite addnC m p; rewrite addnC n p; apply eqn_add2l p m n;
end;
opaque symbol 2*=0 n : π(n + n = _0) → π(n = _0) ≔
begin
assume n h; apply ∧ₑ₁ (∧ₑ₁ (addn_eq0 n n) h)
end;
opaque symbol 2*_inj x y : π(x + x = y + y) → π(x = y) ≔
begin
induction
{ assume y h; symmetry; apply 2*=0; symmetry; apply h }
{ assume x h; induction
{ assume i; apply ⊥ₑ (s≠0 i) }
{ assume y i j; apply feq (+1); apply h; apply +1_inj; apply +1_inj j }
}
end;
opaque symbol odd≠even p q : π((p + p) +1 ≠ q + q) ≔
begin
induction
{ induction
{ refine s≠0 }
{ assume x h; simplify; assume i; apply 0≠s (+1_inj i) }
}
{ assume x h; induction
{ refine s≠0 }
{ assume y i j; apply h y; apply +1_inj; apply +1_inj; apply j }
}
end;
// substraction
symbol - : ℕ → ℕ → ℕ; notation - infix left 20;
rule _0 - _ ↪ _0
with $x - _0 ↪ $x
with $x +1 - $y +1 ↪ $x - $y;
opaque symbol sub0n n : π (n - _0 = n) ≔
begin
reflexivity;
end;
opaque symbol subn0 n : π (_0 - n = _0) ≔
begin
reflexivity;
end;
opaque symbol subnn x : π (x - x = _0) ≔
begin
induction
{ reflexivity }
{ assume x h; simplify; apply h;}
end;
opaque symbol subSS m n : π (m +1 - (n +1) = m - n) ≔
begin
reflexivity;
end;
opaque symbol subn1 n : π (n - (_0 +1) = n ∸1) ≔
begin
induction
{ reflexivity }
{ assume n h; reflexivity; }
end;
opaque symbol subnS x y : π (x - (y +1) = (x - y) ∸1) ≔
begin
induction
{ reflexivity }
{ assume x h; induction
{ reflexivity }
{ assume y i; simplify; rewrite h; reflexivity }
}
end;
opaque symbol subSnn n : π (n +1 - n = (_0 +1)) ≔
begin
induction
{ reflexivity }
{ assume n h; simplify; apply h }
end;
opaque symbol predn_sub x y : π ((x - y) ∸1 = x ∸1 - y) ≔
begin
induction
{ reflexivity }
{ assume x h; induction
{ reflexivity }
{ assume y i; simplify; symmetry; rewrite subnS; reflexivity }
}
end;
opaque symbol subnAC z x y : π ((x - y) - z = (x - z) - y) ≔
begin
induction
{ reflexivity }
{ assume z h; induction
{ reflexivity }
{ assume x i; induction
{ reflexivity }
{ assume y j; simplify; rewrite i; rewrite subnS; symmetry;
rewrite subnS; rewrite predn_sub; reflexivity}
}
}
end;
opaque symbol addnK x y : π ((x + y) - y = x) ≔
begin
induction
{ assume x; simplify; rewrite subnn; reflexivity }
{ assume x h; induction
{ reflexivity }
{ simplify; assume y i; rewrite i; reflexivity }
}
end;
opaque symbol subnDA x y z : π (x - (y + z) = (x - y) - z) ≔
begin
induction
{ reflexivity }
{ assume x h; induction
{ reflexivity }
{ assume y i; induction
{ reflexivity }
{ assume z j; simplify; rewrite subnS; symmetry; rewrite subnS;
rewrite h; reflexivity }
}
}
end;
opaque symbol subnDl z x y : π ((z + x) - (z + y) = x - y) ≔
begin
induction
{ assume x y; simplify; reflexivity }
{ assume z h; induction
{ assume y; simplify; rewrite subnDA; rewrite subnn; reflexivity }
{ assume x i; induction
{ rewrite addnC; rewrite addn0; rewrite addnK; reflexivity }
{ assume y j; rewrite addnC; rewrite subnDA; rewrite addnK; reflexivity }
}
}
end;
opaque symbol subnDr z x y : π ((x + z) - (y + z) = x - y) ≔
begin
assume z x y; rewrite addnC; rewrite .[y + _] addnC; rewrite subnDl;
reflexivity;
end;
opaque symbol subSKn m n : π ((m +1 - n) ∸1 = m - n) ≔
begin
assume m n; rewrite left subnS; reflexivity;
end;
// multiplication
symbol * : ℕ → ℕ → ℕ; notation * infix left 30; // \times
assert x y z ⊢ x + y * z ≡ x + (y * z);
rule _0 * _ ↪ _0
with ($x +1) * $y ↪ $y + $x * $y;
opaque symbol mul0n x : π (x * _0 = _0) ≔
begin
induction { reflexivity } { assume x' h; apply h }
end;
rule _ * _0 ↪ _0;
opaque symbol muln0 x : π (_0 * x = _0) ≔
begin
assume x; reflexivity;
end;
opaque symbol mul1n n : π ((_0 +1) * n = n) ≔
begin
assume n; reflexivity;
end;
opaque symbol muln1 n : π (n * (_0 +1) = n) ≔
begin
induction
{ reflexivity }
{ assume n h; simplify; rewrite h; reflexivity }
end;
opaque symbol mulSn m n : π ((m +1) * n = n + m * n) ≔
begin
reflexivity;
end;
opaque symbol mulSnr m n : π ((m +1) * n = m * n + n) ≔
begin
assume m n; rewrite addnC; reflexivity;
end;
opaque symbol mulnS x y : π (x * (y +1) = x + x * y) ≔
begin
induction
{ reflexivity }
{ assume x' h y; simplify; rewrite h; rewrite addnAC; reflexivity }
end;
//rule $x * ($y +1) ↪ $x + $x * $y; is confluent modulo C only
opaque symbol mulnSr x y : π (x * (y +1) = x * y + x) ≔
begin
induction
{ reflexivity }
{ assume x' h y; simplify; rewrite mulnS; rewrite .[x' + _] addnC;
reflexivity }
end;
//rule $x * s $y ↪ $x * $y + $x; is confluent modulo C only
opaque symbol mulnC x y : π (x * y = y * x) ≔
begin
induction
{ reflexivity }
{ assume x' h y; simplify; rewrite mulnS; rewrite h; reflexivity }
end;
opaque symbol mulnDl x y z : π ((x + y) * z = x * z + y * z) ≔
begin
induction
{ reflexivity }
{ assume x' h y z; simplify; rewrite h; reflexivity }
end;
//rule ($x + $y) * $z ↪ $x * $z + $y * $z; is confluent modulo AC only
opaque symbol mulnDr x y z : π (z * (x + y) = z * x + z * y) ≔
begin
assume x y z; rewrite mulnC; rewrite mulnDl; rewrite mulnC;
rewrite .[y * _] mulnC; reflexivity
end;
//rule $z * ($x + $y) ↪ $z * $x + $z * $y; is confluent modulo AC only
opaque symbol mulnBr x y z : π (x * (y - z) = x * y - x * z) ≔
begin
induction
{ reflexivity }
{ assume x h; induction
{ reflexivity }
{ assume y i; induction
{ reflexivity }
{ assume z j; simplify; rewrite mulnS; rewrite mulnS;
rewrite left addnAC; rewrite .[z + (x + _)] addnAC;
rewrite subnDl; rewrite left mulSn; rewrite left mulSn;
rewrite left mulSn; rewrite left i; reflexivity }
}
}
end;
opaque symbol mulnBl x y z : π ((x - y) * z = x * z - y * z) ≔
begin
assume x y z; rewrite mulnC; rewrite .[x * z] mulnC; rewrite .[y * z] mulnC;
rewrite left mulnBr; reflexivity;
end;
opaque symbol mulnA x y z : π ((x * y) * z = x * (y * z)) ≔
begin
induction
{ reflexivity }
{ assume x' h y z; simplify; rewrite mulnDl; rewrite h; reflexivity }
end;
//rule ($x * $y) * $z ↪ $x * ($y * $z); is not confluent
opaque symbol mulnCA x y z: π (x * (y * z) = y * (x * z)) ≔
begin
assume x y z; rewrite left mulnA; rewrite .[x * y] mulnC; rewrite mulnA;
reflexivity;
end;
opaque symbol mulnAC x y z: π ((x * y) * z = (x * z) * y) ≔
begin
assume x y z; rewrite mulnA; rewrite .[y * z] mulnC; rewrite left mulnA;
reflexivity;
end;
opaque symbol mulnACA x y z t: π ((x * y) * (z * t) = (x * z) * (y * t)) ≔
begin
assume x y z t; rewrite mulnA; rewrite .[y * _] mulnCA; rewrite mulnA;
reflexivity;
end;
opaque symbol muln_eq0 m n : π (m * n = _0 ⇔ m = _0 ∨ n = _0) ≔
begin
assume m n; apply ∧ᵢ {
generalize m; induction
{ assume n h; apply ∨ᵢ₁ h }
{ assume m h n i;
have t: π (n = _0 ∧ (m * n) = _0) { apply ∧ₑ₁ (addn_eq0 n (m * n)) i };
have u: π (n = _0) { apply ∧ₑ₁ t };
apply ∨ᵢ₂ u;
}
} {
assume h;
refine ∨ₑ h _ _ {
assume i; rewrite i; reflexivity;
} {
assume i; rewrite i; reflexivity;
};
};
end;
// order on ℕ
symbol ≤ : ℕ → ℕ → 𝔹; notation ≤ infix left 10;
rule _0 ≤ _ ↪ true
with _ +1 ≤ _0 ↪ false
with $x +1 ≤ $y +1 ↪ $x ≤ $y;
symbol < : ℕ → ℕ → 𝔹; notation < infix 10;
rule $x < $y ↪ $x +1 ≤ $y;
symbol ≥ : ℕ → ℕ → 𝔹; notation ≥ infix 10;
rule $x ≥ $y ↪ $y ≤ $x;
symbol > : ℕ → ℕ → 𝔹; notation > infix 10;
rule $x > $y ↪ $y +1 ≤ $x;
opaque symbol ≤0 x : π (istrue (x ≤ _0)) → π (x = _0) ≔
begin
induction
{ assume h; reflexivity;}
{ assume x h i; apply ⊥ₑ; apply i }
end;
opaque symbol ≤_refl x : π (istrue (x ≤ x)) ≔
begin
induction
{ simplify; apply ⊤ᵢ;}
{ assume x h; simplify; apply h }
end;
opaque symbol eq_leq x y : π (x = y) → π (istrue (x ≤ y)) ≔
begin
assume x y h; rewrite h; apply ≤_refl y;
end;
opaque symbol leq_trans [x y z] :
π (istrue (x ≤ y)) → π (istrue (y ≤ z)) → π (istrue (x ≤ z)) ≔
begin
induction
{ assume y z h i; apply h }
{ assume x h; induction
{ assume y i; apply ⊥ₑ i }
{ assume y i; induction
{ assume j k; apply k }
{ assume z j k l; apply h y z k l }
}
}
end;
opaque symbol eqn_leq x y : π (istrue (x ≤ y) ∧ istrue (y ≤ x) ⇔ (x = y)) ≔
begin
assume x y; apply ∧ᵢ {
generalize x; induction
{ assume y g;
have i: π (istrue (y ≤ _0)) { apply ∧ₑ₂ g };
symmetry; apply ≤0 y i;
}
{
assume x h; induction
{ assume g;
have i: π (istrue (x +1 ≤ _0)) { apply ∧ₑ₁ g };
apply ≤0 (x +1) i;
}
{ assume y i j; apply feq (+1); apply h y j;}
}
} {
assume h;
have t: π (istrue (x ≤ y)) { apply eq_leq _ _ h;};
have u: π (istrue (y ≤ x)) { apply eq_leq y x _; symmetry; apply h;};
apply ∧ᵢ t u;
};
end;
opaque symbol leqsnn n: π (istrue (n +1 ≤ n)) → π ⊥ ≔
begin
induction
{ assume h; apply h }
{ assume n h i; apply h; apply i;}
end;
opaque symbol letnS m n : π (istrue (m < n +1)) → π (istrue (m ≤ n)) ≔
begin
assume m n h; apply h;
end;
opaque symbol ltn0 n : π (istrue (n < _0)) → π ⊥ ≔
begin
assume n h; apply h;
end;
opaque symbol ltnn n : π (istrue (n < n)) → π ⊥ ≔
begin
assume n h; apply leqsnn n; apply h;
end;
opaque symbol ltnSn n : π (istrue (n < n +1)) ≔
begin
assume n; simplify; apply ≤_refl n;
end;
opaque symbol leq0n n : π (istrue (_0 ≤ n)) ≔
begin
induction
{ apply ≤_refl _0;}
{ assume n h; simplify; apply h }
end;
opaque symbol ltn0Sn n : π (istrue (_0 < n +1)) ≔
begin
assume n; apply leq0n n;
end;
opaque symbol leqnSn n : π (istrue (n ≤ n +1)) ≔
begin
induction
{ apply leq0n (_0 +1) }
{ assume n h; simplify; apply h }
end;
opaque symbol leq_pred n : π (istrue (n ∸1 ≤ n)) ≔
begin
induction
{ apply ⊤ᵢ;}
{ assume n h; simplify; apply leqnSn n;}
end;
opaque symbol ltnW m n : π (istrue (m < n)) → π (istrue (m ≤ n)) ≔
begin
induction
{ assume m h; apply ⊤ᵢ;}
{ assume m h; induction
{ assume i; apply i;}
{ assume n i j; apply h n; apply j;}
}
end;
opaque symbol leqW m n : π (istrue (m ≤ n)) → π (istrue (m ≤ n +1)) ≔
begin
assume m n h; apply ltnW m (n +1); simplify; apply h;
end;
opaque symbol ltn_trans x y z : π (istrue (x < y)) → π (istrue (y < z)) → π (istrue (x < z)) ≔
begin
assume x y z h i;
have v:π (istrue (x +1 ≤ y +1)) { apply leqW (x +1) y h };
apply @leq_trans (x +1) (y +1) z v i;
end;
opaque symbol <_asym x y : π (istrue (x < y)) → π (¬ (istrue (y < x))) ≔
begin
assume x y h i;
have t:π (istrue (y ≤ y +1)) { apply leqnSn y };
have u:π (istrue (x +1 ≤ y +1)) { apply @leq_trans (x +1) y (y +1) h t };
have v:π (istrue (x +1 ≤ x)) { apply @leq_trans (x +1) (y +1) x u i };
apply leqsnn x; apply v;
end;
opaque symbol anti_ltn x y : π(istrue (x < y)) → π(istrue (y < x)) → π(x = y) ≔
begin
assume x y h i;
have c:π (istrue (x ≤ x +1)) { apply leqnSn x };
have d:π (istrue (y ≤ y +1)) { apply leqnSn y };
have e:π (istrue (x ≤ y)) { apply @leq_trans x (x +1) y c h };
have f:π (istrue (y ≤ x)) { apply @leq_trans y (y +1) x d i };
have g:π (istrue (x ≤ y) ∧ istrue (y ≤ x)) { apply ∧ᵢ e f };
apply ∧ₑ₁ (eqn_leq x y) g;
end;
opaque symbol leq_total x y : π (istrue (x ≤ y) ∨ istrue (y ≤ x)) ≔
begin
induction
{ assume y; simplify; apply ∨ᵢ₁; apply ⊤ᵢ }
{ assume x h; induction
{ simplify; apply ∨ᵢ₂; apply ⊤ᵢ }
{ assume y i; simplify; apply h y }
}
end;
opaque symbol lt0n n : π (istrue (n > _0) ⇔ (n ≠ _0)) ≔
begin
assume n; apply ∧ᵢ {
generalize n; induction
{ assume h i; apply h }
{ assume n h i j; apply s≠0 j;}
} {
generalize n; induction
{ assume h; apply h (eq_refl _0) }
{ assume n h i; apply ⊤ᵢ }
};
end;
opaque symbol leq_eqVlt m n : π (istrue (m ≤ n) ⇔ (m = n) ∨ istrue (m < n)) ≔
begin
assume m n; apply ∧ᵢ {
generalize m; induction
{ induction
{ assume h; apply ∨ᵢ₁ (eq_refl _0) }
{ assume n h i; apply ∨ᵢ₂ ⊤ᵢ }
}
{ assume m h; induction
{ assume i; apply ∨ᵢ₂ i }
{ assume n i j; apply ∨ₑ (h n j)
{ assume e; apply ∨ᵢ₁; rewrite e; reflexivity }
{ assume e; apply ∨ᵢ₂; apply e }
}
}
} {
assume h; apply ∨ₑ h {
assume i; apply eq_leq m n i
} {
assume i; apply ltnW m n i
}
}
end;
opaque symbol ltn_neqAle m n : π (istrue (m < n) ⇔ istrue (m ≤ n) ∧ (m ≠ n)) ≔
begin
abort;
opaque symbol leq_add0 m n :
π (istrue (_0 ≤ m)) → π (istrue (_0 ≤ n)) → π (istrue (_0 ≤ m + n)) ≔
begin
assume m n h i; apply leq0n (m + n);
end;
opaque symbol leq_add2l p m n : π (istrue (p + m ≤ p + n) ⇔ istrue (m ≤ n)) ≔
begin
assume p m n; apply ∧ᵢ {
generalize p; induction
{ assume m n h; apply h }
{ assume p h m n i; apply h m n i }
} {
generalize p; induction
{ assume m n h; apply h }
{ assume p h m n i; apply h m n i }
};
end;
opaque symbol ltn_add2l p m n : π (istrue (p + m < p + n) ⇔ istrue (m < n)) ≔
begin
assume p m n; simplify; rewrite left addnS; refine leq_add2l p (m +1) n;
end;
opaque symbol leq_add2r p m n : π (istrue (m + p ≤ n + p) ⇔ istrue (m ≤ n)) ≔
begin
assume p m n; rewrite addnC m p; rewrite addnC n p; refine leq_add2l p m n;
end;
opaque symbol ltn_add2r p m n : π (istrue (m + p < n + p) ⇔ istrue (m < n)) ≔
begin
assume p m n; rewrite addnC m p; rewrite addnC n p; refine ltn_add2l p m n;
end;
opaque symbol leq_addl m n : π (istrue (n ≤ m + n)) ≔
begin
assume m; induction
{ apply ⊤ᵢ }
{ assume n h; apply h }
end;
opaque symbol leq_addr m n : π (istrue (n ≤ n + m)) ≔
begin
assume m n; rewrite addnC n m; apply leq_addl m n;
end;
opaque symbol leq_subr m n : π (istrue (n - m ≤ n)) ≔
begin
induction
{ assume n; apply ≤_refl n }
{ assume m h; induction
{ apply ⊤ᵢ }
{ assume n i; simplify;
have t: π (istrue (n ≤ n +1)) { apply leqnSn n };
apply @leq_trans (n - m) n (n +1) (h n) t;
}
}
end;
opaque symbol subn_eq0 m n : π ((m - n = _0) ⇔ istrue (m ≤ n)) ≔
begin
assume m n; apply ∧ᵢ {
generalize m; induction
{ assume n h; apply ⊤ᵢ }
{ assume m h; induction
{ assume i; apply s≠0 i }
{ assume n i j; apply h n j }
}
} {
generalize m; induction
{ assume n h; apply eq_refl _0 }
{ assume m h; induction
{ assume i; apply ⊥ₑ i }
{ assume n i j; apply h n j }
}
};
end;
opaque symbol ltn_addl m n p : π (istrue (m < n)) → π (istrue (m < p + n)) ≔
begin
assume m n; induction
{ assume h; apply h }
{ assume p h i; refine @leq_trans m (m +1) (p + n) (leqnSn m) (h i) }
end;
opaque symbol ltn_addr m n p : π (istrue (m < n)) → π (istrue (m < n + p)) ≔
begin
assume m n p h; rewrite addnC n p; apply ltn_addl m n p; apply h;
end;
opaque symbol addn_gt0 m n :
π (istrue (_0 < m + n) ⇔ istrue (_0 < m) ∨ istrue (_0 < n)) ≔
begin
assume m n; apply ∧ᵢ {
generalize m; induction
{ assume n i; apply ∨ᵢ₂ i }
{ assume m h n i; apply ∨ᵢ₁; apply ltn0Sn m }
} {
assume h; apply ∨ₑ h {
assume i; apply ltn_addr _0 m n i;
} {
assume i; apply ltn_addl _0 n m i;
};
};
end;
opaque symbol subn_gt0 m n : π (istrue (_0 < n - m) ⇔ istrue (m < n)) ≔
begin
assume m n; apply ∧ᵢ {
generalize m; induction
{ assume n h; apply h }
{ assume m h; induction
{ assume i; apply i }
{ assume n i; rewrite subSS; assume j;
have t: π (istrue (m < n)) { apply h n j };
have u: π (istrue (m +1 < n +1))
{ refine ∧ₑ₂ (ltn_add2r (_0 +1) m n) t };
rewrite left addn1 m; rewrite left addn1 n; apply u;
}
}
} {
generalize m; induction
{ assume n h; apply h }
{ assume m h; induction
{ assume i; apply i }
{ assume n i j; rewrite subSS; apply h n;
refine ∧ₑ₁ (ltn_add2r (_0 +1) m n) j;
}
}
};
end;
opaque symbol leq_add m1 m2 n1 n2 :
π (istrue (m1 ≤ n1)) → π (istrue (m2 ≤ n2)) → π (istrue (m1 + m2 ≤ n1 + n2)) ≔
begin
assume m1 m2 n1 n2 h i;
have a: π (istrue (m1 + m2 ≤ m1 + n2)) { apply ∧ₑ₂ (leq_add2l m1 m2 n2) i };
have b: π (istrue (m1 + n2 ≤ n1 + n2)) { refine ∧ₑ₂ (leq_add2r n2 m1 n1) h };
refine @leq_trans (m1 + m2) (m1 + n2) (n1 + n2) a b;
end;
opaque symbol leq_subLR m n p : π (istrue (m - n ≤ p) ⇔ istrue (m ≤ n + p)) ≔
begin
assume m n p; apply ∧ᵢ
{ assume h;
have t: π (((m - (n + p)) = _0)) → π (istrue (m ≤ (n + p)))
{ apply ∧ₑ₁ (subn_eq0 m (n + p)) };
apply t; rewrite subnDA; apply ∧ₑ₂ (subn_eq0 (m - n) p) h;
}
{ assume h;
have t: π (((m - n) - p) = _0) → π (istrue ((m - n) ≤ p))
{ apply ∧ₑ₁ (subn_eq0 (m - n) p) };
apply t; rewrite left subnDA; apply ∧ₑ₂ (subn_eq0 m (n + p)) h;
};
end;
opaque symbol subnKC m n : π (istrue (m ≤ n)) → π (m + (n - m) = n) ≔
begin
induction
{ assume n h; apply eq_refl n }
{ assume m h; induction