HORIZON HASKELLDocslts/ghc-9.10.xc74966e2026-09-27Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27

Moduledlist-1.0Haskell2010

Data.DList.DNonEmpty

A non-empty difference list is a difference list paired with a head element. Like the difference list, it supports \mathcal{O}(1) append and snoc operations.

This module provides the type for a non-empty difference list, DNonEmpty, and a collection of supporting functions for (a) converting to and from NonEmpty and DList and (b) operating efficiently on DNonEmpty values. The functions also retain the non-strict semantics of NonEmpty.

  • 1 type
  • 12 values
  • Packagedlist-1.0
  • Exports13
  • LanguageHaskell2010
  • LicenceBSD-3-Clause
  • SourceDNonEmpty.hs

Non-Empty Difference List Type

1 declaration
datadata DNonEmpty a
#

A non-empty difference list is a pair of a head element and a (possibly empty) difference list.

Just as DList is a representation of a list, so is DNonEmpty a representation of a NonEmpty. DNonEmpty supports \mathcal{O}(1) append and snoc operations, making it useful for replacing frequent applications of <> on NonEmpty (which is implemented with ++), especially if those uses are left-nested (e.g. (a <> b) <> c ).

Unlike DList, DNonEmpty is not an abstract type: its constructor is exported. An alternative definition of DNonEmpty is:

newtype DNonEmpty a = DNonEmpty ([a] -> NonEmpty a)

This type would need to be abstract to avoid producing DNonEmpty values that are not isomorphic to NonEmpty values. However, this type would also require some functions (such as map) to be implemented with fromNonEmpty (and thus ++), which could introduce efficiencies.

Constructors

Instances13Monad, Functor, Applicative, Foldable, IsList, Eq, …

Conversion

4 declarations
valuefromNonEmpty :: NonEmpty a -> DNonEmpty a
#

fromNonEmpty xs is a DNonEmpty representing the NonEmpty xs.

fromNonEmpty obeys the laws:

toNonEmpty . fromNonEmpty = id
fromNonEmpty . toNonEmpty = id

As with fromList, this function is implemented with ++. Repeated uses of fromNonEmpty are just as inefficient as repeated uses of ++. If you find yourself doing some form of the following (possibly indirectly), you may not be taking advantage of the DNonEmpty representation and library:

fromNonEmpty . f . toNonEmpty

More likely, you will convert from a NonEmpty, perform some operation on the DNonEmpty, and convert back to a NonEmpty:

toNonEmpty . g . fromNonEmpty
valuetoNonEmpty :: DNonEmpty a -> NonEmpty a
#

toNonEmpty xs is the NonEmpty represented by xs.

toNonEmpty obeys the laws:

toNonEmpty . fromNonEmpty = id
fromNonEmpty . toNonEmpty = id

As with toList, evaluating toNonEmpty xs may “collapse” the chain of function composition underlying many DList functions (append in particular) used to construct the tail of xs. This may affect any efficiency you achieved due to laziness in the construction.

Basic Functions

8 declarations