1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
|
{-# LANGUAGE CPP #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE StandaloneDeriving #-}
module Foundation.Monad
( MonadIO(..)
, MonadFailure(..)
, MonadThrow(..)
, MonadCatch(..)
, MonadBracket(..)
, MonadTrans(..)
, Identity(..)
, replicateM
) where
import Basement.Imports
import Basement.Types.OffsetSize
import Basement.Monad (MonadFailure(..))
import Foundation.Monad.MonadIO
import Foundation.Monad.Exception
import Foundation.Monad.Transformer
import Foundation.Numerical
#if MIN_VERSION_base(4,8,0)
import Data.Functor.Identity
#else
import Control.Monad.Fix
import Control.Monad.Zip
import Basement.Compat.Base
import GHC.Generics (Generic1)
-- | Identity functor and monad. (a non-strict monad)
--
-- @since 4.8.0.0
newtype Identity a = Identity { runIdentity :: a }
deriving (Eq, Ord, Data, Generic, Generic1, Typeable)
instance Functor Identity where
fmap f (Identity x) = Identity (f x)
instance Applicative Identity where
pure = Identity
Identity f <*> Identity x = Identity (f x)
instance Monad Identity where
return = Identity
m >>= k = k (runIdentity m)
instance MonadFix Identity where
mfix f = Identity (fix (runIdentity . f))
instance MonadZip Identity where
mzipWith f (Identity x) (Identity y) = Identity (f x y)
munzip (Identity (x, y)) = (Identity x, Identity y)
#endif
-- | @'replicateM' n act@ performs the action @n@ times,
-- gathering the results.
replicateM :: Applicative m => CountOf a -> m a -> m [a]
replicateM (CountOf count) f = loop count
where
loop cnt
| cnt <= 0 = pure []
| otherwise = (:) <$> f <*> (loop (cnt - 1))
{-# INLINEABLE replicateM #-}
|