mirror of
https://github.com/maciej-bendkowski/generic-boltzmann-brain.git
synced 2024-08-17 08:30:30 +03:00
Rename BitOracle
to BuffonMachine
.
This commit is contained in:
parent
64cc3147e0
commit
be03ce0fb3
@ -54,7 +54,7 @@ library
|
||||
|
||||
library generic-boltzmann-brain-internal
|
||||
exposed-modules:
|
||||
Data.Boltzmann.BitOracle
|
||||
Data.Boltzmann.BuffonMachine
|
||||
Data.Boltzmann.Sampler
|
||||
Data.Boltzmann.Sampler.TH
|
||||
Data.Boltzmann.System
|
||||
@ -171,7 +171,7 @@ test-suite generic-boltzmann-brain-test
|
||||
type: exitcode-stdio-1.0
|
||||
main-is: Spec.hs
|
||||
other-modules:
|
||||
Test.Unit.BitOracle
|
||||
Test.Unit.BuffonMachine
|
||||
Paths_generic_boltzmann_brain
|
||||
hs-source-dirs:
|
||||
test
|
||||
|
@ -1,14 +1,14 @@
|
||||
{-# LANGUAGE TemplateHaskell #-}
|
||||
|
||||
-- |
|
||||
-- Module : Data.Boltzmann.BitOracle
|
||||
-- Module : Data.Boltzmann.BuffonMachine
|
||||
-- Description :
|
||||
-- Copyright : (c) Maciej Bendkowski, 2022
|
||||
-- License : BSD3
|
||||
-- Maintainer : maciej.bendkowski@gmail.com
|
||||
-- Stability : experimental
|
||||
module Data.Boltzmann.BitOracle (
|
||||
BitOracle,
|
||||
module Data.Boltzmann.BuffonMachine (
|
||||
BuffonMachine,
|
||||
Discrete,
|
||||
Oracle (..),
|
||||
EvalIO (..),
|
||||
@ -67,18 +67,18 @@ regenerate oracle =
|
||||
|
||||
-- |
|
||||
-- Buffon machines implemented as a `State` monad over `Oracle`.
|
||||
newtype BitOracle g a = MkBitOracle
|
||||
newtype BuffonMachine g a = MkBuffonMachine
|
||||
{runBitOracle :: State (Oracle g) a}
|
||||
deriving (Functor, Applicative, Monad) via State (Oracle g)
|
||||
|
||||
class RandomGen g => EvalIO g where
|
||||
evalIO :: BitOracle g a -> IO a
|
||||
evalIO :: BuffonMachine g a -> IO a
|
||||
|
||||
type Bern g = BitOracle g Bool
|
||||
type Bern g = BuffonMachine g Bool
|
||||
|
||||
{-# INLINEABLE getBit #-}
|
||||
getBit :: RandomGen g => Bern g
|
||||
getBit = MkBitOracle $ do
|
||||
getBit = MkBuffonMachine $ do
|
||||
modify' regenerate
|
||||
oracle <- get
|
||||
put $ useBit oracle
|
||||
@ -86,12 +86,12 @@ getBit = MkBitOracle $ do
|
||||
|
||||
-- |
|
||||
-- Random computations resulting in discrete random variables.
|
||||
type Discrete g = BitOracle g Int
|
||||
type Discrete g = BuffonMachine g Int
|
||||
|
||||
-- |
|
||||
-- Runs the given random computation using the given random generator.
|
||||
{-# INLINEABLE eval #-}
|
||||
eval :: RandomGen g => BitOracle g a -> g -> a
|
||||
eval :: RandomGen g => BuffonMachine g a -> g -> a
|
||||
eval m g = evalState (runBitOracle m) (fresh g)
|
||||
|
||||
instance EvalIO SMGen where
|
@ -13,7 +13,7 @@ module Data.Boltzmann.Sampler (
|
||||
) where
|
||||
|
||||
import Control.Monad.Trans.Maybe (MaybeT, runMaybeT)
|
||||
import Data.Boltzmann.BitOracle (BitOracle, eval)
|
||||
import Data.Boltzmann.BuffonMachine (BuffonMachine, eval)
|
||||
import System.Random (RandomGen)
|
||||
import Test.QuickCheck (Gen)
|
||||
import Test.QuickCheck.Gen (Gen (MkGen))
|
||||
@ -21,10 +21,10 @@ import Test.QuickCheck.Random (QCGen (QCGen))
|
||||
|
||||
-- | Multiparametric Boltzmann samplers.
|
||||
class BoltzmannSampler a where
|
||||
sample :: RandomGen g => Int -> MaybeT (BitOracle g) (a, Int)
|
||||
sample :: RandomGen g => Int -> MaybeT (BuffonMachine g) (a, Int)
|
||||
|
||||
rejectionSampler ::
|
||||
(RandomGen g, BoltzmannSampler a) => Int -> Int -> BitOracle g a
|
||||
(RandomGen g, BoltzmannSampler a) => Int -> Int -> BuffonMachine g a
|
||||
rejectionSampler lb ub = do
|
||||
runMaybeT (sample ub)
|
||||
>>= ( \case
|
||||
@ -36,7 +36,7 @@ rejectionSampler lb ub = do
|
||||
)
|
||||
|
||||
toleranceRejectionSampler ::
|
||||
(RandomGen g, BoltzmannSampler a) => Int -> Double -> BitOracle g a
|
||||
(RandomGen g, BoltzmannSampler a) => Int -> Double -> BuffonMachine g a
|
||||
toleranceRejectionSampler n eps = rejectionSampler lb ub
|
||||
where
|
||||
lb = floor $ (1 - eps) * fromIntegral n
|
||||
|
@ -21,7 +21,7 @@ import Data.Map (Map)
|
||||
import qualified Data.Map.Strict as Map
|
||||
|
||||
import Control.Monad (forM)
|
||||
import Data.Boltzmann.BitOracle (Distribution)
|
||||
import Data.Boltzmann.BuffonMachine (Distribution)
|
||||
import Data.Boltzmann.System (
|
||||
ConstructorWeights (MkConstructorWeights, unConstructorWeights),
|
||||
Distributions (Distributions, listTypeDdgs, regTypeDdgs),
|
||||
|
@ -21,7 +21,7 @@ import Language.Haskell.TH.Syntax (
|
||||
)
|
||||
|
||||
import Control.Monad (foldM, forM, replicateM, unless)
|
||||
import Data.Boltzmann.BitOracle (Distribution (Distribution))
|
||||
import Data.Boltzmann.BuffonMachine (Distribution (Distribution))
|
||||
import qualified Data.Map as Map
|
||||
import Data.Map.Strict (Map)
|
||||
import Data.Maybe (fromJust, fromMaybe)
|
||||
|
@ -12,7 +12,7 @@ import qualified Data.Set as Set
|
||||
import Control.Monad (forM, guard)
|
||||
import Control.Monad.Trans (MonadTrans (lift))
|
||||
import Control.Monad.Trans.Reader (ReaderT (runReaderT), asks)
|
||||
import Data.Boltzmann.BitOracle (Distribution, choice)
|
||||
import Data.Boltzmann.BuffonMachine (Distribution, choice)
|
||||
import Data.Boltzmann.Sampler.TH (
|
||||
ConstructorName (MkConstructorName),
|
||||
ListTypeDistributions (unListTypeDistributions),
|
||||
|
@ -9,7 +9,7 @@ module Data.Boltzmann (
|
||||
rejectionSampler,
|
||||
toleranceRejectionSampler,
|
||||
mkDefWeights,
|
||||
BitOracle,
|
||||
BuffonMachine,
|
||||
EvalIO (..),
|
||||
eval,
|
||||
) where
|
||||
@ -36,8 +36,8 @@ import Data.Boltzmann.Sampler.TH (
|
||||
mkDefWeights,
|
||||
)
|
||||
|
||||
import Data.Boltzmann.BitOracle (
|
||||
BitOracle,
|
||||
import Data.Boltzmann.BuffonMachine (
|
||||
BuffonMachine,
|
||||
EvalIO (..),
|
||||
eval,
|
||||
)
|
||||
|
@ -1,5 +1,5 @@
|
||||
import Test.Tasty (TestTree, defaultMain, testGroup)
|
||||
import qualified Test.Unit.BitOracle as BitOracle
|
||||
import qualified Test.Unit.BuffonMachine as BuffonMachine
|
||||
|
||||
main :: IO ()
|
||||
main = defaultMain tests
|
||||
@ -9,4 +9,4 @@ tests = testGroup "Unit tests" unitTests
|
||||
|
||||
unitTests :: [TestTree]
|
||||
unitTests =
|
||||
[BitOracle.unitTests]
|
||||
[BuffonMachine.unitTests]
|
||||
|
@ -1,7 +1,7 @@
|
||||
module Test.Unit.BitOracle (unitTests) where
|
||||
module Test.Unit.BuffonMachine (unitTests) where
|
||||
|
||||
import Control.Monad (replicateM)
|
||||
import Data.Boltzmann.BitOracle (Distribution (..), choice, evalIO)
|
||||
import Data.Boltzmann.BuffonMachine (Distribution (..), choice, evalIO)
|
||||
import qualified Data.Map as Map
|
||||
import Data.Vector (fromList)
|
||||
import System.Random.SplitMix (SMGen)
|
||||
@ -18,7 +18,7 @@ import Test.Tasty.HUnit (
|
||||
unitTests :: TestTree
|
||||
unitTests =
|
||||
testGroup
|
||||
"BitOracle unit tests"
|
||||
"BuffonMachine unit tests"
|
||||
[choiceTests]
|
||||
|
||||
choiceTests :: TestTree
|
Loading…
Reference in New Issue
Block a user