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
(** {6 Generic multisets} *)
module type S = Multiset_intf.S
module Make(E : Map.OrderedType) = struct
type elt = E.t
module M = CCMap.Make(E)
type t = Z.t M.t
let empty = M.empty
let size = M.cardinal
let cardinal m = M.fold (fun _ n acc -> Z.(n+acc)) m Z.zero
let is_empty = M.is_empty
let mem m x = M.mem x m
let find m x =
try M.find x m
with Not_found -> Z.zero
let singleton x = M.singleton x Z.one
let add_coeff m x n =
let n = Z.add n (find m x) in
if Z.equal Z.zero n
then M.remove x m
else M.add x n m
let add m x = add_coeff m x Z.one
let doubleton x y = add (singleton x) y
let _map f m : t = M.mapi f m
let _merge f m1 m2 =
M.merge_safe m1 m2
~f:(fun _ v -> match v with
| `Left n
| `Right n -> Some n
| `Both (n1,n2) ->
let n = f n1 n2 in
if Z.equal n Z.zero then None else Some n)
let union = _merge Z.max
let intersection = _merge Z.min
let sum = _merge Z.add
let difference = _merge Z.sub
let product n m =
if Z.sign n <= 0 then empty
else _map (fun _ n' -> Z.mul n n') m
let map f (m:t) : t =
M.fold
(fun x n acc -> add_coeff acc (f x) n)
m empty
let map_coeff f m = _map f m
let filter_map f (m:t) =
M.fold
(fun x n acc -> match f x n with
| None -> acc
| Some (x',n') -> add_coeff acc x' n')
m empty
let filter p m = map_coeff (fun x n -> if p x n then n else Z.zero) m
let flat_map f (m:t): t =
M.fold
(fun x _ m' ->
let m'' = f x in
union m' m'')
m empty
module Seq = struct
let of_seq m seq =
let m = ref m in
seq (fun x -> m := add !m x);
!m
let to_seq (m:t) k =
M.iter (fun x n -> for _i=1 to Z.to_int n do k x; done) m
let of_coeffs m seq =
let m = ref m in
seq (fun (x,n) -> m := add_coeff !m x n);
!m
let to_coeffs m k = M.iter (fun x n -> k (x,n)) m
end
let iter f m = Seq.to_seq m f
let iter_coeffs f m = Seq.to_coeffs m (fun (x,n) -> f x n)
let fold f acc m =
let acc = ref acc in
Seq.to_seq m (fun x -> acc := f !acc x);
!acc
let fold_coeffs f acc (m:t) = M.fold (fun x n acc -> f acc x n) m acc
let for_all p m = M.for_all (fun x _ -> p x) m
let exists p m = M.exists (fun x _ -> p x) m
let choose m = M.choose m |> fst
let of_list = List.fold_left add empty
let of_coeffs =
List.fold_left
(fun acc (x,n) -> add_coeff acc x n)
empty
let of_iarray = IArray.fold add empty
let of_array = Array.fold_left add empty
let to_list m =
M.fold (fun x n acc -> (x,n)::acc) m []
|> List.rev
let equal m1 m2 = M.equal Z.equal m1 m2
let cancel m1 m2 =
let diff m1 m2 =
M.merge_safe m1 m2
~f:(fun _ v -> match v with
| `Left n -> Some n
| `Right _ -> None
| `Both (n1,n2) ->
let n = Z.(n1-n2) in
if Z.sign n <= 0 then None else Some n)
in
diff m1 m2, diff m2 m1
let rec cancel_l m1 m2 = match m1, m2 with
| [], _
| _, [] -> m1, m2
| (x1,n1)::m1', (x2,n2)::m2' ->
let c = E.compare x1 x2 in
if c = 0
then match Z.compare n1 n2 with
| 0 -> cancel_l m1' m2'
| n when n<0 ->
let m1'', m2'' = cancel_l m1' m2' in
m1'', (x2, Z.sub n2 n1) :: m2''
| _ ->
let m1'', m2'' = cancel_l m1' m2' in
(x1, Z.sub n1 n2) :: m1'', m2''
else if c < 0
then
let m1'', m2'' = cancel_l m1' m2 in
(x1,n1)::m1'', m2''
else
let m1'', m2'' = cancel_l m1 m2' in
m1'', (x2,n2)::m2''
let compare_partial f m1 m2 =
let m1, m2 = cancel_l (to_list m1) (to_list m2) in
let rec check_left ~neq ~max1 m1 ~max2 m2 = match m1 with
| [] ->
let max2 = max2 || m2<>[] in
begin match max1, max2 with
| true, true -> Comparison.Incomparable
| true, false -> Comparison.Gt
| false, true -> Comparison.Lt
| false, false ->
if neq
then Comparison.Incomparable
else Comparison.Eq
end
| (x1,n1)::m1' ->
assert (Z.sign n1 > 0);
filter_with ~neq ~max1 x1 n1 m1' ~max2 m2 []
and filter_with ~neq ~max1 x1 n1 m1' ~max2 m2 rest2 = match m2 with
| _ when Z.sign n1 <= 0 ->
check_left ~neq ~max1 m1' ~max2 m2
| [] ->
check_left ~neq ~max1:true m1' ~max2 rest2
| (x2,n2)::m2' ->
begin match f x1 x2 with
| Comparison.Eq ->
let c = Z.compare n1 n2 in
if c < 0
then
check_left ~neq ~max1 m1' ~max2 ((x2, Z.(n2-n1)) :: List.rev_append m2' rest2)
else if c > 0
then
filter_with ~neq ~max1 x1 Z.(n1-n2) m1' ~max2 m2' rest2
else
check_left ~neq ~max1 m1' ~max2 (List.rev_append m2' rest2)
| Comparison.Incomparable ->
filter_with ~neq ~max1 x1 n1 m1' ~max2 m2' ((x2,n2)::rest2)
| Comparison.Gt ->
filter_with ~neq:true ~max1 x1 n1 m1' ~max2 m2' rest2
| Comparison.Lt ->
check_left ~neq:true ~max1 m1' ~max2 (List.rev_append m2 rest2)
end
in
check_left ~neq:false ~max1:false m1 ~max2:false m2
let compare_l m1 m2 =
let rec aux m1 m2 = match m1, m2 with
| [], [] -> 0
| [], _ -> -1
| _, [] -> 1
| (x1,n1)::m1', (x2,n2)::m2' ->
let c = E.compare x1 x2 in
if c = 0
then
let c' = aux m1' m2' in
if c' <> 0 then c'
else Z.compare n1 n2
else if c < 0
then
let c' = aux m1' m2 in
if c' <> 0 then c'
else 1
else
let c' = aux m1 m2' in
if c' <> 0 then c'
else -1
in
aux m1 m2
let compare m1 m2 = compare_l (to_list m1) (to_list m2)
let is_max f x m =
M.for_all
(fun y _ -> match f x y with
| Comparison.Lt -> false
| _ -> true)
m
let max_seq f (m:t) k =
let m = ref m in
while not (M.is_empty !m) do
let x, n = M.choose !m in
m := M.remove x !m;
try
let n' =
M.fold
(fun y n' acc -> match f x y with
| Comparison.Lt -> raise Exit
| Comparison.Eq ->
m := M.remove y !m;
Z.(acc+n')
| Comparison.Gt ->
m := M.remove y !m;
acc
| Comparison.Incomparable -> acc)
!m n
in
k (x, n')
with Exit -> ()
done
let max f m =
max_seq f m
|> Iter.fold (fun m (c,t) -> add_coeff m c t) empty
let max_l f l =
max_seq f (of_list l)
|> Iter.fold (fun acc (x,_) -> x::acc) []
let compare_partial_l f l1 l2 =
compare_partial f (of_list l1) (of_list l2)
let pp pp_x out m =
let pp_p out (x,n) = Format.fprintf out "%a: %s" pp_x x (Z.to_string n) in
Format.fprintf out "{@[<hov>%a@]}" (Util.pp_seq ~sep:", " pp_p) (M.to_seq m)
end