MoreLabels.Set.Make(3)

๐Ÿ’– MoreLabels.Set.Make(3) ๐Ÿ’–

okay bestie, welcome to the MoreLabels.Set.Make page from the OCaml library manual. this one is lowkey a functor era and it's giving "make your own set type" energy. let's go ๐Ÿซถ


โœจ NAME โœจ

MoreLabels.Set.Make โ€” bestie, it's a functor that builds you an implementation of the set structure, and it just needs a totally ordered type to do it. that's it. that's the whole vibe. ๐Ÿ’…


๐Ÿฆ‹ Module ๐Ÿฆ‹

Module MoreLabels.Set.Make


๐ŸŒธ Documentation ๐ŸŒธ

Module Make
 : (Ord : OrderedType) -> sig end

so, Make is a functor building an implementation of the set structure given a totally ordered type. fr fr, that's the entire deal.

Parameters:

  • "Ord" โ€” which is MoreLabels.Set.OrderedType. that's your totally-ordered-type plug-in, iykyk ๐ŸŽ€

๐ŸŽ€ Sets ๐ŸŽ€

  • type elt โ€” the type of the set elements.
  • type t โ€” the type of sets. your actual set type, iconic.

  • val empty : t โ€” the empty set. the blank canvas. the "no thoughts head empty" of sets ๐Ÿซง
  • val add : elt -> t -> t โ€” add x s returns a set containing all elements of s, plus x. if x was already in s, s is returned unchanged โ€” and the result of the function is then physically equal to s. slay, no wasted allocation ๐Ÿ’…
Before 4.03: physical equality was not ensured. so like, old OCaml said "maybe", and we don't stan that anymore ๐Ÿ’€
  • val singleton : elt -> t โ€” singleton x returns the one-element set containing only x. main character energy, it's all about them โœจ
  • val remove : elt -> t -> t โ€” remove x s returns a set containing all elements of s, except x. if x was not in s, s is returned unchanged โ€” result physically equal to s, in that case. so we're not allocating for nothing ๐ŸŽ€
Before 4.03: physical equality was not ensured. same lore as before ๐Ÿซ 
  • val union : t -> t -> t โ€” set union. combine the friend groups ๐Ÿ’–
  • val inter : t -> t -> t โ€” set intersection. only the mutuals ๐Ÿ’…
  • val disjoint : t -> t -> bool โ€” test if two sets are disjoint. like, do they share zero elements? yes/no. Since 4.08
  • val diff : t -> t -> t โ€” set difference: diff s1 s2 contains the elements of s1 that are not in s2. the "unfollowed back" of sets ๐Ÿ˜ญ
  • val cardinal : t -> int โ€” return the number of elements of a set. the headcount ๐Ÿงฎ

๐ŸŒˆ Elements ๐ŸŒˆ

  • val elements : t -> elt list โ€” return the list of all elements of the given set. the returned list is sorted in increasing order with respect to the ordering Ord.compare, where Ord is the argument given to MoreLabels.Set.Make. sorted, bestie, always sorted โœจ
  • val min_elt : t -> elt โ€” return the smallest element of the given set (with respect to the Ord.compare ordering), or raise Not_found if the set is empty. the littlest guy ๐Ÿฅบ
  • val min_elt_opt : t -> elt option โ€” return the smallest element of the given set (with respect to the Ord.compare ordering), or None if the set is empty. same thing but the polite option version ๐ŸŽ€ Since 4.05
  • val max_elt : t -> elt โ€” same as MoreLabels.Set.S.min_elt, but returns the largest element of the given set. big boss energy ๐Ÿ‘‘
  • val max_elt_opt : t -> elt option โ€” same as MoreLabels.Set.S.min_elt_opt, but returns the largest element of the given set. Since 4.05
  • val choose : t -> elt โ€” return one element of the given set, or raise Not_found if the set is empty. which element is chosen is unspecified, but equal elements will be chosen for equal sets. so it's a mystery pick, but a consistent mystery pick, and we respect that ๐ŸŽฒ โ€” ngl, "unspecified which one" is highkey important, so don't go depending on it being a particular element, ok?
  • val choose_opt : t -> elt option โ€” return one element of the given set, or None if the set is empty. which element is chosen is unspecified, but equal elements will be chosen for equal sets. Since 4.05

๐Ÿฆ‹ Searching ๐Ÿฆ‹

  • val find : elt -> t -> elt โ€” find x s returns the element of s equal to x (according to Ord.compare), or raise Not_found if no such element exists. note bestie: it gives you back the element in the set, not the one you passed in ๐Ÿ‘€ Since 4.01
  • val find_opt : elt -> t -> elt option โ€” find_opt x s returns the element of s equal to x (according to Ord.compare), or None if no such element exists. Since 4.05
  • val find_first : f:(elt -> bool) -> t -> elt โ€” find_first ~f s, where f is a monotonically increasing function, returns the lowest element e of s such that f e, or raises Not_found if no such element exists.

for example, find_first (fun e -> Ord.compare e x >= 0) s will return the first element e of s where Ord.compare e x >= 0 (intuitively: e >= x), or raise Not_found if x is greater than any element of s. it's giving lower bound, and we're obsessed ๐Ÿ’œ Since 4.05

  • val find_first_opt : f:(elt -> bool) -> t -> elt option โ€” find_first_opt ~f s, where f is a monotonically increasing function, returns an option containing the lowest element e of s such that f e, or None if no such element exists. Since 4.05
  • val find_last : f:(elt -> bool) -> t -> elt โ€” find_last ~f s, where f is a monotonically decreasing function, returns the highest element e of s such that f e, or raises Not_found if no such element exists. yeah it's decreasing here, don't mix them up bestie, that's a whole trap ๐Ÿ™ˆ Since 4.05
  • val find_last_opt : f:(elt -> bool) -> t -> elt option โ€” find_last_opt ~f s, where f is a monotonically decreasing function, returns an option containing the highest element e of s such that f e, or None if no such element exists. Since 4.05

โญ Traversing โญ

  • val iter : f:(elt -> unit) -> t -> unit โ€” iter ~f s applies f in turn to all elements of s. the elements of s are presented to f in increasing order with respect to the ordering over the type of the elements. in order, always ๐Ÿ’ซ
  • val fold : f:(elt -> 'acc -> 'acc) -> t -> init:'acc -> 'acc โ€” fold ~f s ~init computes (f xN ... (f x2 (f x1 init))...), where x1 ... xN are the elements of s, in increasing order. the way that nesting goes, it's honestly art ๐ŸŽจ

๐Ÿงš Transforming ๐Ÿงš

  • val map : f:(elt -> elt) -> t -> t โ€” map ~f s is the set whose elements are f a0, f a1 ... f aN, where a0, a1 ... aN are the elements of s.

the elements are passed to f in increasing order with respect to the ordering over the type of the elements.

if no element of s is changed by f, s is returned unchanged. (if each output of f is physically equal to its input, the returned set is physically equal to s.) zero-waste glow-up โœจ Since 4.04

  • val filter : f:(elt -> bool) -> t -> t โ€” filter ~f s returns the set of all elements in s that satisfy predicate f. if every element in s satisfies f, s is returned unchanged (the result of the function is then physically equal to s). nothing dropped, nothing reallocated ๐Ÿ’…
Before 4.03: physical equality was not ensured. yeah yeah, same note ๐Ÿ˜Œ
  • val filter_map : f:(elt -> elt option) -> t -> t โ€” filter_map ~f s returns the set of all v such that f x = Some v for some element x of s.

for example,

filter_map (fun n -> if n mod 2 = 0 then Some (n / 2) else None) s

is the set of halves of the even elements of s. halve the evens, yeet the odds, iconic behaviour tbh โœ‚๏ธ

if no element of s is changed or dropped by f (if f x = Some x for each element x), then s is returned unchanged: the result of the function is then physically equal to s. Since 4.11

  • val partition : f:(elt -> bool) -> t -> t * t โ€” partition ~f s returns a pair of sets (s1, s2), where s1 is the set of all the elements of s that satisfy the predicate f, and s2 is the set of all the elements of s that do not satisfy f. two friend groups, no drama ๐ŸŒธ
  • val split : elt -> t -> t * bool * t โ€” split x s returns a triple (l, present, r), where l is the set of elements of s that are strictly less than x; r is the set of elements of s that are strictly greater than x; present is false if s contains no element equal to x, or true if s contains an element equal to x. three-for-one, we love a triple ๐ŸŽ

๐Ÿ’œ Predicates and comparisons ๐Ÿ’œ

  • val is_empty : t -> bool โ€” test whether a set is empty or not. vibe check on the emptiness ๐Ÿซง
  • val is_singleton : t -> bool โ€” test whether a set has exactly one element or not. solo-set check ๐ŸŽค Since 5.5
  • val mem : elt -> t -> bool โ€” mem x s tests whether x belongs to the set s. are they in the group chat? ๐Ÿ‘€
  • val equal : t -> t -> bool โ€” equal s1 s2 tests whether the sets s1 and s2 are equal, that is, contain equal elements.
  • val compare : t -> t -> int โ€” total ordering between sets. can be used as the ordering function for doing sets of sets. yes bestie, sets of sets, it's recursion all the way down ๐ŸŒ€
  • val subset : t -> t -> bool โ€” subset s1 s2 tests whether the set s1 is a subset of the set s2.
  • val for_all : f:(elt -> bool) -> t -> bool โ€” for_all ~f s checks if all elements of the set satisfy the predicate f. every single one, no exceptions โœ‹
  • val exists : f:(elt -> bool) -> t -> bool โ€” exists ~f s checks if at least one element of the set satisfies the predicate f. just one is enough, we're not picky ๐Ÿ“

๐Ÿ”ฅ Converting ๐Ÿ”ฅ

  • val to_list : t -> elt list โ€” to_list s is MoreLabels.Set.S.elements s. that's it, that's the whole function, it's a rebrand ๐Ÿ’… Since 5.1
  • val of_list : elt list -> t โ€” of_list l creates a set from a list of elements. this is usually more efficient than folding add over the list, except perhaps for lists with many duplicated elements. so use it, unless your list is like 90% dupes ๐Ÿซ  Since 4.02
  • val to_seq_from : elt -> t -> elt Seq.t โ€” to_seq_from x s iterates on a subset of the elements of s in ascending order, from x or above. Since 4.07
  • val to_seq : t -> elt Seq.t โ€” iterate on the whole set, in ascending order. โฌ†๏ธ Since 4.07
  • val to_rev_seq : t -> elt Seq.t โ€” iterate on the whole set, in descending order. โฌ‡๏ธ Since 4.12
  • val add_seq : elt Seq.t -> t -> t โ€” add the given elements to the set, in order. Since 4.07
  • val of_seq : elt Seq.t -> t โ€” build a set from the given elements. Since 4.07

and that's the whole page, bestie!! ๐ŸŽ€ you ate that. tysm for reading, ily, and remember: sorted, specified, and slaying ๐Ÿ’–

sign-off szn: this lil doc was generated by OCamldoc, dated 2026-06-22, for the page MoreLabels.Set.Make(3). bye bestie, go build a set ๐Ÿฆ‹โœจ
๐Ÿ“ฆ where this page comes from
  • arch rolling: ocaml 5.5.0-1 ยท LGPL2.1 AND custom: QPL-1.0
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms