Created
February 10, 2017 19:44
-
-
Save cartazio/32fdede6090abe397a484d867b2fe088 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
newtype RandomT m a = RandomT# { unRandomT# :: (SplitMix64 -> m (a , SplitMix64) ) } | |
instance Functor m => Functor (RandomT m) where | |
fmap = \ f (RandomT# mf) -> | |
RandomT# $ \ seed -> | |
fmap (\(a,s) -> (f a, s) ) $ mf seed | |
instance Applicative m => Applicative (RandomT m) where | |
pure = \ x -> RandomT# $ \ s -> pure ( x , s ) | |
(<*>) = \ (RandomT# frmb) (RandomT# rma) -> RandomT# $ \ s -> | |
let (# !fseed, !maseed #) = splitGeneratorSplitMix s | |
mfOldSeed= frmb fseed | |
mArgNewSeed = rma maseed | |
in (fmap (\(f,_s)-> \(x,newSeed)-> (f x, newSeed) ) mfOldSeed) | |
<*> mArgNewSeed | |
instance Monad m => Monad (RandomT m) where | |
(>>=) = \ (RandomT# ma) f -> RandomT# $ \ s -> | |
let (# fseed, nextSeed #) = splitGeneratorSplitMix s | |
in | |
do | |
(a,_boring) <- ma fseed | |
unRandomT# (f a) nextSeed |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment