HORIZON HASKELLDocslts/ghc-9.10.x248f8f02026-10-05Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · 248f8f0 · 2026-10-05

Moduleuniverse-base-1.1.4Haskell2010

Data.Universe.Helpers

  • 2 types
  • 13 values

This module is for functions that are useful for writing instances, but not necessarily for using them (and hence are not exported by the main module to avoid cluttering up the namespace).

Building lists

9 declarations
valueuniverseDef :: (Bounded a, Enum a) => [a]
#

For many types, the universe should be [minBound .. maxBound]; universeDef makes it easy to make such types an instance of Universe via the snippet

instance Universe Foo where universe = universeDef
valueinterleave :: [[a]] -> [a]
#

Fair n-way interleaving: given a finite number of (possibly infinite) lists, produce a single list such that whenever v has finite index in one of the input lists, v also has finite index in the output list. No list's elements occur more frequently (on average) than another's.

valuediagonal :: [[a]] -> [a]
#

Unfair n-way interleaving: given a possibly infinite number of (possibly infinite) lists, produce a single list such that whenever v has finite index in an input list at finite index, v also has finite index in the output list. Elements from lists at lower index occur more frequently, but not exponentially so.

valuediagonals :: [[a]] -> [[a]]
#

Like diagonal, but expose a tiny bit more (non-semantic) information: if you lay out the input list in two dimensions, each list in the result will be one of the diagonals of the input. In particular, each element of the output will be a list whose elements are each from a distinct input list.

value(+++) :: [a] -> [a] -> [a]
#

Fair 2-way interleaving.

valuecartesianProduct :: (a -> b -> c) -> [a] -> [b] -> [c]
#

Slightly unfair 2-way Cartesian product: given two (possibly infinite) lists, produce a single list such that whenever v and w have finite indices in the input lists, (v,w) has finite index in the output list. Lower indices occur as the fst part of the tuple more frequently, but not exponentially so.

valuechoices :: [[a]] -> [[a]]
#

Slightly unfair n-way Cartesian product: given a finite number of (possibly infinite) lists, produce a single list such that whenever vi has finite index in list i for each i, [v1, ..., vn] has finite index in the output list.

Building cardinalities

4 declarations

These functions are handy for inheriting the definition of cardinality in a newtype instance. For example, one might write

newtype Foo = Foo Bar
instance Finite Foo where cardinality = retagWith Foo cardinality
valueretag :: Tagged s b -> Tagged t b
#

Some times you need to change the tag you have lying around. Idiomatic usage is to make a new combinator for the relationship between the tags that you want to enforce, and define that combinator using retag.

data Succ n
retagSucc :: Tagged n a -> Tagged (Succ n) a
retagSucc = retag
newtypenewtype Tagged (s :: k) b
#

A Tagged s b value is a value b with an attached phantom type s. This can be used in place of the more traditional but less safe idiom of passing in an undefined value with the type, because unlike an (s -> b), a Tagged s b can't try to use the argument s as a real value.

Moreover, you don't have to rely on the compiler to inline away the extra argument, because the newtype is "free"

Tagged has kind k -> * -> * if the compiler supports PolyKinds, therefore there is an extra k showing in the instance haddocks that may cause confusion.

Constructors

Instances46Generic1, Bifoldable, Bifoldable1, Bifunctor, Bitraversable, Eq2, …
datadata Natural
#

Natural number

Invariant: numbers <= 0xffffffffffffffff use the NS constructor

Instances19Enum, Eq, Integral, Data, Num, Ord, …
  • Enum NaturalDefined in ghc-internal-9.1003.0 · GHC.Internal.Enum
  • Eq NaturalDefined in ghc-bignum-1.3 · GHC.Num.Natural
  • Integral NaturalDefined in ghc-internal-9.1003.0 · GHC.Internal.Real
  • Data NaturalDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Data
  • Num NaturalDefined in ghc-internal-9.1003.0 · GHC.Internal.Num

    Note that Natural's Num instance isn't a ring: no element but 0 has an additive inverse. It is a semiring though.

  • Ord NaturalDefined in ghc-bignum-1.3 · GHC.Num.Natural
  • Read NaturalDefined in ghc-internal-9.1003.0 · GHC.Internal.Read
  • Real NaturalDefined in ghc-internal-9.1003.0 · GHC.Internal.Real
  • Show NaturalDefined in ghc-internal-9.1003.0 · GHC.Internal.Show
  • Ix NaturalDefined in ghc-internal-9.1003.0 · GHC.Internal.Ix
  • Bits NaturalDefined in ghc-internal-9.1003.0 · GHC.Internal.Bits
  • PrintfArg NaturalDefined in base-4.20.2.0 · Text.Printf
  • NFData NaturalDefined in deepseq-1.5.0.0 · Control.DeepSeq
  • Universe NaturalDefined in universe-base-1.1.4 · Data.Universe.Class
  • RationalUniverse NaturalDefined in universe-base-1.1.4 · Data.Universe.Class
  • Lift NaturalDefined in template-haskell-2.22.0.0 · Language.Haskell.TH.Syntax
  • TestCoercion SNatDefined in ghc-internal-9.1003.0 · GHC.Internal.TypeNats
  • TestEquality SNatDefined in ghc-internal-9.1003.0 · GHC.Internal.TypeNats
  • type Compare a b = CmpNat a bDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Type.Ord

Debugging

2 declarations

These functions exist primarily as a specification to test against.

valueunfairCartesianProduct :: (a -> b -> c) -> [a] -> [b] -> [c]
#

Very unfair 2-way Cartesian product: same guarantee as the slightly unfair one, except that lower indices may occur as the fst part of the tuple exponentially more frequently.

valueunfairChoices :: [[a]] -> [[a]]
#

Very unfair n-way Cartesian product: same guarantee as the slightly unfair one, but not as good in the same sense that the very unfair 2-way product is worse than the slightly unfair 2-way product.