Commit 559e696c authored by Ian Lynagh's avatar Ian Lynagh
Browse files

Remove non-random stuff (of base), and rename package to "random"

parent 6b1a36a5
-----------------------------------------------------------------------------
-- |
-- Module : Control.Applicative
-- Copyright : Conor McBride and Ross Paterson 2005
-- License : BSD-style (see the LICENSE file in the distribution)
--
-- Maintainer : ross@soi.city.ac.uk
-- Stability : experimental
-- Portability : portable
--
-- This module describes a structure intermediate between a functor and
-- a monad: it provides pure expressions and sequencing, but no binding.
-- (Technically, a strong lax monoidal functor.) For more details, see
-- /Applicative Programming with Effects/,
-- by Conor McBride and Ross Paterson, online at
-- <http://www.soi.city.ac.uk/~ross/papers/Applicative.html>.
--
-- This interface was introduced for parsers by Niklas R&#xF6;jemo, because
-- it admits more sharing than the monadic interface. The names here are
-- mostly based on recent parsing work by Doaitse Swierstra.
--
-- This class is also useful with instances of the
-- 'Data.Traversable.Traversable' class.
module Control.Applicative (
-- * Applicative functors
Applicative(..),
-- * Alternatives
Alternative(..),
-- * Instances
Const(..), WrappedMonad(..), WrappedArrow(..), ZipList(..),
-- * Utility functions
(<$>), (<$), (*>), (<*), (<**>),
liftA, liftA2, liftA3,
optional, some, many
) where
#ifdef __HADDOCK__
import Prelude
#endif
import Control.Arrow
(Arrow(arr, (>>>), (&&&)), ArrowZero(zeroArrow), ArrowPlus((<+>)))
import Control.Monad (liftM, ap, MonadPlus(..))
import Control.Monad.Instances ()
import Data.Monoid (Monoid(..))
infixl 3 <|>
infixl 4 <$>, <$
infixl 4 <*>, <*, *>, <**>
-- | A functor with application.
--
-- Instances should satisfy the following laws:
--
-- [/identity/]
-- @'pure' 'id' '<*>' v = v@
--
-- [/composition/]
-- @'pure' (.) '<*>' u '<*>' v '<*>' w = u '<*>' (v '<*>' w)@
--
-- [/homomorphism/]
-- @'pure' f '<*>' 'pure' x = 'pure' (f x)@
--
-- [/interchange/]
-- @u '<*>' 'pure' y = 'pure' ('$' y) '<*>' u@
--
-- The 'Functor' instance should satisfy
--
-- @
-- 'fmap' f x = 'pure' f '<*>' x
-- @
--
-- If @f@ is also a 'Monad', define @'pure' = 'return'@ and @('<*>') = 'ap'@.
class Functor f => Applicative f where
-- | Lift a value.
pure :: a -> f a
-- | Sequential application.
(<*>) :: f (a -> b) -> f a -> f b
-- | A monoid on applicative functors.
class Applicative f => Alternative f where
-- | The identity of '<|>'
empty :: f a
-- | An associative binary operation
(<|>) :: f a -> f a -> f a
-- instances for Prelude types
instance Applicative Maybe where
pure = return
(<*>) = ap
instance Alternative Maybe where
empty = Nothing
Nothing <|> p = p
Just x <|> _ = Just x
instance Applicative [] where
pure = return
(<*>) = ap
instance Alternative [] where
empty = []
(<|>) = (++)
instance Applicative IO where
pure = return
(<*>) = ap
instance Applicative ((->) a) where
pure = const
(<*>) f g x = f x (g x)
instance Monoid a => Applicative ((,) a) where
pure x = (mempty, x)
(u, f) <*> (v, x) = (u `mappend` v, f x)
-- new instances
newtype Const a b = Const { getConst :: a }
instance Functor (Const m) where
fmap _ (Const v) = Const v
instance Monoid m => Applicative (Const m) where
pure _ = Const mempty
Const f <*> Const v = Const (f `mappend` v)
newtype WrappedMonad m a = WrapMonad { unwrapMonad :: m a }
instance Monad m => Functor (WrappedMonad m) where
fmap f (WrapMonad v) = WrapMonad (liftM f v)
instance Monad m => Applicative (WrappedMonad m) where
pure = WrapMonad . return
WrapMonad f <*> WrapMonad v = WrapMonad (f `ap` v)
instance MonadPlus m => Alternative (WrappedMonad m) where
empty = WrapMonad mzero
WrapMonad u <|> WrapMonad v = WrapMonad (u `mplus` v)
newtype WrappedArrow a b c = WrapArrow { unwrapArrow :: a b c }
instance Arrow a => Functor (WrappedArrow a b) where
fmap f (WrapArrow a) = WrapArrow (a >>> arr f)
instance Arrow a => Applicative (WrappedArrow a b) where
pure x = WrapArrow (arr (const x))
WrapArrow f <*> WrapArrow v = WrapArrow (f &&& v >>> arr (uncurry id))
instance (ArrowZero a, ArrowPlus a) => Alternative (WrappedArrow a b) where
empty = WrapArrow zeroArrow
WrapArrow u <|> WrapArrow v = WrapArrow (u <+> v)
-- | Lists, but with an 'Applicative' functor based on zipping, so that
--
-- @f '<$>' 'ZipList' xs1 '<*>' ... '<*>' 'ZipList' xsn = 'ZipList' (zipWithn f xs1 ... xsn)@
--
newtype ZipList a = ZipList { getZipList :: [a] }
instance Functor ZipList where
fmap f (ZipList xs) = ZipList (map f xs)
instance Applicative ZipList where
pure x = ZipList (repeat x)
ZipList fs <*> ZipList xs = ZipList (zipWith id fs xs)
-- extra functions
-- | A synonym for 'fmap'.
(<$>) :: Functor f => (a -> b) -> f a -> f b
f <$> a = fmap f a
-- | Replace the value.
(<$) :: Functor f => a -> f b -> f a
(<$) = (<$>) . const
-- | Sequence actions, discarding the value of the first argument.
(*>) :: Applicative f => f a -> f b -> f b
(*>) = liftA2 (const id)
-- | Sequence actions, discarding the value of the second argument.
(<*) :: Applicative f => f a -> f b -> f a
(<*) = liftA2 const
-- | A variant of '<*>' with the arguments reversed.
(<**>) :: Applicative f => f a -> f (a -> b) -> f b
(<**>) = liftA2 (flip ($))
-- | Lift a function to actions.
-- This function may be used as a value for `fmap` in a `Functor` instance.
liftA :: Applicative f => (a -> b) -> f a -> f b
liftA f a = pure f <*> a
-- | Lift a binary function to actions.
liftA2 :: Applicative f => (a -> b -> c) -> f a -> f b -> f c
liftA2 f a b = f <$> a <*> b
-- | Lift a ternary function to actions.
liftA3 :: Applicative f => (a -> b -> c -> d) -> f a -> f b -> f c -> f d
liftA3 f a b c = f <$> a <*> b <*> c
-- | One or none.
optional :: Alternative f => f a -> f (Maybe a)
optional v = Just <$> v <|> pure Nothing
-- | One or more.
some :: Alternative f => f a -> f [a]
some v = some_v
where many_v = some_v <|> pure []
some_v = (:) <$> v <*> many_v
-- | Zero or more.
many :: Alternative f => f a -> f [a]
many v = many_v
where many_v = some_v <|> pure []
some_v = (:) <$> v <*> many_v
-----------------------------------------------------------------------------
-- |
-- Module : Control.Arrow
-- Copyright : (c) Ross Paterson 2002
-- License : BSD-style (see the LICENSE file in the distribution)
--
-- Maintainer : ross@soi.city.ac.uk
-- Stability : experimental
-- Portability : portable
--
-- Basic arrow definitions, based on
-- /Generalising Monads to Arrows/, by John Hughes,
-- /Science of Computer Programming/ 37, pp67-111, May 2000.
-- plus a couple of definitions ('returnA' and 'loop') from
-- /A New Notation for Arrows/, by Ross Paterson, in /ICFP 2001/,
-- Firenze, Italy, pp229-240.
-- See these papers for the equations these combinators are expected to
-- satisfy. These papers and more information on arrows can be found at
-- <http://www.haskell.org/arrows/>.
module Control.Arrow (
-- * Arrows
Arrow(..), Kleisli(..),
-- ** Derived combinators
returnA,
(^>>), (>>^),
-- ** Right-to-left variants
(<<<), (<<^), (^<<),
-- * Monoid operations
ArrowZero(..), ArrowPlus(..),
-- * Conditionals
ArrowChoice(..),
-- * Arrow application
ArrowApply(..), ArrowMonad(..), leftApp,
-- * Feedback
ArrowLoop(..)
) where
import Prelude
import Control.Monad
import Control.Monad.Fix
infixr 5 <+>
infixr 3 ***
infixr 3 &&&
infixr 2 +++
infixr 2 |||
infixr 1 >>>, ^>>, >>^
infixr 1 <<<, ^<<, <<^
-- | The basic arrow class.
-- Any instance must define either 'arr' or 'pure' (which are synonyms),
-- as well as '>>>' and 'first'. The other combinators have sensible
-- default definitions, which may be overridden for efficiency.
class Arrow a where
-- | Lift a function to an arrow: you must define either this
-- or 'pure'.
arr :: (b -> c) -> a b c
arr = pure
-- | A synonym for 'arr': you must define one or other of them.
pure :: (b -> c) -> a b c
pure = arr
-- | Left-to-right composition of arrows.
(>>>) :: a b c -> a c d -> a b d
-- | Send the first component of the input through the argument
-- arrow, and copy the rest unchanged to the output.
first :: a b c -> a (b,d) (c,d)
-- | A mirror image of 'first'.
--
-- The default definition may be overridden with a more efficient
-- version if desired.
second :: a b c -> a (d,b) (d,c)
second f = arr swap >>> first f >>> arr swap
where swap ~(x,y) = (y,x)
-- | Split the input between the two argument arrows and combine
-- their output. Note that this is in general not a functor.
--
-- The default definition may be overridden with a more efficient
-- version if desired.
(***) :: a b c -> a b' c' -> a (b,b') (c,c')
f *** g = first f >>> second g
-- | Fanout: send the input to both argument arrows and combine
-- their output.
--
-- The default definition may be overridden with a more efficient
-- version if desired.
(&&&) :: a b c -> a b c' -> a b (c,c')
f &&& g = arr (\b -> (b,b)) >>> f *** g
{-# RULES
"compose/arr" forall f g .
arr f >>> arr g = arr (f >>> g)
"first/arr" forall f .
first (arr f) = arr (first f)
"second/arr" forall f .
second (arr f) = arr (second f)
"product/arr" forall f g .
arr f *** arr g = arr (f *** g)
"fanout/arr" forall f g .
arr f &&& arr g = arr (f &&& g)
"compose/first" forall f g .
first f >>> first g = first (f >>> g)
"compose/second" forall f g .
second f >>> second g = second (f >>> g)
#-}
-- Ordinary functions are arrows.
instance Arrow (->) where
arr f = f
f >>> g = g . f
first f = f *** id
second f = id *** f
-- (f *** g) ~(x,y) = (f x, g y)
-- sorry, although the above defn is fully H'98, nhc98 can't parse it.
(***) f g ~(x,y) = (f x, g y)
-- | Kleisli arrows of a monad.
newtype Kleisli m a b = Kleisli { runKleisli :: a -> m b }
instance Monad m => Arrow (Kleisli m) where
arr f = Kleisli (return . f)
Kleisli f >>> Kleisli g = Kleisli (\b -> f b >>= g)
first (Kleisli f) = Kleisli (\ ~(b,d) -> f b >>= \c -> return (c,d))
second (Kleisli f) = Kleisli (\ ~(d,b) -> f b >>= \c -> return (d,c))
-- | The identity arrow, which plays the role of 'return' in arrow notation.
returnA :: Arrow a => a b b
returnA = arr id
-- | Precomposition with a pure function.
(^>>) :: Arrow a => (b -> c) -> a c d -> a b d
f ^>> a = arr f >>> a
-- | Postcomposition with a pure function.
(>>^) :: Arrow a => a b c -> (c -> d) -> a b d
a >>^ f = a >>> arr f
-- | Right-to-left composition, for a better fit with arrow notation.
(<<<) :: Arrow a => a c d -> a b c -> a b d
f <<< g = g >>> f
-- | Precomposition with a pure function (right-to-left variant).
(<<^) :: Arrow a => a c d -> (b -> c) -> a b d
a <<^ f = a <<< arr f
-- | Postcomposition with a pure function (right-to-left variant).
(^<<) :: Arrow a => (c -> d) -> a b c -> a b d
f ^<< a = arr f <<< a
class Arrow a => ArrowZero a where
zeroArrow :: a b c
instance MonadPlus m => ArrowZero (Kleisli m) where
zeroArrow = Kleisli (\x -> mzero)
class ArrowZero a => ArrowPlus a where
(<+>) :: a b c -> a b c -> a b c
instance MonadPlus m => ArrowPlus (Kleisli m) where
Kleisli f <+> Kleisli g = Kleisli (\x -> f x `mplus` g x)
-- | Choice, for arrows that support it. This class underlies the
-- @if@ and @case@ constructs in arrow notation.
-- Any instance must define 'left'. The other combinators have sensible
-- default definitions, which may be overridden for efficiency.
class Arrow a => ArrowChoice a where
-- | Feed marked inputs through the argument arrow, passing the
-- rest through unchanged to the output.
left :: a b c -> a (Either b d) (Either c d)
-- | A mirror image of 'left'.
--
-- The default definition may be overridden with a more efficient
-- version if desired.
right :: a b c -> a (Either d b) (Either d c)
right f = arr mirror >>> left f >>> arr mirror
where mirror (Left x) = Right x
mirror (Right y) = Left y
-- | Split the input between the two argument arrows, retagging
-- and merging their outputs.
-- Note that this is in general not a functor.
--
-- The default definition may be overridden with a more efficient
-- version if desired.
(+++) :: a b c -> a b' c' -> a (Either b b') (Either c c')
f +++ g = left f >>> right g
-- | Fanin: Split the input between the two argument arrows and
-- merge their outputs.
--
-- The default definition may be overridden with a more efficient
-- version if desired.
(|||) :: a b d -> a c d -> a (Either b c) d
f ||| g = f +++ g >>> arr untag
where untag (Left x) = x
untag (Right y) = y
{-# RULES
"left/arr" forall f .
left (arr f) = arr (left f)
"right/arr" forall f .
right (arr f) = arr (right f)
"sum/arr" forall f g .
arr f +++ arr g = arr (f +++ g)
"fanin/arr" forall f g .
arr f ||| arr g = arr (f ||| g)
"compose/left" forall f g .
left f >>> left g = left (f >>> g)
"compose/right" forall f g .
right f >>> right g = right (f >>> g)
#-}
instance ArrowChoice (->) where
left f = f +++ id
right f = id +++ f
f +++ g = (Left . f) ||| (Right . g)
(|||) = either
instance Monad m => ArrowChoice (Kleisli m) where
left f = f +++ arr id
right f = arr id +++ f
f +++ g = (f >>> arr Left) ||| (g >>> arr Right)
Kleisli f ||| Kleisli g = Kleisli (either f g)
-- | Some arrows allow application of arrow inputs to other inputs.
class Arrow a => ArrowApply a where
app :: a (a b c, b) c
instance ArrowApply (->) where
app (f,x) = f x
instance Monad m => ArrowApply (Kleisli m) where
app = Kleisli (\(Kleisli f, x) -> f x)
-- | The 'ArrowApply' class is equivalent to 'Monad': any monad gives rise
-- to a 'Kleisli' arrow, and any instance of 'ArrowApply' defines a monad.
newtype ArrowApply a => ArrowMonad a b = ArrowMonad (a () b)
instance ArrowApply a => Monad (ArrowMonad a) where
return x = ArrowMonad (arr (\z -> x))
ArrowMonad m >>= f = ArrowMonad (m >>>
arr (\x -> let ArrowMonad h = f x in (h, ())) >>>
app)
-- | Any instance of 'ArrowApply' can be made into an instance of
-- 'ArrowChoice' by defining 'left' = 'leftApp'.
leftApp :: ArrowApply a => a b c -> a (Either b d) (Either c d)
leftApp f = arr ((\b -> (arr (\() -> b) >>> f >>> arr Left, ())) |||
(\d -> (arr (\() -> d) >>> arr Right, ()))) >>> app
-- | The 'loop' operator expresses computations in which an output value is
-- fed back as input, even though the computation occurs only once.
-- It underlies the @rec@ value recursion construct in arrow notation.
class Arrow a => ArrowLoop a where
loop :: a (b,d) (c,d) -> a b c
instance ArrowLoop (->) where
loop f b = let (c,d) = f (b,d) in c
instance MonadFix m => ArrowLoop (Kleisli m) where
loop (Kleisli f) = Kleisli (liftM fst . mfix . f')
where f' x y = f (x, snd y)
-----------------------------------------------------------------------------
-- |
-- Module : Control.Concurrent
-- Copyright : (c) The University of Glasgow 2001
-- License : BSD-style (see the file libraries/base/LICENSE)
--
-- Maintainer : libraries@haskell.org
-- Stability : experimental
-- Portability : non-portable (concurrency)
--
-- A common interface to a collection of useful concurrency
-- abstractions.
--
-----------------------------------------------------------------------------
module Control.Concurrent (
-- * Concurrent Haskell
-- $conc_intro
-- * Basic concurrency operations
ThreadId,
#ifdef __GLASGOW_HASKELL__
myThreadId,
#endif
forkIO,
#ifdef __GLASGOW_HASKELL__
killThread,
throwTo,
#endif
-- * Scheduling
-- $conc_scheduling
yield, -- :: IO ()
-- ** Blocking
-- $blocking
#ifdef __GLASGOW_HASKELL__
-- ** Waiting
threadDelay, -- :: Int -> IO ()
threadWaitRead, -- :: Int -> IO ()
threadWaitWrite, -- :: Int -> IO ()
#endif
-- * Communication abstractions
module Control.Concurrent.MVar,
module Control.Concurrent.Chan,
module Control.Concurrent.QSem,
module Control.Concurrent.QSemN,
module Control.Concurrent.SampleVar,
-- * Merging of streams
#ifndef __HUGS__
mergeIO, -- :: [a] -> [a] -> IO [a]
nmergeIO, -- :: [[a]] -> IO [a]
#endif
-- $merge
#ifdef __GLASGOW_HASKELL__
-- * Bound Threads
-- $boundthreads
rtsSupportsBoundThreads,
forkOS,
isCurrentThreadBound,
runInBoundThread,
runInUnboundThread
#endif
-- * GHC's implementation of concurrency
-- |This section describes features specific to GHC's
-- implementation of Concurrent Haskell.
-- ** Haskell threads and Operating System threads
-- $osthreads
-- ** Terminating the program
-- $termination
-- ** Pre-emption
-- $preemption
) where
import Prelude
import Control.Exception as Exception
#ifdef __GLASGOW_HASKELL__
import GHC.Conc ( ThreadId(..), myThreadId, killThread, yield,
threadDelay, threadWaitRead, threadWaitWrite,
forkIO, childHandler )
import GHC.TopHandler ( reportStackOverflow, reportError )
import GHC.IOBase ( IO(..) )
import GHC.IOBase ( unsafeInterleaveIO )
import GHC.IOBase ( newIORef, readIORef, writeIORef )
import GHC.Base
import Foreign.StablePtr
import Foreign.C.Types ( CInt )
import Control.Monad ( when )
#endif
#ifdef __HUGS__
import Hugs.ConcBase
#endif