union-0.1.2: Extensible type-safe unions

Safe HaskellNone
LanguageHaskell2010

Data.Union

Description

Extensible type-safe unions.

>>> let a = openUnion # (5 :: Int) :: OpenUnion '[Bool, Int]
>>> a ^? openUnion @Int
Just 5
>>> a ^? openUnion @Bool
Nothing
>>> a ^? openUnion @Char
<interactive>:7:6: error:
    • No instance for (UElem Char '[] (RIndex Char '[]))
        arising from a use of ‘openUnion’
    • In the second argument of ‘(^?)’, namely ‘openUnion @Char’
      In the expression: a ^? openUnion @Char
      In an equation for ‘it’: it = a ^? openUnion @Char
Synopsis

Documentation

data Union (f :: u -> *) (as :: [u]) where Source #

A union is parameterized by a universe u, an interpretation f and a list of labels as. The labels of the union are given by inhabitants of the kind u; the type of values at any label a :: u is given by its interpretation f a :: *.

Constructors

This :: !(f a) -> Union f (a ': as) 
That :: !(Union f as) -> Union f (a ': as) 
Instances
(Eq (f a2), Eq (Union f as)) => Eq (Union f (a2 ': as)) Source # 
Instance details

Defined in Data.Union

Methods

(==) :: Union f (a2 ': as) -> Union f (a2 ': as) -> Bool #

(/=) :: Union f (a2 ': as) -> Union f (a2 ': as) -> Bool #

Eq (Union f ([] :: [u])) Source # 
Instance details

Defined in Data.Union

Methods

(==) :: Union f [] -> Union f [] -> Bool #

(/=) :: Union f [] -> Union f [] -> Bool #

(Ord (f a2), Ord (Union f as)) => Ord (Union f (a2 ': as)) Source # 
Instance details

Defined in Data.Union

Methods

compare :: Union f (a2 ': as) -> Union f (a2 ': as) -> Ordering #

(<) :: Union f (a2 ': as) -> Union f (a2 ': as) -> Bool #

(<=) :: Union f (a2 ': as) -> Union f (a2 ': as) -> Bool #

(>) :: Union f (a2 ': as) -> Union f (a2 ': as) -> Bool #

(>=) :: Union f (a2 ': as) -> Union f (a2 ': as) -> Bool #

max :: Union f (a2 ': as) -> Union f (a2 ': as) -> Union f (a2 ': as) #

min :: Union f (a2 ': as) -> Union f (a2 ': as) -> Union f (a2 ': as) #

Ord (Union f ([] :: [u])) Source # 
Instance details

Defined in Data.Union

Methods

compare :: Union f [] -> Union f [] -> Ordering #

(<) :: Union f [] -> Union f [] -> Bool #

(<=) :: Union f [] -> Union f [] -> Bool #

(>) :: Union f [] -> Union f [] -> Bool #

(>=) :: Union f [] -> Union f [] -> Bool #

max :: Union f [] -> Union f [] -> Union f [] #

min :: Union f [] -> Union f [] -> Union f [] #

(Show (f a2), Show (Union f as)) => Show (Union f (a2 ': as)) Source # 
Instance details

Defined in Data.Union

Methods

showsPrec :: Int -> Union f (a2 ': as) -> ShowS #

show :: Union f (a2 ': as) -> String #

showList :: [Union f (a2 ': as)] -> ShowS #

Show (Union f ([] :: [u])) Source # 
Instance details

Defined in Data.Union

Methods

showsPrec :: Int -> Union f [] -> ShowS #

show :: Union f [] -> String #

showList :: [Union f []] -> ShowS #

Generic (Union f (a ': as)) Source # 
Instance details

Defined in Data.Union

Associated Types

type Rep (Union f (a ': as)) :: * -> * #

Methods

from :: Union f (a ': as) -> Rep (Union f (a ': as)) x #

to :: Rep (Union f (a ': as)) x -> Union f (a ': as) #

Generic (Union f ([] :: [u])) Source # 
Instance details

Defined in Data.Union

Associated Types

type Rep (Union f []) :: * -> * #

Methods

from :: Union f [] -> Rep (Union f []) x #

to :: Rep (Union f []) x -> Union f [] #

(f ~ Identity, Exception a, Typeable as, Exception (Union f as)) => Exception (Union f (a ': as)) Source # 
Instance details

Defined in Data.Union

Methods

toException :: Union f (a ': as) -> SomeException #

fromException :: SomeException -> Maybe (Union f (a ': as)) #

displayException :: Union f (a ': as) -> String #

f ~ Identity => Exception (Union f ([] :: [*])) Source # 
Instance details

Defined in Data.Union

(NFData (f a2), NFData (Union f as)) => NFData (Union f (a2 ': as)) Source # 
Instance details

Defined in Data.Union

Methods

rnf :: Union f (a2 ': as) -> () #

NFData (Union f ([] :: [u])) Source # 
Instance details

Defined in Data.Union

Methods

rnf :: Union f [] -> () #

(Hashable (f a2), Hashable (Union f as)) => Hashable (Union f (a2 ': as)) Source # 
Instance details

Defined in Data.Union

Methods

hashWithSalt :: Int -> Union f (a2 ': as) -> Int #

hash :: Union f (a2 ': as) -> Int #

Hashable (Union f ([] :: [u])) Source # 
Instance details

Defined in Data.Union

Methods

hashWithSalt :: Int -> Union f [] -> Int #

hash :: Union f [] -> Int #

type Rep (Union f (a ': as)) Source # 
Instance details

Defined in Data.Union

type Rep (Union f (a ': as)) = C1 (MetaCons "This" PrefixI False) (Rec0 (f a)) :+: C1 (MetaCons "That" PrefixI False) (Rec0 (Union f as))
type Rep (Union f ([] :: [u])) Source # 
Instance details

Defined in Data.Union

type Rep (Union f ([] :: [u])) = (V1 :: * -> *)

union :: (Union f as -> c) -> (f a -> c) -> Union f (a ': as) -> c Source #

Case analysis for unions.

absurdUnion :: Union f '[] -> a Source #

Since a union with an empty list of labels is uninhabited, we can recover any type from it.

umap :: (forall a. f a -> g a) -> Union f as -> Union g as Source #

_This :: Prism (Union f (a ': as)) (Union f (b ': as)) (f a) (f b) Source #

_That :: Prism (Union f (a ': as)) (Union f (a ': bs)) (Union f as) (Union f bs) Source #

class i ~ RIndex a as => UElem (a :: u) (as :: [u]) (i :: Nat) where Source #

Minimal complete definition

uprism | ulift, umatch

Methods

uprism :: Prism' (Union f as) (f a) Source #

ulift :: f a -> Union f as Source #

umatch :: Union f as -> Maybe (f a) Source #

Instances
UElem (a2 :: a1) (a2 ': as :: [a1]) Z Source # 
Instance details

Defined in Data.Union

Methods

uprism :: (Choice p, Applicative f) => p (f a2) (f (f a2)) -> p (Union f (a2 ': as)) (f (Union f (a2 ': as))) Source #

ulift :: f a2 -> Union f (a2 ': as) Source #

umatch :: Union f (a2 ': as) -> Maybe (f a2) Source #

(RIndex a2 (b ': as) ~ S i, UElem a2 as i) => UElem (a2 :: a1) (b ': as :: [a1]) (S i) Source # 
Instance details

Defined in Data.Union

Methods

uprism :: (Choice p, Applicative f) => p (f a2) (f (f a2)) -> p (Union f (b ': as)) (f (Union f (b ': as))) Source #

ulift :: f a2 -> Union f (b ': as) Source #

umatch :: Union f (b ': as) -> Maybe (f a2) Source #

class is ~ RImage as bs => USubset (as :: [u]) (bs :: [u]) is where Source #

Minimal complete definition

usubset | urelax, urestrict

Methods

usubset :: Prism' (Union f bs) (Union f as) Source #

urelax :: Union f as -> Union f bs Source #

urestrict :: Union f bs -> Maybe (Union f as) Source #

Instances
USubset ([] :: [u]) (bs :: [u]) ([] :: [Nat]) Source # 
Instance details

Defined in Data.Union

Methods

usubset :: (Choice p, Applicative f) => p (Union f []) (f (Union f [])) -> p (Union f bs) (f (Union f bs)) Source #

urelax :: Union f [] -> Union f bs Source #

urestrict :: Union f bs -> Maybe (Union f []) Source #

(UElem a bs i, USubset as bs is) => USubset (a ': as :: [u]) (bs :: [u]) (i ': is) Source # 
Instance details

Defined in Data.Union

Methods

usubset :: (Choice p, Applicative f) => p (Union f (a ': as)) (f (Union f (a ': as))) -> p (Union f bs) (f (Union f bs)) Source #

urelax :: Union f (a ': as) -> Union f bs Source #

urestrict :: Union f bs -> Maybe (Union f (a ': as)) Source #

openUnion :: forall a as. UElem a as (RIndex a as) => Prism' (OpenUnion as) a Source #