module Control.Monad.Constrained
(
Functor(..)
,Applicative(..)
,Monad(..)
,Alternative(..)
,Traversable(..)
,
AppVect(..)
,FunType
,liftAP
,liftAM
,
guard
,ensure
,(<**>)
,(<$>)
,(=<<)
,(<=<)
,(>=>)
,foldM
,traverse_
,sequenceA
,sequenceA_
,mapAccumL
,replicateM
,void
,forever
,for_
,
ifThenElse
,fail
,(>>)
,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.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 Control.Arrow (first)
import Data.Tuple
import Control.Monad.Trans.State.Strict (state, runState)
infixl 5 :>
data AppVect f xs where
Nil :: AppVect f '[]
(:>) :: AppVect f xs -> f x -> AppVect f (x ': xs)
type family FunType (xs :: [*]) (y :: *) :: * where
FunType '[] y = y
FunType (x ': xs) y = FunType xs (x -> y)
class Functor f where
type Suitable f a :: Constraint
fmap
:: Suitable f b
=> (a -> b) -> f a -> f b
infixl 4 <$
(<$) :: Suitable f a => a -> f b -> f a
(<$) = fmap . const
class Functor f =>
Applicative f where
pure
:: Suitable f a
=> a -> f a
pure x = liftA x Nil
infixl 4 <*>
(<*>)
:: Suitable f b
=> f (a -> b) -> f a -> f b
fs <*> xs = liftA ($) (Nil :> fs :> 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
liftA
:: Suitable f a
=> FunType xs a -> AppVect f xs -> f a
liftA2
:: Suitable f c
=> (a -> b -> c) -> f a -> f b -> f c
liftA2 f xs ys =
liftA f (Nil :> xs :> ys)
liftA3
:: Suitable f d
=> (a -> b -> c -> d) -> f a -> f b -> f c -> f d
liftA3 f xs ys zs =
liftA f (Nil :> xs :> ys :> zs)
(<**>) :: (Applicative f, Suitable f b) => f a -> f (a -> b) -> f b
(<**>) = liftA2 (flip ($))
liftAM :: (Monad f, Suitable f a) => FunType xs a -> AppVect f xs -> f a
liftAM = go pure where
go :: (Suitable f b, Monad f) => (a -> f b) -> FunType xs a -> AppVect f xs -> f b
go f g Nil = f g
go f g (xs :> x) = go (\c -> x >>= f . c) g xs
liftAP :: Prelude.Applicative f => FunType xs a -> AppVect f xs -> f a
liftAP f Nil = Prelude.pure f
liftAP f (Nil :> xs) = Prelude.fmap f xs
liftAP f (ys :> xs) = liftAP f ys Prelude.<*> xs
liftA2P
:: (Prelude.Applicative f)
=> (a -> b -> c) -> f a -> f b -> f c
liftA2P f x y = f Prelude.<$> x Prelude.<*> y
liftA3P
:: Prelude.Applicative f
=> (a -> b -> c -> d) -> f a -> f b -> f c -> f d
liftA3P f xs ys zs = f Prelude.<$> xs Prelude.<*> ys Prelude.<*> zs
class Applicative f =>
Monad f where
infixl 1 >>=
(>>=)
:: Suitable f b
=> f a -> (a -> f b) -> f b
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))
=> (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)
=> 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 = (<$) ()
ifThenElse :: Bool -> a -> a -> a
ifThenElse True t _ = t
ifThenElse False _ f = f
fail :: String -> a
fail = error
(>>)
:: (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
liftA = liftAP
(<*>) = (Prelude.<*>)
(*>) = (Prelude.*>)
(<*) = (Prelude.<*)
pure = Prelude.pure
liftA2 = liftA2P
liftA3 = liftA3P
instance Alternative [] where
empty = []
(<|>) = (++)
instance Monad [] where
(>>=) = (Prelude.>>=)
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
liftA = liftAP
(<*>) = (Prelude.<*>)
(*>) = (Prelude.*>)
(<*) = (Prelude.<*)
pure = Prelude.pure
liftA2 = liftA2P
liftA3 = liftA3P
instance Alternative Maybe where
empty = Control.Applicative.empty
(<|>) = (Control.Applicative.<|>)
instance Monad Maybe where
(>>=) = (Prelude.>>=)
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
liftA = liftAP
(<*>) = (Prelude.<*>)
(*>) = (Prelude.*>)
(<*) = (Prelude.<*)
pure = Prelude.pure
liftA2 = liftA2P
liftA3 = liftA3P
instance Alternative IO where
empty = Control.Applicative.empty
(<|>) = (Control.Applicative.<|>)
instance Monad IO where
(>>=) = (Prelude.>>=)
instance Functor Identity where
type Suitable Identity a = ()
fmap = Prelude.fmap
(<$) = (Prelude.<$)
instance Applicative Identity where
liftA = liftAP
(<*>) = (Prelude.<*>)
(*>) = (Prelude.*>)
(<*) = (Prelude.<*)
pure = Prelude.pure
liftA2 = liftA2P
liftA3 = liftA3P
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
liftA = liftAP
(<*>) = (Prelude.<*>)
(*>) = (Prelude.*>)
(<*) = (Prelude.<*)
pure = Prelude.pure
liftA2 = liftA2P
liftA3 = liftA3P
instance Monad (Either a) where
(>>=) = (Prelude.>>=)
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
pure = Set.singleton
fs <*> xs = foldMap (`Set.map` xs) fs
xs *> ys = if null xs then Set.empty else ys
xs <* ys = if null ys then Set.empty else xs
liftA = liftAM
instance Monad Set where
(>>=) = flip foldMap
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
liftA = liftAP
(<*>) = (Prelude.<*>)
(*>) = (Prelude.*>)
(<*) = (Prelude.<*)
pure = Prelude.pure
liftA2 = liftA2P
liftA3 = liftA3P
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
liftA = liftAP
(<*>) = (Prelude.<*>)
(*>) = (Prelude.*>)
(<*) = (Prelude.<*)
pure = Prelude.pure
liftA2 = liftA2P
liftA3 = liftA3P
instance Alternative Seq where
empty = Control.Applicative.empty
(<|>) = (Control.Applicative.<|>)
instance Monad Seq where
(>>=) = (Prelude.>>=)
instance Functor Tree where
type Suitable Tree a = ()
fmap = Prelude.fmap
(<$) = (Prelude.<$)
instance Applicative Tree where
liftA = liftAP
(<*>) = (Prelude.<*>)
(*>) = (Prelude.*>)
(<*) = (Prelude.<*)
pure = Prelude.pure
liftA2 = liftA2P
liftA3 = liftA3P
instance Monad Tree where
(>>=) = (Prelude.>>=)
instance Functor ((->) a) where
type Suitable ((->) a) b = ()
fmap = Prelude.fmap
(<$) = (Prelude.<$)
instance Applicative ((->) a) where
liftA = liftAP
(<*>) = (Prelude.<*>)
(*>) = (Prelude.*>)
(<*) = (Prelude.<*)
pure = Prelude.pure
liftA2 = liftA2P
liftA3 = liftA3P
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
liftA = liftAP
(<*>) = (Prelude.<*>)
(*>) = (Prelude.*>)
(<*) = (Prelude.<*)
pure = Prelude.pure
liftA2 = liftA2P
liftA3 = liftA3P
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
liftA = liftAP
(<*>) = (Prelude.<*>)
(*>) = (Prelude.*>)
(<*) = (Prelude.<*)
pure = Prelude.pure
liftA2 = liftA2P
liftA3 = liftA3P
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 =>
Applicative (Strict.StateT s m) where
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'')
liftA = liftAM
instance (Monad m, Alternative 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) => 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) =>
Applicative (StateT s m) where
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'')
liftA = liftAM
instance (Monad m, Alternative m) => Alternative (StateT s m) where
empty = StateT (const empty)
StateT m <|> StateT n = StateT $ \ s -> m s <|> n s
instance (Monad 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
pure = liftReaderT . pure
f <*> v = ReaderT $ \ r -> runReaderT f r <*> runReaderT v r
liftA f ys = ReaderT $ \r -> liftA f (tr r ys) where
tr :: r -> AppVect (ReaderT r m) xs -> AppVect m xs
tr _ Nil = Nil
tr r (Nil :> xs) = Nil :> runReaderT xs r
tr r (xs :> x) = tr r xs :> runReaderT x r
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 (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 Monad m => Applicative (MaybeT m) where
pure x = MaybeT (pure (Just x))
MaybeT fs <*> MaybeT xs = MaybeT (liftA2 (<*>) fs xs)
liftA = liftAM
MaybeT xs *> MaybeT ys = MaybeT (liftA2 (*>) xs ys)
MaybeT xs <* MaybeT ys = MaybeT (liftA2 (<*) xs ys)
instance Monad m => Monad (MaybeT m) where
MaybeT x >>= f = MaybeT (x >>= maybe (pure Nothing) (runMaybeT . f))
instance Monad 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 =>
Applicative (ExceptT e m) where
pure x = ExceptT (pure (Right x))
ExceptT fs <*> ExceptT xs = ExceptT (liftA2 (<*>) fs xs)
liftA = liftAM
ExceptT xs *> ExceptT ys = ExceptT (xs *> ys)
ExceptT xs <* ExceptT ys = ExceptT (xs <* ys)
instance Monad m => Monad (ExceptT e m) where
ExceptT xs >>= f = ExceptT (xs >>= either (pure . Left) (runExceptT . f))
instance (Monad m, Monoid e) => 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
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)
(<*>)
liftA f =
(coerce :: (AppVect f xs -> f b) -> (AppVect (IdentityT f) xs -> IdentityT f b))
(liftA f)
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)
(>>=)