Visitar URL original
Dhall.Map: Reflect original key ordering in Ord instance by sjakobi · Pull Request #1050 · dhall-lang/dhall-haskell · GitHub
Skip to content

Dhall.Map: Reflect original key ordering in Ord instance - #1050

Merged
sjakobi merged 4 commits into
masterfrom
sjakobi/ord-map
Jul 2, 2019
Merged

sjakobi merged 4 commits into
masterfrom
sjakobi/ord-map

Conversation

@sjakobi

@sjakobi sjakobi commented Jul 2, 2019

Copy link
Copy Markdown
Collaborator

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)

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)

@EggBaconAndSpam EggBaconAndSpam left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

{-| A `Map` that remembers the original ordering of keys
This is primarily used so that formatting preserves field order
This is done primarily to avoid a dependency on @insert-ordered-containers@
and also to improve performance
-}

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.

@sjakobi

sjakobi commented Jul 2, 2019

Copy link
Copy Markdown
Collaborator Author

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 Data.Maps.

@sjakobi sjakobi changed the title Dhall.Map: Reflect key ordering in Ord instance Dhall.Map: Reflect original key ordering in Ord instance Jul 2, 2019
@EggBaconAndSpam

Copy link
Copy Markdown
Collaborator

Yes, that makes sense! I got confused because I thought toList to be the one from Data.Map.

Maybe it would be a good idea to add comments to the typeclass instances clearly describing their behaviour?

Comment thread dhall/src/Dhall/Map.hs
-}
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)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you could accomplish the same thing more efficiently by a slight modification of the original code:

Suggested change
compare m1 m2 = compare (toList m1) (toList m2)
compare m1 m2 = compare ksL ksR <> compare mL mR

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, you're right. Never mind, then

@sjakobi
sjakobi merged commit 51a020e into master Jul 2, 2019
@sjakobi
sjakobi deleted the sjakobi/ord-map branch July 2, 2019 17:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants