Safe Haskell  None 

Language  Haskell2010 
Reexport Which without the constructor
 data Which xs
 zilch :: Which '[]
 pick :: forall x xs. UniqueMember x xs => x > Which xs
 pick0 :: x > Which (x ': xs)
 pickOnly :: x > Which '[x]
 pickL :: forall l x xs proxy. (UniqueLabelMember l xs, x ~ KindAtLabel l xs) => proxy l > x > Which xs
 pickN :: forall n x xs proxy. MemberAt n x xs => proxy n > x > Which xs
 obvious :: Which '[a] > a
 trial :: forall x xs. UniqueMember x xs => Which xs > Either (Which (Without x xs)) x
 trial' :: forall x xs. UniqueMember x xs => Which xs > Maybe x
 trial0 :: forall x xs. Which (x ': xs) > Either (Which xs) x
 trial0' :: forall x xs. Which (x ': xs) > Maybe x
 trialL :: forall l x xs proxy. (UniqueLabelMember l xs, x ~ KindAtLabel l xs) => proxy l > Which xs > Either (Which (Without x xs)) x
 trialL' :: forall l x xs proxy. (UniqueLabelMember l xs, x ~ KindAtLabel l xs) => proxy l > Which xs > Maybe x
 trialN :: forall n x xs proxy. MemberAt n x xs => proxy n > Which xs > Either (Which (WithoutIndex n xs)) x
 trialN' :: forall n x xs proxy. MemberAt n x xs => proxy n > Which xs > Maybe x
 type Diversify branch tree = Reduce (Which branch) (Switcher (CaseDiversify branch tree) branch (Which tree))
 diversify :: forall branch tree. Diversify branch tree => Which branch > Which tree
 diversify' :: forall branch tree. (Diversify branch tree, SameLength branch tree) => Which branch > Which tree
 diversify0 :: forall x xs. Which xs > Which (x ': xs)
 diversifyL :: forall ls branch tree proxy. (Diversify branch tree, branch ~ KindsAtLabels ls tree, UniqueLabels ls tree, IsDistinct ls) => proxy ls > Which branch > Which tree
 type DiversifyN indices branch tree = (Reduce (Which branch) (SwitcherN (CaseDiversifyN indices) 0 branch (Which tree)), KindsAtIndices indices tree ~ branch)
 diversifyN :: forall indices branch tree proxy. DiversifyN indices branch tree => proxy indices > Which branch > Which tree
 type Reinterpret branch tree = Reduce (Which tree) (Switcher (CaseReinterpret branch tree) tree (Either (Which (Complement tree branch)) (Which branch)))
 reinterpret :: forall branch tree. Reinterpret branch tree => Which tree > Either (Which (Complement tree branch)) (Which branch)
 type Reinterpret' branch tree = Reduce (Which tree) (Switcher (CaseReinterpret' branch tree) tree (Maybe (Which branch)))
 reinterpret' :: forall branch tree. Reinterpret' branch tree => Which tree > Maybe (Which branch)
 reinterpretL :: forall ls branch tree proxy. (Reinterpret branch tree, branch ~ KindsAtLabels ls tree, UniqueLabels ls tree, IsDistinct ls) => proxy ls > Which tree > Either (Which (Complement tree branch)) (Which branch)
 reinterpretL' :: forall ls branch tree proxy. (Reinterpret' branch tree, branch ~ KindsAtLabels ls tree, UniqueLabels ls tree, IsDistinct ls) => proxy ls > Which tree > Maybe (Which branch)
 type ReinterpretN' indices branch tree = (Reduce (Which tree) (SwitcherN (CaseReinterpretN' indices) 0 tree (Maybe (Which branch))), KindsAtIndices indices tree ~ branch)
 reinterpretN' :: forall indices branch tree proxy. ReinterpretN' indices branch tree => proxy indices > Which tree > Maybe (Which branch)
 type Switch case' xs r = Reduce (Which xs) (Switcher case' xs r)
 newtype Switcher c xs r = Switcher (c xs r)
 which :: Switch case' xs r => case' xs r > Which xs > r
 switch :: Switch case' xs r => Which xs > case' xs r > r
 type SwitchN case' n xs r = Reduce (Which xs) (SwitcherN case' n xs r)
 newtype SwitcherN c n xs r = SwitcherN (c n xs r)
 whichN :: SwitchN case' n xs r => case' n xs r > Which xs > r
 switchN :: SwitchN case' n xs r => Which xs > case' n xs r > r
Which
type
A Which
is an anonymous sum type (also known as a polymorphic variant, or corecord)
which can only contain one of the types in the typelist.
This is essentially a typed version of Dynamic
.
The following functions are available can be used to manipulate unique types in the typelist
These functions are type specified. This means labels are not required because the types themselves can be used to access the Which
.
It is a compile error to use those functions for duplicate fields.
For duplicate types in the list of possible types, Natindexed version of the functions are available:
 constructor:
