--------------------------------------------------------------------------------
-- |
-- Module : Database.EventStore.Internal.Prelude
-- Copyright : (C) 2017 Yorick Laupa
-- License : (see the file LICENSE)
--
-- Maintainer : Yorick Laupa <yo.eight@gmail.com>
-- Stability : provisional
-- Portability : non-portable
--
--------------------------------------------------------------------------------
module Database.EventStore.Internal.Prelude
  ( IsString(..)
  , Semigroup(..)
  , MonadIO(..)
  , Hashable(..)
  , Monoid(..)
  , Down(..)
  , HashMap
  , Seq
  , Set
  , ByteString
  , Text
  , UUID
  , Generic
  , Alternative(..)
  , MonadBaseControl(..)
  , atomically
  , zip
  , zipWith
  , tshow
  , length
  , lift
  , retrySTM
  , fromMaybe
  , unlessM
  , whenM
  , isJust
  , null
  , newUUID
  , module Prelude
  , module Control.Applicative
  , module Data.Int
  , module Data.Foldable
  , module Data.Traversable
  , module Control.Concurrent.Lifted
  , module Control.Concurrent.Async.Lifted
  , module Control.Concurrent.MVar.Lifted
  , module Control.Concurrent.STM
  , module Control.Concurrent.STM.TBMQueue
  , module Control.Exception.Safe
  , module Control.Monad
  , module Control.Monad.Base
  , module Control.Monad.Catch
  , module Control.Monad.Trans.Control
  , module Data.Containers
  , module Data.Functor
  , module Data.IORef.Lifted
  , module Data.Sequences
  , module Data.Time
  , module Data.Typeable
  , module Data.Word
  ) where

--------------------------------------------------------------------------------
import Prelude
  ( IO
  , FilePath
  , Num(..)
  , Show(..)
  , Eq(..)
  , Ord(..)
  , Enum(..)
  , Bounded(..)
  , Either(..)
  , Maybe(..)
  , Bool(..)
  , Integral(..)
  , Float
  , Fractional(..)
  , Ordering(..)
  , Double
  , Integer
  , (.)
  , id
  , ($)
  , otherwise
  , not
  , fromIntegral
  , truncate
  , print
  , realToFrac
  , (||)
  , (&&)
  , error
  , undefined
  , toRational
  , maybe
  , either
  , const
  , flip
  )
import Control.Monad
  ( Monad(..)
  , MonadPlus(..)
  , (=<<)
  , (<=<)
  , foldM
  , foldM_
  , when
  , unless
  , forever
  , ap
  )
import Control.Applicative (Applicative(..), Alternative(..))
import Data.Int
import Data.List (zip, zipWith)
import Data.Maybe (fromMaybe, isJust)
import Data.String (IsString(..))
import Data.Foldable
  ( Foldable
  , foldMap
  , traverse_
  , for_
  , toList
  , forM_
  , foldl'
  )
import Data.Traversable
import Data.Functor
  ( Functor(..)
  , (<$)
  , (<$>)
  )
import Data.Monoid(Monoid(..))
import Data.Ord (Down(..))
import Data.Typeable
import Data.Word
import GHC.Generics (Generic)

--------------------------------------------------------------------------------
import           Control.Concurrent.Lifted hiding (throwTo, yield)
import           Control.Concurrent.Async.Lifted
import           Control.Concurrent.MVar.Lifted
import           Control.Concurrent.STM hiding (atomically, retry, check)
import qualified Control.Concurrent.STM as STM
import           Control.Concurrent.STM.TBMQueue
import           Control.Monad.Catch (MonadCatch(..), MonadThrow(..))
import           Control.Monad.Trans.Control hiding (embed, embed_)
import           Control.Exception.Safe hiding (handle, throwM, catch)
import           Control.Monad.Base
import           Control.Monad.Trans
import           Data.ByteString (ByteString)
import           Data.Containers
import           Data.IORef.Lifted
import           Data.Hashable (Hashable(..))
import           Data.HashMap.Strict (HashMap)
import           Data.MonoTraversable.Unprefixed (length, null)
import           Data.Semigroup
import           Data.Sequences hiding (group)
import           Data.Sequence (Seq)
import           Data.Set (Set)
import           Data.Text (Text)
import           Data.Time
import           Data.UUID (UUID)
import           Data.UUID.V4 (nextRandom)

--------------------------------------------------------------------------------
-- | Generalized version of 'STM.atomically'.
atomically :: MonadIO m => STM a -> m a
atomically :: STM a -> m a
atomically = IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO a -> m a) -> (STM a -> IO a) -> STM a -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. STM a -> IO a
forall a. STM a -> IO a
STM.atomically

--------------------------------------------------------------------------------
tshow :: Show a => a -> Text
tshow :: a -> Text
tshow = [Char] -> Text
forall seq. IsSequence seq => [Element seq] -> seq
pack ([Char] -> Text) -> (a -> [Char]) -> a -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> [Char]
forall a. Show a => a -> [Char]
show

--------------------------------------------------------------------------------
retrySTM :: STM a
retrySTM :: STM a
retrySTM = STM a
forall a. STM a
STM.retry

--------------------------------------------------------------------------------
-- | Only perform the action if the predicate returns 'True'.
whenM :: Monad m => m Bool -> m () -> m ()
whenM :: m Bool -> m () -> m ()
whenM m Bool
mbool m ()
action = m Bool
mbool m Bool -> (Bool -> m ()) -> m ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (Bool -> m () -> m ()) -> m () -> Bool -> m ()
forall a b c. (a -> b -> c) -> b -> a -> c
flip Bool -> m () -> m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when m ()
action

--------------------------------------------------------------------------------
-- | Only perform the action if the predicate returns 'False'.
unlessM :: Monad m => m Bool -> m () -> m ()
unlessM :: m Bool -> m () -> m ()
unlessM m Bool
mbool m ()
action = m Bool
mbool m Bool -> (Bool -> m ()) -> m ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (Bool -> m () -> m ()) -> m () -> Bool -> m ()
forall a b c. (a -> b -> c) -> b -> a -> c
flip Bool -> m () -> m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless m ()
action

--------------------------------------------------------------------------------
newUUID :: MonadBase IO m => m UUID
newUUID :: m UUID
newUUID = IO UUID -> m UUID
forall (b :: * -> *) (m :: * -> *) α. MonadBase b m => b α -> m α
liftBase IO UUID
nextRandom