buffon-machines 1.0.0.0 → 1.1.0.0
raw patch · 2 files changed
+31/−34 lines, 2 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
- Data.Buffon.Machine: dyadic :: RandomGen g => Int -> Int -> Bern g
- Data.Buffon.Machine: Rand :: Word32 -> Int -> g -> Rand g
+ Data.Buffon.Machine: Rand :: !Word32 -> !Int -> !g -> Rand g
- Data.Buffon.Machine: [buffer] :: Rand g -> Word32
+ Data.Buffon.Machine: [buffer] :: Rand g -> !Word32
- Data.Buffon.Machine: [counter] :: Rand g -> Int
+ Data.Buffon.Machine: [counter] :: Rand g -> !Int
- Data.Buffon.Machine: [oracle] :: Rand g -> g
+ Data.Buffon.Machine: [oracle] :: Rand g -> !g
- Data.Buffon.Machine: choice :: (Num a, Enum a, RandomGen g) => DecisionTree a -> BuffonMachine g a
+ Data.Buffon.Machine: choice :: RandomGen g => DecisionTree a -> BuffonMachine g a
Files
- Data/Buffon/Machine.hs +30/−33
- buffon-machines.cabal +1/−1
Data/Buffon/Machine.hs view
@@ -45,7 +45,7 @@ in Algorithms and Complexity: New Directions and Recent Results, Academic Press, (1976) -}-{-# LANGUAGE TupleSections, BangPatterns, DeriveLift #-}+{-# LANGUAGE BangPatterns, DeriveLift #-} module Data.Buffon.Machine ( -- * Buffon machines and related utilities. Rand(..), empty, init@@ -61,7 +61,7 @@ , flip, flip' -- * Bernoulli variable generators.- , dyadic, rational, real+ , rational, real -- * Buffon machine combinators. , repeat, cond, neg@@ -105,9 +105,9 @@ -- | 32-bit buffered random bit generator (RBG). data Rand g =- Rand { buffer :: Word32 -- ^ Generator buffer.- , counter :: Int -- ^ Number of consumed buffer bits.- , oracle :: g -- ^ Random bit oracle.+ Rand { buffer :: !Word32 -- ^ Generator buffer.+ , counter :: !Int -- ^ Number of consumed buffer bits.+ , oracle :: !g -- ^ Random bit oracle. } -- | Checks if the given RBG is empty or not.@@ -117,10 +117,10 @@ -- | A fresh RBG. init :: RandomGen g => g -> Rand g-init g = let (x, g') = random g- in Rand { buffer = x- , counter = 0- , oracle = g' }+init g = case random g of+ (x, g') -> Rand { buffer = x+ , counter = 0+ , oracle = g' } -- | Computations consuming random bits using RBGs. -- Note that the implementation is essentially a State monad,@@ -136,11 +136,11 @@ (<*>) = ap instance Monad (BuffonMachine g) where- return x = BuffonMachine (x,)+ return x = BuffonMachine $ \ !rng -> (x, rng) (BuffonMachine f) >>= h =- BuffonMachine $ \rng ->- let (x, rng') = f rng- in runR (h x) rng'+ BuffonMachine $ \ !rng ->+ case f rng of+ (x, !rng') -> runR (h x) rng' -- | Runs the given Buffon machine within the IO monad -- using StdGen as its random bit oracle.@@ -203,7 +203,7 @@ histogramIO m n = runRIO (histogram m n) >>= print mkFlip :: Rand g -> (Bool, Rand g)-mkFlip rng =+mkFlip !rng = (testBit (buffer rng) (counter rng), -- test the respective bit. rng { counter = succ (counter rng) }) @@ -223,7 +223,7 @@ -- | Random coin flip. Note that the implementation -- handles the regeneration of the RBG, see 'Rand'. flip :: RandomGen g => Bern g-flip = BuffonMachine $ \rng ->+flip = BuffonMachine $ \ !rng -> mkFlip $ if empty rng then init (oracle rng) else rng @@ -241,12 +241,6 @@ (True, False) -> return True _ -> flip' --- | Generates all 2^n boolean strings of length n.-genStream :: Int -> [[Bool]]-genStream 0 = [[]]-genStream !n = map (False :) (genStream $ pred n)- ++ map (True :) (genStream $ pred n)- -- | Evaluates the given Bernoulli variable n times -- and returns a list of resulting values. repeat :: RandomGen g@@ -258,13 +252,6 @@ bs <- repeat (pred n) m return (b : bs) --- | Bernoulli variable machine with dyadic parameter λ = s/(2^t).-dyadic :: RandomGen g => Int -> Int -> Bern g-dyadic s t = do- let ps = take s (genStream t)- bs <- repeat t flip- return $ bs `elem` ps- -- | Given parameters a < b, both positive, returns a Bernoulli -- variable with rational parameter λ = a/b. Note: Implements -- the algorithm 'Bernoulli' described by J. Lumbroso.@@ -639,11 +626,21 @@ -- | Draws a discrete variable according -- to the given decision tree.-choice :: (Num a, Enum a, RandomGen g)+choice :: RandomGen g => DecisionTree a -> BuffonMachine g a -choice (Decision n) = return n-choice (Toss lt rt) = do+choice !x = do heads <- flip- if heads then choice rt- else choice lt+ choice' heads x++choice' :: RandomGen g+ => Bool -> DecisionTree a -> BuffonMachine g a++choice' _ (Decision n) = return n+choice' True (Toss _ rt) = do+ heads <- flip+ choice' heads rt++choice' False (Toss lt _) = do+ heads <- flip+ choice' heads lt
buffon-machines.cabal view
@@ -1,5 +1,5 @@ name: buffon-machines-version: 1.0.0.0+version: 1.1.0.0 synopsis: Perfect simulation of discrete random variables description: Monadic implementation of Buffon machines meant for perfect simulation of discrete random variables homepage: https://github.com/maciej-bendkowski/buffon-machines#readme