rex-typesystem 3.9.12

Rex: A strongly-typed, pure, implicitly parallel functional programming language
Documentation
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
{- prelude typeclasses and instances

   this file is parsed and injected by typesystem with_prelude

   design note:
   - class methods define the public surface (the names users call)
   - instance bodies in this file attach those names to rust-backed prim_ intrinsics
   - prim_ is the only magic: it is the equivalent of haskell / ghc primops
-}

{- numeric hierarchy -}
class AdditiveMonoid a
    zero : a
    + : a -> a -> a

class MultiplicativeMonoid a
    one : a
    * : a -> a -> a

class Semiring a <= AdditiveMonoid a, MultiplicativeMonoid a

class AdditiveGroup a <= Semiring a
    negate : a -> a
    - : a -> a -> a

class Ring a <= AdditiveGroup a, MultiplicativeMonoid a

class Field a <= Ring a
    / : a -> a -> a

class Integral a
    % : a -> a -> a

{- equality and ordering -}
class Eq a
    == : a -> a -> bool
    != : a -> a -> bool

class Ord a <= Eq a
    cmp : a -> a -> i32
    < : a -> a -> bool
    <= : a -> a -> bool
    > : a -> a -> bool
    >= : a -> a -> bool

{- show printing -}
class Show a
    show : a -> string

{- default values -}
class Default a
    default : a

{- collection combinators -}
class Functor f
    map : (a -> b) -> f a -> f b

class Applicative f <= Functor f
    pure : a -> f a
    ap : f (a -> b) -> f a -> f b

class Monad m <= Applicative m
    {- Monad's core operation is "bind".

       We keep the argument order as (a -> m b) first, then (m a), to match
       the rest of Rex's collection API (map f xs, filter p xs, ...) and to
       map directly to the host intrinsic prim_flat_map without extra
       wrappers/allocations.
    -}
    bind : (a -> m b) -> m a -> m b

class Foldable t
    foldl : (b -> a -> b) -> b -> t a -> b
    foldr : (a -> b -> b) -> b -> t a -> b
    fold : (b -> a -> b) -> b -> t a -> b

class Filterable f <= Functor f
    filter : (a -> bool) -> f a -> f a
    filter_map : (a -> Option b) -> f a -> f b

class Sequence f <= Functor f, Foldable f
    take : i32 -> f a -> f a
    skip : i32 -> f a -> f a
    zip : f a -> f b -> f (a, b)
    unzip : f (a, b) -> (f a, f b)

class Alternative f <= Applicative f
    or_else : (f a -> f a) -> f a -> f a

{- Indexable needs two parameters: the container type and the element type. -}
class Indexable t a
    get : i32 -> t -> a

{- AdditiveMonoid instances -}
instance AdditiveMonoid string
    zero = prim_zero
    + = prim_add
instance AdditiveMonoid u8
    zero = prim_zero
    + = prim_add
instance AdditiveMonoid u16
    zero = prim_zero
    + = prim_add
instance AdditiveMonoid u32
    zero = prim_zero
    + = prim_add
instance AdditiveMonoid u64
    zero = prim_zero
    + = prim_add
instance AdditiveMonoid i8
    zero = prim_zero
    + = prim_add
instance AdditiveMonoid i16
    zero = prim_zero
    + = prim_add
instance AdditiveMonoid i32
    zero = prim_zero
    + = prim_add
instance AdditiveMonoid i64
    zero = prim_zero
    + = prim_add
instance AdditiveMonoid f32
    zero = prim_zero
    + = prim_add
instance AdditiveMonoid f64
    zero = prim_zero
    + = prim_add

{- MultiplicativeMonoid instances -}
instance MultiplicativeMonoid u8
    one = prim_one
    * = prim_mul
instance MultiplicativeMonoid u16
    one = prim_one
    * = prim_mul
instance MultiplicativeMonoid u32
    one = prim_one
    * = prim_mul
instance MultiplicativeMonoid u64
    one = prim_one
    * = prim_mul
instance MultiplicativeMonoid i8
    one = prim_one
    * = prim_mul
instance MultiplicativeMonoid i16
    one = prim_one
    * = prim_mul
instance MultiplicativeMonoid i32
    one = prim_one
    * = prim_mul
instance MultiplicativeMonoid i64
    one = prim_one
    * = prim_mul
instance MultiplicativeMonoid f32
    one = prim_one
    * = prim_mul
instance MultiplicativeMonoid f64
    one = prim_one
    * = prim_mul

{- Semiring instances -}
instance Semiring u8
instance Semiring u16
instance Semiring u32
instance Semiring u64
instance Semiring i8
instance Semiring i16
instance Semiring i32
instance Semiring i64
instance Semiring f32
instance Semiring f64

{- AdditiveGroup and Ring instances -}
instance AdditiveGroup i8 <= Semiring i8
    negate = prim_negate
    - = prim_sub
