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

  • Packagesop-core-0.5.0.2
  • Exports73
  • LanguageHaskell2010
  • LicenceBSD-3-Clause
  • SourceNP.hs

Datatypes

3 declarations
datadata NP (a :: k -> Type) (b :: [k]) where
#

An n-ary product.

The product is parameterized by a type constructor f and indexed by a type-level list xs. The length of the list determines the number of elements in the product, and if the i-th element of the list is of type x, then the i-th element of the product is of type f x.

The constructor names are chosen to resemble the names of the list constructors.

Two common instantiations of f are the identity functor I and the constant functor K. For I, the product becomes a heterogeneous list, where the type-level list describes the types of its components. For K a, the product becomes a homogeneous list, where the contents of the type-level list are ignored, but its length still specifies the number of elements.

In the context of the SOP approach to generic programming, an n-ary product describes the structure of the arguments of a single data constructor.

Examples:

I 'x'    :* I True  :* Nil  ::  NP I       '[ Char, Bool ]
K 0      :* K 1     :* Nil  ::  NP (K Int) '[ Char, Bool ]
Just 'x' :* Nothing :* Nil  ::  NP Maybe   '[ Char, Bool ]

Constructors

  • Nil :: NP a '[]
  • (:*) :: a x -> NP a xs -> NP a (x ': xs)infixr 5
Instances19HTrans, HAp, HCollapse, HPure, HSequence, HTraverse_, …
newtypenewtype POP (f :: k -> Type) (xss :: [[k]])
#

A product of products.

This is a newtype for an NP of an NP. The elements of the inner products are applications of the parameter f. The type POP is indexed by the list of lists that determines the lengths of both the outer and all the inner products, as well as the types of all the elements of the inner products.

A POP is reminiscent of a two-dimensional table (but the inner lists can all be of different length). In the context of the SOP approach to generic programming, a POP is useful to represent information that is available for all arguments of all constructors of a datatype.

Constructors

Instances19HTrans, HAp, HCollapse, HPure, HSequence, HTraverse_, …
  • HTrans POP POPDefined in sop-core-0.5.0.2 · Data.SOP.NP
  • HAp POPDefined in sop-core-0.5.0.2 · Data.SOP.NP
  • HCollapse POPDefined in sop-core-0.5.0.2 · Data.SOP.NP
  • HPure POPDefined in sop-core-0.5.0.2 · Data.SOP.NP
  • HSequence POPDefined in sop-core-0.5.0.2 · Data.SOP.NP
  • HTraverse_ POPDefined in sop-core-0.5.0.2 · Data.SOP.NP
  • Eq (NP (NP f) xss) => Eq (POP f xss)Defined in sop-core-0.5.0.2 · Data.SOP.NP
  • Ord (NP (NP f) xss) => Ord (POP f xss)Defined in sop-core-0.5.0.2 · Data.SOP.NP
  • Show (NP (NP f) xss) => Show (POP f xss)Defined in sop-core-0.5.0.2 · Data.SOP.NP
  • Semigroup (NP (NP f) xss) => Semigroup (POP f xss)Defined in sop-core-0.5.0.2 · Data.SOP.NP
  • Monoid (NP (NP f) xss) => Monoid (POP f xss)Defined in sop-core-0.5.0.2 · Data.SOP.NP
  • NFData (NP (NP f) xss) => NFData (POP f xss)Defined in sop-core-0.5.0.2 · Data.SOP.NP
  • type CollapseTo POP a = [[a]]Defined in sop-core-0.5.0.2 · Data.SOP.NP
  • type Prod POP = POPDefined in sop-core-0.5.0.2 · Data.SOP.NP
  • type UnProd POP = SOPDefined in sop-core-0.5.0.2 · Data.SOP.NS · orphan
  • type AllN POP c = All2 cDefined in sop-core-0.5.0.2 · Data.SOP.NP
  • type AllZipN POP c = AllZip2 cDefined in sop-core-0.5.0.2 · Data.SOP.NP
  • type Same POP = POPDefined in sop-core-0.5.0.2 · Data.SOP.NP
  • type SListIN POP = SListI2Defined in sop-core-0.5.0.2 · Data.SOP.NP
valueunPOP :: POP f xss -> NP (NP f) xss
#

Unwrap a product of products.

Constructing products

4 declarations
valuepure_NP :: SListI xs => (forall (a :: k). f a) -> NP f xs
#

Specialization of hpure.

The call pure_NP x generates a product that contains x in every element position.

Example:

Example2 expressions
pure_NP [] :: NP [] '[Char, Bool]"" :* [] :* Nilpure_NP (K 0) :: NP (K Int) '[Double, Int, String]K 0 :* K 0 :* K 0 :* Nil
valuepure_POP :: All SListI xss => (forall (a :: k). f a) -> POP f xss
#

Specialization of hpure.

The call pure_POP x generates a product of products that contains x in every element position.

valuecpure_NP :: All c xs => proxy c -> (forall (a :: k). c a => f a) -> NP f xs
#

Specialization of hcpure.

The call cpure_NP p x generates a product that contains x in every element position.

valuecpure_POP
  1. :: All2 c xss
  2. => proxy c
  3. -> forall (a :: k). c a => f a
  4. -> POP f xss
#

Specialization of hcpure.

The call cpure_NP p x generates a product of products that contains x in every element position.

Construction from a list

valuefromList :: SListI xs => [a] -> Maybe (NP (K a) xs)
#

Construct a homogeneous n-ary product from a normal Haskell list.

Returns Nothing if the length of the list does not exactly match the expected size of the product.

Application

2 declarations
valueap_NP :: NP (f -.-> g) xs -> NP f xs -> NP g xs
#

Specialization of hap.

Applies a product of (lifted) functions pointwise to a product of suitable arguments.

valueap_POP :: POP (f -.-> g) xss -> POP f xss -> POP g xss
#

Specialization of hap.

Applies a product of (lifted) functions pointwise to a product of suitable arguments.

Destructing products

5 declarations
valuehd :: NP f (x ': xs) -> f x
#

Obtain the head of an n-ary product.

valuetl :: NP f (x ': xs) -> NP f xs
#

Obtain the tail of an n-ary product.

typetype Projection (f :: k -> Type) (xs :: [k]) = K (NP f xs) -.-> f
#

The type of projections from an n-ary product.

A projection is a function from the n-ary product to a single element.

valueprojections :: SListI xs => NP (Projection f xs) xs
#

Compute all projections from an n-ary product.

Each element of the resulting product contains one of the projections.

Lifting / mapping

24 declarations
valuemap_NP :: SListI xs => (forall (a :: k). f a -> g a) -> NP f xs -> NP g xs
#

Specialization of hmap, which is equivalent to hliftA.

valuecliftA_NP
  1. :: All c xs
  2. => proxy c
  3. -> forall (a :: k). c a => f a -> g a
  4. -> NP f xs
  5. -> NP g xs
#

Specialization of hcliftA.

valuecliftA2_NP
  1. :: All c xs
  2. => proxy c
  3. -> forall (a :: k). c a => f a -> g a -> h a
  4. -> NP f xs
  5. -> NP g xs
  6. -> NP h xs
#

Specialization of hcliftA2.

valuecliftA3_NP
  1. :: All c xs
  2. => proxy c
  3. -> forall (a :: k). c a => f a -> g a -> h a -> i a
  4. -> NP f xs
  5. -> NP g xs
  6. -> NP h xs
  7. -> NP i xs
#

Specialization of hcliftA3.

valuecmap_NP
  1. :: All c xs
  2. => proxy c
  3. -> forall (a :: k). c a => f a -> g a
  4. -> NP f xs
  5. -> NP g xs
#

Specialization of hcmap, which is equivalent to hcliftA.

valuecmap_POP
  1. :: All2 c xss
  2. => proxy c
  3. -> forall (a :: k). c a => f a -> g a
  4. -> POP f xss
  5. -> POP g xss
#

Specialization of hcmap, which is equivalent to hcliftA.

Dealing with All c

4 declarations
valuehcliftA'
  1. :: (All2 c xss, Prod h ~ NP, HAp h)
  2. => proxy c
  3. -> forall (xs :: [k]). All c xs => f xs -> f' xs
  4. -> h f xss
  5. -> h f' xss
#

Deprecated. Use hcliftA or hcmap instead.

Lift a constrained function operating on a list-indexed structure to a function on a list-of-list-indexed structure.

This is a variant of hcliftA.

Specification:

hcliftA' p f xs = hpure (fn_2 $ \ AllDictC -> f) ` hap ` allDict_NP p ` hap ` xs

Instances:

hcliftA' :: All2 c xss => proxy c -> (forall xs. All c xs => f xs -> f' xs) -> NP f xss -> NP f' xss
hcliftA' :: All2 c xss => proxy c -> (forall xs. All c xs => f xs -> f' xs) -> NS f xss -> NS f' xss

Collapsing

2 declarations
valuecollapse_NP :: NP (K a) xs -> [a]
#

Specialization of hcollapse.

Example:

Example1 expression
collapse_NP (K 1 :* K 2 :* K 3 :* Nil)[1,2,3]
valuecollapse_POP :: SListI xss => POP (K a) xss -> [[a]]
#

Specialization of hcollapse.

Example:

Example1 expression
collapse_POP (POP ((K 'a' :* Nil) :* (K 'b' :* K 'c' :* Nil) :* Nil) :: POP (K Char) '[ '[(a :: Type)], '[b, c] ])["a","bc"]

(The type signature is only necessary in this case to fix the kind of the type variables.)

Folding and sequencing

16 declarations
valuesequence_POP
  1. :: (All SListI xss, Applicative f)
  2. => POP f xss
  3. -> f (POP I xss)
#

Specialization of hsequence.

Example:

Example1 expression
sequence_POP (POP ((Just 1 :* Nil) :* (Just 2 :* Just 3 :* Nil) :* Nil))Just (POP ((I 1 :* Nil) :* (I 2 :* I 3 :* Nil) :* Nil))

Catamorphism and anamorphism

4 declarations
valuecata_NP
  1. :: r '[]
  2. -> forall (y :: a) (ys :: [a]). f y -> r ys -> r (y ': ys)
  3. -> NP f xs
  4. -> r xs
#

Catamorphism for NP.

This is a suitable generalization of foldr. It takes parameters on what to do for Nil and :*. Since the input list is heterogeneous, the result is also indexed by a type-level list.

valueccata_NP
  1. :: All c xs
  2. => proxy c
  3. -> r '[]
  4. -> forall (y :: a) (ys :: [a]). c y => f y -> r ys -> r (y ': ys)
  5. -> NP f xs
  6. -> r xs
#

Constrained catamorphism for NP.

The difference compared to cata_NP is that the function for the cons-case can make use of the fact that the specified constraint holds for all the types in the signature of the product.

valueana_NP
  1. :: SListI xs
  2. => forall (y :: k) (ys :: [k]). s (y ': ys) -> (f y, s ys)
  3. -> s xs
  4. -> NP f xs
#

Anamorphism for NP.

In contrast to the anamorphism for normal lists, the generating function does not return an Either, but simply an element and a new seed value.

This is because the decision on whether to generate a Nil or a :* is determined by the types.

valuecana_NP
  1. :: All c xs
  2. => proxy c
  3. -> forall (y :: k) (ys :: [k]). c y => s (y ': ys) -> (f y, s ys)
  4. -> s xs
  5. -> NP f xs
#

Constrained anamorphism for NP.

Compared to ana_NP, the generating function can make use of the specified constraint here for the elements that it generates.

Transformation of index lists and coercions

8 declarations
valuetrans_NP
  1. :: AllZip c xs ys
  2. => proxy c
  3. -> forall (x :: k1) (y :: k2). c x y => f x -> g y
  4. -> NP f xs
  5. -> NP g ys
#

Specialization of htrans.

valuetrans_POP
  1. :: AllZip2 c xss yss
  2. => proxy c
  3. -> forall (x :: k1) (y :: k2). c x y => f x -> g y
  4. -> POP f xss
  5. -> POP g yss
#

Specialization of htrans.