<commit_before>{-# LANGUAGE TypeFamilies     #-}
{-# LANGUAGE KindSignatures   #-}
{-# LANGUAGE FlexibleContexts #-}
module Raaz.Core.Random
  ( Random(..)
  , RandomDev(..)
  , PRG(..), fromPRG
  , openPseudoRandom
  , openEntropy
  ) where

import Control.Monad   (void)
import Foreign.Ptr     (castPtr)
import Foreign.Storable(Storable, peek)
import System.IO (openBinaryFile, Handle, IOMode(..))

import Raaz.Core.ByteSource(ByteSource, fillBytes)
import Raaz.Core.Util.Ptr  (byteSize, allocaBuffer)

-- | The class that captures pseudo-random generators. Essentially the
-- a pseudo-random generator (PRG) is a byte sources that can be
-- seeded.
class ByteSource prg => PRG prg where

  -- Associated type that captures the seed for the PRG.
  type Seed prg :: *

  -- | Creates a new pseudo-random generators
  newPRG :: Seed prg -> IO prg

  -- | Re-seeding the prg.
  reseed :: prg -> Seed prg -> IO ()

-- | Generate a random element of a type which is an instance of `Storable`.
fromPRG :: (PRG prg, Storable a) => prg -> IO a
fromPRG =  go undefined
  where go       :: (PRG prg, Storable a) => a -> prg -> IO a
        go w prg = let sz = byteSize w in
          allocaBuffer sz $ \ ptr -> do
            void $ fillBytes sz prg ptr
            peek $ castPtr ptr

newtype RandomDev = RandomDev Handle

openPseudoRandom :: IO RandomDev
openPseudoRandom = do
  handle <- openBinaryFile "/dev/urandom" ReadMode
  return $ RandomDev handle

openEntropy :: IO RandomDev
openEntropy = do
  handle <- openBinaryFile "/dev/random" ReadMode
  return $ RandomDev handle

class Random a where
  gen :: RandomDev -> IO a<commit_msg>got rid of the random class.<commit_after>{-# LANGUAGE TypeFamilies     #-}
{-# LANGUAGE KindSignatures   #-}
{-# LANGUAGE FlexibleContexts #-}
module Raaz.Core.Random
  ( Random(..)
  , RandomDev(..)
  , PRG(..), fromPRG
  , openPseudoRandom
  , openEntropy
  ) where

import Control.Monad   (void)
import Foreign.Ptr     (castPtr)
import Foreign.Storable(Storable, peek)
import System.IO (openBinaryFile, Handle, IOMode(..))

import Raaz.Core.ByteSource(ByteSource, fillBytes)
import Raaz.Core.Util.Ptr  (byteSize, allocaBuffer)

-- | The class that captures pseudo-random generators. Essentially the
-- a pseudo-random generator (PRG) is a byte sources that can be
-- seeded.
class ByteSource prg => PRG prg where

  -- Associated type that captures the seed for the PRG.
  type Seed prg :: *

  -- | Creates a new pseudo-random generators
  newPRG :: Seed prg -> IO prg

  -- | Re-seeding the prg.
  reseed :: prg -> Seed prg -> IO ()

-- | Generate a random element of a type which is an instance of `Storable`.
fromPRG :: (PRG prg, Storable a) => prg -> IO a
fromPRG =  go undefined
  where go       :: (PRG prg, Storable a) => a -> prg -> IO a
        go w prg = let sz = byteSize w in
          allocaBuffer sz $ \ ptr -> do
            void $ fillBytes sz prg ptr
            peek $ castPtr ptr

newtype RandomDev = RandomDev Handle

openPseudoRandom :: IO RandomDev
openPseudoRandom = do
  handle <- openBinaryFile "/dev/urandom" ReadMode
  return $ RandomDev handle

openEntropy :: IO RandomDev
openEntropy = do
  handle <- openBinaryFile "/dev/random" ReadMode
  return $ RandomDev handle
{'fraction_non_alphanumeric': 0.10667903525046382, 'fraction_numerical': 0.0, 'mean_word_length': 2.9693251533742333, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4502451', 'n_tokens_mistral': 1073, 'n_tokens_neox': 1038, 'n_words': 362}