aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/data/collection/dictionary/ordered.lux
blob: 3c6f1a2e90cf4fd14ecd7c7cf819ae8f957fd21e (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
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
(.using
 [library
  [lux {"-" has revised}
   [abstract
    equivalence
    [monad (.only Monad do)]
    ["[0]" order (.only Order)]]
   [control
    ["[0]" maybe]]
   [data
    ["p" product]
    [collection
     ["[0]" list ("[1]#[0]" monoid mix)]]]
   [macro
    ["^" pattern]]
   [math
    [number
     ["n" nat]]]]])

(def: error_message
  "Invariant violation")

(type: Color
  (Variant
   {#Red}
   {#Black}))

(type: (Node k v)
  (Record
   [#color Color
    #key k
    #value v
    #left (Maybe (Node k v))
    #right (Maybe (Node k v))]))

(template [<create> <color>]
  [(def: (<create> key value left right)
     (All (_ k v) (-> k v (Maybe (Node k v)) (Maybe (Node k v)) (Node k v)))
     [#color {<color>}
      #key key
      #value value
      #left left
      #right right])]

  [red   #Red]
  [black #Black]
  )

(type: .public (Dictionary k v)
  (Record
   [#order (Order k)
    #root (Maybe (Node k v))]))

(def: .public (empty order)
  (All (_ k v) (-> (Order k) (Dictionary k v)))
  [#order order
   #root {.#None}])

... TODO: Doing inneficient access of Order functions due to compiler bug.
... TODO: Must improve it as soon as bug is fixed.
(def: .public (value key dict)
  (All (_ k v) (-> k (Dictionary k v) (Maybe v)))
  (let [... (open "_#[0]") (the #order dict)
        ]
    (loop (again [node (the #root dict)])
      (case node
        {.#None}
        {.#None}

        {.#Some node}
        (let [node_key (the #key node)]
          (cond (# dict = node_key key)
                ... (_#= node_key key)
                {.#Some (the #value node)}

                (# dict < node_key key)
                ... (_#< node_key key)
                (again (the #left node))

                ... (_#> (the #key node) key)
                (again (the #right node))))
        ))))

... TODO: Doing inneficient access of Order functions due to compiler bug.
... TODO: Must improve it as soon as bug is fixed.
(def: .public (key? dict key)
  (All (_ k v) (-> (Dictionary k v) k Bit))
  (let [... (open "_#[0]") (the #order dict)
        ]
    (loop (again [node (the #root dict)])
      (case node
        {.#None}
        #0

        {.#Some node}
        (let [node_key (the #key node)]
          (or (# dict = node_key key)
              ... (_#= node_key key)
              (if (# dict < node_key key)
                ... (_#< node_key key)
                (again (the #left node))
                (again (the #right node)))))))))

(template [<name> <side>]
  [(def: .public (<name> dict)
     (All (_ k v) (-> (Dictionary k v) (Maybe v)))
     (case (the #root dict)
       {.#None}
       {.#None}

       {.#Some node}
       (loop (again [node node])
         (case (the <side> node)
           {.#None}
           {.#Some (the #value node)}

           {.#Some side}
           (again side)))))]

  [min #left]
  [max #right]
  )

(def: .public (size dict)
  (All (_ k v) (-> (Dictionary k v) Nat))
  (loop (again [node (the #root dict)])
    (case node
      {.#None}
      0

      {.#Some node}
      (++ (n.+ (again (the #left node))
               (again (the #right node)))))))

(def: .public empty?
  (All (_ k v) (-> (Dictionary k v) Bit))
  (|>> ..size (n.= 0)))

(template [<name> <other_color> <self_color> <no_change>]
  [(def: (<name> self)
     (All (_ k v) (-> (Node k v) (Node k v)))
     (case (the #color self)
       {<other_color>}
       (.has #color {<self_color>} self)

       {<self_color>}
       <no_change>
       ))]

  [blackened #Red   #Black self]
  [reddened  #Black #Red   (panic! error_message)]
  )

(def: (with_left addition center)
  (All (_ k v) (-> (Node k v) (Node k v) (Node k v)))
  (case (the #color center)
    {#Red}
    (red (the #key center)
         (the #value center)
         {.#Some addition}
         (the #right center))
    
    {#Black}
    (with_expansions
      [<default_behavior> (these (black (the #key center)
                                        (the #value center)
                                        {.#Some addition}
                                        (the #right center)))]
      (case (the #color addition)
        {#Red}
        (case (the #left addition)
          (^.multi {.#Some left}
                   [(the #color left) {#Red}])
          (red (the #key addition)
               (the #value addition)
               {.#Some (blackened left)}
               {.#Some (black (the #key center)
                              (the #value center)
                              (the #right addition)
                              (the #right center))})

          _
          (case (the #right addition)
            (^.multi {.#Some right}
                     [(the #color right) {#Red}])
            (red (the #key right)
                 (the #value right)
                 {.#Some (black (the #key addition)
                                (the #value addition)
                                (the #left addition)
                                (the #left right))}
                 {.#Some (black (the #key center)
                                (the #value center)
                                (the #right right)
                                (the #right center))})

            _
            <default_behavior>))
        
        {#Black}
        <default_behavior>))))

(def: (with_right addition center)
  (All (_ k v) (-> (Node k v) (Node k v) (Node k v)))
  (case (the #color center)
    {#Red}
    (red (the #key center)
         (the #value center)
         (the #left center)
         {.#Some addition})
    
    {#Black}
    (with_expansions
      [<default_behavior> (these (black (the #key center)
                                        (the #value center)
                                        (the #left center)
                                        {.#Some addition}))]
      (case (the #color addition)
        {#Red}
        (case (the #right addition)
          (^.multi {.#Some right}
                   [(the #color right) {#Red}])
          (red (the #key addition)
               (the #value addition)
               {.#Some (black (the #key center)
                              (the #value center)
                              (the #left center)
                              (the #left addition))}
               {.#Some (blackened right)})

          _
          (case (the #left addition)
            (^.multi {.#Some left}
                     [(the #color left) {#Red}])
            (red (the #key left)
                 (the #value left)
                 {.#Some (black (the #key center)
                                (the #value center)
                                (the #left center)
                                (the #left left))}
                 {.#Some (black (the #key addition)
                                (the #value addition)
                                (the #right left)
                                (the #right addition))})

            _
            <default_behavior>))
        
        {#Black}
        <default_behavior>))))

(def: .public (has key value dict)
  (All (_ k v) (-> k v (Dictionary k v) (Dictionary k v)))
  (let [(open "_#[0]") (the #order dict)
        root' (loop (again [?root (the #root dict)])
                (case ?root
                  {.#None}
                  {.#Some (red key value {.#None} {.#None})}

                  {.#Some root}
                  (let [reference (the #key root)]
                    (`` (cond (~~ (template [<comp> <tag> <add>]
                                    [(<comp> reference key)
                                     (let [side_root (the <tag> root)
                                           outcome (again side_root)]
                                       (if (same? side_root outcome)
                                         ?root
                                         {.#Some (<add> (maybe.trusted outcome)
                                                        root)}))]

                                    [_#<                             #left  ..with_left]
                                    [(order.> (the #order dict)) #right ..with_right]
                                    ))

                              ... (_#= reference key)
                              {.#Some (.has #value value root)}
                              )))
                  ))]
    (.has #root root' dict)))

(def: (left_balanced key value ?left ?right)
  (All (_ k v) (-> k v (Maybe (Node k v)) (Maybe (Node k v)) (Node k v)))
  (case ?left
    (^.multi {.#Some left}
             [(the #color left) {#Red}]
             [(the #left left) {.#Some left>>left}]
             [(the #color left>>left) {#Red}])
    (red (the #key left)
         (the #value left)
         {.#Some (blackened left>>left)}
         {.#Some (black key value (the #right left) ?right)})

    (^.multi {.#Some left}
             [(the #color left) {#Red}]
             [(the #right left) {.#Some left>>right}]
             [(the #color left>>right) {#Red}])
    (red (the #key left>>right)
         (the #value left>>right)
         {.#Some (black (the #key left)
                        (the #value left)
                        (the #left left)
                        (the #left left>>right))}
         {.#Some (black key value
                        (the #right left>>right)
                        ?right)})

    _
    (black key value ?left ?right)))

(def: (right_balanced key value ?left ?right)
  (All (_ k v) (-> k v (Maybe (Node k v)) (Maybe (Node k v)) (Node k v)))
  (case ?right
    (^.multi {.#Some right}
             [(the #color right) {#Red}]
             [(the #right right) {.#Some right>>right}]
             [(the #color right>>right) {#Red}])
    (red (the #key right)
         (the #value right)
         {.#Some (black key value ?left (the #left right))}
         {.#Some (blackened right>>right)})

    (^.multi {.#Some right}
             [(the #color right) {#Red}]
             [(the #left right) {.#Some right>>left}]
             [(the #color right>>left) {#Red}])
    (red (the #key right>>left)
         (the #value right>>left)
         {.#Some (black key value ?left (the #left right>>left))}
         {.#Some (black (the #key right)
                        (the #value right)
                        (the #right right>>left)
                        (the #right right))})

    _
    (black key value ?left ?right)))

(def: (without_left key value ?left ?right)
  (All (_ k v) (-> k v (Maybe (Node k v)) (Maybe (Node k v)) (Node k v)))
  (case ?left
    (^.multi {.#Some left}
             [(the #color left) {#Red}])
    (red key value {.#Some (blackened left)} ?right)

    _
    (case ?right
      (^.multi {.#Some right}
               [(the #color right) {#Black}])
      (right_balanced key value ?left {.#Some (reddened right)})

      (^.multi {.#Some right}
               [(the #color right) {#Red}]
               [(the #left right) {.#Some right>>left}]
               [(the #color right>>left) {#Black}])
      (red (the #key right>>left)
           (the #value right>>left)
           {.#Some (black key value ?left (the #left right>>left))}
           {.#Some (right_balanced (the #key right)
                                   (the #value right)
                                   (the #right right>>left)
                                   (# maybe.functor each reddened (the #right right)))})

      _
      (panic! error_message))
    ))

(def: (without_right key value ?left ?right)
  (All (_ k v) (-> k v (Maybe (Node k v)) (Maybe (Node k v)) (Node k v)))
  (case ?right
    (^.multi {.#Some right}
             [(the #color right) {#Red}])
    (red key value ?left {.#Some (blackened right)})

    _
    (case ?left
      (^.multi {.#Some left}
               [(the #color left) {#Black}])
      (left_balanced key value {.#Some (reddened left)} ?right)

      (^.multi {.#Some left}
               [(the #color left) {#Red}]
               [(the #right left) {.#Some left>>right}]
               [(the #color left>>right) {#Black}])
      (red (the #key left>>right)
           (the #value left>>right)
           {.#Some (left_balanced (the #key left)
                                  (the #value left)
                                  (# maybe.functor each reddened (the #left left))
                                  (the #left left>>right))}
           {.#Some (black key value (the #right left>>right) ?right)})

      _
      (panic! error_message)
      )))

(def: (prepended ?left ?right)
  (All (_ k v) (-> (Maybe (Node k v)) (Maybe (Node k v)) (Maybe (Node k v))))
  (case [?left ?right]
    [{.#None} _]
    ?right

    [_ {.#None}]
    ?left

    [{.#Some left} {.#Some right}]
    (case [(the #color left) (the #color right)]
      [{#Red} {#Red}]
      (do maybe.monad
        [fused (prepended (the #right left) (the #right right))]
        (case (the #color fused)
          {#Red}
          (in (red (the #key fused)
                   (the #value fused)
                   {.#Some (red (the #key left)
                                (the #value left)
                                (the #left left)
                                (the #left fused))}
                   {.#Some (red (the #key right)
                                (the #value right)
                                (the #right fused)
                                (the #right right))}))

          {#Black}
          (in (red (the #key left)
                   (the #value left)
                   (the #left left)
                   {.#Some (red (the #key right)
                                (the #value right)
                                {.#Some fused}
                                (the #right right))}))))

      [{#Red} {#Black}]
      {.#Some (red (the #key left)
                   (the #value left)
                   (the #left left)
                   (prepended (the #right left)
                              ?right))}
      
      [{#Black} {#Red}]
      {.#Some (red (the #key right)
                   (the #value right)
                   (prepended ?left
                              (the #left right))
                   (the #right right))}

      [{#Black} {#Black}]
      (do maybe.monad
        [fused (prepended (the #right left) (the #left right))]
        (case (the #color fused)
          {#Red}
          (in (red (the #key fused)
                   (the #value fused)
                   {.#Some (black (the #key left)
                                  (the #value left)
                                  (the #left left)
                                  (the #left fused))}
                   {.#Some (black (the #key right)
                                  (the #value right)
                                  (the #right fused)
                                  (the #right right))}))
          
          {#Black}
          (in (without_left (the #key left)
                            (the #value left)
                            (the #left left)
                            {.#Some (black (the #key right)
                                           (the #value right)
                                           {.#Some fused}
                                           (the #right right))}))
          ))
      )

    _
    (undefined)))

(def: .public (lacks key dict)
  (All (_ k v) (-> k (Dictionary k v) (Dictionary k v)))
  (let [(open "_#[0]") (the #order dict)
        [?root found?] (loop (again [?root (the #root dict)])
                         (case ?root
                           {.#Some root}
                           (let [root_key (the #key root)
                                 root_val (the #value root)]
                             (if (_#= root_key key)
                               [(prepended (the #left root)
                                           (the #right root))
                                #1]
                               (let [go_left? (_#< root_key key)]
                                 (case (again (if go_left?
                                                (the #left root)
                                                (the #right root)))
                                   [{.#None} #0]
                                   [{.#None} #0]

                                   [side_outcome _]
                                   (if go_left?
                                     (case (the #left root)
                                       (^.multi {.#Some left}
                                                [(the #color left) {#Black}])
                                       [{.#Some (without_left root_key root_val side_outcome (the #right root))}
                                        #0]

                                       _
                                       [{.#Some (red root_key root_val side_outcome (the #right root))}
                                        #0])
                                     (case (the #right root)
                                       (^.multi {.#Some right}
                                                [(the #color right) {#Black}])
                                       [{.#Some (without_right root_key root_val (the #left root) side_outcome)}
                                        #0]

                                       _
                                       [{.#Some (red root_key root_val (the #left root) side_outcome)}
                                        #0])
                                     )))
                               ))

                           {.#None}
                           [{.#None} #0]
                           ))]
    (case ?root
      {.#None}
      (if found?
        (.has #root ?root dict)
        dict)

      {.#Some root}
      (.has #root {.#Some (blackened root)} dict)
      )))

(def: .public (revised key transform dict)
  (All (_ k v) (-> k (-> v v) (Dictionary k v) (Dictionary k v)))
  (case (..value key dict)
    {.#Some old}
    (..has key (transform old) dict)

    {.#None}
    dict))

(def: .public (of_list order list)
  (All (_ k v) (-> (Order k) (List [k v]) (Dictionary k v)))
  (list#mix (function (_ [key value] dict)
              (..has key value dict))
            (empty order)
            list))

(template [<name> <type> <output>]
  [(def: .public (<name> dict)
     (All (_ k v) (-> (Dictionary k v) (List <type>)))
     (loop (again [node (the #root dict)])
       (case node
         {.#None}
         (list)

         {.#Some node'}
         (all list#composite
              (again (the #left node'))
              (list <output>)
              (again (the #right node'))))))]

  [entries [k v] [(the #key node') (the #value node')]]
  [keys    k     (the #key node')]
  [values  v     (the #value node')]
  )

(implementation: .public (equivalence (open ",#[0]"))
  (All (_ k v) (-> (Equivalence v) (Equivalence (Dictionary k v))))
  
  (def: (= reference sample)
    (let [(open "/#[0]") (the #order reference)]
      (loop (again [entriesR (entries reference)
                    entriesS (entries sample)])
        (case [entriesR entriesS]
          [{.#End} {.#End}]
          #1

          [{.#Item [keyR valueR] entriesR'} {.#Item [keyS valueS] entriesS'}]
          (and (/#= keyR keyS)
               (,#= valueR valueS)
               (again entriesR' entriesS'))

          _
          #0)))))