{-# LANGUAGE CPP, RankNTypes, ScopedTypeVariables,
GeneralizedNewtypeDeriving, DeriveDataTypeable #-}
module Test.ChasingBottoms.ContinuousFunctions
(
function
, functionTo
, PatternMatch(..)
, GenTransformer
, MakePM
, MakeResult
, transform
, lift'
, arbitrary'
, choose'
, elements'
, oneof'
, frequency'
, sized'
, resize'
, match
, flat
, finiteListOf
, infiniteListOf
, listOf
) where
import Test.QuickCheck
hiding ( (><)
, listOf
, infiniteListOf
, function
)
import Test.QuickCheck.Arbitrary (CoArbitrary(..))
import Test.QuickCheck.Gen.Unsafe (promote)
import Data.Sequence as Seq
import Data.Foldable as Seq (foldr)
import Prelude as P hiding (concat)
import Test.ChasingBottoms.IsBottom
import Control.Monad
import Control.Monad.Reader
import Control.Applicative
import Control.Arrow
import System.Random
import Data.Generics
import qualified Data.List as L
import qualified Test.ChasingBottoms.SemanticOrd as O
function :: MakePM a -> MakeResult b -> Gen (a -> b)
function :: MakePM a -> MakeResult b -> Gen (a -> b)
function MakePM a
makePM MakeResult b
makeResult =
(a -> Gen b) -> Gen (a -> b)
forall (m :: * -> *) a. Monad m => m (Gen a) -> Gen (m a)
promote ((a -> Gen b) -> Gen (a -> b)) -> (a -> Gen b) -> Gen (a -> b)
forall a b. (a -> b) -> a -> b
$ \a
a -> MakeResult b -> PatternMatches -> Gen b
forall a. MakeResult a -> PatternMatches -> Gen a
run MakeResult b
makeResult (PatternMatch -> PatternMatches
forall a. a -> Seq a
singleton (PatternMatch -> PatternMatches) -> PatternMatch -> PatternMatches
forall a b. (a -> b) -> a -> b
$ MakePM a
makePM a
a)
functionTo :: Data a => MakeResult b -> Gen (a -> b)
functionTo :: MakeResult b -> Gen (a -> b)
functionTo = MakePM a -> MakeResult b -> Gen (a -> b)
forall a b. MakePM a -> MakeResult b -> Gen (a -> b)
function MakePM a
forall a. Data a => MakePM a
match
data PatternMatch
= PatternMatch { PatternMatch -> forall a. Gen a -> Gen a
apply :: GenTransformer
, PatternMatch -> PatternMatches
more :: Seq PatternMatch
}
type GenTransformer = forall a. Gen a -> Gen a
newtype GenTransformer' = GenT GenTransformer
type MakePM a = a -> PatternMatch
matchFlat :: CoArbitrary a => MakePM a
matchFlat :: MakePM a
matchFlat a
a = PatternMatch :: (forall a. Gen a -> Gen a) -> PatternMatches -> PatternMatch
PatternMatch { apply :: forall a. Gen a -> Gen a
apply = a -> Gen a -> Gen a
forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary a
a, more :: PatternMatches
more = PatternMatches
forall a. Seq a
Seq.empty }
data Tree a
= Branch (Tree a) (Tree a)
| Leaf a
deriving (Int -> Tree a -> ShowS
[Tree a] -> ShowS
Tree a -> String
(Int -> Tree a -> ShowS)
-> (Tree a -> String) -> ([Tree a] -> ShowS) -> Show (Tree a)
forall a. Show a => Int -> Tree a -> ShowS
forall a. Show a => [Tree a] -> ShowS
forall a. Show a => Tree a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Tree a] -> ShowS
$cshowList :: forall a. Show a => [Tree a] -> ShowS
show :: Tree a -> String
$cshow :: forall a. Show a => Tree a -> String
showsPrec :: Int -> Tree a -> ShowS
$cshowsPrec :: forall a. Show a => Int -> Tree a -> ShowS
Show, Typeable, Typeable (Tree a)
DataType
Constr
Typeable (Tree a)
-> (forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Tree a -> c (Tree a))
-> (forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Tree a))
-> (Tree a -> Constr)
-> (Tree a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c (Tree a)))
-> (forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (Tree a)))
-> ((forall b. Data b => b -> b) -> Tree a -> Tree a)
-> (forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Tree a -> r)
-> (forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Tree a -> r)
-> (forall u. (forall d. Data d => d -> u) -> Tree a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> Tree a -> u)
-> (forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> Tree a -> m (Tree a))
-> (forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Tree a -> m (Tree a))
-> (forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Tree a -> m (Tree a))
-> Data (Tree a)
Tree a -> DataType
Tree a -> Constr
(forall d. Data d => c (t d)) -> Maybe (c (Tree a))
(forall b. Data b => b -> b) -> Tree a -> Tree a
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Tree a -> c (Tree a)
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Tree a)
forall a. Data a => Typeable (Tree a)
forall a. Data a => Tree a -> DataType
forall a. Data a => Tree a -> Constr
forall a.
Data a =>
(forall b. Data b => b -> b) -> Tree a -> Tree a
forall a u.
Data a =>
Int -> (forall d. Data d => d -> u) -> Tree a -> u
forall a u. Data a => (forall d. Data d => d -> u) -> Tree a -> [u]
forall a r r'.
Data a =>
(r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Tree a -> r
forall a r r'.
Data a =>
(r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Tree a -> r
forall a (m :: * -> *).
(Data a, Monad m) =>
(forall d. Data d => d -> m d) -> Tree a -> m (Tree a)
forall a (m :: * -> *).
(Data a, MonadPlus m) =>
(forall d. Data d => d -> m d) -> Tree a -> m (Tree a)
forall a (c :: * -> *).
Data a =>
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Tree a)
forall a (c :: * -> *).
Data a =>
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Tree a -> c (Tree a)
forall a (t :: * -> *) (c :: * -> *).
(Data a, Typeable t) =>
(forall d. Data d => c (t d)) -> Maybe (c (Tree a))
forall a (t :: * -> * -> *) (c :: * -> *).
(Data a, Typeable t) =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (Tree a))
forall a.
Typeable a
-> (forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
(r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
(r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall u. Int -> (forall d. Data d => d -> u) -> Tree a -> u
forall u. (forall d. Data d => d -> u) -> Tree a -> [u]
forall r r'.
(r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Tree a -> r
forall r r'.
(r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Tree a -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> Tree a -> m (Tree a)
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Tree a -> m (Tree a)
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Tree a)
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Tree a -> c (Tree a)
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c (Tree a))
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (Tree a))
$cLeaf :: Constr
$cBranch :: Constr
$tTree :: DataType
gmapMo :: (forall d. Data d => d -> m d) -> Tree a -> m (Tree a)
$cgmapMo :: forall a (m :: * -> *).
(Data a, MonadPlus m) =>
(forall d. Data d => d -> m d) -> Tree a -> m (Tree a)
gmapMp :: (forall d. Data d => d -> m d) -> Tree a -> m (Tree a)
$cgmapMp :: forall a (m :: * -> *).
(Data a, MonadPlus m) =>
(forall d. Data d => d -> m d) -> Tree a -> m (Tree a)
gmapM :: (forall d. Data d => d -> m d) -> Tree a -> m (Tree a)
$cgmapM :: forall a (m :: * -> *).
(Data a, Monad m) =>
(forall d. Data d => d -> m d) -> Tree a -> m (Tree a)
gmapQi :: Int -> (forall d. Data d => d -> u) -> Tree a -> u
$cgmapQi :: forall a u.
Data a =>
Int -> (forall d. Data d => d -> u) -> Tree a -> u
gmapQ :: (forall d. Data d => d -> u) -> Tree a -> [u]
$cgmapQ :: forall a u. Data a => (forall d. Data d => d -> u) -> Tree a -> [u]
gmapQr :: (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Tree a -> r
$cgmapQr :: forall a r r'.
Data a =>
(r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Tree a -> r
gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Tree a -> r
$cgmapQl :: forall a r r'.
Data a =>
(r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Tree a -> r
gmapT :: (forall b. Data b => b -> b) -> Tree a -> Tree a
$cgmapT :: forall a.
Data a =>
(forall b. Data b => b -> b) -> Tree a -> Tree a
dataCast2 :: (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (Tree a))
$cdataCast2 :: forall a (t :: * -> * -> *) (c :: * -> *).
(Data a, Typeable t) =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (Tree a))
dataCast1 :: (forall d. Data d => c (t d)) -> Maybe (c (Tree a))
$cdataCast1 :: forall a (t :: * -> *) (c :: * -> *).
(Data a, Typeable t) =>
(forall d. Data d => c (t d)) -> Maybe (c (Tree a))
dataTypeOf :: Tree a -> DataType
$cdataTypeOf :: forall a. Data a => Tree a -> DataType
toConstr :: Tree a -> Constr
$ctoConstr :: forall a. Data a => Tree a -> Constr
gunfold :: (forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Tree a)
$cgunfold :: forall a (c :: * -> *).
Data a =>
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Tree a)
gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Tree a -> c (Tree a)
$cgfoldl :: forall a (c :: * -> *).
Data a =>
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Tree a -> c (Tree a)
$cp1Data :: forall a. Data a => Typeable (Tree a)
Data)
matchTree :: MakePM a -> MakePM (Tree a)
matchTree :: MakePM a -> MakePM (Tree a)
matchTree MakePM a
match Tree a
t = PatternMatch :: (forall a. Gen a -> Gen a) -> PatternMatches -> PatternMatch
PatternMatch { apply :: forall a. Gen a -> Gen a
apply = Tree a -> Gen a -> Gen a
forall a a. Tree a -> Gen a -> Gen a
toVariant Tree a
t, more :: PatternMatches
more = Tree a -> PatternMatches
moreT Tree a
t }
where
toVariant :: Tree a -> Gen a -> Gen a
toVariant (Branch {}) = Integer -> Gen a -> Gen a
forall n a. Integral n => n -> Gen a -> Gen a
variant Integer
1
toVariant (Leaf {}) = Integer -> Gen a -> Gen a
forall n a. Integral n => n -> Gen a -> Gen a
variant Integer
0
moreT :: Tree a -> PatternMatches
moreT (Branch Tree a
l Tree a
r) = [PatternMatch] -> PatternMatches
forall a. [a] -> Seq a
fromList [MakePM a -> MakePM (Tree a)
forall a. MakePM a -> MakePM (Tree a)
matchTree MakePM a
match Tree a
l, MakePM a -> MakePM (Tree a)
forall a. MakePM a -> MakePM (Tree a)
matchTree MakePM a
match Tree a
r]
moreT (Leaf a
x) = PatternMatch -> PatternMatches
forall a. a -> Seq a
singleton (MakePM a
match a
x)
match :: forall a. Data a => MakePM a
match :: MakePM a
match a
x = PatternMatch :: (forall a. Gen a -> Gen a) -> PatternMatches -> PatternMatch
PatternMatch
{ apply :: forall a. Gen a -> Gen a
apply = a -> Gen a -> Gen a
forall a b. Data a => a -> Gen b -> Gen b
toVariant a
x
, more :: PatternMatches
more = a -> PatternMatches
forall a. Data a => a -> PatternMatches
more a
x
}
where
toVariant :: forall a b. Data a => a -> Gen b -> Gen b
toVariant :: a -> Gen b -> Gen b
toVariant a
x = case Constr -> ConstrRep
constrRep (a -> Constr
forall a. Data a => a -> Constr
toConstr a
x) of
AlgConstr Int
n -> Int -> Gen b -> Gen b
forall n a. Integral n => n -> Gen a -> Gen a
variant (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)
IntConstr Integer
i -> Integer -> Gen b -> Gen b
forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary Integer
i
FloatConstr Rational
d -> Rational -> Gen b -> Gen b
forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary Rational
d
CharConstr Char
s -> String -> Gen b -> Gen b
forall a. String -> a
nonBottomError String
"match: Encountered CharConstr."
more :: forall a. Data a => a -> Seq PatternMatch
more :: a -> PatternMatches
more = (PatternMatch -> PatternMatches -> PatternMatches)
-> PatternMatches
-> (forall a. Data a => MakePM a)
-> a
-> PatternMatches
forall a r r'.
Data a =>
(r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r
gmapQr PatternMatch -> PatternMatches -> PatternMatches
forall a. a -> Seq a -> Seq a
(<|) PatternMatches
forall a. Seq a
Seq.empty forall a. Data a => MakePM a
match
newtype MakeResult a
= MR { MakeResult a -> ReaderT PatternMatches Gen a
unMR :: ReaderT PatternMatches Gen a }
deriving (a -> MakeResult b -> MakeResult a
(a -> b) -> MakeResult a -> MakeResult b
(forall a b. (a -> b) -> MakeResult a -> MakeResult b)
-> (forall a b. a -> MakeResult b -> MakeResult a)
-> Functor MakeResult
forall a b. a -> MakeResult b -> MakeResult a
forall a b. (a -> b) -> MakeResult a -> MakeResult b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: a -> MakeResult b -> MakeResult a
$c<$ :: forall a b. a -> MakeResult b -> MakeResult a
fmap :: (a -> b) -> MakeResult a -> MakeResult b
$cfmap :: forall a b. (a -> b) -> MakeResult a -> MakeResult b
Functor, Functor MakeResult
a -> MakeResult a
Functor MakeResult
-> (forall a. a -> MakeResult a)
-> (forall a b.
MakeResult (a -> b) -> MakeResult a -> MakeResult b)
-> (forall a b c.
(a -> b -> c) -> MakeResult a -> MakeResult b -> MakeResult c)
-> (forall a b. MakeResult a -> MakeResult b -> MakeResult b)
-> (forall a b. MakeResult a -> MakeResult b -> MakeResult a)
-> Applicative MakeResult
MakeResult a -> MakeResult b -> MakeResult b
MakeResult a -> MakeResult b -> MakeResult a
MakeResult (a -> b) -> MakeResult a -> MakeResult b
(a -> b -> c) -> MakeResult a -> MakeResult b -> MakeResult c
forall a. a -> MakeResult a
forall a b. MakeResult a -> MakeResult b -> MakeResult a
forall a b. MakeResult a -> MakeResult b -> MakeResult b
forall a b. MakeResult (a -> b) -> MakeResult a -> MakeResult b
forall a b c.
(a -> b -> c) -> MakeResult a -> MakeResult b -> MakeResult c
forall (f :: * -> *).
Functor f
-> (forall a. a -> f a)
-> (forall a b. f (a -> b) -> f a -> f b)
-> (forall a b c. (a -> b -> c) -> f a -> f b -> f c)
-> (forall a b. f a -> f b -> f b)
-> (forall a b. f a -> f b -> f a)
-> Applicative f
<* :: MakeResult a -> MakeResult b -> MakeResult a
$c<* :: forall a b. MakeResult a -> MakeResult b -> MakeResult a
*> :: MakeResult a -> MakeResult b -> MakeResult b
$c*> :: forall a b. MakeResult a -> MakeResult b -> MakeResult b
liftA2 :: (a -> b -> c) -> MakeResult a -> MakeResult b -> MakeResult c
$cliftA2 :: forall a b c.
(a -> b -> c) -> MakeResult a -> MakeResult b -> MakeResult c
<*> :: MakeResult (a -> b) -> MakeResult a -> MakeResult b
$c<*> :: forall a b. MakeResult (a -> b) -> MakeResult a -> MakeResult b
pure :: a -> MakeResult a
$cpure :: forall a. a -> MakeResult a
$cp1Applicative :: Functor MakeResult
Applicative, Applicative MakeResult
a -> MakeResult a
Applicative MakeResult
-> (forall a b.
MakeResult a -> (a -> MakeResult b) -> MakeResult b)
-> (forall a b. MakeResult a -> MakeResult b -> MakeResult b)
-> (forall a. a -> MakeResult a)
-> Monad MakeResult
MakeResult a -> (a -> MakeResult b) -> MakeResult b
MakeResult a -> MakeResult b -> MakeResult b
forall a. a -> MakeResult a
forall a b. MakeResult a -> MakeResult b -> MakeResult b
forall a b. MakeResult a -> (a -> MakeResult b) -> MakeResult b
forall (m :: * -> *).
Applicative m
-> (forall a b. m a -> (a -> m b) -> m b)
-> (forall a b. m a -> m b -> m b)
-> (forall a. a -> m a)
-> Monad m
return :: a -> MakeResult a
$creturn :: forall a. a -> MakeResult a
>> :: MakeResult a -> MakeResult b -> MakeResult b
$c>> :: forall a b. MakeResult a -> MakeResult b -> MakeResult b
>>= :: MakeResult a -> (a -> MakeResult b) -> MakeResult b
$c>>= :: forall a b. MakeResult a -> (a -> MakeResult b) -> MakeResult b
$cp1Monad :: Applicative MakeResult
Monad)
type PatternMatches = Seq PatternMatch
run :: MakeResult a -> PatternMatches -> Gen a
run :: MakeResult a -> PatternMatches -> Gen a
run MakeResult a
mr PatternMatches
pms = ReaderT PatternMatches Gen a -> PatternMatches -> Gen a
forall r (m :: * -> *) a. ReaderT r m a -> r -> m a
runReaderT (MakeResult a -> ReaderT PatternMatches Gen a
forall a. MakeResult a -> ReaderT PatternMatches Gen a
unMR MakeResult a
mr) PatternMatches
pms
lift' :: Gen a -> MakeResult a
lift' :: Gen a -> MakeResult a
lift' Gen a
gen = ReaderT PatternMatches Gen a -> MakeResult a
forall a. ReaderT PatternMatches Gen a -> MakeResult a
MR (ReaderT PatternMatches Gen a -> MakeResult a)
-> ReaderT PatternMatches Gen a -> MakeResult a
forall a b. (a -> b) -> a -> b
$ Gen a -> ReaderT PatternMatches Gen a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift Gen a
gen
getPMs :: MakeResult PatternMatches
getPMs :: MakeResult PatternMatches
getPMs = ReaderT PatternMatches Gen PatternMatches
-> MakeResult PatternMatches
forall a. ReaderT PatternMatches Gen a -> MakeResult a
MR ReaderT PatternMatches Gen PatternMatches
forall r (m :: * -> *). MonadReader r m => m r
ask
withPMs :: (PatternMatches -> Gen a) -> MakeResult a
withPMs :: (PatternMatches -> Gen a) -> MakeResult a
withPMs PatternMatches -> Gen a
f = do
PatternMatches
pms <- MakeResult PatternMatches
getPMs
Gen a -> MakeResult a
forall a. Gen a -> MakeResult a
lift' (Gen a -> MakeResult a) -> Gen a -> MakeResult a
forall a b. (a -> b) -> a -> b
$ PatternMatches -> Gen a
f PatternMatches
pms
transform :: MakeResult a -> MakeResult a
transform :: MakeResult a -> MakeResult a
transform MakeResult a
makeResult = (PatternMatches -> Gen a) -> MakeResult a
forall a. (PatternMatches -> Gen a) -> MakeResult a
withPMs ((PatternMatches -> Gen a) -> MakeResult a)
-> (PatternMatches -> Gen a) -> MakeResult a
forall a b. (a -> b) -> a -> b
$ \PatternMatches
pms -> do
(GenT forall a. Gen a -> Gen a
trans, PatternMatches
keep) <- PatternMatches -> Gen (GenTransformer', PatternMatches)
getMatches PatternMatches
pms
Gen a -> Gen a
forall a. Gen a -> Gen a
trans (MakeResult a -> PatternMatches -> Gen a
forall a. MakeResult a -> PatternMatches -> Gen a
run MakeResult a
makeResult PatternMatches
keep)
getMatches :: Seq PatternMatch -> Gen (GenTransformer', Seq PatternMatch)
getMatches :: PatternMatches -> Gen (GenTransformer', PatternMatches)
getMatches PatternMatches
pms = do
(PatternMatches
_, PatternMatches
pms') <- Int -> PatternMatches -> Gen (PatternMatches, PatternMatches)
forall a. Int -> Seq a -> Gen (Seq a, Seq a)
partition' Int
9 PatternMatches
pms
(PatternMatches
use, PatternMatches
keep) <- Int -> PatternMatches -> Gen (PatternMatches, PatternMatches)
forall a. Int -> Seq a -> Gen (Seq a, Seq a)
partition' Int
2 PatternMatches
pms'
let transform :: Gen a -> Gen a
transform = Seq (Gen a -> Gen a) -> Gen a -> Gen a
forall a. Seq (a -> a) -> a -> a
compose (Seq (Gen a -> Gen a) -> Gen a -> Gen a)
-> Seq (Gen a -> Gen a) -> Gen a -> Gen a
forall a b. (a -> b) -> a -> b
$ (PatternMatch -> Gen a -> Gen a)
-> PatternMatches -> Seq (Gen a -> Gen a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\PatternMatch
pm -> PatternMatch -> forall a. Gen a -> Gen a
apply PatternMatch
pm) PatternMatches
use
further :: PatternMatches
further = Seq PatternMatches -> PatternMatches
forall a. Seq (Seq a) -> Seq a
concat (Seq PatternMatches -> PatternMatches)
-> Seq PatternMatches -> PatternMatches
forall a b. (a -> b) -> a -> b
$ (PatternMatch -> PatternMatches)
-> PatternMatches -> Seq PatternMatches
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap PatternMatch -> PatternMatches
more PatternMatches
use
if PatternMatches -> Bool
forall a. Seq a -> Bool
Seq.null PatternMatches
further then
(GenTransformer', PatternMatches)
-> Gen (GenTransformer', PatternMatches)
forall (m :: * -> *) a. Monad m => a -> m a
return ((forall a. Gen a -> Gen a) -> GenTransformer'
GenT forall a. Gen a -> Gen a
transform, PatternMatches
keep)
else do
(GenT forall a. Gen a -> Gen a
transform', PatternMatches
keep') <- PatternMatches -> Gen (GenTransformer', PatternMatches)
getMatches PatternMatches
further
(GenTransformer', PatternMatches)
-> Gen (GenTransformer', PatternMatches)
forall (m :: * -> *) a. Monad m => a -> m a
return ((forall a. Gen a -> Gen a) -> GenTransformer'
GenT (Gen a -> Gen a
forall a. Gen a -> Gen a
transform (Gen a -> Gen a) -> (Gen a -> Gen a) -> Gen a -> Gen a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Gen a -> Gen a
forall a. Gen a -> Gen a
transform'), PatternMatches
keep PatternMatches -> PatternMatches -> PatternMatches
forall a. Seq a -> Seq a -> Seq a
>< PatternMatches
keep')
concat :: Seq (Seq a) -> Seq a
concat :: Seq (Seq a) -> Seq a
concat = (Seq a -> Seq a -> Seq a) -> Seq a -> Seq (Seq a) -> Seq a
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
Seq.foldr Seq a -> Seq a -> Seq a
forall a. Seq a -> Seq a -> Seq a
(><) Seq a
forall a. Seq a
Seq.empty
compose :: Seq (a -> a) -> a -> a
compose :: Seq (a -> a) -> a -> a
compose = ((a -> a) -> (a -> a) -> a -> a)
-> (a -> a) -> Seq (a -> a) -> a -> a
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
Seq.foldr (a -> a) -> (a -> a) -> a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
(.) a -> a
forall a. a -> a
id
partition' :: Int -> Seq a -> Gen (Seq a, Seq a)
partition' :: Int -> Seq a -> Gen (Seq a, Seq a)
partition' Int
freq Seq a
ss = case Seq a -> ViewL a
forall a. Seq a -> ViewL a
viewl Seq a
ss of
ViewL a
EmptyL -> (Seq a, Seq a) -> Gen (Seq a, Seq a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Seq a
forall a. Seq a
Seq.empty, Seq a
forall a. Seq a
Seq.empty)
a
x :< Seq a
xs -> do
(Seq a
ys, Seq a
zs) <- Int -> Seq a -> Gen (Seq a, Seq a)
forall a. Int -> Seq a -> Gen (Seq a, Seq a)
partition' Int
freq Seq a
xs
[(Int, Gen (Seq a, Seq a))] -> Gen (Seq a, Seq a)
forall a. [(Int, Gen a)] -> Gen a
frequency [ (Int
1, (Seq a, Seq a) -> Gen (Seq a, Seq a)
forall (m :: * -> *) a. Monad m => a -> m a
return (a
x a -> Seq a -> Seq a
forall a. a -> Seq a -> Seq a
<| Seq a
ys, Seq a
zs))
, (Int
freq, (Seq a, Seq a) -> Gen (Seq a, Seq a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Seq a
ys, a
x a -> Seq a -> Seq a
forall a. a -> Seq a -> Seq a
<| Seq a
zs))
]
arbitrary' :: Arbitrary a => MakeResult a
arbitrary' :: MakeResult a
arbitrary' = Gen a -> MakeResult a
forall a. Gen a -> MakeResult a
lift' Gen a
forall a. Arbitrary a => Gen a
arbitrary
choose' :: Random a => (a, a) -> MakeResult a
choose' :: (a, a) -> MakeResult a
choose' = Gen a -> MakeResult a
forall a. Gen a -> MakeResult a
lift' (Gen a -> MakeResult a)
-> ((a, a) -> Gen a) -> (a, a) -> MakeResult a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a, a) -> Gen a
forall a. Random a => (a, a) -> Gen a
choose
elements' :: [a] -> MakeResult a
elements' :: [a] -> MakeResult a
elements' = Gen a -> MakeResult a
forall a. Gen a -> MakeResult a
lift' (Gen a -> MakeResult a) -> ([a] -> Gen a) -> [a] -> MakeResult a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [a] -> Gen a
forall a. [a] -> Gen a
elements
oneof' :: [MakeResult a] -> MakeResult a
oneof' :: [MakeResult a] -> MakeResult a
oneof' [MakeResult a]
mrs = (PatternMatches -> Gen a) -> MakeResult a
forall a. (PatternMatches -> Gen a) -> MakeResult a
withPMs ((PatternMatches -> Gen a) -> MakeResult a)
-> (PatternMatches -> Gen a) -> MakeResult a
forall a b. (a -> b) -> a -> b
$ \PatternMatches
pms -> [Gen a] -> Gen a
forall a. [Gen a] -> Gen a
oneof ([Gen a] -> Gen a) -> [Gen a] -> Gen a
forall a b. (a -> b) -> a -> b
$ (MakeResult a -> Gen a) -> [MakeResult a] -> [Gen a]
forall a b. (a -> b) -> [a] -> [b]
map (\MakeResult a
mr -> MakeResult a -> PatternMatches -> Gen a
forall a. MakeResult a -> PatternMatches -> Gen a
run MakeResult a
mr PatternMatches
pms) [MakeResult a]
mrs
frequency' :: [(Int, MakeResult a)] -> MakeResult a
frequency' :: [(Int, MakeResult a)] -> MakeResult a
frequency' [(Int, MakeResult a)]
freqs = (PatternMatches -> Gen a) -> MakeResult a
forall a. (PatternMatches -> Gen a) -> MakeResult a
withPMs ((PatternMatches -> Gen a) -> MakeResult a)
-> (PatternMatches -> Gen a) -> MakeResult a
forall a b. (a -> b) -> a -> b
$ \PatternMatches
pms ->
[(Int, Gen a)] -> Gen a
forall a. [(Int, Gen a)] -> Gen a
frequency ([(Int, Gen a)] -> Gen a) -> [(Int, Gen a)] -> Gen a
forall a b. (a -> b) -> a -> b
$ ((Int, MakeResult a) -> (Int, Gen a))
-> [(Int, MakeResult a)] -> [(Int, Gen a)]
forall a b. (a -> b) -> [a] -> [b]
map (Int -> Int
forall a. a -> a
id (Int -> Int)
-> (MakeResult a -> Gen a) -> (Int, MakeResult a) -> (Int, Gen a)
forall (a :: * -> * -> *) b c b' c'.
Arrow a =>
a b c -> a b' c' -> a (b, b') (c, c')
*** (MakeResult a -> PatternMatches -> Gen a)
-> PatternMatches -> MakeResult a -> Gen a
forall a b c. (a -> b -> c) -> b -> a -> c
flip MakeResult a -> PatternMatches -> Gen a
forall a. MakeResult a -> PatternMatches -> Gen a
run PatternMatches
pms) [(Int, MakeResult a)]
freqs
sized' :: (Int -> MakeResult a) -> MakeResult a
sized' :: (Int -> MakeResult a) -> MakeResult a
sized' Int -> MakeResult a
mr = (PatternMatches -> Gen a) -> MakeResult a
forall a. (PatternMatches -> Gen a) -> MakeResult a
withPMs ((PatternMatches -> Gen a) -> MakeResult a)
-> (PatternMatches -> Gen a) -> MakeResult a
forall a b. (a -> b) -> a -> b
$ \PatternMatches
pms -> (Int -> Gen a) -> Gen a
forall a. (Int -> Gen a) -> Gen a
sized (\Int
size -> MakeResult a -> PatternMatches -> Gen a
forall a. MakeResult a -> PatternMatches -> Gen a
run (Int -> MakeResult a
mr Int
size) PatternMatches
pms)
resize' :: Int -> MakeResult a -> MakeResult a
resize' :: Int -> MakeResult a -> MakeResult a
resize' Int
n MakeResult a
mr = (PatternMatches -> Gen a) -> MakeResult a
forall a. (PatternMatches -> Gen a) -> MakeResult a
withPMs ((PatternMatches -> Gen a) -> MakeResult a)
-> (PatternMatches -> Gen a) -> MakeResult a
forall a b. (a -> b) -> a -> b
$ \PatternMatches
pms -> Int -> Gen a -> Gen a
forall a. Int -> Gen a -> Gen a
resize Int
n (MakeResult a -> PatternMatches -> Gen a
forall a. MakeResult a -> PatternMatches -> Gen a
run MakeResult a
mr PatternMatches
pms)
flat :: Arbitrary a => MakeResult a
flat :: MakeResult a
flat = MakeResult a -> MakeResult a
forall a. MakeResult a -> MakeResult a
transform (MakeResult a -> MakeResult a) -> MakeResult a -> MakeResult a
forall a b. (a -> b) -> a -> b
$
[(Int, MakeResult a)] -> MakeResult a
forall a. [(Int, MakeResult a)] -> MakeResult a
frequency' [ (Int
1, a -> MakeResult a
forall (m :: * -> *) a. Monad m => a -> m a
return a
forall a. a
bottom)
, (Int
9, MakeResult a
forall a. Arbitrary a => MakeResult a
arbitrary')
]
finiteListOf :: MakeResult a -> MakeResult [a]
finiteListOf :: MakeResult a -> MakeResult [a]
finiteListOf MakeResult a
makeResult = (Int -> MakeResult [a]) -> MakeResult [a]
forall a. (Int -> MakeResult a) -> MakeResult a
sized' Int -> MakeResult [a]
forall t. (Eq t, Num t) => t -> MakeResult [a]
list
where
list :: t -> MakeResult [a]
list t
size = MakeResult [a] -> MakeResult [a]
forall a. MakeResult a -> MakeResult a
transform (MakeResult [a] -> MakeResult [a])
-> MakeResult [a] -> MakeResult [a]
forall a b. (a -> b) -> a -> b
$
if t
size t -> t -> Bool
forall a. Eq a => a -> a -> Bool
== t
0 then
MakeResult [a]
forall a. MakeResult [a]
baseCase
else
[(Int, MakeResult [a])] -> MakeResult [a]
forall a. [(Int, MakeResult a)] -> MakeResult a
frequency' [ (Int
1, MakeResult [a]
forall a. MakeResult [a]
baseCase)
, (Int
9, (a -> [a] -> [a])
-> MakeResult a -> MakeResult [a] -> MakeResult [a]
forall (m :: * -> *) a1 a2 r.
Monad m =>
(a1 -> a2 -> r) -> m a1 -> m a2 -> m r
liftM2 (:) MakeResult a
makeResult (t -> MakeResult [a]
list (t
size t -> t -> t
forall a. Num a => a -> a -> a
- t
1)))
]
baseCase :: MakeResult [a]
baseCase =
[(Int, MakeResult [a])] -> MakeResult [a]
forall a. [(Int, MakeResult a)] -> MakeResult a
frequency' [(Int
1, [a] -> MakeResult [a]
forall (m :: * -> *) a. Monad m => a -> m a
return [a]
forall a. a
bottom), (Int
1, [a] -> MakeResult [a]
forall (m :: * -> *) a. Monad m => a -> m a
return [])]
infiniteListOf :: MakeResult a -> MakeResult [a]
infiniteListOf :: MakeResult a -> MakeResult [a]
infiniteListOf MakeResult a
makeResult = MakeResult [a] -> MakeResult [a]
forall a. MakeResult a -> MakeResult a
transform (MakeResult [a] -> MakeResult [a])
-> MakeResult [a] -> MakeResult [a]
forall a b. (a -> b) -> a -> b
$
(a -> [a] -> [a])
-> MakeResult a -> MakeResult [a] -> MakeResult [a]
forall (m :: * -> *) a1 a2 r.
Monad m =>
(a1 -> a2 -> r) -> m a1 -> m a2 -> m r
liftM2 (:) MakeResult a
makeResult (MakeResult a -> MakeResult [a]
forall a. MakeResult a -> MakeResult [a]
infiniteListOf MakeResult a
makeResult)
listOf :: MakeResult a -> MakeResult [a]
listOf :: MakeResult a -> MakeResult [a]
listOf MakeResult a
makeResult = MakeResult [a] -> MakeResult [a]
forall a. MakeResult a -> MakeResult a
transform (MakeResult [a] -> MakeResult [a])
-> MakeResult [a] -> MakeResult [a]
forall a b. (a -> b) -> a -> b
$
[MakeResult [a]] -> MakeResult [a]
forall a. [MakeResult a] -> MakeResult a
oneof' [ MakeResult a -> MakeResult [a]
forall a. MakeResult a -> MakeResult [a]
finiteListOf MakeResult a
makeResult
, MakeResult a -> MakeResult [a]
forall a. MakeResult a -> MakeResult [a]
infiniteListOf MakeResult a
makeResult
]
makeResult :: forall a. Data a => MakeResult a
makeResult :: MakeResult a
makeResult = MakeResult a -> MakeResult a
forall a. MakeResult a -> MakeResult a
transform ([(Int, MakeResult a)] -> MakeResult a
forall a. [(Int, MakeResult a)] -> MakeResult a
frequency' ([(Int, MakeResult a)] -> MakeResult a)
-> [(Int, MakeResult a)] -> MakeResult a
forall a b. (a -> b) -> a -> b
$ (Int
1, a -> MakeResult a
forall (m :: * -> *) a. Monad m => a -> m a
return a
forall a. a
bottom) (Int, MakeResult a)
-> [(Int, MakeResult a)] -> [(Int, MakeResult a)]
forall a. a -> [a] -> [a]
: [(Int, MakeResult a)]
others)
where
others :: [(Int, MakeResult a)]
others = case DataType -> DataRep
dataTypeRep (a -> DataType
forall a. Data a => a -> DataType
dataTypeOf (a
forall a. HasCallStack => a
undefined :: a)) of
AlgRep [Constr]
constrs ->
(Constr -> (Int, MakeResult a))
-> [Constr] -> [(Int, MakeResult a)]
forall a b. (a -> b) -> [a] -> [b]
map (Double -> Constr -> (Int, MakeResult a)
forall a a.
(RealFrac a, Integral a) =>
a -> Constr -> (a, MakeResult a)
handle ([Constr] -> Double
forall i a. Num i => [a] -> i
L.genericLength [Constr]
constrs)) [Constr]
constrs
DataRep
IntRep -> [(Int
9, MakeResult Integer -> MakeResult a
forall (f :: * -> *) a b.
(Functor f, Typeable a, Typeable b) =>
f a -> f b
cast' (MakeResult Integer
forall a. Arbitrary a => MakeResult a
arbitrary' :: MakeResult Integer))]
DataRep
FloatRep -> [(Int
9, MakeResult Double -> MakeResult a
forall (f :: * -> *) a b.
(Functor f, Typeable a, Typeable b) =>
f a -> f b
cast' (MakeResult Double
forall a. Arbitrary a => MakeResult a
arbitrary' :: MakeResult Double))]
DataRep
CharRep -> String -> [(Int, MakeResult a)]
forall a. String -> a
nonBottomError String
"makeResult: CharRep."
DataRep
NoRep -> String -> [(Int, MakeResult a)]
forall a. String -> a
nonBottomError String
"makeResult: NoRep."
handle :: a -> Constr -> (a, MakeResult a)
handle a
noConstrs Constr
con =
(a
freq, (forall d. Data d => MakeResult d) -> Constr -> MakeResult a
forall (m :: * -> *) a.
(Monad m, Data a) =>
(forall d. Data d => m d) -> Constr -> m a
fromConstrM forall d. Data d => MakeResult d
makeResult Constr
con :: MakeResult a)
where noArgs :: Int
noArgs = a -> Int
GenericQ Int
glength (Constr -> a
forall a. Data a => Constr -> a
fromConstr Constr
con :: a)
freq :: a
freq = a
1 a -> a -> a
forall a. Ord a => a -> a -> a
`max` a -> a
forall a b. (RealFrac a, Integral b) => a -> b
ceiling (a
9 a -> a -> a
forall a. Fractional a => a -> a -> a
/ a
noConstrs)
cast' :: f a -> f b
cast' f a
gen = ((a -> b) -> f a -> f b) -> f a -> (a -> b) -> f b
forall a b c. (a -> b -> c) -> b -> a -> c
flip (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap f a
gen ((a -> b) -> f b) -> (a -> b) -> f b
forall a b. (a -> b) -> a -> b
$ \a
x -> case a -> Maybe b
forall a b. (Typeable a, Typeable b) => a -> Maybe b
cast a
x of
Just b
x' -> b
x'
Maybe b
Nothing -> String -> b
forall a. String -> a
nonBottomError (String -> b) -> String -> b
forall a b. (a -> b) -> a -> b
$
String
"makeResult: Cannot handle Int and Float." String -> ShowS
forall a. [a] -> [a] -> [a]
++
String
" Use Integer or Double instead."