{-# LANGUAGE CPP #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE TypeFamilies #-}
module ClassyPrelude
    ( -- * CorePrelude
      module CorePrelude
    , undefined
      -- * Standard
      -- ** Monoid
    , (++)
      -- ** Semigroup
    , Semigroup (..)
    , WrappedMonoid
      -- ** Functor
    , module Data.Functor
      -- ** Applicative
    , module Control.Applicative
    , (<&&>)
    , (<||>)
      -- ** Monad
    , module Control.Monad
    , whenM
    , unlessM
      -- ** UnliftIO reexports
    , module UnliftIO
      -- ** Mutable references
    , orElseSTM
    , module Data.Mutable
      -- ** STM Channels
    , module Control.Concurrent.STM.TBChan
    , module Control.Concurrent.STM.TBMChan
    , module Control.Concurrent.STM.TBMQueue
    , module Control.Concurrent.STM.TMChan
    , module Control.Concurrent.STM.TMQueue
      -- ** Primitive (exported since 0.9.4)
    , primToPrim
    , primToIO
    , primToST
    , module Data.Primitive.MutVar
      -- ** Debugging
    , trace
    , traceShow
    , traceId
    , traceM
    , traceShowId
    , traceShowM
      -- ** Time (since 0.6.1)
    , module Data.Time
#if MIN_VERSION_time(1,10,0)
    , parseTime
#endif
      -- ** Generics (since 0.8.1)
    , Generic
      -- ** Transformers (since 0.9.4)
    , Identity (..)
    , MonadReader
    , ask
    , asks
    , ReaderT (..)
    , Reader
      -- * Poly hierarchy
    , module Data.Foldable
    , module Data.Traversable
      -- ** Bifunctor (since 0.10.0)
    , module Data.Bifunctor
      -- * Mono hierarchy
    , module Data.MonoTraversable
    , module Data.MonoTraversable.Unprefixed
    , module Data.Sequences
    , module Data.Containers
    , module Data.Builder
    , module Data.NonNull
    , toByteVector
    , fromByteVector
      -- * I\/O
    , module Say
      -- * Concurrency
    , yieldThread
    , waitAsync
    , pollAsync
    , waitCatchAsync
    , linkAsync
    , link2Async
      -- * Non-standard
      -- ** List-like classes
    , map
    --, split
    , readMay
    , zip, zip3, zip4, zip5, zip6, zip7
    , unzip, unzip3, unzip4, unzip5, unzip6, unzip7
    , zipWith, zipWith3, zipWith4, zipWith5, zipWith6, zipWith7

    , hashNub
    , ordNub
    , ordNubBy

    , sortWith
    , Prelude.repeat
      -- ** Set-like
    , (\\)
    , intersect
    -- FIXME , mapSet
      -- ** Text-like
    , Show (..)
    , tshow
    , tlshow
      -- *** Case conversion
    , charToLower
    , charToUpper
      -- ** IO
    , readFile
    , readFileUtf8
    , writeFile
    , writeFileUtf8
    , hGetContents
    , hPut
    , hGetChunk
    , print
    -- Prelude IO operations
    , putChar
    , putStr
    , putStrLn
    , getChar
    , getLine
    , getContents
    , interact
      -- ** Difference lists
    , DList
    , asDList
    , applyDList
      -- ** Exceptions
    , module Control.DeepSeq
      -- ** Force types
      -- | Helper functions for situations where type inferer gets confused.
    , asByteString
    , asLByteString
    , asHashMap
    , asHashSet
    , asText
    , asLText
    , asList
    , asMap
    , asIntMap
    , asMaybe
    , asSet
    , asIntSet
    , asVector
    , asUVector
    , asSVector
    , asString
    ) where

import qualified Prelude
import Control.Applicative ((<**>),liftA,liftA2,liftA3,Alternative (..), optional)
import Data.Functor
import Control.Exception (assert)
import Control.DeepSeq (deepseq, ($!!), force, NFData (..))
import Control.Monad (when, unless, void, liftM, ap, forever, join, replicateM_, guard, MonadPlus (..), (=<<), (>=>), (<=<), liftM2, liftM3, liftM4, liftM5)
import qualified Control.Concurrent.STM as STM
import Data.Mutable
import Data.Traversable (Traversable (..), for, forM)
import Data.Foldable (Foldable)
import UnliftIO

import Data.Vector.Instances ()
import CorePrelude hiding
    ( putStr, putStrLn, print, undefined, (<>), catMaybes, first, second
    , catchIOError
    )
import Data.ChunkedZip
import qualified Data.Char as Char
import Data.Sequences
import Data.MonoTraversable
import Data.MonoTraversable.Unprefixed
import Data.MonoTraversable.Instances ()
import Data.Containers
import Data.Builder
import Data.NonNull
import qualified Data.ByteString
import qualified Data.Text.IO as TextIO
import qualified Data.Text.Lazy.IO as LTextIO
import Data.ByteString.Internal (ByteString (PS))
import Data.ByteString.Lazy.Internal (defaultChunkSize)
import Data.Vector.Storable (unsafeToForeignPtr, unsafeFromForeignPtr)

import qualified Debug.Trace as Trace
import Data.Semigroup (Semigroup (..), WrappedMonoid (..))
import Prelude (Show (..))
import Data.Time
    ( UTCTime (..)
    , Day (..)
    , toGregorian
    , fromGregorian
    , formatTime
#if !MIN_VERSION_time(1,10,0)
    , parseTime
#endif
    , parseTimeM
    , getCurrentTime
    , defaultTimeLocale
    )
import qualified Data.Time as Time

import qualified Data.Set as Set
import qualified Data.Map as Map
import qualified Data.HashSet as HashSet

import GHC.Generics (Generic)
import GHC.Stack (HasCallStack)

import Control.Monad.Primitive (primToPrim, primToIO, primToST)
import Data.Primitive.MutVar

import Data.Functor.Identity (Identity (..))
import Control.Monad.Reader (MonadReader, ask, asks, ReaderT (..), Reader)
import Data.Bifunctor
import Data.DList (DList)
import qualified Data.DList as DList
import Say
import Control.Concurrent.STM.TBChan
import Control.Concurrent.STM.TBMChan
import Control.Concurrent.STM.TBMQueue
import Control.Concurrent.STM.TMChan
import Control.Concurrent.STM.TMQueue
import qualified Control.Concurrent

tshow :: Show a => a -> Text
tshow :: a -> Text
tshow = [Char] -> Text
forall seq. IsSequence seq => [Element seq] -> seq
fromList ([Char] -> Text) -> (a -> [Char]) -> a -> Text
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. a -> [Char]
forall a. Show a => a -> [Char]
Prelude.show

tlshow :: Show a => a -> LText
tlshow :: a -> LText
tlshow = [Char] -> LText
forall seq. IsSequence seq => [Element seq] -> seq
fromList ([Char] -> LText) -> (a -> [Char]) -> a -> LText
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. a -> [Char]
forall a. Show a => a -> [Char]
Prelude.show

-- | Convert a character to lower case.
--
-- Character-based case conversion is lossy in comparison to string-based 'Data.MonoTraversable.toLower'.
-- For instance, &#x130; will be converted to i, instead of i&#x307;.
charToLower :: Char -> Char
charToLower :: Char -> Char
charToLower = Char -> Char
Char.toLower

-- | Convert a character to upper case.
--
-- Character-based case conversion is lossy in comparison to string-based 'Data.MonoTraversable.toUpper'.
-- For instance, &#xdf; won't be converted to SS.
charToUpper :: Char -> Char
charToUpper :: Char -> Char
charToUpper = Char -> Char
Char.toUpper

readMay :: (Element c ~ Char, MonoFoldable c, Read a) => c -> Maybe a
readMay :: c -> Maybe a
readMay c
a = -- FIXME replace with safe-failure stuff
    case [a
x | (a
x, [Char]
t) <- ReadS a
forall a. Read a => ReadS a
Prelude.reads (c -> [Element c]
forall mono. MonoFoldable mono => mono -> [Element mono]
otoList c
a :: String), [Char] -> Bool
forall mono. MonoFoldable mono => mono -> Bool
onull [Char]
t] of
        [a
x] -> a -> Maybe a
forall a. a -> Maybe a
Just a
x
        [a]
_ -> Maybe a
forall a. Maybe a
Nothing

map :: Functor f => (a -> b) -> f a -> f b
map :: (a -> b) -> f a -> f b
map = (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap

infixr 5  ++
(++) :: Monoid m => m -> m -> m
++ :: m -> m -> m
(++) = m -> m -> m
forall a. Monoid a => a -> a -> a
mappend
{-# INLINE (++) #-}

infixl 9 \\{-This comment teaches CPP correct behaviour -}
-- | An alias for 'difference'.
(\\) :: SetContainer a => a -> a -> a
\\ :: a -> a -> a
(\\) = a -> a -> a
forall set. SetContainer set => set -> set -> set
difference
{-# INLINE (\\) #-}

-- | An alias for 'intersection'.
intersect :: SetContainer a => a -> a -> a
intersect :: a -> a -> a
intersect = a -> a -> a
forall set. SetContainer set => set -> set -> set
intersection
{-# INLINE intersect #-}

asByteString :: ByteString -> ByteString
asByteString :: ByteString -> ByteString
asByteString = ByteString -> ByteString
forall k (cat :: k -> k -> *) (a :: k). Category cat => cat a a
id

asLByteString :: LByteString -> LByteString
asLByteString :: LByteString -> LByteString
asLByteString = LByteString -> LByteString
forall k (cat :: k -> k -> *) (a :: k). Category cat => cat a a
id

asHashMap :: HashMap k v -> HashMap k v
asHashMap :: HashMap k v -> HashMap k v
asHashMap = HashMap k v -> HashMap k v
forall k (cat :: k -> k -> *) (a :: k). Category cat => cat a a
id

asHashSet :: HashSet a -> HashSet a
asHashSet :: HashSet a -> HashSet a
asHashSet = HashSet a -> HashSet a
forall k (cat :: k -> k -> *) (a :: k). Category cat => cat a a
id

asText :: Text -> Text
asText :: Text -> Text
asText = Text -> Text
forall k (cat :: k -> k -> *) (a :: k). Category cat => cat a a
id

asLText :: LText -> LText
asLText :: LText -> LText
asLText = LText -> LText
forall k (cat :: k -> k -> *) (a :: k). Category cat => cat a a
id

asList :: [a] -> [a]
asList :: [a] -> [a]
asList = [a] -> [a]
forall k (cat :: k -> k -> *) (a :: k). Category cat => cat a a
id

asMap :: Map k v -> Map k v
asMap :: Map k v -> Map k v
asMap = Map k v -> Map k v
forall k (cat :: k -> k -> *) (a :: k). Category cat => cat a a
id

asIntMap :: IntMap v -> IntMap v
asIntMap :: IntMap v -> IntMap v
asIntMap = IntMap v -> IntMap v
forall k (cat :: k -> k -> *) (a :: k). Category cat => cat a a
id

asMaybe :: Maybe a -> Maybe a
asMaybe :: Maybe a -> Maybe a
asMaybe = Maybe a -> Maybe a
forall k (cat :: k -> k -> *) (a :: k). Category cat => cat a a
id

asSet :: Set a -> Set a
asSet :: Set a -> Set a
asSet = Set a -> Set a
forall k (cat :: k -> k -> *) (a :: k). Category cat => cat a a
id

asIntSet :: IntSet -> IntSet
asIntSet :: IntSet -> IntSet
asIntSet = IntSet -> IntSet
forall k (cat :: k -> k -> *) (a :: k). Category cat => cat a a
id

asVector :: Vector a -> Vector a
asVector :: Vector a -> Vector a
asVector = Vector a -> Vector a
forall k (cat :: k -> k -> *) (a :: k). Category cat => cat a a
id

asUVector :: UVector a -> UVector a
asUVector :: UVector a -> UVector a
asUVector = UVector a -> UVector a
forall k (cat :: k -> k -> *) (a :: k). Category cat => cat a a
id

asSVector :: SVector a -> SVector a
asSVector :: SVector a -> SVector a
asSVector = SVector a -> SVector a
forall k (cat :: k -> k -> *) (a :: k). Category cat => cat a a
id

asString :: [Char] -> [Char]
asString :: [Char] -> [Char]
asString = [Char] -> [Char]
forall k (cat :: k -> k -> *) (a :: k). Category cat => cat a a
id

print :: (Show a, MonadIO m) => a -> m ()
print :: a -> m ()
print = IO () -> m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ()) -> (a -> IO ()) -> a -> m ()
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. a -> IO ()
forall a. Show a => a -> IO ()
Prelude.print

-- | Sort elements using the user supplied function to project something out of
-- each element.
-- Inspired by <http://hackage.haskell.org/packages/archive/base/latest/doc/html/GHC-Exts.html#v:sortWith>.
sortWith :: (Ord a, IsSequence c) => (Element c -> a) -> c -> c
sortWith :: (Element c -> a) -> c -> c
sortWith Element c -> a
f = (Element c -> Element c -> Ordering) -> c -> c
forall seq.
SemiSequence seq =>
(Element seq -> Element seq -> Ordering) -> seq -> seq
sortBy ((Element c -> Element c -> Ordering) -> c -> c)
-> (Element c -> Element c -> Ordering) -> c -> c
forall a b. (a -> b) -> a -> b
$ (Element c -> a) -> Element c -> Element c -> Ordering
forall a b. Ord a => (b -> a) -> b -> b -> Ordering
comparing Element c -> a
f

-- | We define our own 'undefined' which is marked as deprecated. This makes it
-- useful to use during development, but lets you more easily get
-- notifications if you accidentally ship partial code in production.
--
-- The classy prelude recommendation for when you need to really have a partial
-- function in production is to use 'error' with a very descriptive message so
-- that, in case an exception is thrown, you get more information than
-- @"Prelude".'Prelude.undefined'@.
--
-- Since 0.5.5
undefined :: HasCallStack => a
undefined :: a
undefined = [Char] -> a
forall a. HasCallStack => [Char] -> a
error [Char]
"ClassyPrelude.undefined"
{-# DEPRECATED undefined "It is highly recommended that you either avoid partial functions or provide meaningful error messages" #-}

-- | We define our own 'trace' (and also its variants) which provides a warning
-- when used. So that tracing is available during development, but the compiler
-- reminds you to not leave them in the code for production.
{-# WARNING trace "Leaving traces in the code" #-}
trace :: String -> a -> a
trace :: [Char] -> a -> a
trace = [Char] -> a -> a
forall a. [Char] -> a -> a
Trace.trace

{-# WARNING traceShow "Leaving traces in the code" #-}
traceShow :: Show a => a -> b -> b
traceShow :: a -> b -> b
traceShow = a -> b -> b
forall a b. Show a => a -> b -> b
Trace.traceShow

-- |
--
-- Since 0.5.9
{-# WARNING traceId "Leaving traces in the code" #-}
traceId :: String -> String
traceId :: [Char] -> [Char]
traceId [Char]
a = [Char] -> [Char] -> [Char]
forall a. [Char] -> a -> a
Trace.trace [Char]
a [Char]
a

-- |
--
-- Since 0.5.9
{-# WARNING traceM "Leaving traces in the code" #-}
traceM :: (Monad m) => String -> m ()
traceM :: [Char] -> m ()
traceM [Char]
string = [Char] -> m () -> m ()
forall a. [Char] -> a -> a
Trace.trace [Char]
string (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$ () -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()

-- |
--
-- Since 0.5.9
{-# WARNING traceShowId "Leaving traces in the code" #-}
traceShowId :: (Show a) => a -> a
traceShowId :: a -> a
traceShowId a
a = [Char] -> a -> a
forall a. [Char] -> a -> a
Trace.trace (a -> [Char]
forall a. Show a => a -> [Char]
show a
a) a
a

-- |
--
-- Since 0.5.9
{-# WARNING traceShowM "Leaving traces in the code" #-}
traceShowM :: (Show a, Monad m) => a -> m ()
traceShowM :: a -> m ()
traceShowM = [Char] -> m ()
forall (m :: * -> *). Monad m => [Char] -> m ()
traceM ([Char] -> m ()) -> (a -> [Char]) -> a -> m ()
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. a -> [Char]
forall a. Show a => a -> [Char]
show

-- | Originally 'Conc.yield'.
yieldThread :: MonadIO m => m ()
yieldThread :: m ()
yieldThread = IO () -> m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO IO ()
Control.Concurrent.yield
{-# INLINE yieldThread #-}

-- Below is a lot of coding for classy-prelude!
-- These functions are restricted to lists right now.
-- Should eventually exist in mono-foldable and be extended to MonoFoldable
-- when doing that should re-run the haskell-ordnub benchmarks

-- | same behavior as 'Data.List.nub', but requires 'Hashable' & 'Eq' and is @O(n log n)@
--
-- <https://github.com/nh2/haskell-ordnub>
hashNub :: (Hashable a, Eq a) => [a] -> [a]
hashNub :: [a] -> [a]
hashNub = HashSet a -> [a] -> [a]
forall a. Hashable a => HashSet a -> [a] -> [a]
go HashSet a
forall a. HashSet a
HashSet.empty
  where
    go :: HashSet a -> [a] -> [a]
go HashSet a
_ []     = []
    go HashSet a
s (a
x:[a]
xs) | a
x a -> HashSet a -> Bool
forall a. (Eq a, Hashable a) => a -> HashSet a -> Bool
`HashSet.member` HashSet a
s = HashSet a -> [a] -> [a]
go HashSet a
s [a]
xs
                | Bool
otherwise            = a
x a -> [a] -> [a]
forall a. a -> [a] -> [a]
: HashSet a -> [a] -> [a]
go (a -> HashSet a -> HashSet a
forall a. (Eq a, Hashable a) => a -> HashSet a -> HashSet a
HashSet.insert a
x HashSet a
s) [a]
xs

-- | same behavior as 'Data.List.nub', but requires 'Ord' and is @O(n log n)@
--
-- <https://github.com/nh2/haskell-ordnub>
ordNub :: (Ord a) => [a] -> [a]
ordNub :: [a] -> [a]
ordNub = Set a -> [a] -> [a]
forall a. Ord a => Set a -> [a] -> [a]
go Set a
forall a. Set a
Set.empty
  where
    go :: Set a -> [a] -> [a]
go Set a
_ [] = []
    go Set a
s (a
x:[a]
xs) | a
x a -> Set a -> Bool
forall a. Ord a => a -> Set a -> Bool
`Set.member` Set a
s = Set a -> [a] -> [a]
go Set a
s [a]
xs
                | Bool
otherwise        = a
x a -> [a] -> [a]
forall a. a -> [a] -> [a]
: Set a -> [a] -> [a]
go (a -> Set a -> Set a
forall a. Ord a => a -> Set a -> Set a
Set.insert a
x Set a
s) [a]
xs

-- | same behavior as 'Data.List.nubBy', but requires 'Ord' and is @O(n log n)@
--
-- <https://github.com/nh2/haskell-ordnub>
ordNubBy :: (Ord b) => (a -> b) -> (a -> a -> Bool) -> [a] -> [a]
ordNubBy :: (a -> b) -> (a -> a -> Bool) -> [a] -> [a]
ordNubBy a -> b
p a -> a -> Bool
f = Map b [a] -> [a] -> [a]
go Map b [a]
forall k a. Map k a
Map.empty
  -- When removing duplicates, the first function assigns the input to a bucket,
  -- the second function checks whether it is already in the bucket (linear search).
  where
    go :: Map b [a] -> [a] -> [a]
go Map b [a]
_ []     = []
    go Map b [a]
m (a
x:[a]
xs) = let b :: b
b = a -> b
p a
x in case b
b b -> Map b [a] -> Maybe [a]
forall k a. Ord k => k -> Map k a -> Maybe a
`Map.lookup` Map b [a]
m of
                    Maybe [a]
Nothing     -> a
x a -> [a] -> [a]
forall a. a -> [a] -> [a]
: Map b [a] -> [a] -> [a]
go (b -> [a] -> Map b [a] -> Map b [a]
forall k a. Ord k => k -> a -> Map k a -> Map k a
Map.insert b
b [a
x] Map b [a]
m) [a]
xs
                    Just [a]
bucket
                      | (a -> a -> Bool) -> a -> [a] -> Bool
forall a. (a -> a -> Bool) -> a -> [a] -> Bool
elem_by a -> a -> Bool
f a
x [a]
bucket -> Map b [a] -> [a] -> [a]
go Map b [a]
m [a]
xs
                      | Bool
otherwise          -> a
x a -> [a] -> [a]
forall a. a -> [a] -> [a]
: Map b [a] -> [a] -> [a]
go (b -> [a] -> Map b [a] -> Map b [a]
forall k a. Ord k => k -> a -> Map k a -> Map k a
Map.insert b
b (a
xa -> [a] -> [a]
forall a. a -> [a] -> [a]
:[a]
bucket) Map b [a]
m) [a]
xs

    -- From the Data.List source code.
    elem_by :: (a -> a -> Bool) -> a -> [a] -> Bool
    elem_by :: (a -> a -> Bool) -> a -> [a] -> Bool
elem_by a -> a -> Bool
_  a
_ []     = Bool
False
    elem_by a -> a -> Bool
eq a
y (a
x:[a]
xs) = a
y a -> a -> Bool
`eq` a
x Bool -> Bool -> Bool
|| (a -> a -> Bool) -> a -> [a] -> Bool
forall a. (a -> a -> Bool) -> a -> [a] -> Bool
elem_by a -> a -> Bool
eq a
y [a]
xs

-- | Synonym for 'STM.orElse'.
orElseSTM :: STM a -> STM a -> STM a
orElseSTM :: STM a -> STM a -> STM a
orElseSTM = STM a -> STM a -> STM a
forall a. STM a -> STM a -> STM a
STM.orElse
{-# INLINE orElseSTM #-}

-- | Only perform the action if the predicate returns 'True'.
--
-- Since 0.9.2
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'.
--
-- Since 0.9.2
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

-- | Force type to a 'DList'
--
-- Since 0.11.0
asDList :: DList a -> DList a
asDList :: DList a -> DList a
asDList = DList a -> DList a
forall k (cat :: k -> k -> *) (a :: k). Category cat => cat a a
id
{-# INLINE asDList #-}

-- | Synonym for 'DList.apply'
--
-- Since 0.11.0
applyDList :: DList a -> [a] -> [a]
applyDList :: DList a -> [a] -> [a]
applyDList = DList a -> [a] -> [a]
forall a. DList a -> [a] -> [a]
DList.apply
{-# INLINE applyDList #-}

infixr 3 <&&>
-- | '&&' lifted to an Applicative.
--
-- @since 0.12.8
(<&&>) :: Applicative a => a Bool -> a Bool -> a Bool
<&&> :: a Bool -> a Bool -> a Bool
(<&&>) = (Bool -> Bool -> Bool) -> a Bool -> a Bool -> a Bool
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 Bool -> Bool -> Bool
(&&)
{-# INLINE (<&&>) #-}

infixr 2 <||>
-- | '||' lifted to an Applicative.
--
-- @since 0.12.8
(<||>) :: Applicative a => a Bool -> a Bool -> a Bool
<||> :: a Bool -> a Bool -> a Bool
(<||>) = (Bool -> Bool -> Bool) -> a Bool -> a Bool -> a Bool
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 Bool -> Bool -> Bool
(||)
{-# INLINE (<||>) #-}

-- | Convert a 'ByteString' into a storable 'Vector'.
toByteVector :: ByteString -> SVector Word8
toByteVector :: ByteString -> SVector Word8
toByteVector (PS ForeignPtr Word8
fptr Int
offset Int
idx) = ForeignPtr Word8 -> Int -> Int -> SVector Word8
forall a. Storable a => ForeignPtr a -> Int -> Int -> Vector a
unsafeFromForeignPtr ForeignPtr Word8
fptr Int
offset Int
idx
{-# INLINE toByteVector #-}

-- | Convert a storable 'Vector' into a 'ByteString'.
fromByteVector :: SVector Word8 -> ByteString
fromByteVector :: SVector Word8 -> ByteString
fromByteVector SVector Word8
v =
    ForeignPtr Word8 -> Int -> Int -> ByteString
PS ForeignPtr Word8
fptr Int
offset Int
idx
  where
    (ForeignPtr Word8
fptr, Int
offset, Int
idx) = SVector Word8 -> (ForeignPtr Word8, Int, Int)
forall a. Storable a => Vector a -> (ForeignPtr a, Int, Int)
unsafeToForeignPtr SVector Word8
v
{-# INLINE fromByteVector #-}

-- | 'waitSTM' for any 'MonadIO'
--
-- @since 1.0.0
waitAsync :: MonadIO m => Async a -> m a
waitAsync :: Async a -> m a
waitAsync = STM a -> m a
forall (m :: * -> *) a. MonadIO m => STM a -> m a
atomically (STM a -> m a) -> (Async a -> STM a) -> Async a -> m a
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Async a -> STM a
forall a. Async a -> STM a
waitSTM

-- | 'pollSTM' for any 'MonadIO'
--
-- @since 1.0.0
pollAsync :: MonadIO m => Async a -> m (Maybe (Either SomeException a))
pollAsync :: Async a -> m (Maybe (Either SomeException a))
pollAsync = STM (Maybe (Either SomeException a))
-> m (Maybe (Either SomeException a))
forall (m :: * -> *) a. MonadIO m => STM a -> m a
atomically (STM (Maybe (Either SomeException a))
 -> m (Maybe (Either SomeException a)))
-> (Async a -> STM (Maybe (Either SomeException a)))
-> Async a
-> m (Maybe (Either SomeException a))
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Async a -> STM (Maybe (Either SomeException a))
forall a. Async a -> STM (Maybe (Either SomeException a))
pollSTM

-- | 'waitCatchSTM' for any 'MonadIO'
--
-- @since 1.0.0
waitCatchAsync :: MonadIO m => Async a -> m (Either SomeException a)
waitCatchAsync :: Async a -> m (Either SomeException a)
waitCatchAsync = Async a -> m (Either SomeException a)
forall (m :: * -> *) a.
MonadIO m =>
Async a -> m (Either SomeException a)
waitCatch

-- | 'Async.link' generalized to any 'MonadIO'
--
-- @since 1.0.0
linkAsync :: MonadIO m => Async a -> m ()
linkAsync :: Async a -> m ()
linkAsync = Async a -> m ()
forall (m :: * -> *) a. MonadIO m => Async a -> m ()
UnliftIO.link

-- | 'Async.link2' generalized to any 'MonadIO'
--
-- @since 1.0.0
link2Async :: MonadIO m => Async a -> Async b -> m ()
link2Async :: Async a -> Async b -> m ()
link2Async Async a
a = Async a -> Async b -> m ()
forall (m :: * -> *) a b. MonadIO m => Async a -> Async b -> m ()
UnliftIO.link2 Async a
a

-- | Strictly read a file into a 'ByteString'.
--
-- @since 1.2.0
readFile :: MonadIO m => FilePath -> m ByteString
readFile :: [Char] -> m ByteString
readFile = IO ByteString -> m ByteString
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO ByteString -> m ByteString)
-> ([Char] -> IO ByteString) -> [Char] -> m ByteString
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. [Char] -> IO ByteString
Data.ByteString.readFile

-- | Strictly read a file into a 'Text' using a UTF-8 character
-- encoding. In the event of a character encoding error, a Unicode
-- replacement character will be used (a.k.a., @lenientDecode@).
--
-- @since 1.2.0
readFileUtf8 :: MonadIO m => FilePath -> m Text
readFileUtf8 :: [Char] -> m Text
readFileUtf8 = (ByteString -> Text) -> m ByteString -> m Text
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM ByteString -> Text
forall textual binary. Utf8 textual binary => binary -> textual
decodeUtf8 (m ByteString -> m Text)
-> ([Char] -> m ByteString) -> [Char] -> m Text
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. [Char] -> m ByteString
forall (m :: * -> *). MonadIO m => [Char] -> m ByteString
readFile

-- | Write a 'ByteString' to a file.
--
-- @since 1.2.0
writeFile :: MonadIO m => FilePath -> ByteString -> m ()
writeFile :: [Char] -> ByteString -> m ()
writeFile [Char]
fp = IO () -> m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ()) -> (ByteString -> IO ()) -> ByteString -> m ()
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. [Char] -> ByteString -> IO ()
Data.ByteString.writeFile [Char]
fp

-- | Write a 'Text' to a file using a UTF-8 character encoding.
--
-- @since 1.2.0
writeFileUtf8 :: MonadIO m => FilePath -> Text -> m ()
writeFileUtf8 :: [Char] -> Text -> m ()
writeFileUtf8 [Char]
fp = [Char] -> ByteString -> m ()
forall (m :: * -> *). MonadIO m => [Char] -> ByteString -> m ()
writeFile [Char]
fp (ByteString -> m ()) -> (Text -> ByteString) -> Text -> m ()
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Text -> ByteString
forall textual binary. Utf8 textual binary => textual -> binary
encodeUtf8

-- | Strictly read the contents of the given 'Handle' into a
-- 'ByteString'.
--
-- @since 1.2.0
hGetContents :: MonadIO m => Handle -> m ByteString
hGetContents :: Handle -> m ByteString
hGetContents = IO ByteString -> m ByteString
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO ByteString -> m ByteString)
-> (Handle -> IO ByteString) -> Handle -> m ByteString
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Handle -> IO ByteString
Data.ByteString.hGetContents

-- | Write a 'ByteString' to the given 'Handle'.
--
-- @since 1.2.0
hPut :: MonadIO m => Handle -> ByteString -> m ()
hPut :: Handle -> ByteString -> m ()
hPut Handle
h = IO () -> m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ()) -> (ByteString -> IO ()) -> ByteString -> m ()
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Handle -> ByteString -> IO ()
Data.ByteString.hPut Handle
h

-- | Read a single chunk of data as a 'ByteString' from the given
-- 'Handle'.
--
-- Under the surface, this uses 'Data.ByteString.hGetSome' with the
-- default chunk size.
--
-- @since 1.2.0
hGetChunk :: MonadIO m => Handle -> m ByteString
hGetChunk :: Handle -> m ByteString
hGetChunk = IO ByteString -> m ByteString
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO ByteString -> m ByteString)
-> (Handle -> IO ByteString) -> Handle -> m ByteString
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. (Handle -> Int -> IO ByteString) -> Int -> Handle -> IO ByteString
forall a b c. (a -> b -> c) -> b -> a -> c
flip Handle -> Int -> IO ByteString
Data.ByteString.hGetSome Int
defaultChunkSize

-- | Write a character to stdout
--
-- Uses system locale settings
--
-- @since 1.3.1
putChar :: MonadIO m => Char -> m ()
putChar :: Char -> m ()
putChar = IO () -> m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ()) -> (Char -> IO ()) -> Char -> m ()
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Char -> IO ()
Prelude.putChar

-- | Write a Text to stdout
--
-- Uses system locale settings
--
-- @since 1.3.1
putStr :: MonadIO m => Text -> m ()
putStr :: Text -> m ()
putStr = IO () -> m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ()) -> (Text -> IO ()) -> Text -> m ()
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Text -> IO ()
TextIO.putStr

-- | Write a Text followed by a newline to stdout
--
-- Uses system locale settings
--
-- @since 1.3.1
putStrLn :: MonadIO m => Text -> m ()
putStrLn :: Text -> m ()
putStrLn = IO () -> m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ()) -> (Text -> IO ()) -> Text -> m ()
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Text -> IO ()
TextIO.putStrLn

-- | Read a character from stdin
--
-- Uses system locale settings
--
-- @since 1.3.1
getChar :: MonadIO m => m Char
getChar :: m Char
getChar = IO Char -> m Char
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO IO Char
Prelude.getChar

-- | Read a line from stdin
--
-- Uses system locale settings
--
-- @since 1.3.1
getLine :: MonadIO m => m Text
getLine :: m Text
getLine = IO Text -> m Text
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO IO Text
TextIO.getLine

-- | Read all input from stdin into a lazy Text ('LText')
--
-- Uses system locale settings
--
-- @since 1.3.1
getContents :: MonadIO m => m LText
getContents :: m LText
getContents = IO LText -> m LText
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO IO LText
LTextIO.getContents

-- | Takes a function of type 'LText -> LText' and passes all input on stdin
-- to it, then prints result to stdout
--
-- Uses lazy IO
-- Uses system locale settings
--
-- @since 1.3.1
interact :: MonadIO m => (LText -> LText) -> m ()
interact :: (LText -> LText) -> m ()
interact = IO () -> m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ())
-> ((LText -> LText) -> IO ()) -> (LText -> LText) -> m ()
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. (LText -> LText) -> IO ()
LTextIO.interact


#if MIN_VERSION_time(1,10,0)
parseTime 
  :: Time.ParseTime t
  => Time.TimeLocale -- ^ Time locale.
  -> String -- ^ Format string.
  -> String -- ^ Input string.
  -> Maybe t -- ^ The time value, or 'Nothing' if the input could not be parsed using the given format.
parseTime = parseTimeM True
#endif