{-# LANGUAGE DeriveTraversable #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
module Control.Effect.Sum
(
Member(..)
, Members
, (:+:)(..)
, reassociateSumL
) where
import Data.Kind (Constraint, Type)
data (f :+: g) (m :: Type -> Type) k
= L (f m k)
| R (g m k)
deriving ((:+:) f g m k -> (:+:) f g m k -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall (f :: (* -> *) -> * -> *) (g :: (* -> *) -> * -> *)
(m :: * -> *) k.
(Eq (f m k), Eq (g m k)) =>
(:+:) f g m k -> (:+:) f g m k -> Bool
/= :: (:+:) f g m k -> (:+:) f g m k -> Bool
$c/= :: forall (f :: (* -> *) -> * -> *) (g :: (* -> *) -> * -> *)
(m :: * -> *) k.
(Eq (f m k), Eq (g m k)) =>
(:+:) f g m k -> (:+:) f g m k -> Bool
== :: (:+:) f g m k -> (:+:) f g m k -> Bool
$c== :: forall (f :: (* -> *) -> * -> *) (g :: (* -> *) -> * -> *)
(m :: * -> *) k.
(Eq (f m k), Eq (g m k)) =>
(:+:) f g m k -> (:+:) f g m k -> Bool
Eq, forall a. (:+:) f g m a -> Bool
forall m a. Monoid m => (a -> m) -> (:+:) f g m a -> m
forall a b. (a -> b -> b) -> b -> (:+:) f g m a -> b
forall (t :: * -> *).
(forall m. Monoid m => t m -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. t a -> [a])
-> (forall a. t a -> Bool)
-> (forall a. t a -> Int)
-> (forall a. Eq a => a -> t a -> Bool)
-> (forall a. Ord a => t a -> a)
-> (forall a. Ord a => t a -> a)
-> (forall a. Num a => t a -> a)
-> (forall a. Num a => t a -> a)
-> Foldable t
forall (f :: (* -> *) -> * -> *) (g :: (* -> *) -> * -> *)
(m :: * -> *) a.
(Foldable (f m), Foldable (g m), Eq a) =>
a -> (:+:) f g m a -> Bool
forall (f :: (* -> *) -> * -> *) (g :: (* -> *) -> * -> *)
(m :: * -> *) a.
(Foldable (f m), Foldable (g m), Num a) =>
(:+:) f g m a -> a
forall (f :: (* -> *) -> * -> *) (g :: (* -> *) -> * -> *)
(m :: * -> *) a.
(Foldable (f m), Foldable (g m), Ord a) =>
(:+:) f g m a -> a
forall (f :: (* -> *) -> * -> *) (g :: (* -> *) -> * -> *)
(m :: * -> *) m.
(Foldable (f m), Foldable (g m), Monoid m) =>
(:+:) f g m m -> m
forall (f :: (* -> *) -> * -> *) (g :: (* -> *) -> * -> *)
(m :: * -> *) a.
(Foldable (f m), Foldable (g m)) =>
(:+:) f g m a -> Bool
forall (f :: (* -> *) -> * -> *) (g :: (* -> *) -> * -> *)
(m :: * -> *) a.
(Foldable (f m), Foldable (g m)) =>
(:+:) f g m a -> Int
forall (f :: (* -> *) -> * -> *) (g :: (* -> *) -> * -> *)
(m :: * -> *) a.
(Foldable (f m), Foldable (g m)) =>
(:+:) f g m a -> [a]
forall (f :: (* -> *) -> * -> *) (g :: (* -> *) -> * -> *)
(m :: * -> *) a.
(Foldable (f m), Foldable (g m)) =>
(a -> a -> a) -> (:+:) f g m a -> a
forall (f :: (* -> *) -> * -> *) (g :: (* -> *) -> * -> *)
(m :: * -> *) m a.
(Foldable (f m), Foldable (g m), Monoid m) =>
(a -> m) -> (:+:) f g m a -> m
forall (f :: (* -> *) -> * -> *) (g :: (* -> *) -> * -> *)
(m :: * -> *) b a.
(Foldable (f m), Foldable (g m)) =>
(b -> a -> b) -> b -> (:+:) f g m a -> b
forall (f :: (* -> *) -> * -> *) (g :: (* -> *) -> * -> *)
(m :: * -> *) a b.
(Foldable (f m), Foldable (g m)) =>
(a -> b -> b) -> b -> (:+:) f g m a -> b
product :: forall a. Num a => (:+:) f g m a -> a
$cproduct :: forall (f :: (* -> *) -> * -> *) (g :: (* -> *) -> * -> *)
(m :: * -> *) a.
(Foldable (f m), Foldable (g m), Num a) =>
(:+:) f g m a -> a
sum :: forall a. Num a => (:+:) f g m a -> a
$csum :: forall (f :: (* -> *) -> * -> *) (g :: (* -> *) -> * -> *)
(m :: * -> *) a.
(Foldable (f m), Foldable (g m), Num a) =>
(:+:) f g m a -> a
minimum :: forall a. Ord a => (:+:) f g m a -> a
$cminimum :: forall (f :: (* -> *) -> * -> *) (g :: (* -> *) -> * -> *)
(m :: * -> *) a.
(Foldable (f m), Foldable (g m), Ord a) =>
(:+:) f g m a -> a
maximum :: forall a. Ord a => (:+:) f g m a -> a
$cmaximum :: forall (f :: (* -> *) -> * -> *) (g :: (* -> *) -> * -> *)
(m :: * -> *) a.
(Foldable (f m), Foldable (g m), Ord a) =>
(:+:) f g m a -> a
elem :: forall a. Eq a => a -> (:+:) f g m a -> Bool
$celem :: forall (f :: (* -> *) -> * -> *) (g :: (* -> *) -> * -> *)
(m :: * -> *) a.
(Foldable (f m), Foldable (g m), Eq a) =>
a -> (:+:) f g m a -> Bool
length :: forall a. (:+:) f g m a -> Int
$clength :: forall (f :: (* -> *) -> * -> *) (g :: (* -> *) -> * -> *)
(m :: * -> *) a.
(Foldable (f m), Foldable (g m)) =>
(:+:) f g m a -> Int
null :: forall a. (:+:) f g m a -> Bool
$cnull :: forall (f :: (* -> *) -> * -> *) (g :: (* -> *) -> * -> *)
(m :: * -> *) a.
(Foldable (f m), Foldable (g m)) =>
(:+:) f g m a -> Bool
toList :: forall a. (:+:) f g m a -> [a]
$ctoList :: forall (f :: (* -> *) -> * -> *) (g :: (* -> *) -> * -> *)
(m :: * -> *) a.
(Foldable (f m), Foldable (g m)) =>
(:+:) f g m a -> [a]
foldl1 :: forall a. (a -> a -> a) -> (:+:) f g m a -> a
$cfoldl1 :: forall (f :: (* -> *) -> * -> *) (g :: (* -> *) -> * -> *)
(m :: * -> *) a.
(Foldable (f m), Foldable (g m)) =>
(a -> a -> a) -> (:+:) f g m a -> a
foldr1 :: forall a. (a -> a -> a) -> (:+:) f g m a -> a
$cfoldr1 :: forall (f :: (* -> *) -> * -> *) (g :: (* -> *) -> * -> *)
(m :: * -> *) a.
(Foldable (f m), Foldable (g m)) =>
(a -> a -> a) -> (:+:) f g m a -> a
foldl' :: forall b a. (b -> a -> b) -> b -> (:+:) f g m a -> b
$cfoldl' :: forall (f :: (* -> *) -> * -> *) (g :: (* -> *) -> * -> *)
(m :: * -> *) b a.
(Foldable (f m), Foldable (g m)) =>
(b -> a -> b) -> b -> (:+:) f g m a -> b
foldl :: forall b a. (b -> a -> b) -> b -> (:+:) f g m a -> b
$cfoldl :: forall (f :: (* -> *) -> * -> *) (g :: (* -> *) -> * -> *)
(m :: * -> *) b a.
(Foldable (f m), Foldable (g m)) =>
(b -> a -> b) -> b -> (:+:) f g m a -> b
foldr' :: forall a b. (a -> b -> b) -> b -> (:+:) f g m a -> b
$cfoldr' :: forall (f :: (* -> *) -> * -> *) (g :: (* -> *) -> * -> *)
(m :: * -> *) a b.
(Foldable (f m), Foldable (g m)) =>
(a -> b -> b) -> b -> (:+:) f g m a -> b
foldr :: forall a b. (a -> b -> b) -> b -> (:+:) f g m a -> b
$cfoldr :: forall (f :: (* -> *) -> * -> *) (g :: (* -> *) -> * -> *)
(m :: * -> *) a b.
(Foldable (f m), Foldable (g m)) =>
(a -> b -> b) -> b -> (:+:) f g m a -> b
foldMap' :: forall m a. Monoid m => (a -> m) -> (:+:) f g m a -> m
$cfoldMap' :: forall (f :: (* -> *) -> * -> *) (g :: (* -> *) -> * -> *)
(m :: * -> *) m a.
(Foldable (f m), Foldable (g m), Monoid m) =>
(a -> m) -> (:+:) f g m a -> m
foldMap :: forall m a. Monoid m => (a -> m) -> (:+:) f g m a -> m
$cfoldMap :: forall (f :: (* -> *) -> * -> *) (g :: (* -> *) -> * -> *)
(m :: * -> *) m a.
(Foldable (f m), Foldable (g m), Monoid m) =>
(a -> m) -> (:+:) f g m a -> m
fold :: forall m. Monoid m => (:+:) f g m m -> m
$cfold :: forall (f :: (* -> *) -> * -> *) (g :: (* -> *) -> * -> *)
(m :: * -> *) m.
(Foldable (f m), Foldable (g m), Monoid m) =>
(:+:) f g m m -> m
Foldable, forall a b. a -> (:+:) f g m b -> (:+:) f g m a
forall a b. (a -> b) -> (:+:) f g m a -> (:+:) f g m b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
forall (f :: (* -> *) -> * -> *) (g :: (* -> *) -> * -> *)
(m :: * -> *) a b.
(Functor (f m), Functor (g m)) =>
a -> (:+:) f g m b -> (:+:) f g m a
forall (f :: (* -> *) -> * -> *) (g :: (* -> *) -> * -> *)
(m :: * -> *) a b.
(Functor (f m), Functor (g m)) =>
(a -> b) -> (:+:) f g m a -> (:+:) f g m b
<$ :: forall a b. a -> (:+:) f g m b -> (:+:) f g m a
$c<$ :: forall (f :: (* -> *) -> * -> *) (g :: (* -> *) -> * -> *)
(m :: * -> *) a b.
(Functor (f m), Functor (g m)) =>
a -> (:+:) f g m b -> (:+:) f g m a
fmap :: forall a b. (a -> b) -> (:+:) f g m a -> (:+:) f g m b
$cfmap :: forall (f :: (* -> *) -> * -> *) (g :: (* -> *) -> * -> *)
(m :: * -> *) a b.
(Functor (f m), Functor (g m)) =>
(a -> b) -> (:+:) f g m a -> (:+:) f g m b
Functor, (:+:) f g m k -> (:+:) f g m k -> Bool
(:+:) f g m k -> (:+:) f g m k -> Ordering
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall {f :: (* -> *) -> * -> *} {g :: (* -> *) -> * -> *}
{m :: * -> *} {k}.
(Ord (f m k), Ord (g m k)) =>
Eq ((:+:) f g m k)
forall (f :: (* -> *) -> * -> *) (g :: (* -> *) -> * -> *)
(m :: * -> *) k.
(Ord (f m k), Ord (g m k)) =>
(:+:) f g m k -> (:+:) f g m k -> Bool
forall (f :: (* -> *) -> * -> *) (g :: (* -> *) -> * -> *)
(m :: * -> *) k.
(Ord (f m k), Ord (g m k)) =>
(:+:) f g m k -> (:+:) f g m k -> Ordering
forall (f :: (* -> *) -> * -> *) (g :: (* -> *) -> * -> *)
(m :: * -> *) k.
(Ord (f m k), Ord (g m k)) =>
(:+:) f g m k -> (:+:) f g m k -> (:+:) f g m k
min :: (:+:) f g m k -> (:+:) f g m k -> (:+:) f g m k
$cmin :: forall (f :: (* -> *) -> * -> *) (g :: (* -> *) -> * -> *)
(m :: * -> *) k.
(Ord (f m k), Ord (g m k)) =>
(:+:) f g m k -> (:+:) f g m k -> (:+:) f g m k
max :: (:+:) f g m k -> (:+:) f g m k -> (:+:) f g m k
$cmax :: forall (f :: (* -> *) -> * -> *) (g :: (* -> *) -> * -> *)
(m :: * -> *) k.
(Ord (f m k), Ord (g m k)) =>
(:+:) f g m k -> (:+:) f g m k -> (:+:) f g m k
>= :: (:+:) f g m k -> (:+:) f g m k -> Bool
$c>= :: forall (f :: (* -> *) -> * -> *) (g :: (* -> *) -> * -> *)
(m :: * -> *) k.
(Ord (f m k), Ord (g m k)) =>
(:+:) f g m k -> (:+:) f g m k -> Bool
> :: (:+:) f g m k -> (:+:) f g m k -> Bool
$c> :: forall (f :: (* -> *) -> * -> *) (g :: (* -> *) -> * -> *)
(m :: * -> *) k.
(Ord (f m k), Ord (g m k)) =>
(:+:) f g m k -> (:+:) f g m k -> Bool
<= :: (:+:) f g m k -> (:+:) f g m k -> Bool
$c<= :: forall (f :: (* -> *) -> * -> *) (g :: (* -> *) -> * -> *)
(m :: * -> *) k.
(Ord (f m k), Ord (g m k)) =>
(:+:) f g m k -> (:+:) f g m k -> Bool
< :: (:+:) f g m k -> (:+:) f g m k -> Bool
$c< :: forall (f :: (* -> *) -> * -> *) (g :: (* -> *) -> * -> *)
(m :: * -> *) k.
(Ord (f m k), Ord (g m k)) =>
(:+:) f g m k -> (:+:) f g m k -> Bool
compare :: (:+:) f g m k -> (:+:) f g m k -> Ordering
$ccompare :: forall (f :: (* -> *) -> * -> *) (g :: (* -> *) -> * -> *)
(m :: * -> *) k.
(Ord (f m k), Ord (g m k)) =>
(:+:) f g m k -> (:+:) f g m k -> Ordering
Ord, Int -> (:+:) f g m k -> ShowS
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall (f :: (* -> *) -> * -> *) (g :: (* -> *) -> * -> *)
(m :: * -> *) k.
(Show (f m k), Show (g m k)) =>
Int -> (:+:) f g m k -> ShowS
forall (f :: (* -> *) -> * -> *) (g :: (* -> *) -> * -> *)
(m :: * -> *) k.
(Show (f m k), Show (g m k)) =>
[(:+:) f g m k] -> ShowS
forall (f :: (* -> *) -> * -> *) (g :: (* -> *) -> * -> *)
(m :: * -> *) k.
(Show (f m k), Show (g m k)) =>
(:+:) f g m k -> String
showList :: [(:+:) f g m k] -> ShowS
$cshowList :: forall (f :: (* -> *) -> * -> *) (g :: (* -> *) -> * -> *)
(m :: * -> *) k.
(Show (f m k), Show (g m k)) =>
[(:+:) f g m k] -> ShowS
show :: (:+:) f g m k -> String
$cshow :: forall (f :: (* -> *) -> * -> *) (g :: (* -> *) -> * -> *)
(m :: * -> *) k.
(Show (f m k), Show (g m k)) =>
(:+:) f g m k -> String
showsPrec :: Int -> (:+:) f g m k -> ShowS
$cshowsPrec :: forall (f :: (* -> *) -> * -> *) (g :: (* -> *) -> * -> *)
(m :: * -> *) k.
(Show (f m k), Show (g m k)) =>
Int -> (:+:) f g m k -> ShowS
Show, forall (t :: * -> *).
Functor t
-> Foldable t
-> (forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> t a -> f (t b))
-> (forall (f :: * -> *) a. Applicative f => t (f a) -> f (t a))
-> (forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> t a -> m (t b))
-> (forall (m :: * -> *) a. Monad m => t (m a) -> m (t a))
-> Traversable t
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> (:+:) f g m a -> f ((:+:) f g m b)
forall {f :: (* -> *) -> * -> *} {g :: (* -> *) -> * -> *}
{m :: * -> *}.
(Traversable (f m), Traversable (g m)) =>
Functor ((:+:) f g m)
forall {f :: (* -> *) -> * -> *} {g :: (* -> *) -> * -> *}
{m :: * -> *}.
(Traversable (f m), Traversable (g m)) =>
Foldable ((:+:) f g m)
forall (f :: (* -> *) -> * -> *) (g :: (* -> *) -> * -> *)
(m :: * -> *) (m :: * -> *) a.
(Traversable (f m), Traversable (g m), Monad m) =>
(:+:) f g m (m a) -> m ((:+:) f g m a)
forall (f :: (* -> *) -> * -> *) (g :: (* -> *) -> * -> *)
(m :: * -> *) (f :: * -> *) a.
(Traversable (f m), Traversable (g m), Applicative f) =>
(:+:) f g m (f a) -> f ((:+:) f g m a)
forall (f :: (* -> *) -> * -> *) (g :: (* -> *) -> * -> *)
(m :: * -> *) (m :: * -> *) a b.
(Traversable (f m), Traversable (g m), Monad m) =>
(a -> m b) -> (:+:) f g m a -> m ((:+:) f g m b)
forall (f :: (* -> *) -> * -> *) (g :: (* -> *) -> * -> *)
(m :: * -> *) (f :: * -> *) a b.
(Traversable (f m), Traversable (g m), Applicative f) =>
(a -> f b) -> (:+:) f g m a -> f ((:+:) f g m b)
sequence :: forall (m :: * -> *) a.
Monad m =>
(:+:) f g m (m a) -> m ((:+:) f g m a)
$csequence :: forall (f :: (* -> *) -> * -> *) (g :: (* -> *) -> * -> *)
(m :: * -> *) (m :: * -> *) a.
(Traversable (f m), Traversable (g m), Monad m) =>
(:+:) f g m (m a) -> m ((:+:) f g m a)
mapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> (:+:) f g m a -> m ((:+:) f g m b)
$cmapM :: forall (f :: (* -> *) -> * -> *) (g :: (* -> *) -> * -> *)
(m :: * -> *) (m :: * -> *) a b.
(Traversable (f m), Traversable (g m), Monad m) =>
(a -> m b) -> (:+:) f g m a -> m ((:+:) f g m b)
sequenceA :: forall (f :: * -> *) a.
Applicative f =>
(:+:) f g m (f a) -> f ((:+:) f g m a)
$csequenceA :: forall (f :: (* -> *) -> * -> *) (g :: (* -> *) -> * -> *)
(m :: * -> *) (f :: * -> *) a.
(Traversable (f m), Traversable (g m), Applicative f) =>
(:+:) f g m (f a) -> f ((:+:) f g m a)
traverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> (:+:) f g m a -> f ((:+:) f g m b)
$ctraverse :: forall (f :: (* -> *) -> * -> *) (g :: (* -> *) -> * -> *)
(m :: * -> *) (f :: * -> *) a b.
(Traversable (f m), Traversable (g m), Applicative f) =>
(a -> f b) -> (:+:) f g m a -> f ((:+:) f g m b)
Traversable)
infixr 4 :+:
class Member (sub :: (Type -> Type) -> (Type -> Type)) sup where
inj :: sub m a -> sup m a
instance Member t t where
inj :: forall (m :: * -> *) a. t m a -> t m a
inj = forall a. a -> a
id
{-# INLINE inj #-}
instance {-# OVERLAPPABLE #-}
Member t (l1 :+: l2 :+: r)
=> Member t ((l1 :+: l2) :+: r) where
inj :: forall (m :: * -> *) a. t m a -> (:+:) (l1 :+: l2) r m a
inj = forall (l1 :: (* -> *) -> * -> *) (l2 :: (* -> *) -> * -> *)
(r :: (* -> *) -> * -> *) (m :: * -> *) a.
(:+:) l1 (l2 :+: r) m a -> (:+:) (l1 :+: l2) r m a
reassociateSumL forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (sub :: (* -> *) -> * -> *) (sup :: (* -> *) -> * -> *)
(m :: * -> *) a.
Member sub sup =>
sub m a -> sup m a
inj
{-# INLINE inj #-}
instance {-# OVERLAPPABLE #-}
Member l (l :+: r) where
inj :: forall (m :: * -> *) a. l m a -> (:+:) l r m a
inj = forall (l :: (* -> *) -> * -> *) (r :: (* -> *) -> * -> *)
(m :: * -> *) a.
l m a -> (:+:) l r m a
L
{-# INLINE inj #-}
instance {-# OVERLAPPABLE #-}
Member l r
=> Member l (l' :+: r) where
inj :: forall (m :: * -> *) a. l m a -> (:+:) l' r m a
inj = forall (f :: (* -> *) -> * -> *) (g :: (* -> *) -> * -> *)
(m :: * -> *) k.
g m k -> (:+:) f g m k
R forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (sub :: (* -> *) -> * -> *) (sup :: (* -> *) -> * -> *)
(m :: * -> *) a.
Member sub sup =>
sub m a -> sup m a
inj
{-# INLINE inj #-}
reassociateSumL :: (l1 :+: l2 :+: r) m a -> ((l1 :+: l2) :+: r) m a
reassociateSumL :: forall (l1 :: (* -> *) -> * -> *) (l2 :: (* -> *) -> * -> *)
(r :: (* -> *) -> * -> *) (m :: * -> *) a.
(:+:) l1 (l2 :+: r) m a -> (:+:) (l1 :+: l2) r m a
reassociateSumL = \case
L l1 m a
l -> forall (l :: (* -> *) -> * -> *) (r :: (* -> *) -> * -> *)
(m :: * -> *) a.
l m a -> (:+:) l r m a
L (forall (l :: (* -> *) -> * -> *) (r :: (* -> *) -> * -> *)
(m :: * -> *) a.
l m a -> (:+:) l r m a
L l1 m a
l)
R (L l2 m a
l) -> forall (l :: (* -> *) -> * -> *) (r :: (* -> *) -> * -> *)
(m :: * -> *) a.
l m a -> (:+:) l r m a
L (forall (f :: (* -> *) -> * -> *) (g :: (* -> *) -> * -> *)
(m :: * -> *) k.
g m k -> (:+:) f g m k
R l2 m a
l)
R (R r m a
r) -> forall (f :: (* -> *) -> * -> *) (g :: (* -> *) -> * -> *)
(m :: * -> *) k.
g m k -> (:+:) f g m k
R r m a
r
{-# INLINE reassociateSumL #-}
type family Members sub sup :: Constraint where
Members (l :+: r) u = (Members l u, Members r u)
Members t u = Member t u