aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/data/collection/row.lux
blob: 5080c4c1c551c81be7302787c3e5bb7206d89a50 (plain)
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
... https://hypirion.com/musings/understanding-persistent-vector-pt-1
... https://hypirion.com/musings/understanding-persistent-vector-pt-2
... https://hypirion.com/musings/understanding-persistent-vector-pt-3
(.module:
  [library
   [lux {"-" [list]}
    ["@" target]
    [abstract
     [functor {"+" [Functor]}]
     [apply {"+" [Apply]}]
     [monad {"+" [Monad do]}]
     [equivalence {"+" [Equivalence]}]
     [monoid {"+" [Monoid]}]
     [mix {"+" [Mix]}]
     [predicate {"+" [Predicate]}]]
    [control
     ["[0]" maybe]
     ["[0]" try {"+" [Try]}]
     ["[0]" exception {"+" [exception:]}]
     ["<>" parser
      ["<[0]>" code {"+" [Parser]}]]]
    [data
     ["[0]" product]
     [collection
      ["[0]" list ("[1]\[0]" mix functor monoid)]
      ["[0]" array {"+" [Array]} ("[1]\[0]" functor mix)]]]
    [macro
     [syntax {"+" [syntax:]}]
     ["[0]" code]]
    [math
     [number
      ["n" nat]
      ["[0]" i64]]]]])

(type: (Node a)
  (Variant
   (#Base (Array a))
   (#Hierarchy (Array (Node a)))))

(type: (Base a)
  (Array a))

(type: (Hierarchy a)
  (Array (Node a)))

(type: Level
  Nat)

(type: Index
  Nat)

(def: branching_exponent
  Nat
  5)

(def: root_level
  Level
  0)

(template [<name> <op>]
  [(def: <name>
     (-> Level Level)
     (<op> branching_exponent))]

  [level_up   n.+]
  [level_down n.-]
  )

(def: full_node_size
  Nat
  (i64.left_shifted branching_exponent 1))

(def: branch_idx_mask
  Nat
  (-- full_node_size))

(def: branch_idx
  (-> Index Index)
  (i64.and branch_idx_mask))

(def: (empty_hierarchy _)
  (All (_ a) (-> Any (Hierarchy a)))
  (array.empty ..full_node_size))

(def: (tail_off row_size)
  (-> Nat Nat)
  (if (n.< full_node_size row_size)
    0
    (|> (-- row_size)
        (i64.right_shifted branching_exponent)
        (i64.left_shifted branching_exponent))))

(def: (path level tail)
  (All (_ a) (-> Level (Base a) (Node a)))
  (if (n.= 0 level)
    (#Base tail)
    (|> (empty_hierarchy [])
        (array.write! 0 (path (level_down level) tail))
        #Hierarchy)))

(def: (tail singleton)
  (All (_ a) (-> a (Base a)))
  (|> (array.empty 1)
      (array.write! 0 singleton)))

(def: (with_tail size level tail parent)
  (All (_ a) (-> Nat Level (Base a) (Hierarchy a) (Hierarchy a)))
  (let [sub_idx (branch_idx (i64.right_shifted level (-- size)))
        ... If we're currently on a bottom node
        sub_node (if (n.= branching_exponent level)
                   ... Just add the tail to it
                   (#Base tail)
                   ... Otherwise, check whether there's a vacant spot
                   (case (array.read! sub_idx parent)
                     ... If so, set the path to the tail
                     #.None
                     (..path (level_down level) tail)
                     ... If not, push the tail onto the sub_node.
                     (#.Some (#Hierarchy sub_node))
                     (#Hierarchy (with_tail size (level_down level) tail sub_node))

                     _
                     (undefined))
                   )]
    (|> (array.clone parent)
        (array.write! sub_idx sub_node))))

(def: (expanded_tail val tail)
  (All (_ a) (-> a (Base a) (Base a)))
  (let [tail_size (array.size tail)]
    (|> (array.empty (++ tail_size))
        (array.copy! tail_size 0 tail 0)
        (array.write! tail_size val))))

(def: (hierarchy\has level idx val hierarchy)
  (All (_ a) (-> Level Index a (Hierarchy a) (Hierarchy a)))
  (let [sub_idx (branch_idx (i64.right_shifted level idx))]
    (case (array.read! sub_idx hierarchy)
      (#.Some (#Hierarchy sub_node))
      (|> (array.clone hierarchy)
          (array.write! sub_idx (#Hierarchy (hierarchy\has (level_down level) idx val sub_node))))

      (^multi (#.Some (#Base base))
              (n.= 0 (level_down level)))
      (|> (array.clone hierarchy)
          (array.write! sub_idx (|> (array.clone base)
                                    (array.write! (branch_idx idx) val)
                                    #Base)))

      _
      (undefined))))

(def: (without_tail size level hierarchy)
  (All (_ a) (-> Nat Level (Hierarchy a) (Maybe (Hierarchy a))))
  (let [sub_idx (branch_idx (i64.right_shifted level (n.- 2 size)))]
    (cond (n.= 0 sub_idx)
          #.None

          (n.> branching_exponent level)
          (do maybe.monad
            [base|hierarchy (array.read! sub_idx hierarchy)
             sub (case base|hierarchy
                   (#Hierarchy sub)
                   (without_tail size (level_down level) sub)

                   (#Base _)
                   (undefined))]
            (|> (array.clone hierarchy)
                (array.write! sub_idx (#Hierarchy sub))
                #.Some))

          ... Else...
          (|> (array.clone hierarchy)
              (array.delete! sub_idx)
              #.Some)
          )))

(def: (node\list node)
  (All (_ a) (-> (Node a) (List a)))
  (case node
    (#Base base)
    (array.list #.None base)
    
    (#Hierarchy hierarchy)
    (|> hierarchy
        (array.list #.None)
        list.reversed
        (list\mix (function (_ sub acc)
                    (list\composite (node\list sub) acc))
                  #.End))))

(type: .public (Row a)
  (Record
   [#level Level
    #size Nat
    #root (Hierarchy a)
    #tail (Base a)]))

(def: .public empty
  Row
  [#level (level_up root_level)
   #size 0
   #root (empty_hierarchy [])
   #tail (array.empty 0)])

(def: .public (size row)
  (All (_ a) (-> (Row a) Nat))
  (value@ #size row))

(def: .public (suffix val row)
  (All (_ a) (-> a (Row a) (Row a)))
  ... Check if there is room in the tail.
  (let [row_size (value@ #size row)]
    (if (|> row_size (n.- (tail_off row_size)) (n.< full_node_size))
      ... If so, append to it.
      (|> row
          (revised@ #size ++)
          (revised@ #tail (..expanded_tail val)))
      ... Otherwise, push tail into the tree
      ... --------------------------------------------------------
      ... Will the root experience an overflow with this addition?
      (|> (if (n.> (i64.left_shifted (value@ #level row) 1)
                   (i64.right_shifted branching_exponent row_size))
            ... If so, a brand-new root must be established, that is
            ... 1-level taller.
            (|> row
                (with@ #root (|> (for {@.old
                                       (: (Hierarchy (:parameter 0))
                                          (empty_hierarchy []))}
                                      (empty_hierarchy []))
                                 (array.write! 0 (#Hierarchy (value@ #root row)))
                                 (array.write! 1 (..path (value@ #level row) (value@ #tail row)))))
                (revised@ #level level_up))
            ... Otherwise, just push the current tail onto the root.
            (|> row
                (revised@ #root (..with_tail row_size (value@ #level row) (value@ #tail row)))))
          ... Finally, update the size of the row and grow a new
          ... tail with the new element as it's sole member.
          (revised@ #size ++)
          (with@ #tail (..tail val)))
      )))

(exception: incorrect_row_structure)

(exception: .public [a] (index_out_of_bounds [row (Row a)
                                              index Nat])
  (exception.report ["Size" (\ n.decimal encoded (value@ #size row))]
                    ["Index" (\ n.decimal encoded index)]))

(exception: base_was_not_found)

(def: .public (within_bounds? row idx)
  (All (_ a) (-> (Row a) Nat Bit))
  (n.< (value@ #size row) idx))

(def: (base_for idx row)
  (All (_ a) (-> Index (Row a) (Try (Base a))))
  (if (within_bounds? row idx)
    (if (n.< (tail_off (value@ #size row)) idx)
      (loop [level (value@ #level row)
             hierarchy (value@ #root row)]
        (case [(n.> branching_exponent level)
               (array.read! (branch_idx (i64.right_shifted level idx)) hierarchy)]
          [#1 (#.Some (#Hierarchy sub))]
          (recur (level_down level) sub)

          [#0 (#.Some (#Base base))]
          (#try.Success base)

          [_ #.None]
          (exception.except ..base_was_not_found [])

          _
          (exception.except ..incorrect_row_structure [])))
      (#try.Success (value@ #tail row)))
    (exception.except ..index_out_of_bounds [row idx])))

(def: .public (item idx row)
  (All (_ a) (-> Nat (Row a) (Try a)))
  (do try.monad
    [base (base_for idx row)]
    (case (array.read! (branch_idx idx) base)
      (#.Some value)
      (#try.Success value)
      
      #.None
      (exception.except ..incorrect_row_structure []))))

(def: .public (has idx val row)
  (All (_ a) (-> Nat a (Row a) (Try (Row a))))
  (let [row_size (value@ #size row)]
    (if (within_bounds? row idx)
      (#try.Success (if (n.< (tail_off row_size) idx)
                      (revised@ #root (hierarchy\has (value@ #level row) idx val)
                                row)
                      (revised@ #tail (for {@.old
                                            (: (-> (Base (:parameter 0)) (Base (:parameter 0)))
                                               (|>> array.clone (array.write! (branch_idx idx) val)))}
                                           (|>> array.clone (array.write! (branch_idx idx) val)))
                                row)))
      (exception.except ..index_out_of_bounds [row idx]))))

(def: .public (revised idx f row)
  (All (_ a) (-> Nat (-> a a) (Row a) (Try (Row a))))
  (do try.monad
    [val (..item idx row)]
    (..has idx (f val) row)))

(def: .public (prefix row)
  (All (_ a) (-> (Row a) (Row a)))
  (case (value@ #size row)
    0
    empty

    1
    empty

    row_size
    (if (|> row_size (n.- (tail_off row_size)) (n.> 1))
      (let [old_tail (value@ #tail row)
            new_tail_size (-- (array.size old_tail))]
        (|> row
            (revised@ #size --)
            (with@ #tail (|> (array.empty new_tail_size)
                             (array.copy! new_tail_size 0 old_tail 0)))))
      (maybe.trusted
       (do maybe.monad
         [new_tail (base_for (n.- 2 row_size) row)
          .let [[level' root'] (let [init_level (value@ #level row)]
                                 (loop [level init_level
                                        root (maybe.else (empty_hierarchy [])
                                                         (without_tail row_size init_level (value@ #root row)))]
                                   (if (n.> branching_exponent level)
                                     (case [(array.read! 1 root) (array.read! 0 root)]
                                       [#.None (#.Some (#Hierarchy sub_node))]
                                       (recur (level_down level) sub_node)

                                       ... [#.None (#.Some (#Base _))]
                                       ... (undefined)

                                       _
                                       [level root])
                                     [level root])))]]
         (in (|> row
                 (revised@ #size --)
                 (with@ #level level')
                 (with@ #root root')
                 (with@ #tail new_tail))))))
    ))

(def: .public (list row)
  (All (_ a) (-> (Row a) (List a)))
  (list\composite (node\list (#Hierarchy (value@ #root row)))
                  (node\list (#Base (value@ #tail row)))))

(def: .public of_list
  (All (_ a) (-> (List a) (Row a)))
  (list\mix ..suffix ..empty))

(def: .public (member? equivalence row val)
  (All (_ a) (-> (Equivalence a) (Row a) a Bit))
  (list.member? equivalence (list row) val))

(def: .public empty?
  (All (_ a) (-> (Row a) Bit))
  (|>> (value@ #size) (n.= 0)))

(syntax: .public (row [elems (<>.some <code>.any)])
  (in (.list (` (..of_list (.list (~+ elems)))))))

(implementation: (node_equivalence Equivalence<a>)
  (All (_ a) (-> (Equivalence a) (Equivalence (Node a))))
  
  (def: (= v1 v2)
    (case [v1 v2]
      [(#Base b1) (#Base b2)]
      (\ (array.equivalence Equivalence<a>) = b1 b2)
      
      [(#Hierarchy h1) (#Hierarchy h2)]
      (\ (array.equivalence (node_equivalence Equivalence<a>)) = h1 h2)

      _
      #0)))

(implementation: .public (equivalence Equivalence<a>)
  (All (_ a) (-> (Equivalence a) (Equivalence (Row a))))
  
  (def: (= v1 v2)
    (and (n.= (value@ #size v1) (value@ #size v2))
         (let [(^open "node\[0]") (node_equivalence Equivalence<a>)]
           (and (node\= (#Base (value@ #tail v1))
                        (#Base (value@ #tail v2)))
                (node\= (#Hierarchy (value@ #root v1))
                        (#Hierarchy (value@ #root v2))))))))

(implementation: node_mix
  (Mix Node)
  
  (def: (mix f init xs)
    (case xs
      (#Base base)
      (array\mix f init base)
      
      (#Hierarchy hierarchy)
      (array\mix (function (_ node init') (mix f init' node))
                 init
                 hierarchy))))

(implementation: .public mix
  (Mix Row)
  
  (def: (mix f init xs)
    (let [(^open "[0]") node_mix]
      (mix f
           (mix f
                init
                (#Hierarchy (value@ #root xs)))
           (#Base (value@ #tail xs))))))

(implementation: .public monoid
  (All (_ a) (Monoid (Row a)))
  
  (def: identity ..empty)
  
  (def: (composite xs ys)
    (list\mix suffix xs (..list ys))))

(implementation: node_functor
  (Functor Node)
  
  (def: (each f xs)
    (case xs
      (#Base base)
      (#Base (array\each f base))
      
      (#Hierarchy hierarchy)
      (#Hierarchy (array\each (each f) hierarchy)))))

(implementation: .public functor
  (Functor Row)
  
  (def: (each f xs)
    [#level (value@ #level xs)
     #size (value@ #size xs)
     #root (|> xs (value@ #root) (array\each (\ node_functor each f)))
     #tail (|> xs (value@ #tail) (array\each f))]))

(implementation: .public apply
  (Apply Row)
  
  (def: &functor ..functor)

  (def: (on fa ff)
    (let [(^open "[0]") ..functor
          (^open "[0]") ..mix
          (^open "[0]") ..monoid
          results (each (function (_ f) (each f fa))
                        ff)]
      (mix composite identity results))))

(implementation: .public monad
  (Monad Row)
  
  (def: &functor ..functor)

  (def: in
    (|>> row))

  (def: conjoint
    (let [(^open "[0]") ..mix
          (^open "[0]") ..monoid]
      (mix (function (_ post pre) (composite pre post)) identity))))

(def: .public reversed
  (All (_ a) (-> (Row a) (Row a)))
  (|>> ..list
       list.reversed
       (list\mix suffix ..empty)))

(template [<name> <array> <init> <op>]
  [(def: .public <name>
     (All (_ a)
       (-> (Predicate a) (Row a) Bit))
     (let [help (: (All (_ a)
                     (-> (Predicate a) (Node a) Bit))
                   (function (help predicate node)
                     (case node
                       (#Base base)
                       (<array> predicate base)

                       (#Hierarchy hierarchy)
                       (<array> (help predicate) hierarchy))))]
       (function (<name> predicate row)
         (let [(^slots [#root #tail]) row]
           (<op> (help predicate (#Hierarchy root))
                 (help predicate (#Base tail)))))))]

  [every? array.every? #1 and]
  [any?   array.any?   #0 or]
  )