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

GHC 9.14.1 · lts/ghc-9.14.x · 3e2d568 · 2026-10-11

Modulenonempty-containers-0.3.6.0Haskell2010

Data.Containers.NonEmpty

Non-Empty Typeclass

Provides the typeclass HasNonEmpty, which abstracts over different types which have a "non-empty" variant.

Used to convert between and in between possibly-empty and non-empty types. Instances are provided for all modules in this package, as well as for NonEmpty in base and NonEmptyVector.

  • 1 class
  • 2 values
classclass HasNonEmpty s where
#

If s is an instance of HasNonEmpty, it means that there is a corresponding "non-empty" version of s, NE s.

In order for things to be well-behaved, we expect that nonEmpty and maybe empty fromNonEmpty should form an isomorphism (or that withNonEmpty empty fromNonEmpty == id. In addition, the following properties should hold for most exectations:

  • (x == empty) ==> isEmpty x
  • (x == empty) ==> isNothing (nonEmpty x)
  • isEmpty x    ==> isNothing (nonEmpty x)
  • unsafeToNonEmpty x == fromJust (nonEmpty x)
  • Usually, not (isEmpty x) ==> isJust (nonEmpty x), but this isn't necessary.

Associated types

  • type family NE s

    NE s is the "non-empty" version of s.

Methods

Instances7HasNonEmpty, …
  • HasNonEmpty IntSetDefined in nonempty-containers-0.3.6.0 · Data.Containers.NonEmpty
  • HasNonEmpty (IntMap a)Defined in nonempty-containers-0.3.6.0 · Data.Containers.NonEmpty
  • HasNonEmpty (Seq a)Defined in nonempty-containers-0.3.6.0 · Data.Containers.NonEmpty
  • HasNonEmpty (Set a)Defined in nonempty-containers-0.3.6.0 · Data.Containers.NonEmpty
  • HasNonEmpty (Vector a)Defined in nonempty-containers-0.3.6.0 · Data.Containers.NonEmpty
  • HasNonEmpty [a]Defined in nonempty-containers-0.3.6.0 · Data.Containers.NonEmpty
  • HasNonEmpty (Map k a)Defined in nonempty-containers-0.3.6.0 · Data.Containers.NonEmpty
patternpattern IsNonEmpty :: HasNonEmpty s => NE s -> s
#

The IsNonEmpty and IsEmpty patterns allow you to treat a s as if it were either a IsNonEmpty n (where n is a non-empty version of s, type NE s) or an IsEmpty.

For example, you can pattern match on a list to get a NonEmpty (non-empty list):

safeHead :: [Int] -> Int
safeHead (IsNonEmpty (x :| _)) = x     -- here, the list was not empty
safehead IsEmpty               = 0     -- here, the list was empty

Matching on IsNonEmpty n means that the original input was not empty, and you have a verified-non-empty n :: NE s to use.

Note that because of the way coverage checking works for polymorphic pattern synonyms, you will unfortunatelly still get incomplete pattern match warnings if you match on both IsNonEmpty and NonEmpty, even though the two are meant to provide complete coverage. However, many instances of HasNonEmpty (like NEMap, NEIntMap, NESet, NEIntSet) will provide their own monomorphic versions of these patterns that can be verified as complete covers by GHC.

This is a bidirectional pattern, so you can use IsNonEmpty to convert a NE s back into an s, "obscuring" its non-emptiness (see fromNonEmpty).

patternpattern IsEmpty :: HasNonEmpty s => s
#

The IsNonEmpty and IsEmpty patterns allow you to treat a s as if it were either a IsNonEmpty n (where n is a non-empty version of s, type NE s) or an IsEmpty.

Matching on IsEmpty means that the original item was empty.

This is a bidirectional pattern, so you can use IsEmpty as an expression, and it will be interpreted as empty.

Note that because of the way coverage checking works for polymorphic pattern synonyms, you will unfortunatelly still get incomplete pattern match warnings if you match on both IsNonEmpty and NonEmpty, even though the two are meant to provide complete coverage. However, many instances of HasNonEmpty (like NEMap, NEIntMap, NESet, NEIntSet) will provide their own monomorphic versions of these patterns that can be verified as complete covers by GHC.

See IsNonEmpty for more information.