{-# OPTIONS_HADDOCK not-home #-}
module Hedgehog.Internal.Shrink (
towards
, towardsFloat
, list
, halves
, removes
, consNub
) where
towards :: Integral a => a -> a -> [a]
towards :: a -> a -> [a]
towards a
destination a
x =
if a
destination a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
x then
[]
else
let
diff :: a
diff =
(a
x a -> a -> a
forall a. Integral a => a -> a -> a
`quot` a
2) a -> a -> a
forall a. Num a => a -> a -> a
- (a
destination a -> a -> a
forall a. Integral a => a -> a -> a
`quot` a
2)
in
a
destination a -> [a] -> [a]
forall a. Eq a => a -> [a] -> [a]
`consNub` (a -> a) -> [a] -> [a]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (a
x a -> a -> a
forall a. Num a => a -> a -> a
-) (a -> [a]
forall a. Integral a => a -> [a]
halves a
diff)
towardsFloat :: RealFloat a => a -> a -> [a]
towardsFloat :: a -> a -> [a]
towardsFloat a
destination a
x =
if a
destination a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
x then
[]
else
let
diff :: a
diff =
a
x a -> a -> a
forall a. Num a => a -> a -> a
- a
destination
ok :: a -> Bool
ok a
y =
a
y a -> a -> Bool
forall a. Eq a => a -> a -> Bool
/= a
x Bool -> Bool -> Bool
&& Bool -> Bool
not (a -> Bool
forall a. RealFloat a => a -> Bool
isNaN a
y) Bool -> Bool -> Bool
&& Bool -> Bool
not (a -> Bool
forall a. RealFloat a => a -> Bool
isInfinite a
y)
in
(a -> Bool) -> [a] -> [a]
forall a. (a -> Bool) -> [a] -> [a]
takeWhile a -> Bool
ok ([a] -> [a]) -> ([a] -> [a]) -> [a] -> [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
(a -> a) -> [a] -> [a]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (a
x a -> a -> a
forall a. Num a => a -> a -> a
-) ([a] -> [a]) -> [a] -> [a]
forall a b. (a -> b) -> a -> b
$
(a -> a) -> a -> [a]
forall a. (a -> a) -> a -> [a]
iterate (a -> a -> a
forall a. Fractional a => a -> a -> a
/ a
2) a
diff
list :: [a] -> [[a]]
list :: [a] -> [[a]]
list [a]
xs =
(Int -> [[a]]) -> [Int] -> [[a]]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap
(\Int
k -> Int -> [a] -> [[a]]
forall a. Int -> [a] -> [[a]]
removes Int
k [a]
xs)
(Int -> [Int]
forall a. Integral a => a -> [a]
halves (Int -> [Int]) -> Int -> [Int]
forall a b. (a -> b) -> a -> b
$ [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [a]
xs)
removes :: Int -> [a] -> [[a]]
removes :: Int -> [a] -> [[a]]
removes Int
k0 [a]
xs0 =
let
loop :: Int -> Int -> [a] -> [[a]]
loop Int
k Int
n [a]
xs =
let
([a]
hd, [a]
tl) =
Int -> [a] -> ([a], [a])
forall a. Int -> [a] -> ([a], [a])
splitAt Int
k [a]
xs
in
if Int
k Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
n then
[]
else if [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [a]
tl then
[[]]
else
[a]
tl [a] -> [[a]] -> [[a]]
forall a. a -> [a] -> [a]
: ([a] -> [a]) -> [[a]] -> [[a]]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ([a]
hd [a] -> [a] -> [a]
forall a. [a] -> [a] -> [a]
++) (Int -> Int -> [a] -> [[a]]
loop Int
k (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
k) [a]
tl)
in
Int -> Int -> [a] -> [[a]]
forall a. Int -> Int -> [a] -> [[a]]
loop Int
k0 ([a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [a]
xs0) [a]
xs0
halves :: Integral a => a -> [a]
halves :: a -> [a]
halves =
(a -> Bool) -> [a] -> [a]
forall a. (a -> Bool) -> [a] -> [a]
takeWhile (a -> a -> Bool
forall a. Eq a => a -> a -> Bool
/= a
0) ([a] -> [a]) -> (a -> [a]) -> a -> [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> a) -> a -> [a]
forall a. (a -> a) -> a -> [a]
iterate (a -> a -> a
forall a. Integral a => a -> a -> a
`quot` a
2)
consNub :: Eq a => a -> [a] -> [a]
consNub :: a -> [a] -> [a]
consNub a
x [a]
ys0 =
case [a]
ys0 of
[] ->
a
x a -> [a] -> [a]
forall a. a -> [a] -> [a]
: []
a
y : [a]
ys ->
if a
x a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
y then
a
y a -> [a] -> [a]
forall a. a -> [a] -> [a]
: [a]
ys
else
a
x a -> [a] -> [a]
forall a. a -> [a] -> [a]
: a
y a -> [a] -> [a]
forall a. a -> [a] -> [a]
: [a]
ys