aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/data/collection/set/multi.lux
blob: 0afe973ef2aa001991f1a3ac4f583669b59ba879 (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
(.module:
  [library
   [lux #*
    ["_" test (#+ Test)]
    [abstract
     [hash (#+ Hash)]
     [monad (#+ do)]
     ["." predicate]
     [\\specification
      ["$." equivalence]
      ["$." hash]]]
    [data
     ["." bit ("#\." equivalence)]
     [collection
      ["." set]
      ["." list ("#\." mix)]]]
    [math
     ["." random (#+ Random)]
     [number
      ["n" nat]]]]]
  [\\library
   ["." /]])

(def: count
  (Random Nat)
  (\ random.monad map (|>> (n.% 10) ++) random.nat))

(def: .public (random size hash count element)
  (All [a] (-> Nat (Hash a) (Random Nat) (Random a) (Random (/.Set a))))
  (do {! random.monad}
    [elements (random.set hash size element)
     element_counts (random.list size ..count)]
    (in (list\mix (function (_ [count element] set)
                    (/.has count element set))
                  (/.empty hash)
                  (list.zipped/2 element_counts
                                 (set.list elements))))))

(def: signature
  Test
  (do {! random.monad}
    [diversity (\ ! map (n.% 10) random.nat)]
    ($_ _.and
        (_.for [/.equivalence]
               ($equivalence.spec /.equivalence (..random diversity n.hash ..count random.nat)))
        (_.for [/.hash]
               (|> random.nat
                   (\ random.monad map (function (_ single)
                                         (/.has 1 single (/.empty n.hash))))
                   ($hash.spec /.hash)))
        )))

(def: composition
  Test
  (do {! random.monad}
    [diversity (\ ! map (n.% 10) random.nat)
     sample (..random diversity n.hash ..count random.nat)
     another (..random diversity n.hash ..count random.nat)]
    (`` ($_ _.and
            (~~ (template [<name> <composition>]
                  [(_.cover [<name>]
                            (let [|sample| (/.support sample)
                                  |another| (/.support another)
                                  sample_only (set.difference |another| |sample|)
                                  another_only (set.difference |sample| |another|)
                                  common (set.intersection |sample| |another|)
                                  composed (<name> sample another)

                                  no_left_changes! (list.every? (function (_ member)
                                                                  (n.= (/.multiplicity sample member)
                                                                       (/.multiplicity composed member)))
                                                                (set.list sample_only))
                                  no_right_changes! (list.every? (function (_ member)
                                                                   (n.= (/.multiplicity another member)
                                                                        (/.multiplicity composed member)))
                                                                 (set.list another_only))
                                  common_changes! (list.every? (function (_ member)
                                                                 (n.= (<composition> (/.multiplicity sample member)
                                                                                     (/.multiplicity another member))
                                                                      (/.multiplicity composed member)))
                                                               (set.list common))]
                              (and no_left_changes!
                                   no_right_changes!
                                   common_changes!)))]

                  [/.sum n.+]
                  [/.union n.max]
                  ))
            (_.cover [/.intersection]
                     (let [|sample| (/.support sample)
                           |another| (/.support another)
                           sample_only (set.difference |another| |sample|)
                           another_only (set.difference |sample| |another|)
                           common (set.intersection |sample| |another|)
                           composed (/.intersection sample another)

                           left_removals! (list.every? (|>> (/.member? composed) not)
                                                       (set.list sample_only))
                           right_removals! (list.every? (|>> (/.member? composed) not)
                                                        (set.list another_only))
                           common_changes! (list.every? (function (_ member)
                                                          (n.= (n.min (/.multiplicity sample member)
                                                                      (/.multiplicity another member))
                                                               (/.multiplicity composed member)))
                                                        (set.list common))]
                       (and left_removals!
                            right_removals!
                            common_changes!)))
            ))))

(def: .public test
  Test
  (<| (_.covering /._)
      (_.for [/.Set])
      (do {! random.monad}
        [diversity (\ ! map (n.% 10) random.nat)
         sample (..random diversity n.hash ..count random.nat)
         non_member (random.only (predicate.complement (set.member? (/.support sample)))
                                 random.nat)
         addition_count ..count
         partial_removal_count (\ ! map (n.% addition_count) random.nat)
         another (..random diversity n.hash ..count random.nat)]
        ($_ _.and
            (_.cover [/.list /.of_list]
                     (|> sample
                         /.list
                         (/.of_list n.hash)
                         (\ /.equivalence = sample)))
            (_.cover [/.size]
                     (n.= (list.size (/.list sample))
                          (/.size sample)))
            (_.cover [/.empty?]
                     (bit\= (/.empty? sample)
                            (n.= 0 (/.size sample))))
            (_.cover [/.empty]
                     (/.empty? (/.empty n.hash)))
            (_.cover [/.support]
                     (list.every? (set.member? (/.support sample))
                                  (/.list sample)))
            (_.cover [/.member?]
                     (let [non_member_is_not_identified!
                           (not (/.member? sample non_member))

                           all_members_are_identified!
                           (list.every? (/.member? sample)
                                        (/.list sample))]
                       (and non_member_is_not_identified!
                            all_members_are_identified!)))
            (_.cover [/.multiplicity]
                     (let [non_members_have_0_multiplicity!
                           (n.= 0 (/.multiplicity sample non_member))

                           every_member_has_positive_multiplicity!
                           (list.every? (|>> (/.multiplicity sample) (n.> 0))
                                        (/.list sample))]
                       (and non_members_have_0_multiplicity!
                            every_member_has_positive_multiplicity!)))
            (_.cover [/.has]
                     (let [null_scenario!
                           (|> sample
                               (/.has 0 non_member)
                               (\ /.equivalence = sample))

                           normal_scenario!
                           (let [sample+ (/.has addition_count non_member sample)]
                             (and (not (/.member? sample non_member))
                                  (/.member? sample+ non_member)
                                  (n.= addition_count (/.multiplicity sample+ non_member))))]
                       (and null_scenario!
                            normal_scenario!)))
            (_.cover [/.lacks]
                     (let [null_scenario!
                           (\ /.equivalence =
                              (|> sample
                                  (/.has addition_count non_member))
                              (|> sample
                                  (/.has addition_count non_member)
                                  (/.lacks 0 non_member)))

                           partial_scenario!
                           (let [sample* (|> sample
                                             (/.has addition_count non_member)
                                             (/.lacks partial_removal_count non_member))]
                             (and (/.member? sample* non_member)
                                  (n.= (n.- partial_removal_count
                                            addition_count)
                                       (/.multiplicity sample* non_member))))

                           total_scenario!
                           (|> sample
                               (/.has addition_count non_member)
                               (/.lacks addition_count non_member)
                               (\ /.equivalence = sample))]
                       (and null_scenario!
                            partial_scenario!
                            total_scenario!)))
            (_.cover [/.of_set]
                     (let [unary (|> sample /.support /.of_set)]
                       (list.every? (|>> (/.multiplicity unary) (n.= 1))
                                    (/.list unary))))
            (_.cover [/.sub?]
                     (let [unary (|> sample /.support /.of_set)]
                       (and (/.sub? sample unary)
                            (or (not (/.sub? unary sample))
                                (\ /.equivalence = sample unary)))))
            (_.cover [/.super?]
                     (let [unary (|> sample /.support /.of_set)]
                       (and (/.super? unary sample)
                            (or (not (/.super? sample unary))
                                (\ /.equivalence = sample unary)))))
            (_.cover [/.difference]
                     (let [|sample| (/.support sample)
                           |another| (/.support another)
                           sample_only (set.difference |another| |sample|)
                           another_only (set.difference |sample| |another|)
                           common (set.intersection |sample| |another|)
                           composed (/.difference sample another)

                           ommissions! (list.every? (|>> (/.member? composed) not)
                                                    (set.list sample_only))
                           intact! (list.every? (function (_ member)
                                                  (n.= (/.multiplicity another member)
                                                       (/.multiplicity composed member)))
                                                (set.list another_only))
                           subtractions! (list.every? (function (_ member)
                                                        (let [sample_multiplicity (/.multiplicity sample member)
                                                              another_multiplicity (/.multiplicity another member)]
                                                          (n.= (if (n.> another_multiplicity sample_multiplicity)
                                                                 0
                                                                 (n.- sample_multiplicity
                                                                      another_multiplicity))
                                                               (/.multiplicity composed member))))
                                                      (set.list common))]
                       (and ommissions!
                            intact!
                            subtractions!)))

            ..signature
            ..composition
            ))))