module Haskus.Utils.Variant
( Variant
, V
, variantIndex
, pattern V
, pattern VMaybe
, toVariantAt
, toVariantHead
, toVariantTail
, fromVariantAt
, popVariantAt
, popVariantHead
, updateVariantAt
, foldMapVariantAt
, foldMapVariantAtM
, toVariant
, Member
, Filter
, Popable
, MaybePopable
, popVariant
, popVariantMaybe
, fromVariant
, fromVariantMaybe
, fromVariantFirst
, updateVariantFirst
, updateVariantFirstM
, MappableVariant
, mapVariant
, foldMapVariantFirst
, foldMapVariantFirstM
, foldMapVariant
, AlterVariant (..)
, TraverseVariant (..)
, NoConstraint
, alterVariant
, traverseVariant
, traverseVariant_
, appendVariant
, prependVariant
, Liftable
, liftVariant
, nubVariant
, variantToValue
, variantToEither
, variantFromEither
, variantToHList
, variantToTuple
, ContVariant (..)
)
where
import Unsafe.Coerce
import GHC.Exts (Any,Constraint)
import Haskus.Utils.Monad
import Haskus.Utils.Types
import Haskus.Utils.Tuple
import Haskus.Utils.HList
import Haskus.Utils.ContFlow
import Haskus.Utils.Types.List
data Variant (l :: [*]) = Variant !Word Any
type V = Variant
type role Variant representational
pattern V :: forall c cs. Popable c cs => c -> Variant cs
pattern V x <- (fromVariant -> Just x)
where
V x = toVariant x
pattern VMaybe :: forall c cs. (MaybePopable c cs) => c -> Variant cs
pattern VMaybe x <- (fromVariantMaybe -> Just x)
instance Eq (Variant '[]) where
(==) = error "Empty variant"
instance
( Eq (Variant xs)
, Eq x
) => Eq (Variant (x ': xs))
where
(==) v1@(Variant t1 _) v2@(Variant t2 _)
| t1 /= t2 = False
| otherwise = case (popVariantHead v1, popVariantHead v2) of
(Right a, Right b) -> a == b
(Left as, Left bs) -> as == bs
_ -> False
instance Ord (Variant '[]) where
compare = error "Empty variant"
instance
( Ord (Variant xs)
, Ord x
) => Ord (Variant (x ': xs))
where
compare v1 v2 = case (popVariantHead v1, popVariantHead v2) of
(Right a, Right b) -> compare a b
(Left as, Left bs) -> compare as bs
(Right _, Left _) -> LT
(Left _, Right _) -> GT
instance Show (Variant '[]) where
show = error "Empty variant"
instance
( Show (Variant xs)
, Show x
) => Show (Variant (x ': xs))
where
show v = case popVariantHead v of
Right x -> show x
Left xs -> show xs
variantIndex :: Variant a -> Word
variantIndex (Variant n _) = n
toVariantAt :: forall (n :: Nat) (l :: [*]).
( KnownNat n
) => Index n l -> Variant l
toVariantAt a = Variant (natValue' @n) (unsafeCoerce a)
toVariantHead :: forall x xs. x -> Variant (x ': xs)
toVariantHead a = Variant 0 (unsafeCoerce a)
toVariantTail :: forall x xs. Variant xs -> Variant (x ': xs)
toVariantTail (Variant t a) = Variant (t+1) a
fromVariantAt :: forall (n :: Nat) (l :: [*]).
( KnownNat n
) => Variant l -> Maybe (Index n l)
fromVariantAt (Variant t a) = do
guard (t == natValue' @n)
return (unsafeCoerce a)
popVariantAt :: forall (n :: Nat) l.
( KnownNat n
) => Variant l -> Either (Variant (RemoveAt n l)) (Index n l)
popVariantAt v@(Variant t a) = case fromVariantAt @n v of
Just x -> Right x
Nothing -> Left $ if t > natValue' @n
then Variant (t1) a
else Variant t a
popVariantHead :: forall x xs. Variant (x ': xs) -> Either (Variant xs) x
popVariantHead v@(Variant t a) = case fromVariantAt @0 v of
Just x -> Right x
Nothing -> Left $ Variant (t1) a
updateVariantAt :: forall (n :: Nat) a b l.
( KnownNat n
, a ~ Index n l
) => (a -> b) -> Variant l -> Variant (ReplaceN n b l)
updateVariantAt f v@(Variant t a) =
case fromVariantAt @n v of
Nothing -> Variant t a
Just x -> Variant t (unsafeCoerce (f x))
toVariant :: forall a l.
( Member a l
) => a -> Variant l
toVariant = toVariantAt @(IndexOf a l)
class PopVariant a xs where
popVariant' :: Variant xs -> Either (Variant (Filter a xs)) a
instance PopVariant a '[] where
popVariant' _ = undefined
instance forall a xs n xs' y ys.
( PopVariant a xs'
, n ~ MaybeIndexOf a xs
, xs' ~ RemoveAt1 n xs
, Filter a xs' ~ Filter a xs
, KnownNat n
, xs ~ (y ': ys)
) => PopVariant a (y ': ys)
where
popVariant' (Variant t a)
= case natValue' @n of
0 -> Left (Variant t a)
n | n1 == t -> Right (unsafeCoerce a)
| n1 < t -> popVariant' @a @xs' (Variant (t1) a)
| otherwise -> Left (Variant t a)
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
popVariant v = popVariant' @a v
popVariantMaybe :: forall a xs.
( MaybePopable a xs
) => Variant xs -> Either (Variant (Filter a xs)) a
popVariantMaybe v = popVariant' @a v
fromVariantFirst :: forall a l.
( Member a l
) => Variant l -> Maybe a
fromVariantFirst = fromVariantAt @(IndexOf a l)
fromVariant :: forall a xs.
( Popable a xs
) => Variant xs -> Maybe a
fromVariant v = case popVariant v of
Right a -> Just a
Left _ -> Nothing
fromVariantMaybe :: forall a xs.
( MaybePopable a xs
) => Variant xs -> Maybe a
fromVariantMaybe v = case popVariantMaybe v of
Right a -> Just a
Left _ -> Nothing
updateVariantFirst :: forall a b n l.
( Member a l
, n ~ IndexOf a l
) => (a -> b) -> Variant l -> Variant (ReplaceN n b l)
updateVariantFirst f v = updateVariantAt @n f v
updateVariantFirstM :: forall (n :: Nat) l l2 m .
(KnownNat n, Monad m)
=> (Index n l -> m (Index n l2)) -> Variant l -> m (Variant l2)
updateVariantFirstM f v@(Variant t a) =
case fromVariantAt @n v of
Nothing -> return (Variant t a)
Just x -> Variant t <$> unsafeCoerce (f x)
class MapVariant a b cs (is :: [Nat]) where
mapVariant' :: (a -> b) -> Variant cs -> Variant (ReplaceNS is b cs)
instance MapVariant a b '[] is where
mapVariant' = undefined
instance MapVariant a b cs '[] where
mapVariant' _ v = v
instance forall a b cs is i.
( MapVariant a b (ReplaceN i b cs) is
, a ~ Index i cs
, KnownNat i
) => MapVariant a b cs (i ': is) where
mapVariant' f v = mapVariant' @a @b @(ReplaceN i b cs) @is f (updateVariantAt @i f v)
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)
mapVariant = mapVariant' @a @b @cs @(IndexesOf a cs)
foldMapVariantAt :: forall (n :: Nat) l l2 .
( KnownNat n
, KnownNat (Length l2)
) => (Index n l -> Variant l2) -> Variant l -> Variant (ReplaceAt n l l2)
foldMapVariantAt f v@(Variant t a) =
case fromVariantAt @n v of
Nothing ->
if t < n
then Variant t a
else Variant (t+nl21) a
Just x -> case f x of
Variant t2 a2 -> Variant (t2+n) a2
where
n = natValue' @n
nl2 = natValue' @(Length 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))
foldMapVariantAtM f v@(Variant t a) =
case fromVariantAt @n v of
Nothing ->
return $ if t < n
then Variant t a
else Variant (t+nl21) a
Just x -> do
y <- f x
case y of
Variant t2 a2 -> return (Variant (t2+n) a2)
where
n = natValue' @n
nl2 = natValue' @(Length l2)
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)
foldMapVariantFirst f v = foldMapVariantAt @n f v
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))
foldMapVariantFirstM f v = foldMapVariantAtM @n f v
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)
foldMapVariant f v = case popVariant v of
Right a -> case f a of
Variant t x -> Variant (i + t) x
Left (Variant t x)
| t < i -> Variant t x
| otherwise -> Variant (i+t) x
where
i = natValue' @i
class AlterVariant c (b :: [*]) where
alterVariant' :: Alter c -> Word -> Any -> Any
instance AlterVariant c '[] where
alterVariant' = undefined
instance
( AlterVariant c xs
, c x
) => AlterVariant c (x ': xs)
where
alterVariant' m@(Alter f) t v =
case t of
0 -> unsafeCoerce (f (unsafeCoerce v :: x))
n -> alterVariant' @c @xs m (n1) v
data Alter (c :: * -> Constraint) = Alter (forall a. c a => a -> a)
data AlterM (c :: * -> Constraint) m = AlterM (forall a. (Monad m, c a) => a -> m a)
class NoConstraint a
instance NoConstraint a
class TraverseVariant c (b :: [*]) m where
traverseVariant' :: AlterM c m -> Word -> Any -> m Any
instance TraverseVariant c '[] m where
traverseVariant' = undefined
instance
( TraverseVariant c xs m
, c x
, Monad m
) => TraverseVariant c (x ': xs) m
where
traverseVariant' m@(AlterM f) t v =
case t of
0 -> unsafeCoerce <$> f (unsafeCoerce v :: x)
n -> traverseVariant' @c @xs m (n1) v
alterVariant :: forall c (a :: [*]).
( AlterVariant c a
) => (forall x. c x => x -> x) -> Variant a -> Variant a
alterVariant f (Variant t a) =
Variant t (alterVariant' @c @a (Alter @c f) t 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 f (Variant t a) =
Variant t <$> traverseVariant' @c @a (AlterM @c @m f) t a
traverseVariant_ :: forall c (a :: [*]) m.
( TraverseVariant c a m
, Monad m
) => (forall x. c x => x -> m ()) -> Variant a -> m ()
traverseVariant_ f v = void (traverseVariant @c @a f' v)
where
f' :: forall x. c x => x -> m x
f' x = f x >> return x
appendVariant :: forall (ys :: [*]) (xs :: [*]). Variant xs -> Variant (Concat xs ys)
appendVariant (Variant t a) = Variant t a
prependVariant :: forall (ys :: [*]) (xs :: [*]).
( KnownNat (Length ys)
) => Variant xs -> Variant (Concat ys xs)
prependVariant (Variant t a) = Variant (n+t) a
where
n = natValue' @(Length ys)
type Liftable xs ys =
( IsSubset xs ys ~ 'True
, VariantLift xs ys
)
class VariantLift xs ys where
liftVariant' :: Variant xs -> Variant ys
instance VariantLift '[] ys where
liftVariant' = error "Lifting empty variant"
instance forall xs ys x.
( VariantLift xs ys
, KnownNat (IndexOf x ys)
) => VariantLift (x ': xs) ys
where
liftVariant' (Variant t a)
| t == 0 = Variant (natValue' @(IndexOf x ys)) a
| otherwise = liftVariant' @xs (Variant (t1) a)
liftVariant :: forall xs ys.
( Liftable xs ys
) => Variant xs -> Variant ys
liftVariant = liftVariant'
nubVariant :: (Liftable xs (Nub xs)) => V xs -> V (Nub xs)
nubVariant = liftVariant
variantToValue :: Variant '[a] -> a
variantToValue (Variant _ a) = unsafeCoerce a
variantToEither :: forall a b. Variant '[a,b] -> Either b a
variantToEither (Variant 0 a) = Right (unsafeCoerce a)
variantToEither (Variant _ a) = Left (unsafeCoerce a)
class VariantToHList xs where
variantToHList :: Variant xs -> HList (MapMaybe xs)
instance VariantToHList '[] where
variantToHList _ = HNil
instance
( VariantToHList xs
) => VariantToHList (x ': xs)
where
variantToHList v@(Variant t a) =
fromVariantAt @0 v `HCons` variantToHList v'
where
v' :: Variant xs
v' = Variant (t1) a
variantToTuple :: forall l t.
( VariantToHList l
, HTuple' (MapMaybe l) t
) => Variant l -> t
variantToTuple = hToTuple' . variantToHList
variantFromEither :: Either a b -> Variant '[b,a]
variantFromEither (Left a) = toVariantAt @1 a
variantFromEither (Right b) = toVariantAt @0 b
class ContVariant xs where
variantToCont :: Variant xs -> ContFlow xs r
variantToContM :: Monad m => m (Variant xs) -> ContFlow xs (m r)
contToVariant :: ContFlow xs (Variant xs) -> Variant xs
contToVariantM :: Monad m => ContFlow xs (m (Variant xs)) -> m (Variant xs)
instance ContVariant '[a] where
variantToCont (Variant _ a) = ContFlow $ \(Single f) ->
f (unsafeCoerce a)
variantToContM act = ContFlow $ \(Single f) -> do
Variant _ a <- act
f (unsafeCoerce a)
contToVariant c = c >::>
Single (toVariantAt @0)
contToVariantM c = c >::>
Single (return . toVariantAt @0)
instance ContVariant '[a,b] where
variantToCont (Variant t a) = ContFlow $ \(f1,f2) ->
case t of
0 -> f1 (unsafeCoerce a)
_ -> f2 (unsafeCoerce a)
variantToContM act = ContFlow $ \(f1,f2) -> do
Variant t a <- act
case t of
0 -> f1 (unsafeCoerce a)
_ -> f2 (unsafeCoerce a)
contToVariant c = c >::>
( toVariantAt @0
, toVariantAt @1
)
contToVariantM c = c >::>
( return . toVariantAt @0
, return . toVariantAt @1
)
instance ContVariant '[a,b,c] where
variantToCont (Variant t a) = ContFlow $ \(f1,f2,f3) ->
case t of
0 -> f1 (unsafeCoerce a)
1 -> f2 (unsafeCoerce a)
_ -> f3 (unsafeCoerce a)
variantToContM act = ContFlow $ \(f1,f2,f3) -> do
Variant t a <- act
case t of
0 -> f1 (unsafeCoerce a)
1 -> f2 (unsafeCoerce a)
_ -> f3 (unsafeCoerce a)
contToVariant c = c >::>
( toVariantAt @0
, toVariantAt @1
, toVariantAt @2
)
contToVariantM c = c >::>
( return . toVariantAt @0
, return . toVariantAt @1
, return . toVariantAt @2
)
instance ContVariant '[a,b,c,d] where
variantToCont (Variant t a) = ContFlow $ \(f1,f2,f3,f4) ->
case t of
0 -> f1 (unsafeCoerce a)
1 -> f2 (unsafeCoerce a)
2 -> f3 (unsafeCoerce a)
_ -> f4 (unsafeCoerce a)
variantToContM act = ContFlow $ \(f1,f2,f3,f4) -> do
Variant t a <- act
case t of
0 -> f1 (unsafeCoerce a)
1 -> f2 (unsafeCoerce a)
2 -> f3 (unsafeCoerce a)
_ -> f4 (unsafeCoerce a)
contToVariant c = c >::>
( toVariantAt @0
, toVariantAt @1
, toVariantAt @2
, toVariantAt @3
)
contToVariantM c = c >::>
( return . toVariantAt @0
, return . toVariantAt @1
, return . toVariantAt @2
, return . toVariantAt @3
)
instance ContVariant '[a,b,c,d,e] where
variantToCont (Variant t a) = ContFlow $ \(f1,f2,f3,f4,f5) ->
case t of
0 -> f1 (unsafeCoerce a)
1 -> f2 (unsafeCoerce a)
2 -> f3 (unsafeCoerce a)
3 -> f4 (unsafeCoerce a)
_ -> f5 (unsafeCoerce a)
variantToContM act = ContFlow $ \(f1,f2,f3,f4,f5) -> do
Variant t a <- act
case t of
0 -> f1 (unsafeCoerce a)
1 -> f2 (unsafeCoerce a)
2 -> f3 (unsafeCoerce a)
3 -> f4 (unsafeCoerce a)
_ -> f5 (unsafeCoerce a)
contToVariant c = c >::>
( toVariantAt @0
, toVariantAt @1
, toVariantAt @2
, toVariantAt @3
, toVariantAt @4
)
contToVariantM c = c >::>
( return . toVariantAt @0
, return . toVariantAt @1
, return . toVariantAt @2
, return . toVariantAt @3
, return . toVariantAt @4
)
instance ContVariant '[a,b,c,d,e,f] where
variantToCont (Variant t a) = ContFlow $ \(f1,f2,f3,f4,f5,f6) ->
case t of
0 -> f1 (unsafeCoerce a)
1 -> f2 (unsafeCoerce a)
2 -> f3 (unsafeCoerce a)
3 -> f4 (unsafeCoerce a)
4 -> f5 (unsafeCoerce a)
_ -> f6 (unsafeCoerce a)
variantToContM act = ContFlow $ \(f1,f2,f3,f4,f5,f6) -> do
Variant t a <- act
case t of
0 -> f1 (unsafeCoerce a)
1 -> f2 (unsafeCoerce a)
2 -> f3 (unsafeCoerce a)
3 -> f4 (unsafeCoerce a)
4 -> f5 (unsafeCoerce a)
_ -> f6 (unsafeCoerce a)
contToVariant c = c >::>
( toVariantAt @0
, toVariantAt @1
, toVariantAt @2
, toVariantAt @3
, toVariantAt @4
, toVariantAt @5
)
contToVariantM c = c >::>
( return . toVariantAt @0
, return . toVariantAt @1
, return . toVariantAt @2
, return . toVariantAt @3
, return . toVariantAt @4
, return . toVariantAt @5
)
instance ContVariant '[a,b,c,d,e,f,g] where
variantToCont (Variant t a) = ContFlow $ \(f1,f2,f3,f4,f5,f6,f7) ->
case t of
0 -> f1 (unsafeCoerce a)
1 -> f2 (unsafeCoerce a)
2 -> f3 (unsafeCoerce a)
3 -> f4 (unsafeCoerce a)
4 -> f5 (unsafeCoerce a)
5 -> f6 (unsafeCoerce a)
_ -> f7 (unsafeCoerce a)
variantToContM act = ContFlow $ \(f1,f2,f3,f4,f5,f6,f7) -> do
Variant t a <- act
case t of
0 -> f1 (unsafeCoerce a)
1 -> f2 (unsafeCoerce a)
2 -> f3 (unsafeCoerce a)
3 -> f4 (unsafeCoerce a)
4 -> f5 (unsafeCoerce a)
5 -> f6 (unsafeCoerce a)
_ -> f7 (unsafeCoerce a)
contToVariant c = c >::>
( toVariantAt @0
, toVariantAt @1
, toVariantAt @2
, toVariantAt @3
, toVariantAt @4
, toVariantAt @5
, toVariantAt @6
)
contToVariantM c = c >::>
( return . toVariantAt @0
, return . toVariantAt @1
, return . toVariantAt @2
, return . toVariantAt @3
, return . toVariantAt @4
, return . toVariantAt @5
, return . toVariantAt @6
)
instance ContVariant '[a,b,c,d,e,f,g,h] where
variantToCont (Variant t a) = ContFlow $ \(f1,f2,f3,f4,f5,f6,f7,f8) ->
case t of
0 -> f1 (unsafeCoerce a)
1 -> f2 (unsafeCoerce a)
2 -> f3 (unsafeCoerce a)
3 -> f4 (unsafeCoerce a)
4 -> f5 (unsafeCoerce a)
5 -> f6 (unsafeCoerce a)
6 -> f7 (unsafeCoerce a)
_ -> f8 (unsafeCoerce a)
variantToContM act = ContFlow $ \(f1,f2,f3,f4,f5,f6,f7,f8) -> do
Variant t a <- act
case t of
0 -> f1 (unsafeCoerce a)
1 -> f2 (unsafeCoerce a)
2 -> f3 (unsafeCoerce a)
3 -> f4 (unsafeCoerce a)
4 -> f5 (unsafeCoerce a)
5 -> f6 (unsafeCoerce a)
6 -> f7 (unsafeCoerce a)
_ -> f8 (unsafeCoerce a)
contToVariant c = c >::>
( toVariantAt @0
, toVariantAt @1
, toVariantAt @2
, toVariantAt @3
, toVariantAt @4
, toVariantAt @5
, toVariantAt @6
, toVariantAt @7
)
contToVariantM c = c >::>
( return . toVariantAt @0
, return . toVariantAt @1
, return . toVariantAt @2
, return . toVariantAt @3
, return . toVariantAt @4
, return . toVariantAt @5
, return . toVariantAt @6
, return . toVariantAt @7
)
instance ContVariant '[a,b,c,d,e,f,g,h,i] where
variantToCont (Variant t a) = ContFlow $ \(f1,f2,f3,f4,f5,f6,f7,f8,f9) ->
case t of
0 -> f1 (unsafeCoerce a)
1 -> f2 (unsafeCoerce a)
2 -> f3 (unsafeCoerce a)
3 -> f4 (unsafeCoerce a)
4 -> f5 (unsafeCoerce a)
5 -> f6 (unsafeCoerce a)
6 -> f7 (unsafeCoerce a)
7 -> f8 (unsafeCoerce a)
_ -> f9 (unsafeCoerce a)
variantToContM act = ContFlow $ \(f1,f2,f3,f4,f5,f6,f7,f8,f9) -> do
Variant t a <- act
case t of
0 -> f1 (unsafeCoerce a)
1 -> f2 (unsafeCoerce a)
2 -> f3 (unsafeCoerce a)
3 -> f4 (unsafeCoerce a)
4 -> f5 (unsafeCoerce a)
5 -> f6 (unsafeCoerce a)
6 -> f7 (unsafeCoerce a)
7 -> f8 (unsafeCoerce a)
_ -> f9 (unsafeCoerce a)
contToVariant c = c >::>
( toVariantAt @0
, toVariantAt @1
, toVariantAt @2
, toVariantAt @3
, toVariantAt @4
, toVariantAt @5
, toVariantAt @6
, toVariantAt @7
, toVariantAt @8
)
contToVariantM c = c >::>
( return . toVariantAt @0
, return . toVariantAt @1
, return . toVariantAt @2
, return . toVariantAt @3
, return . toVariantAt @4
, return . toVariantAt @5
, return . toVariantAt @6
, return . toVariantAt @7
, return . toVariantAt @8
)
instance ContVariant '[a,b,c,d,e,f,g,h,i,j] where
variantToCont (Variant t a) = ContFlow $ \(f1,f2,f3,f4,f5,f6,f7,f8,f9,f10) ->
case t of
0 -> f1 (unsafeCoerce a)
1 -> f2 (unsafeCoerce a)
2 -> f3 (unsafeCoerce a)
3 -> f4 (unsafeCoerce a)
4 -> f5 (unsafeCoerce a)
5 -> f6 (unsafeCoerce a)
6 -> f7 (unsafeCoerce a)
7 -> f8 (unsafeCoerce a)
8 -> f9 (unsafeCoerce a)
_ -> f10 (unsafeCoerce a)
variantToContM act = ContFlow $ \(f1,f2,f3,f4,f5,f6,f7,f8,f9,f10) -> do
Variant t a <- act
case t of
0 -> f1 (unsafeCoerce a)
1 -> f2 (unsafeCoerce a)
2 -> f3 (unsafeCoerce a)
3 -> f4 (unsafeCoerce a)
4 -> f5 (unsafeCoerce a)
5 -> f6 (unsafeCoerce a)
6 -> f7 (unsafeCoerce a)
7 -> f8 (unsafeCoerce a)
8 -> f9 (unsafeCoerce a)
_ -> f10 (unsafeCoerce a)
contToVariant c = c >::>
( toVariantAt @0
, toVariantAt @1
, toVariantAt @2
, toVariantAt @3
, toVariantAt @4
, toVariantAt @5
, toVariantAt @6
, toVariantAt @7
, toVariantAt @8
, toVariantAt @9
)
contToVariantM c = c >::>
( return . toVariantAt @0
, return . toVariantAt @1
, return . toVariantAt @2
, return . toVariantAt @3
, return . toVariantAt @4
, return . toVariantAt @5
, return . toVariantAt @6
, return . toVariantAt @7
, return . toVariantAt @8
, return . toVariantAt @9
)
instance ContVariant '[a,b,c,d,e,f,g,h,i,j,k] where
variantToCont (Variant t a) = ContFlow $ \(f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f11) ->
case t of
0 -> f1 (unsafeCoerce a)
1 -> f2 (unsafeCoerce a)
2 -> f3 (unsafeCoerce a)
3 -> f4 (unsafeCoerce a)
4 -> f5 (unsafeCoerce a)
5 -> f6 (unsafeCoerce a)
6 -> f7 (unsafeCoerce a)
7 -> f8 (unsafeCoerce a)
8 -> f9 (unsafeCoerce a)
9 -> f10 (unsafeCoerce a)
_ -> f11 (unsafeCoerce a)
variantToContM act = ContFlow $ \(f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f11) -> do
Variant t a <- act
case t of
0 -> f1 (unsafeCoerce a)
1 -> f2 (unsafeCoerce a)
2 -> f3 (unsafeCoerce a)
3 -> f4 (unsafeCoerce a)
4 -> f5 (unsafeCoerce a)
5 -> f6 (unsafeCoerce a)
6 -> f7 (unsafeCoerce a)
7 -> f8 (unsafeCoerce a)
8 -> f9 (unsafeCoerce a)
9 -> f10 (unsafeCoerce a)
_ -> f11 (unsafeCoerce a)
contToVariant c = c >::>
( toVariantAt @0
, toVariantAt @1
, toVariantAt @2
, toVariantAt @3
, toVariantAt @4
, toVariantAt @5
, toVariantAt @6
, toVariantAt @7
, toVariantAt @8
, toVariantAt @9
, toVariantAt @10
)
contToVariantM c = c >::>
( return . toVariantAt @0
, return . toVariantAt @1
, return . toVariantAt @2
, return . toVariantAt @3
, return . toVariantAt @4
, return . toVariantAt @5
, return . toVariantAt @6
, return . toVariantAt @7
, return . toVariantAt @8
, return . toVariantAt @9
, return . toVariantAt @10
)
instance ContVariant '[a,b,c,d,e,f,g,h,i,j,k,l] where
variantToCont (Variant t a) = ContFlow $ \(f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f11,f12) ->
case t of
0 -> f1 (unsafeCoerce a)
1 -> f2 (unsafeCoerce a)
2 -> f3 (unsafeCoerce a)
3 -> f4 (unsafeCoerce a)
4 -> f5 (unsafeCoerce a)
5 -> f6 (unsafeCoerce a)
6 -> f7 (unsafeCoerce a)
7 -> f8 (unsafeCoerce a)
8 -> f9 (unsafeCoerce a)
9 -> f10 (unsafeCoerce a)
10 -> f11 (unsafeCoerce a)
_ -> f12 (unsafeCoerce a)
variantToContM act = ContFlow $ \(f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f11,f12) -> do
Variant t a <- act
case t of
0 -> f1 (unsafeCoerce a)
1 -> f2 (unsafeCoerce a)
2 -> f3 (unsafeCoerce a)
3 -> f4 (unsafeCoerce a)
4 -> f5 (unsafeCoerce a)
5 -> f6 (unsafeCoerce a)
6 -> f7 (unsafeCoerce a)
7 -> f8 (unsafeCoerce a)
8 -> f9 (unsafeCoerce a)
9 -> f10 (unsafeCoerce a)
10 -> f11 (unsafeCoerce a)
_ -> f12 (unsafeCoerce a)
contToVariant c = c >::>
( toVariantAt @0
, toVariantAt @1
, toVariantAt @2
, toVariantAt @3
, toVariantAt @4
, toVariantAt @5
, toVariantAt @6
, toVariantAt @7
, toVariantAt @8
, toVariantAt @9
, toVariantAt @10
, toVariantAt @11
)
contToVariantM c = c >::>
( return . toVariantAt @0
, return . toVariantAt @1
, return . toVariantAt @2
, return . toVariantAt @3
, return . toVariantAt @4
, return . toVariantAt @5
, return . toVariantAt @6
, return . toVariantAt @7
, return . toVariantAt @8
, return . toVariantAt @9
, return . toVariantAt @10
, return . toVariantAt @11
)