The HashConsSet structure
Author: John Reppy
Keywords: Standard ML, SML, SML/NJ Library

The SML of NJ Library
The `HashConsSet` structure

The `HashConsSet` structure implements finite sets of hash-consed objects.

## Synopsis

    signature HASH_CONS_SET
    structure HashConsSet : HASH_CONS_SET

## Interface

    type 'a obj = 'a HashCons.obj

    type 'a set

    val empty : 'a set

    val singleton : 'a obj -> 'a set

    val fromList : 'a obj list -> 'a set

    val add  : 'a set * 'a obj -> 'a set
    val add' : ('a obj * 'a set) -> 'a set

    val addList : 'a set * 'a obj list -> 'a set

    val subtract  : 'a set * 'a obj -> 'a set
    val subtract' : ('a obj * 'a set) -> 'a set

    val subtractList : 'a set * 'a obj list -> 'a set

    val delete : 'a set * 'a obj -> 'a set

    val member : 'a set * 'a obj -> bool

    val isEmpty : 'a set -> bool

    val equal : ('a set * 'a set) -> bool

    val compare : ('a set * 'a set) -> order

    val isSubset : ('a set * 'a set) -> bool

    val disjoint : 'a set * 'a set -> bool

    val numItems : 'a set ->  int

    val toList : 'a set -> 'a obj list
    val listItems : 'a set -> 'a obj list

    val union : 'a set * 'a set -> 'a set
    val intersection : 'a set * 'a set -> 'a set
    val difference : 'a set * 'a set -> 'a set

    val map : ('a obj -> 'b obj) -> 'a set -> 'b set

    val mapPartial : ('a obj -> 'b obj option) -> 'a set -> 'b set

    val app : ('a obj -> unit) -> 'a set -> unit

    val foldl : ('a obj * 'b -> 'b) -> 'b -> 'a set -> 'b
    val foldr : ('a obj * 'b -> 'b) -> 'b -> 'a set -> 'b

    val partition : ('a obj -> bool) -> 'a set -> ('a set * 'a set)

    val filter : ('a obj -> bool) -> 'a set -> 'a set

    val all : ('a obj -> bool) -> 'a set -> bool
    val exists : ('a obj -> bool) -> 'a set -> bool

    val find : ('a obj -> bool) -> 'a set -> 'a obj option

## Description

`type 'a obj = 'a HashCons.obj`

  The elements in the set are hash-cons objects.

`type 'a set`

  A finite set of `'a obj` values.

`val empty : 'a set`

  The empty set.

`val singleton : 'a obj -> 'a set`

  `singleton obj` creates a singleton set containing `obj`.

`val fromList : 'a obj list -> 'a set`

  `fromList objs` creates a set from the list of objects.

`val add : 'a set * 'a obj -> 'a set`

  `add (set, obj)` adds the object to the set.

`val add' : ('a obj * 'a set) -> 'a set`

  `add' (obj, set)` adds the object to the set.

`val addList : 'a set * 'a obj list -> 'a set`

  `addList (set, objs)` adds the list of objects to the set.

`val subtract : 'a set * 'a obj -> 'a set`

  `subtract (set, obj)` removes the object `obj` from `set`. Acts as the identity if `obj` is not in the set.

`val subtract' : ('a obj * 'a set) -> 'a set`

  `subtract (obj, set)` removes the object `obj` from `set`. Acts as the identity if `obj` is not in the set.

`val delete : 'a set * 'a obj -> 'a set`

  `delete (set, obj)` removes the object `obj` from `set`. Unlike `subtract`, this function raises the `NotFound` exception if `obj` is not in the set.

`val member : 'a set * 'a obj -> bool`

  `member (obj, set)` returns `true` if, and only if, `obj` is an element of `set`.

`val isEmpty : 'a set -> bool`

  `isEmpty set` returns true if, and only if, `set` is empty.

`val equal : ('a set * 'a set) -> bool`

  `equal (set1, set2)` returns true if, and only if, the two sets are equal (i.e., they contain the same elements).

`val compare : ('a set * 'a set) -> order`

  `compare (set1, set2)` returns the lexical order of the two sets.

`val isSubset : ('a set * 'a set) -> bool`

  `isSubset (set1, set2)` returns true if, and only if, `set1` is a subset of `set2` (i.e., any element of `set1` is an element of `set2`).

`val disjoint : 'a set * 'a set -> bool`

  `equal (set1, set2)` returns true if, and only if, the two sets are disjoint (i.e., their intersection is empty).

`val numItems : 'a set -> int`

  `numItems set` returns the number of items in the `set`.

`val toList : 'a set -> 'a obj list`

  `toList set` returns a list of the objects in `set`.

`val union : 'a set * 'a set -> 'a set`

  `union (set1, set2)` returns the union of the two sets.

`val intersection : 'a set * 'a set -> 'a set`

  `intersection (set1, set2)` returns the intersection of the two sets.

`val difference : 'a set * 'a set -> 'a set`

  `difference (set1, set2)` returns the difference of the two sets; i.e., the set of objects that are in `set1`, but not in `set2`.

`val map : ('a obj -> 'b obj) -> 'a set -> 'b set`

  `map f set` constructs a new set from the result of applying the function `f` to the elements of `set`. This expression is equivalent to

      fromList (List.map f (toList set))

`val mapPartial : ('a obj -> 'b obj option) -> 'a set -> 'b set`

  `mapPartial f set` constructs a new set from the result of applying the function `f` to the elements of `set`. This expression is equivalent to

      fromList (List.mapPartial f (toList set))

`val app : ('a obj -> unit) -> 'a set -> unit`

  `app f set` applies the function `f` to the objects in `set`. This expression is equivalent to

      List.app f (toList set)

`val fold : ('a obj * 'b -> 'b) -> 'b -> 'a set -> 'b`

  `fold f init set` folds the function `f` over the objects in `set` using `init` as the initial value. This expression is equivalent to

      List.foldl f init (toList set)

  Although the order in which the elements are processed is unspecified.

`val partition : ('a obj -> bool) -> 'a set -> ('a set * 'a set)`

  `partition pred set` returns a pair of disjoint sets `(tSet, fSet)`, where the predicate `pred` returns true for every element of `tSet`, `false` for every element of `fSet`, and `set` is the union of `tSet` and `fSet`.

`val filter : ('a obj -> bool) -> 'a set -> 'a set`

  `filter pred set` filters out any elements of set for which the predicate `pred` returns false. This expression is equivalent to

      #1 (partition pred set)

`val all : ('a obj -> bool) -> 'a set -> bool`

  `all pred set` returns `true` if, and only if, `pred obj` returns true for all elements `obj` in `set`.

`val exists : ('a obj -> bool) -> 'a set -> bool`

  `exists pred set` returns `true` if, and only if, there exists an element `obj` in `set` such that `pred obj` returns `true`.

`val find : ('a obj -> bool) -> 'a set -> 'a obj option`

  `find pred set` returns `SOME obj` if there exists an object `obj` in the set for which `pred obj` returns `true`; otherwise `NONE` is returned.

### Deprecated functions

The following functions are part of the interface, but have been deprecated.

`val listItems : 'a set -> 'a obj list`

  Use `toList` instead.

`val foldl : ('a obj * 'b -> 'b) -> 'b -> 'a set -> 'b`

  Use `fold` instead.

`val foldr : ('a obj * 'b -> 'b) -> 'b -> 'a set -> 'b`

  Use `fold` instead.

## See Also

`HashCons`, `HashConsMap`, The HashCons Library

SML/NJ Version 110.99.9 (November 4, 2025)
Last updated 2025-11-04 15:48:25 UTC