module Test.QuickCheck.DynamicLogic.CanGenerate (canGenerate) where

import System.IO.Unsafe
import Test.QuickCheck

-- | @canGenerate prob g p@
--   returns @False@ if we are sure @Prob(g generates x satisfying p) >= prob@
--   otherwise @True@ (and we know such an x can be generated).
canGenerate :: Double -> Gen a -> (a -> Bool) -> Bool
canGenerate :: Double -> Gen a -> (a -> Bool) -> Bool
canGenerate Double
prob Gen a
g a -> Bool
p = IO Bool -> Bool
forall a. IO a -> a
unsafePerformIO (IO Bool -> Bool) -> IO Bool -> Bool
forall a b. (a -> b) -> a -> b
$ Double -> IO Bool
tryToGenerate Double
1
 where
  tryToGenerate :: Double -> IO Bool
tryToGenerate Double
luck
    | Double
luck Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
< Double
eps = Bool -> IO Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
    | Bool
otherwise = do
        a
x <- Gen a -> IO a
forall a. Gen a -> IO a
generate Gen a
g
        if a -> Bool
p a
x
          then Bool -> IO Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True
          else Double -> IO Bool
tryToGenerate (Double
luck Double -> Double -> Double
forall a. Num a => a -> a -> a
* (Double
1 Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
prob))

  -- Our confidence level is 1-eps
  eps :: Double
eps = Double
1.0e-9