packages feed

random-class (empty) → 0.1.0.0

raw patch · 5 files changed

+127/−0 lines, 5 filesdep +basedep +primitivedep +transformerssetup-changed

Dependencies added: base, primitive, transformers

Files

+ LICENSE view
@@ -0,0 +1,30 @@+Copyright (c) 2017, M Farkas-Dyck++All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++    * Redistributions of source code must retain the above copyright+      notice, this list of conditions and the following disclaimer.++    * Redistributions in binary form must reproduce the above+      copyright notice, this list of conditions and the following+      disclaimer in the documentation and/or other materials provided+      with the distribution.++    * Neither the name of M Farkas-Dyck nor the names of other+      contributors may be used to endorse or promote products derived+      from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ Random.hs view
@@ -0,0 +1,43 @@+{-# LANGUAGE DefaultSignatures #-}++module Random where++import Control.Monad.Primitive+import Control.Monad.Trans.Reader+import qualified Control.Monad.Trans.State as M+import Data.Primitive.MutVar+import Data.Tuple (swap)+import Data.Void++import Util++class Gen g where+    type Mut s g = m | m -> s g+    type instance Mut s g = MutVar s g+    type Native g+    uniformNative :: M.State g (Native g)+    uniformNativeM :: PrimMonad m => ReaderT (Mut (PrimState m) g) m (Native g)+    default uniformNativeM :: (Mut (PrimState m) g ~ MutVar (PrimState m) g,+                               PrimMonad m) => ReaderT (Mut (PrimState m) g) m (Native g)+    uniformNativeM = ReaderT $ flip atomicModifyMutVar $ swap . M.runState uniformNative++class Split g where+    split :: g -> (g, g)++class Uniform b a where+    liftUniform :: Monad m => m b -> m a++instance Uniform a a where liftUniform = id++uniform :: (Gen g, Uniform (Native g) a) => M.State g a+uniform = liftUniform uniformNative++uniformM :: (Gen g, Uniform (Native g) a, PrimMonad m) => ReaderT (Mut (PrimState m) g) m a+uniformM = liftUniform uniformNativeM++instance {-# OVERLAPPABLE #-} (Bounded a, Enum a, Bounded b, Enum b) => Uniform b a where+    liftUniform = fmap (toEnum' . foldr (\ m n -> card @b * n + fromEnum' m) 0)+                . replicateA ((card @a + card @b - 1) `div` card @b)++instance Uniform Void a where+    liftUniform = fmap $ \ case
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ Util.hs view
@@ -0,0 +1,18 @@+{-# LANGUAGE AllowAmbiguousTypes #-}++module Util where++import qualified Data.List as L+import Numeric.Natural++replicateA :: Applicative p => Natural -> p a -> p [a]+replicateA n = sequenceA . L.genericReplicate n++card :: ∀ a . (Bounded a, Enum a) => Natural+card = L.genericLength [minBound @a..]+            +fromEnum' :: (Bounded a, Enum a) => a -> Natural+fromEnum' a = L.genericLength [minBound..a] - 1++toEnum' :: (Bounded a, Enum a) => Natural -> a+toEnum' = L.genericIndex [minBound..]
+ random-class.cabal view
@@ -0,0 +1,34 @@+name:                random-class+version:             0.1.0.0+synopsis:            Class of random value generation+-- description:         +license:             BSD3+license-file:        LICENSE+author:              M Farkas-Dyck+maintainer:          strake888@gmail.com+-- copyright:           +category:            Random+build-type:          Simple+cabal-version:       >=1.10++library+  exposed-modules:     Random+  other-modules:       Util+  -- other-extensions:    +  build-depends:       base >=4.9 && <5+                     , primitive+                     , transformers+  -- hs-source-dirs:      +  default-language:    Haskell2010+  default-extensions:  UnicodeSyntax+                     , LambdaCase+                     , EmptyCase+                     , TypeApplications+                     , ScopedTypeVariables+                     , PolyKinds+                     , TypeFamilies+                     , TypeFamilyDependencies+                     , MultiParamTypeClasses+                     , FlexibleContexts+                     , FlexibleInstances+  ghc-options:         -Wall -Wno-name-shadowing