March Docs

Set

Set module: persistent hash-array-mapped-trie set.

Set(a) is an immutable, hash-indexed collection backed by a HAMT via the Map module (Set(a) ≅ Map(a, Unit) internally). This gives O(log₃₂ n) ≈ O(1) amortized membership tests, insert, and delete.

Operations that need key identity take an explicit comparator: cmp : a -> a -> Bool where cmp(a)(b) = true means a < b

Standard comparators: fn(a) -> fn(b) -> a < b for Int elements fn(a) -> fn(b) -> a < b for String elements (lexicographic)

Iteration order is hash-traversal order (not sorted). Use to_list then sort externally if sorted order is needed.

Types

ptypeSEntrySEntry(a) =#
ptypeSetSet(a) = HamtSet(Int, SEntry(a))#

Functions

fncontainscontains(s, elem, cmp) : {Bool | _ == member(elem, elts(s))}#
fndifferencedifference(a, b, cmp) : {Set(a) | elts(_) == diff(elts(a), elts(b))}#
fnemptyempty() : {Set(a) | elts(_) == empty}#
fneqeq(a, b, cmp) : {Bool | _ == (elts(a) == elts(b))}#
fnfoldfold(s, acc, f)#

Left fold over all elements. Argument order: collection first, init second, callback last (uncurried-collection convention). The callback f is called uncurried as f(acc, elem) = new_acc.

fnfrom_listfrom_list(xs, cmp) : {Set(a) | elts(_) == elts(xs)}#
fninsertinsert(s, elem, cmp) : {Set(a) | elts(_) == union(elts(s), singleton(elem))}#
fnintersectionintersection(a, b, cmp) : {Set(a) | elts(_) == inter(elts(a), elts(b))}#
fnis_emptyis_empty(s) : {Bool | _ == (elts(s) == empty)}#
fnis_subsetis_subset(a, b, cmp) : {Bool | _ == subset(elts(a), elts(b))}#
fnremoveremove(s, elem, cmp) : {Set(a) | elts(_) == diff(elts(s), singleton(elem))}#
fnsingletonsingleton(x) : {Set(a) | elts(_) == singleton(x)}#
fnsizesize(s)#

Returns the number of elements in the set. O(1).

fnto_listto_list(s) : {List(a) | elts(_) == elts(s)}#
fnunionunion(a, b, cmp) : {Set(a) | elts(_) == union(elts(a), elts(b))}#