Generics.SOP.NS

generics-sop-0.2.5.0: Generic Programming using True Sums of Products

Safe Haskell | None
Language | Haskell2010

Generics.SOP.NS

Contents

- Datatypes
- Constructing sums
- Destructing sums
- Application
- Lifting / mapping
- Dealing with ``All` c`
- Collapsing
- Sequencing
- Catamorphism and anamorphism
- Expanding sums to products

Description

n-ary sums (and sums of products)

Synopsis

- data NS :: (k -> *) -> [k] -> * where
  - Z :: f x -> NS f (x ': xs)
  - S :: NS f xs -> NS f (x ': xs)
- newtype SOP f xss = SOP (NS (NP f) xss)
- unSOP :: SOP f xss -> NS (NP f) xss
- type Injection f xs = f -.-> K (NS f xs)
- injections :: forall xs f. SListI xs => NP (Injection f xs) xs
- shift :: Injection f xs a -> Injection f (x ': xs) a
- shiftInjection :: Injection f xs a -> Injection f (x ': xs) a
- apInjs_NP :: SListI xs => NP f xs -> [NS f xs]
- apInjs'_NP :: SListI xs => NP f xs -> NP (K (NS f xs)) xs
- apInjs_POP :: SListI xss => POP f xss -> [SOP f xss]
- apInjs'_POP :: SListI xss => POP f xss -> NP (K (SOP f xss)) xss
- unZ :: NS f '[x] -> f x
- index_NS :: forall f xs. NS f xs -> Int
- index_SOP :: SOP f xs -> Int
- ap_NS :: NP (f -.-> g) xs -> NS f xs -> NS g xs
- ap_SOP :: POP (f -.-> g) xss -> SOP f xss -> SOP g xss
- liftA_NS :: SListI xs => (forall a. f a -> g a) -> NS f xs -> NS g xs
- liftA_SOP :: All SListI xss => (forall a. f a -> g a) -> SOP f xss -> SOP g xss
- liftA2_NS :: SListI xs => (forall a. f a -> g a -> h a) -> NP f xs -> NS g xs -> NS h xs
- liftA2_SOP :: All SListI xss => (forall a. f a -> g a -> h a) -> POP f xss -> SOP g xss -> SOP h xss
- cliftA_NS :: All c xs => proxy c -> (forall a. c a => f a -> g a) -> NS f xs -> NS g xs
- cliftA_SOP :: All2 c xss => proxy c -> (forall a. c a => f a -> g a) -> SOP f xss -> SOP g xss
- cliftA2_NS :: All c xs => proxy c -> (forall a. c a => f a -> g a -> h a) -> NP f xs -> NS g xs -> NS h xs
- cliftA2_SOP :: All2 c xss => proxy c -> (forall a. c a => f a -> g a -> h a) -> POP f xss -> SOP g xss -> SOP h xss
- map_NS :: SListI xs => (forall a. f a -> g a) -> NS f xs -> NS g xs
- map_SOP :: All SListI xss => (forall a. f a -> g a) -> SOP f xss -> SOP g xss
- cmap_NS :: All c xs => proxy c -> (forall a. c a => f a -> g a) -> NS f xs -> NS g xs
- cmap_SOP :: All2 c xss => proxy c -> (forall a. c a => f a -> g a) -> SOP f xss -> SOP g xss
- cliftA2'_NS :: All2 c xss => proxy c -> (forall xs. All c xs => f xs -> g xs -> h xs) -> NP f xss -> NS g xss -> NS h xss
- collapse_NS :: NS (K a) xs -> a
- collapse_SOP :: SListI xss => SOP (K a) xss -> [a]
- sequence'_NS :: Applicative f => NS (f :.: g) xs -> f (NS g xs)
- sequence'_SOP :: (SListI xss, Applicative f) => SOP (f :.: g) xss -> f (SOP g xss)
- sequence_NS :: (SListI xs, Applicative f) => NS f xs -> f (NS I xs)
- sequence_SOP :: (All SListI xss, Applicative f) => SOP f xss -> f (SOP I xss)
- cata_NS :: forall r f xs. (forall y ys. f y -> r (y ': ys)) -> (forall y ys. r ys -> r (y ': ys)) -> NS f xs -> r xs
- ccata_NS :: forall c proxy r f xs. All c xs => proxy c -> (forall y ys. c y => f y -> r (y ': ys)) -> (forall y ys. c y => r ys -> r (y ': ys)) -> NS f xs -> r xs
- ana_NS :: forall s f xs. SListI xs => (forall r. s '[] -> r) -> (forall y ys. s (y ': ys) -> Either (f y) (s ys)) -> s xs -> NS f xs
- cana_NS :: forall c proxy s f xs. All c xs => proxy c -> (forall r. s '[] -> r) -> (forall y ys. c y => s (y ': ys) -> Either (f y) (s ys)) -> s xs -> NS f xs
- expand_NS :: forall f xs. SListI xs => (forall x. f x) -> NS f xs -> NP f xs
- cexpand_NS :: forall c proxy f xs. All c xs => proxy c -> (forall x. c x => f x) -> NS f xs -> NP f xs
- expand_SOP :: forall f xss. All SListI xss => (forall x. f x) -> SOP f xss -> POP f xss
- cexpand_SOP :: forall c proxy f xss. All2 c xss => proxy c -> (forall x. c x => f x) -> SOP f xss -> POP f xss

# Datatypes

data NS :: (k -> *) -> [k] -> * where #

An n-ary sum.

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

The constructor names are chosen to resemble Peano-style natural numbers, i.e., `Z` is for "zero", and `S` is for "successor". Chaining `S` and `Z` chooses the corresponding component of the sum.

Examples:

    Z         :: f x -> NS f (x ': xs)
    S . Z     :: f y -> NS f (x ': y ': xs)
    S . S . Z :: f z -> NS f (x ': y ': z ': xs)
    ...

Note that empty sums (indexed by an empty list) have no non-bottom elements.

Two common instantiations of `f` are the identity functor `I` and the constant functor `K`. For `I`, the sum becomes a direct generalization of the `Either` type to arbitrarily many choices. For ``K` a`, the result is a homogeneous choice type, where the contents of the type-level list are ignored, but its length specifies the number of options.

In the context of the SOP approach to generic programming, an n-ary sum describes the top-level structure of a datatype, which is a choice between all of its constructors.

Examples:

    Z (I 'x')      :: NS I       '[ Char, Bool ]
    S (Z (I True)) :: NS I       '[ Char, Bool ]
    S (Z (K 1))    :: NS (K Int) '[ Char, Bool ]

Constructors

Z :: f x -> NS f (x ': xs) |
S :: NS f xs -> NS f (x ': xs) |

Instances

HExpand [k] k (NS k) # |
Methods hexpand :: SListIN (NS k) k (Prod (NS k) k h) xs => (forall x. f x) -> h f xs -> Prod (NS k) k h f xs # hcexpand :: AllN (NS k) k (Prod (NS k) k h) c xs => proxy c -> (forall x. c x => f x) -> h f xs -> Prod (NS k) k h f xs #
HApInjs [k] k (NS k) # |
Methods hapInjs :: SListIN (NS k) k h xs => Prod (NS k) k h f xs -> [h f xs] #
HIndex [k] k (NS k) # |
Methods hindex :: h f xs -> Int #
HSequence [k] k (NS k) # |
Methods hsequence' :: (SListIN (NS k) k h xs, Applicative f) => h ((k :.: *) f g) xs -> f (h g xs) #
HCollapse [k] k (NS k) # |
Methods hcollapse :: SListIN (NS k) k h xs => h (K k a) xs -> CollapseTo (NS k) k h a #
HAp [k] k (NS k) # |
Methods hap :: Prod (NS k) k h ((k -.-> f) g) xs -> h f xs -> h g xs #
All k (Compose * k Eq f) xs => Eq (NS k f xs) # |
Methods (==) :: NS k f xs -> NS k f xs -> Bool # (/=) :: NS k f xs -> NS k f xs -> Bool #
(All k (Compose * k Eq f) xs, All k (Compose * k Ord f) xs) => Ord (NS k f xs) # |
Methods compare :: NS k f xs -> NS k f xs -> Ordering # (<) :: NS k f xs -> NS k f xs -> Bool # (<=) :: NS k f xs -> NS k f xs -> Bool # (>) :: NS k f xs -> NS k f xs -> Bool # (>=) :: NS k f xs -> NS k f xs -> Bool # max :: NS k f xs -> NS k f xs -> NS k f xs # min :: NS k f xs -> NS k f xs -> NS k f xs #
All k (Compose * k Show f) xs => Show (NS k f xs) # |
Methods showsPrec :: Int -> NS k f xs -> ShowS # show :: NS k f xs -> String # showList :: [NS k f xs] -> ShowS #
All k (Compose * k NFData f) xs => NFData (NS k f xs) # | Since: 0.2.5.0
Methods rnf :: NS k f xs -> () #
type SListIN [k] k (NS k) # |
type SListIN [k] k (NS k) = SListI k
type Prod [k] k (NS k) # |
type Prod [k] k (NS k) = NP k
type CollapseTo [k] k (NS k) a # |
type CollapseTo [k] k (NS k) a = a

newtype SOP f xss #

A sum of products.

This is a 'newtype' for an `NS` of an `NP`. The elements of the (inner) products are applications of the parameter `f`. The type `SOP` is indexed by the list of lists that determines the sizes of both the (outer) sum and all the (inner) products, as well as the types of all the elements of the inner products.

An ``SOP` `I`` reflects the structure of a normal Haskell datatype. The sum structure represents the choice between the different constructors, the product structure represents the arguments of each constructor.

Constructors

SOP (NS (NP f) xss) |

Instances

HExpand [[k]] k (SOP k) # |
Methods hexpand :: SListIN (SOP k) k (Prod (SOP k) k h) xs => (forall x. f x) -> h f xs -> Prod (SOP k) k h f xs # hcexpand :: AllN (SOP k) k (Prod (SOP k) k h) c xs => proxy c -> (forall x. c x => f x) -> h f xs -> Prod (SOP k) k h f xs #
HApInjs [[k]] k (SOP k) # |
Methods hapInjs :: SListIN (SOP k) k h xs => Prod (SOP k) k h f xs -> [h f xs] #
HIndex [[k]] k (SOP k) # |
Methods hindex :: h f xs -> Int #
HSequence [[k]] k (SOP k) # |
Methods hsequence' :: (SListIN (SOP k) k h xs, Applicative f) => h ((k :.: *) f g) xs -> f (h g xs) #
HCollapse [[k]] k (SOP k) # |
Methods hcollapse :: SListIN (SOP k) k h xs => h (K k a) xs -> CollapseTo (SOP k) k h a #
HAp [[k]] k (SOP k) # |
Methods hap :: Prod (SOP k) k h ((k -.-> f) g) xs -> h f xs -> h g xs #
Eq (NS [k] (NP k f) xss) => Eq (SOP k f xss) # |
Methods (==) :: SOP k f xss -> SOP k f xss -> Bool # (/=) :: SOP k f xss -> SOP k f xss -> Bool #
Ord (NS [k] (NP k f) xss) => Ord (SOP k f xss) # |
Methods compare :: SOP k f xss -> SOP k f xss -> Ordering # (<) :: SOP k f xss -> SOP k f xss -> Bool # (<=) :: SOP k f xss -> SOP k f xss -> Bool # (>) :: SOP k f xss -> SOP k f xss -> Bool # (>=) :: SOP k f xss -> SOP k f xss -> Bool # max :: SOP k f xss -> SOP k f xss -> SOP k f xss # min :: SOP k f xss -> SOP k f xss -> SOP k f xss #
Show (NS [k] (NP k f) xss) => Show (SOP k f xss) # |
Methods showsPrec :: Int -> SOP k f xss -> ShowS # show :: SOP k f xss -> String # showList :: [SOP k f xss] -> ShowS #
NFData (NS [k] (NP k f) xss) => NFData (SOP k f xss) # | Since: 0.2.5.0
Methods rnf :: SOP k f xss -> () #
type SListIN [[k]] k (SOP k) # |
type SListIN [[k]] k (SOP k) = SListI2 k
type Prod [[k]] k (SOP k) # |
type Prod [[k]] k (SOP k) = POP k
type CollapseTo [[k]] k (SOP k) a # |
type CollapseTo [[k]] k (SOP k) a = [a]

unSOP :: SOP f xss -> NS (NP f) xss #

Unwrap a sum of products.

# Constructing sums

type Injection f xs = f -.-> K (NS f xs) #

The type of injections into an n-ary sum.

If you expand the type synonyms and newtypes involved, you get

    Injection f xs a = (f -.-> K (NS f xs)) a ~= f a -> K (NS f xs) a ~= f a -> NS f xs

If we pick `a` to be an element of `xs`, this indeed corresponds to an injection into the sum.

injections :: forall xs f. SListI xs => NP (Injection f xs) xs #

Compute all injections into an n-ary sum.

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

shift :: Injection f xs a -> Injection f (x ': xs) a #

Deprecated: Use `shiftInjection` instead.

Shift an injection.

Given an injection, return an injection into a sum that is one component larger.

shiftInjection :: Injection f xs a -> Injection f (x ': xs) a #

Shift an injection.

Given an injection, return an injection into a sum that is one component larger.

apInjs_NP :: SListI xs => NP f xs -> [NS f xs] #

Apply injections to a product.

Given a product containing all possible choices, produce a list of sums by applying each injection to the appropriate element.

Example:

    >>> apInjs_NP (I 'x' :* I True :* I 2 :* Nil)
    [Z (I 'x'), S (Z (I True)), S (S (Z (I 2)))]

apInjs'_NP :: SListI xs => NP f xs -> NP (K (NS f xs)) xs #

`apInjs_NP` without `hcollapse`.

    >>> apInjs'_NP (I 'x' :* I True :* I 2 :* Nil)
    K (Z (I 'x')) :* K (S (Z (I True))) :* K (S (S (Z (I 2)))) :* Nil

Since: 0.2.5.0

apInjs_POP :: SListI xss => POP f xss -> [SOP f xss] #

Apply injections to a product of product.

This operates on the outer product only. Given a product containing all possible choices (that are products), produce a list of sums (of products) by applying each injection to the appropriate element.

Example:

    >>> apInjs_POP (POP ((I 'x' :* Nil) :* (I True :* I 2 :* Nil) :* Nil))
    [SOP (Z (I 'x' :* Nil)),SOP (S (Z (I True :* (I 2 :* Nil))))]

apInjs'_POP :: SListI xss => POP f xss -> NP (K (SOP f xss)) xss #

`apInjs_POP` without `hcollapse`.

Example:

    >>> apInjs'_POP (POP ((I 'x' :* Nil) :* (I True :* I 2 :* Nil) :* Nil))
    K (SOP (Z (I 'x' :* Nil))) :* K (SOP (S (Z (I True :* I 2 :* Nil)))) :* Nil

Since: 0.2.5.0

# Destructing sums

unZ :: NS f '[x] -> f x #

Extract the payload from a unary sum.

For larger sums, this function would be partial, so it is only provided with a rather restrictive type.

Example:

    >>> unZ (Z (I 'x'))
    I 'x'

Since: 0.2.2.0

index_NS :: forall f xs. NS f xs -> Int #

Obtain the index from an n-ary sum.

An n-nary sum represents a choice between n different options. This function returns an integer between 0 and n - 1 indicating the option chosen by the given value.

Examples:

    >>> index_NS (S (S (Z (I False))))
    2
    >>> index_NS (Z (K ()))
    0

Since: 0.2.4.0

index_SOP :: SOP f xs -> Int #

Obtain the index from an n-ary sum of products.

An n-nary sum represents a choice between n different options. This function returns an integer between 0 and n - 1 indicating the option chosen by the given value.

Specification:

    index_SOP = index_NS . unSOP

Example:

    >>> index_SOP (SOP (S (Z (I True :* I 'x' :* Nil))))
    1

Since: 0.2.4.0

# Application

ap_NS :: NP (f -.-> g) xs -> NS f xs -> NS g xs #

Specialization of `hap`.

ap_SOP :: POP (f -.-> g) xss -> SOP f xss -> SOP g xss #

Specialization of `hap`.

# Lifting / mapping

liftA_NS :: SListI xs => (forall a. f a -> g a) -> NS f xs -> NS g xs #

Specialization of `hliftA`.

liftA_SOP :: All SListI xss => (forall a. f a -> g a) -> SOP f xss -> SOP g xss #

Specialization of `hliftA`.

liftA2_NS :: SListI xs => (forall a. f a -> g a -> h a) -> NP f xs -> NS g xs -> NS h xs #

Specialization of `hliftA2`.

liftA2_SOP :: All SListI xss => (forall a. f a -> g a -> h a) -> POP f xss -> SOP g xss -> SOP h xss #

Specialization of `hliftA2`.

cliftA_NS :: All c xs => proxy c -> (forall a. c a => f a -> g a) -> NS f xs -> NS g xs #

Specialization of `hcliftA`.

cliftA_SOP :: All2 c xss => proxy c -> (forall a. c a => f a -> g a) -> SOP f xss -> SOP g xss #

Specialization of `hcliftA`.

cliftA2_NS :: All c xs => proxy c -> (forall a. c a => f a -> g a -> h a) -> NP f xs -> NS g xs -> NS h xs #

Specialization of `hcliftA2`.

cliftA2_SOP :: All2 c xss => proxy c -> (forall a. c a => f a -> g a -> h a) -> POP f xss -> SOP g xss -> SOP h xss #

Specialization of `hcliftA2`.

map_NS :: SListI xs => (forall a. f a -> g a) -> NS f xs -> NS g xs #

Specialization of `hmap`, which is equivalent to `hliftA`.

map_SOP :: All SListI xss => (forall a. f a -> g a) -> SOP f xss -> SOP g xss #

Specialization of `hmap`, which is equivalent to `hliftA`.

cmap_NS :: All c xs => proxy c -> (forall a. c a => f a -> g a) -> NS f xs -> NS g xs #

Specialization of `hcmap`, which is equivalent to `hcliftA`.

cmap_SOP :: All2 c xss => proxy c -> (forall a. c a => f a -> g a) -> SOP f xss -> SOP g xss #

Specialization of `hcmap`, which is equivalent to `hcliftA`.

# Dealing with ``All` c`

cliftA2'_NS :: All2 c xss => proxy c -> (forall xs. All c xs => f xs -> g xs -> h xs) -> NP f xss -> NS g xss -> NS h xss #

Deprecated: Use `cliftA2_NS` instead.

Specialization of `hcliftA2'`.

# Collapsing

collapse_NS :: NS (K a) xs -> a #

Specialization of `hcollapse`.

collapse_SOP :: SListI xss => SOP (K a) xss -> [a] #

Specialization of `hcollapse`.

# Sequencing

sequence'_NS :: Applicative f => NS (f :.: g) xs -> f (NS g xs) #

Specialization of `hsequence'`.

sequence'_SOP :: (SListI xss, Applicative f) => SOP (f :.: g) xss -> f (SOP g xss) #

Specialization of `hsequence'`.

sequence_NS :: (SListI xs, Applicative f) => NS f xs -> f (NS I xs) #

Specialization of `hsequence`.

sequence_SOP :: (All SListI xss, Applicative f) => SOP f xss -> f (SOP I xss) #

Specialization of `hsequence`.

# Catamorphism and anamorphism

cata_NS :: forall r f xs. (forall y ys. f y -> r (y ': ys)) -> (forall y ys. r ys -> r (y ': ys)) -> NS f xs -> r xs #

Catamorphism for `NS`.

Takes arguments determining what to do for `Z` and what to do for `S`. The result type is still indexed over the type-level lit.

Since: 0.2.3.0

ccata_NS :: forall c proxy r f xs. All c xs => proxy c -> (forall y ys. c y => f y -> r (y ': ys)) -> (forall y ys. c y => r ys -> r (y ': ys)) -> NS f xs -> r xs #

Constrained catamorphism for `NS`.

Since: 0.2.3.0

ana_NS :: forall s f xs. SListI xs => (forall r. s '[] -> r) -> (forall y ys. s (y ': ys) -> Either (f y) (s ys)) -> s xs -> NS f xs #

Anamorphism for `NS`.

Since: 0.2.3.0

cana_NS :: forall c proxy s f xs. All c xs => proxy c -> (forall r. s '[] -> r) -> (forall y ys. c y => s (y ': ys) -> Either (f y) (s ys)) -> s xs -> NS f xs #

Constrained anamorphism for `NS`.

Since: 0.2.3.0

# Expanding sums to products

expand_NS :: forall f xs. SListI xs => (forall x. f x) -> NS f xs -> NP f xs #

Specialization of `hexpand`.

Since: 0.2.5.0

cexpand_NS :: forall c proxy f xs. All c xs => proxy c -> (forall x. c x => f x) -> NS f xs -> NP f xs #

Specialization of `hcexpand`.

Since: 0.2.5.0

expand_SOP :: forall f xss. All SListI xss => (forall x. f x) -> SOP f xss -> POP f xss #

Specialization of `hexpand`.

Since: 0.2.5.0

cexpand_SOP :: forall c proxy f xss. All2 c xss => proxy c -> (forall x. c x => f x) -> SOP f xss -> POP f xss #

Specialization of `hcexpand`.

Since: 0.2.5.0