Skip to content

Instantly share code, notes, and snippets.

@zyla
Created February 22, 2018 07:52
Show Gist options
  • Save zyla/c4086b90453b510e47dfc1cf0bfa483e to your computer and use it in GitHub Desktop.
Save zyla/c4086b90453b510e47dfc1cf0bfa483e to your computer and use it in GitHub Desktop.
diff --git a/src/Jack/Seed.purs b/src/Jack/Seed.purs
index db308ac..73db0a3 100644
--- a/src/Jack/Seed.purs
+++ b/src/Jack/Seed.purs
@@ -19,20 +19,24 @@ module Jack.Seed (
, splitSeed
) where
+import Prelude
+
import Control.Monad.Eff (Eff)
+import Control.Monad.Eff.Console (log)
import Control.Monad.Eff.Random (RANDOM, randomInt)
-
+import Control.Monad.Eff.Unsafe (unsafePerformEff)
import Data.Int.Bits ((.&.))
import Data.Int53 (Int53)
import Data.Int53 as Int53
import Data.Tuple (Tuple(..))
-import Prelude
-
-- | Splittable random number generator.
data Seed =
Seed Int Int
+instance showSeed :: Show Seed where
+ show (Seed x y) = "(Seed " <> show x <> " " <> show y <> ")"
+
-- | Create a new 'Seed' from a 32-bit integer.
mkSeed :: Int -> Seed
mkSeed s0 =
@@ -125,8 +129,7 @@ nextInt53 lo hi seed =
-- On average, log q / log b more random values will be generated than
-- the minimum.
--
- b =
- Int53.fromInt nextMax - Int53.fromInt nextMin + one
+ b = Int53.fromInt nextMax - Int53.fromInt nextMin + one
q =
Int53.fromInt 1000
@@ -139,20 +142,36 @@ nextInt53 lo hi seed =
-- Generate random values until we exceed the target magnitude.
loop mag v0 seed0 =
- if mag >= magtgt then
+ trace ("-- loop " <> show mag <> " " <> show v0 <> " " <> show seed0) $ \_ ->
+ if mag * b >= magtgt then
Tuple v0 seed0
else
- case nextSeed seed of
+ case nextSeed seed0 of
Tuple x seed1 ->
+ trace ("--- x=" <> show x <> " seed1=" <> show seed1) $ \_ ->
let
v1 =
- v0 * b + (Int53.fromInt x - Int53.fromInt nextMin)
+ v0 * b + Int53.fromInt x
in
loop (mag * b) v1 seed1
in
case loop one zero seed of
Tuple v seedN ->
- Tuple (lo + (v `mod` k)) seedN
+ let result = (lo + (v `mod` k)) in
+ trace ("seed=" <> show seed <>
+ "\n b=" <> show b <> " q=" <> show q <>
+ "\n k=" <> show k <> " magtgt=" <> show magtgt <>
+ "\n lo=" <> show lo <>
+ "\n v=" <> show v <>
+ "\n (v mod k)=" <> show (v `mod` k) <>
+ "\n result=" <> show result <>
+ "\n toInt result=" <> show (Int53.toInt result)
+ ) $ \_ ->
+ Tuple result seedN
+
+trace s x = unsafePerformEff $ do
+ log s
+ pure (x unit)
-- | Splits a random number generator in to two.
splitSeed :: Seed -> Tuple Seed Seed
diff --git a/test/Foo.purs b/test/Foo.purs
index 287adb0..8fc4ac7 100644
--- a/test/Foo.purs
+++ b/test/Foo.purs
@@ -1,20 +1,20 @@
module Test.Foo where
-import Control.Lazy (fix)
+import Prelude
+import Control.Lazy (fix)
+import Control.Monad.Eff.Console (logShow)
+import Control.Monad.Eff.Unsafe (unsafePerformEff)
import Data.Array as Array
import Data.Foldable (elem)
import Data.Generic (class Generic, gShow, gEq)
import Data.Maybe (Maybe(..))
import Data.String (Pattern(..), toCharArray, fromCharArray, contains)
import Data.String as String
-
import Jack.Combinators (boundedInt, chooseInt, elements, oneOfRec, arrayOf)
import Jack.Gen (Gen, reshrink)
import Jack.Property (Property, property, forAll)
-import Prelude
-
data Exp =
Lit Int
| Var String
@@ -102,3 +102,16 @@ prop_even_strings_end_with_evens =
property false
Just x ->
property $ elem x evens
+
+{-
+prop_dummy :: Property
+prop_dummy =
+ forAll boundedInt \x ->
+ property $ unsafePerformEff $ logShow x $> true
+ -}
+
+{-
+prop_dummy :: Property
+prop_dummy =
+ forAll boundedInt \x ->
+ property $ unsafePerformEff $ logShow x $> true-}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment