-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCorrectness.lean
More file actions
6373 lines (5265 loc) · 195 KB
/
Copy pathCorrectness.lean
File metadata and controls
6373 lines (5265 loc) · 195 KB
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
import Init.Data.ByteArray.Extra
import LeanExe.Ascii.Decimal
import LeanExe.Runtime
namespace LeanExe.Examples.Correctness
def shortOrSkipsTrap : UInt64 :=
if true || ((Array.replicate 1 (0 : UInt64))[5]! == 0) then 1 else 0
def shortAndSkipsTrap : UInt64 :=
if false && ((Array.replicate 1 (0 : UInt64))[5]! == 0) then 1 else 0
def divByZero : UInt64 :=
(5 : UInt64) / 0
def modByZero : UInt64 :=
(5 : UInt64) % 0
def overflow : UInt64 :=
(18446744073709551615 : UInt64) + 1
def underflow : UInt64 :=
(0 : UInt64) - 1
def natSubSaturates : Nat :=
(0 : Nat) - 1
def natSubNormal : Nat :=
(5 : Nat) - 3
def natAddNormal : Nat :=
(5 : Nat) + 3
def natMulNormal : Nat :=
(7 : Nat) * 6
def natDivModNormal (x : Nat) : Nat :=
(x / 3) * 10 + (x % 3)
def natDivModZero (x : Nat) : Nat :=
(x / 0) * 10 + (x % 0)
def natAddOverflow : Nat :=
(18446744073709551615 : Nat) + 1
def natMulOverflow : Nat :=
(9223372036854775808 : Nat) * 2
def natSuccPred (x : Nat) : Nat :=
Nat.succ x + Nat.pred x
def natSuccOverflow : Nat :=
Nat.succ 18446744073709551615
def optionGetBangNoneTrap : UInt64 :=
(none : Option UInt64).get!
def bitwiseOrXor : UInt64 :=
UInt64.xor (UInt64.lor (10 : UInt64) (12 : UInt64)) (UInt64.land (10 : UInt64) (12 : UInt64))
def bitwiseNotation : UInt64 :=
((10 : UInt64) ||| (12 : UInt64)) ^^^ ((10 : UInt64) &&& (12 : UInt64))
def complementNotation : UInt64 :=
(~~~(0 : UInt64)) &&& (255 : UInt64)
def u8Complement : Nat :=
(~~~(0 : UInt8)).toNat
def shiftMasking : UInt64 :=
UInt64.shiftLeft (1 : UInt64) (65 : UInt64) +
UInt64.shiftRight (8 : UInt64) (65 : UInt64) * (10 : UInt64)
def shiftNotation : UInt64 :=
((1 : UInt64) <<< (65 : UInt64)) +
(((8 : UInt64) >>> (65 : UInt64)) * (10 : UInt64))
def uint8ShiftNotation : Nat :=
(((1 : UInt8) <<< (8 : UInt8)).toNat) * 100 +
(((1 : UInt8) <<< (9 : UInt8)).toNat) * 10 +
(((128 : UInt8) >>> (1 : UInt8)).toNat)
def uint8DirectShift : Nat :=
(UInt8.shiftLeft (129 : UInt8) (1 : UInt8)).toNat * 1000 +
(UInt8.shiftRight (255 : UInt8) (8 : UInt8)).toNat
def uint64OfNatValue (n : Nat) : UInt64 :=
UInt64.ofNat (n + 1)
def uint64OfHugeNat : UInt64 :=
UInt64.ofNat 18446744073709551616
def natToUInt64Value (n : Nat) : UInt64 :=
Nat.toUInt64 (n + 1)
def natToUInt64Huge : UInt64 :=
Nat.toUInt64 18446744073709551616
def uint64ToNatValue (x : UInt64) : Nat :=
UInt64.toNat (x + 1)
def uint64ToNatMethodMax : Nat :=
(18446744073709551615 : UInt64).toNat
def wrappedUInt8Literal : Nat :=
(300 : UInt8).toNat
def uint64ToUInt8Wrap : Nat :=
(UInt64.toUInt8 (300 : UInt64)).toNat
def uint8ToUInt64Value : UInt64 :=
UInt8.toUInt64 (255 : UInt8) + 1
def wrappedUInt32Literal : Nat :=
(4294967296 : UInt32).toNat
def uint32AddWrap : Nat :=
(((4294967295 : UInt32) + 1).toNat)
def uint32BitwiseShift : Nat :=
(((1 : UInt32) <<< (33 : UInt32)).toNat) * 10 +
(((8 : UInt32) >>> (33 : UInt32)).toNat)
def uint32Complement : Nat :=
(~~~(0 : UInt32)).toNat
def uint32MinMax : Nat :=
(min (4000000000 : UInt32) (3 : UInt32)).toNat * 10 +
(max (4000000000 : UInt32) (3 : UInt32)).toNat
def uint32Comparisons : UInt64 :=
if (4000000000 : UInt32) > (3 : UInt32) &&
(3 : UInt32) <= (3 : UInt32) &&
((3 : UInt32) == (3 : UInt32)) then
1
else
0
def uint32DivMod : Nat :=
(((4000000000 : UInt32) / (3 : UInt32)).toNat) * 10 +
(((4000000000 : UInt32) % (3 : UInt32)).toNat)
def uint32DivModZero : Nat :=
(((7 : UInt32) / 0).toNat) * 10 + (((7 : UInt32) % 0).toNat)
def uint32ToUInt64Value : UInt64 :=
UInt32.toUInt64 (4294967295 : UInt32) + 1
def uint64ToUInt32Wrap : Nat :=
(UInt64.toUInt32 (4294967297 : UInt64)).toNat
def uint8ToUInt32Value : Nat :=
(UInt8.toUInt32 (255 : UInt8) + (1 : UInt32)).toNat
def uint32ToUInt8Wrap : Nat :=
(UInt32.toUInt8 (300 : UInt32)).toNat
def uint8OfNatValue (n : Nat) : Nat :=
(UInt8.ofNat (n + 1)).toNat
def uint8AddWrap : Nat :=
(((255 : UInt8) + 1).toNat)
def uint8SubWrap : Nat :=
(((0 : UInt8) - 1).toNat)
def uint8MulWrap : Nat :=
(((16 : UInt8) * 17).toNat)
def uint8DivModZero : Nat :=
(((7 : UInt8) / 0).toNat) * 10 + (((7 : UInt8) % 0).toNat)
def nestedShadow (x : UInt64) : UInt64 :=
let x := x + 1
let y := (let x := x + 2; x * 10)
x + y
def unusedScalarLetSkipsTrap : UInt64 :=
let _unused := (Array.replicate 1 (0 : UInt64))[5]!
1
def letUsedOnlyInUnusedProductField : UInt64 :=
(let x := (Array.replicate 1 (0 : UInt64))[5]!
(x, (7 : UInt64))).2
def ignoreUInt64 (_x : UInt64) : UInt64 :=
1
def ignoredCallArgSkipsTrap : UInt64 :=
ignoreUInt64 ((Array.replicate 1 (0 : UInt64))[5]!)
def useOnlyUnusedProductField (x : UInt64) : UInt64 :=
(x, (7 : UInt64)).2
def callArgUsedOnlyInUnusedProductField : UInt64 :=
useOnlyUnusedProductField ((Array.replicate 1 (0 : UInt64))[5]!)
def combine (left right : UInt64) : UInt64 :=
left * (100 : UInt64) + right
def callArgLets (x : UInt64) : UInt64 :=
combine (let y := x + 1; y) (let z := x + 2; z)
def productLet : UInt64 :=
let pair := ((1 : UInt64), (2 : UInt64))
pair.1 * (10 : UInt64) + pair.2
def nestedProduct : UInt64 :=
let pair := (((1 : UInt64), (2 : UInt64)), ((3 : UInt64), (4 : UInt64)))
pair.1.2 * (100 : UInt64) + pair.2.1
def productSkipsUnusedField : UInt64 :=
let pair := ((Array.replicate 1 (0 : UInt64))[5]!, (7 : UInt64))
pair.2
def productBranch (flag : UInt64) : UInt64 :=
let pair :=
if flag == 0 then
((1 : UInt64), (2 : UInt64))
else
((3 : UInt64), (4 : UInt64))
pair.1 * (10 : UInt64) + pair.2
def productMatchDestructure : UInt64 :=
match ((1 : UInt64), (2 : UInt64)) with
| (left, right) => left * (10 : UInt64) + right
def productMatchUsesFirstOnly : UInt64 :=
match ((7 : UInt64), (Array.replicate 0 (0 : UInt64)).back!) with
| (left, _right) => left
def productMatchCondition : UInt64 :=
if (
match ((2 : Nat), (5 : Nat)) with
| (left, right) => left < right
)
then
1
else
0
def productMatchNested : UInt64 :=
let pair :=
match (((1 : UInt64), (2 : UInt64)), ((3 : UInt64), (4 : UInt64))) with
| (left, right) => (left.2, right.1)
pair.1 * (10 : UInt64) + pair.2
def makePairHelper (x : UInt64) : UInt64 × UInt64 :=
(x, x + 1)
def productHelperResult : UInt64 :=
let pair := makePairHelper 4
pair.1 * (10 : UInt64) + pair.2
def productFirstHelper (pair : UInt64 × UInt64) : UInt64 :=
pair.1
def productHelperParamSkipsTrap : UInt64 :=
productFirstHelper ((7 : UInt64), (Array.replicate 0 (0 : UInt64)).back!)
def productEquality : UInt64 :=
if (((1 : UInt64), (2 : UInt64)) == ((1 : UInt64), (2 : UInt64))) then
1
else
0
def productInequality : UInt64 :=
if (((1 : UInt64), (2 : UInt64)) != ((1 : UInt64), (3 : UInt64))) then
1
else
0
def productEqualityShortCircuit : UInt64 :=
if (((1 : UInt64), (Array.replicate 0 (0 : UInt64)).back!) ==
((2 : UInt64), (0 : UInt64))) then
0
else
1
structure Point where
x : UInt64
y : UInt64
structure EqPoint where
x : UInt64
y : UInt64
deriving BEq, DecidableEq
structure EqTaggedPoint where
point : EqPoint
tag : UInt64
deriving BEq, DecidableEq
structure TaggedPoint where
point : Point
tag : UInt64
structure ArrayBox where
values : Array UInt64
count : UInt64
structure PointArrayBox where
values : Array Point
count : UInt64
structure CheckedPoint where
value : UInt64
ok : value = value
structure Box (α : Type) where
value : α
structure PairBox (α β : Type) where
left : α
right : β
structure CheckedBox (α : Type) where
value : α
ok : True
def structureProjection : UInt64 :=
let point : Point := { x := (Array.replicate 0 (0 : UInt64)).back!, y := 7 }
point.y
def structureUpdateProjection : UInt64 :=
let point : Point := { x := 1, y := 2 }
let updated := { point with y := 9 }
updated.x * (10 : UInt64) + updated.y
def makePointHelper (x : UInt64) : Point :=
{ x := x + 1, y := x + 2 }
def structureHelperResult : UInt64 :=
let point := makePointHelper 4
point.x * (10 : UInt64) + point.y
def structureEquality : UInt64 :=
if (({ x := 1, y := 2 } : EqPoint) == ({ x := 1, y := 2 } : EqPoint)) then
1
else
0
def structureInequality : UInt64 :=
if (({ x := 1, y := 2 } : EqPoint) != ({ x := 1, y := 3 } : EqPoint)) then
1
else
0
def nestedStructureEquality : UInt64 :=
if (({ point := { x := 1, y := 2 }, tag := 3 } : EqTaggedPoint) ==
({ point := { x := 1, y := 2 }, tag := 3 } : EqTaggedPoint)) then
1
else
0
def structureEqualityShortCircuit : UInt64 :=
if (({ x := 1, y := (Array.replicate 0 (0 : UInt64)).back! } : EqPoint) ==
({ x := 2, y := 0 } : EqPoint)) then
0
else
1
def structurePropEquality : UInt64 :=
if ({ x := 3, y := 4 } : EqPoint) = ({ x := 3, y := 4 } : EqPoint) then
1
else
0
def structureReturn (x : UInt64) : Point :=
{ x := x + 1, y := x + 2 }
def structureBranchReturn (flag : UInt64) : Point :=
if flag == 0 then
{ x := 1, y := 2 }
else
{ x := 3, y := 4 }
def nestedStructureReturn : TaggedPoint :=
{ point := { x := 1, y := 2 }, tag := 3 }
def structureArrayReturn : ArrayBox :=
{ values := #[4, 5], count := 2 }
def structurePointArrayReturn : PointArrayBox :=
{ values := #[({ x := 1, y := 2 } : Point), ({ x := 3, y := 4 } : Point)], count := 2 }
def structureMatchDestructure : UInt64 :=
match ({ x := 1, y := 2 } : Point) with
| { x, y } => x * (10 : UInt64) + y
def structureMatchUsesFirstOnly : UInt64 :=
match ({ x := 7, y := (Array.replicate 0 (0 : UInt64)).back! } : Point) with
| { x, y := _ } => x
def structureMatchCondition : UInt64 :=
if (
match ({ x := 2, y := 5 } : Point) with
| { x, y } => x < y
)
then
1
else
0
def proofStructureProjection : UInt64 :=
let point : CheckedPoint := { value := 9, ok := rfl }
point.value + 1
def proofStructureReturn : CheckedPoint :=
{ value := 9, ok := rfl }
def proofStructureMatch : UInt64 :=
match ({ value := 8, ok := rfl } : CheckedPoint) with
| { value, ok := _ } => value
def structureParam (point : Point) : UInt64 :=
point.x * 10 + point.y
def structureCallArgMaterialized : UInt64 :=
structureParam (makePointHelper 7)
def proofStructureParam (point : CheckedPoint) : UInt64 :=
point.value + 1
def paramBoxProjection : UInt64 :=
let box : Box UInt64 := { value := 41 }
box.value + 1
def paramBoxMatch : UInt64 :=
match ({ value := 7 } : Box UInt64) with
| { value } => value + 1
def paramPairBoxParam (box : PairBox UInt64 Bool) : UInt64 :=
if box.right then box.left else 0
def paramBoxReturn (x : UInt64) : Box UInt64 :=
{ value := x + 1 }
def paramCheckedBoxProjection : UInt64 :=
let box : CheckedBox UInt64 := { value := 8, ok := True.intro }
box.value + 1
def paramBoxArrayFold : UInt64 :=
let boxes : Array (Box UInt64) := #[{ value := 2 }, { value := 3 }]
boxes.foldl (fun acc box => acc + box.value) 0
def genericBoxValue {α : Type} (box : Box α) : α :=
box.value
def genericPairLeft {α β : Type} (box : PairBox α β) : α :=
box.left
def genericPairRight {α β : Type} (box : PairBox α β) : β :=
box.right
def genericFirstBoxValue {α β : Type} (left : Box α) (_right : Box β) : α :=
left.value
def genericApplyWithSeed {α : Type} (seed : UInt64) (value : α)
(f : α -> UInt64) : UInt64 :=
seed + f value
def genericBoxHelperProjection : UInt64 :=
genericBoxValue ({ value := 20 } : Box UInt64) + 2
def genericPairBoxHelper : UInt64 :=
let box : PairBox UInt64 Bool := { left := 9, right := true }
if genericPairRight box then genericPairLeft box else 0
def genericBoxHelperSkipsUnusedTrap : UInt64 :=
genericFirstBoxValue
({ value := 7 } : Box UInt64)
({ value := (Array.replicate 0 (0 : UInt64)).back! } : Box UInt64)
def genericInterleavedLambdaHelper : UInt64 :=
let bonus := (5 : UInt64)
genericApplyWithSeed
10
({ value := 7 } : Box UInt64)
(fun box => box.value + bonus)
structure TypeclassPoint where
x : UInt64
y : UInt64
instance : Inhabited TypeclassPoint where
default := { x := 3, y := 4 }
structure WeirdEq where
value : UInt64
instance : BEq WeirdEq where
beq _left _right := false
class TypeclassScore (α : Type) where
score : α -> UInt64
instance : TypeclassScore UInt64 where
score value := value + 3
instance : TypeclassScore TypeclassPoint where
score point := point.x * (10 : UInt64) + point.y
instance [TypeclassScore α] : TypeclassScore (Option α) where
score
| none => 5
| some value => TypeclassScore.score value + 10
def typeclassSame [BEq α] (left right : α) : Bool :=
left == right
def typeclassSameUInt64 : UInt64 :=
if typeclassSame (7 : UInt64) 7 then 1 else 0
def typeclassSameCustomBEq : UInt64 :=
if typeclassSame ({ value := 1 } : WeirdEq) ({ value := 1 } : WeirdEq) then 0 else 1
def typeclassDefaultOr [Inhabited α] (flag : Bool) (value : α) : α :=
if flag then value else default
def typeclassDefaultUInt64 : UInt64 :=
typeclassDefaultOr false (7 : UInt64)
def typeclassDefaultPoint : UInt64 :=
let point := typeclassDefaultOr false ({ x := 9, y := 9 } : TypeclassPoint)
point.x * (10 : UInt64) + point.y
def typeclassScoreGeneric [TypeclassScore α] (value : α) : UInt64 :=
TypeclassScore.score value
def typeclassScoreUInt64 : UInt64 :=
typeclassScoreGeneric (5 : UInt64)
def typeclassScorePoint : UInt64 :=
typeclassScoreGeneric ({ x := 8, y := 6 } : TypeclassPoint)
def typeclassScoreOptionUInt64 : UInt64 :=
typeclassScoreGeneric (some (5 : UInt64))
def typeclassScoreArrayTotal [TypeclassScore α] (values : Array α) : UInt64 :=
values.foldl (fun acc value => acc + TypeclassScore.score value) 0
def typeclassScoreArrayTotalDemo : UInt64 :=
typeclassScoreArrayTotal (#[some (1 : UInt64), none, some 2] : Array (Option UInt64))
def typeclassScoreArrayAny [TypeclassScore α] (limit : UInt64) (values : Array α) : Bool :=
values.any (fun value => limit < TypeclassScore.score value)
def typeclassScoreArrayAnyDemo : UInt64 :=
if typeclassScoreArrayAny 14 (#[some (1 : UInt64), none, some 2] : Array (Option UInt64)) then
1
else
0
def typeclassScoreArrayFind? [TypeclassScore α] (target : UInt64) (values : Array α) : Option α :=
values.find? (fun value => TypeclassScore.score value == target)
def typeclassScoreArrayFindDemo : UInt64 :=
match typeclassScoreArrayFind? 15 (#[some (1 : UInt64), none, some 2] : Array (Option UInt64)) with
| none => 0
| some value => TypeclassScore.score value
def typeclassScoreListFoldl [TypeclassScore α] (values : List α) : UInt64 :=
values.foldl (fun acc value => acc + TypeclassScore.score value) 0
def typeclassScoreListFoldlDemo : UInt64 :=
typeclassScoreListFoldl ([some (1 : UInt64), none, some 2] : List (Option UInt64))
def typeclassScoreListFind? [TypeclassScore α] (target : UInt64) (values : List α) : Option α :=
values.find? (fun value => TypeclassScore.score value == target)
def typeclassScoreListFindDemo : UInt64 :=
match typeclassScoreListFind? 15 ([some (1 : UInt64), none, some 2] : List (Option UInt64)) with
| none => 0
| some value => TypeclassScore.score value
def rejectTypeclassEntry [TypeclassScore UInt64] (value : UInt64) : UInt64 :=
TypeclassScore.score value
def rejectTypeclassRuntimeDictionaryParam (inst : TypeclassScore UInt64) (value : UInt64) : UInt64 :=
TypeclassScore.score (self := inst) value
structure DigitState where
pos : Nat
sum : UInt64
structure ByteOutputState where
count : UInt64
bytes : ByteArray
structure ByteArrayGroup where
values : Array ByteArray
marker : UInt64
deriving BEq
inductive PublicToken where
| text : ByteArray -> PublicToken
| number : UInt64 -> PublicToken
deriving BEq
inductive PublicHeapArrayResult where
| failed : ByteArray -> PublicHeapArrayResult
| ok : Array ByteArray -> UInt64 -> PublicHeapArrayResult
structure ParserBufferState where
pos : Nat
out : ByteArray
ok : Bool
structure ArrayBuilderState where
values : Array UInt64
count : UInt64
structure OwnedCallBox where
values : Array UInt64
bytes : ByteArray
count : UInt64
structure EqByteBox where
bytes : ByteArray
count : UInt64
deriving BEq
def isAsciiDigitByte (byte : UInt8) : Bool :=
(48 : UInt8) <= byte && byte <= (57 : UInt8)
def digitStateCanContinue (input : ByteArray) (state : DigitState) : Bool :=
if state.pos < input.size then
isAsciiDigitByte input[state.pos]!
else
false
def digitStateStep (input : ByteArray) (state : DigitState) : DigitState :=
let byte := input[state.pos]!
{ pos := state.pos + 1, sum := state.sum + (byte.toUInt64 - 48) }
def digitStateParseFuel : Nat → ByteArray → DigitState → DigitState
| 0, _input, state => state
| fuel + 1, input, state =>
if digitStateCanContinue input state then
digitStateParseFuel fuel input (digitStateStep input state)
else
state
def digitStateParseValue (input : ByteArray) : UInt64 :=
let state := digitStateParseFuel (input.size + 1) input { pos := 0, sum := 0 }
if state.pos == input.size then
state.sum * 100 + Nat.toUInt64 state.pos
else
999
def digitStateParserAllDigitsDemo : UInt64 :=
digitStateParseValue (ByteArray.mk #[(49 : UInt8), (50 : UInt8), (51 : UInt8)])
def digitStateParserStopsDemo : UInt64 :=
digitStateParseValue (ByteArray.mk #[(49 : UInt8), (50 : UInt8), (65 : UInt8)])
inductive Status where
| ok : UInt64 -> Status
| error : UInt64 -> Status
inductive EqStatus where
| ok : UInt64 -> EqStatus
| error : UInt64 -> EqStatus
deriving BEq, DecidableEq
inductive Mode where
| idle : Mode
| busy : Mode
| done : Mode
inductive CheckedStatus where
| checked : (value : UInt64) -> value = value -> CheckedStatus
| failed : UInt64 -> CheckedStatus
inductive ParamResult (ε α : Type) where
| error : ε -> ParamResult ε α
| ok : α -> ParamResult ε α
inductive CheckedPayload (α : Type) where
| wrap : (value : α) -> True -> CheckedPayload α
def statusOkMatch : UInt64 :=
match Status.ok 8 with
| .ok value => value
| .error code => code + 100
def statusErrorMatch : UInt64 :=
match Status.error 9 with
| .ok value => value + 100
| .error code => code
def statusSourceOrderIndependentMatch : UInt64 :=
match Status.error 11 with
| .error code => code
| .ok value => value + 100
def statusSkipsUnusedPayloadTrap : UInt64 :=
match Status.ok ((Array.replicate 0 (0 : UInt64)).back!) with
| .ok _ => 7
| .error _ => 8
def statusMatchCondition : UInt64 :=
if (
match Status.ok 2 with
| .ok value => value == 2
| .error _ => false
)
then
1
else
0
def makeStatusHelper (x : UInt64) : Status :=
Status.ok (x + 1)
def statusHelperResult : UInt64 :=
match makeStatusHelper 4 with
| .ok value => value
| .error code => code
def statusBranchReturn (flag : UInt64) : Status :=
if flag == 0 then
Status.ok 5
else
Status.error 9
def modeMatch : UInt64 :=
match Mode.busy with
| .idle => 1
| .busy => 2
| .done => 3
def modeReturn (flag : UInt64) : Mode :=
if flag == 0 then
Mode.idle
else
Mode.done
def checkedStatusMatch : UInt64 :=
match CheckedStatus.checked 8 rfl with
| .checked value _ => value
| .failed code => code
def checkedStatusReturn : CheckedStatus :=
CheckedStatus.checked 9 rfl
def paramResultOkMatch : UInt64 :=
match (ParamResult.ok ({ value := 5 } : Box UInt64) : ParamResult UInt64 (Box UInt64)) with
| .error code => code
| .ok box => box.value + 1
def paramResultErrorMatch : UInt64 :=
match (ParamResult.error (4 : UInt64) : ParamResult UInt64 (Box UInt64)) with
| .error code => code + 10
| .ok box => box.value
def paramResultReturn (flag : UInt64) : ParamResult UInt64 Point :=
if flag == 0 then
ParamResult.error 7
else
ParamResult.ok { x := flag, y := flag + 1 }
def paramResultParam (value : ParamResult UInt64 Point) : UInt64 :=
match value with
| .error code => code
| .ok point => point.x * 10 + point.y
def checkedPayloadMatch : UInt64 :=
match (CheckedPayload.wrap (9 : UInt64) True.intro) with
| .wrap value _ => value + 1
def paramResultArrayFold : UInt64 :=
let values : Array (ParamResult UInt64 UInt64) :=
#[ParamResult.ok 5, ParamResult.error 7]
values.foldl
(fun acc item =>
match item with
| .error code => acc + code * 10
| .ok value => acc + value)
0
def genericResultIsOk {ε α : Type} (value : ParamResult ε α) : Bool :=
match value with
| .error _ => false
| .ok _ => true
def genericResultValueOr {ε α : Type} (fallback : α) (value : ParamResult ε α) : α :=
match value with
| .error _ => fallback
| .ok item => item
def genericCheckedPayloadValue {α : Type} (payload : CheckedPayload α) : α :=
match payload with
| .wrap value _ => value
def genericResultIsOkDemo : UInt64 :=
if genericResultIsOk (ParamResult.ok ({ x := 1, y := 2 } : Point) :
ParamResult UInt64 Point) then
1
else
0
def genericResultValueOrDemo : UInt64 :=
let point :=
genericResultValueOr
({ x := 9, y := 9 } : Point)
(ParamResult.ok ({ x := 3, y := 4 } : Point) : ParamResult UInt64 Point)
point.x * 10 + point.y
def genericCheckedPayloadDemo : UInt64 :=
genericCheckedPayloadValue (CheckedPayload.wrap (11 : UInt64) True.intro) + 1
def statusParam (status : Status) : UInt64 :=
match status with
| .ok value => value + 10
| .error code => code + 20
def statusLeftScore (status : Status) : UInt64 :=
match status with
| .ok value => value
| .error code => code + 100
def statusRightScore (status : Status) : UInt64 :=
match status with
| .ok value => value + 100
| .error code => code
def inductiveEqualitySameCtor : UInt64 :=
if (EqStatus.ok 7 == EqStatus.ok 7) then
1
else
0
def inductiveInequalitySameCtor : UInt64 :=
if (EqStatus.ok 7 != EqStatus.ok 8) then
1
else
0
def inductiveEqualityDifferentCtor : UInt64 :=
if (EqStatus.ok 7 == EqStatus.error 7) then
0
else
1
def inductiveEqualityDifferentCtorSkipsPayload : UInt64 :=
if (EqStatus.ok ((Array.replicate 0 (0 : UInt64)).back!) == EqStatus.error 0) then
0
else
1
def inductivePropEquality : UInt64 :=
if EqStatus.ok 5 = EqStatus.ok 5 then
1
else
0
def optionStructuralEquality : UInt64 :=
if ((some ({ x := 1, y := 2 } : EqPoint)) == some ({ x := 1, y := 2 } : EqPoint)) &&
((none : Option EqPoint) != some ({ x := 0, y := 0 } : EqPoint)) then
1
else
0
def byteArrayEquality : UInt64 :=
if (ByteArray.mk #[(65 : UInt8), (66 : UInt8)] ==
ByteArray.mk #[(65 : UInt8), (66 : UInt8)]) then
1
else
0
def byteArrayInequalityValue : UInt64 :=
if (ByteArray.mk #[(65 : UInt8), (66 : UInt8)] !=
ByteArray.mk #[(65 : UInt8), (67 : UInt8)]) then
1
else
0
def byteArrayInequalityLength : UInt64 :=
if (ByteArray.mk #[(65 : UInt8), (66 : UInt8)] !=
ByteArray.mk #[(65 : UInt8)]) then
1
else
0
def byteArrayFieldStructureEquality : UInt64 :=
if (({ bytes := ByteArray.mk #[(65 : UInt8)], count := 1 } : EqByteBox) ==
({ bytes := ByteArray.mk #[(65 : UInt8)], count := 1 } : EqByteBox)) then
1
else
0
def byteArrayOptionEquality : UInt64 :=
if ((some (ByteArray.mk #[(65 : UInt8), (66 : UInt8)])) ==
some (ByteArray.mk #[(65 : UInt8), (66 : UInt8)])) then
1
else
0
def arrayEquality : UInt64 :=
if ((#[(1 : UInt64), (2 : UInt64)] : Array UInt64) ==
(#[(1 : UInt64), (2 : UInt64)] : Array UInt64)) then
1
else
0
def arrayInequalityValue : UInt64 :=
if ((#[(1 : UInt64), (2 : UInt64)] : Array UInt64) !=
(#[(1 : UInt64), (3 : UInt64)] : Array UInt64)) then
1
else
0
def arrayInequalityLength : UInt64 :=
if ((#[(1 : UInt64), (2 : UInt64)] : Array UInt64) !=
(#[(1 : UInt64)] : Array UInt64)) then
1
else
0
def arrayPropEquality : UInt64 :=
if (#[(1 : UInt64), (2 : UInt64)] : Array UInt64) =
(#[(1 : UInt64), (2 : UInt64)] : Array UInt64) then
1
else
0
def pointArrayEquality : UInt64 :=
if ((#[({ x := 1, y := 2 } : EqPoint), ({ x := 3, y := 4 } : EqPoint)] : Array EqPoint) ==
#[({ x := 1, y := 2 } : EqPoint), ({ x := 3, y := 4 } : EqPoint)]) then
1
else
0
def statusArrayEquality : UInt64 :=
if ((#[EqStatus.ok 5, EqStatus.error 7] : Array EqStatus) ==
#[EqStatus.ok 5, EqStatus.error 7]) then
1
else
0
def nestedArrayEquality : UInt64 :=
if ((#[#[1, 2], #[3]] : Array (Array UInt64)) ==
(#[#[1, 2], #[3]] : Array (Array UInt64))) then
1
else
0
def byteArrayArrayEquality : UInt64 :=
if ((#[ByteArray.mk #[(65 : UInt8)], ByteArray.mk #[(66 : UInt8), (67 : UInt8)]] :
Array ByteArray) ==
#[ByteArray.mk #[(65 : UInt8)], ByteArray.mk #[(66 : UInt8), (67 : UInt8)]]) then
1
else
0
def byteArrayStructureArrayEquality : UInt64 :=
if ((#[
({ bytes := ByteArray.mk #[(65 : UInt8)], count := 1 } : EqByteBox),
({ bytes := ByteArray.mk #[(66 : UInt8), (67 : UInt8)], count := 2 } : EqByteBox)
] : Array EqByteBox) ==
#[
({ bytes := ByteArray.mk #[(65 : UInt8)], count := 1 } : EqByteBox),
({ bytes := ByteArray.mk #[(66 : UInt8), (67 : UInt8)], count := 2 } : EqByteBox)
]) then
1
else
0
inductive U64List where
| nil : U64List
| cons : UInt64 → U64List → U64List
inductive EqU64List where
| nil : EqU64List
| cons : UInt64 → EqU64List → EqU64List
deriving BEq, DecidableEq
def rejectRecursiveInductiveEquality : UInt64 :=
if (EqU64List.cons 1 EqU64List.nil == EqU64List.cons 1 EqU64List.nil) then
1
else
0
def u64List123 : U64List :=
U64List.cons 1 (U64List.cons 2 (U64List.cons 3 U64List.nil))
def u64ListHeadOrZero (xs : U64List) : UInt64 :=
match xs with
| .nil => 0
| .cons head _tail => head
def u64ListHeadDemo : UInt64 :=
u64ListHeadOrZero u64List123
def u64ListTailHeadDemo : UInt64 :=
match u64List123 with
| .nil => 0
| .cons _head tail => u64ListHeadOrZero tail