{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE Trustworthy #-}
module Grisette.Lib.Data.Foldable
(
mrgFoldlM,
mrgFoldrM,
mrgTraverse_,
mrgFor_,
mrgMapM_,
mrgForM_,
mrgSequence_,
mrgMsum,
)
where
import Control.Monad
import Grisette.Core.Control.Monad.Union
import Grisette.Core.Data.Class.Bool
import Grisette.Core.Data.Class.Mergeable
import Grisette.Core.Data.Class.SimpleMergeable
import {-# SOURCE #-} Grisette.Lib.Control.Monad
mrgFoldlM :: (MonadUnion m, Mergeable b, Foldable t) => (b -> a -> m b) -> b -> t a -> m b
mrgFoldlM :: forall (m :: * -> *) b (t :: * -> *) a.
(MonadUnion m, Mergeable b, Foldable t) =>
(b -> a -> m b) -> b -> t a -> m b
mrgFoldlM b -> a -> m b
f b
z0 t a
xs = forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr forall {b}. a -> (b -> m b) -> b -> m b
c forall (u :: * -> *) a. (MonadUnion u, Mergeable a) => a -> u a
mrgReturn t a
xs b
z0
where
c :: a -> (b -> m b) -> b -> m b
c a
x b -> m b
k b
z = forall (u :: * -> *) a. (UnionLike u, Mergeable a) => u a -> u a
merge (b -> a -> m b
f b
z a
x) forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= b -> m b
k
{-# INLINE mrgFoldlM #-}
mrgFoldrM :: (MonadUnion m, Mergeable b, Foldable t) => (a -> b -> m b) -> b -> t a -> m b
mrgFoldrM :: forall (m :: * -> *) b (t :: * -> *) a.
(MonadUnion m, Mergeable b, Foldable t) =>
(a -> b -> m b) -> b -> t a -> m b
mrgFoldrM a -> b -> m b
f b
z0 t a
xs = forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl forall {b}. (b -> m b) -> a -> b -> m b
c forall (u :: * -> *) a. (MonadUnion u, Mergeable a) => a -> u a
mrgReturn t a
xs b
z0
where
c :: (b -> m b) -> a -> b -> m b
c b -> m b
k a
x b
z = forall (u :: * -> *) a. (UnionLike u, Mergeable a) => u a -> u a
merge (a -> b -> m b
f a
x b
z) forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= b -> m b
k
{-# INLINE mrgFoldrM #-}
mrgTraverse_ :: (MonadUnion m, Foldable t) => (a -> m b) -> t a -> m ()
mrgTraverse_ :: forall (m :: * -> *) (t :: * -> *) a b.
(MonadUnion m, Foldable t) =>
(a -> m b) -> t a -> m ()
mrgTraverse_ a -> m b
f = forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr forall {b}. a -> m b -> m b
c (forall (u :: * -> *) a. (MonadUnion u, Mergeable a) => a -> u a
mrgReturn ())
where
c :: a -> m b -> m b
c a
x m b
k = a -> m b
f a
x forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> m b
k
{-# INLINE mrgTraverse_ #-}
mrgFor_ :: (MonadUnion m, Foldable t) => t a -> (a -> m b) -> m ()
mrgFor_ :: forall (m :: * -> *) (t :: * -> *) a b.
(MonadUnion m, Foldable t) =>
t a -> (a -> m b) -> m ()
mrgFor_ = forall a b c. (a -> b -> c) -> b -> a -> c
flip forall (m :: * -> *) (t :: * -> *) a b.
(MonadUnion m, Foldable t) =>
(a -> m b) -> t a -> m ()
mrgTraverse_
{-# INLINE mrgFor_ #-}
mrgMapM_ :: (MonadUnion m, Foldable t) => (a -> m b) -> t a -> m ()
mrgMapM_ :: forall (m :: * -> *) (t :: * -> *) a b.
(MonadUnion m, Foldable t) =>
(a -> m b) -> t a -> m ()
mrgMapM_ = forall (m :: * -> *) (t :: * -> *) a b.
(MonadUnion m, Foldable t) =>
(a -> m b) -> t a -> m ()
mrgTraverse_
{-# INLINE mrgMapM_ #-}
mrgForM_ :: (MonadUnion m, Foldable t) => t a -> (a -> m b) -> m ()
mrgForM_ :: forall (m :: * -> *) (t :: * -> *) a b.
(MonadUnion m, Foldable t) =>
t a -> (a -> m b) -> m ()
mrgForM_ = forall a b c. (a -> b -> c) -> b -> a -> c
flip forall (m :: * -> *) (t :: * -> *) a b.
(MonadUnion m, Foldable t) =>
(a -> m b) -> t a -> m ()
mrgMapM_
{-# INLINE mrgForM_ #-}
mrgSequence_ :: (Foldable t, MonadUnion m) => t (m a) -> m ()
mrgSequence_ :: forall (t :: * -> *) (m :: * -> *) a.
(Foldable t, MonadUnion m) =>
t (m a) -> m ()
mrgSequence_ = forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr forall {m :: * -> *} {a} {b}. Monad m => m a -> m b -> m b
c (forall (u :: * -> *) a. (MonadUnion u, Mergeable a) => a -> u a
mrgReturn ())
where
c :: m a -> m b -> m b
c m a
m m b
k = m a
m forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> m b
k
{-# INLINE mrgSequence_ #-}
mrgMsum :: forall m a t. (MonadUnion m, Mergeable a, MonadPlus m, Foldable t) => t (m a) -> m a
mrgMsum :: forall (m :: * -> *) a (t :: * -> *).
(MonadUnion m, Mergeable a, MonadPlus m, Foldable t) =>
t (m a) -> m a
mrgMsum = forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr forall (m :: * -> *) a.
(MonadUnion m, Mergeable a, MonadPlus m) =>
m a -> m a -> m a
mrgMplus forall (m :: * -> *) a.
(MonadUnion m, Mergeable a, MonadPlus m) =>
m a
mrgMzero
{-# INLINE mrgMsum #-}