/
Prelude.lean
2477 lines (1879 loc) · 83.8 KB
/
Prelude.lean
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
/-
Copyright (c) 2020 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
prelude
universe u v w
@[inline] def id {α : Sort u} (a : α) : α := a
@[inline] def Function.comp {α : Sort u} {β : Sort v} {δ : Sort w} (f : β → δ) (g : α → β) : α → δ :=
fun x => f (g x)
@[inline] def Function.const {α : Sort u} (β : Sort v) (a : α) : β → α :=
fun _ => a
set_option checkBinderAnnotations false in
@[reducible] def inferInstance {α : Sort u} [i : α] : α := i
set_option checkBinderAnnotations false in
@[reducible] def inferInstanceAs (α : Sort u) [i : α] : α := i
set_option bootstrap.inductiveCheckResultingUniverse false in
inductive PUnit : Sort u where
| unit : PUnit
/-- An abbreviation for `PUnit.{0}`, its most common instantiation.
This Type should be preferred over `PUnit` where possible to avoid
unnecessary universe parameters. -/
abbrev Unit : Type := PUnit
@[matchPattern] abbrev Unit.unit : Unit := PUnit.unit
/-- Auxiliary unsafe constant used by the Compiler when erasing proofs from code. -/
unsafe axiom lcProof {α : Prop} : α
/-- Auxiliary unsafe constant used by the Compiler to mark unreachable code. -/
unsafe axiom lcUnreachable {α : Sort u} : α
inductive True : Prop where
| intro : True
inductive False : Prop
inductive Empty : Type
set_option bootstrap.inductiveCheckResultingUniverse false in
inductive PEmpty : Sort u where
def Not (a : Prop) : Prop := a → False
@[macroInline] def False.elim {C : Sort u} (h : False) : C :=
False.rec (fun _ => C) h
@[macroInline] def absurd {a : Prop} {b : Sort v} (h₁ : a) (h₂ : Not a) : b :=
False.elim (h₂ h₁)
inductive Eq : α → α → Prop where
| refl (a : α) : Eq a a
@[simp] abbrev Eq.ndrec.{u1, u2} {α : Sort u2} {a : α} {motive : α → Sort u1} (m : motive a) {b : α} (h : Eq a b) : motive b :=
Eq.rec (motive := fun α _ => motive α) m h
@[matchPattern] def rfl {α : Sort u} {a : α} : Eq a a := Eq.refl a
@[simp] theorem id_eq (a : α) : Eq (id a) a := rfl
theorem Eq.subst {α : Sort u} {motive : α → Prop} {a b : α} (h₁ : Eq a b) (h₂ : motive a) : motive b :=
Eq.ndrec h₂ h₁
theorem Eq.symm {α : Sort u} {a b : α} (h : Eq a b) : Eq b a :=
h ▸ rfl
theorem Eq.trans {α : Sort u} {a b c : α} (h₁ : Eq a b) (h₂ : Eq b c) : Eq a c :=
h₂ ▸ h₁
@[macroInline] def cast {α β : Sort u} (h : Eq α β) (a : α) : β :=
Eq.rec (motive := fun α _ => α) a h
theorem congrArg {α : Sort u} {β : Sort v} {a₁ a₂ : α} (f : α → β) (h : Eq a₁ a₂) : Eq (f a₁) (f a₂) :=
h ▸ rfl
theorem congr {α : Sort u} {β : Sort v} {f₁ f₂ : α → β} {a₁ a₂ : α} (h₁ : Eq f₁ f₂) (h₂ : Eq a₁ a₂) : Eq (f₁ a₁) (f₂ a₂) :=
h₁ ▸ h₂ ▸ rfl
theorem congrFun {α : Sort u} {β : α → Sort v} {f g : (x : α) → β x} (h : Eq f g) (a : α) : Eq (f a) (g a) :=
h ▸ rfl
/-!
Initialize the Quotient Module, which effectively adds the following definitions:
```
opaque Quot {α : Sort u} (r : α → α → Prop) : Sort u
opaque Quot.mk {α : Sort u} (r : α → α → Prop) (a : α) : Quot r
opaque Quot.lift {α : Sort u} {r : α → α → Prop} {β : Sort v} (f : α → β) :
(∀ a b : α, r a b → Eq (f a) (f b)) → Quot r → β
opaque Quot.ind {α : Sort u} {r : α → α → Prop} {β : Quot r → Prop} :
(∀ a : α, β (Quot.mk r a)) → ∀ q : Quot r, β q
```
-/
init_quot
inductive HEq : {α : Sort u} → α → {β : Sort u} → β → Prop where
| refl (a : α) : HEq a a
@[matchPattern] protected def HEq.rfl {α : Sort u} {a : α} : HEq a a :=
HEq.refl a
theorem eq_of_heq {α : Sort u} {a a' : α} (h : HEq a a') : Eq a a' :=
have : (α β : Sort u) → (a : α) → (b : β) → HEq a b → (h : Eq α β) → Eq (cast h a) b :=
fun α _ a _ h₁ =>
HEq.rec (motive := fun {β} (b : β) (_ : HEq a b) => (h₂ : Eq α β) → Eq (cast h₂ a) b)
(fun (_ : Eq α α) => rfl)
h₁
this α α a a' h rfl
structure Prod (α : Type u) (β : Type v) where
fst : α
snd : β
attribute [unbox] Prod
/-- Similar to `Prod`, but `α` and `β` can be propositions.
We use this Type internally to automatically generate the brecOn recursor. -/
structure PProd (α : Sort u) (β : Sort v) where
fst : α
snd : β
/-- Similar to `Prod`, but `α` and `β` are in the same universe. -/
structure MProd (α β : Type u) where
fst : α
snd : β
structure And (a b : Prop) : Prop where
intro :: (left : a) (right : b)
inductive Or (a b : Prop) : Prop where
| inl (h : a) : Or a b
| inr (h : b) : Or a b
theorem Or.intro_left (b : Prop) (h : a) : Or a b :=
Or.inl h
theorem Or.intro_right (a : Prop) (h : b) : Or a b :=
Or.inr h
theorem Or.elim {c : Prop} (h : Or a b) (left : a → c) (right : b → c) : c :=
match h with
| Or.inl h => left h
| Or.inr h => right h
inductive Bool : Type where
| false : Bool
| true : Bool
export Bool (false true)
/-- Remark: Subtype must take a Sort instead of Type because of the axiom strongIndefiniteDescription. -/
structure Subtype {α : Sort u} (p : α → Prop) where
val : α
property : p val
set_option linter.unusedVariables.funArgs false in
/-- Gadget for optional parameter support. -/
@[reducible] def optParam (α : Sort u) (default : α) : Sort u := α
/-- Gadget for marking output parameters in type classes. -/
@[reducible] def outParam (α : Sort u) : Sort u := α
/-- Auxiliary Declaration used to implement the notation (a : α) -/
@[reducible] def typedExpr (α : Sort u) (a : α) : α := a
set_option linter.unusedVariables.funArgs false in
/-- Auxiliary Declaration used to implement the named patterns `x@h:p` -/
@[reducible] def namedPattern {α : Sort u} (x a : α) (h : Eq x a) : α := a
/-- Auxiliary axiom used to implement `sorry`. -/
@[extern "lean_sorry", neverExtract]
axiom sorryAx (α : Sort u) (synthetic := false) : α
theorem eq_false_of_ne_true : {b : Bool} → Not (Eq b true) → Eq b false
| true, h => False.elim (h rfl)
| false, _ => rfl
theorem eq_true_of_ne_false : {b : Bool} → Not (Eq b false) → Eq b true
| true, _ => rfl
| false, h => False.elim (h rfl)
theorem ne_false_of_eq_true : {b : Bool} → Eq b true → Not (Eq b false)
| true, _ => fun h => Bool.noConfusion h
| false, h => Bool.noConfusion h
theorem ne_true_of_eq_false : {b : Bool} → Eq b false → Not (Eq b true)
| true, h => Bool.noConfusion h
| false, _ => fun h => Bool.noConfusion h
class Inhabited (α : Sort u) where
default : α
export Inhabited (default)
class inductive Nonempty (α : Sort u) : Prop where
| intro (val : α) : Nonempty α
axiom Classical.choice {α : Sort u} : Nonempty α → α
protected def Nonempty.elim {α : Sort u} {p : Prop} (h₁ : Nonempty α) (h₂ : α → p) : p :=
match h₁ with
| intro a => h₂ a
instance {α : Sort u} [Inhabited α] : Nonempty α :=
⟨default⟩
noncomputable def Classical.ofNonempty {α : Sort u} [Nonempty α] : α :=
Classical.choice inferInstance
instance (α : Sort u) {β : Sort v} [Nonempty β] : Nonempty (α → β) :=
Nonempty.intro fun _ => Classical.ofNonempty
instance (α : Sort u) {β : α → Sort v} [(a : α) → Nonempty (β a)] : Nonempty ((a : α) → β a) :=
Nonempty.intro fun _ => Classical.ofNonempty
instance : Inhabited (Sort u) where
default := PUnit
instance (α : Sort u) {β : Sort v} [Inhabited β] : Inhabited (α → β) where
default := fun _ => default
instance (α : Sort u) {β : α → Sort v} [(a : α) → Inhabited (β a)] : Inhabited ((a : α) → β a) where
default := fun _ => default
deriving instance Inhabited for Bool
/-- Universe lifting operation from Sort to Type -/
structure PLift (α : Sort u) : Type u where
up :: (down : α)
/-- Bijection between α and PLift α -/
theorem PLift.up_down {α : Sort u} : ∀ (b : PLift α), Eq (up (down b)) b
| up _ => rfl
theorem PLift.down_up {α : Sort u} (a : α) : Eq (down (up a)) a :=
rfl
/-- Pointed types -/
def NonemptyType := Subtype fun α : Type u => Nonempty α
abbrev NonemptyType.type (type : NonemptyType.{u}) : Type u :=
type.val
instance : Inhabited NonemptyType.{u} where
default := ⟨PUnit.{u+1}, Nonempty.intro ⟨⟩⟩
/-- Universe lifting operation -/
structure ULift.{r, s} (α : Type s) : Type (max s r) where
up :: (down : α)
/-- Bijection between α and ULift.{v} α -/
theorem ULift.up_down {α : Type u} : ∀ (b : ULift.{v} α), Eq (up (down b)) b
| up _ => rfl
theorem ULift.down_up {α : Type u} (a : α) : Eq (down (up.{v} a)) a :=
rfl
class inductive Decidable (p : Prop) where
| isFalse (h : Not p) : Decidable p
| isTrue (h : p) : Decidable p
@[inlineIfReduce, nospecialize] def Decidable.decide (p : Prop) [h : Decidable p] : Bool :=
Decidable.casesOn (motive := fun _ => Bool) h (fun _ => false) (fun _ => true)
export Decidable (isTrue isFalse decide)
abbrev DecidablePred {α : Sort u} (r : α → Prop) :=
(a : α) → Decidable (r a)
abbrev DecidableRel {α : Sort u} (r : α → α → Prop) :=
(a b : α) → Decidable (r a b)
abbrev DecidableEq (α : Sort u) :=
(a b : α) → Decidable (Eq a b)
def decEq {α : Sort u} [inst : DecidableEq α] (a b : α) : Decidable (Eq a b) :=
inst a b
set_option linter.unusedVariables false in
theorem decide_eq_true : [inst : Decidable p] → p → Eq (decide p) true
| isTrue _, _ => rfl
| isFalse h₁, h₂ => absurd h₂ h₁
theorem decide_eq_false : [Decidable p] → Not p → Eq (decide p) false
| isTrue h₁, h₂ => absurd h₁ h₂
| isFalse _, _ => rfl
theorem of_decide_eq_true [inst : Decidable p] : Eq (decide p) true → p := fun h =>
match (generalizing := false) inst with
| isTrue h₁ => h₁
| isFalse h₁ => absurd h (ne_true_of_eq_false (decide_eq_false h₁))
theorem of_decide_eq_false [inst : Decidable p] : Eq (decide p) false → Not p := fun h =>
match (generalizing := false) inst with
| isTrue h₁ => absurd h (ne_false_of_eq_true (decide_eq_true h₁))
| isFalse h₁ => h₁
theorem of_decide_eq_self_eq_true [inst : DecidableEq α] (a : α) : Eq (decide (Eq a a)) true :=
match (generalizing := false) inst a a with
| isTrue _ => rfl
| isFalse h₁ => absurd rfl h₁
@[inline] instance : DecidableEq Bool :=
fun a b => match a, b with
| false, false => isTrue rfl
| false, true => isFalse (fun h => Bool.noConfusion h)
| true, false => isFalse (fun h => Bool.noConfusion h)
| true, true => isTrue rfl
class BEq (α : Type u) where
/-- Boolean equality. -/
beq : α → α → Bool
open BEq (beq)
instance [DecidableEq α] : BEq α where
beq a b := decide (Eq a b)
-- We use "dependent" if-then-else to be able to communicate the if-then-else condition
-- to the branches
@[macroInline] def dite {α : Sort u} (c : Prop) [h : Decidable c] (t : c → α) (e : Not c → α) : α :=
Decidable.casesOn (motive := fun _ => α) h e t
/-! # if-then-else -/
@[macroInline] def ite {α : Sort u} (c : Prop) [h : Decidable c] (t e : α) : α :=
Decidable.casesOn (motive := fun _ => α) h (fun _ => e) (fun _ => t)
@[macroInline] instance {p q} [dp : Decidable p] [dq : Decidable q] : Decidable (And p q) :=
match dp with
| isTrue hp =>
match dq with
| isTrue hq => isTrue ⟨hp, hq⟩
| isFalse hq => isFalse (fun h => hq (And.right h))
| isFalse hp =>
isFalse (fun h => hp (And.left h))
@[macroInline] instance [dp : Decidable p] [dq : Decidable q] : Decidable (Or p q) :=
match dp with
| isTrue hp => isTrue (Or.inl hp)
| isFalse hp =>
match dq with
| isTrue hq => isTrue (Or.inr hq)
| isFalse hq =>
isFalse fun h => match h with
| Or.inl h => hp h
| Or.inr h => hq h
instance [dp : Decidable p] : Decidable (Not p) :=
match dp with
| isTrue hp => isFalse (absurd hp)
| isFalse hp => isTrue hp
/-! # Boolean operators -/
@[macroInline] def cond {α : Type u} (c : Bool) (x y : α) : α :=
match c with
| true => x
| false => y
@[macroInline] def or (x y : Bool) : Bool :=
match x with
| true => true
| false => y
@[macroInline] def and (x y : Bool) : Bool :=
match x with
| false => false
| true => y
@[inline] def not : Bool → Bool
| true => false
| false => true
/-- The type of natural numbers. `0`, `1`, `2`, ...-/
inductive Nat where
| zero : Nat
| succ (n : Nat) : Nat
instance : Inhabited Nat where
default := Nat.zero
/-- For numeric literals notation -/
class OfNat (α : Type u) (_ : Nat) where
ofNat : α
@[defaultInstance 100] /- low prio -/
instance (n : Nat) : OfNat Nat n where
ofNat := n
class LE (α : Type u) where le : α → α → Prop
class LT (α : Type u) where lt : α → α → Prop
@[reducible] def GE.ge {α : Type u} [LE α] (a b : α) : Prop := LE.le b a
@[reducible] def GT.gt {α : Type u} [LT α] (a b : α) : Prop := LT.lt b a
@[inline] def max [LT α] [DecidableRel (@LT.lt α _)] (a b : α) : α :=
ite (LT.lt b a) a b
@[inline] def min [LE α] [DecidableRel (@LE.le α _)] (a b : α) : α :=
ite (LE.le a b) a b
/-- Transitive chaining of proofs, used e.g. by `calc`. -/
class Trans (r : α → β → Prop) (s : β → γ → Prop) (t : outParam (α → γ → Prop)) where
trans : r a b → s b c → t a c
export Trans (trans)
instance (r : α → γ → Prop) : Trans Eq r r where
trans heq h' := heq ▸ h'
instance (r : α → β → Prop) : Trans r Eq r where
trans h' heq := heq ▸ h'
class HAdd (α : Type u) (β : Type v) (γ : outParam (Type w)) where
hAdd : α → β → γ
class HSub (α : Type u) (β : Type v) (γ : outParam (Type w)) where
hSub : α → β → γ
class HMul (α : Type u) (β : Type v) (γ : outParam (Type w)) where
hMul : α → β → γ
class HDiv (α : Type u) (β : Type v) (γ : outParam (Type w)) where
hDiv : α → β → γ
class HMod (α : Type u) (β : Type v) (γ : outParam (Type w)) where
hMod : α → β → γ
class HPow (α : Type u) (β : Type v) (γ : outParam (Type w)) where
hPow : α → β → γ
class HAppend (α : Type u) (β : Type v) (γ : outParam (Type w)) where
hAppend : α → β → γ
class HOrElse (α : Type u) (β : Type v) (γ : outParam (Type w)) where
hOrElse : α → (Unit → β) → γ
class HAndThen (α : Type u) (β : Type v) (γ : outParam (Type w)) where
hAndThen : α → (Unit → β) → γ
class HAnd (α : Type u) (β : Type v) (γ : outParam (Type w)) where
hAnd : α → β → γ
class HXor (α : Type u) (β : Type v) (γ : outParam (Type w)) where
hXor : α → β → γ
class HOr (α : Type u) (β : Type v) (γ : outParam (Type w)) where
hOr : α → β → γ
class HShiftLeft (α : Type u) (β : Type v) (γ : outParam (Type w)) where
hShiftLeft : α → β → γ
class HShiftRight (α : Type u) (β : Type v) (γ : outParam (Type w)) where
hShiftRight : α → β → γ
class Add (α : Type u) where
add : α → α → α
class Sub (α : Type u) where
sub : α → α → α
class Mul (α : Type u) where
mul : α → α → α
class Neg (α : Type u) where
neg : α → α
class Div (α : Type u) where
div : α → α → α
class Mod (α : Type u) where
mod : α → α → α
class Pow (α : Type u) (β : Type v) where
pow : α → β → α
class Append (α : Type u) where
append : α → α → α
class OrElse (α : Type u) where
orElse : α → (Unit → α) → α
class AndThen (α : Type u) where
andThen : α → (Unit → α) → α
class AndOp (α : Type u) where
and : α → α → α
class Xor (α : Type u) where
xor : α → α → α
class OrOp (α : Type u) where
or : α → α → α
class Complement (α : Type u) where
complement : α → α
class ShiftLeft (α : Type u) where
shiftLeft : α → α → α
class ShiftRight (α : Type u) where
shiftRight : α → α → α
@[defaultInstance]
instance [Add α] : HAdd α α α where
hAdd a b := Add.add a b
@[defaultInstance]
instance [Sub α] : HSub α α α where
hSub a b := Sub.sub a b
@[defaultInstance]
instance [Mul α] : HMul α α α where
hMul a b := Mul.mul a b
@[defaultInstance]
instance [Div α] : HDiv α α α where
hDiv a b := Div.div a b
@[defaultInstance]
instance [Mod α] : HMod α α α where
hMod a b := Mod.mod a b
@[defaultInstance]
instance [Pow α β] : HPow α β α where
hPow a b := Pow.pow a b
@[defaultInstance]
instance [Append α] : HAppend α α α where
hAppend a b := Append.append a b
@[defaultInstance]
instance [OrElse α] : HOrElse α α α where
hOrElse a b := OrElse.orElse a b
@[defaultInstance]
instance [AndThen α] : HAndThen α α α where
hAndThen a b := AndThen.andThen a b
@[defaultInstance]
instance [AndOp α] : HAnd α α α where
hAnd a b := AndOp.and a b
@[defaultInstance]
instance [Xor α] : HXor α α α where
hXor a b := Xor.xor a b
@[defaultInstance]
instance [OrOp α] : HOr α α α where
hOr a b := OrOp.or a b
@[defaultInstance]
instance [ShiftLeft α] : HShiftLeft α α α where
hShiftLeft a b := ShiftLeft.shiftLeft a b
@[defaultInstance]
instance [ShiftRight α] : HShiftRight α α α where
hShiftRight a b := ShiftRight.shiftRight a b
open HAdd (hAdd)
open HMul (hMul)
open HPow (hPow)
open HAppend (hAppend)
class Membership (α : outParam (Type u)) (γ : Type v) where
mem : α → γ → Prop
set_option bootstrap.genMatcherCode false in
@[extern "lean_nat_add"]
protected def Nat.add : (@& Nat) → (@& Nat) → Nat
| a, Nat.zero => a
| a, Nat.succ b => Nat.succ (Nat.add a b)
instance : Add Nat where
add := Nat.add
/- We mark the following definitions as pattern to make sure they can be used in recursive equations,
and reduced by the equation Compiler. -/
attribute [matchPattern] Nat.add Add.add HAdd.hAdd Neg.neg
set_option bootstrap.genMatcherCode false in
@[extern "lean_nat_mul"]
protected def Nat.mul : (@& Nat) → (@& Nat) → Nat
| _, 0 => 0
| a, Nat.succ b => Nat.add (Nat.mul a b) a
instance : Mul Nat where
mul := Nat.mul
set_option bootstrap.genMatcherCode false in
@[extern "lean_nat_pow"]
protected def Nat.pow (m : @& Nat) : (@& Nat) → Nat
| 0 => 1
| succ n => Nat.mul (Nat.pow m n) m
instance : Pow Nat Nat where
pow := Nat.pow
set_option bootstrap.genMatcherCode false in
@[extern "lean_nat_dec_eq"]
def Nat.beq : (@& Nat) → (@& Nat) → Bool
| zero, zero => true
| zero, succ _ => false
| succ _, zero => false
| succ n, succ m => beq n m
instance : BEq Nat where
beq := Nat.beq
theorem Nat.eq_of_beq_eq_true : {n m : Nat} → Eq (beq n m) true → Eq n m
| zero, zero, _ => rfl
| zero, succ _, h => Bool.noConfusion h
| succ _, zero, h => Bool.noConfusion h
| succ n, succ m, h =>
have : Eq (beq n m) true := h
have : Eq n m := eq_of_beq_eq_true this
this ▸ rfl
theorem Nat.ne_of_beq_eq_false : {n m : Nat} → Eq (beq n m) false → Not (Eq n m)
| zero, zero, h₁, _ => Bool.noConfusion h₁
| zero, succ _, _, h₂ => Nat.noConfusion h₂
| succ _, zero, _, h₂ => Nat.noConfusion h₂
| succ n, succ m, h₁, h₂ =>
have : Eq (beq n m) false := h₁
Nat.noConfusion h₂ (fun h₂ => absurd h₂ (ne_of_beq_eq_false this))
@[reducible, extern "lean_nat_dec_eq"]
protected def Nat.decEq (n m : @& Nat) : Decidable (Eq n m) :=
match h:beq n m with
| true => isTrue (eq_of_beq_eq_true h)
| false => isFalse (ne_of_beq_eq_false h)
@[inline] instance : DecidableEq Nat := Nat.decEq
set_option bootstrap.genMatcherCode false in
@[extern "lean_nat_dec_le"]
def Nat.ble : @& Nat → @& Nat → Bool
| zero, zero => true
| zero, succ _ => true
| succ _, zero => false
| succ n, succ m => ble n m
protected inductive Nat.le (n : Nat) : Nat → Prop
| refl : Nat.le n n
| step {m} : Nat.le n m → Nat.le n (succ m)
instance : LE Nat where
le := Nat.le
protected def Nat.lt (n m : Nat) : Prop :=
Nat.le (succ n) m
instance : LT Nat where
lt := Nat.lt
theorem Nat.not_succ_le_zero : ∀ (n : Nat), LE.le (succ n) 0 → False
| 0, h => nomatch h
| succ _, h => nomatch h
theorem Nat.not_lt_zero (n : Nat) : Not (LT.lt n 0) :=
not_succ_le_zero n
theorem Nat.zero_le : (n : Nat) → LE.le 0 n
| zero => Nat.le.refl
| succ n => Nat.le.step (zero_le n)
theorem Nat.succ_le_succ : LE.le n m → LE.le (succ n) (succ m)
| Nat.le.refl => Nat.le.refl
| Nat.le.step h => Nat.le.step (succ_le_succ h)
theorem Nat.zero_lt_succ (n : Nat) : LT.lt 0 (succ n) :=
succ_le_succ (zero_le n)
theorem Nat.le_step (h : LE.le n m) : LE.le n (succ m) :=
Nat.le.step h
protected theorem Nat.le_trans {n m k : Nat} : LE.le n m → LE.le m k → LE.le n k
| h, Nat.le.refl => h
| h₁, Nat.le.step h₂ => Nat.le.step (Nat.le_trans h₁ h₂)
protected theorem Nat.lt_trans {n m k : Nat} (h₁ : LT.lt n m) : LT.lt m k → LT.lt n k :=
Nat.le_trans (le_step h₁)
theorem Nat.le_succ (n : Nat) : LE.le n (succ n) :=
Nat.le.step Nat.le.refl
theorem Nat.le_succ_of_le {n m : Nat} (h : LE.le n m) : LE.le n (succ m) :=
Nat.le_trans h (le_succ m)
protected theorem Nat.le_refl (n : Nat) : LE.le n n :=
Nat.le.refl
theorem Nat.succ_pos (n : Nat) : LT.lt 0 (succ n) :=
zero_lt_succ n
set_option bootstrap.genMatcherCode false in
@[extern c inline "lean_nat_sub(#1, lean_box(1))"]
def Nat.pred : (@& Nat) → Nat
| 0 => 0
| succ a => a
theorem Nat.pred_le_pred : {n m : Nat} → LE.le n m → LE.le (pred n) (pred m)
| _, _, Nat.le.refl => Nat.le.refl
| 0, succ _, Nat.le.step h => h
| succ _, succ _, Nat.le.step h => Nat.le_trans (le_succ _) h
theorem Nat.le_of_succ_le_succ {n m : Nat} : LE.le (succ n) (succ m) → LE.le n m :=
pred_le_pred
theorem Nat.le_of_lt_succ {m n : Nat} : LT.lt m (succ n) → LE.le m n :=
le_of_succ_le_succ
protected theorem Nat.eq_or_lt_of_le : {n m: Nat} → LE.le n m → Or (Eq n m) (LT.lt n m)
| zero, zero, _ => Or.inl rfl
| zero, succ _, _ => Or.inr (Nat.succ_le_succ (Nat.zero_le _))
| succ _, zero, h => absurd h (not_succ_le_zero _)
| succ n, succ m, h =>
have : LE.le n m := Nat.le_of_succ_le_succ h
match Nat.eq_or_lt_of_le this with
| Or.inl h => Or.inl (h ▸ rfl)
| Or.inr h => Or.inr (succ_le_succ h)
protected theorem Nat.lt_or_ge (n m : Nat) : Or (LT.lt n m) (GE.ge n m) :=
match m with
| zero => Or.inr (zero_le n)
| succ m =>
match Nat.lt_or_ge n m with
| Or.inl h => Or.inl (le_succ_of_le h)
| Or.inr h =>
match Nat.eq_or_lt_of_le h with
| Or.inl h1 => Or.inl (h1 ▸ Nat.le_refl _)
| Or.inr h1 => Or.inr h1
theorem Nat.not_succ_le_self : (n : Nat) → Not (LE.le (succ n) n)
| 0 => not_succ_le_zero _
| succ n => fun h => absurd (le_of_succ_le_succ h) (not_succ_le_self n)
protected theorem Nat.lt_irrefl (n : Nat) : Not (LT.lt n n) :=
Nat.not_succ_le_self n
protected theorem Nat.lt_of_le_of_lt {n m k : Nat} (h₁ : LE.le n m) (h₂ : LT.lt m k) : LT.lt n k :=
Nat.le_trans (Nat.succ_le_succ h₁) h₂
protected theorem Nat.le_antisymm {n m : Nat} (h₁ : LE.le n m) (h₂ : LE.le m n) : Eq n m :=
match h₁ with
| Nat.le.refl => rfl
| Nat.le.step h => absurd (Nat.lt_of_le_of_lt h h₂) (Nat.lt_irrefl n)
protected theorem Nat.lt_of_le_of_ne {n m : Nat} (h₁ : LE.le n m) (h₂ : Not (Eq n m)) : LT.lt n m :=
match Nat.lt_or_ge n m with
| Or.inl h₃ => h₃
| Or.inr h₃ => absurd (Nat.le_antisymm h₁ h₃) h₂
theorem Nat.le_of_ble_eq_true (h : Eq (Nat.ble n m) true) : LE.le n m :=
match n, m with
| 0, _ => Nat.zero_le _
| succ _, succ _ => Nat.succ_le_succ (le_of_ble_eq_true h)
theorem Nat.ble_self_eq_true : (n : Nat) → Eq (Nat.ble n n) true
| 0 => rfl
| succ n => ble_self_eq_true n
theorem Nat.ble_succ_eq_true : {n m : Nat} → Eq (Nat.ble n m) true → Eq (Nat.ble n (succ m)) true
| 0, _, _ => rfl
| succ n, succ _, h => ble_succ_eq_true (n := n) h
theorem Nat.ble_eq_true_of_le (h : LE.le n m) : Eq (Nat.ble n m) true :=
match h with
| Nat.le.refl => Nat.ble_self_eq_true n
| Nat.le.step h => Nat.ble_succ_eq_true (ble_eq_true_of_le h)
theorem Nat.not_le_of_not_ble_eq_true (h : Not (Eq (Nat.ble n m) true)) : Not (LE.le n m) :=
fun h' => absurd (Nat.ble_eq_true_of_le h') h
@[extern "lean_nat_dec_le"]
instance Nat.decLe (n m : @& Nat) : Decidable (LE.le n m) :=
dite (Eq (Nat.ble n m) true) (fun h => isTrue (Nat.le_of_ble_eq_true h)) (fun h => isFalse (Nat.not_le_of_not_ble_eq_true h))
@[extern "lean_nat_dec_lt"]
instance Nat.decLt (n m : @& Nat) : Decidable (LT.lt n m) :=
decLe (succ n) m
set_option bootstrap.genMatcherCode false in
@[extern "lean_nat_sub"]
protected def Nat.sub : (@& Nat) → (@& Nat) → Nat
| a, 0 => a
| a, succ b => pred (Nat.sub a b)
instance : Sub Nat where
sub := Nat.sub
@[extern "lean_system_platform_nbits"] opaque System.Platform.getNumBits : Unit → Subtype fun (n : Nat) => Or (Eq n 32) (Eq n 64) :=
fun _ => ⟨64, Or.inr rfl⟩ -- inhabitant
/-- Gets the word size of the platform.
That is, whether the platform is 64 or 32 bits. -/
def System.Platform.numBits : Nat :=
(getNumBits ()).val
theorem System.Platform.numBits_eq : Or (Eq numBits 32) (Eq numBits 64) :=
(getNumBits ()).property
/-- `Fin n` is a natural number `i` with the constraint that `0 ≤ i < n`. -/
structure Fin (n : Nat) where
val : Nat
isLt : LT.lt val n
theorem Fin.eq_of_val_eq {n} : ∀ {i j : Fin n}, Eq i.val j.val → Eq i j
| ⟨_, _⟩, ⟨_, _⟩, rfl => rfl
theorem Fin.val_eq_of_eq {n} {i j : Fin n} (h : Eq i j) : Eq i.val j.val :=
h ▸ rfl
theorem Fin.ne_of_val_ne {n} {i j : Fin n} (h : Not (Eq i.val j.val)) : Not (Eq i j) :=
fun h' => absurd (val_eq_of_eq h') h
instance (n : Nat) : DecidableEq (Fin n) :=
fun i j =>
match decEq i.val j.val with
| isTrue h => isTrue (Fin.eq_of_val_eq h)
| isFalse h => isFalse (Fin.ne_of_val_ne h)
instance {n} : LT (Fin n) where
lt a b := LT.lt a.val b.val
instance {n} : LE (Fin n) where
le a b := LE.le a.val b.val
instance Fin.decLt {n} (a b : Fin n) : Decidable (LT.lt a b) := Nat.decLt ..
instance Fin.decLe {n} (a b : Fin n) : Decidable (LE.le a b) := Nat.decLe ..
def UInt8.size : Nat := 256
/-- Unsigned 8-bit integer. -/
structure UInt8 where
val : Fin UInt8.size
attribute [extern "lean_uint8_of_nat_mk"] UInt8.mk
attribute [extern "lean_uint8_to_nat"] UInt8.val
@[extern "lean_uint8_of_nat"]
def UInt8.ofNatCore (n : @& Nat) (h : LT.lt n UInt8.size) : UInt8 := {
val := { val := n, isLt := h }
}
set_option bootstrap.genMatcherCode false in
@[extern "lean_uint8_dec_eq"]
def UInt8.decEq (a b : UInt8) : Decidable (Eq a b) :=
match a, b with
| ⟨n⟩, ⟨m⟩ =>
dite (Eq n m) (fun h => isTrue (h ▸ rfl)) (fun h => isFalse (fun h' => UInt8.noConfusion h' (fun h' => absurd h' h)))
instance : DecidableEq UInt8 := UInt8.decEq
instance : Inhabited UInt8 where
default := UInt8.ofNatCore 0 (by decide)
def UInt16.size : Nat := 65536
/-- Unsigned 16-bit integer. -/
structure UInt16 where
val : Fin UInt16.size
attribute [extern "lean_uint16_of_nat_mk"] UInt16.mk
attribute [extern "lean_uint16_to_nat"] UInt16.val
@[extern "lean_uint16_of_nat"]
def UInt16.ofNatCore (n : @& Nat) (h : LT.lt n UInt16.size) : UInt16 := {
val := { val := n, isLt := h }
}
set_option bootstrap.genMatcherCode false in
@[extern "lean_uint16_dec_eq"]
def UInt16.decEq (a b : UInt16) : Decidable (Eq a b) :=
match a, b with
| ⟨n⟩, ⟨m⟩ =>
dite (Eq n m) (fun h => isTrue (h ▸ rfl)) (fun h => isFalse (fun h' => UInt16.noConfusion h' (fun h' => absurd h' h)))
instance : DecidableEq UInt16 := UInt16.decEq
instance : Inhabited UInt16 where
default := UInt16.ofNatCore 0 (by decide)
def UInt32.size : Nat := 4294967296
/-- Unsigned, 32-bit integer. -/
structure UInt32 where
val : Fin UInt32.size
attribute [extern "lean_uint32_of_nat_mk"] UInt32.mk
attribute [extern "lean_uint32_to_nat"] UInt32.val
@[extern "lean_uint32_of_nat"]
def UInt32.ofNatCore (n : @& Nat) (h : LT.lt n UInt32.size) : UInt32 := {
val := { val := n, isLt := h }
}
@[extern "lean_uint32_to_nat"]
def UInt32.toNat (n : UInt32) : Nat := n.val.val
set_option bootstrap.genMatcherCode false in
@[extern "lean_uint32_dec_eq"]
def UInt32.decEq (a b : UInt32) : Decidable (Eq a b) :=
match a, b with
| ⟨n⟩, ⟨m⟩ =>
dite (Eq n m) (fun h => isTrue (h ▸ rfl)) (fun h => isFalse (fun h' => UInt32.noConfusion h' (fun h' => absurd h' h)))
instance : DecidableEq UInt32 := UInt32.decEq
instance : Inhabited UInt32 where
default := UInt32.ofNatCore 0 (by decide)
instance : LT UInt32 where
lt a b := LT.lt a.val b.val
instance : LE UInt32 where
le a b := LE.le a.val b.val
set_option bootstrap.genMatcherCode false in
@[extern "lean_uint32_dec_lt"]
def UInt32.decLt (a b : UInt32) : Decidable (LT.lt a b) :=
match a, b with
| ⟨n⟩, ⟨m⟩ => inferInstanceAs (Decidable (LT.lt n m))
set_option bootstrap.genMatcherCode false in
@[extern "lean_uint32_dec_le"]
def UInt32.decLe (a b : UInt32) : Decidable (LE.le a b) :=
match a, b with
| ⟨n⟩, ⟨m⟩ => inferInstanceAs (Decidable (LE.le n m))
instance (a b : UInt32) : Decidable (LT.lt a b) := UInt32.decLt a b
instance (a b : UInt32) : Decidable (LE.le a b) := UInt32.decLe a b
def UInt64.size : Nat := 18446744073709551616
/-- Unsigned, 64-bit integer. -/
structure UInt64 where
val : Fin UInt64.size
attribute [extern "lean_uint64_of_nat_mk"] UInt64.mk
attribute [extern "lean_uint64_to_nat"] UInt64.val
@[extern "lean_uint64_of_nat"]
def UInt64.ofNatCore (n : @& Nat) (h : LT.lt n UInt64.size) : UInt64 := {
val := { val := n, isLt := h }
}
set_option bootstrap.genMatcherCode false in
@[extern "lean_uint64_dec_eq"]
def UInt64.decEq (a b : UInt64) : Decidable (Eq a b) :=
match a, b with
| ⟨n⟩, ⟨m⟩ =>
dite (Eq n m) (fun h => isTrue (h ▸ rfl)) (fun h => isFalse (fun h' => UInt64.noConfusion h' (fun h' => absurd h' h)))
instance : DecidableEq UInt64 := UInt64.decEq
instance : Inhabited UInt64 where
default := UInt64.ofNatCore 0 (by decide)
def USize.size : Nat := hPow 2 System.Platform.numBits
theorem usize_size_eq : Or (Eq USize.size 4294967296) (Eq USize.size 18446744073709551616) :=
show Or (Eq (hPow 2 System.Platform.numBits) 4294967296) (Eq (hPow 2 System.Platform.numBits) 18446744073709551616) from
match System.Platform.numBits, System.Platform.numBits_eq with
| _, Or.inl rfl => Or.inl (by decide)
| _, Or.inr rfl => Or.inr (by decide)
/-- A USize is an unsigned integer with the size of a word
for the platform's architecture.
For example, if running on a 32-bit machine, USize is equivalent to UInt32.
Or on a 64-bit machine, UInt64.
-/
structure USize where
val : Fin USize.size
attribute [extern "lean_usize_of_nat_mk"] USize.mk
attribute [extern "lean_usize_to_nat"] USize.val
@[extern "lean_usize_of_nat"]
def USize.ofNatCore (n : @& Nat) (h : LT.lt n USize.size) : USize := {
val := { val := n, isLt := h }
}
set_option bootstrap.genMatcherCode false in
@[extern "lean_usize_dec_eq"]
def USize.decEq (a b : USize) : Decidable (Eq a b) :=
match a, b with
| ⟨n⟩, ⟨m⟩ =>
dite (Eq n m) (fun h =>isTrue (h ▸ rfl)) (fun h => isFalse (fun h' => USize.noConfusion h' (fun h' => absurd h' h)))
instance : DecidableEq USize := USize.decEq
instance : Inhabited USize where