-- Hoogle documentation, generated by Haddock
-- See Hoogle, http://www.haskell.org/hoogle/
-- | Concurrent utilities
--
-- Concurrent utilities
@package ConcurrentUtils
@version 0.4.4.0
-- | Lists suitable for parallel execution (taken from Hackage's monad-par
-- package). (For converting to regular lists, there is the toList
-- function in Data.Foldable.)
module Control.CUtils.AList
data AList t
Append :: (AList t) -> (AList t) -> AList t
List :: [t] -> AList t
-- | Filters the AList using a predicate.
filterAList :: (b -> Bool) -> AList b -> AList b
-- | Folds the AList with a function, that must be associative. This allows
-- parallelism to be introduced.
assocFold :: (c -> c -> c) -> AList c -> c
-- | Combine monoid elements to get a result.
monoid :: (Eq a, Monoid a) => AList a -> a
-- | Length of an AList.
lenAList :: (Eq b, Num a) => AList b -> a
-- | Find the first element satisfying a predicate.
findAList :: Eq a => (a -> Bool) -> AList a -> Maybe a
-- | Concatenate an AList of ALists.
concatAList :: Monad m => m (m b) -> m b
instance Data.Data.Data t => Data.Data.Data (Control.CUtils.AList.AList t)
instance GHC.Show.Show t => GHC.Show.Show (Control.CUtils.AList.AList t)
instance GHC.Classes.Ord t => GHC.Classes.Ord (Control.CUtils.AList.AList t)
instance GHC.Classes.Eq t => GHC.Classes.Eq (Control.CUtils.AList.AList t)
instance GHC.Base.Monad Control.CUtils.AList.AList
instance GHC.Base.MonadPlus Control.CUtils.AList.AList
instance GHC.Base.Applicative Control.CUtils.AList.AList
instance GHC.Base.Alternative Control.CUtils.AList.AList
instance GHC.Base.Functor Control.CUtils.AList.AList
instance Data.Traversable.Traversable Control.CUtils.AList.AList
instance Data.Foldable.Foldable Control.CUtils.AList.AList
-- | A lock-free channel (queue) data structure.
module Control.CUtils.Channel
data Channel a t
-- | Create a channel with a buffer at least as big as buffer.
newChannel :: (MArray a t IO) => Word32 -> IO (Channel a t)
-- | Write into the channel, blocking when the buffer is full.
writeChannel :: MArray a e IO => Channel a e -> e -> IO ()
-- | Read from the channel, blocking when the buffer is empty.
readChannel :: MArray a b IO => Channel a b -> IO b
tryReadChannel :: MArray a1 a IO => Channel a1 a -> IO (Maybe a)
module Control.CUtils.StrictArrow
-- | Arrows possessing a strictness effect
class Strict a
force :: Strict a => a t u -> a t u
instance Control.CUtils.StrictArrow.Strict (->)
instance GHC.Base.Monad m => Control.CUtils.StrictArrow.Strict (Control.Arrow.Kleisli m)
-- | A module of concurrent higher order functions.
module Control.CUtils.Conc
-- | For exceptions caused by caller code.
data ExceptionList
ExceptionList :: [SomeException] -> ExceptionList
-- | For internal errors. If a procedure throws this, some threads it
-- created may still be running. It is thrown separately from
-- ExceptionList.
data ConcException
ConcException :: ConcException
assocFold :: Concurrent (Kleisli m) => (b -> b -> m b) -> (c -> b) -> (b, Array Int c) -> m b
-- | A type class of arrows that support some form of concurrency.
class Concurrent a
-- | Runs an associative folding function on the given array. Note: this
-- function only spawns enough threads to make effective use of the
-- capabilities. Any two list elements may be processed
-- sequentially or concurrently. To get parallelism, you have to set the
-- numCapabilities value, e.g. using GHC's +RTS -N flag.
arr_assocFold :: Concurrent a => a (b, b) b -> (c -> b) -> a (b, Array Int c) b
-- | The first parameter is the number of computations which are indexed
-- from 0 to n - 1.
arr_concF_ :: (Concurrent a, ?seq :: Bool) => a (t, Int) () -> a (t, Int) ()
arr_concF :: (Concurrent a, ?seq :: Bool) => a (u, Int) t -> a (u, Int) (Array Int t)
arr_oneOfF :: Concurrent a => a (u, Int) b -> a (u, Int) b
concF_ :: (?seq :: Bool, Concurrent (Kleisli m)) => Int -> (Int -> m ()) -> m ()
concF :: (?seq :: Bool, Concurrent (Kleisli m)) => Int -> (Int -> m t) -> m (Array Int t)
conc_ :: (?seq :: Bool, Concurrent (Kleisli m)) => Array Int (m ()) -> m ()
-- | The next function takes an implicit parameter ?seq. Set it to True if
-- you want to only spawn threads for the capabilities (same as
-- assocFold; good for speed). If you need all the actions to be
-- executed concurrently, set it to False.
conc :: (?seq :: Bool) => Array Int (IO e) -> IO (Array Int e)
-- | Version of concF specialized for two computations.
concP :: (Monad m, Concurrent (Kleisli m)) => m t -> m t1 -> m (t, t1)
progressConcF :: (?seq :: Bool) => Int -> (Int -> IO t) -> IO (Array Int t)
oneOfF :: Concurrent (Kleisli m) => Int -> (Int -> m b) -> m b
-- | Runs several computations in parallel, and returns one of their
-- results (terminating the other computations).
oneOf :: Array Int (IO a) -> IO a
instance GHC.Show.Show Control.CUtils.Conc.ConcException
instance GHC.Show.Show Control.CUtils.Conc.ExceptionList
instance GHC.Exception.Exception Control.CUtils.Conc.ExceptionList
instance GHC.Exception.Exception Control.CUtils.Conc.ConcException
instance Control.CUtils.Conc.Concurrent (Control.Arrow.Kleisli GHC.Types.IO)
instance Control.CUtils.Conc.Concurrent (->)
-- | An implementation of nested data parallelism (due to Simon Peyton
-- Jones et al)
module Control.CUtils.DataParallel
data ArrC t
newArray :: [e] -> Array Int e
inject :: Array Int e -> ArrC e
project :: ArrC t -> Array Int t
data Structural a t u
-- | The A arrow includes a set of primitives that may be executed
-- concurrently. Programs are incrementally optimized as they are put
-- together. A program may be optimized once, and the result saved for
-- repeated use.
--
-- Notes:
--
--
-- - The exact output of the optimizer is subject to change.
-- - The program must be a finite data structure, or optimization will
-- diverge.
--
data A a t u
-- | Obtain a Structural program from an A program.
unA :: Category * t => A t t1 t2 -> Structural t t1 t2
mapA' :: (ArrowChoice a) => A a t u -> A a (ArrC t) (ArrC u)
liftA :: (Category a) => a t u -> A a t u
-- | Supplies an array of a repeated value paired with the index of each
-- element.
countA :: A a (t, Int) (ArrC (t, Int))
-- | Access one index of an array.
indexA :: A a (ArrC u, Int) u
-- | An operation analogous to zip.
zipA :: (Category a) => A a (ArrC t, ArrC u) (ArrC (t, u))
-- | unzipA and zipA are inverses.
unzipA :: (Category a) => A a (ArrC (t, u)) (ArrC t, ArrC u)
concatA :: (Category a) => A a (ArrC (ArrC t)) (ArrC t)
-- | Evaluates arrows.
--
-- Notes:
--
--
-- - Effects are supported, but with much weaker semantics than the
-- Kleisli arrows of the monad. In particular, the Map and
-- *** operations are allowed to be parallelized, but on the other
-- hand parallelism is not guaranteed.
--
eval :: (ArrowChoice a, Strict a, Concurrent a) => Structural a t u -> a t u
nQueens :: Int -> A (->) () (ArrC [Int])
sorting :: (Ord t) => Int -> A (->) (ArrC t) (ArrC t)
permute :: A (->) (ArrC Int) (ArrC Int)
instance GHC.Base.Functor Control.CUtils.DataParallel.ArrC
instance GHC.Show.Show (t -> u)
instance Control.Category.Category a => Control.Category.Category (Control.CUtils.DataParallel.A a)
instance Control.Arrow.ArrowChoice a => Control.Arrow.Arrow (Control.CUtils.DataParallel.A a)
instance Control.Arrow.ArrowChoice a => Control.Arrow.ArrowChoice (Control.CUtils.DataParallel.A a)
instance GHC.Show.Show (Control.CUtils.DataParallel.Structural a t u)
instance Control.Category.Category a => Control.Category.Category (Control.CUtils.DataParallel.Structural a)
instance (Control.CUtils.Conc.Concurrent a, Control.CUtils.StrictArrow.Strict a, Control.Arrow.ArrowChoice a, Control.Arrow.ArrowApply a) => Control.Arrow.ArrowApply (Control.CUtils.DataParallel.A a)
module Data.BellmanFord
-- | Edge relaxation.
relaxEdge :: (Fractional b, Ord b, Ord t, Ord t1) => Map t (a, b) -> Map (t, t1) b -> t1 -> (t, b) -> (t, b)
-- | The Bellman-Ford shortest path algorithm.
bellmanFord :: (Ord a) => Map (a, a) Double -> a -> Map a (a, Double)
retrievePath :: (Ord a) => Map a (a, Double) -> a -> a -> [a]
-- | Cycle finding
cycles :: (Ord a) => Map (a, a) () -> a -> Maybe a
-- | Automatic deadlock prevention.
--
-- Automatic deadlock detection is inefficient, and computations cannot
-- be rolled back or aborted in general.
--
-- Instead, I prevent deadlocks before they happen.
module Control.CUtils.Deadlock
-- | The typical sequence that produces a deadlock is as follows:
--
--
-- - Thread 1 acquires lock A
-- - Thread 2 acquires lock B
-- - Thread 1 tries to acquire B
-- - Thread 2 tries to acquire A
--
--
-- Deadlock.
--
-- Standard deadlock detection intervenes after (4) has occurred. I
-- intervene in a lock acquisition that is followed by an unsafe schedule
-- (here at (2)). I suspend thread 2 until a safe schedule is guaranteed
-- -- in this case until thread 1 relinquishes lock A.
--
-- The Res arrow.
--
-- Computations are built with these constructors (and the arrow
-- interface). Pieces of the arrow that hold locks have to be finitely
-- examinable, Locks have to be used with the Acq and Rel constructors.
data Res t u
Lift :: Kleisli IO t v -> Res v u -> Res t u
Acq :: MVar () -> Res t u -> Res t u
Rel :: MVar () -> Res t u -> Res t u
Fork :: Res t () -> Res t u -> Res t u
Plus :: Res t v -> Res u v -> Res (Either t u) v
Id :: Res t t
liftK :: (t -> IO u) -> Res t u
lft :: IO v -> Res v u -> Res b u
acq :: MVar () -> Res u u
rel :: MVar () -> Res u u
fork :: Res t () -> Res t u -> Res t u
-- | Use this to run computations built in the Res arrow.
run :: Res t u -> t -> IO u
instance Control.Category.Category Control.CUtils.Deadlock.Res
instance Control.Arrow.Arrow Control.CUtils.Deadlock.Res
instance Control.Arrow.ArrowChoice Control.CUtils.Deadlock.Res
instance Control.CUtils.Conc.Concurrent Control.CUtils.Deadlock.Res
instance Control.CUtils.StrictArrow.Strict Control.CUtils.Deadlock.Res
instance GHC.Classes.Ord (GHC.MVar.MVar t)
instance GHC.Show.Show (GHC.MVar.MVar t)
-- | A channel module with transparent network communication.
module Control.CUtils.NetChan
data NetSend t
data NetRecv t
localHost :: IO [Char]
-- | Creates a new channel, with receive and send ends.
newNetChan :: (Binary t) => IO (NetRecv t, NetSend t)
-- | Open a channel to another host
newNetSend :: HostName -> IO (NetSend t)
-- | Creates a receive end of this host's channel. Type unsafe!
newNetRecv :: (Binary t) => IO (NetRecv t)
-- | Sends something on a channel.
send :: (Binary t) => NetSend t -> t -> IO ()
receive :: NetRecv t -> IO ByteString
-- | Receives something from a channel.
recv :: (Binary t) => NetRecv t -> IO t
-- | Receives the send end of a channel, on a channel.
recvSend :: NetRecv (NetSend t) -> IO (NetSend t)
-- | Sends the receive end of a channel, on a channel.
sendRecv :: NetSend (NetRecv t) -> NetRecv t -> IO ()
-- | Receives the receive end of a channel, on a channel.
recvRecv :: Binary t => NetRecv (NetRecv t) -> IO (NetRecv t)
activateSend :: NetSend t -> IO (NetSend t)
activateRecv :: (Binary t) => NetRecv t -> IO (NetRecv t)
data Auth t
-- | Remote exercise of authority. Commands are transmitted in the clear,
-- but authenticated.
--
-- auth - The authority to be served (runs on a separate thread).
--
-- r - The receive end from the host.
--
-- s - The send end to the host.
--
-- publicKey - The public key of the intended recipient.
authServer :: (Binary t) => (t -> IO ()) -> NetRecv (Auth t) -> NetSend ByteString -> PublicKey -> IO ()
-- | privateKey - The private key for this host.
--
-- Returns a function that can be used to send messages.
authClient :: (Binary t) => NetRecv ByteString -> NetSend (Auth t) -> PrivateKey -> IO (t -> IO ())
example :: IO ()
instance GHC.Classes.Eq Control.CUtils.NetChan.ChannelFibre
instance GHC.Classes.Eq (Control.CUtils.NetChan.NetSend t)
instance GHC.Classes.Eq (Control.CUtils.NetChan.NetRecv t)
instance Data.Binary.Class.Binary (Control.CUtils.NetChan.NetSend t)
instance Data.Binary.Class.Binary (Control.CUtils.NetChan.NetRecv t)
instance Data.Binary.Class.Binary (Control.CUtils.NetChan.Auth t)
instance Crypto.Random.CryptoRandomGen Crypto.Random.Entropy.EntropyPool
-- | Functional channels | A channel data type which allows consumers to
-- hold references to different points in a stream at the same time.
-- Elements of a channel are kept alive only so long as there are
-- references pointing before those elements. And producers on a channel
-- are kept alive only so long as there are consumers.
module Control.CUtils.FChan
data Chan t
-- | Construct a channel from a list.
listToChan :: [t] -> Chan t
chanContents :: Chan t -> IO [t]
-- | Thrown by the writer function.
data DoneReadingException
DoneReadingException :: DoneReadingException
-- | Take the first element from a channel, and a channel representing the
-- remainder of the output.
takeChan :: Chan t -> IO (t, Chan t)
tryTakeChan :: Chan t -> IO (Maybe (t, Chan t))
-- | Create a new channel. The first return value is a function that can be
-- used to add values to the channel. The second return value is the
-- channel itself.
newChan :: IO (t -> IO (), Chan t)
-- | The first return value is a thunk that returns values from the channel
-- successively, starting from the position of the parameter channel. The
-- second thunk can be used to retrieve the position of the channel after
-- all the reads made using the first thunk.
makeConsumer :: Chan b -> IO (IO b, IO (Chan b))
-- | Create a channel which is initially empty, but accumulates new
-- elements.
dupChan :: Chan a -> IO (Chan a)
instance GHC.Show.Show Control.CUtils.FChan.DoneReadingException
instance GHC.Exception.Exception Control.CUtils.FChan.DoneReadingException
-- | An implementation of communicating sequential processes.
module Control.CUtils.Processes
-- | The CSP data type:
--
-- :|| - interleave
--
-- :? - deterministic choice
--
-- Join - interface parallel
--
-- :-> - prefix
--
-- Stop - empty computation
--
-- Do - execute IO, then behave as the returned process
data CSP
(:||) :: CSP -> CSP -> CSP
(:?) :: CSP -> CSP -> CSP
Join :: CSP -> [String] -> CSP -> CSP
(:->) :: String -> CSP -> CSP
Stop :: CSP
Do :: (IO CSP) -> CSP
runCSP0 :: (String -> IO (), Chan String) -> [(MVar Side, String, Side)] -> CSP -> IO ()
-- | Run a CSP computation.
runCSP :: CSP -> IO ()
instance GHC.Classes.Eq Control.CUtils.Processes.Side
instance GHC.Show.Show Control.CUtils.Processes.CSP
instance GHC.Show.Show (GHC.Types.IO t)