red-black-record-2.1.0.3: Extensible records and variants indexed by a type-level Red-Black tree.

Safe HaskellNone
LanguageHaskell2010

Data.RBR

Contents

Description

This module provides extensible Record and Variant types, which are indexed by a type-level Map.

Many functions in this module require the use of TypeApplications to avoid ambiguity. The order of the applications is the order of the type variables in the function signature's forall. The first type variable is usually the field/branch name and it's always required. The other type variables can often be inferred.

Meaning of commonly used type and kind variables:

  • t: A type-level Map, usually of kind Map Symbol q.
  • k: A key of kind Symbol in a type-level Map.
  • v: A type value of kind q in a type-level Map.
  • q: The kind of the type value v.
  • f: A type constructor of kind q -> Type that wraps the type v.
  • flat: A type-level list of kind [q] whose elements correspond to values in a type-level Map.
Synopsis

Type-level map

A type-level map that keeps track of which keys are present, and to which types they correspond.

Implemented as a red-black tree, and used as a kind by means of DataKinds.

data Map symbol q Source #

A Red-Black tree. It will be used as a kind, to index the Record and Variant types.

Instances
(Eq symbol, Eq q) => Eq (Map symbol q) Source # 
Instance details

Defined in Data.RBR.Internal

Methods

(==) :: Map symbol q -> Map symbol q -> Bool #

(/=) :: Map symbol q -> Map symbol q -> Bool #

(Show symbol, Show q) => Show (Map symbol q) Source # 
Instance details

Defined in Data.RBR.Internal

Methods

showsPrec :: Int -> Map symbol q -> ShowS #

show :: Map symbol q -> String #

showList :: [Map symbol q] -> ShowS #

type Empty = E Source #

A map without entries. See also unit and impossible.

class KeysValuesAllF c t => KeysValuesAll (c :: symbol -> q -> Constraint) (t :: Map symbol q) Source #

Require a constraint for every key-value pair in a tree. This is a generalization of All from Data.SOP.

Minimal complete definition

cpara_Map

Instances
KeysValuesAll (c :: symbol -> q -> Constraint) (E :: Map symbol q) Source # 
Instance details

Defined in Data.RBR.Internal

Methods

cpara_Map :: proxy c -> r E -> (forall (left :: Map symbol0 q0) (k :: symbol0) (v :: q0) (right :: Map symbol0 q0) (color :: Color). (c k v, KeysValuesAll c left, KeysValuesAll c right) => r left -> r right -> r (N color left k v right)) -> r E Source #

(c k v, KeysValuesAll c left, KeysValuesAll c right) => KeysValuesAll (c :: symbol -> q -> Constraint) (N color left k v right :: Map symbol q) Source # 
Instance details

Defined in Data.RBR.Internal

Methods

cpara_Map :: proxy c -> r E -> (forall (left0 :: Map symbol0 q0) (k0 :: symbol0) (v0 :: q0) (right0 :: Map symbol0 q0) (color0 :: Color). (c k0 v0, KeysValuesAll c left0, KeysValuesAll c right0) => r left0 -> r right0 -> r (N color0 left0 k0 v0 right0)) -> r (N color left k v right) Source #

class KnownSymbol k => KnownKey (k :: Symbol) (v :: q) Source #

Two-place constraint saying that a Symbol key can be demoted to String. Nothing is required from the corresponding value.

Defined using the "class synonym" trick.

Instances
KnownSymbol k => KnownKey k (v :: q) Source # 
Instance details

Defined in Data.RBR.Internal

demoteKeys :: forall t. KeysValuesAll KnownKey t => Record (K String) t Source #

Create a Record containing the names of each field.

The names are represented by a constant functor K carrying an annotation of type String. This means that there aren't actually any values of the type that corresponds to each field, only the String annotations.

>>> putStrLn $ prettyShowRecord show $ demoteKeys @(Insert "foo" Char (Insert "bar" Bool Empty))
{bar = K "bar", foo = K "foo"}

class (KnownSymbol k, Typeable v) => KnownKeyTypeableValue (k :: Symbol) (v :: q) Source #

Two-place constraint saying that a Symbol key can be demoted to String, and that the corresponding value Type has a term-level representation.

Defined using the "class synonym" trick.

Instances
(KnownSymbol k, Typeable v) => KnownKeyTypeableValue k (v :: q) Source # 
Instance details

Defined in Data.RBR.Internal

demoteEntries :: forall t. KeysValuesAll KnownKeyTypeableValue t => Record (K (String, TypeRep)) t Source #

Create a record containing the names of each field along with a term-level representation of each type.

>>> putStrLn $ prettyShowRecord show $ demoteEntries @(Insert "foo" Char (Insert "bar" Bool Empty))
{bar = K ("bar",Bool), foo = K ("foo",Char)}

See also collapse_Record for getting the entries as a list.

Records and Variants

data Record (f :: q -> Type) (t :: Map Symbol q) Source #

An extensible product-like type with named fields.

The values in the Record come wrapped in a type constructor f, which por pure records will be the identity functor I.

See also insert, delete and project.

Instances
(Productlike ([] :: [q]) t result, Show (NP f result)) => Show (Record f t) Source # 
Instance details

Defined in Data.RBR.Internal

Methods

showsPrec :: Int -> Record f t -> ShowS #

show :: Record f t -> String #

showList :: [Record f t] -> ShowS #

unit :: Record f Empty Source #

A Record without components is a boring, uninformative type whose single value can be conjured out of thin air.

cpure_Record :: forall c t f. KeysValuesAll c t => Proxy c -> (forall k v. c k v => f v) -> Record f t Source #

Create a Record, knowing that both keys and values satisfy a 2-place constraint. The constraint is passed as a Proxy.

The naming scheme follows that of cpure_NP.

collapse_Record :: forall t result a. Productlike '[] t result => Record (K a) t -> [a] Source #

Collapse a Record composed of K annotations.

>>> collapse_Record unit
[]
>>> collapse_Record (insert @"bar" (K False) unit)
[False]

The naming scheme follows that of collapse_NP.

prettyShowRecord :: forall t flat f. (KeysValuesAll KnownKey t, Productlike '[] t flat, All Show flat, SListI flat) => (forall x. Show x => f x -> String) -> Record f t -> String Source #

Show a Record in a friendlier way than the default Show instance. The function argument will usually be show, but it can be used to unwrap the value of each field before showing it.

prettyShowRecordI :: forall t flat. (KeysValuesAll KnownKey t, Productlike '[] t flat, All Show flat, SListI flat) => Record I t -> String Source #

Like prettyShowRecord but specialized to pure records.

data Variant (f :: q -> Type) (t :: Map Symbol q) Source #

An extensible sum-like type with named branches.

The values in the Variant come wrapped in a type constructor f, which por pure variants will be the identity functor I.

See also widen, winnow and inject.

Instances
(Sumlike ([] :: [q]) t result, Show (NS f result)) => Show (Variant f t) Source # 
Instance details

Defined in Data.RBR.Internal

Methods

showsPrec :: Int -> Variant f t -> ShowS #

show :: Variant f t -> String #

showList :: [Variant f t] -> ShowS #

impossible :: Variant f Empty -> b Source #

A Variant without branches doesn't have any values. From an impossible thing, anything can come out.

prettyShowVariant :: forall t flat f. (KeysValuesAll KnownKey t, Productlike '[] t flat, Sumlike '[] t flat, All Show flat, SListI flat) => (forall x. Show x => f x -> String) -> Variant f t -> String Source #

Show a Variant in a friendlier way than the default Show instance. The function argument will usually be show, but it can be used to unwrap the value of the branch before showing it.

prettyShowVariantI :: forall t flat. (KeysValuesAll KnownKey t, Productlike '[] t flat, Sumlike '[] t flat, All Show flat, SListI flat) => Variant I t -> String Source #

Like prettyShowVariant but specialized to pure variants.

Inserting and widening

class Insertable (k :: Symbol) (v :: q) (t :: Map Symbol q) Source #

Class that determines if the pair of a Symbol key and a type can be inserted into a type-level map.

The associated type family Insert produces the resulting map.

At the term level, this manifests in insert, which adds a new field to a record, and in widen, which lets you use a Variant in a bigger context than the one in which is was defined. insert tends to be more useful in practice.

If the map already has the key but with a different type, the insertion fails to compile.

Minimal complete definition

_insert, _widen

Associated Types

type Insert k v t :: Map Symbol q Source #

Instances
(InsertableHelper1 k v t, Insert1 k v t ~ inserted, CanMakeBlack inserted) => Insertable k (v :: q) (t :: Map Symbol q) Source # 
Instance details

Defined in Data.RBR.Internal

Associated Types

type Insert k v t :: Map Symbol q Source #

Methods

_insert :: f v -> Record f t -> Record f (Insert k v t) Source #

_widen :: Variant f t -> Variant f (Insert k v t) Source #

type family InsertAll (es :: [(Symbol, q)]) (t :: Map Symbol q) :: Map Symbol q where ... Source #

Insert a list of type level key / value pairs into a type-level map.

Equations

InsertAll '[] t = t 
InsertAll ('(name, fieldType) ': es) t = Insert name fieldType (InsertAll es t) 

type FromList (es :: [(Symbol, q)]) = InsertAll es Empty Source #

Build a type-level map out of a list of type level key / value pairs.

insert :: forall k v t f. Insertable k v t => f v -> Record f t -> Record f (Insert k v t) Source #

Adds a new field to a Record.

>>> project @"foo" (insert @"foo" (I 'a') unit)
I 'a'
>>> project @"foo" (insert @"foo" @Char Nothing unit)
Nothing

addField :: forall k v t f. Insertable k v t => f v -> Record f t -> Record f (Insert k v t) Source #

Alias for insert.

insertI :: forall k v t. Insertable k v t => v -> Record I t -> Record I (Insert k v t) Source #

Like insert but specialized to pure Records.

>>> projectI @"foo" (insertI @"foo" 'a' unit)
'a'

addFieldI :: forall k v t. Insertable k v t => v -> Record I t -> Record I (Insert k v t) Source #

Like addField but specialized to pure Records.

widen :: forall k v t f. Insertable k v t => Variant f t -> Variant f (Insert k v t) Source #

Lets you use a Variant in a bigger context than the one in which is was defined.

Deleting and winnowing

class Deletable (k :: Symbol) (v :: q) (t :: Map Symbol q) Source #

Class that determines if the pair of a Symbol key and a type can be deleted from a type-level map.

The associated type family Delete produces the resulting map.

At the term level, this manifests in delete, which removes a field from a record, and in winnow, which checks if a Variant is of a given branch and returns the value in the branch if there's a match, or a reduced Variant if there isn't. winnow tends to be more useful in practice.

If the map already has the key but with a different type, the deletion fails to compile.

Minimal complete definition

_delete, _winnow

Associated Types

type Delete k v t :: Map Symbol q Source #

Instances
(Delable k v t, Del k v t ~ deleted, CanMakeBlack deleted) => Deletable k (v :: q) (t :: Map Symbol q) Source # 
Instance details

Defined in Data.RBR.Internal

Associated Types

type Delete k v t :: Map Symbol q Source #

Methods

_delete :: Record f t -> Record f (Delete k v t) Source #

_winnow :: Variant f t -> Either (Variant f (Delete k v t)) (f v) Source #

delete :: forall k v t f. Deletable k v t => Record f t -> Record f (Delete k v t) Source #

Removes a field from a Record.

winnow :: forall k v t f. Deletable k v t => Variant f t -> Either (Variant f (Delete k v t)) (f v) Source #

Checks if a Variant is of a given branch and returns the value in the branch if there's a match, or a reduced Variant if there isn't.

winnowI :: forall k v t. Deletable k v t => Variant I t -> Either (Variant I (Delete k v t)) v Source #

Like winnow but specialized to pure Variants.

>>> winnow @"bar" @Bool (injectI @"bar" False :: Variant I (Insert "foo" Char (Insert "bar" Bool Empty)))
Right (I False)
>>> prettyShowVariantI `first` winnow @"foo" @Char (injectI @"bar" False :: Variant I (Insert "foo" Char (Insert "bar" Bool Empty)))
Left "bar (False)" 

Projecting and injecting

class Key (k :: Symbol) (t :: Map Symbol q) Source #

Class that determines if a given Symbol key is present in a type-level map.

The Value type family gives the Type corresponding to the key.

Minimal complete definition

_field, _branch

Associated Types

type Value k t :: q Source #

Instances
(CmpSymbol k' k ~ ordering, KeyHelper ordering k left v' right) => Key k (N color left k' v' right :: Map Symbol q) Source # 
Instance details

Defined in Data.RBR.Internal

Associated Types

type Value k (N color left k' v' right) :: q Source #

Methods

_field :: Field f (N color left k' v' right) (Value k (N color left k' v' right)) Source #

_branch :: Branch f (N color left k' v' right) (Value k (N color left k' v' right)) Source #

type family Field (f :: q -> Type) (t :: Map Symbol q) (v :: q) where ... Source #

Auxiliary type family to avoid repetition and help improve compilation times.

Equations

Field f t v = Record f t -> (f v -> Record f t, f v) 

field :: forall k t f. Key k t => Field f t (Value k t) Source #

Takes a field name (given through TypeApplications) and a Record, and returns a pair of a setter for the field and the original value of the field.

type family Branch (f :: q -> Type) (t :: Map Symbol q) (v :: q) where ... Source #

Auxiliary type family to avoid repetition and help improve compilation times.

Equations

Branch f t v = (Variant f t -> Maybe (f v), f v -> Variant f t) 

branch :: forall k t f. Key k t => Branch f t (Value k t) Source #

Takes a branch name (given through TypeApplications) and returns a pair of a match function and a constructor.

project :: forall k t f. Key k t => Record f t -> f (Value k t) Source #

Get the value of a field for a Record.

projectI :: forall k t. Key k t => Record I t -> Value k t Source #

Like project but specialized to pure Records.

>>> projectI @"foo" (insertI @"foo" 'a' (insertI @"bar" False unit))
'a'

getField :: forall k t f. Key k t => Record f t -> f (Value k t) Source #

Alias for project.

getFieldI :: forall k t. Key k t => Record I t -> Value k t Source #

Like getField but specialized to pure Records.

setField :: forall k t f. Key k t => f (Value k t) -> Record f t -> Record f t Source #

Set the value of a field for a Record.

setFieldI :: forall k t. Key k t => Value k t -> Record I t -> Record I t Source #

Like setField but specialized to pure Records.

modifyField :: forall k t f. Key k t => (f (Value k t) -> f (Value k t)) -> Record f t -> Record f t Source #

Modify the value of a field for a Record.

modifyFieldI :: forall k t. Key k t => (Value k t -> Value k t) -> Record I t -> Record I t Source #

Like modifyField but specialized to pure Records.

inject :: forall k t f. Key k t => f (Value k t) -> Variant f t Source #

Put a value into the branch of a Variant.

>>> match @"foo" (inject @"foo" (I 'a') :: Variant I (Insert "foo" Char Empty))
Just (I 'a')

injectI :: forall k t. Key k t => Value k t -> Variant I t Source #

Like inject but specialized to pure Variants.

>>> matchI @"foo" (injectI @"foo" 'a' :: Variant I (Insert "foo" Char Empty))
Just 'a'

match :: forall k t f. Key k t => Variant f t -> Maybe (f (Value k t)) Source #

Check if a Variant value is the given branch.

matchI :: forall k t. Key k t => Variant I t -> Maybe (Value k t) Source #

Like match but specialized to pure Variantss.

Eliminating variants

eliminate :: (Productlike '[] t result, Sumlike '[] t result, SListI result) => Record (Case f r) t -> Variant f t -> r Source #

Process a Variant using a eliminator Record that carries handlers for each possible branch of the Variant.

>>> eliminate (addCaseI @"foo" @Int succ (addCaseI @"bar" pred unit)) (injectI @"bar" 33)
32

newtype Case f a b Source #

Represents a handler for a branch of a Variant.

Constructors

Case (f b -> a) 
Instances
Functor f => Contravariant (Case f a) Source # 
Instance details

Defined in Data.RBR.Internal

Methods

contramap :: (a0 -> b) -> Case f a b -> Case f a a0 #

(>$) :: b -> Case f a b -> Case f a a0 #

addCase :: forall k v t f a. Insertable k v t => (f v -> a) -> Record (Case f a) t -> Record (Case f a) (Insert k v t) Source #

A form of addField for creating eliminators for Variants.

addCaseI :: forall k v t a. Insertable k v t => (v -> a) -> Record (Case I a) t -> Record (Case I a) (Insert k v t) Source #

A pure version of addCase.

Subsets of fields and branches

class (Key k t, Value k t ~ v) => PresentIn (t :: Map Symbol q) (k :: Symbol) (v :: q) Source #

Instances
(Key k t, Value k t ~ v) => PresentIn (t :: Map Symbol q) k (v :: q) Source # 
Instance details

Defined in Data.RBR.Internal

type ProductlikeSubset (subset :: Map Symbol q) (whole :: Map Symbol q) (flat :: [q]) = (KeysValuesAll (PresentIn whole) subset, Productlike '[] subset flat, SListI flat) Source #

Constraint for maps that represent subsets of fields of Record-like types.

fieldSubset :: forall subset whole flat f. ProductlikeSubset subset whole flat => Record f whole -> (Record f subset -> Record f whole, Record f subset) Source #

Like field, but targets multiple fields at the same time

projectSubset :: forall subset whole flat f. ProductlikeSubset subset whole flat => Record f whole -> Record f subset Source #

Like project, but extracts multiple fields at the same time.

The types in the subset tree can often be inferred and left as wildcards in type signature.

>>> prettyShowRecordI $ projectSubset @(Insert "foo" _ (Insert "bar" _ Empty)) (insertI @"foo" 'a' (insertI @"bar" True (insertI @"baz" (Just ()) unit)))
"{bar = True, foo = 'a'}"

Can also be used to convert between Records with structurally dissimilar type-level maps that nevertheless hold the same entries.

getFieldSubset :: forall subset whole flat f. ProductlikeSubset subset whole flat => Record f whole -> Record f subset Source #

Alias for projectSubset.

setFieldSubset :: forall subset whole flat f. ProductlikeSubset subset whole flat => Record f subset -> Record f whole -> Record f whole Source #

Like setField, but sets multiple fields at the same time.

modifyFieldSubset :: forall subset whole flat f. ProductlikeSubset subset whole flat => (Record f subset -> Record f subset) -> Record f whole -> Record f whole Source #

Like modifyField, but modifies multiple fields at the same time.

type SumlikeSubset (subset :: Map Symbol q) (whole :: Map Symbol q) (subflat :: [q]) (wholeflat :: [q]) = (KeysValuesAll (PresentIn whole) subset, Productlike '[] whole wholeflat, Sumlike '[] whole wholeflat, SListI wholeflat, Productlike '[] subset subflat, Sumlike '[] subset subflat, SListI subflat) Source #

Constraint for maps that represent subsets of branches of Variant-like types.

branchSubset :: forall subset whole subflat wholeflat f. SumlikeSubset subset whole subflat wholeflat => (Variant f whole -> Maybe (Variant f subset), Variant f subset -> Variant f whole) Source #

Like branch, but targets multiple branches at the same time.

injectSubset :: forall subset whole subflat wholeflat f. SumlikeSubset subset whole subflat wholeflat => Variant f subset -> Variant f whole Source #

Like inject, but injects one of several possible branches.

Can also be used to convert between Variants with structurally dissimilar type-level maps that nevertheless hold the same entries.

matchSubset :: forall subset whole subflat wholeflat f. SumlikeSubset subset whole subflat wholeflat => Variant f whole -> Maybe (Variant f subset) Source #

Like match, but matches more than one branch.

eliminateSubset :: forall subset whole subflat wholeflat f r. SumlikeSubset subset whole subflat wholeflat => Record (Case f r) whole -> Variant f subset -> r Source #

Like eliminate, but allows the eliminator Record to have more fields than there are branches in the Variant.

Interfacing with normal records

Typeclasses for converting to and from normal Haskell records and sum types.

They have default implementations based in GHC.Generics:

>>> data Person = Person { name :: String, age :: Int } deriving (Generic, Show)
>>> instance ToRecord Person
>>> instance FromRecord Person
>>> data Summy = Lefty Int | Righty Bool deriving (Generic,Show)
>>> instance ToVariant Summy
>>> instance FromVariant Summy

Only single-constructor records with named fields can have ToRecord and FromRecord instances.

Only sum types with exactly one anonymous argument on each branch can have ToVariant and FromVariant instances.

class ToRecord (r :: Type) where Source #

Minimal complete definition

Nothing

Associated Types

type RecordCode r :: Map Symbol Type Source #

class ToRecord r => FromRecord (r :: Type) where Source #

Minimal complete definition

Nothing

type family VariantCode (s :: Type) :: Map Symbol Type where ... Source #

Equations

VariantCode s = VariantCode' E (Rep s) 

class ToVariant (s :: Type) where Source #

Minimal complete definition

Nothing

class FromVariant (s :: Type) where Source #

Minimal complete definition

Nothing

Interfacing with Data.SOP

class Productlike (start :: [k]) (t :: Map Symbol k) (result :: [k]) | start t -> result, result t -> start where Source #

Class from converting Records to and from the n-ary product type NP from Data.SOP.

prefixNP flattens a Record and adds it to the initial part of the product.

breakNP reconstructs a Record from the initial part of the product and returns the unconsumed part.

The functions toNP and fromNP are usually easier to use.

Methods

_prefixNP :: Record f t -> NP f start -> NP f result Source #

_breakNP :: NP f result -> (Record f t, NP f start) Source #

Instances
Productlike (start :: [k]) (E :: Map Symbol k) (start :: [k]) Source # 
Instance details

Defined in Data.RBR.Internal

Methods

_prefixNP :: Record f E -> NP f start -> NP f start Source #

_breakNP :: NP f start -> (Record f E, NP f start) Source #

(Productlike start right middle, Productlike (v ': middle) left result) => Productlike (start :: [q]) (N color left k v right :: Map Symbol q) (result :: [q]) Source # 
Instance details

Defined in Data.RBR.Internal

Methods

_prefixNP :: Record f (N color left k v right) -> NP f start -> NP f result Source #

_breakNP :: NP f result -> (Record f (N color left k v right), NP f start) Source #

prefixNP :: forall start t result f. Productlike start t result => Record f t -> NP f start -> NP f result Source #

Flattens a Record and adds it to the initial part of the product.

breakNP :: forall start t result f. Productlike start t result => NP f result -> (Record f t, NP f start) Source #

Reconstructs a Record from the initial part of the product and returns the unconsumed part.

toNP :: forall t result f. Productlike '[] t result => Record f t -> NP f result Source #

Convert a Record into a n-ary product. The order of the elements in the product is not the order of insertion in the record.

>>> toNP (insertI @"foo" 'a' (insertI @"bar" True unit))
I True :* I 'a' :* Nil 

fromNP :: forall t result f. Productlike '[] t result => NP f result -> Record f t Source #

Convert a n-ary product into a compatible Record. Usually follows an invocation of toNP.

>>> prettyShowRecordI . fromNP @(Insert "foo" _ (Insert "bar" _ Empty)) . toNP $ insertI @"foo" 'a' (insertI @"bar" True unit)
"{bar = True, foo = 'a'}"

class Sumlike (start :: [k]) (t :: Map Symbol k) (result :: [k]) | start t -> result, result t -> start where Source #

Class from converting Variants to and from the n-ary sum type NS from Data.SOP.

prefixNS flattens a Variant and adds it to the initial part of the sum.

breakNS reconstructs a Variant from the initial part of the sum and returns the unconsumed part.

The functions toNS and fromNS are usually easier to use.

Methods

_prefixNS :: Either (NS f start) (Variant f t) -> NS f result Source #

_breakNS :: NS f result -> Either (NS f start) (Variant f t) Source #

Instances
Sumlike (v ': start) (N colorL leftL kL vL rightL) result => Sumlike (start :: [q]) (N color (N colorL leftL kL vL rightL) k v (E :: Map Symbol q) :: Map Symbol q) (result :: [q]) Source # 
Instance details

Defined in Data.RBR.Internal

Methods

_prefixNS :: Either (NS f start) (Variant f (N color (N colorL leftL kL vL rightL) k v E)) -> NS f result Source #

_breakNS :: NS f result -> Either (NS f start) (Variant f (N color (N colorL leftL kL vL rightL) k v E)) Source #

(Sumlike start (N colorR leftR kR vR rightR) middle, Sumlike (v ': middle) (N colorL leftL kL vL rightL) result) => Sumlike (start :: [q]) (N color (N colorL leftL kL vL rightL) k v (N colorR leftR kR vR rightR) :: Map Symbol q) (result :: [q]) Source # 
Instance details

Defined in Data.RBR.Internal

Methods

_prefixNS :: Either (NS f start) (Variant f (N color (N colorL leftL kL vL rightL) k v (N colorR leftR kR vR rightR))) -> NS f result Source #

_breakNS :: NS f result -> Either (NS f start) (Variant f (N color (N colorL leftL kL vL rightL) k v (N colorR leftR kR vR rightR))) Source #

Sumlike start (N colorR leftR kR vR rightR) middle => Sumlike (start :: [a]) (N color (E :: Map Symbol a) k v (N colorR leftR kR vR rightR) :: Map Symbol a) (v ': middle :: [a]) Source # 
Instance details

Defined in Data.RBR.Internal

Methods

_prefixNS :: Either (NS f start) (Variant f (N color E k v (N colorR leftR kR vR rightR))) -> NS f (v ': middle) Source #

_breakNS :: NS f (v ': middle) -> Either (NS f start) (Variant f (N color E k v (N colorR leftR kR vR rightR))) Source #

Sumlike (start :: [a]) (N color (E :: Map Symbol a) k v (E :: Map Symbol a) :: Map Symbol a) (v ': start :: [a]) Source # 
Instance details

Defined in Data.RBR.Internal

Methods

_prefixNS :: Either (NS f start) (Variant f (N color E k v E)) -> NS f (v ': start) Source #

_breakNS :: NS f (v ': start) -> Either (NS f start) (Variant f (N color E k v E)) Source #

prefixNS :: forall start t result f. Sumlike start t result => Either (NS f start) (Variant f t) -> NS f result Source #

Flattens a Variant and adds it to the initial part of the sum.

breakNS :: forall start t result f. Sumlike start t result => NS f result -> Either (NS f start) (Variant f t) Source #

Reconstructs a Variant from the initial part of the sum and returns the unconsumed part.

toNS :: forall t result f. Sumlike '[] t result => Variant f t -> NS f result Source #

Convert a Variant into a n-ary sum.

>>> toNS (injectI @"foo" 'a' :: Variant I (Insert "foo" Char (Insert "bar" Bool Empty)))
S (Z (I 'a')) 

fromNS :: forall t result f. Sumlike '[] t result => NS f result -> Variant f t Source #

Convert a n-ary sum into a compatible Variant.

>>> prettyShowVariantI $ fromNS @(Insert "foo" _ (Insert "bar" _ Empty)) . toNS $ (injectI @"foo" 'a' :: Variant I (Insert "foo" Char (Insert "bar" Bool Empty)))
"foo ('a')"

Data.SOP re-exports

newtype I a #

The identity type functor.

Like Identity, but with a shorter name.

Constructors

I a 
Instances
Monad I 
Instance details

Defined in Data.SOP.BasicFunctors

Methods

(>>=) :: I a -> (a -> I b) -> I b #

(>>) :: I a -> I b -> I b #

return :: a -> I a #

fail :: String -> I a #

Functor I 
Instance details

Defined in Data.SOP.BasicFunctors

Methods

fmap :: (a -> b) -> I a -> I b #

(<$) :: a -> I b -> I a #

Applicative I 
Instance details

Defined in Data.SOP.BasicFunctors

Methods

pure :: a -> I a #

(<*>) :: I (a -> b) -> I a -> I b #

liftA2 :: (a -> b -> c) -> I a -> I b -> I c #

(*>) :: I a -> I b -> I b #

(<*) :: I a -> I b -> I a #

Foldable I 
Instance details

Defined in Data.SOP.BasicFunctors

Methods

fold :: Monoid m => I m -> m #

foldMap :: Monoid m => (a -> m) -> I a -> m #

foldr :: (a -> b -> b) -> b -> I a -> b #

foldr' :: (a -> b -> b) -> b -> I a -> b #

foldl :: (b -> a -> b) -> b -> I a -> b #

foldl' :: (b -> a -> b) -> b -> I a -> b #

foldr1 :: (a -> a -> a) -> I a -> a #

foldl1 :: (a -> a -> a) -> I a -> a #

toList :: I a -> [a] #

null :: I a -> Bool #

length :: I a -> Int #

elem :: Eq a => a -> I a -> Bool #

maximum :: Ord a => I a -> a #

minimum :: Ord a => I a -> a #

sum :: Num a => I a -> a #

product :: Num a => I a -> a #

Traversable I 
Instance details

Defined in Data.SOP.BasicFunctors

Methods

traverse :: Applicative f => (a -> f b) -> I a -> f (I b) #

sequenceA :: Applicative f => I (f a) -> f (I a) #

mapM :: Monad m => (a -> m b) -> I a -> m (I b) #

sequence :: Monad m => I (m a) -> m (I a) #

Eq1 I

Since: sop-core-0.2.4.0

Instance details

Defined in Data.SOP.BasicFunctors

Methods

liftEq :: (a -> b -> Bool) -> I a -> I b -> Bool #

Ord1 I

Since: sop-core-0.2.4.0

Instance details

Defined in Data.SOP.BasicFunctors

Methods

liftCompare :: (a -> b -> Ordering) -> I a -> I b -> Ordering #

Read1 I

Since: sop-core-0.2.4.0

Instance details

Defined in Data.SOP.BasicFunctors

Methods

liftReadsPrec :: (Int -> ReadS a) -> ReadS [a] -> Int -> ReadS (I a) #

liftReadList :: (Int -> ReadS a) -> ReadS [a] -> ReadS [I a] #

liftReadPrec :: ReadPrec a -> ReadPrec [a] -> ReadPrec (I a) #

liftReadListPrec :: ReadPrec a -> ReadPrec [a] -> ReadPrec [I a] #

Show1 I

Since: sop-core-0.2.4.0

Instance details

Defined in Data.SOP.BasicFunctors

Methods

liftShowsPrec :: (Int -> a -> ShowS) -> ([a] -> ShowS) -> Int -> I a -> ShowS #

liftShowList :: (Int -> a -> ShowS) -> ([a] -> ShowS) -> [I a] -> ShowS #

NFData1 I

Since: sop-core-0.2.5.0

Instance details

Defined in Data.SOP.BasicFunctors

Methods

liftRnf :: (a -> ()) -> I a -> () #

Eq a => Eq (I a) 
Instance details

Defined in Data.SOP.BasicFunctors

Methods

(==) :: I a -> I a -> Bool #

(/=) :: I a -> I a -> Bool #

Ord a => Ord (I a) 
Instance details

Defined in Data.SOP.BasicFunctors

Methods

compare :: I a -> I a -> Ordering #

(<) :: I a -> I a -> Bool #

(<=) :: I a -> I a -> Bool #

(>) :: I a -> I a -> Bool #

(>=) :: I a -> I a -> Bool #

max :: I a -> I a -> I a #

min :: I a -> I a -> I a #

Read a => Read (I a) 
Instance details

Defined in Data.SOP.BasicFunctors

Methods

readsPrec :: Int -> ReadS (I a) #

readList :: ReadS [I a] #

readPrec :: ReadPrec (I a) #

readListPrec :: ReadPrec [I a] #

Show a => Show (I a) 
Instance details

Defined in Data.SOP.BasicFunctors

Methods

showsPrec :: Int -> I a -> ShowS #

show :: I a -> String #

showList :: [I a] -> ShowS #

Generic (I a) 
Instance details

Defined in Data.SOP.BasicFunctors

Associated Types

type Rep (I a) :: Type -> Type #

Methods

from :: I a -> Rep (I a) x #

to :: Rep (I a) x -> I a #

Semigroup a => Semigroup (I a)

Since: sop-core-0.4.0.0

Instance details

Defined in Data.SOP.BasicFunctors

Methods

(<>) :: I a -> I a -> I a #

sconcat :: NonEmpty (I a) -> I a #

stimes :: Integral b => b -> I a -> I a #

Monoid a => Monoid (I a)

Since: sop-core-0.4.0.0

Instance details

Defined in Data.SOP.BasicFunctors

Methods

mempty :: I a #

mappend :: I a -> I a -> I a #

mconcat :: [I a] -> I a #

NFData a => NFData (I a)

Since: sop-core-0.2.5.0

Instance details

Defined in Data.SOP.BasicFunctors

Methods

rnf :: I a -> () #

type Rep (I a) 
Instance details

Defined in Data.SOP.BasicFunctors

type Rep (I a) = D1 (MetaData "I" "Data.SOP.BasicFunctors" "sop-core-0.5.0.0-9cKeYbaA4A8Gi6ExvPweAM" True) (C1 (MetaCons "I" PrefixI False) (S1 (MetaSel (Nothing :: Maybe Symbol) NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 a)))

newtype K a (b :: k) :: forall k. Type -> k -> Type #

The constant type functor.

Like Constant, but kind-polymorphic in its second argument and with a shorter name.

Constructors

K a 
Instances
Eq2 (K :: Type -> Type -> Type)

Since: sop-core-0.2.4.0

Instance details

Defined in Data.SOP.BasicFunctors

Methods

liftEq2 :: (a -> b -> Bool) -> (c -> d -> Bool) -> K a c -> K b d -> Bool #

Ord2 (K :: Type -> Type -> Type)

Since: sop-core-0.2.4.0

Instance details

Defined in Data.SOP.BasicFunctors

Methods

liftCompare2 :: (a -> b -> Ordering) -> (c -> d -> Ordering) -> K a c -> K b d -> Ordering #

Read2 (K :: Type -> Type -> Type)

Since: sop-core-0.2.4.0

Instance details

Defined in Data.SOP.BasicFunctors

Methods

liftReadsPrec2 :: (Int -> ReadS a) -> ReadS [a] -> (Int -> ReadS b) -> ReadS [b] -> Int -> ReadS (K a b) #

liftReadList2 :: (Int -> ReadS a) -> ReadS [a] -> (Int -> ReadS b) -> ReadS [b] -> ReadS [K a b] #

liftReadPrec2 :: ReadPrec a -> ReadPrec [a] -> ReadPrec b -> ReadPrec [b] -> ReadPrec (K a b) #

liftReadListPrec2 :: ReadPrec a -> ReadPrec [a] -> ReadPrec b -> ReadPrec [b] -> ReadPrec [K a b] #

Show2 (K :: Type -> Type -> Type)

Since: sop-core-0.2.4.0

Instance details

Defined in Data.SOP.BasicFunctors

Methods

liftShowsPrec2 :: (Int -> a -> ShowS) -> ([a] -> ShowS) -> (Int -> b -> ShowS) -> ([b] -> ShowS) -> Int -> K a b -> ShowS #

liftShowList2 :: (Int -> a -> ShowS) -> ([a] -> ShowS) -> (Int -> b -> ShowS) -> ([b] -> ShowS) -> [K a b] -> ShowS #

NFData2 (K :: Type -> Type -> Type)

Since: sop-core-0.2.5.0

Instance details

Defined in Data.SOP.BasicFunctors

Methods

liftRnf2 :: (a -> ()) -> (b -> ()) -> K a b -> () #

Functor (K a :: Type -> Type) 
Instance details

Defined in Data.SOP.BasicFunctors

Methods

fmap :: (a0 -> b) -> K a a0 -> K a b #

(<$) :: a0 -> K a b -> K a a0 #

Monoid a => Applicative (K a :: Type -> Type) 
Instance details

Defined in Data.SOP.BasicFunctors

Methods

pure :: a0 -> K a a0 #

(<*>) :: K a (a0 -> b) -> K a a0 -> K a b #

liftA2 :: (a0 -> b -> c) -> K a a0 -> K a b -> K a c #

(*>) :: K a a0 -> K a b -> K a b #

(<*) :: K a a0 -> K a b -> K a a0 #

Foldable (K a :: Type -> Type) 
Instance details

Defined in Data.SOP.BasicFunctors

Methods

fold :: Monoid m => K a m -> m #

foldMap :: Monoid m => (a0 -> m) -> K a a0 -> m #

foldr :: (a0 -> b -> b) -> b -> K a a0 -> b #

foldr' :: (a0 -> b -> b) -> b -> K a a0 -> b #

foldl :: (b -> a0 -> b) -> b -> K a a0 -> b #

foldl' :: (b -> a0 -> b) -> b -> K a a0 -> b #

foldr1 :: (a0 -> a0 -> a0) -> K a a0 -> a0 #

foldl1 :: (a0 -> a0 -> a0) -> K a a0 -> a0 #

toList :: K a a0 -> [a0] #

null :: K a a0 -> Bool #

length :: K a a0 -> Int #

elem :: Eq a0 => a0 -> K a a0 -> Bool #

maximum :: Ord a0 => K a a0 -> a0 #

minimum :: Ord a0 => K a a0 -> a0 #

sum :: Num a0 => K a a0 -> a0 #

product :: Num a0 => K a a0 -> a0 #

Traversable (K a :: Type -> Type) 
Instance details

Defined in Data.SOP.BasicFunctors

Methods

traverse :: Applicative f => (a0 -> f b) -> K a a0 -> f (K a b) #

sequenceA :: Applicative f => K a (f a0) -> f (K a a0) #

mapM :: Monad m => (a0 -> m b) -> K a a0 -> m (K a b) #

sequence :: Monad m => K a (m a0) -> m (K a a0) #

Eq a => Eq1 (K a :: Type -> Type)

Since: sop-core-0.2.4.0

Instance details

Defined in Data.SOP.BasicFunctors

Methods

liftEq :: (a0 -> b -> Bool) -> K a a0 -> K a b -> Bool #

Ord a => Ord1 (K a :: Type -> Type)

Since: sop-core-0.2.4.0

Instance details

Defined in Data.SOP.BasicFunctors

Methods

liftCompare :: (a0 -> b -> Ordering) -> K a a0 -> K a b -> Ordering #

Read a => Read1 (K a :: Type -> Type)

Since: sop-core-0.2.4.0

Instance details

Defined in Data.SOP.BasicFunctors

Methods

liftReadsPrec :: (Int -> ReadS a0) -> ReadS [a0] -> Int -> ReadS (K a a0) #

liftReadList :: (Int -> ReadS a0) -> ReadS [a0] -> ReadS [K a a0] #

liftReadPrec :: ReadPrec a0 -> ReadPrec [a0] -> ReadPrec (K a a0) #

liftReadListPrec :: ReadPrec a0 -> ReadPrec [a0] -> ReadPrec [K a a0] #

Show a => Show1 (K a :: Type -> Type)

Since: sop-core-0.2.4.0

Instance details

Defined in Data.SOP.BasicFunctors

Methods

liftShowsPrec :: (Int -> a0 -> ShowS) -> ([a0] -> ShowS) -> Int -> K a a0 -> ShowS #

liftShowList :: (Int -> a0 -> ShowS) -> ([a0] -> ShowS) -> [K a a0] -> ShowS #

NFData a => NFData1 (K a :: Type -> Type)

Since: sop-core-0.2.5.0

Instance details

Defined in Data.SOP.BasicFunctors

Methods

liftRnf :: (a0 -> ()) -> K a a0 -> () #

Eq a => Eq (K a b) 
Instance details

Defined in Data.SOP.BasicFunctors

Methods

(==) :: K a b -> K a b -> Bool #

(/=) :: K a b -> K a b -> Bool #

Ord a => Ord (K a b) 
Instance details

Defined in Data.SOP.BasicFunctors

Methods

compare :: K a b -> K a b -> Ordering #

(<) :: K a b -> K a b -> Bool #

(<=) :: K a b -> K a b -> Bool #

(>) :: K a b -> K a b -> Bool #

(>=) :: K a b -> K a b -> Bool #

max :: K a b -> K a b -> K a b #

min :: K a b -> K a b -> K a b #

Read a => Read (K a b) 
Instance details

Defined in Data.SOP.BasicFunctors

Methods

readsPrec :: Int -> ReadS (K a b) #

readList :: ReadS [K a b] #

readPrec :: ReadPrec (K a b) #

readListPrec :: ReadPrec [K a b] #

Show a => Show (K a b) 
Instance details

Defined in Data.SOP.BasicFunctors

Methods

showsPrec :: Int -> K a b -> ShowS #

show :: K a b -> String #

showList :: [K a b] -> ShowS #

Generic (K a b) 
Instance details

Defined in Data.SOP.BasicFunctors

Associated Types

type Rep (K a b) :: Type -> Type #

Methods

from :: K a b -> Rep (K a b) x #

to :: Rep (K a b) x -> K a b #

Semigroup a => Semigroup (K a b)

Since: sop-core-0.4.0.0

Instance details

Defined in Data.SOP.BasicFunctors

Methods

(<>) :: K a b -> K a b -> K a b #

sconcat :: NonEmpty (K a b) -> K a b #

stimes :: Integral b0 => b0 -> K a b -> K a b #

Monoid a => Monoid (K a b)

Since: sop-core-0.4.0.0

Instance details

Defined in Data.SOP.BasicFunctors

Methods

mempty :: K a b #

mappend :: K a b -> K a b -> K a b #

mconcat :: [K a b] -> K a b #

NFData a => NFData (K a b)

Since: sop-core-0.2.5.0

Instance details

Defined in Data.SOP.BasicFunctors

Methods

rnf :: K a b -> () #

type Rep (K a b) 
Instance details

Defined in Data.SOP.BasicFunctors

type Rep (K a b) = D1 (MetaData "K" "Data.SOP.BasicFunctors" "sop-core-0.5.0.0-9cKeYbaA4A8Gi6ExvPweAM" True) (C1 (MetaCons "K" PrefixI False) (S1 (MetaSel (Nothing :: Maybe Symbol) NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 a)))

data NP (a :: k -> Type) (b :: [k]) :: forall k. (k -> Type) -> [k] -> Type 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 :: forall k (a :: k -> Type) (b :: [k]). NP a ([] :: [k]) 
(:*) :: forall k (a :: k -> Type) (b :: [k]) (x :: k) (xs :: [k]). a x -> NP a xs -> NP a (x ': xs) infixr 5 
Instances
HTrans (NP :: (k1 -> Type) -> [k1] -> Type) (NP :: (k2 -> Type) -> [k2] -> Type) 
Instance details

Defined in Data.SOP.NP

Methods

htrans :: AllZipN (Prod NP) c xs ys => proxy c -> (forall (x :: k10) (y :: k20). c x y => f x -> g y) -> NP f xs -> NP g ys #

hcoerce :: (AllZipN (Prod NP) (LiftedCoercible f g) xs ys, HTrans NP NP) => NP f xs -> NP g ys #

HPure (NP :: (k -> Type) -> [k] -> Type) 
Instance details

Defined in Data.SOP.NP

Methods

hpure :: SListIN NP xs => (forall (a :: k0). f a) -> NP f xs #

hcpure :: AllN NP c xs => proxy c -> (forall (a :: k0). c a => f a) -> NP f xs #

HAp (NP :: (k -> Type) -> [k] -> Type) 
Instance details

Defined in Data.SOP.NP

Methods

hap :: Prod NP (f -.-> g) xs -> NP f xs -> NP g xs #

HCollapse (NP :: (k -> Type) -> [k] -> Type) 
Instance details

Defined in Data.SOP.NP

Methods

hcollapse :: SListIN NP xs => NP (K a) xs -> CollapseTo NP a #

HTraverse_ (NP :: (k -> Type) -> [k] -> Type) 
Instance details

Defined in Data.SOP.NP

Methods

hctraverse_ :: (AllN NP c xs, Applicative g) => proxy c -> (forall (a :: k0). c a => f a -> g ()) -> NP f xs -> g () #

htraverse_ :: (SListIN NP xs, Applicative g) => (forall (a :: k0). f a -> g ()) -> NP f xs -> g () #

HSequence (NP :: (k -> Type) -> [k] -> Type) 
Instance details

Defined in Data.SOP.NP

Methods

hsequence' :: (SListIN NP xs, Applicative f) => NP (f :.: g) xs -> f (NP g xs) #

hctraverse' :: (AllN NP c xs, Applicative g) => proxy c -> (forall (a :: k0). c a => f a -> g (f' a)) -> NP f xs -> g (NP f' xs) #

htraverse' :: (SListIN NP xs, Applicative g) => (forall (a :: k0). f a -> g (f' a)) -> NP f xs -> g (NP f' xs) #

All (Compose Eq f) xs => Eq (NP f xs) 
Instance details

Defined in Data.SOP.NP

Methods

(==) :: NP f xs -> NP f xs -> Bool #

(/=) :: NP f xs -> NP f xs -> Bool #

(All (Compose Eq f) xs, All (Compose Ord f) xs) => Ord (NP f xs) 
Instance details

Defined in Data.SOP.NP

Methods

compare :: NP f xs -> NP f xs -> Ordering #

(<) :: NP f xs -> NP f xs -> Bool #

(<=) :: NP f xs -> NP f xs -> Bool #

(>) :: NP f xs -> NP f xs -> Bool #

(>=) :: NP f xs -> NP f xs -> Bool #

max :: NP f xs -> NP f xs -> NP f xs #

min :: NP f xs -> NP f xs -> NP f xs #

All (Compose Show f) xs => Show (NP f xs) 
Instance details

Defined in Data.SOP.NP

Methods

showsPrec :: Int -> NP f xs -> ShowS #

show :: NP f xs -> String #

showList :: [NP f xs] -> ShowS #

All (Compose Semigroup f) xs => Semigroup (NP f xs)

Since: sop-core-0.4.0.0

Instance details

Defined in Data.SOP.NP

Methods

(<>) :: NP f xs -> NP f xs -> NP f xs #

sconcat :: NonEmpty (NP f xs) -> NP f xs #

stimes :: Integral b => b -> NP f xs -> NP f xs #

(All (Compose Monoid f) xs, All (Compose Semigroup f) xs) => Monoid (NP f xs)

Since: sop-core-0.4.0.0

Instance details

Defined in Data.SOP.NP

Methods

mempty :: NP f xs #

mappend :: NP f xs -> NP f xs -> NP f xs #

mconcat :: [NP f xs] -> NP f xs #

All (Compose NFData f) xs => NFData (NP f xs)

Since: sop-core-0.2.5.0

Instance details

Defined in Data.SOP.NP

Methods

rnf :: NP f xs -> () #

type Same (NP :: (k1 -> Type) -> [k1] -> Type) 
Instance details

Defined in Data.SOP.NP

type Same (NP :: (k1 -> Type) -> [k1] -> Type) = (NP :: (k2 -> Type) -> [k2] -> Type)
type Prod (NP :: (k -> Type) -> [k] -> Type) 
Instance details

Defined in Data.SOP.NP

type Prod (NP :: (k -> Type) -> [k] -> Type) = (NP :: (k -> Type) -> [k] -> Type)
type UnProd (NP :: (k -> Type) -> [k] -> Type) 
Instance details

Defined in Data.SOP.NS

type UnProd (NP :: (k -> Type) -> [k] -> Type) = (NS :: (k -> Type) -> [k] -> Type)
type CollapseTo (NP :: (k -> Type) -> [k] -> Type) a 
Instance details

Defined in Data.SOP.NP

type CollapseTo (NP :: (k -> Type) -> [k] -> Type) a = [a]
type SListIN (NP :: (k -> Type) -> [k] -> Type) 
Instance details

Defined in Data.SOP.NP

type SListIN (NP :: (k -> Type) -> [k] -> Type) = (SListI :: [k] -> Constraint)
type AllN (NP :: (k -> Type) -> [k] -> Type) (c :: k -> Constraint) 
Instance details

Defined in Data.SOP.NP

type AllN (NP :: (k -> Type) -> [k] -> Type) (c :: k -> Constraint) = All c
type AllZipN (NP :: (k -> Type) -> [k] -> Type) (c :: a -> b -> Constraint) 
Instance details

Defined in Data.SOP.NP

type AllZipN (NP :: (k -> Type) -> [k] -> Type) (c :: a -> b -> Constraint) = AllZip c

data NS (a :: k -> Type) (b :: [k]) :: forall k. (k -> Type) -> [k] -> Type 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 :: forall k (a :: k -> Type) (b :: [k]) (x :: k) (xs :: [k]). a x -> NS a (x ': xs) 
S :: forall k (a :: k -> Type) (b :: [k]) (xs :: [k]) (x :: k). NS a xs -> NS a (x ': xs) 
Instances
HTrans (NS :: (k1 -> Type) -> [k1] -> Type) (NS :: (k2 -> Type) -> [k2] -> Type) 
Instance details

Defined in Data.SOP.NS

Methods

htrans :: AllZipN (Prod NS) c xs ys => proxy c -> (forall (x :: k10) (y :: k20). c x y => f x -> g y) -> NS f xs -> NS g ys #

hcoerce :: (AllZipN (Prod NS) (LiftedCoercible f g) xs ys, HTrans NS NS) => NS f xs -> NS g ys #

HAp (NS :: (k -> Type) -> [k] -> Type) 
Instance details

Defined in Data.SOP.NS

Methods

hap :: Prod NS (f -.-> g) xs -> NS f xs -> NS g xs #

HCollapse (NS :: (k -> Type) -> [k] -> Type) 
Instance details

Defined in Data.SOP.NS

Methods

hcollapse :: SListIN NS xs => NS (K a) xs -> CollapseTo NS a #

HTraverse_ (NS :: (k -> Type) -> [k] -> Type) 
Instance details

Defined in Data.SOP.NS

Methods

hctraverse_ :: (AllN NS c xs, Applicative g) => proxy c -> (forall (a :: k0). c a => f a -> g ()) -> NS f xs -> g () #

htraverse_ :: (SListIN NS xs, Applicative g) => (forall (a :: k0). f a -> g ()) -> NS f xs -> g () #

HSequence (NS :: (k -> Type) -> [k] -> Type) 
Instance details

Defined in Data.SOP.NS

Methods

hsequence' :: (SListIN NS xs, Applicative f) => NS (f :.: g) xs -> f (NS g xs) #

hctraverse' :: (AllN NS c xs, Applicative g) => proxy c -> (forall (a :: k0). c a => f a -> g (f' a)) -> NS f xs -> g (NS f' xs) #

htraverse' :: (SListIN NS xs, Applicative g) => (forall (a :: k0). f a -> g (f' a)) -> NS f xs -> g (NS f' xs) #

HIndex (NS :: (k -> Type) -> [k] -> Type) 
Instance details

Defined in Data.SOP.NS

Methods

hindex :: NS f xs -> Int #

HApInjs (NS :: (k -> Type) -> [k] -> Type) 
Instance details

Defined in Data.SOP.NS

Methods

hapInjs :: SListIN NS xs => Prod NS f xs -> [NS f xs] #

HExpand (NS :: (k -> Type) -> [k] -> Type) 
Instance details

Defined in Data.SOP.NS

Methods

hexpand :: SListIN (Prod NS) xs => (forall (x :: k0). f x) -> NS f xs -> Prod NS f xs #

hcexpand :: AllN (Prod NS) c xs => proxy c -> (forall (x :: k0). c x => f x) -> NS f xs -> Prod NS f xs #

All (Compose Eq f) xs => Eq (NS f xs) 
Instance details

Defined in Data.SOP.NS

Methods

(==) :: NS f xs -> NS f xs -> Bool #

(/=) :: NS f xs -> NS f xs -> Bool #

(All (Compose Eq f) xs, All (Compose Ord f) xs) => Ord (NS f xs) 
Instance details

Defined in Data.SOP.NS

Methods

compare :: NS f xs -> NS f xs -> Ordering #

(<) :: NS f xs -> NS f xs -> Bool #

(<=) :: NS f xs -> NS f xs -> Bool #

(>) :: NS f xs -> NS f xs -> Bool #

(>=) :: NS f xs -> NS f xs -> Bool #

max :: NS f xs -> NS f xs -> NS f xs #

min :: NS f xs -> NS f xs -> NS f xs #

All (Compose Show f) xs => Show (NS f xs) 
Instance details

Defined in Data.SOP.NS

Methods

showsPrec :: Int -> NS f xs -> ShowS #

show :: NS f xs -> String #

showList :: [NS f xs] -> ShowS #

All (Compose NFData f) xs => NFData (NS f xs)

Since: sop-core-0.2.5.0

Instance details

Defined in Data.SOP.NS

Methods

rnf :: NS f xs -> () #

type Same (NS :: (k1 -> Type) -> [k1] -> Type) 
Instance details

Defined in Data.SOP.NS

type Same (NS :: (k1 -> Type) -> [k1] -> Type) = (NS :: (k2 -> Type) -> [k2] -> Type)
type Prod (NS :: (k -> Type) -> [k] -> Type) 
Instance details

Defined in Data.SOP.NS

type Prod (NS :: (k -> Type) -> [k] -> Type) = (NP :: (k -> Type) -> [k] -> Type)
type CollapseTo (NS :: (k -> Type) -> [k] -> Type) a 
Instance details

Defined in Data.SOP.NS

type CollapseTo (NS :: (k -> Type) -> [k] -> Type) a = a
type SListIN (NS :: (k -> Type) -> [k] -> Type) 
Instance details

Defined in Data.SOP.NS

type SListIN (NS :: (k -> Type) -> [k] -> Type) = (SListI :: [k] -> Constraint)
type AllN (NS :: (k -> Type) -> [k] -> Type) (c :: k -> Constraint) 
Instance details

Defined in Data.SOP.NS

type AllN (NS :: (k -> Type) -> [k] -> Type) (c :: k -> Constraint) = All c