Skip to content

Commit 1d4e528

Browse files
committed
Add law that tests that subtraction is the same as adding the negation of a number
1 parent de2ab87 commit 1d4e528

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

quickcheck-classes-base/changelog.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ and this project adheres to the [Haskell Package Versioning Policy](https://pvp.
1010
from `quickcheck-classes` and depend on this library that
1111
have a more minimal dependency footprint.
1212
- Add laws for left rotate and right rotate.
13-
13+
- Add law that checks that subtraction is the same thing as
14+
adding the negation of a number.

quickcheck-classes-base/src/Test/QuickCheck/Classes/Num.hs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ import Test.QuickCheck.Classes.Internal (Laws(..), myForAllShrink)
3636
-- @a * 0 ≡ 0@
3737
-- [/Additive Inverse/]
3838
-- @'negate' a '+' a ≡ 0@
39+
-- [/Subtraction/]
40+
-- @a '+' 'negate' b ≡ a '-' b@
3941
numLaws :: (Num a, Eq a, Arbitrary a, Show a) => Proxy a -> Laws
4042
numLaws p = Laws "Num"
4143
[ ("Additive Commutativity", numCommutativePlus p)
@@ -49,6 +51,7 @@ numLaws p = Laws "Num"
4951
, ("Multiplicative Left Annihilation", numLeftAnnihilation p)
5052
, ("Multiplicative Right Annihilation", numRightAnnihilation p)
5153
, ("Additive Inverse", numAdditiveInverse p)
54+
, ("Subtraction", numSubtraction p)
5255
]
5356

5457
numLeftMultiplicationDistributes :: forall a. (Num a, Eq a, Arbitrary a, Show a) => Proxy a -> Property
@@ -138,3 +141,11 @@ numAdditiveInverse _ = myForAllShrink True (const True)
138141
(\a -> (-a) + a)
139142
"0"
140143
(const 0)
144+
145+
numSubtraction :: forall a. (Num a, Eq a, Arbitrary a, Show a) => Proxy a -> Property
146+
numSubtraction _ = myForAllShrink True (const True)
147+
(\(a :: a, b :: a) -> ["a = " ++ show a, "b = " ++ show b])
148+
"a + negate b"
149+
(\(a,b) -> a + negate b)
150+
"a - b"
151+
(\(a,b) -> a - b)

0 commit comments

Comments
 (0)