The first solution that comes to my mind is QuickCheck .
quickCheck $ \(x, y, z) -> fx (fyz) == f (fxy) z quickCheck $ \(x, y) -> fxy == fyx
where f
is the function we are testing. This will prove neither associativity nor commutativity; it's just the easiest way to write the brute force solution you were thinking about. The advantage of QuickCheck is its ability to select test parameters, which we hope will be angular for the tested code.
An isAssociative
that you request can be defined as
isAssociative :: (Arbitrary t, Show t, Eq t) => (t -> t -> t) -> IO () isAssociative f = quickCheck $ \(x, y, z) -> fx (fyz) == f (fxy) z
In IO
, because QuickCheck picks random random cases.
Jan
source share