|
1 | 1 |
|
2 |
| -{-# LANGUAGE TypeFamilies #-} |
3 |
| --- {-# LANGUAGE MultiParamTypeClasses #-} |
| 2 | +{-# LANGUAGE GeneralizedNewtypeDeriving #-} |
| 3 | +{-# LANGUAGE FlexibleContexts #-} |
4 | 4 |
|
5 | 5 | module Graphics.Bling.SamplingNew (
|
| 6 | + main |
6 | 7 | ) where
|
7 | 8 |
|
8 |
| -{- |
9 |
| -type Sample1D s = s Float |
10 |
| -type Sample2D s = s (Float, Float) |
| 9 | +import Control.Applicative |
| 10 | +import Control.Monad.State.Strict |
| 11 | +import Control.Monad.Primitive |
| 12 | +import System.Random.MWC as MWC |
11 | 13 |
|
12 |
| ---class Monad m => Sample1D m where |
13 |
| --- get1d :: a -> m Float |
| 14 | +newtype Stratified m a = Stratified (StateT (Int, Int) m a) |
| 15 | + deriving ( Functor, Applicative, Monad, MonadState (Int, Int) ) |
14 | 16 |
|
15 |
| ---class Monad m => Sample2D m a where |
16 |
| --- get2d :: a -> m (Float, Float) |
| 17 | +type SRunState m = ([Float], Gen (PrimState m)) |
17 | 18 |
|
18 |
| --- a computation which uses sampled values to produce a result |
19 |
| --- newtype Sampled s a = Sampled { runSampled :: s -> a } |
| 19 | +newtype StratRun m a = StratRun (StateT (SRunState m) m a) |
| 20 | + deriving ( Functor, Applicative, Monad, MonadState (SRunState m) ) |
20 | 21 |
|
21 |
| --- type Sampled r a = r -> a |
| 22 | +instance MonadTrans StratRun where |
| 23 | + lift c = StratRun $ lift c |
22 | 24 |
|
23 |
| -class Monad a => Sampled a where |
24 |
| - runSampled :: g a -> a |
| 25 | +mk1d :: (Monad m, PrimMonad r, MonadState (SRunState r) (StratRun r)) => Stratified m (StratRun r Float) |
| 26 | +mk1d = do |
| 27 | + (n1d, n2d) <- get |
| 28 | + put (n1d + 1, n2d) |
25 | 29 |
|
26 |
| -class Monad g => SampleGet g where |
27 |
| - getSample1d :: g (Sample1D r) |
28 |
| - getSample2d :: g (Sample2D r) |
29 |
| --- mkSampled :: g a -> a |
30 |
| --} |
31 |
| - |
32 |
| ---class Monad s => Sampler s where |
33 |
| --- data Sampled s :: * -> * |
34 |
| --- getSample1d :: s (Sampled s Float) |
35 |
| --- getSample2d :: (Sampled s (Float, Float)) |
36 |
| --- evs :: s -> Sampled s a -> a |
37 |
| - |
38 |
| -newtype Sampled s m a = Sampled { runSampled :: s -> m a } |
39 |
| - |
40 |
| -class Monad m => Sampler m where |
41 |
| - getSample1d :: m (Sampled s m Float) |
42 |
| - getSample2d :: m (Sampled s m (Float, Float)) |
| 30 | + return $ do |
| 31 | + fs <- gets fst |
| 32 | + if length fs < n1d |
| 33 | + then return (fs !! n1d) |
| 34 | + else gets snd >>= (lift . MWC.uniform) |
| 35 | + |
| 36 | +test2d :: (Monad m, PrimMonad r, MonadState (SRunState r) (StratRun r)) => Stratified m (StratRun r (Float, Float)) |
| 37 | +test2d = do |
| 38 | + f1 <- mk1d |
| 39 | + f2 <- mk1d |
43 | 40 |
|
| 41 | + return $ do |
| 42 | + v1 <- f1 |
| 43 | + v2 <- f2 |
| 44 | + return (v1, v2) |
| 45 | + |
| 46 | +runStratified :: (PrimMonad m) => Stratified m a -> m a |
| 47 | +runStratified (Stratified c) = do |
| 48 | + (StratRun x, (n1d, n2d)) <- runStateT c (0, 0) |
| 49 | + gen <- MWC.create |
| 50 | + |
| 51 | + --evalStateT x ([0.5 :: Float], undefined) |
| 52 | + undefined |
44 | 53 |
|
45 |
| -test r = do |
46 |
| - s1d <- getSample1d -- gives a monadic action that when run provides a Float |
47 |
| --- s2d <- getSample2d -- gives a monadic action that when run provides a (Float, Float) |
| 54 | +main :: IO () |
| 55 | +main = do |
48 | 56 |
|
49 |
| - return $ Sampled $ do |
50 |
| - s1d >>= \x -> if (x > 0.5) |
51 |
| - then return $ Just "yo" |
52 |
| - else return Nothing |
| 57 | + return () |
0 commit comments