Repository navigation
Dhall.Map: Reflect original key ordering in Ord instance - #1050
Conversation
This definition is similar to the one in Data.Map:
instance (Ord k, Ord v) => Ord (Map k v) where
compare m1 m2 = compare (toAscList m1) (toAscList m2)
There was a problem hiding this comment.
As far as I understand it the current behaviour of the Ord instance (as well as fromList and toList) is the raison d'etre of Dhall.Map. At least that's how I interpret the following comment:
dhall-haskell/dhall/src/Dhall/Map.hs
Lines 64 to 70 in 1a40b9f
I haven't worked with Dhall.Map before though and might be getting this wrong!
edit: Didn't expect the scary looking red icon! This should probably have been a plain comment instead of a request for changes.
|
Ah, maybe I should have expressed myself more clearly. With this change, the original order of the keys gains importance. Previously, this ordering was only considered after comparing the internal |
|
Yes, that makes sense! I got confused because I thought Maybe it would be a good idea to add comments to the typeclass instances clearly describing their behaviour? |
| -} | ||
| instance (Ord k, Ord v) => Ord (Map k v) where | ||
| compare (Map mL ksL) (Map mR ksR) = compare mL mR <> compare ksL ksR | ||
| compare m1 m2 = compare (toList m1) (toList m2) |
There was a problem hiding this comment.
I think you could accomplish the same thing more efficiently by a slight modification of the original code:
| compare m1 m2 = compare (toList m1) (toList m2) | |
| compare m1 m2 = compare ksL ksR <> compare mL mR |
There was a problem hiding this comment.
I don't believe so. Take
fromList [('B',1),('A',2)] < fromList [('B',2),('A',1)]
That's True with my version, but False with yours.
There was a problem hiding this comment.
Ah, you're right. Never mind, then
This definition is similar to the one in Data.Map: