Skip to content

Commit 450bf5e

Browse files
committed
Make Ord instance Eq compatible
1 parent be721ad commit 450bf5e

File tree

3 files changed

+62
-5
lines changed

3 files changed

+62
-5
lines changed

Data/HashMap/Base.hs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -268,10 +268,12 @@ equal eqk eqv t1 t2 = go (toList' t1 []) (toList' t2 [])
268268
-- order of elements in 'Collision').
269269

270270
go (Leaf k1 l1 : tl1) (Leaf k2 l2 : tl2)
271-
| k1 == k2 && leafEq l1 l2
271+
| k1 == k2 &&
272+
leafEq l1 l2
272273
= go tl1 tl2
273274
go (Collision k1 ary1 : tl1) (Collision k2 ary2 : tl2)
274-
| k1 == k2 && A.length ary1 == A.length ary2 &&
275+
| k1 == k2 &&
276+
A.length ary1 == A.length ary2 &&
275277
isPermutationBy leafEq (A.toList ary1) (A.toList ary2)
276278
= go tl1 tl2
277279
go [] [] = True
@@ -287,6 +289,11 @@ instance Ord k => Ord1 (HashMap k) where
287289
liftCompare = cmp compare
288290
#endif
289291

292+
-- | The order is total.
293+
--
294+
-- /Note:/ Because the hash is not guaranteed to be stable across library
295+
-- versions, OSes, or architectures, neither is an actual order of elements in
296+
-- 'HashMap' or an result of `compare`.is stable.
290297
instance (Ord k, Ord v) => Ord (HashMap k v) where
291298
compare = cmp compare compare
292299

@@ -295,10 +302,14 @@ cmp :: (k -> k' -> Ordering) -> (v -> v' -> Ordering)
295302
cmp cmpk cmpv t1 t2 = go (toList' t1 []) (toList' t2 [])
296303
where
297304
go (Leaf k1 l1 : tl1) (Leaf k2 l2 : tl2)
298-
= compare k1 k2 `mappend` leafCompare l1 l2 `mappend` go tl1 tl2
305+
= compare k1 k2 `mappend`
306+
leafCompare l1 l2 `mappend`
307+
go tl1 tl2
299308
go (Collision k1 ary1 : tl1) (Collision k2 ary2 : tl2)
300-
= compare k1 k2 `mappend` compare (A.length ary1) (A.length ary2) `mappend`
301-
unorderedCompare leafCompare (A.toList ary1) (A.toList ary2)
309+
= compare k1 k2 `mappend`
310+
compare (A.length ary1) (A.length ary2) `mappend`
311+
unorderedCompare leafCompare (A.toList ary1) (A.toList ary2) `mappend`
312+
go tl1 tl2
302313
go (Leaf _ _ : _) (Collision _ _ : _) = LT
303314
go (Collision _ _ : _) (Leaf _ _ : _) = GT
304315
go [] [] = EQ

tests/HashMapProperties.hs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,26 @@ pOrd2 xs ys zs = case (compare x y, compare y z) of
5959
y = HM.fromList ys
6060
z = HM.fromList zs
6161

62+
pOrd3 :: [(Key, Int)] -> [(Key, Int)] -> Bool
63+
pOrd3 xs ys = case (compare x y, compare y x) of
64+
(EQ, EQ) -> True
65+
(LT, GT) -> True
66+
(GT, LT) -> True
67+
_ -> False
68+
where
69+
x = HM.fromList xs
70+
y = HM.fromList ys
71+
72+
pOrdEq :: [(Key, Int)] -> [(Key, Int)] -> Bool
73+
pOrdEq xs ys = case (compare x y, x == y) of
74+
(EQ, True) -> True
75+
(LT, False) -> True
76+
(GT, False) -> True
77+
_ -> False
78+
where
79+
x = HM.fromList xs
80+
y = HM.fromList ys
81+
6282
pReadShow :: [(Key, Int)] -> Bool
6383
pReadShow xs = M.fromList xs == read (show (M.fromList xs))
6484

@@ -275,6 +295,8 @@ tests =
275295
, testProperty "/=" pNeq
276296
, testProperty "compare reflexive" pOrd1
277297
, testProperty "compare transitive" pOrd2
298+
, testProperty "compare antisymmetric" pOrd3
299+
, testProperty "Ord => Eq" pOrdEq
278300
, testProperty "Read/Show" pReadShow
279301
, testProperty "Functor" pFunctor
280302
, testProperty "Foldable" pFoldable

tests/HashSetProperties.hs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,26 @@ pOrd2 xs ys zs = case (compare x y, compare y z) of
5353
y = S.fromList ys
5454
z = S.fromList zs
5555

56+
pOrd3 :: [Key] -> [Key] -> Bool
57+
pOrd3 xs ys = case (compare x y, compare y x) of
58+
(EQ, EQ) -> True
59+
(LT, GT) -> True
60+
(GT, LT) -> True
61+
_ -> False
62+
where
63+
x = S.fromList xs
64+
y = S.fromList ys
65+
66+
pOrdEq :: [Key] -> [Key] -> Bool
67+
pOrdEq xs ys = case (compare x y, x == y) of
68+
(EQ, True) -> True
69+
(LT, False) -> True
70+
(GT, False) -> True
71+
_ -> False
72+
where
73+
x = S.fromList xs
74+
y = S.fromList ys
75+
5676
pReadShow :: [Key] -> Bool
5777
pReadShow xs = Set.fromList xs == read (show (Set.fromList xs))
5878

@@ -155,6 +175,10 @@ tests =
155175
[ testProperty "==" pEq
156176
, testProperty "Permutation ==" pPermutationEq
157177
, testProperty "/=" pNeq
178+
, testProperty "compare reflexive" pOrd1
179+
, testProperty "compare transitive" pOrd2
180+
, testProperty "compare antisymmetric" pOrd3
181+
, testProperty "Ord => Eq" pOrdEq
158182
, testProperty "Read/Show" pReadShow
159183
, testProperty "Foldable" pFoldable
160184
, testProperty "Hashable" pHashable

0 commit comments

Comments
 (0)