pickN
 destructor:
trialN
 inejction:
diversifyN
andreinterpretN
 catamorphism:
whichN
orswitchN
Encoding: The variant contains a value whose type is at the given position in the type list. This is the same encoding as Haskus.Util.Variant and Data.Hlist.Variant.
The constructor is only exported in the Data.Diverse.Which.Internal module
Reduce (Which ((:) Type x xs)) (Switcher CaseEqWhich ((:) Type x xs) Bool) => Eq (Which ((:) Type x xs)) Source #  Two 
Eq (Which ([] Type)) Source #  ( 
(Reduce (Which ((:) Type x xs)) (Switcher CaseEqWhich ((:) Type x xs) Bool), Reduce (Which ((:) Type x xs)) (Switcher CaseOrdWhich ((:) Type x xs) Ordering)) => Ord (Which ((:) Type x xs)) Source #  A 
Ord (Which ([] Type)) Source #  ( 
WhichRead (Which_ ((:) Type x xs)) => Read (Which ((:) Type x xs)) Source #  This 
Read (Which ([] Type)) Source #  read "zilch" == 
Reduce (Which ((:) Type x xs)) (Switcher CaseShowWhich ((:) Type x xs) ShowS) => Show (Which ((:) Type x xs)) Source #  show ( 
Show (Which ([] Type)) Source #  read "zilch" == 
Generic (Which ((:) Type x ((:) Type x' xs))) Source #  A 
Generic (Which ((:) Type x ([] Type))) Source #  A terminating 
Generic (Which ([] Type)) Source #  A terminating 
Semigroup (Which ([] Type)) Source #  
Monoid (Which ([] Type)) Source #  
(NFData x, NFData (Which ((:) Type x' xs))) => NFData (Which ((:) * x ((:) Type x' xs))) Source #  
NFData x => NFData (Which ((:) * x ([] *))) Source #  
NFData (Which ([] Type)) Source #  
Case c ((:) Type x ([] Type)) r => Reduce (Which ((:) Type x ([] Type))) (Switcher c ((:) Type x ([] Type)) r) Source #  Terminating case of the loop, ensuring that a instance of 
(Case c ((:) Type x ((:) Type x' xs)) r, Reduce (Which ((:) Type x' xs)) (Switcher c ((:) Type x' xs) r), Reiterate * c ((:) Type x ((:) Type x' xs))) => Reduce (Which ((:) Type x ((:) Type x' xs))) (Switcher c ((:) Type x ((:) Type x' xs)) r) Source # 

Case (c n) ((:) Type x ([] Type)) r => Reduce (Which ((:) Type x ([] Type))) (SwitcherN c n ((:) Type x ([] Type)) r) Source #  Terminating case of the loop, ensuring that a instance of 
(Case (c n) ((:) Type x ((:) Type x' xs)) r, Reduce (Which ((:) Type x' xs)) (SwitcherN c ((+) n 1) ((:) Type x' xs) r), ReiterateN * c n ((:) Type x ((:) Type x' xs))) => Reduce (Which ((:) Type x ((:) Type x' xs))) (SwitcherN c n ((:) Type x ((:) Type x' xs)) r) Source # 

type Rep (Which ((:) Type x ((:) Type x' xs))) Source #  
type Rep (Which ((:) Type x ([] Type))) Source #  
type Rep (Which ([] Type)) Source #  
Single type
Construction
pick :: forall x xs. UniqueMember x xs => x > Which xs Source #
pickL :: forall l x xs proxy. (UniqueLabelMember l xs, x ~ KindAtLabel l xs) => proxy l > x > Which xs Source #
Destruction
trial' :: forall x xs. UniqueMember x xs => Which xs > Maybe x Source #
Variation of trial
which returns a Maybe
trial0' :: forall x xs. Which (x ': xs) > Maybe x Source #
Variation of trial0
which returns a Maybe
trialL :: forall l x xs proxy. (UniqueLabelMember l xs, x ~ KindAtLabel l xs) => proxy l > Which xs > Either (Which (Without x xs)) x Source #
trialL' :: forall l x xs proxy. (UniqueLabelMember l xs, x ~ KindAtLabel l xs) => proxy l > Which xs > Maybe x Source #
Variation of trialL
which returns a Maybe
trialN :: forall n x xs proxy. MemberAt n x xs => proxy n > Which xs > Either (Which (WithoutIndex n xs)) x Source #
trialN' :: forall n x xs proxy. MemberAt n x xs => proxy n > Which xs > Maybe x Source #
Variation of trialN
which returns a Maybe
Multiple types
Injection
type Diversify branch tree = Reduce (Which branch) (Switcher (CaseDiversify branch tree) branch (Which tree)) Source #
A friendlier constraint synonym for diversify
.
diversify :: forall branch tree. Diversify branch tree => Which branch > Which tree Source #
Convert a Which
to another Which
that may include other possibilities.
That is, branch
is equal or is a subset of tree
.
This can also be used to rearrange the order of the types in the Which
.
It is a compile error if tree
has duplicate types with branch
.
NB. forall is used to tree
is ordered first, so TypeApplications can be used to specify tree
first.
let a =pick'
(5 :: Int) ::Which
'[Int] b =diversify
@[Int, Bool] a ::Which
'[Int, Bool] c =diversify
@[Bool, Int] b ::Which
'[Bool, Int]
diversify' :: forall branch tree. (Diversify branch tree, SameLength branch tree) => Which branch > Which tree Source #
A restricted version of diversify
which only rearranges the types
diversify0 :: forall x xs. Which xs > Which (x ': xs) Source #
A simple version of diversify
which add another type to the front of the typelist.
diversifyL :: forall ls branch tree proxy. (Diversify branch tree, branch ~ KindsAtLabels ls tree, UniqueLabels ls tree, IsDistinct ls) => proxy ls > Which branch > Which tree Source #
A variation of diversify
where branch
is additionally specified by a labels list.
let y =pickOnly
(5 :: Tagged Bar Int) y' =diversifyL
@'[Bar] Proxy y ::Which
'[Tagged Bar Int, Tagged Foo Bool] y'' =diversifyL
@'[Bar, Foo] Proxy y' ::Which
'[Tagged Foo Bool, Tagged Bar Int]switch
y'' (CaseTypeable
(show . typeRep . (pure @Proxy))) `shouldBe` "Tagged * Bar Int"
type DiversifyN indices branch tree = (Reduce (Which branch) (SwitcherN (CaseDiversifyN indices) 0 branch (Which tree)), KindsAtIndices indices tree ~ branch) Source #
A friendlier constraint synonym for diversifyN
.
diversifyN :: forall indices branch tree proxy. DiversifyN indices branch tree => proxy indices > Which branch > Which tree Source #
A variation of diversify
which uses a Nat list indices
to specify how to reorder the fields, where
indices[branch_idx] = tree_idx
This variation allows tree
to contain duplicate types with branch
since
the mapping is specified by indicies
.
let y =pickOnly
(5 :: Int) y' =diversifyN
@'[0] @[Int, Bool] Proxy y y'' =diversifyN
@[1,0] @[Bool, Int] Proxy y'switch
y'' (CaseTypeable
(show . typeRep . (pure @Proxy))) `shouldBe` "Int"
Inverse Injection
type Reinterpret branch tree = Reduce (Which tree) (Switcher (CaseReinterpret branch tree) tree (Either (Which (Complement tree branch)) (Which branch))) Source #
A friendlier constraint synonym for reinterpret
.
reinterpret :: forall branch tree. Reinterpret branch tree => Which tree > Either (Which (Complement tree branch)) (Which branch) Source #
Convert a Which
into possibly another Which
with a totally different typelist.
Returns either a Which
with the Right
value, or a Which
with the Left
over compliment
types.
It is a compile error if branch
or compliment
has duplicate types with tree
.
NB. forall used to specify branch
first, so TypeApplications can be used to specify branch
first.
let a =pick
@[Int, Char, Bool] (5 :: Int) ::Which
'[Int, Char, Bool] let b =reinterpret
[String, Char] y b `shouldBe` Left (
[String, Int] a c `shouldBe` Right (pick
(5 :: Int)) ::Which
'[Int, Bool] let c =reinterpret
pick
(5 :: Int)) ::Which
'[String, Int]
type Reinterpret' branch tree = Reduce (Which tree) (Switcher (CaseReinterpret' branch tree) tree (Maybe (Which branch))) Source #
A friendlier constraint synonym for reinterpret'
.
reinterpret' :: forall branch tree. Reinterpret' branch tree => Which tree > Maybe (Which branch) Source #
Variation of reinterpret
which returns a Maybe.
reinterpretL :: forall ls branch tree proxy. (Reinterpret branch tree, branch ~ KindsAtLabels ls tree, UniqueLabels ls tree, IsDistinct ls) => proxy ls > Which tree > Either (Which (Complement tree branch)) (Which branch) Source #
A variation of reinterpret
where the branch
is additionally specified with a labels list.
let y =pick
@[Tagged Bar Int, Tagged Foo Bool, Tagged Hi Char, Tagged Bye Bool] (5 :: Tagged Bar Int) y' =reinterpretL
@[Foo, Bar] Proxy y x =pick
@[Tagged Foo Bool, Tagged Bar Int] (5 :: Tagged Bar Int) y' `shouldBe` Right x
reinterpretL' :: forall ls branch tree proxy. (Reinterpret' branch tree, branch ~ KindsAtLabels ls tree, UniqueLabels ls tree, IsDistinct ls) => proxy ls > Which tree > Maybe (Which branch) Source #
Variation of reinterpretL
which returns a Maybe.
type ReinterpretN' indices branch tree = (Reduce (Which tree) (SwitcherN (CaseReinterpretN' indices) 0 tree (Maybe (Which branch))), KindsAtIndices indices tree ~ branch) Source #
A friendlier constraint synonym for reinterpretN
.
reinterpretN' :: forall indices branch tree proxy. ReinterpretN' indices branch tree => proxy indices > Which tree > Maybe (Which branch) Source #
A limited variation of reinterpret
which uses a Nat list n
to specify how to reorder the fields, where
indices[branch_idx] = tree_idx
This variation allows tree
to contain duplicate types with branch
since the mapping is specified by indicies
.
However, unlike reinterpert
, in this variation,
branch
must be a subset of tree
instead of any arbitrary Which.
Also it returns a Maybe instead of Either.
This is so that the same indices
can be used in narrowN
.
Catamorphism
type Switch case' xs r = Reduce (Which xs) (Switcher case' xs r) Source #
A friendlier constraint synonym for switch
.
newtype Switcher c xs r Source #
Switcher
is an instance of Reduce
for which reiterate
s through the possibilities in a Which
,
delegating handling to Case
, ensuring termination when Which
only contains one type.
Switcher (c xs r) 
Case c ((:) Type x ([] Type)) r => Reduce (Which ((:) Type x ([] Type))) (Switcher c ((:) Type x ([] Type)) r) Source #  Terminating case of the loop, ensuring that a instance of 
(Case c ((:) Type x ((:) Type x' xs)) r, Reduce (Which ((:) Type x' xs)) (Switcher c ((:) Type x' xs) r), Reiterate * c ((:) Type x ((:) Type x' xs))) => Reduce (Which ((:) Type x ((:) Type x' xs))) (Switcher c ((:) Type x ((:) Type x' xs)) r) Source # 

