Skip to content

Commit 53ec7f5

Browse files
committed
Resolve #67. Sort HashSet and HashMap Exprs
1 parent 734ac75 commit 53ec7f5

File tree

6 files changed

+27
-17
lines changed

6 files changed

+27
-17
lines changed

ChangeLog.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
## 0.3
2+
3+
- Breaking change:
4+
Make HashSet and HashMap ToExpr instances sort the resulting
5+
lists of expressions.
6+
This makes the results deterministic.
7+
... but your golden files will need adjustment.
8+
https://github.com/haskellari/tree-diff/issues/67
9+
10+
- Add `Ord Expr` and `Ord OMap` instances
11+
112
## 0.2.2
213

314
- Add instances for base and primitive's `ByteArray`s.

fixtures/HashSet.expr

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
HS.fromList
22
[
3-
"az",
4-
"dz",
53
"ax",
4+
"ay",
5+
"az",
6+
"bx",
7+
"by",
68
"bz",
79
"cx",
8-
"cz",
9-
"dy",
10-
"bx",
1110
"cy",
12-
"by",
11+
"cz",
1312
"dx",
14-
"ay"]
13+
"dy",
14+
"dz"]

src/Data/TreeDiff/Class.hs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ module Data.TreeDiff.Class (
2121
) where
2222

2323
import Data.Foldable (toList)
24+
import Data.List (sort)
2425
import Data.List.Compat (uncons)
2526
import Data.Proxy (Proxy (..))
2627
import GHC.Generics
@@ -550,9 +551,9 @@ instance ToExpr a => ToExpr (Hashed a) where
550551
-------------------------------------------------------------------------------
551552

552553
instance (ToExpr k, ToExpr v) => ToExpr (HM.HashMap k v) where
553-
toExpr x = App "HM.fromList" [ toExpr $ HM.toList x ]
554+
toExpr x = App "HM.fromList" [ Lst $ sort $ map toExpr $ HM.toList x ]
554555
instance (ToExpr k) => ToExpr (HS.HashSet k) where
555-
toExpr x = App "HS.fromList" [ toExpr $ HS.toList x ]
556+
toExpr x = App "HS.fromList" [ Lst $ sort $ map toExpr $ HS.toList x ]
556557

557558
-------------------------------------------------------------------------------
558559
-- aeson

src/Data/TreeDiff/Expr.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ data Expr
3535
= App ConstructorName [Expr] -- ^ application
3636
| Rec ConstructorName (OMap FieldName Expr) -- ^ record constructor
3737
| Lst [Expr] -- ^ list constructor
38-
deriving (Eq, Show)
38+
deriving (Eq, Ord, Show)
3939

4040
instance NFData Expr where
4141
rnf (App n es) = rnf n `seq` rnf es

src/Data/TreeDiff/OMap.hs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,10 @@ instance (Show k, Show v) => Show (OMap k v) where
6161
-- False
6262
--
6363
instance (Eq k, Eq v) => Eq (OMap k v) where
64-
xs == ys = go (toAscList xs) (toAscList ys) where
65-
go [] [] = True
66-
go _ [] = False
67-
go [] _ = False
68-
go ((k1, v1) : kvs1) ((k2, v2) : kvs2) =
69-
k1 == k2 && v1 == v2 && go kvs1 kvs2
64+
xs == ys = toAscList xs == toAscList ys
65+
66+
instance (Ord k, Ord v) => Ord (OMap k v) where
67+
compare xs ys = compare (toAscList xs) (toAscList ys)
7068

7169
-------------------------------------------------------------------------------
7270
-- deepseq

tree-diff.cabal

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
cabal-version: 2.2
22
name: tree-diff
3-
version: 0.2.2
3+
version: 0.3
44
synopsis: Diffing of (expression) trees.
55
category: Data, Testing
66
description:

0 commit comments

Comments
 (0)