-
Notifications
You must be signed in to change notification settings - Fork 109
/
Copy pathprelude.dx
2606 lines (2064 loc) · 77 KB
/
prelude.dx
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
'# Dex prelude
'Runs before every Dex program unless an alternative is provided with `--prelude`.
'## Essentials
### Primitive Types
Type = %TyKind()
Heap = %HeapType()
Effects = %EffKind()
Int64 = %Int64()
Int32 = %Int32()
Float64 = %Float64()
Float32 = %Float32()
Word8 = %Word8()
Word32 = %Word32()
Word64 = %Word64()
Byte = Word8
Char = Byte
RawPtr : Type = %Word8Ptr()
Int = Int32
Float = Float32
def the(a:Type, x:a) -> a = x
interface Data(a:Type)
do_not_implement_this_interface_for_the_compiler_relies_on_the_invariant_it_protects : (a) -> a
'### Casting
@inline
def internal_cast(x:from) -> to given (from:Type, to:Type) =
%cast(to, x)
def unsafe_coerce(x:from) -> to given (from|Data, to|Data) = %unsafeCoerce(to, x)
def uninitialized_value() -> a given (a|Data) = %garbageVal(a)
def f64_to_f(x: Float64) -> Float = internal_cast x
def f32_to_f(x: Float32) -> Float = internal_cast x
def f_to_f64(x: Float) -> Float64 = internal_cast x
def f_to_f32(x: Float) -> Float32 = internal_cast x
def i64_to_i(x: Int64) -> Int = internal_cast x
def i32_to_i(x: Int32) -> Int = internal_cast x
def w8_to_i(x: Word8) -> Int = internal_cast x
def i_to_i64(x: Int) -> Int64 = internal_cast x
def i_to_i32(x: Int) -> Int32 = internal_cast x
def i_to_w8(x: Int) -> Word8 = internal_cast x
def i_to_w32(x: Int) -> Word32 = internal_cast x
def i_to_w64(x: Int) -> Word64 = internal_cast x
def w32_to_w64(x: Word32)-> Word64 = internal_cast x
def i_to_f(x:Int) -> Float = internal_cast x
def f_to_i(x:Float) -> Int = internal_cast x
def raw_ptr_to_i64(x:RawPtr) -> Int64 = internal_cast x
Nat = %Nat()
NatRep = Word32
def nat_to_rep(x : Nat) -> NatRep = %projNewtype(x)
@inline
def rep_to_nat(x : NatRep) -> Nat = %NatCon(x)
def n_to_w8(x: Nat) -> Word8 = nat_to_rep x | internal_cast
def n_to_w32(x: Nat) -> Word32 = nat_to_rep x | internal_cast
def n_to_w64(x: Nat) -> Word64 = nat_to_rep x | internal_cast
def n_to_i32(x: Nat) -> Int32 = nat_to_rep x | internal_cast
def n_to_i64(x: Nat) -> Int64 = nat_to_rep x | internal_cast
def n_to_f32(x: Nat) -> Float32 = nat_to_rep x | internal_cast
def n_to_f64(x: Nat) -> Float64 = nat_to_rep x | internal_cast
def n_to_f(x: Nat) -> Float = nat_to_rep x | internal_cast
def w8_to_n(x : Word8) -> Nat = internal_cast x | rep_to_nat
def w32_to_n(x : Word32) -> Nat = internal_cast x | rep_to_nat
@inline
def w64_to_n(x : Word64) -> Nat = internal_cast x | rep_to_nat
def i32_to_n(x : Int32) -> Nat = internal_cast x | rep_to_nat
def i64_to_n(x : Int64) -> Nat = internal_cast x | rep_to_nat
def f32_to_n(x : Float32) -> Nat = internal_cast x | rep_to_nat
def f64_to_n(x : Float64) -> Nat = internal_cast x | rep_to_nat
def f_to_n(x : Float) -> Nat = internal_cast x | rep_to_nat
interface FromUnsignedInteger(a:Type)
from_unsigned_integer : (Word64) -> a
instance FromUnsignedInteger(Word8)
def from_unsigned_integer(x) = internal_cast x
instance FromUnsignedInteger(Word32)
def from_unsigned_integer(x) = internal_cast x
instance FromUnsignedInteger(Word64)
def from_unsigned_integer(x) = x
instance FromUnsignedInteger(Int32)
def from_unsigned_integer(x) = internal_cast x
instance FromUnsignedInteger(Int64)
def from_unsigned_integer(x) = internal_cast x
instance FromUnsignedInteger(Float32)
def from_unsigned_integer(x) = internal_cast x
instance FromUnsignedInteger(Float64)
def from_unsigned_integer(x) = internal_cast x
instance FromUnsignedInteger(Nat)
def from_unsigned_integer(x) = w64_to_n(x)
interface FromInteger(a:Type)
from_integer : (Int64) -> a
instance FromInteger(Float32)
def from_integer(x) = internal_cast x
instance FromInteger(Int32)
def from_integer(x) = internal_cast x
instance FromInteger(Float64)
def from_integer(x) = internal_cast x
instance FromInteger(Int64)
def from_integer(x) = x
'## Bitwise operations
interface Bits(a:Type)
(.<<.) : (a, Int) -> a
(.>>.) : (a, Int) -> a
(.|.) : (a, a) -> a
(.&.) : (a, a) -> a
(.^.) : (a, a) -> a
instance Bits(Word8)
def (.<<.)(x, y) = %shl(x, i_to_w8 y)
def (.>>.)(x, y) = %shr(x, i_to_w8 y)
def (.|.)(x, y) = %or( x, y)
def (.&.)(x, y) = %and(x, y)
def (.^.)(x, y) = %xor(x, y)
instance Bits(Word32)
def (.<<.)(x, y) = %shl(x, i_to_w32 y)
def (.>>.)(x, y) = %shr(x, i_to_w32 y)
def (.|.)(x, y) = %or( x, y)
def (.&.)(x, y) = %and(x, y)
def (.^.)(x, y) = %xor(x, y)
instance Bits(Word64)
def (.<<.)(x, y) = %shl(x, i_to_w64 y)
def (.>>.)(x, y) = %shr(x, i_to_w64 y)
def (.|.)(x, y) = %or( x ,y)
def (.&.)(x, y) = %and(x ,y)
def (.^.)(x, y) = %xor(x ,y)
def low_word( x : Word64) -> Word32 = internal_cast(x .>>. 32)
def high_word(x : Word64) -> Word32 = internal_cast(x)
'### Basic Arithmetic
#### Add
Things that can be added.
This defines the `Add` [group](https://en.wikipedia.org/wiki/Group_(mathematics)) and its operators.
interface Add(a|Data)
(+) : (a, a) -> a
zero : a
interface Sub(a|Add)
(-) : (a, a) -> a
instance Add(Float64)
def (+)(x, y) = %fadd(x, y)
zero = 0
instance Sub(Float64)
def (-)(x, y) = %fsub(x, y)
instance Add(Float32)
def (+)(x, y) = %fadd(x, y)
zero = 0
instance Sub(Float32)
def (-)(x, y) = %fsub(x, y)
instance Add(Int64)
def (+)(x, y) = %iadd(x, y)
zero = 0
instance Sub(Int64)
def (-)(x, y) = %isub(x, y)
instance Add(Int32)
def (+)(x, y) = %iadd(x, y)
zero = 0
instance Sub(Int32)
def (-)(x, y) = %isub(x, y)
instance Add(Word8)
def (+)(x, y) = %iadd(x, y)
zero = 0
instance Sub(Word8)
def (-)(x, y) = %isub(x, y)
instance Add(Word32)
def (+)(x, y) = %iadd(x, y)
zero = 0
instance Sub(Word32)
def (-)(x, y) = %isub(x, y)
instance Add(Word64)
def (+)(x, y) = %iadd(x, y)
zero = 0
instance Sub(Word64)
def (-)(x, y) = %isub(x, y)
instance Add(Nat)
def (+)(x, y) = rep_to_nat %iadd(nat_to_rep x, nat_to_rep y)
zero = 0
instance Add(())
def (+)(x, y) = ()
zero = ()
instance Sub(())
def (-)(x, y) = ()
'#### Mul
Things that can be multiplied.
This defines the `Mul` [Monoid](https://en.wikipedia.org/wiki/Monoid), and its operator.
interface Mul(a|Data)
(*) : (a, a) -> a
one : a
instance Mul(Float64)
def (*)(x, y) = %fmul(x, y)
one = f_to_f64 1.0
instance Mul(Float32)
def (*)(x, y) = %fmul(x, y)
one = f_to_f32 1.0
instance Mul(Int64)
def (*)(x, y) = %imul(x, y)
one = 1
instance Mul(Int32)
def (*)(x, y) = %imul(x, y)
one = 1
instance Mul(Word8)
def (*)(x, y) = %imul(x, y)
one = 1
instance Mul(Word32)
def (*)(x, y) = %imul(x, y)
one = 1
instance Mul(Word64)
def (*)(x, y) = %imul(x, y)
one = 1
instance Mul(Nat)
def(*)(x, y) = rep_to_nat %imul(nat_to_rep x, nat_to_rep y)
one = 1
instance Mul(())
def (*)(x, y) = ()
one = ()
'#### Integral
Integer-like things.
interface Integral(a:Type)
idiv : (a,a)->a
rem : (a,a)->a
instance Integral(Int64)
def idiv(x, y) = %idiv(x, y)
def rem(x, y) = %irem(x, y)
instance Integral(Int32)
def idiv(x, y) = %idiv(x, y)
def rem(x, y) = %irem(x, y)
instance Integral(Word8)
def idiv(x, y) = %idiv(x, y)
def rem(x, y) = %irem(x, y)
instance Integral(Word32)
def idiv(x, y) = %idiv(x, y)
def rem(x, y) = %irem(x, y)
instance Integral(Word64)
def idiv(x, y) = %idiv(x, y)
def rem(x, y) = %irem(x, y)
instance Integral(Nat)
def idiv(x, y) = rep_to_nat %idiv(nat_to_rep x, (nat_to_rep y))
def rem(x, y) = rep_to_nat %irem(nat_to_rep x, (nat_to_rep y))
'#### Fractional
Rational-like things.
Includes floating point and two field rational representations.
interface Fractional(a:Type)
divide : (a, a) -> a
instance Fractional(Float64)
def divide(x, y) = %fdiv(x, y)
instance Fractional(Float32)
def divide(x, y) = %fdiv(x, y)
'## Index set interface and instances
interface Ix(n|Data)
size' : () -> Nat
ordinal : (n) -> Nat
unsafe_from_ordinal : (Nat) -> n
def size(n:Type|Ix) -> Nat = size'(n=n)
def Fin(n:Nat) -> Type = %Fin(n)
# version of subtraction on Nats that clamps at zero
def (-|)(x: Nat, y:Nat) -> Nat =
x' = nat_to_rep x
y' = nat_to_rep y
requires_clamp = %ilt(x', y')
rep_to_nat %select(requires_clamp, 0::NatRep, (%isub(x', y')))
def unsafe_nat_diff(x:Nat, y:Nat) -> Nat =
x' = nat_to_rep x
y' = nat_to_rep y
rep_to_nat %isub(x', y')
# TODO: need to a way to indicate constructor as private
struct RangeFrom(i:q) given (q:Type) = val : Nat
struct RangeFromExc(i:q) given (q:Type) = val : Nat
struct RangeTo(i:q) given (q:Type) = val : Nat
struct RangeToExc(i:q) given (q:Type) = val : Nat
instance Ix(RangeFrom i) given (q|Ix, i:q)
def size'() = unsafe_nat_diff(size q, ordinal i)
def ordinal(j) = j.val
def unsafe_from_ordinal(j) = RangeFrom(j)
instance Ix(RangeFromExc i) given (q|Ix, i:q)
def size'() = unsafe_nat_diff(size q, ordinal i + 1)
def ordinal(j) = j.val
def unsafe_from_ordinal(j) = RangeFromExc(j)
instance Ix(RangeTo i) given (q|Ix, i:q)
def size'() = ordinal i + 1
def ordinal(j) = j.val
def unsafe_from_ordinal(j) = RangeTo(j)
instance Ix(RangeToExc i) given (q|Ix, i:q)
def size'() = ordinal i
def ordinal(j) = j.val
def unsafe_from_ordinal(j) = RangeToExc(j)
instance Ix(())
def size'() = 1
def ordinal(_) = 0
def unsafe_from_ordinal(_) = ()
def iota(n:Type|Ix) -> n=>Nat = for i. ordinal i
'## Arithmetic instances for table types
instance Add(n=>a) given (a|Add, n|Ix)
def (+)(xs, ys) = for i. xs[i] + ys[i]
zero = for _. zero
instance Sub(n=>a) given (a|Sub, n|Ix)
def (-)(xs, ys) = for i. xs[i] - ys[i]
instance Add((i:n) => RangeFrom i => a) given (a|Add, n|Ix) # Upper triangular tables
def (+)(xs, ys) = for i. xs[i] + ys[i]
zero = for _. zero
instance Sub((i:n) => RangeFrom i => a) given (a|Sub, n|Ix) # Upper triangular tables
def (-)(xs, ys) = for i. xs[i] - ys[i]
instance Add((i:n) => RangeTo i => a) given (a|Add, n|Ix) # Lower triangular tables
def (+)(xs, ys) = for i. xs[i] + ys[i]
zero = for _. zero
instance Sub((i:n) => RangeTo i => a) given (a|Sub, n|Ix) # Lower triangular tables
def (-)(xs, ys) = for i. xs[i] - ys[i]
instance Add((i:n) => RangeToExc i => a) given (a|Add, n|Ix)
def (+)(xs, ys) = for i. xs[i] + ys[i]
zero = for _. zero
instance Sub((i:n) => RangeToExc i => a) given (a|Sub, n|Ix)
def (-)(xs, ys) = for i. xs[i] - ys[i]
instance Add((i:n) => RangeFromExc i => a) given (a|Add, n|Ix)
def (+)(xs, ys) = for i. xs[i] + ys[i]
zero = for _. zero
instance Sub((i:n) => RangeFromExc i => a) given (a|Sub, n|Ix)
def (-)(xs, ys) = for i. xs[i] - ys[i]
instance Mul(n=>a) given (a|Mul, n|Ix)
def (*)(xs, ys) = for i. xs[i] * ys[i]
one = for _. one
'## Basic polymorphic functions and types
def fst(pair:(a, b)) -> a given (a:Type, b:Type) = pair.0
def snd(pair:(a, b)) -> b given (a:Type, b:Type) = pair.1
def swap(pair:(a, b)) -> (b, a) given (a:Type, b:Type) =
(x, y) = pair
(y, x)
instance Add((a, b)) given (a|Add, b|Add)
def (+)(x, y) =
(x1, x2) = x
(y1, y2) = y
(x1 + y1, x2 + y2)
zero = (zero, zero)
instance Sub((a, b)) given (a|Sub, b|Sub)
def(-)(x, y) =
(x1, x2) = x
(y1, y2) = y
(x1 - y1, x2 - y2)
instance Ix((a, b)) given (a|Ix, b|Ix)
def size'() = size a * size b
def ordinal(pair) =
(i, j) = pair
(ordinal i * size b) + ordinal j
def unsafe_from_ordinal(o) =
bs = size b
(unsafe_from_ordinal(n=a, idiv(o, bs)), unsafe_from_ordinal(n=b, rem(o, bs)))
instance Ix((a, b, c)) given (a|Ix, b|Ix, c|Ix)
def size'() = size a * size b * size c
def ordinal(tup) =
(i, j, k) = tup
ordinal((i,(j,k)))
def unsafe_from_ordinal(o) =
(i, (j, k)) = unsafe_from_ordinal(n=(a,(b,c))::Type, o)
(i, j, k)
instance Ix((a, b, c, d)) given (a|Ix, b|Ix, c|Ix, d|Ix)
def size'() = size a * size b * size c * size d
def ordinal(tup) =
(i, j, k, m) = tup
ordinal((i,(j,(k,m))))
def unsafe_from_ordinal(o) =
(i, (j, (k, m))) = unsafe_from_ordinal(n=(a,(b,(c,d)))::Type, o)
(i, j, k, m)
'## Vector spaces
interface VSpace(a|Add|Sub)
(.*) : (Float, a) -> a
def (*.)(v:a, s:Float) -> a given (a|VSpace) = s .* v
def (/)( v:a, s:Float) -> a given (a|VSpace) = divide(1.0, s) .* v
def neg( v:a) -> a given (a|VSpace) = (-1.0) .* v
instance VSpace(Float)
def (.*)(x, y) = x * y
instance VSpace(n=>a) given (a|VSpace, n|Ix)
def (.*)(s, xs) = for i. s .* xs[i]
instance VSpace((a, b)) given (a|VSpace, b|VSpace)
def (.*)(s, pair) =
(x, y) = pair
(s .* x, s .* y)
instance VSpace((i:n) => RangeTo i => a) given (n|Ix, a|VSpace)
def (.*)(s, xs) = for i. s .* xs[i]
instance VSpace((i:n) => RangeFrom i => a) given (n|Ix, a|VSpace)
def (.*)(s, xs) = for i. s .* xs[i]
instance VSpace((i:n) => RangeToExc i => a) given (n|Ix, a|VSpace)
def (.*)(s, xs) = for i. s .* xs[i]
instance VSpace((i:n) => RangeFromExc i => a) given (n|Ix, a|VSpace)
def (.*)(s, xs) = for i. s .* xs[i]
instance VSpace(())
def (.*)(_, _) = ()
'## Boolean type
enum Bool =
False
True
def b_to_w8(x:Bool) -> Word8 = %dataConTag(x)
def w8_to_b(x:Word8) -> Bool = %toEnum(Bool, x)
def (&&)(x:Bool, y:Bool) -> Bool =
x' = b_to_w8 x
y' = b_to_w8 y
w8_to_b $ %and(x', y')
def (||)(x:Bool, y:Bool) -> Bool =
x' = b_to_w8 x
y' = b_to_w8 y
w8_to_b $ %or(x', y')
def not(x:Bool) -> Bool =
x' = b_to_w8 x
w8_to_b $ %not(x')
'## More Boolean operations
TODO: move these with the others?
# Can't use `%select` because it lowers to `ISelect`, which requires
# `a` to be a `BaseTy`.
def select(p:Bool, x:a, y:a) -> a given (a:Type) =
case p of
True -> x
False -> y
def b_to_i(x:Bool) -> Int = w8_to_i(b_to_w8 x)
def b_to_n(x:Bool) -> Nat = w8_to_n(b_to_w8 x)
def b_to_f(x:Bool) -> Float = i_to_f(b_to_i x)
'## Ordering
TODO: move this down to with `Ord`?
enum Ordering =
LT
EQ
GT
def o_to_w8(x:Ordering) -> Word8 = %dataConTag(x)
'## Sum types
A [sum type, or tagged union](https://en.wikipedia.org/wiki/Tagged_union) can hold values from a fixed set of types, distinguished by tags.
For those familiar with the C language, they can be though of as a combination of an `enum` with a `union`.
Here we define several basic kinds, and some operators on them.
enum Maybe(a:Type) =
Nothing
Just(a)
def is_nothing(x:Maybe a) -> Bool given (a:Type) =
case x of
Nothing -> True
Just(_) -> False
def is_just(x:Maybe a) -> Bool given (a:Type) = not $ is_nothing x
def maybe(d:b, f:(a)->b, x:Maybe a) -> b given (a:Type, b:Type) =
case x of
Nothing -> d
Just(x') -> f x'
enum Either(a:Type, b:Type) =
Left(a)
Right(b)
instance Ix(Either(a, b)) given (a|Ix, b|Ix)
def size'() = size a + size b
def ordinal(i) = case i of
Left(ai) -> ordinal ai
Right(bi) -> ordinal bi + size a
def unsafe_from_ordinal(o) =
as = nat_to_rep $ size a
o' = nat_to_rep o
# TODO: Reshuffle the prelude to be able to use (<) here
case w8_to_b $ %ilt(o', as) of
True -> Left $ unsafe_from_ordinal(n=a, o)
# TODO: Reshuffle the prelude to be able to use `diff_nat` here
False -> Right $ unsafe_from_ordinal(n=b, rep_to_nat (%isub(o', as)))
'## Subtraction on Nats
# TODO: think more about the right API here
def unsafe_i_to_n(x:Int) -> Nat =
rep_to_nat $ internal_cast x
def n_to_i(x:Nat) -> Int =
internal_cast (nat_to_rep x)
def i_to_n(x:Int) -> Maybe Nat =
if w8_to_b $ %ilt(x, 0::Int)
then Nothing
else Just $ unsafe_i_to_n x
'### Monoid
A [monoid](https://en.wikipedia.org/wiki/Monoid) is a things that have an associative binary operator and an identity element.
It includes:
- Addition and Multiplication of Numbers
- Boolean Logic
- Concatenation of Lists (including strings)
Monoids support `fold` operations, and similar.
interface Monoid(a|Data)
mempty : a
(<>) : (a, a) -> a
instance Monoid(n=>a) given (a|Monoid, n|Ix)
mempty = for i. mempty
def (<>)(x, y) = for i. x[i] <> y[i]
named-instance AndMonoid : Monoid(Bool)
mempty = True
def (<>)(x, y) = x && y
named-instance OrMonoid : Monoid(Bool)
mempty = False
def (<>)(x, y) = x || y
named-instance AddMonoid(a|Add) -> Monoid(a)
mempty = zero
def (<>)(x, y) = x + y
named-instance MulMonoid(a|Mul) -> Monoid(a)
mempty = one
def (<>)(x, y) = x * y
'## Effects
def Ref(r:Heap, a:Type|Data) -> Type = %Ref(r, a)
def get(ref:Ref h s) -> {State h} s given (h:Heap, s|Data) = %get(ref)
def (:=)(ref:Ref h s, x:s) -> {State h} () given (h:Heap, s|Data) = %put(ref, x)
def ask(ref:Ref h r) -> {Read h} r given (h:Heap, r|Data) = %ask(ref)
enum AccumMonoidData(h:Heap, w:Type) = UnsafeMkAccumMonoidData(b:Type, Monoid b)
interface AccumMonoid(h:Heap, w:Type)
getAccumMonoidData : AccumMonoidData(h, w)
instance AccumMonoid(h, n=>w) given (n|Ix, h:Heap, w:Type) (am:AccumMonoid(h, w))
getAccumMonoidData =
UnsafeMkAccumMonoidData(b, bm) = %applyMethod0(am)
UnsafeMkAccumMonoidData(b, bm)
def (+=)(ref:Ref h w, x:w) -> {Accum h} ()
given (h:Heap, w|Data) (am:AccumMonoid(h, w)) =
UnsafeMkAccumMonoidData(b, bm) = %applyMethod0(am)
empty = %applyMethod0(bm)
%mextend(ref, empty, \x:b y:b. %applyMethod1(bm, x, y), x)
def (!)(ref: Ref h (n=>a), i:n) -> Ref h a given (n|Ix, a|Data, h:Heap) = %indexRef(ref, i)
def fst_ref(ref: Ref h (a,b)) -> Ref h a given (b|Data, a|Data, h:Heap) = ref.0
def snd_ref(ref: Ref h (a,b)) -> Ref h b given (a|Data, b|Data, h:Heap) = ref.1
def run_reader(
init:r,
action:(given (h:Heap), Ref h r) -> {Read h|eff} a
) -> {|eff} a given (r|Data, a:Type, eff:Effects) =
def explicitAction(h':Heap, ref:Ref h' r) -> {Read h'|eff} a = action ref
%runReader(init, explicitAction)
def with_reader(
init:r,
action: (given (h:Heap), Ref(h,r)) -> {Read h|eff} a
) -> {|eff} a given (r|Data, a:Type, eff:Effects) =
run_reader(init, action)
def MonoidLifter(b:Type, w:Type) -> Type =
(given (h:Heap) (AccumMonoid(h, b))) ->> AccumMonoid(h, w)
named-instance mk_accum_monoid (given (h:Heap, w:Type), d:AccumMonoidData(h, w)) -> AccumMonoid(h, w)
getAccumMonoidData = d
def run_accum(
bm:Monoid b,
action: (given (h:Heap) (AccumMonoid(h, b)), Ref h w) -> {Accum h|eff} a
) -> {|eff} (a, w) given (a:Type, b:Type, w|Data, eff:Effects) (MonoidLifter(b,w)) =
empty = %applyMethod0(bm)
def explicitAction(h':Heap, ref:Ref h' w) -> {Accum h'|eff} a =
accumMonoidData : AccumMonoidData h' b = UnsafeMkAccumMonoidData b bm
accumBaseMonoid = mk_accum_monoid accumMonoidData
%explicitApply(action, h', accumBaseMonoid, ref)
%runWriter(empty, \x:b y:b. %applyMethod1(bm, x, y), explicitAction)
def yield_accum(
m:Monoid b,
action: (given (h:Heap) (AccumMonoid(h, b)), Ref h w) -> {Accum h|eff} a
) -> {|eff} w given (a:Type, b:Type, w|Data, eff:Effects) (MonoidLifter b w) =
snd $ run_accum(m, action)
def run_state(
init:s,
action: (given (h:Heap), Ref h s) -> {State h |eff} a
) -> {|eff} (a,s) given (a:Type, s|Data, eff:Effects) =
def explicitAction(h':Heap, ref:Ref h' s) -> {State h'|eff} a = action ref
%runState(init, explicitAction)
def with_state(
init:s,
action: (given (h:Heap), Ref h s) -> {State h |eff} a
) -> {|eff} a given (a:Type, s|Data, eff:Effects) =
fst $ run_state(init, action)
def yield_state(
init:s,
action: (given (h:Heap), Ref h s) -> {State h |eff} a
) -> {|eff} s given (a:Type, s|Data, eff:Effects) =
snd $ run_state(init, action)
def unsafe_io(
f:()->{IO|eff} a
) -> {|eff} a given (a:Type, eff:Effects) =
f' : (() -> {IO|eff} a) = \. f()
%runIO(f')
def unreachable() -> a given (a|Data) = unsafe_io \. %throwError(a)
'## Type classes
'### Eq and Ord
'#### Eq
Equatable.
Things that we can tell if they are equal or not to other things.
interface Eq(a|Data)
(==) : (a, a) -> Bool
def (/=)(x:a, y:a) -> Bool given (a|Eq) = not $ x == y
'#### Ord
Orderable / Comparable.
Things that can be place in a total order.
i.e. things that can be compared to other things to find if larger, smaller or equal in value.
'We take the standard false-hood and pretend that this applies to Floats, even though strictly speaking this not true as our floats follow [IEEE754](https://en.wikipedia.org/wiki/IEEE_754), and thus have `NaN < 1.0 == false` and `1.0 < NaN == false`.
interface Ord(a|Eq)
(>) : (a, a) -> Bool
(<) : (a, a) -> Bool
def (<=)(x:a, y:a) -> Bool given (a|Ord) = x<y || x==y
def (>=)(x:a, y:a) -> Bool given (a|Ord) = x>y || x==y
instance Eq(Float64)
def (==)(x, y) = w8_to_b $ %feq(x, y)
instance Eq(Float32)
def (==)(x, y) = w8_to_b $ %feq(x, y)
instance Eq(Int64)
def (==)(x, y) = w8_to_b $ %ieq(x, y)
instance Eq(Int32)
def (==)(x, y) = w8_to_b $ %ieq(x, y)
instance Eq(Word8)
def (==)(x, y) = w8_to_b $ %ieq(x, y)
instance Eq(Word32)
def (==)(x, y) = w8_to_b $ %ieq(x, y)
instance Eq(Word64)
def (==)(x, y) = w8_to_b $ %ieq(x, y)
instance Eq(Bool)
def (==)(x, y) = b_to_w8 x == b_to_w8 y
instance Eq(())
def (==)(_, _) = True
instance Eq(Either(a, b)) given (a|Eq, b|Eq)
def (==)(x, y) = case x of
Left(x) -> case y of
Left( y) -> x == y
Right(y) -> False
Right(x) -> case y of
Left( y) -> False
Right(y) -> x == y
instance Eq(Maybe a) given (a|Eq)
def (==)(x, y) = case x of
Just(x) -> case y of
Just(y) -> x == y
Nothing -> False
Nothing -> case y of
Just(y) -> False
Nothing -> True
instance Eq(RawPtr)
def (==)(x, y) = raw_ptr_to_i64 x == raw_ptr_to_i64 y
instance Ord(Float64)
def (>)(x, y) = w8_to_b $ %fgt(x, y)
def (<)(x, y) = w8_to_b $ %flt(x, y)
instance Ord(Float32)
def (>)(x, y) = w8_to_b $ %fgt(x, y)
def (<)(x, y) = w8_to_b $ %flt(x, y)
instance Ord(Int64)
def (>)(x, y) = w8_to_b $ %igt(x, y)
def (<)(x, y) = w8_to_b $ %ilt(x, y)
instance Ord(Int32)
def (>)(x, y) = w8_to_b $ %igt(x, y)
def (<)(x, y) = w8_to_b $ %ilt(x, y)
instance Ord(Word8)
def (>)(x, y) = w8_to_b $ %igt(x, y)
def (<)(x, y) = w8_to_b $ %ilt(x, y)
instance Ord(Word32)
def (>)(x, y) = w8_to_b $ %igt(x, y)
def (<)(x, y) = w8_to_b $ %ilt(x, y)
instance Ord(Word64)
def (>)(x, y) = w8_to_b $ %igt(x, y)
def (<)(x, y) = w8_to_b $ %ilt(x, y)
instance Ord(())
def (>)(x, y) = False
def (<)(x, y) = False
instance Eq((a, b)) given (a|Eq, b|Eq)
def (==)(p1, p2) =
(x1, y1) = p1
(x2, y2) = p2
x1 == x2 && y1 == y2
instance Ord((a, b)) given (a|Ord, b|Ord)
def (>)(p1, p2) =
(x1, y1) = p1
(x2, y2) = p2
x1 > x2 || (x1 == x2 && y1 > y2)
def (<)(p1, p2) =
(x1, y1) = p1
(x2, y2) = p2
x1 < x2 || (x1 == x2 && y1 < y2)
instance Eq(Ordering)
def (==)(x, y) = o_to_w8 x == o_to_w8 y
instance Eq(Nat)
def (==)(x, y) = nat_to_rep x == nat_to_rep y
instance Ord(Nat)
def (>)(x, y) = nat_to_rep x > nat_to_rep y
def (<)(x, y) = nat_to_rep x < nat_to_rep y
# TODO: we want Eq and Ord for all index sets, not just `Fin n`
instance Eq(Fin n) given (n:Nat)
def (==)(x, y) = ordinal x == ordinal y
instance Ord(Fin n) given (n:Nat)
def (>)(x, y) = ordinal x > ordinal y
def (<)(x, y) = ordinal x < ordinal y
instance Ix(Bool)
def size'() = 2
def ordinal(b) = case b of
False -> 0
True -> 1
def unsafe_from_ordinal(i) = i > 0
instance Ix(Maybe a) given (a|Ix)
def size'() = size a + 1
def ordinal(i) = case i of
Just(ai) -> ordinal ai
Nothing -> size a
def unsafe_from_ordinal(o) =
case o == size a of
False -> Just $ unsafe_from_ordinal(n=a, o)
True -> Nothing
interface NonEmpty(n|Ix)
first_ix : n
instance NonEmpty(())
first_ix = unsafe_from_ordinal(0)
instance NonEmpty(Bool)
first_ix = unsafe_from_ordinal 0
instance NonEmpty((a,b)) given (a|NonEmpty, b|NonEmpty)
first_ix = unsafe_from_ordinal 0
instance NonEmpty(Either(a,b)) given (a|NonEmpty, b|Ix)
first_ix = unsafe_from_ordinal 0
# The below instance is valid, but causes 'multiple candidate dictionaries'
# errors if both Left and Right are NonEmpty.
# instance NonEmpty (a|b) given {a b} [Ix a, NonEmpty b]
# first_ix = unsafe_from_ordinal _ 0
instance NonEmpty(Maybe a) given (a|Ix)
first_ix = unsafe_from_ordinal 0
'## Fencepost index sets
struct Post(segment:Type) =
val : Nat
instance Ix(Post segment) given (segment|Ix)
def size'() = size segment + 1
def ordinal(i) = i.val
def unsafe_from_ordinal(i) = Post(i)
def left_post(i:n) -> Post n given (n|Ix) =
unsafe_from_ordinal(n=Post n, ordinal i)
def right_post(i:n) -> Post n given (n|Ix) =
unsafe_from_ordinal(n=Post n, ordinal i + 1)
def left_fence(p:Post n) -> Maybe n given (n|Ix) =
ix = ordinal p
if ix == 0
then Nothing
else Just $ unsafe_from_ordinal(n=n, ix -| 1)
def right_fence(p:Post n) -> Maybe n given (n|Ix) =
ix = ordinal p
if ix == size n
then Nothing
else Just $ unsafe_from_ordinal(n=n, ix)
def last_ix() ->> n given (n|NonEmpty) =
unsafe_from_ordinal(unsafe_i_to_n(n_to_i(size n) - 1))
instance NonEmpty(Post n) given (n|Ix)
first_ix = unsafe_from_ordinal(n=Post n, 0)
def compare(x:a, y:a) -> Ordering given (a|Ord) =
if x < y
then LT
else if x == y
then EQ
else GT
instance Monoid(Ordering)
mempty = EQ
def (<>)(x, y) =
case x of
LT -> LT
GT -> GT
EQ -> y
instance Eq(n=>a) given (n|Ix, a|Eq)
def (==)(xs, ys) =
yield_accum AndMonoid \ref.
for i:n. ref += xs[i] == ys[i]
'## Subset class
interface Subset(subset:Type, superset:Type)
inject' : (subset) -> superset
project' : (superset) -> Maybe subset
unsafe_project' : (superset) -> subset
# wrappers with more helpful implicit arg names
def inject(x:from) -> to given (to:Type, from:Type) (Subset(from, to)) = inject'(x)
def project(x:from) -> Maybe to given (to:Type, from:Type) (Subset(to, from)) = project'(x)
def unsafe_project(x:from) -> to given (to:Type, from:Type) (Subset(to, from)) = unsafe_project'(x)
instance Subset(a, c) given (a:Type, b:Type, c:Type) (Subset(a, b), Subset(b, c))
def inject'(x) = inject $ inject(to=b, x)
def project'(x) = case project(to=b, x) of
Nothing -> Nothing
Just(y)-> project y
def unsafe_project'(x) = unsafe_project $ unsafe_project(to=b, x)
def unsafe_project_rangefrom(j:q) -> RangeFrom(i) given (q|Ix, i:q) =
RangeFrom unsafe_nat_diff(ordinal j, ordinal i)
instance Subset(RangeFrom(i), q) given (q|Ix, i:q)
def inject'(j) =
unsafe_from_ordinal $ j.val + ordinal i
def project'(j) =
j' = ordinal j
i' = ordinal i
if j' < i'
then Nothing
else Just $ RangeFrom $ unsafe_nat_diff(j', i')
def unsafe_project'(j) = RangeFrom unsafe_nat_diff(ordinal j, ordinal i)
instance Subset(RangeFromExc(i), q) given (q|Ix, i:q)
def inject'(j) = unsafe_from_ordinal $ j.val + ordinal i + 1
def project'(j) =
j' = ordinal j
i' = ordinal i
if j' <= i'
then Nothing
else Just $ RangeFromExc unsafe_nat_diff(j', i' + 1)
def unsafe_project'(j) =
RangeFromExc unsafe_nat_diff(ordinal j, ordinal i + 1)
instance Subset(RangeTo(i), q) given (q|Ix, i:q)
def inject'(j) = unsafe_from_ordinal j.val
def project'(j) =
j' = ordinal j
i' = ordinal i