aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/data/collection/set/multi.lux
blob: eb8e914e31389d56f540f09174d2bf9f53be9ba0 (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
## https://en.wikipedia.org/wiki/Multiset
(.module:
  [lux #*
   [control
    [equivalence (#+ Equivalence)]
    [hash (#+ Hash)]]
   ["." function]
   [type (#+ :share)
    abstract]]
  [////
   ["." maybe]]
  [///
   ["." list ("list/." Fold<List>)]
   ["." dictionary (#+ Dictionary)]]
  ["." //])

(abstract: #export (Set a)
  {}
  
  (Dictionary a Nat)

  (def: #export new
    (All [a] (-> (Hash a) (Set a)))
    (|>> dictionary.new :abstraction))

  (def: #export size
    (All [a] (-> (Set a) Nat))
    (|>> :representation dictionary.values (list/fold n/+ +0)))

  (def: #export (add/* count elem set)
    (All [a] (-> Nat a (Set a) (Set a)))
    (|> set :representation (dictionary.update~ elem +0 (n/+ count)) :abstraction))

  (def: #export add/1
    (All [a] (-> a (Set a) (Set a)))
    (add/* +1))

  (def: #export (remove/* count elem set)
    (All [a] (-> Nat a (Set a) (Set a)))
    (case (dictionary.get elem (:representation set))
      (#.Some current)
      (let [transform (:share [a]
                              {(Set a)
                               set}
                              {(-> (Dictionary a Nat) (Dictionary a Nat))
                               (if (n/> count current)
                                 (dictionary.update elem (n/- count))
                                 (dictionary.remove elem))})]
        (|> set :representation transform :abstraction))
      
      #.None
      set))

  (def: #export remove/1
    (All [a] (-> a (Set a) (Set a)))
    (remove/* +1))

  (def: #export (multiplicity elem set)
    (All [a] (-> a (Set a) Nat))
    (|> set :representation (dictionary.get elem) (maybe.default +0)))

  (def: #export to-list
    (All [a] (-> (Set a) (List a)))
    (let [append (: (All [a] (-> a Nat (List a) (List a)))
                    (function (append elem count output)
                      (case count
                        +0 output
                        _ (|> output (#.Cons elem) (append elem (dec count))))))]
      (|>> :representation
           dictionary.entries
           (list/fold (function (_ [elem count] output)
                        (append elem count output))
                      #.Nil))))

  (def: #export (union parameter subject)
    (All [a] (-> (Set a) (Set a) (Set a)))
    (:abstraction (dictionary.merge-with n/+ (:representation parameter) (:representation subject))))

  (def: #export (difference parameter subject)
    (All [a] (-> (Set a) (Set a) (Set a)))
    (|> parameter
        :representation
        dictionary.entries
        (list/fold (function (_ [elem count] output)
                     (remove/* count elem output))
                   subject)))

  (def: #export (intersection parameter subject)
    (All [a] (-> (Set a) (Set a) (Set a)))
    (|> parameter
        :representation
        dictionary.entries
        (list/fold (function (_ [elem count] (^:representation output))
                     (:abstraction (if (dictionary.contains? elem output)
                                     (dictionary.update elem (n/min count) output)
                                     output)))
                   subject)))

  (def: #export (sub? reference subject)
    (All [a] (-> (Set a) (Set a) Bit))
    (|> subject
        :representation
        dictionary.entries
        (list.every? (function (_ [elem count])
                       (|> reference
                           :representation
                           (dictionary.get elem)
                           (maybe.default +0)
                           (n/>= count))))))

  (def: #export (support set)
    (All [a] (-> (Set a) (//.Set a)))
    (let [(^@ set [Hash<a> _]) (:representation set)]
      (|> set
          dictionary.keys
          (//.from-list Hash<a>))))

  (structure: #export Equivalence<Set> (All [a] (Equivalence (Set a)))
    (def: (= (^:representation reference) (^:representation sample))
      (and (n/= (dictionary.size reference)
                (dictionary.size sample))
           (|> reference
               dictionary.entries
               (list.every? (function (_ [elem count])
                              (|> sample
                                  (dictionary.get elem)
                                  (maybe.default +0)
                                  (n/= count))))))))

  (structure: #export Hash<Set> (All [a] (Hash (Set a)))
    (def: eq ..Equivalence<Set>)
    
    (def: (hash (^:representation set))
      (let [[Hash<a> _] set]
        (list/fold (function (_ [elem count] acc)
                     (|> elem (:: Hash<a> hash) (n/* count) (n/+ acc)))
                   +0
                   (dictionary.entries set)))))
  )

(def: #export (member? set elem)
  (All [a] (-> (Set a) a Bit))
  (|> set (..multiplicity elem) (n/> +0)))

(def: #export empty?
  (All [a] (-> (Set a) Bit))
  (|>> ..size (n/= +0)))

(def: #export (from-list Hash<a> subject)
  (All [a] (-> (Hash a) (List a) (Set a)))
  (list/fold ..add/1 (..new Hash<a>) subject))

(def: #export super?
  (All [a] (-> (Set a) (Set a) Bit))
  (function.flip sub?))