module Test.FitSpec.Mutable
( Mutable (..)
, mutiersEq
)
where
import Test.LeanCheck
import Data.List (intercalate, delete)
import Data.Maybe
import Test.LeanCheck.Error (errorToNothing)
import Data.Ratio (Ratio)
import Data.Word (Word)
class Mutable a where
mutiers :: a -> [[a]]
mutants :: a -> [a]
mutiers = (a -> [a]) -> [a] -> [[a]]
forall a b. (a -> b) -> [a] -> [b]
map (a -> [a] -> [a]
forall a. a -> [a] -> [a]
:[]) ([a] -> [[a]]) -> (a -> [a]) -> a -> [[a]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> [a]
forall a. Mutable a => a -> [a]
mutants
mutants = [[a]] -> [a]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat ([[a]] -> [a]) -> (a -> [[a]]) -> a -> [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> [[a]]
forall a. Mutable a => a -> [[a]]
mutiers
{-# MINIMAL mutants | mutiers #-}
mutiersEq :: (Listable a, Eq a) => a -> [[a]]
mutiersEq :: a -> [[a]]
mutiersEq a
x = [a
x] [a] -> [[a]] -> [[a]]
forall a. a -> [a] -> [a]
: a -> [[a]] -> [[a]]
forall a. Eq a => a -> [[a]] -> [[a]]
deleteT a
x [[a]]
forall a. Listable a => [[a]]
tiers
instance Mutable () where mutiers :: () -> [[()]]
mutiers = () -> [[()]]
forall a. (Listable a, Eq a) => a -> [[a]]
mutiersEq
instance Mutable Int where mutiers :: Int -> [[Int]]
mutiers = Int -> [[Int]]
forall a. (Listable a, Eq a) => a -> [[a]]
mutiersEq
instance Mutable Integer where mutiers :: Integer -> [[Integer]]
mutiers = Integer -> [[Integer]]
forall a. (Listable a, Eq a) => a -> [[a]]
mutiersEq
instance Mutable Char where mutiers :: Char -> [[Char]]
mutiers = Char -> [[Char]]
forall a. (Listable a, Eq a) => a -> [[a]]
mutiersEq
instance Mutable Bool where mutiers :: Bool -> [[Bool]]
mutiers = Bool -> [[Bool]]
forall a. (Listable a, Eq a) => a -> [[a]]
mutiersEq
instance (Eq a, Listable a) => Mutable [a] where mutiers :: [a] -> [[[a]]]
mutiers = [a] -> [[[a]]]
forall a. (Listable a, Eq a) => a -> [[a]]
mutiersEq
instance (Eq a, Listable a) => Mutable (Maybe a) where mutiers :: Maybe a -> [[Maybe a]]
mutiers = Maybe a -> [[Maybe a]]
forall a. (Listable a, Eq a) => a -> [[a]]
mutiersEq
instance (Eq a, Listable a, Eq b, Listable b) => Mutable (Either a b)
where mutiers :: Either a b -> [[Either a b]]
mutiers = Either a b -> [[Either a b]]
forall a. (Listable a, Eq a) => a -> [[a]]
mutiersEq
instance (Eq a, Listable a, Integral a) => Mutable (Ratio a)
where mutiers :: Ratio a -> [[Ratio a]]
mutiers = Ratio a -> [[Ratio a]]
forall a. (Listable a, Eq a) => a -> [[a]]
mutiersEq
instance Mutable Float where mutiers :: Float -> [[Float]]
mutiers = Float -> [[Float]]
forall a. (Listable a, Eq a) => a -> [[a]]
mutiersEq
instance Mutable Double where mutiers :: Double -> [[Double]]
mutiers = Double -> [[Double]]
forall a. (Listable a, Eq a) => a -> [[a]]
mutiersEq
instance Mutable Ordering where mutiers :: Ordering -> [[Ordering]]
mutiers = Ordering -> [[Ordering]]
forall a. (Listable a, Eq a) => a -> [[a]]
mutiersEq
instance Mutable Word where mutiers :: Word -> [[Word]]
mutiers = Word -> [[Word]]
forall a. (Listable a, Eq a) => a -> [[a]]
mutiersEq
mut :: Eq a => (a -> b) -> (a,b) -> (a -> b)
mut :: (a -> b) -> (a, b) -> a -> b
mut a -> b
f (a
x',b
fx') = \a
x -> if a
x a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
x'
then b
fx'
else a -> b
f a
x
mutate :: Eq a => (a -> b) -> [(a,b)] -> (a -> b)
mutate :: (a -> b) -> [(a, b)] -> a -> b
mutate a -> b
f [(a, b)]
ms = ((a, b) -> (a -> b) -> a -> b) -> (a -> b) -> [(a, b)] -> a -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (((a -> b) -> (a, b) -> a -> b) -> (a, b) -> (a -> b) -> a -> b
forall a b c. (a -> b -> c) -> b -> a -> c
flip (a -> b) -> (a, b) -> a -> b
forall a b. Eq a => (a -> b) -> (a, b) -> a -> b
mut) a -> b
f [(a, b)]
ms
mutationsFor :: Mutable b => (a->b) -> a -> [[(a,b)]]
mutationsFor :: (a -> b) -> a -> [[(a, b)]]
mutationsFor a -> b
f a
x = case b -> Maybe b
forall a. a -> Maybe a
errorToNothing (a -> b
f a
x) of
Maybe b
Nothing -> []
Just b
fx -> ((,) a
x) (b -> (a, b)) -> [[b]] -> [[(a, b)]]
forall a b. (a -> b) -> [[a]] -> [[b]]
`mapT` [[b]] -> [[b]]
forall a. [a] -> [a]
tail (b -> [[b]]
forall a. Mutable a => a -> [[a]]
mutiers b
fx)
tiersMutantsOn :: (Eq a, Mutable b) => (a->b) -> [a] -> [[a->b]]
tiersMutantsOn :: (a -> b) -> [a] -> [[a -> b]]
tiersMutantsOn a -> b
f [a]
xs = (a -> b) -> [(a, b)] -> a -> b
forall a b. Eq a => (a -> b) -> [(a, b)] -> a -> b
mutate a -> b
f ([(a, b)] -> a -> b) -> [[[(a, b)]]] -> [[a -> b]]
forall a b. (a -> b) -> [[a]] -> [[b]]
`mapT` [[[(a, b)]]] -> [[[(a, b)]]]
forall a. [[[a]]] -> [[[a]]]
products ((a -> [[(a, b)]]) -> [a] -> [[[(a, b)]]]
forall a b. (a -> b) -> [a] -> [b]
map ((a -> b) -> a -> [[(a, b)]]
forall b a. Mutable b => (a -> b) -> a -> [[(a, b)]]
mutationsFor a -> b
f) [a]
xs)
instance (Eq a, Listable a, Mutable b) => Mutable (a -> b) where
mutiers :: (a -> b) -> [[a -> b]]
mutiers a -> b
f = (a -> b) -> [a] -> [[a -> b]]
forall a b. (Eq a, Mutable b) => (a -> b) -> [a] -> [[a -> b]]
tiersMutantsOn a -> b
f ([a] -> [[a -> b]]) -> [[[a]]] -> [[a -> b]]
forall a b. (a -> [[b]]) -> [[a]] -> [[b]]
`concatMapT` [[a]] -> [[[a]]]
forall a. [[a]] -> [[[a]]]
setsOf [[a]]
forall a. Listable a => [[a]]
tiers
instance (Mutable a, Mutable b) => Mutable (a,b) where
mutiers :: (a, b) -> [[(a, b)]]
mutiers (a
f,b
g) = a -> [[a]]
forall a. Mutable a => a -> [[a]]
mutiers a
f [[a]] -> [[b]] -> [[(a, b)]]
forall a b. [[a]] -> [[b]] -> [[(a, b)]]
>< b -> [[b]]
forall a. Mutable a => a -> [[a]]
mutiers b
g
instance (Mutable a, Mutable b, Mutable c) => Mutable (a,b,c) where
mutiers :: (a, b, c) -> [[(a, b, c)]]
mutiers (a
f,b
g,c
h) = (a -> (b, c) -> (a, b, c)) -> [[a]] -> [[(b, c)]] -> [[(a, b, c)]]
forall a b c. (a -> b -> c) -> [[a]] -> [[b]] -> [[c]]
productWith (\a
f' (b
g',c
h') -> (a
f',b
g',c
h'))
(a -> [[a]]
forall a. Mutable a => a -> [[a]]
mutiers a
f) ((b, c) -> [[(b, c)]]
forall a. Mutable a => a -> [[a]]
mutiers (b
g,c
h))
instance (Mutable a, Mutable b, Mutable c, Mutable d)
=> Mutable (a,b,c,d) where
mutiers :: (a, b, c, d) -> [[(a, b, c, d)]]
mutiers (a
f,b
g,c
h,d
i) = (a -> (b, c, d) -> (a, b, c, d))
-> [[a]] -> [[(b, c, d)]] -> [[(a, b, c, d)]]
forall a b c. (a -> b -> c) -> [[a]] -> [[b]] -> [[c]]
productWith (\a
f' (b
g',c
h',d
i') -> (a
f',b
g',c
h',d
i'))
(a -> [[a]]
forall a. Mutable a => a -> [[a]]
mutiers a
f) ((b, c, d) -> [[(b, c, d)]]
forall a. Mutable a => a -> [[a]]
mutiers (b
g,c
h,d
i))
instance (Mutable a, Mutable b, Mutable c, Mutable d, Mutable e)
=> Mutable (a,b,c,d,e) where
mutiers :: (a, b, c, d, e) -> [[(a, b, c, d, e)]]
mutiers (a
f,b
g,c
h,d
i,e
j) = (a -> (b, c, d, e) -> (a, b, c, d, e))
-> [[a]] -> [[(b, c, d, e)]] -> [[(a, b, c, d, e)]]
forall a b c. (a -> b -> c) -> [[a]] -> [[b]] -> [[c]]
productWith (\a
f' (b
g',c
h',d
i',e
j') -> (a
f',b
g',c
h',d
i',e
j'))
(a -> [[a]]
forall a. Mutable a => a -> [[a]]
mutiers a
f) ((b, c, d, e) -> [[(b, c, d, e)]]
forall a. Mutable a => a -> [[a]]
mutiers (b
g,c
h,d
i,e
j))
instance (Mutable a, Mutable b, Mutable c, Mutable d, Mutable e, Mutable f)
=> Mutable (a,b,c,d,e,f) where
mutiers :: (a, b, c, d, e, f) -> [[(a, b, c, d, e, f)]]
mutiers (a
f,b
g,c
h,d
i,e
j,f
k) = (a -> (b, c, d, e, f) -> (a, b, c, d, e, f))
-> [[a]] -> [[(b, c, d, e, f)]] -> [[(a, b, c, d, e, f)]]
forall a b c. (a -> b -> c) -> [[a]] -> [[b]] -> [[c]]
productWith (\a
f' (b
g',c
h',d
i',e
j',f
k') ->
(a
f',b
g',c
h',d
i',e
j',f
k'))
(a -> [[a]]
forall a. Mutable a => a -> [[a]]
mutiers a
f) ((b, c, d, e, f) -> [[(b, c, d, e, f)]]
forall a. Mutable a => a -> [[a]]
mutiers (b
g,c
h,d
i,e
j,f
k))