instance AdditiveGroup i16 <= Semiring i16
    negate = prim_negate
    - = prim_sub
instance AdditiveGroup i32 <= Semiring i32
    negate = prim_negate
    - = prim_sub
instance AdditiveGroup i64 <= Semiring i64
    negate = prim_negate
    - = prim_sub
instance AdditiveGroup f32 <= Semiring f32
    negate = prim_negate
    - = prim_sub
instance AdditiveGroup f64 <= Semiring f64
    negate = prim_negate
    - = prim_sub

instance Ring i8 <= AdditiveGroup i8
instance Ring i16 <= AdditiveGroup i16
instance Ring i32 <= AdditiveGroup i32
instance Ring i64 <= AdditiveGroup i64
instance Ring f32 <= AdditiveGroup f32
instance Ring f64 <= AdditiveGroup f64

{- Field instances -}
instance Field f32 <= Ring f32
    / = prim_div
instance Field f64 <= Ring f64
    / = prim_div

{- Integral instances -}
instance Integral u8
    % = prim_mod
instance Integral u16
    % = prim_mod
instance Integral u32
    % = prim_mod
instance Integral u64
    % = prim_mod
instance Integral i8
    % = prim_mod
instance Integral i16
    % = prim_mod
instance Integral i32
    % = prim_mod
instance Integral i64
    % = prim_mod

{- Eq instances -}
instance Eq u8
    == = prim_eq
    != = prim_ne
instance Eq u16
    == = prim_eq
    != = prim_ne
instance Eq u32
    == = prim_eq
    != = prim_ne
instance Eq u64
    == = prim_eq
    != = prim_ne
instance Eq i8
    == = prim_eq
    != = prim_ne
instance Eq i16
    == = prim_eq
    != = prim_ne
instance Eq i32
    == = prim_eq
    != = prim_ne
instance Eq i64
    == = prim_eq
    != = prim_ne
instance Eq f32
    == = prim_eq
    != = prim_ne
instance Eq f64
    == = prim_eq
    != = prim_ne
instance Eq bool
    == = prim_eq
    != = prim_ne
instance Eq string
    == = prim_eq
    != = prim_ne
instance Eq uuid
    == = prim_eq
    != = prim_ne
instance Eq datetime
    == = prim_eq
    != = prim_ne

instance Eq (List a) <= Eq a
    == = \xs ys ->
        match xs
            when [] ->
                (match ys
                    when [] -> true
                    when _ -> false)
            when x::xs1 ->
                (match ys
                    when y::ys1 -> if x == y then xs1 == ys1 else false
                    when [] -> false)
    != = \xs ys -> if xs == ys then false else true
instance Eq (Option a) <= Eq a
    == = \x y ->
        match x
            when Some a0 ->
                (match y
                    when Some b0 -> a0 == b0
                    when None -> false)
            when None ->
                (match y
                    when None -> true
                    when Some _ -> false)
    != = \x y -> if x == y then false else true
instance Eq (Array a) <= Eq a
    == = prim_array_eq
    != = prim_array_ne
instance Eq (Result a e) <= Eq a, Eq e
    == = \x y ->
        match x
            when Ok a0 ->
                (match y
                    when Ok b0 -> a0 == b0
                    when Err _ -> false)
            when Err e0 ->
                (match y
                    when Err e1 -> e0 == e1
                    when Ok _ -> false)
    != = \x y -> if x == y then false else true

{- Ord instances -}
instance Ord u8 <= Eq u8
    cmp = prim_cmp
    < = prim_lt
    <= = prim_le
    > = prim_gt
    >= = prim_ge
instance Ord u16 <= Eq u16
    cmp = prim_cmp
    < = prim_lt
    <= = prim_le
    > = prim_gt
    >= = prim_ge
instance Ord u32 <= Eq u32
    cmp = prim_cmp
    < = prim_lt
    <= = prim_le
    > = prim_gt
    >= = prim_ge
instance Ord u64 <= Eq u64
    cmp = prim_cmp
    < = prim_lt
    <= = prim_le
    > = prim_gt
    >= = prim_ge
instance Ord i8 <= Eq i8
    cmp = prim_cmp
    < = prim_lt
    <= = prim_le
    > = prim_gt
    >= = prim_ge
instance Ord i16 <= Eq i16
    cmp = prim_cmp
    < = prim_lt
    <= = prim_le
    > = prim_gt
    >= = prim_ge
instance Ord i32 <= Eq i32
    cmp = prim_cmp
    < = prim_lt
    <= = prim_le
    > = prim_gt
    >= = prim_ge
instance Ord i64 <= Eq i64
    cmp = prim_cmp
    < = prim_lt
    <= = prim_le
    > = prim_gt
    >= = prim_ge
instance Ord f32 <= Eq f32
    cmp = prim_cmp
    < = prim_lt
    <= = prim_le
    > = prim_gt
    >= = prim_ge
