package zdd

  1. Overview
  2. Docs

Source file zdd_sigs.ml

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
(** @author Benoît Montagu <benoit.montagu@inria.fr> *)

(** Copyright Inria © 2025 *)

(** Signature of types for which set families can be created *)
module type T = sig
  type t

  val hash : t -> int
  (** Hash function *)

  val compare : t -> t -> int
  (** Total order *)

  val equal : t -> t -> bool
  (** Equality test *)

  val pp : Format.formatter -> t -> unit
  (** Pretty printer *)
end

(** The signature of set families *)
module type S = sig
  type elt

  type t
  (** Set families of finite sets over the type [elt], implemented as ZDDs *)

  val hash : t -> int
  (** Hash function *)

  val compare : t -> t -> int
  (** Total order *)

  val equal : t -> t -> bool
  (** Equality test *)

  val pp : Format.formatter -> t -> unit
  (** Pretty printer for ZDDs *)

  val empty : t
  (** The empty family: [∅] *)

  val is_empty : t -> bool
  (** Tests whether a ZDD is equal to [empty] *)

  val base : t
  (** The family that contains only the empty set: [{ ∅ }] *)

  val is_base : t -> bool
  (** Tests whether a ZDD is equal to [base] *)

  val wf : t -> bool
  (** Wellformedness test *)

  val singleton : elt list -> t
  (** [singleton [x1;... ; xn]] is the ZDD that represents the set family
      [{ { x1, ..., xn } }] *)

  val choose : t -> elt list
  (** Retrieves a set of the set family, if any. The choice of which set of the
      family is returned is not specified.

      @raise Not_found if the set family is empty *)

  val choose_opt : t -> elt list option
  (** Retrieves a set of the set family, if any. The choice of which set of the
      family is returned is not specified. *)

  val to_list : t -> elt list list
  (** Returns the sets that belong to a ZDD, as a list of lists of elements *)

  val to_seq : t -> elt list Seq.t
  (** Returns the sets that belong to a ZDD, as a sequence of lists of elements
  *)

  val cardinal_generic : ('a -> 'a -> 'a) -> 'a -> 'a -> t -> 'a
  (** [cardinal_generic plus zero one t] is the cardinal of the family
      represented by [t], i.e., how many sets it contains, where [plus] is used
      as (associative commutative) addition and [zero] as neutral element and
      [one] for [1]. *)

  val cardinal : t -> int
  (** [cardinal t] is the cardinal of the family represented by [t], i.e., how
      many sets it contains. *)

  val max_cardinal : t -> int
  (** [max_cardinal t] returns the maximal cardinal of the sets contained in the
      family represented by [t]. Returns [min_int] if the family is empty. *)

  val min_cardinal : t -> int
  (** [min_cardinal t] returns the minimal cardinal of the sets contained in the
      family represented by [t]. Returns [max_int] if the family is empty. *)

  val nb_nodes : t -> int
  (** Returns the number of nodes that are used to represent the ZDD *)

  val with_elt : elt -> t -> t
  (** [s ∈ with_elt y t] iff [s ∈ t] and [y ∈ s] *)

  val on_elt : elt -> t -> t
  (** [s ∈ on_elt y t] iff [{y} ∪ s ∈ t] and [y ∉ s] *)

  val without_elt : elt -> t -> t
  (** [s ∈ without_elt y t] iff [s ∈ t] and [y ∉ s] *)

  val off_elt : elt -> t -> t
  (** This is the same as [without_elt] *)

  val change_elt : elt -> t -> t
  (** [s ∈ change x t] iff either [x ∈ s] and [s ∖ {x} ∈ t], or [x ∉ s] and
      [{x} ∪ s ∈ t] *)

  val subset : t -> t -> bool
  (** [subset t1 t2] iff for every [s], [s ∈ t1] implies [s ∈ t2] *)

  val union : t -> t -> t
  (** [s ∈ inter t1 t2] iff [s ∈ t1] or [s ∈ t2] *)

  val inter : t -> t -> t
  (** [s ∈ inter t1 t2] iff [s ∈ t1] and [s ∈ t2] *)

  val diff : t -> t -> t
  (** [s ∈ diff t1 t2] iff [s ∈ t1] and [s ∉ t2] *)

  val sym_diff : t -> t -> t
  (** [s ∈ sym_diff t1 t2] iff either [s ∈ t1] and [s ∉ t2], or [s ∈ t2] and
      [s ∉ t1] *)

  val join : t -> t -> t
  (** [s ∈ join t1 t2] iff there exists [s1 ∈ t1] and [s2 ∈ t2] such that
      [s = s1 ∪ s2] *)

  val disjoint_join : t -> t -> t
  (** [s ∈ disjoint_join t1 t2] iff there exists [s1 ∈ t1] and [s2 ∈ t2] such
      that [s1 ∩ s2 = ∅] and [s = s1 ∪ s2] *)

  val joint_join : t -> t -> t
  (** [s ∈ joint_join t1 t2] iff there exists [s1 ∈ t1] and [s2 ∈ t2] such that
      [s1 ∩ s2 ≠ ∅] and [s = s1 ∪ s2] *)

  val meet : t -> t -> t
  (** [s ∈ meet t1 t2] iff there exists [s1 ∈ t1] and [s2 ∈ t2] such that
      [s = s1 ∩ s2] *)

  val delta : t -> t -> t
  (** [s ∈ delta t1 t2] iff there exists [s1 ∈ t1] and [s2 ∈ t2] such that
      [s = (s1 \ s2) ∪ (s2 \ s1)] *)

  val minus : t -> t -> t
  (** [s ∈ minus t1 t2] iff there exists [s1 ∈ t1] and [s2 ∈ t2] such that
      [s = s1 \ s2] *)

  val div : t -> t -> t
  (** When [t2 ≠ ∅]: [s ∈ div t1 t2] iff for any [s2 ∈ t2], [s ∪ s2 ∈ t1] and
      [s ∩ s2 = ∅]. When [t2 = ∅]: [div t1 t2 = t1]. *)

  val rem : t -> t -> t
  (** [rem t1 t2 = diff t1 (join (div t1 t2) t2)]. The following equation is
      always satisfied: [t1 = union (join (div t1 t2) t2) (rem t1 t2)]. **)

  val remove : elt -> t -> t
  (** [s ∈ remove y t] iff there exists [s' ∈ t] such that [s' = s \ { x }] *)

  val restrict : t -> t -> t
  (** [s ∈ restrict t1 t2] iff [s ∈ t1] and there exists [s' ∈ t2] such that
      [s' ⊆ s] *)

  val permit : t -> t -> t
  (** [s ∈ permit t1 t2] iff [s ∈ t1] and there exists [s' ∈ t2] such that
      [s ⊆ s'] *)

  val non_superset : t -> t -> t
  (** [s ∈ non_superset t1 t2] iff [s ∈ t1] and for every [s' ∈ t2], [s' ⊈ s] *)

  val non_subset : t -> t -> t
  (** [s ∈ non_subset t1 t2] iff [s ∈ t1] and for every [s' ∈ t2], [s ⊈ s'] *)

  val minima : t -> t
  (** [s ∈ maxima t] iff [s ∈ t] and for every [s' ∈ t], [s ⊆ s'] implies
      [s' ⊆ s] *)

  val maxima : t -> t
  (** [s ∈ minima t] iff [s ∈ t] and for every [s' ∈ t], [s' ⊆ s] implies
      [s ⊆ s'] *)

  val min_hitting_set : t -> t
  (** [s ∈ min_hitting_set t] iff for every [s' ∈ t], [s ∩ s' ≠ ∅], and such
      that no smaller set than [s] satisfies this property (i.e., [s] is
      minimal). *)

  val closure : t -> t
  (** [s ∈ closure t] iff there exists [t' ⊆ t] such that [s = ⋂ t'] *)

  val subset_closure : t -> t
  (** [s ∈ subset_closure t] iff there exists [s' ∈ t] such that [s ⊆ s'] *)

  val leq_FE_subset : t -> t -> bool
  (** [leq_FE_subset t1 t2] iff for every [S1 ∈ t1], there exists [S2 ∈ t2],
      such that [S1 ⊆ S2] *)

  val leq_FE_superset : t -> t -> bool
  (** [leq_FE_superset t1 t2] iff for every [S1 ∈ t1], there exists [S2 ∈ t2],
      such that [S1 ⊇ S2] *)

  val subst_gen : (t -> t -> t) -> (elt -> t option) -> t -> t
  (** [subst_gen union env t] substitutes in [t] the elements [x] such that
      [env x = Some sx] with [sx], with the interpretation of the set family [t]
      as a boolean expression in disjunctive normal form, using [union] for
      disjunction. Elements [x] such that [env x = None] are not modified, and
      are not removed. [subst_gen] performs memoization as soon as its two first
      arguments [union] and [env] are given. *)

  val subst : (elt -> t option) -> t -> t
  (** [subst env t] substitutes in [t] the elements [x] such that
      [env x = Some sx] with [sx], with the interpretation of the set family [t]
      as a boolean expression in disjunctive normal form. Elements [x] such that
      [env x = None] are not modified, and are not removed. *)

  val iter_elt : (elt -> unit) -> t -> unit
  (** Iterator on the elements that occur in the set family. The elements might
      be encountered more than once, and the order in which they are encountered
      is unspecified. *)

  val fold_elt : (elt -> 'a -> 'a) -> t -> 'a -> 'a
  (** Folder on the elements that occur in the set family. The elements might be
      encountered more than once, and the order in which they are encountered is
      unspecified. *)

  val iter : (elt list -> unit) -> t -> unit
  (** Iterator on the list of elements that represent the sets in the families.
      The sets may occur in an unspecified order. The elements in the lists
      occur in increasing order. *)

  val fold : (elt list -> 'a -> 'a) -> t -> 'a -> 'a
  (** Folder on the list of elements that represent the sets in the families.
      The sets may occur in an unspecified order. The elements in the lists
      occur in increasing order. *)

  val pp_dot : Format.formatter -> t -> unit
  (** Pretty-printer of the representation of the ZDD as a graph in the DOT
      format. Dashed edges are 0 edges, solid edges are 1 edges. Edges that
      start with a dot are 0-attributed edges. *)
end

(** Signature of upward-closed sets, represented as the set of their minimal
    elements *)
module type UPSET = sig
  type elt
  type t

  val hash : t -> int
  (** Hash function *)

  val compare : t -> t -> int
  (** Total order *)

  val equal : t -> t -> bool
  (** Equality test *)

  val pp : Format.formatter -> t -> unit
  (** Pretty-printer *)

  val empty : t
  (** The empty family *)

  val full : t
  (** The full family *)

  val above : elt list -> t
  (** [above [x1;... ; xn]] is the family of the sets that include
      [{x1,..., xn}]. *)

  val subset : t -> t -> bool
  (** Inclusion test *)

  val union : t -> t -> t
  (** Union of set families *)

  val inter : t -> t -> t
  (** Intersection of set families *)

  val join : t -> t -> t
  (** [join t1 t2] is the upward closure of the family
      [{ s1 ∪ s2 | s1 ∈ t1, s2 ∈ t2 }]. This is the same as [inter s1 s2]. *)

  val meet : t -> t -> t
  (** [meet t1 t2] is the upward closure of the family
      [{ s1 ∩ s2 | s1 ∈ t1, s2 ∈ t2 }]. *)

  val minima : t -> elt list list
  (** The minimal elements of a family *)

  val subst : (elt -> t option) -> t -> t
  (** Substitution *)

  val iter_elt : (elt -> unit) -> t -> unit
  (** Iterator on the elements that occur in the minimal elements of the set
      family. The elements might be encountered more than once, and the order in
      which they are encountered is unspecified. *)

  val fold_elt : (elt -> 'a -> 'a) -> t -> 'a -> 'a
  (** Folder on the elements that occur in the minimal elements of the set
      family. The elements might be encountered more than once, and the order in
      which they are encountered is unspecified. *)

  val iter_minima : (elt list -> unit) -> t -> unit
  (** Iterator on the list of elements that represent the minimal sets in the
      families. The sets may occur in an unspecified order. The elements in the
      lists occur in increasing order. *)

  val fold_minima : (elt list -> 'a -> 'a) -> t -> 'a -> 'a
  (** Folder on the list of elements that represent the minimal sets in the
      families. The sets may occur in an unspecified order. The elements in the
      lists occur in increasing order. *)
end

(** Signature of downward-closed sets, represented as the set of their maximal
    elements *)
module type DOWNSET = sig
  type elt
  type t

  val hash : t -> int
  (** Hash function *)

  val compare : t -> t -> int
  (** Total order *)

  val equal : t -> t -> bool
  (** Equality test *)

  val pp : Format.formatter -> t -> unit
  (** Pretty-printer *)

  val empty : t
  (** The empty family *)

  val base : t
  (** The family [{ ∅ }] *)

  val below : elt list -> t
  (** [below [x1;... ; xn]] is the family of the sets that are included in
      [{x1,..., xn}]. *)

  val subset : t -> t -> bool
  (** Inclusion test *)

  val union : t -> t -> t
  (** Union of set families *)

  val inter : t -> t -> t
  (** Intersection of set families *)

  val join : t -> t -> t
  (** [join t1 t2] is the downward closure of the family
      [{ s1 ∪ s2 | s1 ∈ t1, s2 ∈ t2 }]. *)

  val meet : t -> t -> t
  (** [meet t1 t2] is the downward closure of the family
      [{ s1 ∩ s2 | s1 ∈ t1, s2 ∈ t2 }]. This is the same as [inter s1 s2]. *)

  val maxima : t -> elt list list
  (** The maximal elements of a family *)

  val subst : (elt -> t option) -> t -> t
  (** Substitution *)

  val iter_elt : (elt -> unit) -> t -> unit
  (** Iterator on the elements that occur in the maximal elements of the set
      family. The elements might be encountered more than once, and the order in
      which they are encountered is unspecified. *)

  val fold_elt : (elt -> 'a -> 'a) -> t -> 'a -> 'a
  (** Folder on the elements that occur in the maximal elements of the set
      family. The elements might be encountered more than once, and the order in
      which they are encountered is unspecified. *)

  val iter_maxima : (elt list -> unit) -> t -> unit
  (** Iterator on the list of elements that represent the maximal sets in the
      families. The sets may occur in an unspecified order. The elements in the
      lists occur in increasing order. *)

  val fold_maxima : (elt list -> 'a -> 'a) -> t -> 'a -> 'a
  (** Folder on the list of elements that represent the maximal sets in the
      families. The sets may occur in an unspecified order. The elements in the
      lists occur in increasing order. *)
end