type Reduced (Switcher c xs r) Source #  
switch :: Switch case' xs r => Which xs > case' xs r > r Source #
A switch/case statement for Which
. This is equivalent to flip
which
Use Case
instances like Cases
to apply a Which
of functions to a variant of values.
let y =pick
(5 :: Int) ::Which
'[Int, Bool]switch
y (cases
(show @Bool./
show @Int./
nil
)) `shouldBe` "5"
Or CaseTypeable
to apply a polymorphic function that work on all Typeables
.
let y =pick
(5 :: Int) ::Which
'[Int, Bool]switch
y (CaseTypeable
(show . typeRep . (pure @Proxy))) `shouldBe` Int
Or you may use your own custom instance of Case
.
type SwitchN case' n xs r = Reduce (Which xs) (SwitcherN case' n xs r) Source #
A friendlier constraint synonym for switch
.
newtype SwitcherN c n xs r Source #
SwitcherN
is a variation of Switcher
which reiterateN
s through the possibilities in a Which
,
delegating work to CaseN
, ensuring termination when Which
only contains one type.
SwitcherN (c n xs r) 
Case (c n) ((:) Type x ([] Type)) r => Reduce (Which ((:) Type x ([] Type))) (SwitcherN c n ((:) Type x ([] Type)) r) Source #  Terminating case of the loop, ensuring that a instance of 
(Case (c n) ((:) Type x ((:) Type x' xs)) r, Reduce (Which ((:) Type x' xs)) (SwitcherN c ((+) n 1) ((:) Type x' xs) r), ReiterateN * c n ((:) Type x ((:) Type x' xs))) => Reduce (Which ((:) Type x ((:) Type x' xs))) (SwitcherN c n ((:) Type x ((:) Type x' xs)) r) Source # 

type Reduced (SwitcherN c n xs r) Source #  
switchN :: SwitchN case' n xs r => Which xs > case' n xs r > r Source #
A switch/case statement for Which
. This is equivalent to flip
whichN
Use Case
instances like CasesN
to apply a Which
of functions to a variant of values
in index order.
let y =pickN
@0 Proxy (5 :: Int) ::Which
'[Int, Bool, Bool, Int]switchN
y (casesN
(show @Int./
show @Bool./
show @Bool./
show @Int./
nil
)) `shouldBe` "5"
Or you may use your own custom instance of Case
.