instance Ord f64 <= Eq f64
    cmp = prim_cmp
    < = prim_lt
    <= = prim_le
    > = prim_gt
    >= = prim_ge
instance Ord string <= Eq string
    cmp = prim_cmp
    < = prim_lt
    <= = prim_le
    > = prim_gt
    >= = prim_ge

{- Show instances -}
instance Show bool
    show = prim_show
instance Show u8
    show = prim_show
instance Show u16
    show = prim_show
instance Show u32
    show = prim_show
instance Show u64
    show = prim_show
instance Show i8
    show = prim_show
instance Show i16
    show = prim_show
instance Show i32
    show = prim_show
instance Show i64
    show = prim_show
instance Show f32
    show = prim_show
instance Show f64
    show = prim_show
instance Show string
    show = prim_show
instance Show uuid
    show = prim_show
instance Show datetime
    show = prim_show

{- Default instances -}
instance Default bool
    default = false
instance Default u8
    default = prim_zero
instance Default u16
    default = prim_zero
instance Default u32
    default = prim_zero
instance Default u64
    default = prim_zero
instance Default i8
    default = prim_zero
instance Default i16
    default = prim_zero
instance Default i32
    default = prim_zero
instance Default i64
    default = prim_zero
instance Default f32
    default = prim_zero
instance Default f64
    default = prim_zero
instance Default string
    default = prim_zero
instance Default (List a)
    default = []
instance Default (Array a)
    default = prim_array_from_list []
instance Default (Option a)
    default = None
instance Default (Result a e) <= Default a
    default = Ok default

instance Show (List a) <= Show a
    show = \xs ->
        match xs
            when [] -> "[]"
            when x::xs1 ->
                let
                    step = \out y -> out + ", " + show y
                in
                    "[" + foldl step (show x) xs1 + "]"

instance Show (Array a) <= Show a
    show = \xs ->
        let
            step = \out x ->
                if out == "<array "
                    then out + show x
                    else out + ", " + show x,
            out = foldl step "<array " xs
        in
            out + ">"

instance Show (Option a) <= Show a
    show = \x ->
        match x
            when Some a0 -> "Some(" + show a0 + ")"
            when None -> "None"

instance Show (Result a e) <= Show a, Show e
    show = \x ->
        match x
            when Ok a0 -> "Ok(" + show a0 + ")"
            when Err e0 -> "Err(" + show e0 + ")"

{- Functor / Applicative / Monad / Foldable / Filterable / Sequence / Alternative instances -}
instance Functor List
    map = prim_map
instance Functor Option
    map = prim_map
instance Functor Array
    map = prim_map
instance Functor (Result e)
    map = prim_map

instance Applicative List <= Functor List
    pure = \x -> [x]
    ap = \ff xx -> prim_flat_map (\f -> prim_map f xx) ff
instance Applicative Option <= Functor Option
    pure = \x -> Some x
    ap = \ff xx ->
        match ff
            when Some f -> map f xx
            when None -> None
instance Applicative Array <= Functor Array
    pure = prim_array_singleton
    ap = \ff xx -> prim_flat_map (\f -> prim_map f xx) ff
instance Applicative (Result e) <= Functor (Result e)
    pure = \x -> Ok x
    ap = \rf rx ->
        match rf
            when Ok f -> map f rx
            when Err err -> Err err

instance Monad List <= Applicative List
    bind = prim_flat_map
instance Monad Option <= Applicative Option
    bind = prim_flat_map
instance Monad Array <= Applicative Array
    bind = prim_flat_map
instance Monad (Result e) <= Applicative (Result e)
    bind = prim_flat_map

instance Foldable List
    foldl = prim_foldl
    foldr = prim_foldr
    fold = prim_fold
instance Foldable Option
    foldl = prim_foldl
    foldr = prim_foldr
    fold = prim_fold
instance Foldable Array
    foldl = prim_foldl
    foldr = prim_foldr
    fold = prim_fold

instance Filterable List <= Functor List
    filter = prim_filter
    filter_map = prim_filter_map
instance Filterable Option <= Functor Option
    filter = prim_filter
    filter_map = prim_filter_map
instance Filterable Array <= Functor Array
    filter = prim_filter
    filter_map = prim_filter_map

instance Sequence List <= Functor List, Foldable List
    take = prim_take
    skip = prim_skip
    zip = prim_zip
    unzip = prim_unzip
instance Sequence Array <= Functor Array, Foldable Array
    take = prim_take
    skip = prim_skip
    zip = prim_zip
    unzip = prim_unzip

instance Alternative List <= Applicative List
    or_else = prim_or_else
instance Alternative Option <= Applicative Option
    or_else = prim_or_else
instance Alternative Array <= Applicative Array
    or_else = prim_or_else
instance Alternative (Result e) <= Applicative (Result e)
    or_else = prim_or_else

{- Indexable instances -}
instance Indexable (List a, a)
    get = prim_get
instance Indexable (Array a, a)
    get = prim_get

0