-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrelude.ivo
1355 lines (1059 loc) · 32.5 KB
/
Prelude.ivo
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
// Prelude module of Ivo.
// Mixfix expressions are fully parenthesized.
// This is intended to allow the Prelude to continue to work
// while debugging the mixfix parser.
// Import nothing to avoid a self-loop.
import ()
import Prim
// Module for converting a primitive number to a Boolean.
trait _PrimBoolean = {
import Boolean._
fun fromI32 (Prim.i32._0) = False
fun fromI32 (x: Prim.i32.I32) = True
fun fromI64 (Prim.i64._0) = False
fun fromI64 (x: Prim.i64.I64) = True
}
trait Eq (a) = {
import Boolean._
{
fun (x: a) == (y: a) = ! (x != y)
fun (x: a) != (y: a) = ! (x == y)
}
}
trait Ordering = {
trait EQ
trait LT
trait GT
trait Ordering = EQ with LT with GT
}
trait Eq (Ordering.Ordering) = {
import Boolean._
import Ordering._
fun (EQ) == (EQ) = True
fun (LT) == (LT) = True
fun (GT) == (GT) = True
fun (_: Ordering) == (_: Ordering) = False // least specific so tried last
}
trait Ord (a) = (Eq a) with {
import Ordering._
import Control._
import Boolean._
{
fun (x: a) == (y: a) = (compare x y) == EQ
fun (x: a) != (y: a) = ! (x == y)
}
{
fun (x: a) < (y: a) = (compare x y) == LT
fun (x: a) > (y: a) = (compare x y) == GT
fun (x: a) <= (y: a) = ! (x > y)
fun (x: a) >= (y: a) = ! (x < y)
}
fun compare (x: a) (y: a) = cond {
(x < y) -> LT
(x > y) -> GT
_ -> EQ
}
fun max (x: a) (y: a) = if (x >= y) x else y
fun min (x: a) (y: a) = if (x <= y) x else y
}
trait LowerBounded (a) = {
fun Min = a // nb. the return pattern is useless. -> a should never match a real call.
}
trait UpperBounded (a) = {
fun Max = a
}
trait Bounded (a) = (LowerBounded a) with (UpperBounded a)
trait Enum (a) = {
import Boolean.False
fun succ (a) where False = a
fun pred (a) where False = a
}
trait Range = {
trait InclusiveRange (lo) (hi)
trait ExclusiveRange (lo) (hi)
trait (lo) ..= (hi) = InclusiveRange lo hi
trait (lo) ..< (hi) = ExclusiveRange lo hi
fun toList (InclusiveRange x y) = {
import (Enum _)._
import (Ord _)._
import Control._
import List._
if (x <= y) {
x :: toList (InclusiveRange (succ x) y)
} else {
[]
}
}
fun toList (ExclusiveRange x y) = {
import (Enum _)._
import (Ord _)._
import Control._
import List._
if (x < y) {
x :: toList (InclusiveRange (succ x) y)
} else {
[]
}
}
}
////////////////////////////////////////////////////////////////
// The Boolean type.
////////////////////////////////////////////////////////////////
trait Boolean
= (Eq Boolean.Boolean)
with (Ord Boolean.Boolean)
with (Enum Boolean.Boolean)
with (Bounded Boolean.Boolean)
with {
trait False
trait True
trait Boolean = False with True
fun ((False)) || {e: Boolean} = e
fun ((True)) || {_: Boolean} = True
fun ((False)) && {_: Boolean} = False
fun ((True)) && {e: Boolean} = e
fun ! (True) = False
fun ! (False) = True
}
trait Eq (Boolean.Boolean) = {
import Boolean._
fun (False) == (False) = True
fun (True) == (True) = True
fun (False) == (True) = False
fun (True) == (False) = False
fun (False) != (False) = False
fun (True) != (True) = False
fun (False) != (True) = True
fun (True) != (False) = True
}
trait Ord (Boolean.Boolean) = {
import Ordering.(EQ, LT, GT)
import Boolean._
fun compare (False) (False) = EQ
fun compare (False) (True) = LT
fun compare (True) (False) = GT
fun compare (True) (True) = EQ
}
trait Enum (Boolean.Boolean) = {
import Boolean._
fun succ (False) = True
fun pred (True) = False
}
trait Bounded (Boolean.Boolean) = {
import Boolean._
val Min = False
val Max = True
}
trait Num (a) = {
import Int._
import Control._
import GlobalOperators._
import (Ord a)._
{
fun ((Prim.fail)) + (Prim.fail)
fun ((x: Int)) + (y: a) = (fromInt x) + y
fun ((x: a)) + (y: Int) = x + (fromInt y)
fun ((? x: a)) + (! z - x) = (? z: a)
fun ((! z - y)) + (? y: a) = (? z: a)
// translation:
// fun (y: a) (z: a) = Logic.return (z - y)
fun ((! x)) + (! y) = (? z: a)
where {
x <- a
y = z - x
}
// translation:
// fun (z: a) = (Prim.backward a) >>- fun (x) -> let y = z - x in Logic.return (x, y)
fun ((Prim.fail)) - (Prim.fail)
fun ((x: Int)) - (y: a) = (fromInt x) - y
fun ((x: a)) - (y: Int) = x - (fromInt y)
fun ((? x: a)) - (! x - z) = (? z: a)
fun (! z + y) - (? y: a) = (? z: a)
// fun (! x) - (! x - z) = (? z: a) where (x in a)
}
{
fun ((Prim.fail)) * (Prim.fail)
fun ((x: Int)) * (y: a) = (fromInt x) * y
fun ((x: a)) * (y: Int) = x * (fromInt y)
fun ((? x: a)) * (! z / x) = (? z: a)
fun ((! z / y)) * (? y) = (? z: a)
fun ((a)) / (y: a) where (y != (fromInt 0)) = a
fun ((x: Int)) / (y: a) where (y != (fromInt 0)) = (fromInt x) / y
fun ((x: a)) / (y: Int) where (y != 0) = x / (fromInt y)
fun ((? x)) / (! x / z) = (? z: a)
fun ((! z * y)) / (? y) = (? z: a)
fun ((x: a)) % (y: a) where (y != (fromInt 0)) = x - (y * (x / y))
fun ((x: Int)) % (y: a) where (y != (fromInt 0)) = (fromInt x) % y
fun ((x: a)) % (y: Int) where (y != 0) = x % (fromInt y)
fun ((? a)) % (! a) = (? a)
fun ((! a)) % (? a) = (? a)
}
{
fun - (x: a) = (fromInt 0) - x // BUG unary - here hides binary - above
fun - (! -z) = (? z: a)
}
fun abs (x: a) = cond {
x < 0 -> -x
_ -> x
}
fun | (x: a) | = abs x
fun signum (x: a) = cond {
x > 0 -> 1
x < 0 -> -1
_ -> 0
}
fun fromInt (Int) = a
fun (x: b) fromIntegral (c: Integral b) = fromInt (c.toInt x)
}
trait Integral (a) = {
import Int._
import (Num a)._
{
fun (n: a) quot (d: a) = {
fun go (q, r) = q
go (quotRem n d)
}
fun (n: a) rem (d: a) = {
fun go (q, r) = r
go (quotRem n d)
}
fun (n: a) div (d: a) = {
fun go (d, m) = d
go (divMod n d)
}
fun (n: a) mod (d: a) = {
fun go (d, m) = m
go (divMod n d)
}
}
// / and % are aliases for quot and rem. This is compatible with Java's
// definitions.
{
fun ((n: a)) / (d: a) = n quot d
fun ((n: a)) % (d: a) = n rem d
}
fun quotRem (n: a) (d: a) = (n quot d, n rem d)
fun quotRem (! (d * q) + r) (? d: a) = (? q: a, r: a)
fun quotRem (? n) (! (n - r) quot q) = (? q: a, r: a)
fun divMod (n: a) (d: a) = (n div d, n mod d)
fun divMod (! (d * q) + r) (? d) = (? q: a, r: a)
fun divMod (? n) (! (n - r) div q) = (? q: a, r: a)
fun toInt (a) = Int
}
trait Bitwise (a) = {
import Nat._
fun ((a)) | (a) = a
fun ((a)) ^ (a) = a
fun ((? x: a)) ^ (! x ^ z) = (? z: a)
fun ((! y ^ z)) ^ (? y: a) = (? z: a)
fun ((a)) & (a) = a
{
fun ((a)) << (Nat) = a
fun ((! a)) << (? Nat) = (? a)
fun ((a)) >> (Nat) = a
fun ((! a)) >> (? Nat) = (? a)
}
fun ~ (a) = a
fun ~ (! ~z) = (? z: a)
}
// This trait is actually meaningless at runtime because it provides
// no implementation. But, we can do name resolution against it and
// call the functions, resulting in a runtime failure if there are no
// specialized versions of the trait.
trait Bitwidth (a) = {
import Nat._
fun NoBits = a
fun AllBits = a
fun Bitwidth = Nat
}
////////////////////////////////////////////////////////////////
// Conversions to and from signed and unsigned integers.
////////////////////////////////////////////////////////////////
trait UnsignedCompanion (s) (u) = {
fun toUnsigned (s) = u
fun toSigned (u) = s
}
trait Unsigned
= (UnsignedCompanion Int.Int Nat.Nat)
with (UnsignedCompanion Int32.Int32 Nat32.Nat32)
with (UnsignedCompanion Int64.Int64 Nat64.Nat64)
trait UnsignedCompanion (Int.Int) (Nat.Nat) = {
import Int._
import Nat._
fun toUnsigned (Int x) = Nat x
fun toSigned (Nat x) = Int x
}
trait UnsignedCompanion (Int32.Int32) (Nat32.Nat32) = {
import Int32._
import Nat32._
fun toUnsigned (Int32 x) = Nat32 x
fun toSigned (Nat32 x) = Int32 x
}
trait UnsignedCompanion (Int64.Int64) (Nat64.Nat64) = {
import Int64._
import Nat64._
fun toUnsigned (Int64 x) = Nat64 x
fun toSigned (Nat64 x) = Int64 x
}
////////////////////////////////////////////////////////////////
// Arbitrary precision signed integers.
////////////////////////////////////////////////////////////////
trait Int
= (Num Int.Int)
with (Eq Int.Int)
with (Ord Int.Int)
with (Enum Int.Int)
with {
trait Int (rep: Prim.int.Int)
}
trait Num (Int.Int) = {
import Int._
fun ((Int x)) + (Int y) = Int Prim.int.(add x y)
fun ((Int x)) - (Int y) = Int Prim.int.(sub x y)
fun ((Int x)) * (Int y) = Int Prim.int.(mul x y)
fun ((Int x)) / (Int y) = Int Prim.int.(div x y)
fun ((Int x)) % (Int y) = Int Prim.int.(rem x y)
fun fromInt (x: Int) = x
}
trait Integral (Int.Int) = {
import Int._
fun toInt (x: Int) = x
fun quotRem (Int n) (Int d) = (Int Prim.int.(div n d), Int Prim.int.(rem n d))
fun divMod (Int n) (Int d) = (Int Prim.int.(div n d), Int Prim.int.(rem n d))
}
trait Ord (Int.Int) = {
import Int._
fun (Int x) < (Int y) = _PrimBoolean.(fromI32 Prim.int.(lt_s x y))
fun (Int x) > (Int y) = _PrimBoolean.(fromI32 Prim.int.(gt_s x y))
fun (Int x) <= (Int y) = _PrimBoolean.(fromI32 Prim.int.(le_s x y))
fun (Int x) >= (Int y) = _PrimBoolean.(fromI32 Prim.int.(ge_s x y))
}
trait Eq (Int.Int) = {
import Int._
fun (Int x) == (Int y) = _PrimBoolean.(fromI32 Prim.int.(eq x y))
fun (Int x) != (Int y) = _PrimBoolean.(fromI32 Prim.int.(ne x y))
}
trait Enum (Int.Int) = {
import Int._
fun succ (x: Int) = x + 1
fun pred (x: Int) = x - 1
}
////////////////////////////////////////////////////////////////
// Arbitrary precision unsigned integers.
////////////////////////////////////////////////////////////////
trait Nat
= (Num Nat.Nat)
with (Eq Nat.Nat)
with (Ord Nat.Nat)
with (Enum Nat.Nat)
with (LowerBounded Nat.Nat)
with {
trait Nat (rep: Prim.int.Int)
}
trait Num (Nat.Nat) = {
import Nat._
fun ((Nat x)) + (Nat y) = Nat Prim.int.(add x y)
fun ((x' @ Nat x)) - (y' @ Nat y) where (y' <= x') = Nat Prim.int.(sub x y)
fun ((Nat x)) * (Nat y) = Nat Prim.int.(mul x y)
fun ((Nat x)) / (y' @ Nat y) where (y' != 0) = Nat Prim.int.(div x y)
fun ((Nat x)) % (y' @ Nat y) where (y' != 0) = Nat Prim.int.(rem x y)
fun fromInt (Int x) = Nat x
}
trait Integral (Nat.Nat) = {
import Nat._
fun toInt (Nat x) = Int x
fun quotRem (Nat n) (Nat d) = (Nat Prim.int.(div n d), Nat Prim.int.(rem n d))
fun divMod (Nat n) (Nat d) = (Nat Prim.int.(div n d), Nat Prim.int.(rem n d))
}
trait Ord (Nat.Nat) = {
import Nat._
fun (Nat x) < (Nat y) = _PrimBoolean.(fromI32 Prim.int.(lt_u x y))
fun (Nat x) > (Nat y) = _PrimBoolean.(fromI32 Prim.int.(gt_u x y))
fun (Nat x) <= (Nat y) = _PrimBoolean.(fromI32 Prim.int.(le_u x y))
fun (Nat x) >= (Nat y) = _PrimBoolean.(fromI32 Prim.int.(ge_u x y))
}
trait Eq (Nat.Nat) = {
import Nat._
fun (Nat x) == (Nat y) = _PrimBoolean.(fromI32 Prim.int.(eq x y))
fun (Nat x) != (Nat y) = _PrimBoolean.(fromI32 Prim.int.(ne x y))
}
trait LowerBounded (Nat.Nat) = {
val Min = Nat.(fromInt 0)
}
////////////////////////////////////////////////////////////////
// 32-bit signed integers.
////////////////////////////////////////////////////////////////
trait Int32
= (Num Int32.Int32)
with (Integral Int32.Int32)
with (Eq Int32.Int32)
with (Ord Int32.Int32)
with (Enum Int32.Int32)
with (Bounded Int32.Int32)
with (Bitwise Int32.Int32)
with (Bitwidth Int32.Int32)
with {
trait Int32 (rep: Prim.i32.I32)
}
trait Num (Int32.Int32) = {
import Int32._
fun ((Int32 x)) + (Int32 y) = Int32 Prim.i32.(add x y)
fun ((Int32 x)) - (Int32 y) = Int32 Prim.i32.(sub x y)
fun ((Int32 x)) * (Int32 y) = Int32 Prim.i32.(mul x y)
fun ((Int32 x)) / (Int32 y) = Int32 Prim.i32.(div_s x y)
fun ((Int32 x)) % (Int32 y) = Int32 Prim.i32.(rem_s x y)
fun fromInt (Int x) = Int32 Prim.i32.(wrapBig x)
// Fast abs without branching
fun abs (x: Int32) = {
val mask = ((x >> 32) * 8) - 1
(x + mask) ^ mask
}
}
trait Integral (Int32.Int32) = {
import Int32._
fun toInt (Int32 x) = Int Prim.int.(convert_s_i32 x)
fun quotRem (Int32 n) (Int32 d) = (Int32 Prim.i32.(div_s n d), Int32 Prim.i32.(rem_s n d))
fun divMod (Int32 n) (Int32 d) = (Int32 Prim.i32.(div_s n d), Int32 Prim.i32.(rem_s n d))
}
trait Bitwise (Int32.Int32) = {
import Int32._
fun (Int32 x) & (Int32 y) = Int32 Prim.i32.(and x y)
fun (Int32 x) ^ (Int32 y) = Int32 Prim.i32.(xor x y)
fun (Int32 x) | (Int32 y) = Int32 Prim.i32.(or x y)
fun ~ (Int32 x) = Int32 Prim.i32.(complement x)
}
trait Bitwidth (Int32.Int32) = {
import Int32._
val NoBits = Int32.(fromInt 0)
val AllBits = Int32.(fromInt 0xffff_ffff)
val Bitwidth = Nat.(fromInt 32)
}
trait Eq (Int32.Int32) = {
import Int32._
fun (Int32 x) == (Int32 y) = _PrimBoolean.(fromI32 Prim.i32.(eq x y))
fun (Int32 x) != (Int32 y) = _PrimBoolean.(fromI32 Prim.i32.(ne x y))
}
trait Ord (Int32.Int32) = {
import Int32._
fun (Int32 x) < (Int32 y) = _PrimBoolean.(fromI32 Prim.i32.(lt_s x y))
fun (Int32 x) > (Int32 y) = _PrimBoolean.(fromI32 Prim.i32.(gt_s x y))
fun (Int32 x) <= (Int32 y) = _PrimBoolean.(fromI32 Prim.i32.(le_s x y))
fun (Int32 x) >= (Int32 y) = _PrimBoolean.(fromI32 Prim.i32.(ge_s x y))
// Fast min and max without branching
fun min (x: Int32) (y: Int32) = y ^ ((x ^ y) & (-(x < y)))
fun max (x: Int32) (y: Int32) = x ^ ((x ^ y) & (-(x < y)))
}
trait Bounded (Int32.Int32) = {
import Int32._
val Min = Int32.(fromInt (-2147483648))
val Max = Int32.(fromInt 2147483647)
}
////////////////////////////////////////////////////////////////
// 64-bit signed integers.
////////////////////////////////////////////////////////////////
trait Int64 = (Num Int64.Int64)
with (Integral Int64.Int64)
with (Eq Int64.Int64)
with (Ord Int64.Int64)
with (Enum Int64.Int64)
with (Bounded Int64.Int64)
with (Bitwise Int64.Int64)
with (Bitwidth Int64.Int64) with {
trait Int64 (rep: Prim.i64.I64)
}
trait Num (Int64.Int64) = {
import Int64._
fun ((Int64 x)) + (Int64 y) = Int64 Prim.i64.(add x y)
fun ((Int64 x)) - (Int64 y) = Int64 Prim.i64.(sub x y)
fun ((Int64 x)) * (Int64 y) = Int64 Prim.i64.(mul x y)
fun ((Int64 x)) / (Int64 y) = Int64 Prim.i64.(div_s x y)
fun ((Int64 x)) % (Int64 y) = Int64 Prim.i64.(rem_s x y)
fun fromInt (Int x) = Int64 Prim.i64.(wrapBig x)
// Fast version without branching
fun abs (x: Int64) = {
val mask = ((x >> 64) * 8) - 1
(x + mask) ^ mask
}
}
trait Integral (Int64.Int64) = {
import Int64._
fun toInt (Int64 x) = Int Prim.int.(convert_s_i64 x)
fun quotRem (Int64 n) (Int64 d) = (Int64 Prim.i64.(div_s n d), Int64 Prim.i64.(rem_s n d))
fun divMod (Int64 n) (Int64 d) = (Int64 Prim.i64.(div_s n d), Int64 Prim.i64.(rem_s n d))
}
trait Bitwise (Int64.Int64) = {
import Int64._
fun (Int64 x) & (Int64 y) = Int64 Prim.i64.(and x y)
fun (Int64 x) ^ (Int64 y) = Int64 Prim.i64.(xor x y)
fun (Int64 x) | (Int64 y) = Int64 Prim.i64.(or x y)
fun ~ (Int64 x) = Int64 Prim.i64.(complement x)
}
trait Bitwidth (Int64.Int64) = {
import Int64._
val NoBits = Int64.(fromInt 0)
val AllBits = Int64.(fromInt 0xffff_ffff_ffff_ffff)
val Bitwidth = Nat.(fromInt 64)
}
trait Eq (Int64.Int64) = {
import Int64._
fun (Int64 x) == (Int64 y) = _PrimBoolean.(fromI64 Prim.i64.(eq x y))
fun (Int64 x) != (Int64 y) = _PrimBoolean.(fromI64 Prim.i64.(ne x y))
}
trait Ord (Int64.Int64) = {
import Int64._
fun (Int64 x) < (Int64 y) = _PrimBoolean.(fromI64 Prim.i64.(lt_s x y))
fun (Int64 x) > (Int64 y) = _PrimBoolean.(fromI64 Prim.i64.(gt_s x y))
fun (Int64 x) <= (Int64 y) = _PrimBoolean.(fromI64 Prim.i64.(le_s x y))
fun (Int64 x) >= (Int64 y) = _PrimBoolean.(fromI64 Prim.i64.(ge_s x y))
// Fast min and max without branching
fun min (x: Int64) (y: Int64) = y ^ ((x ^ y) & (-(x < y)))
fun max (x: Int64) (y: Int64) = x ^ ((x ^ y) & (-(x < y)))
}
trait Bounded (Int64.Int64) = {
import Int64._
val Min = Int64.(fromInt (-9223372036854775808))
val Max = Int64.(fromInt 9223372036854775807)
}
////////////////////////////////////////////////////////////////
// 32-bit unsigned integers.
////////////////////////////////////////////////////////////////
trait Nat32 =
(Num Nat32.Nat32)
with (Integral Nat32.Nat32)
with (Eq Nat32.Nat32)
with (Ord Nat32.Nat32)
with (Enum Nat32.Nat32)
with (Bounded Nat32.Nat32)
with (Bitwise Nat32.Nat32)
with (Bitwidth Nat32.Nat32) with {
trait Nat32 (rep: Prim.i32.I32)
}
trait Num (Nat32.Nat32) = {
import Nat32._
import (Ord Nat32.Nat32)._
import (Eq Nat32.Nat32)._
fun ((Nat32 x)) + (Nat32 y) = Nat32 Prim.i32.(add x y)
fun ((x' @ Nat32 x)) - (y' @ Nat32 y) where (y' <= x') = Nat32 Prim.i32.(sub x y)
fun ((Nat32 x)) * (Nat32 y) = Nat32 Prim.i32.(mul x y)
fun ((Nat32 x)) / (y' @ Nat32 y) where (y' != 0) = Nat32 Prim.i32.(div_u x y)
fun ((Nat32 x)) % (y' @ Nat32 y) where (y' != 0) = Nat32 Prim.i32.(rem_u x y)
fun - (x: Nat32) = x
fun fromInt (Int x) = Nat32 Prim.i32.(wrapBig x)
fun abs (x: Nat32) = x
}
trait Integral (Nat32.Nat32) = {
import Nat32._
import Int._
fun toInt (Nat32 x) = Int Prim.int.(convert_u_i32 x)
fun quotRem (Nat32 n) (Nat32 d) = (Nat32 Prim.i32.(div_u n d), Nat32 Prim.i32.(rem_u n d))
fun divMod (Nat32 n) (Nat32 d) = (Nat32 Prim.i32.(div_u n d), Nat32 Prim.i32.(rem_u n d))
}
trait Bitwise (Nat32.Nat32) = {
import Nat32._
fun (Nat32 x) & (Nat32 y) = Nat32 Prim.i32.(and x y)
fun (Nat32 x) ^ (Nat32 y) = Nat32 Prim.i32.(xor x y)
fun (Nat32 x) | (Nat32 y) = Nat32 Prim.i32.(or x y)
fun ~ (Nat32 x) = Nat32 Prim.i32.(complement x)
}
trait Bitwidth (Nat32.Nat32) = {
import Nat32._
val NoBits = Int64.(fromInt 0)
val AllBits = Int64.(fromInt 0xffff_ffff)
val Bitwidth = Nat.(fromInt 32)
}
trait Eq (Nat32.Nat32) = {
import Nat32._
fun (Nat32 x) == (Nat32 y) = _PrimBoolean.(fromI32 Prim.i32.(eq x y))
fun (Nat32 x) != (Nat32 y) = _PrimBoolean.(fromI32 Prim.i32.(ne x y))
}
trait Ord (Nat32.Nat32) = {
import Nat32._
fun (Nat32 x) < (Nat32 y) = _PrimBoolean.(fromI32 Prim.i32.(lt_u x y))
fun (Nat32 x) > (Nat32 y) = _PrimBoolean.(fromI32 Prim.i32.(gt_u x y))
fun (Nat32 x) <= (Nat32 y) = _PrimBoolean.(fromI32 Prim.i32.(le_u x y))
fun (Nat32 x) >= (Nat32 y) = _PrimBoolean.(fromI32 Prim.i32.(ge_u x y))
}
trait Bounded (Nat32.Nat32) = {
import Nat32._
val Min = Int32.(fromInt 0)
val Max = Nat32.(fromInt 4294967295)
}
////////////////////////////////////////////////////////////////
// 64-bit unsigned integers.
////////////////////////////////////////////////////////////////
trait Nat64
= (Num Nat64.Nat64)
with (Integral Nat64.Nat64)
with (Eq Nat64.Nat64)
with (Ord Nat64.Nat64)
with (Enum Nat64.Nat64)
with (Bounded Nat64.Nat64)
with (Bitwise Nat64.Nat64)
with (Bitwidth Nat64.Nat64) with {
trait Nat64 (rep: Prim.i64.I64)
}
trait Num (Nat64.Nat64) = {
import Nat64._
import Int.Int
import (Ord Nat64.Nat64)._
import (Eq Nat64.Nat64)._
fun ((Nat64 x)) + (Nat64 y) = Nat64 Prim.i64.(add x y)
fun ((x' @ Nat64 x)) - (y' @ Nat64 y) where (y' <= x') = Nat64 Prim.i64.(sub x y)
fun ((Nat64 x)) * (Nat64 y) = Nat64 Prim.i64.(mul x y)
fun ((Nat64 x)) / (y' @ Nat64 y) where (y' != 0) = Nat64 Prim.i64.(div_u x y)
fun ((Nat64 x)) % (y' @ Nat64 y) where (y' != 0) = Nat64 Prim.i64.(rem_u x y)
fun - (x: Nat64) = x
fun fromInt (Int x) = Nat64 Prim.i64.(wrapBig x)
fun abs (x: Nat64) = x
}
trait Integral (Nat64.Nat64) = {
import Nat64._
import Int.Int
fun toInt (Nat64 x) = Int Prim.int.(convert_u_i64 x)
fun quotRem (Nat64 n) (Nat64 d) = (Nat64 Prim.i64.(div_u n d), Nat64 Prim.i64.(rem_u n d))
fun divMod (Nat64 n) (Nat64 d) = (Nat64 Prim.i64.(div_u n d), Nat64 Prim.i64.(rem_u n d))
}
trait Bitwise (Nat64.Nat64) = {
import Nat64._
fun (Nat64 x) & (Nat64 y) = Nat64 Prim.i64.(and x y)
fun (Nat64 x) ^ (Nat64 y) = Nat64 Prim.i64.(xor x y)
fun (Nat64 x) | (Nat64 y) = Nat64 Prim.i64.(or x y)
fun ~ (Nat64 x) = Nat64 Prim.i64.(complement x)
}
trait Bitwidth (Nat64.Nat64) = {
import Nat64._
val NoBits = Nat64.(fromInt 0)
val AllBits = Nat64.(fromInt 0xffff_ffff_ffff_ffff)
val Bitwidth = Nat.(fromInt 64)
}
trait Eq (Nat64.Nat64) = {
import Nat64._
fun (Nat64 x) == (Nat64 y) = _PrimBoolean.(fromI64 Prim.i64.(eq x y))
fun (Nat64 x) != (Nat64 y) = _PrimBoolean.(fromI64 Prim.i64.(ne x y))
}
trait Ord (Nat64.Nat64) = {
import Nat64._
fun (Nat64 x) < (Nat64 y) = _PrimBoolean.(fromI64 Prim.i64.(lt_u x y))
fun (Nat64 x) > (Nat64 y) = _PrimBoolean.(fromI64 Prim.i64.(gt_u x y))
fun (Nat64 x) <= (Nat64 y) = _PrimBoolean.(fromI64 Prim.i64.(le_u x y))
fun (Nat64 x) >= (Nat64 y) = _PrimBoolean.(fromI64 Prim.i64.(ge_u x y))
}
trait Bounded (Nat64.Nat64) = {
import Nat64._
val Min = Nat64.(fromInt 0)
val Max = Nat64.(fromInt 18446744073709551615)
}
trait Control = {
import Boolean._
fun match (x) ((k: _ -> _)) else {{o}} = {
fun go (Prim.i32._0) = o
fun go (_) = k x
go (Prim.functions.(defined k x))
}
fun match (x) ((k: _ -> _)) = k x
//fun match any (a) (a -> b) -> _
//fun match any (x) (k) -> {
// if x in dom k { k x }
// }
fun cond ((f: Boolean -> _)) else {{o}} = match True f else o
fun cond (f: Boolean -> _) = f True
fun repeat {e} while {c: Boolean} = {
e
while c e
}
fun while {c: Boolean} {{e}} = {
if c {
e
while c e
}
}
fun if (True) {{e}} = {
e
_
}
fun if (False) {{e}} = _
fun if (True) {e} else {{_}} = e
fun if (False) {_} else {{e}} = e
}
////////////////////////////////////////////////////////////////
// exports Tuple2 Tuple3 ..
////////////////////////////////////////////////////////////////
trait Tuples
= (Eq (_, _))
with (Ord (_, _))
with (Eq (_, _, _))
with (Ord (_, _, _))
with (Eq (_, _, _, _))
with (Ord (_, _, _, _))
with (Eq (_, _, _, _, _))
with (Ord (_, _, _, _, _))
with (Eq (_, _, _, _, _, _))
with (Ord (_, _, _, _, _, _))
with (Eq (_, _, _, _, _, _, _))
with (Ord (_, _, _, _, _, _, _))
with (Eq (_, _, _, _, _, _, _, _))
with (Ord (_, _, _, _, _, _, _, _)) with {
// Define some tuple types.
// Note we appear to overload the name Tuple, but really
// we are defining (Tuple _ _), (Tuple _ _ _), etc.
trait Tuple (_1) (_2)
trait Tuple (_1) (_2) (_3)
trait Tuple (_1) (_2) (_3) (_4)
trait Tuple (_1) (_2) (_3) (_4) (_5)
trait Tuple (_1) (_2) (_3) (_4) (_5) (_6)
trait Tuple (_1) (_2) (_3) (_4) (_5) (_6) (_7)
trait Tuple (_1) (_2) (_3) (_4) (_5) (_6) (_7) (_8)
// fst and snd are defined only for pairs
fun fst (a, b) = a
fun snd (a, b) = b
}
trait Eq (a, b) = {
import (Eq a, Eq b)._
import Boolean._
fun (a1, b1) == (a2, b2) = (a1 == a2) && (b1 == b2)
}
trait Ord (a, b) = {
import (Ord a, Ord b)._
import (Eq a, Eq b)._
import Boolean._
fun (a1, b1) < (a2, b2) = (a1 < a2) || ((a1 == a2) && (b1 < b2))
fun (a1, b1) > (a2, b2) = (a1 > a2) || ((a1 == a2) && (b1 > b2))
}
trait Eq (a, b, c) = {
import (Eq a, Eq b, Eq c)._
import Boolean._
fun (a1, b1, c1) == (a2, b2, c2) = ((a1 == a2) && (b1 == b2)) && (c1 == c2)
}
trait Ord (a, b, c) = {
import (Ord a, Ord b, Ord c)._
import (Eq a, Eq b, Eq c)._
import Boolean._
fun (a1, b1, c1) < (a2, b2, c2) = (a1 < a2) || ((a1 == a2) && ((b1, c1) < (b2, c2)))
fun (a1, b1, c1) > (a2, b2, c2) = (a1 > a2) || ((a1 == a2) && ((b1, c1) > (b2, c2)))
}
trait Option = {
trait None
trait Some (value: a)
trait Option (a) = None with (Some a)
fun (Option a) or (Option a) = Option a
fun (None) or (o) = o
fun (Some a) or (o) = Some a
fun (Option a) then (a -> Option b) = Option b
fun (None) then (f) = None
fun (Some a) then (f) = f a
fun unwrap (Option a) or {a} = a
fun unwrap (Some a) or {_} = a
fun unwrap (None) or {a} = a
fun unwrap (Option a) = a
fun unwrap (Some a) = a
}
// Result is like Either
trait Result = {
trait Ok (value: a)
trait Err (value: e)
trait Result (a) (e) = (Ok a) with (Err e)
fun (Result a e) then (a -> Result b e) = Result b e
fun (Ok a) then (f) = f a
fun (Err b) then (f) = Err b
fun (Result a e) and {Result b e} = Result b e
fun (Ok a) then {r} = r
fun (Err b) then {_} = Err b
fun (Result a e) else (e -> Result b e) = Result b e
fun (Ok a) else (_) = Ok a
fun (Err e) else (f) = f e
fun (Result a e) or {Result b e} = Result b e
fun (Ok a) else {_} = Ok a
fun (Err e) else {r} = r
}
trait List
= (Sequence (List.(List _)))
with (Functor (List.(List _)))
with (Foldable (List.(List _)))
with (Ord (List.(List _)))
with (Eq (List.(List _))) with {
// Constructors
trait Nil
trait Cons (head: a) (tail: List a)