HORIZON HASKELLDocslts/ghc-9.14.x45dd5372026-10-11Search names, modules, packages, or :: a typeCtrl K

GHC 9.14.1 · lts/ghc-9.14.x · 45dd537 · 2026-10-11

Modulenonempty-containers-0.3.6.0Haskell2010

Data.Set.NonEmpty.Internal

Unsafe internal-use functions used in the implementation of Data.Set.NonEmpty. These functions can potentially be used to break the abstraction of NESet and produce unsound sets, so be wary!

  • 2 types
  • 17 values
datadata NESet a
#

A non-empty (by construction) set of values a. At least one value exists in an NESet a at all times.

Functions that take an NESet can safely operate on it with the assumption that it has at least one item.

Functions that return an NESet provide an assurance that the result has at least one item.

Data.Set.NonEmpty re-exports the API of Data.Set, faithfully reproducing asymptotics, typeclass constraints, and semantics. Functions that ensure that input and output sets are both non-empty (like insert) return NESet, but functions that might potentially return an empty map (like delete) return a Set instead.

You can directly construct an NESet with the API from Data.Set.NonEmpty; it's more or less the same as constructing a normal Set, except you don't have access to empty. There are also a few ways to construct an NESet from a Set:

  1. The nonEmptySet smart constructor will convert a Set a into a Maybe (NESet a), returning Nothing if the original Set was empty.

  2. You can use the insertSet family of functions to insert a value into a Set to create a guaranteed NESet.

  3. You can use the IsNonEmpty and IsEmpty patterns to "pattern match" on a Set to reveal it as either containing a NESet or an empty map.

  4. withNonEmpty offers a continuation-based interface for deconstructing a Set and treating it as if it were an NESet.

You can convert an NESet into a Set with toSet or IsNonEmpty, essentially "obscuring" the non-empty property from the type.

Constructors

Instances17Foldable, Foldable1, Eq1, Ord1, Show1, IsList, …
  • Foldable NESetDefined in nonempty-containers-0.3.6.0 · Data.Set.NonEmpty.Internal

    Traverses elements in ascending order

    foldr1, foldl1, minimum, maximum are all total.

  • Foldable1 NESetDefined in nonempty-containers-0.3.6.0 · Data.Set.NonEmpty.Internal

    Traverses elements in ascending order

  • Eq1 NESetDefined in nonempty-containers-0.3.6.0 · Data.Set.NonEmpty.Internal
  • Ord1 NESetDefined in nonempty-containers-0.3.6.0 · Data.Set.NonEmpty.Internal
  • Show1 NESetDefined in nonempty-containers-0.3.6.0 · Data.Set.NonEmpty.Internal
  • Ord a => IsList (NESet a)Defined in nonempty-containers-0.3.6.0 · Data.Set.NonEmpty.Internal
  • Eq a => Eq (NESet a)Defined in nonempty-containers-0.3.6.0 · Data.Set.NonEmpty.Internal
  • (Data a, Ord a) => Data (NESet a)Defined in nonempty-containers-0.3.6.0 · Data.Set.NonEmpty.Internal
  • Ord a => Ord (NESet a)Defined in nonempty-containers-0.3.6.0 · Data.Set.NonEmpty.Internal
  • (Read a, Ord a) => Read (NESet a)Defined in nonempty-containers-0.3.6.0 · Data.Set.NonEmpty.Internal
  • Show a => Show (NESet a)Defined in nonempty-containers-0.3.6.0 · Data.Set.NonEmpty.Internal
  • Ord a => Semigroup (NESet a)Defined in nonempty-containers-0.3.6.0 · Data.Set.NonEmpty.Internal

    Left-biased union

  • NFData a => NFData (NESet a)Defined in nonempty-containers-0.3.6.0 · Data.Set.NonEmpty.Internal
  • (FromJSON a, Ord a) => FromJSON (NESet a)Defined in nonempty-containers-0.3.6.0 · Data.Set.NonEmpty.Internal
  • ToJSON a => ToJSON (NESet a)Defined in nonempty-containers-0.3.6.0 · Data.Set.NonEmpty.Internal
  • Ord a => IsNonEmptyList (NESet a)Defined in nonempty-containers-0.3.6.0 · Data.Containers.NonEmpty.List
  • type Item (NESet a) = aDefined in nonempty-containers-0.3.6.0 · Data.Set.NonEmpty.Internal
valuetoSet :: NESet a -> Set a
#

O(log n). Convert a non-empty set back into a normal possibly-empty map, for usage with functions that expect Set.

Can be thought of as "obscuring" the non-emptiness of the set in its type. See the Data.Set.NonEmpty.IsNotEmpty pattern.

nonEmptySet and maybe empty toSet form an isomorphism: they are perfect structure-preserving inverses of eachother.

toSet (fromList ((3,"a") :| [(5,"b")])) == Data.Set.fromList [(3,"a"), (5,"b")]
valuesize :: NESet a -> Int
#

O(1). The number of elements in the set. Guaranteed to be greater than zero.

valueunion :: Ord a => NESet a -> NESet a -> NESet a
#

O(m*log(n/m + 1)), m <= n. The union of two sets, preferring the first set when equal elements are encountered.

valuefoldr :: (a -> b -> b) -> b -> NESet a -> b
#

O(n). Fold the elements in the set using the given right-associative binary operator, such that foldr f z == foldr f z . toAscList.

For example,

elemsList set = foldr (:) [] set
valuefoldl :: (a -> b -> a) -> a -> NESet b -> a
#

O(n). Fold the elements in the set using the given left-associative binary operator, such that foldl f z == foldl f z . toAscList.

For example,

descElemsList set = foldl (flip (:)) [] set
valuefoldr' :: (a -> b -> b) -> b -> NESet a -> b
#

O(n). A strict version of foldr. Each application of the operator is evaluated before using the result in the next application. This function is strict in the starting value.

valuefoldl' :: (a -> b -> a) -> a -> NESet b -> a
#

O(n). A strict version of foldl. Each application of the operator is evaluated before using the result in the next application. This function is strict in the starting value.

valuemerge :: NESet a -> NESet a -> NESet a
#

Unsafely merge two disjoint sets. Only legal if all items in the first set are less than all items in the second set

valuevalid :: Ord a => NESet a -> Bool
#

O(n). Test if the internal set structure is valid.

valueinsertMinSet :: a -> Set a -> Set a
#

O(log n). Insert new value into a set where values are strictly greater than the new values That is, the new value must be strictly less than all values present in the Set. /The precondition is not checked./

While this has the same asymptotics as Data.Set.insert, it saves a constant factor for value comparison (so may be helpful if comparison is expensive) and also does not require an Ord instance for the value type.

valueinsertMaxSet :: a -> Set a -> Set a
#

O(log n). Insert new value into a set where values are /strictly less than the new value. That is, the new value must be strictly greater than all values present in the Set. The precondition is not checked./

While this has the same asymptotics as Data.Set.insert, it saves a constant factor for value comparison (so may be helpful if comparison is expensive) and also does not require an Ord instance for the value type.