Safe Haskell | None |
---|---|
Language | Haskell2010 |
Open sum type
- data Variant (l :: [*])
- type V = Variant
- variantIndex :: Variant a -> Word
- pattern V :: forall c cs. Popable c cs => c -> Variant cs
- pattern VMaybe :: forall c cs. MaybePopable c cs => c -> Variant cs
- toVariantAt :: forall (n :: Nat) (l :: [*]). KnownNat n => Index n l -> Variant l
- toVariantHead :: forall x xs. x -> Variant (x ': xs)
- toVariantTail :: forall x xs. Variant xs -> Variant (x ': xs)
- fromVariantAt :: forall (n :: Nat) (l :: [*]). KnownNat n => Variant l -> Maybe (Index n l)
- popVariantAt :: forall (n :: Nat) l. KnownNat n => Variant l -> Either (Variant (RemoveAt n l)) (Index n l)
- popVariantHead :: forall x xs. Variant (x ': xs) -> Either (Variant xs) x
- updateVariantAt :: forall (n :: Nat) a b l. (KnownNat n, a ~ Index n l) => (a -> b) -> Variant l -> Variant (ReplaceN n b l)
- foldMapVariantAt :: forall (n :: Nat) l l2. (KnownNat n, KnownNat (Length l2)) => (Index n l -> Variant l2) -> Variant l -> Variant (ReplaceAt n l l2)
- foldMapVariantAtM :: forall (n :: Nat) m l l2. (KnownNat n, KnownNat (Length l2), Monad m) => (Index n l -> m (Variant l2)) -> Variant l -> m (Variant (ReplaceAt n l l2))
- toVariant :: forall a l. Member a l => a -> Variant l
- type Member x xs = (IsMember x xs ~ True, x ~ Index (IndexOf x xs) xs, KnownNat (IndexOf x xs))
- type family Filter a (l :: [*]) where ...
- type Popable a xs = (Member a xs, PopVariant a xs)
- type MaybePopable a xs = PopVariant a xs
- popVariant :: forall a xs. Popable a xs => Variant xs -> Either (Variant (Filter a xs)) a
- popVariantMaybe :: forall a xs. MaybePopable a xs => Variant xs -> Either (Variant (Filter a xs)) a
- fromVariant :: forall a xs. Popable a xs => Variant xs -> Maybe a
- fromVariantMaybe :: forall a xs. MaybePopable a xs => Variant xs -> Maybe a
- fromVariantFirst :: forall a l. Member a l => Variant l -> Maybe a
- updateVariantFirst :: forall a b n l. (Member a l, n ~ IndexOf a l) => (a -> b) -> Variant l -> Variant (ReplaceN n b l)
- updateVariantFirstM :: forall (n :: Nat) l l2 m. (KnownNat n, Monad m) => (Index n l -> m (Index n l2)) -> Variant l -> m (Variant l2)
- type MappableVariant a b cs = MapVariant a b cs (IndexesOf a cs)
- mapVariant :: forall a b cs. MappableVariant a b cs => (a -> b) -> Variant cs -> Variant (ReplaceNS (IndexesOf a cs) b cs)
- foldMapVariantFirst :: forall a (n :: Nat) l l2. (KnownNat n, KnownNat (Length l2), n ~ IndexOf a l, a ~ Index n l) => (a -> Variant l2) -> Variant l -> Variant (ReplaceAt n l l2)
- foldMapVariantFirstM :: forall a (n :: Nat) l l2 m. (KnownNat n, KnownNat (Length l2), n ~ IndexOf a l, a ~ Index n l, Monad m) => (a -> m (V l2)) -> V l -> m (V (ReplaceAt n l l2))
- foldMapVariant :: forall a cs ds i. (i ~ IndexOf a cs, Popable a cs) => (a -> V ds) -> V cs -> V (InsertAt i (Filter a cs) ds)
- class AlterVariant c (b :: [*]) where
- class TraverseVariant c (b :: [*]) m where
- class NoConstraint a
- alterVariant :: forall c (a :: [*]). AlterVariant c a => (forall x. c x => x -> x) -> Variant a -> Variant a
- traverseVariant :: forall c (a :: [*]) m. (TraverseVariant c a m, Monad m) => (forall x. c x => x -> m x) -> Variant a -> m (Variant a)
- traverseVariant_ :: forall c (a :: [*]) m. (TraverseVariant c a m, Monad m) => (forall x. c x => x -> m ()) -> Variant a -> m ()
- appendVariant :: forall (ys :: [*]) (xs :: [*]). Variant xs -> Variant (Concat xs ys)
- prependVariant :: forall (ys :: [*]) (xs :: [*]). KnownNat (Length ys) => Variant xs -> Variant (Concat ys xs)
- type Liftable xs ys = (IsSubset xs ys ~ True, VariantLift xs ys)
- liftVariant :: forall xs ys. Liftable xs ys => Variant xs -> Variant ys
- nubVariant :: Liftable xs (Nub xs) => V xs -> V (Nub xs)
- variantToValue :: Variant '[a] -> a
- variantToEither :: forall a b. Variant '[a, b] -> Either b a
- variantFromEither :: Either a b -> Variant '[b, a]
- variantToHList :: VariantToHList xs => Variant xs -> HList (MapMaybe xs)
- variantToTuple :: forall l t. (VariantToHList l, HTuple' (MapMaybe l) t) => Variant l -> t
- class ContVariant xs where
Documentation
data Variant (l :: [*]) Source #
A variant contains a value whose type is at the given position in the type list
variantIndex :: Variant a -> Word Source #
Get Variant index
Patterns
pattern V :: forall c cs. Popable c cs => c -> Variant cs Source #
Make GHC consider l
as a representational parameter to make coercions
between Variant values unsafe
Pattern synonym for Variant
Usage: case v of V (x :: Int) -> ... V (x :: String) -> ...
pattern VMaybe :: forall c cs. MaybePopable c cs => c -> Variant cs Source #
Statically unchecked matching on a Variant
Operations by index
toVariantAt :: forall (n :: Nat) (l :: [*]). KnownNat n => Index n l -> Variant l Source #
Set the value with the given indexed type
toVariantHead :: forall x xs. x -> Variant (x ': xs) Source #
Set the first value
toVariantTail :: forall x xs. Variant xs -> Variant (x ': xs) Source #
Set the tail
fromVariantAt :: forall (n :: Nat) (l :: [*]). KnownNat n => Variant l -> Maybe (Index n l) Source #
Get the value if it has the indexed type
popVariantAt :: forall (n :: Nat) l. KnownNat n => Variant l -> Either (Variant (RemoveAt n l)) (Index n l) Source #
Pop a variant value by index, return either the value or the remaining variant
popVariantHead :: forall x xs. Variant (x ': xs) -> Either (Variant xs) x Source #
Pop the head of a variant value
updateVariantAt :: forall (n :: Nat) a b l. (KnownNat n, a ~ Index n l) => (a -> b) -> Variant l -> Variant (ReplaceN n b l) Source #
Update a variant value
foldMapVariantAt :: forall (n :: Nat) l l2. (KnownNat n, KnownNat (Length l2)) => (Index n l -> Variant l2) -> Variant l -> Variant (ReplaceAt n l l2) Source #
Update a variant value with a variant and fold the result
foldMapVariantAtM :: forall (n :: Nat) m l l2. (KnownNat n, KnownNat (Length l2), Monad m) => (Index n l -> m (Variant l2)) -> Variant l -> m (Variant (ReplaceAt n l l2)) Source #
Update a variant value with a variant and fold the result
Operations by type
toVariant :: forall a l. Member a l => a -> Variant l Source #
Put a value into a Variant
Use the first matching type index.
type Member x xs = (IsMember x xs ~ True, x ~ Index (IndexOf x xs) xs, KnownNat (IndexOf x xs)) Source #
Constraint: x member of xs
type MaybePopable a xs = PopVariant a xs Source #
a may be popable in xs
popVariant :: forall a xs. Popable a xs => Variant xs -> Either (Variant (Filter a xs)) a Source #
Extract a type from a variant. Return either the value of this type or the remaining variant
popVariantMaybe :: forall a xs. MaybePopable a xs => Variant xs -> Either (Variant (Filter a xs)) a Source #
Extract a type from a variant. Return either the value of this type or the remaining variant
fromVariant :: forall a xs. Popable a xs => Variant xs -> Maybe a Source #
Try to a get a value of a given type from a Variant
fromVariantMaybe :: forall a xs. MaybePopable a xs => Variant xs -> Maybe a Source #
Try to a get a value of a given type from a Variant that may not even support the given type.
fromVariantFirst :: forall a l. Member a l => Variant l -> Maybe a Source #
Pick the first matching type of a Variant
fromVariantFirst @A (Variant 2 undefined :: Variant '[A,B,A]) == Nothing
updateVariantFirst :: forall a b n l. (Member a l, n ~ IndexOf a l) => (a -> b) -> Variant l -> Variant (ReplaceN n b l) Source #
Update a variant value
updateVariantFirstM :: forall (n :: Nat) l l2 m. (KnownNat n, Monad m) => (Index n l -> m (Index n l2)) -> Variant l -> m (Variant l2) Source #
Monadic update of the first matching variant value
type MappableVariant a b cs = MapVariant a b cs (IndexesOf a cs) Source #
mapVariant :: forall a b cs. MappableVariant a b cs => (a -> b) -> Variant cs -> Variant (ReplaceNS (IndexesOf a cs) b cs) Source #
Map the matching types of a variant
foldMapVariantFirst :: forall a (n :: Nat) l l2. (KnownNat n, KnownNat (Length l2), n ~ IndexOf a l, a ~ Index n l) => (a -> Variant l2) -> Variant l -> Variant (ReplaceAt n l l2) Source #
Update a variant value with a variant and fold the result
foldMapVariantFirstM :: forall a (n :: Nat) l l2 m. (KnownNat n, KnownNat (Length l2), n ~ IndexOf a l, a ~ Index n l, Monad m) => (a -> m (V l2)) -> V l -> m (V (ReplaceAt n l l2)) Source #
Update a variant value with a variant and fold the result
foldMapVariant :: forall a cs ds i. (i ~ IndexOf a cs, Popable a cs) => (a -> V ds) -> V cs -> V (InsertAt i (Filter a cs) ds) Source #
Update a variant value with a variant and fold the result
Generic operations with type classes
class AlterVariant c (b :: [*]) where Source #
AlterVariant c ([] *) Source # | |
(AlterVariant c xs, c x) => AlterVariant c ((:) * x xs) Source # | |
class TraverseVariant c (b :: [*]) m where Source #
TraverseVariant c ([] *) m Source # | |
(TraverseVariant c xs m, c x, Monad m) => TraverseVariant c ((:) * x xs) m Source # | |
class NoConstraint a Source #
Useful to specify a "* -> Constraint" function returning no constraint
alterVariant :: forall c (a :: [*]). AlterVariant c a => (forall x. c x => x -> x) -> Variant a -> Variant a Source #
Alter a variant. You need to specify the constraints required by the modifying function.
Usage:
alterVariant NoConstraint id v
alterVariant
Resizable (resize 4) v
class (Ord a, Num a) => OrdNum a instance (Ord a, Num a) => OrdNum a
traverseVariant :: forall c (a :: [*]) m. (TraverseVariant c a m, Monad m) => (forall x. c x => x -> m x) -> Variant a -> m (Variant a) Source #
Traverse a variant. You need to specify the constraints required by the modifying function.
traverseVariant_ :: forall c (a :: [*]) m. (TraverseVariant c a m, Monad m) => (forall x. c x => x -> m ()) -> Variant a -> m () Source #
Traverse a variant. You need to specify the constraints required by the modifying function.
Conversions between variants
appendVariant :: forall (ys :: [*]) (xs :: [*]). Variant xs -> Variant (Concat xs ys) Source #
Extend a variant by appending other possible values
prependVariant :: forall (ys :: [*]) (xs :: [*]). KnownNat (Length ys) => Variant xs -> Variant (Concat ys xs) Source #
Extend a variant by prepending other possible values
liftVariant :: forall xs ys. Liftable xs ys => Variant xs -> Variant ys Source #
Lift a variant into another
Set values to the first matching type
Conversions to/from other data types
variantToValue :: Variant '[a] -> a Source #
Retreive a single value
variantToEither :: forall a b. Variant '[a, b] -> Either b a Source #
Convert a variant of two values in a Either
variantFromEither :: Either a b -> Variant '[b, a] Source #
Lift an Either into a Variant (reversed order by convention)
variantToHList :: VariantToHList xs => Variant xs -> HList (MapMaybe xs) Source #
Convert a variant into a HList of Maybes
variantToTuple :: forall l t. (VariantToHList l, HTuple' (MapMaybe l) t) => Variant l -> t Source #
Get variant possible values in a tuple of Maybe types
Continuations
class ContVariant xs where Source #
variantToCont :: Variant xs -> ContFlow xs r Source #
Convert a variant into a multi-continuation
variantToContM :: Monad m => m (Variant xs) -> ContFlow xs (m r) Source #
Convert a variant into a multi-continuation
contToVariant :: ContFlow xs (Variant xs) -> Variant xs Source #
Convert a multi-continuation into a Variant
contToVariantM :: Monad m => ContFlow xs (m (Variant xs)) -> m (Variant xs) Source #
Convert a multi-continuation into a Variant
ContVariant ((:) * a ((:) * b ((:) * c ((:) * d ((:) * e ((:) * f ((:) * g ((:) * h ((:) * i ((:) * j ((:) * k ((:) * l ([] *))))))))))))) Source # | |
ContVariant ((:) * a ((:) * b ((:) * c ((:) * d ((:) * e ((:) * f ((:) * g ((:) * h ((:) * i ((:) * j ((:) * k ([] *)))))))))))) Source # | |
ContVariant ((:) * a ((:) * b ((:) * c ((:) * d ((:) * e ((:) * f ((:) * g ((:) * h ((:) * i ((:) * j ([] *))))))))))) Source # | |
ContVariant ((:) * a ((:) * b ((:) * c ((:) * d ((:) * e ((:) * f ((:) * g ((:) * h ((:) * i ([] *)))))))))) Source # | |
ContVariant ((:) * a ((:) * b ((:) * c ((:) * d ((:) * e ((:) * f ((:) * g ((:) * h ([] *))))))))) Source # | |
ContVariant ((:) * a ((:) * b ((:) * c ((:) * d ((:) * e ((:) * f ((:) * g ([] *)))))))) Source # | |
ContVariant ((:) * a ((:) * b ((:) * c ((:) * d ((:) * e ((:) * f ([] *))))))) Source # | |
ContVariant ((:) * a ((:) * b ((:) * c ((:) * d ((:) * e ([] *)))))) Source # | |
ContVariant ((:) * a ((:) * b ((:) * c ((:) * d ([] *))))) Source # | |
ContVariant ((:) * a ((:) * b ((:) * c ([] *)))) Source # | |
ContVariant ((:) * a ((:) * b ([] *))) Source # | |
ContVariant ((:) * a ([] *)) Source # | |