module Control.Monad.Constrained
(
Functor(..)
,Applicative(..)
,Monad(..)
,Alternative(..)
,Traversable(..)
,MonadFail(..)
,
ap
,
guard
,ensure
,(<**>)
,(<$>)
,(=<<)
,(<=<)
,(>=>)
,foldM
,traverse_
,sequenceA
,sequenceA_
,mapAccumL
,replicateM
,void
,forever
,for_
,join
,
ifThenElse
,(>>)
,return
,module RestPrelude)
where
import GHC.Exts
import Prelude as RestPrelude hiding (Applicative (..),
Functor (..),
Monad (..),
Traversable (..),
(<$>), (=<<))
import qualified Control.Applicative
import qualified Prelude
import Data.Functor.Identity (Identity (..))
import Data.IntMap.Strict (IntMap)
import Data.Map.Strict (Map)
import Data.Sequence (Seq)
import Data.Set (Set)
import qualified Data.Set as Set
import Data.Tree (Tree (..))
import Control.Monad.ST (ST)
import Control.Monad.Trans.Cont (ContT)
import Control.Monad.Trans.Except (ExceptT (..), runExceptT)
import Control.Monad.Trans.Identity (IdentityT (..))
import Control.Monad.Trans.Maybe (MaybeT (..))
import Control.Monad.Trans.Reader (ReaderT (..), mapReaderT)
import Control.Monad.Trans.State (StateT (..))
import qualified Control.Monad.Trans.State.Strict as Strict (StateT (..))
import Data.Functor.Compose (Compose (..))
import Data.Functor.Const (Const)
import Data.Functor.Product (Product (..))
import Data.Functor.Sum (Sum (..))
import Control.Arrow (first)
import Control.Monad.Trans.State.Strict (runState, state)
import Data.Tuple
import Control.Applicative.Free (Ap (Ap, Pure))
import qualified Control.Applicative.Free as Initial
import Control.Monad.Constrained.Internal.Unconstrained
class Functor f where
type Suitable f a :: Constraint
type Suitable f a = ()
fmap
:: (Suitable f b)
=> (a -> b) -> f a -> f b
infixl 4 <$
(<$) :: (Suitable f a) => a -> f b -> f a
(<$) = fmap . const
class (Prelude.Applicative (Unconstrained f), Functor f) =>
Applicative f where
type Unconstrained f :: * -> *
type Unconstrained f = f
reflect :: f a -> Unconstrained f a
reify
:: Suitable f a
=> Unconstrained f a -> f a
pure
:: Suitable f a
=> a -> f a
pure = reify . Prelude.pure
infixl 4 <*>
(<*>)
:: Suitable f b
=> f (a -> b) -> f a -> f b
(<*>) fs xs = reify (reflect fs Prelude.<*> reflect xs)
infixl 4 *>
(*>)
:: Suitable f b
=> f a -> f b -> f b
(*>) = liftA2 (const id)
infixl 4 <*
(<*)
:: Suitable f a
=> f a -> f b -> f a
(<*) = liftA2 const
liftA2
:: (Suitable f c)
=> (a -> b -> c) -> f a -> f b -> f c
liftA2 f xs ys = reify (Control.Applicative.liftA2 f (reflect xs) (reflect ys))
liftA3
:: (Suitable f d)
=> (a -> b -> c -> d) -> f a -> f b -> f c -> f d
liftA3 f xs ys zs =
reify (Control.Applicative.liftA3 f (reflect xs) (reflect ys) (reflect zs))
infixl 4 <**>
(<**>) :: (Applicative f, Suitable f b) => f a -> f (a -> b) -> f b
(<**>) = liftA2 (flip ($))
ap
:: (Monad f, Suitable f a)
=> (a -> f a) -> Initial.Ap f a -> f a
ap = flip runAp
where
runAp :: (Suitable f b, Monad f) => Ap f a -> (a -> f b) -> f b
runAp (Pure x) = \c -> c x
runAp (Ap xs fs) = \c -> xs >>= \x -> runAp fs (\g -> (c . g) x)
class Applicative f =>
Monad f where
infixl 1 >>=
(>>=)
:: Suitable f b
=> f a -> (a -> f b) -> f b
class Monad f => MonadFail f where
fail :: Suitable f a => String -> f a
class Applicative f =>
Alternative f where
empty :: Suitable f a => f a
infixl 3 <|>
(<|>)
:: Suitable f a
=> f a -> f a -> f a
some :: Suitable f [a] => f a -> f [a]
some v = some_v
where
many_v = some_v <|> pure []
some_v = liftA2 (:) v many_v
many :: Suitable f [a] => f a -> f [a]
many v = many_v
where
many_v = some_v <|> pure []
some_v = liftA2 (:) v many_v
class (Foldable t, Functor t) =>
Traversable t where
traverse
:: (Suitable t b, Applicative f, Suitable f (t b), Suitable f b)
=> (a -> f b) -> t a -> f (t b)
infixl 4 <$>
(<$>) :: (Functor f, Suitable f b) => (a -> b) -> f a -> f b
(<$>) = fmap
infixr 1 =<<, <=<
(=<<) :: (Monad f, Suitable f b) => (a -> f b) -> f a -> f b
(=<<) = flip (>>=)
(<=<) :: (Monad f, Suitable f c) => (b -> f c) -> (a -> f b) -> a -> f c
(f <=< g) x = f =<< g x
infixl 1 >=>
(>=>) :: (Monad f, Suitable f c) => (a -> f b) -> (b -> f c) -> a -> f c
(f >=> g) x = f x >>= g
forever :: (Applicative f, Suitable f b) => f a -> f b
forever a = let a' = a *> a' in a'
foldM
:: (Foldable t, Monad m, Suitable m b)
=> (b -> a -> m b) -> b -> t a -> m b
foldM f z0 xs = foldr f' pure xs z0
where f' x k z = f z x >>= k
for_
:: (Foldable t, Applicative f, Suitable f ())
=> t a -> (a -> f b) -> f ()
for_ = flip traverse_
traverse_
:: (Applicative f, Foldable t, Suitable f ())
=> (a -> f b) -> t a -> f ()
traverse_ f =
foldr (\e a -> f e *> a) (pure ())
sequenceA_ :: (Foldable t, Applicative f, Suitable f ()) => t (f a) -> f ()
sequenceA_ = foldr (*>) (pure ())
guard :: (Alternative f, Suitable f ()) => Bool -> f ()
guard True = pure ()
guard False = empty
ensure :: (Alternative f, Suitable f a) => Bool -> f a -> f a
ensure True x = x
ensure False _ = empty
sequenceA
:: (Applicative f
,Suitable t a
,Suitable f (t a)
,Traversable t
,Suitable f a)
=> t (f a) -> f (t a)
sequenceA = traverse id
mapAccumL
:: (Traversable t, Suitable t c)
=> (a -> b -> (a, c)) -> a -> t b -> (a, t c)
mapAccumL f s t = swap $ runState (traverse (state . (swap .: flip f)) t) s
where
(.:) = (.) . (.)
replicateM :: (Applicative m, Suitable m [a]) => Int -> m a -> m [a]
replicateM cnt0 f =
loop cnt0
where
loop cnt
| cnt <= 0 = pure []
| otherwise = liftA2 (:) f (loop (cnt 1))
void :: (Functor f, Suitable f ()) => f a -> f ()
void = (<$) ()
join :: (Monad f, Suitable f a) => f (f a) -> f a
join x = x >>= id
ifThenElse :: Bool -> a -> a -> a
ifThenElse True t _ = t
ifThenElse False _ f = f
infixl 1 >>
(>>)
:: (Applicative f, Suitable f b)
=> f a -> f b -> f b
(>>) = (*>)
return
:: (Applicative f, Suitable f a)
=> a -> f a
return = pure
instance Functor [] where
type Suitable [] a = ()
fmap = map
(<$) = (Prelude.<$)
instance Applicative [] where
type Unconstrained [] = []
reify = id
reflect = id
(<*>) = (Prelude.<*>)
(*>) = (Prelude.*>)
(<*) = (Prelude.<*)
pure = Prelude.pure
liftA2 = Control.Applicative.liftA2
liftA3 = Control.Applicative.liftA3
instance Alternative [] where
empty = []
(<|>) = (++)
instance Monad [] where
(>>=) = (Prelude.>>=)
instance MonadFail [] where
fail _ = []
instance Traversable [] where
traverse f = foldr (liftA2 (:) . f) (pure [])
instance Functor Maybe where
type Suitable Maybe a = ()
fmap = Prelude.fmap
(<$) = (Prelude.<$)
instance Applicative Maybe where
reify = id
reflect = id
(<*>) = (Prelude.<*>)
(*>) = (Prelude.*>)
(<*) = (Prelude.<*)
pure = Prelude.pure
liftA2 = Control.Applicative.liftA2
liftA3 = Control.Applicative.liftA3
instance Alternative Maybe where
empty = Control.Applicative.empty
(<|>) = (Control.Applicative.<|>)
instance Monad Maybe where
(>>=) = (Prelude.>>=)
instance MonadFail Maybe where
fail _ = Nothing
instance Traversable Maybe where
traverse _ Nothing = pure Nothing
traverse f (Just x) = fmap Just (f x)
instance Functor IO where
type Suitable IO a = ()
fmap = Prelude.fmap
(<$) = (Prelude.<$)
instance Applicative IO where
reify = id
reflect = id
(<*>) = (Prelude.<*>)
(*>) = (Prelude.*>)
(<*) = (Prelude.<*)
pure = Prelude.pure
liftA2 = Control.Applicative.liftA2
liftA3 = Control.Applicative.liftA3
instance Alternative IO where
empty = Control.Applicative.empty
(<|>) = (Control.Applicative.<|>)
instance Monad IO where
(>>=) = (Prelude.>>=)
instance MonadFail IO where
fail = Prelude.fail
instance Functor Identity where
type Suitable Identity a = ()
fmap = Prelude.fmap
(<$) = (Prelude.<$)
instance Applicative Identity where
reify = id
reflect = id
(<*>) = (Prelude.<*>)
(*>) = (Prelude.*>)
(<*) = (Prelude.<*)
pure = Prelude.pure
liftA2 = Control.Applicative.liftA2
liftA3 = Control.Applicative.liftA3
instance Monad Identity where
(>>=) = (Prelude.>>=)
instance Traversable Identity where
traverse f (Identity x) = fmap Identity (f x)
instance Functor (Either e) where
type Suitable (Either e) a = ()
fmap = Prelude.fmap
(<$) = (Prelude.<$)
instance Applicative (Either a) where
reify = id
reflect = id
(<*>) = (Prelude.<*>)
(*>) = (Prelude.*>)
(<*) = (Prelude.<*)
pure = Prelude.pure
liftA2 = Control.Applicative.liftA2
liftA3 = Control.Applicative.liftA3
instance Monad (Either a) where
(>>=) = (Prelude.>>=)
instance IsString a =>
MonadFail (Either a) where
fail = Left . fromString
instance Traversable (Either a) where
traverse f = either (pure . Left) (fmap Right . f)
instance Functor Set where
type Suitable Set a = Ord a
fmap = Set.map
x <$ xs = if null xs then Set.empty else Set.singleton x
instance Applicative Set where
type Unconstrained Set = StrictLeftFold
pure = Set.singleton
xs *> ys = if null xs then Set.empty else ys
xs <* ys = if null ys then Set.empty else xs
reify (StrictLeftFold xs) = xs (flip Set.insert) Set.empty
reflect xs = StrictLeftFold (\f b -> Set.foldl' f b xs)
instance Monad Set where
(>>=) = flip foldMap
instance MonadFail Set where
fail _ = Set.empty
instance Alternative Set where
empty = Set.empty
(<|>) = Set.union
instance Functor (Map a) where
type Suitable (Map a) b = ()
fmap = Prelude.fmap
(<$) = (Prelude.<$)
instance Functor ((,) a) where
type Suitable ((,) a) b = ()
fmap = Prelude.fmap
(<$) = (Prelude.<$)
instance Monoid a => Applicative ((,) a) where
reify = id
reflect = id
(<*>) = (Prelude.<*>)
(*>) = (Prelude.*>)
(<*) = (Prelude.<*)
pure = Prelude.pure
liftA2 = Control.Applicative.liftA2
liftA3 = Control.Applicative.liftA3
instance Monoid a => Monad ((,) a) where
(>>=) = (Prelude.>>=)
instance Traversable ((,) a) where
traverse f (x,y) = fmap ((,) x) (f y)
instance Functor IntMap where
type Suitable IntMap a = ()
fmap = Prelude.fmap
(<$) = (Prelude.<$)
instance Functor Seq where
type Suitable Seq a = ()
fmap = Prelude.fmap
(<$) = (Prelude.<$)
instance Applicative Seq where
reify = id
reflect = id
(<*>) = (Prelude.<*>)
(*>) = (Prelude.*>)
(<*) = (Prelude.<*)
pure = Prelude.pure
liftA2 = Control.Applicative.liftA2
liftA3 = Control.Applicative.liftA3
instance Alternative Seq where
empty = Control.Applicative.empty
(<|>) = (Control.Applicative.<|>)
instance Monad Seq where
(>>=) = (Prelude.>>=)
instance MonadFail Seq where
fail _ = empty
instance Functor Tree where
type Suitable Tree a = ()
fmap = Prelude.fmap
(<$) = (Prelude.<$)
instance Applicative Tree where
reify = id
reflect = id
(<*>) = (Prelude.<*>)
(*>) = (Prelude.*>)
(<*) = (Prelude.<*)
pure = Prelude.pure
liftA2 = Control.Applicative.liftA2
liftA3 = Control.Applicative.liftA3
instance Monad Tree where
(>>=) = (Prelude.>>=)
instance Traversable Tree where
traverse f (Node x ts) =
let g = (reflect . f)
in reify
(Node Prelude.<$> g x Prelude.<*>
Prelude.traverse (Prelude.traverse g) ts)
instance Functor ((->) a) where
type Suitable ((->) a) b = ()
fmap = Prelude.fmap
(<$) = (Prelude.<$)
instance Applicative ((->) a) where
reify = id
reflect = id
(<*>) = (Prelude.<*>)
(*>) = (Prelude.*>)
(<*) = (Prelude.<*)
pure = Prelude.pure
liftA2 = Control.Applicative.liftA2
liftA3 = Control.Applicative.liftA3
instance Monad ((->) a) where
(>>=) = (Prelude.>>=)
instance Functor (ContT r m) where
type Suitable (ContT r m) a = ()
fmap = Prelude.fmap
(<$) = (Prelude.<$)
instance Applicative (ContT r m) where
reify = id
reflect = id
(<*>) = (Prelude.<*>)
(*>) = (Prelude.*>)
(<*) = (Prelude.<*)
pure = Prelude.pure
liftA2 = Control.Applicative.liftA2
liftA3 = Control.Applicative.liftA3
instance Monad (ContT r m) where
(>>=) = (Prelude.>>=)
instance Functor Control.Applicative.ZipList where
type Suitable Control.Applicative.ZipList a = ()
fmap = Prelude.fmap
(<$) = (Prelude.<$)
instance Applicative Control.Applicative.ZipList where
reify = id
reflect = id
(<*>) = (Prelude.<*>)
(*>) = (Prelude.*>)
(<*) = (Prelude.<*)
pure = Prelude.pure
liftA2 = Control.Applicative.liftA2
liftA3 = Control.Applicative.liftA3
instance Functor m =>
Functor (Strict.StateT s m) where
type Suitable (Strict.StateT s m) a = Suitable m (a, s)
fmap f m =
Strict.StateT $
\s ->
(\(!a,!s') ->
(f a, s')) <$>
Strict.runStateT m s
x <$ xs = Strict.StateT ((fmap . first) (const x) . Strict.runStateT xs)
instance (Monad m, Prelude.Monad (Unconstrained m)) =>
Applicative (Strict.StateT s m) where
type Unconstrained (Strict.StateT s m)
= Strict.StateT s (Unconstrained m)
reflect (Strict.StateT xs) = Strict.StateT (reflect . xs)
pure a =
Strict.StateT $
\ !s ->
pure (a, s)
Strict.StateT mf <*> Strict.StateT mx =
Strict.StateT $
\ !s -> do
(f,!s') <- mf s
(x,!s'') <- mx s'
pure (f x, s'')
Strict.StateT xs *> Strict.StateT ys =
Strict.StateT $
\ !s -> do
(_,!s') <- xs s
ys s'
Strict.StateT xs <* Strict.StateT ys =
Strict.StateT $
\ !s -> do
(x,!s') <- xs s
(_,!s'') <- ys s'
pure (x, s'')
reify (Strict.StateT xs) = Strict.StateT (reify . xs)
instance (Monad m, Alternative m, Prelude.Monad (Unconstrained m)) =>
Alternative (Strict.StateT s m) where
empty = Strict.StateT (const empty)
Strict.StateT m <|> Strict.StateT n =
Strict.StateT $
\ !s ->
m s <|> n s
instance (Monad m, Prelude.Monad (Unconstrained m)) =>
Monad (Strict.StateT s m) where
m >>= k =
Strict.StateT $
\ !s -> do
(a, !s') <- Strict.runStateT m s
Strict.runStateT (k a) s'
instance Functor m => Functor (StateT s m) where
type Suitable (StateT s m) a = Suitable m (a, s)
fmap f m = StateT $ \ s ->
(\ ~(a, s') -> (f a, s')) <$> runStateT m s
x <$ StateT xs = StateT ((fmap.first) (const x) . xs)
instance (Monad m, Prelude.Monad (Unconstrained m)) =>
Applicative (StateT s m) where
type Unconstrained (StateT s m) = StateT s (Unconstrained m)
reflect (StateT xs) = StateT (reflect . xs)
pure a =
StateT $
\s ->
pure (a, s)
StateT mf <*> StateT mx =
StateT $
\s -> do
~(f,s') <- mf s
~(x,s'') <- mx s'
pure (f x, s'')
StateT xs *> StateT ys =
StateT $
\s -> do
~(_,s') <- xs s
ys s'
StateT xs <* StateT ys =
StateT $
\s -> do
~(x,s') <- xs s
~(_,s'') <- ys s'
pure (x,s'')
reify (StateT xs) = StateT (reify . xs)
instance (Monad m, Alternative m, Prelude.Monad (Unconstrained m)) =>
Alternative (StateT s m) where
empty = StateT (const empty)
StateT m <|> StateT n =
StateT $
\s ->
m s <|> n s
instance (Monad m, Prelude.Monad (Unconstrained m)) => Monad (StateT s m) where
m >>= k = StateT $ \ s -> do
~(a, s') <- runStateT m s
runStateT (k a) s'
instance (Functor m) => Functor (ReaderT r m) where
type Suitable (ReaderT r m) a = Suitable m a
fmap f = mapReaderT (fmap f)
x <$ ReaderT xs = ReaderT (\r -> x <$ xs r)
instance (Applicative m) => Applicative (ReaderT r m) where
type Unconstrained (ReaderT r m)
= ReaderT r (Unconstrained m)
pure = liftReaderT . pure
reflect (ReaderT f) = ReaderT (reflect . f)
f <*> v = ReaderT $ \ r -> runReaderT f r <*> runReaderT v r
reify ys = ReaderT (reify . runReaderT ys)
ReaderT xs *> ReaderT ys = ReaderT (\c -> xs c *> ys c)
ReaderT xs <* ReaderT ys = ReaderT (\c -> xs c <* ys c)
instance (Alternative m) => Alternative (ReaderT r m) where
empty = liftReaderT empty
m <|> n = ReaderT $ \ r -> runReaderT m r <|> runReaderT n r
instance MonadFail m =>
MonadFail (ReaderT r m) where
fail = ReaderT . const . fail
instance (Monad m) => Monad (ReaderT r m) where
m >>= k = ReaderT $ \ r -> do
a <- runReaderT m r
runReaderT (k a) r
liftReaderT :: m a -> ReaderT r m a
liftReaderT m = ReaderT (const m)
instance Functor m =>
Functor (MaybeT m) where
type Suitable (MaybeT m) a = (Suitable m (Maybe a), Suitable m a)
fmap f (MaybeT xs) = MaybeT ((fmap . fmap) f xs)
x <$ MaybeT xs = MaybeT (fmap (x <$) xs)
instance (Prelude.Monad (Unconstrained m), Monad m) =>
Applicative (MaybeT m) where
type Unconstrained (MaybeT m) = MaybeT (Unconstrained m)
reflect (MaybeT x) = MaybeT (reflect x)
pure x = MaybeT (pure (Just x))
MaybeT fs <*> MaybeT xs = MaybeT (liftA2 (<*>) fs xs)
reify (MaybeT x) = MaybeT (reify x)
MaybeT xs *> MaybeT ys = MaybeT (liftA2 (*>) xs ys)
MaybeT xs <* MaybeT ys = MaybeT (liftA2 (<*) xs ys)
instance (Monad m, Prelude.Monad (Unconstrained m)) =>
Monad (MaybeT m) where
MaybeT x >>= f = MaybeT (x >>= maybe (pure Nothing) (runMaybeT . f))
instance (Monad m, Prelude.Monad (Unconstrained m)) =>
MonadFail (MaybeT m) where
fail _ = empty
instance (Monad m, Prelude.Monad (Unconstrained m)) =>
Alternative (MaybeT m) where
empty = MaybeT (pure Nothing)
MaybeT x <|> MaybeT y = MaybeT (x >>= maybe y (pure . Just))
instance Functor m =>
Functor (ExceptT e m) where
type Suitable (ExceptT e m) a = Suitable m (Either e a)
fmap f (ExceptT xs) = ExceptT ((fmap . fmap) f xs)
x <$ ExceptT xs = ExceptT (fmap (x <$) xs)
instance (Monad m, Prelude.Monad (Unconstrained m)) =>
Applicative (ExceptT e m) where
type Unconstrained (ExceptT e m) = ExceptT e (Unconstrained m)
reflect (ExceptT x) = ExceptT (reflect x)
pure x = ExceptT (pure (Right x))
ExceptT fs <*> ExceptT xs = ExceptT (liftA2 (<*>) fs xs)
reify (ExceptT xs) = ExceptT (reify xs)
ExceptT xs *> ExceptT ys = ExceptT (xs *> ys)
ExceptT xs <* ExceptT ys = ExceptT (xs <* ys)
instance (Monad m, IsString e, Prelude.Monad (Unconstrained m)) =>
MonadFail (ExceptT e m) where
fail = ExceptT . pure . Left . fromString
instance (Monad m, Prelude.Monad (Unconstrained m)) =>
Monad (ExceptT e m) where
ExceptT xs >>= f = ExceptT (xs >>= either (pure . Left) (runExceptT . f))
instance (Monad m, Monoid e, Prelude.Monad (Unconstrained m)) =>
Alternative (ExceptT e m) where
empty = ExceptT (pure (Left mempty))
ExceptT xs <|> ExceptT ys =
ExceptT (xs >>= either (const ys) (pure . Right))
instance Functor m =>
Functor (IdentityT m) where
type Suitable (IdentityT m) a = Suitable m a
fmap =
(coerce :: ((a -> b) -> f a -> f b) -> (a -> b) -> IdentityT f a -> IdentityT f b)
fmap
(<$) =
(coerce :: (a -> f b -> f a) -> a -> IdentityT f b -> IdentityT f a)
(<$)
instance Applicative m =>
Applicative (IdentityT m) where
type Unconstrained (IdentityT m) = IdentityT (Unconstrained m)
reflect (IdentityT x) = IdentityT (reflect x)
pure = (coerce :: (a -> f a) -> a -> IdentityT f a) pure
(<*>) =
(coerce :: (f (a -> b) -> f a -> f b) -> IdentityT f (a -> b) -> IdentityT f a -> IdentityT f b)
(<*>)
reify =
(coerce :: (Unconstrained f b -> f b) -> (IdentityT (Unconstrained f) b -> IdentityT f b))
reify
IdentityT xs *> IdentityT ys = IdentityT (xs *> ys)
IdentityT xs <* IdentityT ys = IdentityT (xs <* ys)
instance Monad m =>
Monad (IdentityT m) where
(>>=) =
(coerce :: (f a -> (a -> f b) -> f b) -> IdentityT f a -> (a -> IdentityT f b) -> IdentityT f b)
(>>=)
instance MonadFail m =>
MonadFail (IdentityT m) where
fail = IdentityT . fail
instance Functor (ST s) where
type Suitable (ST s) a = ()
fmap = Prelude.fmap
(<$) = (Prelude.<$)
instance Applicative (ST s) where
reify = id
reflect = id
(<*>) = (Prelude.<*>)
(*>) = (Prelude.*>)
(<*) = (Prelude.<*)
pure = Prelude.pure
liftA2 = Control.Applicative.liftA2
liftA3 = Control.Applicative.liftA3
instance Monad (ST s) where
(>>=) = (Prelude.>>=)
instance Functor (Const a) where
type Suitable (Const a) b = ()
fmap = Prelude.fmap
(<$) = (Prelude.<$)
instance Monoid a => Applicative (Const a) where
reify = id
reflect = id
(<*>) = (Prelude.<*>)
(*>) = (Prelude.*>)
(<*) = (Prelude.<*)
pure = Prelude.pure
liftA2 = Control.Applicative.liftA2
liftA3 = Control.Applicative.liftA3
instance (Functor f, Functor g) =>
Functor (Compose f g) where
type Suitable (Compose f g) a = (Suitable g a, Suitable f (g a))
fmap f (Compose xs) = Compose ((fmap . fmap) f xs)
instance (Applicative f, Applicative g) =>
Applicative (Compose f g) where
type Unconstrained (Compose f g) =
Compose (Unconstrained f) (Unconstrained g)
reify (Compose xs) = Compose (reify (Prelude.fmap reify xs))
reflect (Compose xs) = Compose (Prelude.fmap reflect (reflect xs))
instance (Alternative f, Applicative g) => Alternative (Compose f g) where
empty = Compose empty
Compose x <|> Compose y = Compose (x <|> y)
instance (Functor f, Functor g) => Functor (Product f g) where
type Suitable (Product f g) a = (Suitable f a, Suitable g a)
fmap f (Pair x y) = Pair (fmap f x) (fmap f y)
instance (Applicative f, Applicative g) =>
Applicative (Product f g) where
type Unconstrained (Product f g) =
Product (Unconstrained f) (Unconstrained g)
pure x = Pair (pure x) (pure x)
Pair f g <*> Pair x y = Pair (f <*> x) (g <*> y)
reify (Pair xs ys) = Pair (reify xs) (reify ys)
reflect (Pair xs ys) = Pair (reflect xs) (reflect ys)
instance (Alternative f, Alternative g) => Alternative (Product f g) where
empty = Pair empty empty
Pair x1 y1 <|> Pair x2 y2 = Pair (x1 <|> x2) (y1 <|> y2)
instance (Monad f, Monad g) => Monad (Product f g) where
Pair m n >>= f = Pair (m >>= fstP . f) (n >>= sndP . f)
where
fstP (Pair a _) = a
sndP (Pair _ b) = b
instance (Functor f, Functor g) => Functor (Sum f g) where
type Suitable (Sum f g) a = (Suitable f a, Suitable g a)
fmap f (InL x) = InL (fmap f x)
fmap f (InR y) = InR (fmap f y)