Skip to content

Commit

Permalink
Resolve #67. Sort HashSet and HashMap Exprs
Browse files Browse the repository at this point in the history
  • Loading branch information
phadej committed Jan 10, 2023
1 parent 734ac75 commit 53ec7f5
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 17 deletions.
11 changes: 11 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
## 0.3

- Breaking change:
Make HashSet and HashMap ToExpr instances sort the resulting
lists of expressions.
This makes the results deterministic.
... but your golden files will need adjustment.
https://github.com/haskellari/tree-diff/issues/67

- Add `Ord Expr` and `Ord OMap` instances

## 0.2.2

- Add instances for base and primitive's `ByteArray`s.
Expand Down
14 changes: 7 additions & 7 deletions fixtures/HashSet.expr
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
HS.fromList
[
"az",
"dz",
"ax",
"ay",
"az",
"bx",
"by",
"bz",
"cx",
"cz",
"dy",
"bx",
"cy",
"by",
"cz",
"dx",
"ay"]
"dy",
"dz"]
5 changes: 3 additions & 2 deletions src/Data/TreeDiff/Class.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ module Data.TreeDiff.Class (
) where

import Data.Foldable (toList)
import Data.List (sort)
import Data.List.Compat (uncons)
import Data.Proxy (Proxy (..))
import GHC.Generics
Expand Down Expand Up @@ -550,9 +551,9 @@ instance ToExpr a => ToExpr (Hashed a) where
-------------------------------------------------------------------------------

instance (ToExpr k, ToExpr v) => ToExpr (HM.HashMap k v) where
toExpr x = App "HM.fromList" [ toExpr $ HM.toList x ]
toExpr x = App "HM.fromList" [ Lst $ sort $ map toExpr $ HM.toList x ]
instance (ToExpr k) => ToExpr (HS.HashSet k) where
toExpr x = App "HS.fromList" [ toExpr $ HS.toList x ]
toExpr x = App "HS.fromList" [ Lst $ sort $ map toExpr $ HS.toList x ]

-------------------------------------------------------------------------------
-- aeson
Expand Down
2 changes: 1 addition & 1 deletion src/Data/TreeDiff/Expr.hs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ data Expr
= App ConstructorName [Expr] -- ^ application
| Rec ConstructorName (OMap FieldName Expr) -- ^ record constructor
| Lst [Expr] -- ^ list constructor
deriving (Eq, Show)
deriving (Eq, Ord, Show)

instance NFData Expr where
rnf (App n es) = rnf n `seq` rnf es
Expand Down
10 changes: 4 additions & 6 deletions src/Data/TreeDiff/OMap.hs
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,10 @@ instance (Show k, Show v) => Show (OMap k v) where
-- False
--
instance (Eq k, Eq v) => Eq (OMap k v) where
xs == ys = go (toAscList xs) (toAscList ys) where
go [] [] = True
go _ [] = False
go [] _ = False
go ((k1, v1) : kvs1) ((k2, v2) : kvs2) =
k1 == k2 && v1 == v2 && go kvs1 kvs2
xs == ys = toAscList xs == toAscList ys

instance (Ord k, Ord v) => Ord (OMap k v) where
compare xs ys = compare (toAscList xs) (toAscList ys)

-------------------------------------------------------------------------------
-- deepseq
Expand Down
2 changes: 1 addition & 1 deletion tree-diff.cabal
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cabal-version: 2.2
name: tree-diff
version: 0.2.2
version: 0.3
synopsis: Diffing of (expression) trees.
category: Data, Testing
description:
Expand Down

0 comments on commit 53ec7f5

Please sign in to comment.