{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE TypeApplications #-}
{-# OPTIONS_GHC -Wno-dodgy-imports #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
module Evaluation.Builtins.Costing where
import PlutusCore
import PlutusCore.Evaluation.Machine.CostStream
import PlutusCore.Evaluation.Machine.ExBudget
import PlutusCore.Evaluation.Machine.ExBudgetStream
import PlutusCore.Evaluation.Machine.ExMemory
import PlutusCore.Evaluation.Machine.ExMemoryUsage
import PlutusCore.Generators.QuickCheck.Builtin (magnitudesPositive)
import PlutusCore.Generators.QuickCheck.Utils
import Control.Monad (forM_)
import Data.Bifunctor
import Data.Int
import Data.List
import Data.Maybe
import Data.SatInt
import Test.Cardano.Base.QuickCheck qualified as BaseQC
import Test.QuickCheck.Gen
import Test.Tasty
import Test.Tasty.HUnit (testCase, (@?=))
import Test.Tasty.QuickCheck hiding (Some (..))
deriving newtype instance Foldable NonEmptyList
eqCostStream :: CostStream -> CostStream -> Bool
eqCostStream :: CostStream -> CostStream -> Bool
eqCostStream (CostLast CostingInteger
cost1) (CostLast CostingInteger
cost2) = CostingInteger
cost1 CostingInteger -> CostingInteger -> Bool
forall a. Eq a => a -> a -> Bool
== CostingInteger
cost2
eqCostStream (CostCons CostingInteger
cost1 CostStream
costs1) (CostCons CostingInteger
cost2 CostStream
costs2) =
CostingInteger
cost1 CostingInteger -> CostingInteger -> Bool
forall a. Eq a => a -> a -> Bool
== CostingInteger
cost2 Bool -> Bool -> Bool
&& CostStream -> CostStream -> Bool
eqCostStream CostStream
costs1 CostStream
costs2
eqCostStream CostStream
_ CostStream
_ = Bool
False
fromCostList :: NonEmptyList CostingInteger -> CostStream
fromCostList :: NonEmptyList CostingInteger -> CostStream
fromCostList (NonEmpty []) = [Char] -> CostStream
forall a. HasCallStack => [Char] -> a
error [Char]
"Panic: an empty non-empty list"
fromCostList (NonEmpty (CostingInteger
cost0 : [CostingInteger]
costs0)) = CostingInteger -> [CostingInteger] -> CostStream
go CostingInteger
cost0 [CostingInteger]
costs0
where
go :: CostingInteger -> [CostingInteger] -> CostStream
go CostingInteger
cost [] = CostingInteger -> CostStream
CostLast CostingInteger
cost
go CostingInteger
cost (CostingInteger
cost' : [CostingInteger]
costs) = CostingInteger -> CostStream -> CostStream
CostCons CostingInteger
cost (CostStream -> CostStream) -> CostStream -> CostStream
forall a b. (a -> b) -> a -> b
$ CostingInteger -> [CostingInteger] -> CostStream
go CostingInteger
cost' [CostingInteger]
costs
toCostList :: CostStream -> NonEmptyList CostingInteger
toCostList :: CostStream -> NonEmptyList CostingInteger
toCostList = [CostingInteger] -> NonEmptyList CostingInteger
forall a. [a] -> NonEmptyList a
NonEmpty ([CostingInteger] -> NonEmptyList CostingInteger)
-> (CostStream -> [CostingInteger])
-> CostStream
-> NonEmptyList CostingInteger
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CostStream -> [CostingInteger]
go
where
go :: CostStream -> [CostingInteger]
go (CostLast CostingInteger
cost) = [CostingInteger
cost]
go (CostCons CostingInteger
cost CostStream
costs) = CostingInteger
cost CostingInteger -> [CostingInteger] -> [CostingInteger]
forall a. a -> [a] -> [a]
: CostStream -> [CostingInteger]
go CostStream
costs
toExBudgetList :: ExBudgetStream -> NonEmptyList ExBudget
toExBudgetList :: ExBudgetStream -> NonEmptyList ExBudget
toExBudgetList = [ExBudget] -> NonEmptyList ExBudget
forall a. [a] -> NonEmptyList a
NonEmpty ([ExBudget] -> NonEmptyList ExBudget)
-> (ExBudgetStream -> [ExBudget])
-> ExBudgetStream
-> NonEmptyList ExBudget
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ExBudgetStream -> [ExBudget]
go
where
go :: ExBudgetStream -> [ExBudget]
go (ExBudgetLast ExBudget
budget) = [ExBudget
budget]
go (ExBudgetCons ExBudget
budget ExBudgetStream
budgets) = ExBudget
budget ExBudget -> [ExBudget] -> [ExBudget]
forall a. a -> [a] -> [a]
: ExBudgetStream -> [ExBudget]
go ExBudgetStream
budgets
magnitudes :: [(SatInt, SatInt)]
magnitudes :: [(CostingInteger, CostingInteger)]
magnitudes =
((Integer, Integer) -> (CostingInteger, CostingInteger))
-> [(Integer, Integer)] -> [(CostingInteger, CostingInteger)]
forall a b. (a -> b) -> [a] -> [b]
map ((Integer -> CostingInteger)
-> (Integer -> CostingInteger)
-> (Integer, Integer)
-> (CostingInteger, CostingInteger)
forall a b c d. (a -> b) -> (c -> d) -> (a, c) -> (b, d)
forall (p :: * -> * -> *) a b c d.
Bifunctor p =>
(a -> b) -> (c -> d) -> p a c -> p b d
bimap Integer -> CostingInteger
forall a. Num a => Integer -> a
fromInteger Integer -> CostingInteger
forall a. Num a => Integer -> a
fromInteger)
([(Integer, Integer)] -> [(CostingInteger, CostingInteger)])
-> (Integer -> [(Integer, Integer)])
-> Integer
-> [(CostingInteger, CostingInteger)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((Integer
0, Integer
0) (Integer, Integer) -> [(Integer, Integer)] -> [(Integer, Integer)]
forall a. a -> [a] -> [a]
:)
([(Integer, Integer)] -> [(Integer, Integer)])
-> (Integer -> [(Integer, Integer)])
-> Integer
-> [(Integer, Integer)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Integer -> Integer) -> Integer -> [(Integer, Integer)]
magnitudesPositive (Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* Integer
10)
(Integer -> [(CostingInteger, CostingInteger)])
-> Integer -> [(CostingInteger, CostingInteger)]
forall a b. (a -> b) -> a -> b
$ CostingInteger -> Integer
forall a. Num a => CostingInteger -> a
fromSatInt (CostingInteger
forall a. Bounded a => a
maxBound :: SatInt)
toRange :: SatInt -> (SatInt, SatInt)
toRange :: CostingInteger -> (CostingInteger, CostingInteger)
toRange CostingInteger
cost =
(CostingInteger, CostingInteger)
-> Maybe (CostingInteger, CostingInteger)
-> (CostingInteger, CostingInteger)
forall a. a -> Maybe a -> a
fromMaybe ([Char] -> (CostingInteger, CostingInteger)
forall a. HasCallStack => [Char] -> a
error ([Char] -> (CostingInteger, CostingInteger))
-> [Char] -> (CostingInteger, CostingInteger)
forall a b. (a -> b) -> a -> b
$ [Char]
"Panic: an unexpected cost: " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ CostingInteger -> [Char]
forall a. Show a => a -> [Char]
show CostingInteger
cost) (Maybe (CostingInteger, CostingInteger)
-> (CostingInteger, CostingInteger))
-> Maybe (CostingInteger, CostingInteger)
-> (CostingInteger, CostingInteger)
forall a b. (a -> b) -> a -> b
$
((CostingInteger, CostingInteger) -> Bool)
-> [(CostingInteger, CostingInteger)]
-> Maybe (CostingInteger, CostingInteger)
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Maybe a
find ((CostingInteger -> CostingInteger -> Bool
forall a. Ord a => a -> a -> Bool
>= CostingInteger
cost) (CostingInteger -> Bool)
-> ((CostingInteger, CostingInteger) -> CostingInteger)
-> (CostingInteger, CostingInteger)
-> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (CostingInteger, CostingInteger) -> CostingInteger
forall a b. (a, b) -> b
snd) [(CostingInteger, CostingInteger)]
magnitudes
chooseSatInt :: (SatInt, SatInt) -> Gen SatInt
chooseSatInt :: (CostingInteger, CostingInteger) -> Gen CostingInteger
chooseSatInt = (Int64 -> CostingInteger) -> Gen Int64 -> Gen CostingInteger
forall a b. (a -> b) -> Gen a -> Gen b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Int64 -> CostingInteger
unsafeToSatInt (Gen Int64 -> Gen CostingInteger)
-> ((CostingInteger, CostingInteger) -> Gen Int64)
-> (CostingInteger, CostingInteger)
-> Gen CostingInteger
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Int64, Int64) -> Gen Int64
forall a. Random a => (a, a) -> Gen a
choose ((Int64, Int64) -> Gen Int64)
-> ((CostingInteger, CostingInteger) -> (Int64, Int64))
-> (CostingInteger, CostingInteger)
-> Gen Int64
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (CostingInteger -> Int64)
-> (CostingInteger -> Int64)
-> (CostingInteger, CostingInteger)
-> (Int64, Int64)
forall a b c d. (a -> b) -> (c -> d) -> (a, c) -> (b, d)
forall (p :: * -> * -> *) a b c d.
Bifunctor p =>
(a -> b) -> (c -> d) -> p a c -> p b d
bimap CostingInteger -> Int64
unSatInt CostingInteger -> Int64
unSatInt
instance Arbitrary SatInt where
arbitrary :: Gen CostingInteger
arbitrary = [(Int, Gen CostingInteger)] -> Gen CostingInteger
forall a. HasCallStack => [(Int, Gen a)] -> Gen a
frequency ([(Int, Gen CostingInteger)] -> Gen CostingInteger)
-> ([Gen CostingInteger] -> [(Int, Gen CostingInteger)])
-> [Gen CostingInteger]
-> Gen CostingInteger
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Int] -> [Gen CostingInteger] -> [(Int, Gen CostingInteger)]
forall a b. [a] -> [b] -> [(a, b)]
zip [Int]
freqs ([Gen CostingInteger] -> [(Int, Gen CostingInteger)])
-> ([Gen CostingInteger] -> [Gen CostingInteger])
-> [Gen CostingInteger]
-> [(Int, Gen CostingInteger)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Gen CostingInteger] -> [Gen CostingInteger]
forall a. [a] -> [a]
reverse ([Gen CostingInteger] -> Gen CostingInteger)
-> [Gen CostingInteger] -> Gen CostingInteger
forall a b. (a -> b) -> a -> b
$ ((CostingInteger, CostingInteger) -> Gen CostingInteger)
-> [(CostingInteger, CostingInteger)] -> [Gen CostingInteger]
forall a b. (a -> b) -> [a] -> [b]
map (CostingInteger, CostingInteger) -> Gen CostingInteger
chooseSatInt [(CostingInteger, CostingInteger)]
magnitudes
where
freqs :: [Int]
freqs = (Double -> Int) -> [Double] -> [Int]
forall a b. (a -> b) -> [a] -> [b]
map Double -> Int
forall b. Integral b => Double -> b
forall a b. (RealFrac a, Integral b) => a -> b
floor ([Double] -> [Int]) -> [Double] -> [Int]
forall a b. (a -> b) -> a -> b
$ (Double -> Double) -> Double -> [Double]
forall a. (a -> a) -> a -> [a]
iterate (Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
1.3) (Double
2 :: Double)
shrink :: CostingInteger -> [CostingInteger]
shrink = (Int64 -> CostingInteger) -> [Int64] -> [CostingInteger]
forall a b. (a -> b) -> [a] -> [b]
map Int64 -> CostingInteger
forall a b. (Integral a, Num b) => a -> b
fromIntegral ([Int64] -> [CostingInteger])
-> (CostingInteger -> [Int64])
-> CostingInteger
-> [CostingInteger]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Arbitrary a => a -> [a]
shrink @Int64 (Int64 -> [Int64])
-> (CostingInteger -> Int64) -> CostingInteger -> [Int64]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CostingInteger -> Int64
forall a. Num a => CostingInteger -> a
fromSatInt
instance Arbitrary CostStream where
arbitrary :: Gen CostStream
arbitrary =
[(Int, Gen CostStream)] -> Gen CostStream
forall a. HasCallStack => [(Int, Gen a)] -> Gen a
frequency
[
(Int
1, CostingInteger -> CostStream
CostLast (CostingInteger -> CostStream)
-> Gen CostingInteger -> Gen CostStream
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Gen CostingInteger
forall a. Arbitrary a => Gen a
arbitrary)
, (Int
6, NonEmptyList CostingInteger -> CostStream
fromCostList (NonEmptyList CostingInteger -> CostStream)
-> Gen (NonEmptyList CostingInteger) -> Gen CostStream
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Gen (NonEmptyList CostingInteger)
forall a. Arbitrary a => Gen a
arbitrary)
]
shrink :: CostStream -> [CostStream]
shrink (CostLast CostingInteger
cost) = (CostingInteger -> CostStream) -> [CostingInteger] -> [CostStream]
forall a b. (a -> b) -> [a] -> [b]
map CostingInteger -> CostStream
CostLast ([CostingInteger] -> [CostStream])
-> [CostingInteger] -> [CostStream]
forall a b. (a -> b) -> a -> b
$ CostingInteger -> [CostingInteger]
forall a. Arbitrary a => a -> [a]
shrink CostingInteger
cost
shrink (CostCons CostingInteger
cost CostStream
costs) =
CostingInteger -> CostStream
CostLast CostingInteger
cost CostStream -> [CostStream] -> [CostStream]
forall a. a -> [a] -> [a]
: CostStream
costs CostStream -> [CostStream] -> [CostStream]
forall a. a -> [a] -> [a]
: do
(CostStream
costs', CostingInteger
cost') <- (CostStream, CostingInteger) -> [(CostStream, CostingInteger)]
forall a. Arbitrary a => a -> [a]
shrink (CostStream
costs, CostingInteger
cost)
CostStream -> [CostStream]
forall a. a -> [a]
forall (f :: * -> *) a. Applicative f => a -> f a
pure (CostStream -> [CostStream]) -> CostStream -> [CostStream]
forall a b. (a -> b) -> a -> b
$ CostingInteger -> CostStream -> CostStream
CostCons CostingInteger
cost' CostStream
costs'
instance CoArbitrary SatInt where
coarbitrary :: forall b. CostingInteger -> Gen b -> Gen b
coarbitrary = Int64 -> Gen b -> Gen b
forall b. Int64 -> Gen b -> Gen b
forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary (Int64 -> Gen b -> Gen b)
-> (CostingInteger -> Int64) -> CostingInteger -> Gen b -> Gen b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Num a => CostingInteger -> a
fromSatInt @Int64
instance Function SatInt where
function :: forall b. (CostingInteger -> b) -> CostingInteger :-> b
function = (CostingInteger -> Int64)
-> (Int64 -> CostingInteger)
-> (CostingInteger -> b)
-> CostingInteger :-> b
forall b a c.
Function b =>
(a -> b) -> (b -> a) -> (a -> c) -> a :-> c
functionMap CostingInteger -> Int64
forall a. Num a => CostingInteger -> a
fromSatInt ((Int64 -> CostingInteger)
-> (CostingInteger -> b) -> CostingInteger :-> b)
-> (Int64 -> CostingInteger)
-> (CostingInteger -> b)
-> CostingInteger :-> b
forall a b. (a -> b) -> a -> b
$ forall a b. (Integral a, Num b) => a -> b
fromIntegral @Int64
checkEqualsVia :: Show a => (a -> a -> Bool) -> a -> a -> Property
checkEqualsVia :: forall a. Show a => (a -> a -> Bool) -> a -> a -> Property
checkEqualsVia a -> a -> Bool
eq a
x a
y =
[Char] -> Bool -> Property
forall prop. Testable prop => [Char] -> prop -> Property
counterexample (a -> [Char]
forall a. Show a => a -> [Char]
show a
x [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ Bool -> [Char]
interpret Bool
res [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ a -> [Char]
forall a. Show a => a -> [Char]
show a
y) Bool
res
where
res :: Bool
res = a -> a -> Bool
eq a
x a
y
interpret :: Bool -> [Char]
interpret Bool
True = [Char]
" === "
interpret Bool
False = [Char]
" =/= "
bottom :: a
bottom :: forall a. a
bottom = [Char] -> a
forall a. HasCallStack => [Char] -> a
error [Char]
"this value wasn't supposed to be forced"
test_magnitudes :: TestTree
test_magnitudes :: TestTree
test_magnitudes =
[Char] -> Bool -> TestTree
forall a. Testable a => [Char] -> a -> TestTree
testProperty [Char]
"magnitudes" (Bool -> TestTree) -> Bool -> TestTree
forall a b. (a -> b) -> a -> b
$
let check :: (a, a) -> (a, a) -> Bool
check (a
0, a
0) (a
1, a
10) = Bool
True
check (a
_, a
hi1) (a
lo2, a
hi2) = a
hi1 a -> a -> a
forall a. Num a => a -> a -> a
+ a
1 a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
lo2 Bool -> Bool -> Bool
&& a
hi1 a -> a -> a
forall a. Num a => a -> a -> a
* a
10 a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
hi2
in [Bool] -> Bool
forall (t :: * -> *). Foldable t => t Bool -> Bool
and
[ (CostingInteger, CostingInteger) -> CostingInteger
forall a b. (a, b) -> a
fst ([(CostingInteger, CostingInteger)]
-> (CostingInteger, CostingInteger)
forall a. HasCallStack => [a] -> a
head [(CostingInteger, CostingInteger)]
magnitudes) CostingInteger -> CostingInteger -> Bool
forall a. Eq a => a -> a -> Bool
== CostingInteger
0
, (CostingInteger, CostingInteger) -> CostingInteger
forall a b. (a, b) -> b
snd ([(CostingInteger, CostingInteger)]
-> (CostingInteger, CostingInteger)
forall a. HasCallStack => [a] -> a
last [(CostingInteger, CostingInteger)]
magnitudes) CostingInteger -> CostingInteger -> Bool
forall a. Eq a => a -> a -> Bool
== CostingInteger
forall a. Bounded a => a
maxBound
, [Bool] -> Bool
forall (t :: * -> *). Foldable t => t Bool -> Bool
and ([Bool] -> Bool)
-> ([(CostingInteger, CostingInteger)] -> [Bool])
-> [(CostingInteger, CostingInteger)]
-> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((CostingInteger, CostingInteger)
-> (CostingInteger, CostingInteger) -> Bool)
-> [(CostingInteger, CostingInteger)]
-> [(CostingInteger, CostingInteger)]
-> [Bool]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith (CostingInteger, CostingInteger)
-> (CostingInteger, CostingInteger) -> Bool
forall {a} {a}.
(Eq a, Eq a, Num a, Num a) =>
(a, a) -> (a, a) -> Bool
check [(CostingInteger, CostingInteger)]
magnitudes ([(CostingInteger, CostingInteger)] -> Bool)
-> [(CostingInteger, CostingInteger)] -> Bool
forall a b. (a -> b) -> a -> b
$ [(CostingInteger, CostingInteger)]
-> [(CostingInteger, CostingInteger)]
forall a. HasCallStack => [a] -> [a]
tail [(CostingInteger, CostingInteger)]
magnitudes
]
test_CostStreamDistribution :: TestTree
test_CostStreamDistribution :: TestTree
test_CostStreamDistribution =
[Char] -> Property -> TestTree
forall a. Testable a => [Char] -> a -> TestTree
testProperty [Char]
"distribution of the generated CostStream values" (Property -> TestTree)
-> ((CostStream -> Property) -> Property)
-> (CostStream -> Property)
-> TestTree
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> (CostStream -> Property) -> Property
forall prop. Testable prop => Int -> prop -> Property
BaseQC.withNumTests Int
10000 ((CostStream -> Property) -> TestTree)
-> (CostStream -> Property) -> TestTree
forall a b. (a -> b) -> a -> b
$
\CostStream
costs ->
let costsSum :: CostingInteger
costsSum = CostStream -> CostingInteger
sumCostStream CostStream
costs
(CostingInteger
low, CostingInteger
high) = CostingInteger -> (CostingInteger, CostingInteger)
toRange CostingInteger
costsSum
in [Char] -> Bool -> Property
forall prop. Testable prop => [Char] -> prop -> Property
label (CostingInteger -> [Char]
forall a. Show a => a -> [Char]
show CostingInteger
low [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
" - " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ CostingInteger -> [Char]
forall a. Show a => a -> [Char]
show CostingInteger
high) Bool
True
test_toCostListRoundtrip :: TestTree
test_toCostListRoundtrip :: TestTree
test_toCostListRoundtrip =
[Char] -> Property -> TestTree
forall a. Testable a => [Char] -> a -> TestTree
testProperty [Char]
"fromCostList cancels toCostList" (Property -> TestTree)
-> ((CostStream -> Property) -> Property)
-> (CostStream -> Property)
-> TestTree
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> (CostStream -> Property) -> Property
forall prop. Testable prop => Int -> prop -> Property
BaseQC.withNumTests Int
5000 ((CostStream -> Property) -> TestTree)
-> (CostStream -> Property) -> TestTree
forall a b. (a -> b) -> a -> b
$ \CostStream
costs ->
(CostStream -> CostStream -> Bool)
-> CostStream -> CostStream -> Property
forall a. Show a => (a -> a -> Bool) -> a -> a -> Property
checkEqualsVia
CostStream -> CostStream -> Bool
eqCostStream
(NonEmptyList CostingInteger -> CostStream
fromCostList (NonEmptyList CostingInteger -> CostStream)
-> NonEmptyList CostingInteger -> CostStream
forall a b. (a -> b) -> a -> b
$ CostStream -> NonEmptyList CostingInteger
toCostList CostStream
costs)
CostStream
costs
test_fromCostListRoundtrip :: TestTree
test_fromCostListRoundtrip :: TestTree
test_fromCostListRoundtrip =
[Char] -> Property -> TestTree
forall a. Testable a => [Char] -> a -> TestTree
testProperty [Char]
"toCostList cancels fromCostList" (Property -> TestTree)
-> ((NonEmptyList CostingInteger -> Property) -> Property)
-> (NonEmptyList CostingInteger -> Property)
-> TestTree
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> (NonEmptyList CostingInteger -> Property) -> Property
forall prop. Testable prop => Int -> prop -> Property
BaseQC.withNumTests Int
5000 ((NonEmptyList CostingInteger -> Property) -> TestTree)
-> (NonEmptyList CostingInteger -> Property) -> TestTree
forall a b. (a -> b) -> a -> b
$ \NonEmptyList CostingInteger
costs ->
CostStream -> NonEmptyList CostingInteger
toCostList (NonEmptyList CostingInteger -> CostStream
fromCostList NonEmptyList CostingInteger
costs)
NonEmptyList CostingInteger
-> NonEmptyList CostingInteger -> Property
forall a. (Eq a, Show a) => a -> a -> Property
=== NonEmptyList CostingInteger
costs
test_unconsCostRoundtrip :: TestTree
test_unconsCostRoundtrip :: TestTree
test_unconsCostRoundtrip =
[Char] -> Property -> TestTree
forall a. Testable a => [Char] -> a -> TestTree
testProperty [Char]
"reconsCost cancels unconsCost" (Property -> TestTree)
-> ((CostStream -> Property) -> Property)
-> (CostStream -> Property)
-> TestTree
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> (CostStream -> Property) -> Property
forall prop. Testable prop => Int -> prop -> Property
BaseQC.withNumTests Int
5000 ((CostStream -> Property) -> TestTree)
-> (CostStream -> Property) -> TestTree
forall a b. (a -> b) -> a -> b
$ \CostStream
costs ->
(CostStream -> CostStream -> Bool)
-> CostStream -> CostStream -> Property
forall a. Show a => (a -> a -> Bool) -> a -> a -> Property
checkEqualsVia
CostStream -> CostStream -> Bool
eqCostStream
((CostingInteger -> Maybe CostStream -> CostStream)
-> (CostingInteger, Maybe CostStream) -> CostStream
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry CostingInteger -> Maybe CostStream -> CostStream
reconsCost ((CostingInteger, Maybe CostStream) -> CostStream)
-> (CostingInteger, Maybe CostStream) -> CostStream
forall a b. (a -> b) -> a -> b
$ CostStream -> (CostingInteger, Maybe CostStream)
unconsCost CostStream
costs)
CostStream
costs
test_sumCostStreamIsSum :: TestTree
test_sumCostStreamIsSum :: TestTree
test_sumCostStreamIsSum =
[Char] -> Property -> TestTree
forall a. Testable a => [Char] -> a -> TestTree
testProperty [Char]
"sumCostStream is sum" (Property -> TestTree)
-> ((CostStream -> Property) -> Property)
-> (CostStream -> Property)
-> TestTree
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> (CostStream -> Property) -> Property
forall prop. Testable prop => Int -> prop -> Property
BaseQC.withNumTests Int
5000 ((CostStream -> Property) -> TestTree)
-> (CostStream -> Property) -> TestTree
forall a b. (a -> b) -> a -> b
$ \CostStream
costs ->
CostStream -> CostingInteger
sumCostStream CostStream
costs
CostingInteger -> CostingInteger -> Property
forall a. (Eq a, Show a) => a -> a -> Property
=== NonEmptyList CostingInteger -> CostingInteger
forall a. Num a => NonEmptyList a -> a
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum (CostStream -> NonEmptyList CostingInteger
toCostList CostStream
costs)
test_mapCostStreamIsMap :: TestTree
test_mapCostStreamIsMap :: TestTree
test_mapCostStreamIsMap =
[Char] -> Property -> TestTree
forall a. Testable a => [Char] -> a -> TestTree
testProperty [Char]
"mapCostStream is map" (Property -> TestTree)
-> ((Fun CostingInteger CostingInteger
-> NonEmptyList CostingInteger -> Property)
-> Property)
-> (Fun CostingInteger CostingInteger
-> NonEmptyList CostingInteger -> Property)
-> TestTree
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int
-> (Fun CostingInteger CostingInteger
-> NonEmptyList CostingInteger -> Property)
-> Property
forall prop. Testable prop => Int -> prop -> Property
BaseQC.withNumTests Int
500 ((Fun CostingInteger CostingInteger
-> NonEmptyList CostingInteger -> Property)
-> TestTree)
-> (Fun CostingInteger CostingInteger
-> NonEmptyList CostingInteger -> Property)
-> TestTree
forall a b. (a -> b) -> a -> b
$ \(Fun (CostingInteger :-> CostingInteger, CostingInteger, Shrunk)
_ CostingInteger -> CostingInteger
f) NonEmptyList CostingInteger
costs ->
(CostStream -> CostStream -> Bool)
-> CostStream -> CostStream -> Property
forall a. Show a => (a -> a -> Bool) -> a -> a -> Property
checkEqualsVia
CostStream -> CostStream -> Bool
eqCostStream
((CostingInteger -> CostingInteger) -> CostStream -> CostStream
mapCostStream CostingInteger -> CostingInteger
f (CostStream -> CostStream) -> CostStream -> CostStream
forall a b. (a -> b) -> a -> b
$ NonEmptyList CostingInteger -> CostStream
fromCostList NonEmptyList CostingInteger
costs)
(NonEmptyList CostingInteger -> CostStream
fromCostList (NonEmptyList CostingInteger -> CostStream)
-> NonEmptyList CostingInteger -> CostStream
forall a b. (a -> b) -> a -> b
$ (CostingInteger -> CostingInteger)
-> NonEmptyList CostingInteger -> NonEmptyList CostingInteger
forall a b. (a -> b) -> NonEmptyList a -> NonEmptyList b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap CostingInteger -> CostingInteger
f NonEmptyList CostingInteger
costs)
test_addCostStreamIsAdd :: TestTree
test_addCostStreamIsAdd :: TestTree
test_addCostStreamIsAdd =
[Char] -> Property -> TestTree
forall a. Testable a => [Char] -> a -> TestTree
testProperty [Char]
"addCostStream is add" (Property -> TestTree)
-> ((CostStream -> CostStream -> Property) -> Property)
-> (CostStream -> CostStream -> Property)
-> TestTree
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> (CostStream -> CostStream -> Property) -> Property
forall prop. Testable prop => Int -> prop -> Property
BaseQC.withNumTests Int
5000 ((CostStream -> CostStream -> Property) -> TestTree)
-> (CostStream -> CostStream -> Property) -> TestTree
forall a b. (a -> b) -> a -> b
$ \CostStream
costs1 CostStream
costs2 ->
CostStream -> CostingInteger
sumCostStream (CostStream -> CostStream -> CostStream
addCostStream CostStream
costs1 CostStream
costs2)
CostingInteger -> CostingInteger -> Property
forall a. (Eq a, Show a) => a -> a -> Property
=== CostStream -> CostingInteger
sumCostStream CostStream
costs1
CostingInteger -> CostingInteger -> CostingInteger
forall a. Num a => a -> a -> a
+ CostStream -> CostingInteger
sumCostStream CostStream
costs2
test_minCostStreamIsMin :: TestTree
test_minCostStreamIsMin :: TestTree
test_minCostStreamIsMin =
[Char] -> Property -> TestTree
forall a. Testable a => [Char] -> a -> TestTree
testProperty [Char]
"minCostStream is min" (Property -> TestTree)
-> ((CostStream -> CostStream -> Property) -> Property)
-> (CostStream -> CostStream -> Property)
-> TestTree
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> (CostStream -> CostStream -> Property) -> Property
forall prop. Testable prop => Int -> prop -> Property
BaseQC.withNumTests Int
5000 ((CostStream -> CostStream -> Property) -> TestTree)
-> (CostStream -> CostStream -> Property) -> TestTree
forall a b. (a -> b) -> a -> b
$ \CostStream
costs1 CostStream
costs2 ->
CostStream -> CostingInteger
sumCostStream (CostStream -> CostStream -> CostStream
minCostStream CostStream
costs1 CostStream
costs2)
CostingInteger -> CostingInteger -> Property
forall a. (Eq a, Show a) => a -> a -> Property
=== CostingInteger -> CostingInteger -> CostingInteger
forall a. Ord a => a -> a -> a
min (CostStream -> CostingInteger
sumCostStream CostStream
costs1) (CostStream -> CostingInteger
sumCostStream CostStream
costs2)
test_zipCostStreamIsZip :: TestTree
test_zipCostStreamIsZip :: TestTree
test_zipCostStreamIsZip =
[Char] -> Property -> TestTree
forall a. Testable a => [Char] -> a -> TestTree
testProperty [Char]
"zipCostStream is zip" (Property -> TestTree)
-> ((CostStream -> CostStream -> Property) -> Property)
-> (CostStream -> CostStream -> Property)
-> TestTree
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> (CostStream -> CostStream -> Property) -> Property
forall prop. Testable prop => Int -> prop -> Property
BaseQC.withNumTests Int
5000 ((CostStream -> CostStream -> Property) -> TestTree)
-> (CostStream -> CostStream -> Property) -> TestTree
forall a b. (a -> b) -> a -> b
$ \CostStream
costs1 CostStream
costs2 ->
ExBudgetStream -> ExBudget
sumExBudgetStream (CostStream -> CostStream -> ExBudgetStream
zipCostStream CostStream
costs1 CostStream
costs2)
ExBudget -> ExBudget -> Property
forall a. (Eq a, Show a) => a -> a -> Property
=== ExCPU -> ExMemory -> ExBudget
ExBudget (CostingInteger -> ExCPU
ExCPU (CostingInteger -> ExCPU) -> CostingInteger -> ExCPU
forall a b. (a -> b) -> a -> b
$ CostStream -> CostingInteger
sumCostStream CostStream
costs1) (CostingInteger -> ExMemory
ExMemory (CostingInteger -> ExMemory) -> CostingInteger -> ExMemory
forall a b. (a -> b) -> a -> b
$ CostStream -> CostingInteger
sumCostStream CostStream
costs2)
test_mapCostStreamReasonableLength :: TestTree
test_mapCostStreamReasonableLength :: TestTree
test_mapCostStreamReasonableLength =
[Char] -> Property -> TestTree
forall a. Testable a => [Char] -> a -> TestTree
testProperty [Char]
"mapCostStream: reasonable length" (Property -> TestTree)
-> ((Fun CostingInteger CostingInteger -> CostStream -> Property)
-> Property)
-> (Fun CostingInteger CostingInteger -> CostStream -> Property)
-> TestTree
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int
-> (Fun CostingInteger CostingInteger -> CostStream -> Property)
-> Property
forall prop. Testable prop => Int -> prop -> Property
BaseQC.withNumTests Int
500 ((Fun CostingInteger CostingInteger -> CostStream -> Property)
-> TestTree)
-> (Fun CostingInteger CostingInteger -> CostStream -> Property)
-> TestTree
forall a b. (a -> b) -> a -> b
$ \(Fun (CostingInteger :-> CostingInteger, CostingInteger, Shrunk)
_ CostingInteger -> CostingInteger
f) CostStream
costs ->
NonEmptyList CostingInteger -> Int
forall a. NonEmptyList a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length (CostStream -> NonEmptyList CostingInteger
toCostList ((CostingInteger -> CostingInteger) -> CostStream -> CostStream
mapCostStream CostingInteger -> CostingInteger
f CostStream
costs))
Int -> Int -> Property
forall a. (Eq a, Show a) => a -> a -> Property
=== NonEmptyList CostingInteger -> Int
forall a. NonEmptyList a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length (CostStream -> NonEmptyList CostingInteger
toCostList CostStream
costs)
test_addCostStreamReasonableLength :: TestTree
test_addCostStreamReasonableLength :: TestTree
test_addCostStreamReasonableLength =
[Char] -> Property -> TestTree
forall a. Testable a => [Char] -> a -> TestTree
testProperty [Char]
"addCostStream: reasonable length " (Property -> TestTree)
-> ((CostStream -> CostStream -> Property) -> Property)
-> (CostStream -> CostStream -> Property)
-> TestTree
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> (CostStream -> CostStream -> Property) -> Property
forall prop. Testable prop => Int -> prop -> Property
BaseQC.withNumTests Int
5000 ((CostStream -> CostStream -> Property) -> TestTree)
-> (CostStream -> CostStream -> Property) -> TestTree
forall a b. (a -> b) -> a -> b
$ \CostStream
costs1 CostStream
costs2 ->
Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
2 (NonEmptyList CostingInteger -> Int
forall a. NonEmptyList a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length (CostStream -> NonEmptyList CostingInteger
toCostList (CostStream -> CostStream -> CostStream
addCostStream CostStream
costs1 CostStream
costs2)))
Int -> Int -> Property
forall a. (Eq a, Show a) => a -> a -> Property
=== NonEmptyList CostingInteger -> Int
forall a. NonEmptyList a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length (CostStream -> NonEmptyList CostingInteger
toCostList CostStream
costs1)
Int -> Int -> Int
forall a. Num a => a -> a -> a
+ NonEmptyList CostingInteger -> Int
forall a. NonEmptyList a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length (CostStream -> NonEmptyList CostingInteger
toCostList CostStream
costs2)
test_minCostStreamReasonableLength :: TestTree
test_minCostStreamReasonableLength :: TestTree
test_minCostStreamReasonableLength =
[Char] -> Property -> TestTree
forall a. Testable a => [Char] -> a -> TestTree
testProperty [Char]
"minCostStream: reasonable length " (Property -> TestTree)
-> ((CostStream -> CostStream -> Bool) -> Property)
-> (CostStream -> CostStream -> Bool)
-> TestTree
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> (CostStream -> CostStream -> Bool) -> Property
forall prop. Testable prop => Int -> prop -> Property
BaseQC.withNumTests Int
5000 ((CostStream -> CostStream -> Bool) -> TestTree)
-> (CostStream -> CostStream -> Bool) -> TestTree
forall a b. (a -> b) -> a -> b
$ \CostStream
costs1 CostStream
costs2 ->
let len1 :: Int
len1 = NonEmptyList CostingInteger -> Int
forall a. NonEmptyList a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length (NonEmptyList CostingInteger -> Int)
-> NonEmptyList CostingInteger -> Int
forall a b. (a -> b) -> a -> b
$ CostStream -> NonEmptyList CostingInteger
toCostList CostStream
costs1
len2 :: Int
len2 = NonEmptyList CostingInteger -> Int
forall a. NonEmptyList a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length (NonEmptyList CostingInteger -> Int)
-> NonEmptyList CostingInteger -> Int
forall a b. (a -> b) -> a -> b
$ CostStream -> NonEmptyList CostingInteger
toCostList CostStream
costs2
lenMin :: Int
lenMin = NonEmptyList CostingInteger -> Int
forall a. NonEmptyList a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length (NonEmptyList CostingInteger -> Int)
-> (CostStream -> NonEmptyList CostingInteger) -> CostStream -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CostStream -> NonEmptyList CostingInteger
toCostList (CostStream -> Int) -> CostStream -> Int
forall a b. (a -> b) -> a -> b
$ CostStream -> CostStream -> CostStream
minCostStream CostStream
costs1 CostStream
costs2
in Int
lenMin Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int -> Int -> Int
forall a. Ord a => a -> a -> a
min Int
len1 Int
len2 Bool -> Bool -> Bool
&& Int
lenMin Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
len1 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
len2
test_zipCostStreamReasonableLength :: TestTree
test_zipCostStreamReasonableLength :: TestTree
test_zipCostStreamReasonableLength =
[Char] -> Property -> TestTree
forall a. Testable a => [Char] -> a -> TestTree
testProperty [Char]
"zipCostStream: reasonable length " (Property -> TestTree)
-> ((CostStream -> CostStream -> Property) -> Property)
-> (CostStream -> CostStream -> Property)
-> TestTree
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> (CostStream -> CostStream -> Property) -> Property
forall prop. Testable prop => Int -> prop -> Property
BaseQC.withNumTests Int
5000 ((CostStream -> CostStream -> Property) -> TestTree)
-> (CostStream -> CostStream -> Property) -> TestTree
forall a b. (a -> b) -> a -> b
$ \CostStream
costs1 CostStream
costs2 ->
NonEmptyList ExBudget -> Int
forall a. NonEmptyList a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length (ExBudgetStream -> NonEmptyList ExBudget
toExBudgetList (CostStream -> CostStream -> ExBudgetStream
zipCostStream CostStream
costs1 CostStream
costs2))
Int -> Int -> Property
forall a. (Eq a, Show a) => a -> a -> Property
=== Int -> Int -> Int
forall a. Ord a => a -> a -> a
max (NonEmptyList CostingInteger -> Int
forall a. NonEmptyList a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length (CostStream -> NonEmptyList CostingInteger
toCostList CostStream
costs1)) (NonEmptyList CostingInteger -> Int
forall a. NonEmptyList a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length (CostStream -> NonEmptyList CostingInteger
toCostList CostStream
costs2))
test_mapCostStreamHandlesBottom :: TestTree
test_mapCostStreamHandlesBottom :: TestTree
test_mapCostStreamHandlesBottom =
[Char] -> Property -> TestTree
forall a. Testable a => [Char] -> a -> TestTree
testProperty [Char]
"mapCostStream handles bottom suffixes" (Property -> TestTree)
-> ((Fun CostingInteger CostingInteger
-> [CostingInteger] -> Property)
-> Property)
-> (Fun CostingInteger CostingInteger
-> [CostingInteger] -> Property)
-> TestTree
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int
-> (Fun CostingInteger CostingInteger
-> [CostingInteger] -> Property)
-> Property
forall prop. Testable prop => Int -> prop -> Property
BaseQC.withNumTests Int
500 ((Fun CostingInteger CostingInteger
-> [CostingInteger] -> Property)
-> TestTree)
-> (Fun CostingInteger CostingInteger
-> [CostingInteger] -> Property)
-> TestTree
forall a b. (a -> b) -> a -> b
$ \(Fun (CostingInteger :-> CostingInteger, CostingInteger, Shrunk)
_ CostingInteger -> CostingInteger
f) [CostingInteger]
xs ->
let n :: Int
n = [CostingInteger] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [CostingInteger]
xs
suff :: [CostingInteger]
suff = CostingInteger
0 CostingInteger -> [CostingInteger] -> [CostingInteger]
forall a. a -> [a] -> [a]
: [CostingInteger]
forall a. a
bottom
costs :: CostStream
costs = NonEmptyList CostingInteger -> CostStream
fromCostList (NonEmptyList CostingInteger -> CostStream)
-> ([CostingInteger] -> NonEmptyList CostingInteger)
-> [CostingInteger]
-> CostStream
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [CostingInteger] -> NonEmptyList CostingInteger
forall a. [a] -> NonEmptyList a
NonEmpty ([CostingInteger] -> CostStream) -> [CostingInteger] -> CostStream
forall a b. (a -> b) -> a -> b
$ [CostingInteger]
xs [CostingInteger] -> [CostingInteger] -> [CostingInteger]
forall a. [a] -> [a] -> [a]
++ [CostingInteger]
suff
in [CostingInteger] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length (Int -> [CostingInteger] -> [CostingInteger]
forall a. Int -> [a] -> [a]
take Int
n ([CostingInteger] -> [CostingInteger])
-> (CostStream -> [CostingInteger])
-> CostStream
-> [CostingInteger]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NonEmptyList CostingInteger -> [CostingInteger]
forall a. NonEmptyList a -> [a]
getNonEmpty (NonEmptyList CostingInteger -> [CostingInteger])
-> (CostStream -> NonEmptyList CostingInteger)
-> CostStream
-> [CostingInteger]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CostStream -> NonEmptyList CostingInteger
toCostList (CostStream -> [CostingInteger]) -> CostStream -> [CostingInteger]
forall a b. (a -> b) -> a -> b
$ (CostingInteger -> CostingInteger) -> CostStream -> CostStream
mapCostStream CostingInteger -> CostingInteger
f CostStream
costs) Int -> Int -> Property
forall a. (Eq a, Show a) => a -> a -> Property
=== Int
n
test_addCostStreamHandlesBottom :: TestTree
test_addCostStreamHandlesBottom :: TestTree
test_addCostStreamHandlesBottom =
[Char] -> Property -> TestTree
forall a. Testable a => [Char] -> a -> TestTree
testProperty [Char]
"addCostStream handles bottom suffixes" (Property -> TestTree)
-> ((Positive Int -> Property) -> Property)
-> (Positive Int -> Property)
-> TestTree
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> (Positive Int -> Property) -> Property
forall prop. Testable prop => Int -> prop -> Property
BaseQC.withNumTests Int
5000 ((Positive Int -> Property) -> TestTree)
-> (Positive Int -> Property) -> TestTree
forall a b. (a -> b) -> a -> b
$ \(Positive Int
n) ->
let interleave :: [a] -> [a] -> [a]
interleave [a]
xs [a]
ys = [[a]] -> [a]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat ([[a]] -> [a]) -> [[a]] -> [a]
forall a b. (a -> b) -> a -> b
$ [[a]] -> [[a]]
forall a. [[a]] -> [[a]]
transpose [[a]
xs, [a]
ys]
zeroToN :: [CostingInteger]
zeroToN = (Int -> CostingInteger) -> [Int] -> [CostingInteger]
forall a b. (a -> b) -> [a] -> [b]
map (Int64 -> CostingInteger
unsafeToSatInt (Int64 -> CostingInteger)
-> (Int -> Int64) -> Int -> CostingInteger
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral) [Int
0 .. Int
n] [CostingInteger] -> [CostingInteger] -> [CostingInteger]
forall a. [a] -> [a] -> [a]
++ [CostingInteger]
forall a. a
bottom
nPlus1To2NPlus1 :: [CostingInteger]
nPlus1To2NPlus1 = (Int -> CostingInteger) -> [Int] -> [CostingInteger]
forall a b. (a -> b) -> [a] -> [b]
map (Int64 -> CostingInteger
unsafeToSatInt (Int64 -> CostingInteger)
-> (Int -> Int64) -> Int -> CostingInteger
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral) [Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1 .. Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
2 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1] [CostingInteger] -> [CostingInteger] -> [CostingInteger]
forall a. [a] -> [a] -> [a]
++ [CostingInteger]
forall a. a
bottom
taken :: [CostingInteger]
taken =
Int -> [CostingInteger] -> [CostingInteger]
forall a. Int -> [a] -> [a]
take Int
n ([CostingInteger] -> [CostingInteger])
-> (CostStream -> [CostingInteger])
-> CostStream
-> [CostingInteger]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NonEmptyList CostingInteger -> [CostingInteger]
forall a. NonEmptyList a -> [a]
getNonEmpty (NonEmptyList CostingInteger -> [CostingInteger])
-> (CostStream -> NonEmptyList CostingInteger)
-> CostStream
-> [CostingInteger]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CostStream -> NonEmptyList CostingInteger
toCostList (CostStream -> [CostingInteger]) -> CostStream -> [CostingInteger]
forall a b. (a -> b) -> a -> b
$
CostStream -> CostStream -> CostStream
addCostStream
(NonEmptyList CostingInteger -> CostStream
fromCostList (NonEmptyList CostingInteger -> CostStream)
-> NonEmptyList CostingInteger -> CostStream
forall a b. (a -> b) -> a -> b
$ [CostingInteger] -> NonEmptyList CostingInteger
forall a. [a] -> NonEmptyList a
NonEmpty [CostingInteger]
zeroToN)
(NonEmptyList CostingInteger -> CostStream
fromCostList (NonEmptyList CostingInteger -> CostStream)
-> NonEmptyList CostingInteger -> CostStream
forall a b. (a -> b) -> a -> b
$ [CostingInteger] -> NonEmptyList CostingInteger
forall a. [a] -> NonEmptyList a
NonEmpty [CostingInteger]
nPlus1To2NPlus1)
in
(CostingInteger -> Bool) -> [CostingInteger] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (\CostingInteger
cost -> CostingInteger
cost CostingInteger -> [CostingInteger] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [CostingInteger] -> [CostingInteger] -> [CostingInteger]
forall a. [a] -> [a] -> [a]
interleave [CostingInteger]
zeroToN [CostingInteger]
nPlus1To2NPlus1) [CostingInteger]
taken
Bool -> Property -> Property
forall prop1 prop2.
(Testable prop1, Testable prop2) =>
prop1 -> prop2 -> Property
.&&.
[CostingInteger] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length (([CostingInteger] -> CostingInteger)
-> [[CostingInteger]] -> [CostingInteger]
forall a b. (a -> b) -> [a] -> [b]
map [CostingInteger] -> CostingInteger
forall a. HasCallStack => [a] -> a
head ([[CostingInteger]] -> [CostingInteger])
-> ([CostingInteger] -> [[CostingInteger]])
-> [CostingInteger]
-> [CostingInteger]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [CostingInteger] -> [[CostingInteger]]
forall a. Eq a => [a] -> [[a]]
group ([CostingInteger] -> [CostingInteger])
-> [CostingInteger] -> [CostingInteger]
forall a b. (a -> b) -> a -> b
$ [CostingInteger] -> [CostingInteger]
forall a. Ord a => [a] -> [a]
sort [CostingInteger]
taken)
Int -> Int -> Property
forall a. (Eq a, Show a) => a -> a -> Property
=== [CostingInteger] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [CostingInteger]
taken
test_minCostStreamHandlesBottom :: TestTree
test_minCostStreamHandlesBottom :: TestTree
test_minCostStreamHandlesBottom =
[Char] -> Property -> TestTree
forall a. Testable a => [Char] -> a -> TestTree
testProperty [Char]
"minCostStream handles bottom suffixes" (Property -> TestTree)
-> (([CostingInteger] -> [CostingInteger] -> Bool) -> Property)
-> ([CostingInteger] -> [CostingInteger] -> Bool)
-> TestTree
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> ([CostingInteger] -> [CostingInteger] -> Bool) -> Property
forall prop. Testable prop => Int -> prop -> Property
BaseQC.withNumTests Int
5000 (([CostingInteger] -> [CostingInteger] -> Bool) -> TestTree)
-> ([CostingInteger] -> [CostingInteger] -> Bool) -> TestTree
forall a b. (a -> b) -> a -> b
$ \[CostingInteger]
xs [CostingInteger]
ys ->
let m :: CostingInteger
m = CostingInteger -> CostingInteger -> CostingInteger
forall a. Ord a => a -> a -> a
min ([CostingInteger] -> CostingInteger
forall a. Num a => [a] -> a
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum [CostingInteger]
xs) ([CostingInteger] -> CostingInteger
forall a. Num a => [a] -> a
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum [CostingInteger]
ys)
suff :: [CostingInteger]
suff = CostingInteger
0 CostingInteger -> [CostingInteger] -> [CostingInteger]
forall a. a -> [a] -> [a]
: [CostingInteger]
forall a. a
bottom
xsYsMin :: CostStream
xsYsMin =
CostStream -> CostStream -> CostStream
minCostStream
(NonEmptyList CostingInteger -> CostStream
fromCostList (NonEmptyList CostingInteger -> CostStream)
-> ([CostingInteger] -> NonEmptyList CostingInteger)
-> [CostingInteger]
-> CostStream
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [CostingInteger] -> NonEmptyList CostingInteger
forall a. [a] -> NonEmptyList a
NonEmpty ([CostingInteger] -> CostStream) -> [CostingInteger] -> CostStream
forall a b. (a -> b) -> a -> b
$ [CostingInteger]
xs [CostingInteger] -> [CostingInteger] -> [CostingInteger]
forall a. [a] -> [a] -> [a]
++ [CostingInteger]
suff)
(NonEmptyList CostingInteger -> CostStream
fromCostList (NonEmptyList CostingInteger -> CostStream)
-> ([CostingInteger] -> NonEmptyList CostingInteger)
-> [CostingInteger]
-> CostStream
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [CostingInteger] -> NonEmptyList CostingInteger
forall a. [a] -> NonEmptyList a
NonEmpty ([CostingInteger] -> CostStream) -> [CostingInteger] -> CostStream
forall a b. (a -> b) -> a -> b
$ [CostingInteger]
ys [CostingInteger] -> [CostingInteger] -> [CostingInteger]
forall a. [a] -> [a] -> [a]
++ [CostingInteger]
suff)
in
CostingInteger -> [CostingInteger] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
elem CostingInteger
m ([CostingInteger] -> Bool)
-> (NonEmptyList CostingInteger -> [CostingInteger])
-> NonEmptyList CostingInteger
-> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (CostingInteger -> CostingInteger -> CostingInteger)
-> CostingInteger -> [CostingInteger] -> [CostingInteger]
forall b a. (b -> a -> b) -> b -> [a] -> [b]
scanl' CostingInteger -> CostingInteger -> CostingInteger
forall a. Num a => a -> a -> a
(+) CostingInteger
0 ([CostingInteger] -> [CostingInteger])
-> (NonEmptyList CostingInteger -> [CostingInteger])
-> NonEmptyList CostingInteger
-> [CostingInteger]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NonEmptyList CostingInteger -> [CostingInteger]
forall a. NonEmptyList a -> [a]
getNonEmpty (NonEmptyList CostingInteger -> Bool)
-> NonEmptyList CostingInteger -> Bool
forall a b. (a -> b) -> a -> b
$ CostStream -> NonEmptyList CostingInteger
toCostList CostStream
xsYsMin
postAlignWith :: a -> [a] -> [a] -> ([a], [a])
postAlignWith :: forall a. a -> [a] -> [a] -> ([a], [a])
postAlignWith a
z [a]
xs [a]
ys = ([a] -> [a]
align [a]
xs, [a] -> [a]
align [a]
ys)
where
align :: [a] -> [a]
align [a]
zs = Int -> [a] -> [a]
forall a. Int -> [a] -> [a]
take ([a] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [a]
xs Int -> Int -> Int
forall a. Ord a => a -> a -> a
`max` [a] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [a]
ys) ([a] -> [a]) -> [a] -> [a]
forall a b. (a -> b) -> a -> b
$ [a]
zs [a] -> [a] -> [a]
forall a. [a] -> [a] -> [a]
++ a -> [a]
forall a. a -> [a]
repeat a
z
test_zipCostStreamHandlesBottom :: TestTree
test_zipCostStreamHandlesBottom :: TestTree
test_zipCostStreamHandlesBottom =
[Char] -> Property -> TestTree
forall a. Testable a => [Char] -> a -> TestTree
testProperty [Char]
"zipCostStream handles bottom suffixes" (Property -> TestTree)
-> (([CostingInteger] -> [CostingInteger] -> Bool) -> Property)
-> ([CostingInteger] -> [CostingInteger] -> Bool)
-> TestTree
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> ([CostingInteger] -> [CostingInteger] -> Bool) -> Property
forall prop. Testable prop => Int -> prop -> Property
BaseQC.withNumTests Int
5000 (([CostingInteger] -> [CostingInteger] -> Bool) -> TestTree)
-> ([CostingInteger] -> [CostingInteger] -> Bool) -> TestTree
forall a b. (a -> b) -> a -> b
$ \[CostingInteger]
xs [CostingInteger]
ys ->
let z :: ExBudget
z = ExCPU -> ExMemory -> ExBudget
ExBudget (CostingInteger -> ExCPU
ExCPU (CostingInteger -> ExCPU) -> CostingInteger -> ExCPU
forall a b. (a -> b) -> a -> b
$ [CostingInteger] -> CostingInteger
forall a. Num a => [a] -> a
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum [CostingInteger]
xs) (CostingInteger -> ExMemory
ExMemory (CostingInteger -> ExMemory) -> CostingInteger -> ExMemory
forall a b. (a -> b) -> a -> b
$ [CostingInteger] -> CostingInteger
forall a. Num a => [a] -> a
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum [CostingInteger]
ys)
([CostingInteger]
xsA, [CostingInteger]
ysA) = CostingInteger
-> [CostingInteger]
-> [CostingInteger]
-> ([CostingInteger], [CostingInteger])
forall a. a -> [a] -> [a] -> ([a], [a])
postAlignWith CostingInteger
0 [CostingInteger]
xs [CostingInteger]
ys
suff :: [CostingInteger]
suff = CostingInteger
0 CostingInteger -> [CostingInteger] -> [CostingInteger]
forall a. a -> [a] -> [a]
: [CostingInteger]
forall a. a
bottom
xys :: ExBudgetStream
xys =
CostStream -> CostStream -> ExBudgetStream
zipCostStream
(NonEmptyList CostingInteger -> CostStream
fromCostList (NonEmptyList CostingInteger -> CostStream)
-> ([CostingInteger] -> NonEmptyList CostingInteger)
-> [CostingInteger]
-> CostStream
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [CostingInteger] -> NonEmptyList CostingInteger
forall a. [a] -> NonEmptyList a
NonEmpty ([CostingInteger] -> CostStream) -> [CostingInteger] -> CostStream
forall a b. (a -> b) -> a -> b
$ [CostingInteger]
xsA [CostingInteger] -> [CostingInteger] -> [CostingInteger]
forall a. [a] -> [a] -> [a]
++ [CostingInteger]
suff)
(NonEmptyList CostingInteger -> CostStream
fromCostList (NonEmptyList CostingInteger -> CostStream)
-> ([CostingInteger] -> NonEmptyList CostingInteger)
-> [CostingInteger]
-> CostStream
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [CostingInteger] -> NonEmptyList CostingInteger
forall a. [a] -> NonEmptyList a
NonEmpty ([CostingInteger] -> CostStream) -> [CostingInteger] -> CostStream
forall a b. (a -> b) -> a -> b
$ [CostingInteger]
ysA [CostingInteger] -> [CostingInteger] -> [CostingInteger]
forall a. [a] -> [a] -> [a]
++ [CostingInteger]
suff)
in
ExBudget -> [ExBudget] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
elem ExBudget
z ([ExBudget] -> Bool)
-> (NonEmptyList ExBudget -> [ExBudget])
-> NonEmptyList ExBudget
-> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (ExBudget -> ExBudget -> ExBudget)
-> ExBudget -> [ExBudget] -> [ExBudget]
forall b a. (b -> a -> b) -> b -> [a] -> [b]
scanl' ExBudget -> ExBudget -> ExBudget
forall a. Semigroup a => a -> a -> a
(<>) ExBudget
forall a. Monoid a => a
mempty ([ExBudget] -> [ExBudget])
-> (NonEmptyList ExBudget -> [ExBudget])
-> NonEmptyList ExBudget
-> [ExBudget]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NonEmptyList ExBudget -> [ExBudget]
forall a. NonEmptyList a -> [a]
getNonEmpty (NonEmptyList ExBudget -> Bool) -> NonEmptyList ExBudget -> Bool
forall a b. (a -> b) -> a -> b
$ ExBudgetStream -> NonEmptyList ExBudget
toExBudgetList ExBudgetStream
xys
sierpinskiSize :: Int -> Int
sierpinskiSize :: Int -> Int
sierpinskiSize Int
n
| Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
1 = Int
1
| Bool
otherwise = Int -> Int
sierpinskiSize (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
3 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1
sierpinskiRose :: Int -> CostRose
sierpinskiRose :: Int -> CostRose
sierpinskiRose Int
n0
| Int
n0 Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
1 = CostingInteger -> CostRose
singletonRose CostingInteger
1
| Bool
otherwise = CostingInteger -> [CostRose] -> CostRose
CostRose (Int -> CostingInteger
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
n0) ([CostRose] -> CostRose) -> (Int -> [CostRose]) -> Int -> CostRose
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Int -> CostRose) -> [Int] -> [CostRose]
forall a b. (a -> b) -> [a] -> [b]
map Int -> CostRose
sierpinskiRose ([Int] -> [CostRose]) -> (Int -> [Int]) -> Int -> [CostRose]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Int -> [Int]
forall a. Int -> a -> [a]
replicate Int
3 (Int -> CostRose) -> Int -> CostRose
forall a b. (a -> b) -> a -> b
$ Int
n0 Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1
test_flattenCostRoseIsLinearForSierpinskiRose :: Int -> TestTree
test_flattenCostRoseIsLinearForSierpinskiRose :: Int -> TestTree
test_flattenCostRoseIsLinearForSierpinskiRose Int
depth =
let size :: Int
size = Int -> Int
sierpinskiSize Int
depth
in [Char] -> Property -> TestTree
forall a. Testable a => [Char] -> a -> TestTree
testProperty ([Char]
"sierpinski rose: taking " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ Int -> [Char]
forall a. Show a => a -> [Char]
show Int
size [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
" elements") (Property -> TestTree) -> Property -> TestTree
forall a b. (a -> b) -> a -> b
$
Int -> Property -> Property
forall prop. Testable prop => Int -> prop -> Property
BaseQC.withNumTests Int
1 (Property -> Property) -> Property -> Property
forall a b. (a -> b) -> a -> b
$
NonEmptyList CostingInteger -> Int
forall a. NonEmptyList a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length (CostStream -> NonEmptyList CostingInteger
toCostList (CostStream -> NonEmptyList CostingInteger)
-> (CostRose -> CostStream)
-> CostRose
-> NonEmptyList CostingInteger
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CostRose -> CostStream
flattenCostRose (CostRose -> NonEmptyList CostingInteger)
-> CostRose -> NonEmptyList CostingInteger
forall a b. (a -> b) -> a -> b
$ Int -> CostRose
sierpinskiRose Int
depth)
Int -> Int -> Property
forall a. (Eq a, Show a) => a -> a -> Property
=== Int
size
test_flattenCostRoseIsLinear :: TestTree
test_flattenCostRoseIsLinear :: TestTree
test_flattenCostRoseIsLinear =
[Char] -> [TestTree] -> TestTree
testGroup
[Char]
"flattenCostRose is linear"
[ Int -> TestTree
test_flattenCostRoseIsLinearForSierpinskiRose Int
12
, Int -> TestTree
test_flattenCostRoseIsLinearForSierpinskiRose Int
13
, Int -> TestTree
test_flattenCostRoseIsLinearForSierpinskiRose Int
14
]
genCostRose :: NonEmptyList SatInt -> Gen CostRose
genCostRose :: NonEmptyList CostingInteger -> Gen CostRose
genCostRose (NonEmpty []) = [Char] -> Gen CostRose
forall a. HasCallStack => [Char] -> a
error [Char]
"Panic: an empty non-empty list"
genCostRose (NonEmpty (CostingInteger
cost : [CostingInteger]
costs)) =
CostingInteger -> [CostRose] -> CostRose
CostRose CostingInteger
cost ([CostRose] -> CostRose) -> Gen [CostRose] -> Gen CostRose
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ((NonEmptyList CostingInteger -> Gen CostRose)
-> [NonEmptyList CostingInteger] -> Gen [CostRose]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> [a] -> f [b]
traverse NonEmptyList CostingInteger -> Gen CostRose
genCostRose ([NonEmptyList CostingInteger] -> Gen [CostRose])
-> Gen [NonEmptyList CostingInteger] -> Gen [CostRose]
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< [CostingInteger] -> Gen [NonEmptyList CostingInteger]
forall a. [a] -> Gen [NonEmptyList a]
multiSplit1 [CostingInteger]
costs)
fromCostRose :: CostRose -> NonEmptyList SatInt
fromCostRose :: CostRose -> NonEmptyList CostingInteger
fromCostRose (CostRose CostingInteger
cost [CostRose]
costs) =
[CostingInteger] -> NonEmptyList CostingInteger
forall a. [a] -> NonEmptyList a
NonEmpty ([CostingInteger] -> NonEmptyList CostingInteger)
-> [CostingInteger] -> NonEmptyList CostingInteger
forall a b. (a -> b) -> a -> b
$ CostingInteger
cost CostingInteger -> [CostingInteger] -> [CostingInteger]
forall a. a -> [a] -> [a]
: (CostRose -> [CostingInteger]) -> [CostRose] -> [CostingInteger]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (NonEmptyList CostingInteger -> [CostingInteger]
forall a. NonEmptyList a -> [a]
getNonEmpty (NonEmptyList CostingInteger -> [CostingInteger])
-> (CostRose -> NonEmptyList CostingInteger)
-> CostRose
-> [CostingInteger]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CostRose -> NonEmptyList CostingInteger
fromCostRose) [CostRose]
costs
instance Arbitrary CostRose where
arbitrary :: Gen CostRose
arbitrary = (Int -> Int)
-> Gen (NonEmptyList CostingInteger)
-> Gen (NonEmptyList CostingInteger)
forall a. (Int -> Int) -> Gen a -> Gen a
scale (Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
10) Gen (NonEmptyList CostingInteger)
forall a. Arbitrary a => Gen a
arbitrary Gen (NonEmptyList CostingInteger)
-> (NonEmptyList CostingInteger -> Gen CostRose) -> Gen CostRose
forall a b. Gen a -> (a -> Gen b) -> Gen b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= NonEmptyList CostingInteger -> Gen CostRose
genCostRose
shrink :: CostRose -> [CostRose]
shrink (CostRose CostingInteger
cost [CostRose]
costs) = do
([CostRose]
costs', CostingInteger
cost') <- ([CostRose], CostingInteger) -> [([CostRose], CostingInteger)]
forall a. Arbitrary a => a -> [a]
shrink ([CostRose]
costs, CostingInteger
cost)
CostRose -> [CostRose]
forall a. a -> [a]
forall (f :: * -> *) a. Applicative f => a -> f a
pure (CostRose -> [CostRose]) -> CostRose -> [CostRose]
forall a b. (a -> b) -> a -> b
$ CostingInteger -> [CostRose] -> CostRose
CostRose CostingInteger
cost' [CostRose]
costs'
collectListLengths :: CostRose -> [Int]
collectListLengths :: CostRose -> [Int]
collectListLengths (CostRose CostingInteger
_ [CostRose]
costs) = [CostRose] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [CostRose]
costs Int -> [Int] -> [Int]
forall a. a -> [a] -> [a]
: (CostRose -> [Int]) -> [CostRose] -> [Int]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap CostRose -> [Int]
collectListLengths [CostRose]
costs
test_CostRoseListLengthsDistribution :: TestTree
test_CostRoseListLengthsDistribution :: TestTree
test_CostRoseListLengthsDistribution =
[Char] -> Property -> TestTree
forall a. Testable a => [Char] -> a -> TestTree
testProperty [Char]
"distribution of list lengths in CostRose values" (Property -> TestTree) -> Property -> TestTree
forall a b. (a -> b) -> a -> b
$
Int -> (CostRose -> Property) -> Property
forall prop. Testable prop => Int -> prop -> Property
BaseQC.withNumTests Int
1000 ((CostRose -> Property) -> Property)
-> (CostRose -> Property) -> Property
forall a b. (a -> b) -> a -> b
$ \CostRose
rose ->
let render :: a -> [Char]
render a
n
| a
n a -> a -> Bool
forall a. Ord a => a -> a -> Bool
<= a
10 = a -> [Char]
forall a. Show a => a -> [Char]
show a
n
| Bool
otherwise = a -> [Char]
forall a. Show a => a -> [Char]
show a
m [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
" < n <= " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ a -> [Char]
forall a. Show a => a -> [Char]
show (a
m a -> a -> a
forall a. Num a => a -> a -> a
+ a
10)
where
m :: a
m = [a] -> a
forall a. HasCallStack => [a] -> a
head ([a] -> a) -> [a] -> a
forall a b. (a -> b) -> a -> b
$ (a -> Bool) -> [a] -> [a]
forall a. (a -> Bool) -> [a] -> [a]
dropWhile (a -> a -> Bool
forall a. Ord a => a -> a -> Bool
< a
n) [a
10, a
20 ..]
in [Char] -> [[Char]] -> Bool -> Property
forall prop.
Testable prop =>
[Char] -> [[Char]] -> prop -> Property
tabulate [Char]
"n" ((Int -> [Char]) -> [Int] -> [[Char]]
forall a b. (a -> b) -> [a] -> [b]
map Int -> [Char]
forall {a}. (Show a, Ord a, Num a, Enum a) => a -> [Char]
render ([Int] -> [[Char]]) -> ([Int] -> [Int]) -> [Int] -> [[Char]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Int -> Bool) -> [Int] -> [Int]
forall a. (a -> Bool) -> [a] -> [a]
filter (Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= Int
0) ([Int] -> [[Char]]) -> [Int] -> [[Char]]
forall a b. (a -> b) -> a -> b
$ CostRose -> [Int]
collectListLengths CostRose
rose) Bool
True
test_genCostRoseSound :: TestTree
test_genCostRoseSound :: TestTree
test_genCostRoseSound =
[Char] -> Property -> TestTree
forall a. Testable a => [Char] -> a -> TestTree
testProperty [Char]
"genCostRose puts 100% of its input and nothing else into the output" (Property -> TestTree) -> Property -> TestTree
forall a b. (a -> b) -> a -> b
$
Int -> (NonEmptyList CostingInteger -> Property) -> Property
forall prop. Testable prop => Int -> prop -> Property
BaseQC.withNumTests Int
1000 ((NonEmptyList CostingInteger -> Property) -> Property)
-> (NonEmptyList CostingInteger -> Property) -> Property
forall a b. (a -> b) -> a -> b
$ \NonEmptyList CostingInteger
costs ->
Gen CostRose -> (CostRose -> Property) -> Property
forall a prop.
(Show a, Testable prop) =>
Gen a -> (a -> prop) -> Property
forAll (NonEmptyList CostingInteger -> Gen CostRose
genCostRose NonEmptyList CostingInteger
costs) ((CostRose -> Property) -> Property)
-> (CostRose -> Property) -> Property
forall a b. (a -> b) -> a -> b
$ \CostRose
rose ->
CostRose -> NonEmptyList CostingInteger
fromCostRose CostRose
rose
NonEmptyList CostingInteger
-> NonEmptyList CostingInteger -> Property
forall a. (Eq a, Show a) => a -> a -> Property
=== NonEmptyList CostingInteger
costs
test_flattenCostRoseSound :: TestTree
test_flattenCostRoseSound :: TestTree
test_flattenCostRoseSound =
[Char] -> Property -> TestTree
forall a. Testable a => [Char] -> a -> TestTree
testProperty [Char]
"flattenCostRose puts 100% of its input and nothing else into the output" (Property -> TestTree) -> Property -> TestTree
forall a b. (a -> b) -> a -> b
$
Int -> (CostRose -> Property) -> Property
forall prop. Testable prop => Int -> prop -> Property
BaseQC.withNumTests Int
1000 ((CostRose -> Property) -> Property)
-> (CostRose -> Property) -> Property
forall a b. (a -> b) -> a -> b
$ \CostRose
rose ->
(CostStream -> CostStream -> Bool)
-> CostStream -> CostStream -> Property
forall a. Show a => (a -> a -> Bool) -> a -> a -> Property
checkEqualsVia
CostStream -> CostStream -> Bool
eqCostStream
(CostRose -> CostStream
flattenCostRose CostRose
rose)
(NonEmptyList CostingInteger -> CostStream
fromCostList (NonEmptyList CostingInteger -> CostStream)
-> NonEmptyList CostingInteger -> CostStream
forall a b. (a -> b) -> a -> b
$ CostRose -> NonEmptyList CostingInteger
fromCostRose CostRose
rose)
test_flattenCostRoseHandlesBottom :: TestTree
test_flattenCostRoseHandlesBottom :: TestTree
test_flattenCostRoseHandlesBottom =
[Char] -> Property -> TestTree
forall a. Testable a => [Char] -> a -> TestTree
testProperty [Char]
"flattenCostRose handles bottom subtrees" (Property -> TestTree)
-> (([CostingInteger] -> [CostingInteger] -> Property) -> Property)
-> ([CostingInteger] -> [CostingInteger] -> Property)
-> TestTree
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int
-> ([CostingInteger] -> [CostingInteger] -> Property) -> Property
forall prop. Testable prop => Int -> prop -> Property
BaseQC.withNumTests Int
5000 (([CostingInteger] -> [CostingInteger] -> Property) -> TestTree)
-> ([CostingInteger] -> [CostingInteger] -> Property) -> TestTree
forall a b. (a -> b) -> a -> b
$ \[CostingInteger]
xs [CostingInteger]
ys ->
Gen CostRose -> (CostRose -> Bool) -> Property
forall a prop.
(Show a, Testable prop) =>
Gen a -> (a -> prop) -> Property
forAll (NonEmptyList CostingInteger -> Gen CostRose
genCostRose (NonEmptyList CostingInteger -> Gen CostRose)
-> ([CostingInteger] -> NonEmptyList CostingInteger)
-> [CostingInteger]
-> Gen CostRose
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [CostingInteger] -> NonEmptyList CostingInteger
forall a. [a] -> NonEmptyList a
NonEmpty ([CostingInteger] -> Gen CostRose)
-> [CostingInteger] -> Gen CostRose
forall a b. (a -> b) -> a -> b
$ [CostingInteger]
xs [CostingInteger] -> [CostingInteger] -> [CostingInteger]
forall a. [a] -> [a] -> [a]
++ (-CostingInteger
1) CostingInteger -> [CostingInteger] -> [CostingInteger]
forall a. a -> [a] -> [a]
: [CostingInteger]
ys) ((CostRose -> Bool) -> Property) -> (CostRose -> Bool) -> Property
forall a b. (a -> b) -> a -> b
$ \CostRose
rose ->
let spoilCostRose :: CostRose -> CostRose
spoilCostRose (CostRose CostingInteger
cost [CostRose]
forest) =
CostingInteger -> [CostRose] -> CostRose
CostRose CostingInteger
cost ([CostRose] -> CostRose) -> [CostRose] -> CostRose
forall a b. (a -> b) -> a -> b
$
if CostingInteger
cost CostingInteger -> CostingInteger -> Bool
forall a. Eq a => a -> a -> Bool
== -CostingInteger
1
then [CostRose
forall a. a
bottom]
else (CostRose -> CostRose) -> [CostRose] -> [CostRose]
forall a b. (a -> b) -> [a] -> [b]
map CostRose -> CostRose
spoilCostRose [CostRose]
forest
in CostingInteger -> NonEmptyList CostingInteger -> Bool
forall a. Eq a => a -> NonEmptyList a -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
elem (-CostingInteger
1) (NonEmptyList CostingInteger -> Bool)
-> (CostRose -> NonEmptyList CostingInteger) -> CostRose -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CostStream -> NonEmptyList CostingInteger
toCostList (CostStream -> NonEmptyList CostingInteger)
-> (CostRose -> CostStream)
-> CostRose
-> NonEmptyList CostingInteger
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CostRose -> CostStream
flattenCostRose (CostRose -> Bool) -> CostRose -> Bool
forall a b. (a -> b) -> a -> b
$ CostRose -> CostRose
spoilCostRose CostRose
rose
test_costsAreNeverNegative :: TestTree
test_costsAreNeverNegative :: TestTree
test_costsAreNeverNegative =
[Char] -> Property -> TestTree
forall a. Testable a => [Char] -> a -> TestTree
testProperty [Char]
"costs coming from 'memoryUsage' are never negative" (Property -> TestTree) -> Property -> TestTree
forall a b. (a -> b) -> a -> b
$
Int -> (Some (ValueOf DefaultUni) -> Bool) -> Property
forall prop. Testable prop => Int -> prop -> Property
BaseQC.withNumTests Int
1000 ((Some (ValueOf DefaultUni) -> Bool) -> Property)
-> (Some (ValueOf DefaultUni) -> Bool) -> Property
forall a b. (a -> b) -> a -> b
$ \(Some (ValueOf DefaultUni)
val :: Some (ValueOf DefaultUni)) ->
(CostingInteger -> Bool) -> NonEmptyList CostingInteger -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (CostingInteger -> CostingInteger -> Bool
forall a. Ord a => a -> a -> Bool
>= CostingInteger
0) (NonEmptyList CostingInteger -> Bool)
-> (CostRose -> NonEmptyList CostingInteger) -> CostRose -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CostStream -> NonEmptyList CostingInteger
toCostList (CostStream -> NonEmptyList CostingInteger)
-> (CostRose -> CostStream)
-> CostRose
-> NonEmptyList CostingInteger
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CostRose -> CostStream
flattenCostRose (CostRose -> Bool) -> CostRose -> Bool
forall a b. (a -> b) -> a -> b
$ Some (ValueOf DefaultUni) -> CostRose
forall a. ExMemoryUsage a => a -> CostRose
memoryUsage Some (ValueOf DefaultUni)
val
test_listMemoryUsage :: TestTree
test_listMemoryUsage :: TestTree
test_listMemoryUsage =
[Char] -> [TestTree] -> TestTree
testGroup
[Char]
"list memory usage"
[ [Char] -> Assertion -> TestTree
testCase [Char]
"counts spine elements across batch boundaries without forcing elements" (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$
[Int] -> (Int -> Assertion) -> Assertion
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [Int
0, Int
1, Int
99, Int
100, Int
101, Int
199, Int
200, Int
201] ((Int -> Assertion) -> Assertion)
-> (Int -> Assertion) -> Assertion
forall a b. (a -> b) -> a -> b
$ \Int
n ->
CostStream -> CostingInteger
sumCostStream (CostRose -> CostStream
flattenCostRose ([Int] -> CostRose
forall a. ExMemoryUsage a => a -> CostRose
memoryUsage (Int -> Int -> [Int]
forall a. Int -> a -> [a]
replicate Int
n Int
forall a. HasCallStack => a
undefined :: [Int])))
CostingInteger -> CostingInteger -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= Int -> CostingInteger
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
n
, [Char] -> Assertion -> TestTree
testCase [Char]
"the first batch does not force the remaining spine" (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$
case [Int] -> CostRose
forall a. ExMemoryUsage a => a -> CostRose
memoryUsage (Int -> Int -> [Int]
forall a. Int -> a -> [a]
replicate Int
100 Int
forall a. HasCallStack => a
undefined [Int] -> [Int] -> [Int]
forall a. [a] -> [a] -> [a]
++ [Int]
forall a. HasCallStack => a
undefined :: [Int]) of
CostRose CostingInteger
cost [CostRose]
_ -> CostingInteger
cost CostingInteger -> CostingInteger -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= CostingInteger
100
]
test_costing :: TestTree
test_costing :: TestTree
test_costing =
[Char] -> [TestTree] -> TestTree
testGroup
[Char]
"costing"
[ TestTree
test_magnitudes
, TestTree
test_CostStreamDistribution
, TestTree
test_toCostListRoundtrip
, TestTree
test_fromCostListRoundtrip
, TestTree
test_unconsCostRoundtrip
, TestTree
test_sumCostStreamIsSum
, TestTree
test_mapCostStreamIsMap
, TestTree
test_addCostStreamIsAdd
, TestTree
test_minCostStreamIsMin
, TestTree
test_zipCostStreamIsZip
, TestTree
test_mapCostStreamReasonableLength
, TestTree
test_addCostStreamReasonableLength
, TestTree
test_minCostStreamReasonableLength
, TestTree
test_zipCostStreamReasonableLength
, TestTree
test_mapCostStreamHandlesBottom
, TestTree
test_addCostStreamHandlesBottom
, TestTree
test_minCostStreamHandlesBottom
, TestTree
test_zipCostStreamHandlesBottom
, TestTree
test_flattenCostRoseIsLinear
, TestTree
test_CostRoseListLengthsDistribution
, TestTree
test_genCostRoseSound
, TestTree
test_flattenCostRoseSound
, TestTree
test_flattenCostRoseHandlesBottom
, TestTree
test_costsAreNeverNegative
, TestTree
test_listMemoryUsage
]